Exemplo n.º 1
0
BOOL cg_http_packet_read(CgHttpPacket *httpPkt, CgSocket *sock, BOOL onlyHeader, char *lineBuf, size_t lineBufSize)
{
	cg_log_debug_l4("Entering...\n");

	cg_http_packet_clear(httpPkt);
	cg_http_packet_read_headers(httpPkt, sock, lineBuf, lineBufSize);
	
	cg_log_debug_l4("Leaving...\n");

	if (onlyHeader)
		return TRUE;

	return cg_http_packet_read_body(httpPkt, sock, lineBuf, lineBufSize);
}
Exemplo n.º 2
0
BOOL cg_http_request_read(CgHttpRequest *httpReq, CgSocket *sock)
{
    char lineBuf[CG_HTTP_READLINE_BUFSIZE];
    CgStringTokenizer *strTok;
    int readLen;
    CgNetURI *uri = NULL;
    BOOL failed = FALSE;

    cg_log_debug_l4("Entering...\n");

    cg_http_request_clear(httpReq);

    /* If first character(s) is \n or \r\n we ignore it(them) and read second line. */
    do {
        readLen = cg_socket_readline(sock, lineBuf, sizeof(lineBuf));
    } while (readLen >= 1 && readLen <=2);

    if (readLen <= 0)
        return FALSE;

    strTok = cg_string_tokenizer_new(lineBuf, CG_HTTP_STATUSLINE_DELIM);
    if (cg_string_tokenizer_hasmoretoken(strTok) == TRUE)
        cg_http_request_setmethod(httpReq, cg_string_tokenizer_nexttoken(strTok));
    else
        failed = TRUE;
    if (cg_string_tokenizer_hasmoretoken(strTok) == TRUE)
        cg_http_request_seturi(httpReq, cg_string_tokenizer_nexttoken(strTok));
    else
        failed = TRUE;
    if (cg_string_tokenizer_hasmoretoken(strTok) == TRUE)
        cg_http_request_setversion(httpReq, cg_string_tokenizer_nexttoken(strTok));
    else
        failed = TRUE;
    cg_string_tokenizer_delete(strTok);

    if (failed == TRUE) return FALSE;

    /* We could do some further validation for the HTTP-request? */

    /* Change URI to be relative (absolute not needed anymore) */
    uri = cg_net_uri_new();
    if (uri != NULL)
    {
        cg_net_uri_set(uri, cg_http_request_geturi(httpReq));
        if (cg_net_uri_isabsolute(uri) == TRUE &&
                cg_net_uri_getrequest(uri) != NULL)
        {
            cg_http_request_seturi(httpReq,
                                   cg_net_uri_getrequest(uri));
        }
        cg_net_uri_delete(uri);
        uri = NULL;
    }

    /* Read headers */
    cg_http_packet_clear((CgHttpPacket *)httpReq);
    cg_http_packet_read_headers((CgHttpPacket *)httpReq, sock, lineBuf, sizeof(lineBuf));

    /* HTTP-request must have Content-Length or Transfer-Encoding header
       in order to have body */
    if (cg_http_packet_hasheader((CgHttpPacket *)httpReq, CG_HTTP_CONTENT_LENGTH) ||
            cg_http_packet_hasheader((CgHttpPacket *)httpReq, CG_HTTP_TRANSFER_ENCODING))
        cg_http_packet_read_body((CgHttpPacket *)httpReq, sock, lineBuf, sizeof(lineBuf));

    cg_log_debug_l4("Leaving...\n");

    return TRUE;
}