Esempio n. 1
0
int parse_request(struct buf *bufp) {
    
    char *p;
    
    p = bufp->parse_p;
    //dbprintf("parse_request:\n\trbuf left to parse:\n%s\n", p);

    // count request number based on CRLF2
    while ((p = strstr(p, CRLF2)) != NULL && p < bufp->rbuf_tail) {
	++(bufp->rbuf_req_count);
	p += strlen(CRLF2);
	bufp->parse_p = p;
    }

    // parse every request
    while (bufp->rbuf_req_count != 0) {

	dbprintf("parse_request: request count %d\n", bufp->rbuf_req_count);

	// calloc http_req
	if (bufp->req_fully_received == 1) {
	    //dbprintf("parse_request: start parsing new request\n");
	    bufp->http_req_p = (struct http_req *)calloc(1, sizeof(struct http_req));

	    bufp->req_fully_received = 0;
	    bufp->req_line_header_received = 0;

	}

	// parse req
	parse_request_line(bufp);
	parse_request_headers(bufp);

	bufp->req_line_header_received = 1;// update 

	parse_message_body(bufp); // set req_fully_received inside

	// if fully received, enqueue	
	if (bufp->req_fully_received == 1) {
	    dbprintf("parse_request: req fully received\n");

	    // update req_queue
	    req_enqueue(bufp->req_queue_p, bufp->http_req_p);

	    // update rbuf
	    --(bufp->rbuf_req_count);
	    bufp->rbuf_head = strstr(bufp->rbuf_head, CRLF2) + strlen(CRLF2);

	} else {
	    // only POST can reach here
	    dbprintf("parse_request: POST req not fully received yet, continue receiving\n");
	    break; // break out while loop
	}
	
    }
    
    return 0;
}
Esempio n. 2
0
static void
req_forward(struct context *ctx, struct conn *c_conn, struct msg *msg)
{
    rstatus_t status;
    struct conn *s_conn;
    struct server_pool *pool;
    uint8_t *key;
    uint32_t keylen;
    struct keypos *kpos;

    ASSERT(c_conn->client && !c_conn->proxy);

    /* enqueue message (request) into client outq, if response is expected */
    if (!msg->noreply) {
        c_conn->enqueue_outq(ctx, c_conn, msg);
    }

    pool = c_conn->owner;

    ASSERT(array_n(msg->keys) > 0);
    kpos = array_get(msg->keys, 0);
    key = kpos->start;
    keylen = (uint32_t)(kpos->end - kpos->start);

    s_conn = msg->routing(ctx, pool, msg, key, keylen);
    if (s_conn == NULL) {
        req_forward_error(ctx, c_conn, msg);
        return;
    }
    ASSERT(!s_conn->client && !s_conn->proxy);

    status = req_enqueue(ctx, s_conn, c_conn, msg);
    if (status != NC_OK) {
        req_put(msg);
        return;
    }

    req_forward_stats(ctx, s_conn->owner, msg);

    log_debug(LOG_VERB, "forward from c %d to s %d req %"PRIu64" len %"PRIu32
              " type %d with key '%.*s'", c_conn->sd, s_conn->sd, msg->id,
              msg->mlen, msg->type, keylen, key);
    return;
}