예제 #1
0
파일: handler.c 프로젝트: ttuna/mongrel2
void Handler_notify_credits(Handler *handler, int id, int credits)
{
    void *socket = handler->send_socket;
    assert(socket && "Socket can't be NULL");
    tns_outbuf outbuf = {.buffer = NULL};
    bstring payload = NULL;

    if(handler->protocol == HANDLER_PROTO_TNET) {
        int header_start = tns_render_request_start(&outbuf);
        bstring creditsstr = bformat("%d", credits);
        tns_render_hash_pair(&outbuf, &HTTP_METHOD, &JSON_METHOD);
        tns_render_hash_pair(&outbuf, &DOWNLOAD_CREDITS, creditsstr);
        bdestroy(creditsstr);
        tns_outbuf_clamp(&outbuf, header_start);

        payload = bformat("%s %d @* %s%d:%s,",
                bdata(handler->send_ident), id,
                bdata(tns_outbuf_to_bstring(&outbuf)),
                blength(&CREDITS_MSG), bdata(&CREDITS_MSG));
    } else {
        bstring headers = bfromcstralloc(PAYLOAD_GUESS, "{");

        bcatcstr(headers, "\"METHOD\":\"JSON\",\"");
        bconcat(headers, &DOWNLOAD_CREDITS);
        bcatcstr(headers, "\":\"");
        bformata(headers, "%d", credits);
        bcatcstr(headers, "\"}");

        payload = bformat("%s %d @* %d:%s,%d:%s,",
                bdata(handler->send_ident), id,
                blength(headers), bdata(headers),
                blength(&CREDITS_MSG), bdata(&CREDITS_MSG));

        bdestroy(headers);
    }

    check(payload != NULL, "Failed to make the payload for credits.");

    if(Handler_deliver(socket, bdata(payload), blength(payload)) == -1) {
        log_err("Can't tell handler %d giving credits.", id);
    }

error: //fallthrough
    if(payload) free(payload);
    if(outbuf.buffer) free(outbuf.buffer);
}
예제 #2
0
        return &XML_METHOD;
    } else {
        return req->request_method;
    }
}

bstring Request_to_tnetstring(Request *req, bstring uuid, int fd, const char *buf, size_t len, Connection *conn, hash_t *altheaders)
{
    tns_outbuf outbuf = {.buffer = NULL};
    bstring method = request_determine_method(req);
    check(method, "Impossible, got an invalid request method.");

    uint32_t id = Register_id_for_fd(fd);
    check_debug(id != UINT32_MAX, "Asked to generate a payload for a fd that doesn't exist: %d", fd);

    int header_start = tns_render_request_start(&outbuf);
    check(header_start != -1, "Failed to initialize outbuf.");

    if(altheaders != NULL) {
        check(tns_render_request_headers(&outbuf, altheaders) == 0,
                "Failed to render headers to a tnetstring.");
    } else {
        check(tns_render_request_headers(&outbuf, req->headers) == 0,
                "Failed to render headers to a tnetstring.");

        if(req->path) tns_render_hash_pair(&outbuf, &HTTP_PATH, req->path);
        if(req->version) tns_render_hash_pair(&outbuf, &HTTP_VERSION, req->version);
        if(req->uri) tns_render_hash_pair(&outbuf, &HTTP_URI, req->uri);
        if(req->query_string) tns_render_hash_pair(&outbuf, &HTTP_QUERY, req->query_string);
        if(req->fragment) tns_render_hash_pair(&outbuf, &HTTP_FRAGMENT, req->fragment);
        if(req->pattern) tns_render_hash_pair(&outbuf, &HTTP_PATTERN, req->pattern);