예제 #1
0
char *cg_upnp_ssdprequest_tostring(CgUpnpSSDPRequest *ssdpReq, CgString *ssdpMsg)
{
	CgHttpHeader *header;
	const char *name;
	const char *value;
	
	cg_log_debug_l4("Entering...\n");

	cg_string_addvalue(ssdpMsg, cg_http_request_getmethod(ssdpReq));
	cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
	cg_string_addvalue(ssdpMsg, cg_http_request_geturi(ssdpReq));
	cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
	cg_string_addvalue(ssdpMsg, cg_http_request_getversion(ssdpReq));
	cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
	
	for (header = cg_http_packet_getheaders((CgHttpPacket *)ssdpReq); header != NULL; header = cg_http_header_next(header)) {
		name = cg_http_header_getname(header);
		value = cg_http_header_getvalue(header);
		cg_string_addvalue(ssdpMsg, name);
		cg_string_addvalue(ssdpMsg, CG_HTTP_COLON);
		cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
		cg_string_addvalue(ssdpMsg, value);
		cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
	}
	cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
	
	return cg_string_getvalue(ssdpMsg);

	cg_log_debug_l4("Leaving...\n");
}
예제 #2
0
size_t cg_http_packet_getheadersize(CgHttpPacket *httpPkt)
{
	CgHttpHeader *header;
	size_t headerSize;
	const char *name;
	const char *value;
	
	cg_log_debug_l4("Entering...\n");

	headerSize = 0;
	for (header = cg_http_packet_getheaders(httpPkt); header != NULL; header = cg_http_header_next(header)) {
		name = cg_http_header_getname(header);
		value = cg_http_header_getvalue(header);
		headerSize += cg_strlen(name); 
		headerSize += sizeof(CG_HTTP_COLON)-1; 
		headerSize += sizeof(CG_HTTP_SP)-1; 
		headerSize += cg_strlen(value); 
		headerSize += sizeof(CG_HTTP_CRLF)-1; 
	}
	headerSize += sizeof(CG_HTTP_CRLF)-1; 

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

	return headerSize;
}
예제 #3
0
void cg_http_packet_print(CgHttpPacket *httpPkt)
{
	CgHttpHeader *header;
	char *content;
	long contentLen;

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

	/**** print headers ****/
	for (header = cg_http_packet_getheaders(httpPkt); header != NULL; header = cg_http_header_next(header)) {
	cg_log_debug_s("%s: %s\n",
			cg_http_header_getname(header),
			cg_http_header_getvalue(header));
	}
	cg_log_debug_s("\n");
	
	/**** print content ****/
	content = cg_http_packet_getcontent(httpPkt);
	contentLen = cg_http_packet_getcontentlength(httpPkt);
	
	if (content != NULL && 0 < contentLen)
		cg_log_debug_s("%s\n", content);

	cg_log_debug_l4("Leaving...\n");
}
예제 #4
0
void cg_http_packet_post(CgHttpPacket *httpPkt, CgSocket *sock)
{
	CgHttpHeader *header;
	const char *name, *value;
	char *content;
	size_t contentLen;
	
	cg_log_debug_l4("Entering...\n");

	/**** send headers ****/
	for (header = cg_http_packet_getheaders(httpPkt); header != NULL; header = cg_http_header_next(header)) {
		name = cg_http_header_getname(header);
		if (name == NULL)
			continue;
		cg_socket_write(sock, name, cg_strlen(name));
		cg_socket_write(sock, CG_HTTP_COLON, sizeof(CG_HTTP_COLON)-1);
		cg_socket_write(sock, CG_HTTP_SP, sizeof(CG_HTTP_SP)-1);
		value = cg_http_header_getvalue(header);
		if (value != NULL)
			cg_socket_write(sock, value, cg_strlen(value));
		cg_socket_write(sock, CG_HTTP_CRLF, sizeof(CG_HTTP_CRLF)-1);
	}
	cg_socket_write(sock, CG_HTTP_CRLF, sizeof(CG_HTTP_CRLF)-1);
	
	/**** send content ****/
	content = cg_http_packet_getcontent(httpPkt);
	contentLen = cg_http_packet_getcontentlength(httpPkt);
	if (content != NULL && 0 < contentLen)
		cg_socket_write(sock, content, contentLen);

	cg_log_debug_l4("Leaving...\n");
}
예제 #5
0
char *cg_upnp_ssdpresponse_tostring(CgUpnpSSDPResponse *ssdpRes, CgString *ssdpMsg)
{
    CgHttpHeader *header;
    char statusCodeBuf[CG_STRING_INTEGER_BUFLEN];
    const char *name;
    const char *value;

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

    cg_string_addvalue(ssdpMsg, cg_http_response_getversion(ssdpRes));
    cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
    cg_string_addvalue(ssdpMsg, cg_int2str(cg_http_response_getstatuscode(ssdpRes), statusCodeBuf, sizeof(statusCodeBuf)));
    cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
    cg_string_addvalue(ssdpMsg, cg_http_response_getreasonphrase(ssdpRes));
    cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);

    for (header = cg_http_packet_getheaders((CgHttpPacket *)ssdpRes); header != NULL; header = cg_http_header_next(header)) {
        name = cg_http_header_getname(header);
        value = cg_http_header_getvalue(header);
        cg_string_addvalue(ssdpMsg, name);
        cg_string_addvalue(ssdpMsg, CG_HTTP_COLON);
        cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
        cg_string_addvalue(ssdpMsg, value);
        cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
    }
    cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);

    return cg_string_getvalue(ssdpMsg);

    cg_log_debug_l4("Leaving...\n");
}
예제 #6
0
void cg_http_packet_copy(CgHttpPacket *destHttpPkt, CgHttpPacket *srcHttpPkt)
{
	CgHttpHeader *srcHeader;
	CgHttpHeader *destHeader;
	
	cg_log_debug_l4("Entering...\n");

	cg_http_packet_clear(destHttpPkt);
	
	/**** copy headers ****/
	for (srcHeader = cg_http_packet_getheaders(srcHttpPkt); srcHeader != NULL; srcHeader = cg_http_header_next(srcHeader)) {
		destHeader = cg_http_header_new();
		cg_http_header_setname(destHeader, cg_http_header_getname(srcHeader));
		cg_http_header_setvalue(destHeader, cg_http_header_getvalue(srcHeader));
		cg_http_packet_addheader(destHttpPkt, destHeader);
	}
	
	/**** copy content ****/
	cg_http_packet_setcontent(destHttpPkt, cg_http_packet_getcontent(srcHttpPkt));

	cg_log_debug_l4("Leaving...\n");
}
예제 #7
0
CgHttpHeader *cg_http_headerlist_get(CgHttpHeaderList *headerList, const char *name)
{
	CgHttpHeader *header;
	const char *headerName;
		
	cg_log_debug_l4("Entering...\n");

	if (name == NULL)
		return NULL;
		
	for (header = cg_http_headerlist_gets(headerList); header != NULL; header = cg_http_header_next(header)) {
		headerName = cg_http_header_getname(header);
		if (headerName == NULL)
			continue;
		if (cg_strcasecmp(headerName, name) == 0)
			return header;
	}
	
	return NULL;

	cg_log_debug_l4("Leaving...\n");
}
예제 #8
0
CgHttpResponse *cg_http_request_post(CgHttpRequest *httpReq, char *ipaddr, int port)
{
    CgHttpResponse *httpRes;
    BOOL newCurl = FALSE;
    CURL *curl;
    CgHttpHeader *reqHeader;
    struct curl_slist *curlHeaderList;
    CgString *headerStr;
    CURLcode res;
    char *uri, *method;
    char url[CG_NET_URI_MAXLEN];
    long retcode;
#ifdef CG_SHOW_TIMINGS
    struct timeval start_time, end_time, elapsed_time;
#endif

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

#ifdef CG_SHOW_TIMINGS
    gettimeofday(&start_time, NULL);
#endif

    httpRes = httpReq->httpRes;

    /* Clear the response data because new data will not
     * overwrite it, but it is appended to the end */
    cg_string_clear(httpRes->content);

    cg_log_debug_s("Posting HTTP request (Curl)\n");
    cg_http_request_print(httpReq);

    cg_http_persistentconnection_lock();
#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS
    cg_log_debug_s("Looking for persistent connection to %s, port %d\n", ipaddr, port);
    curl = (CURL*)cg_http_persistentconnection_get(ipaddr, port);

    if (curl == NULL)
    {
        cg_log_debug_s("Persistent connection not found...\n");
#endif
        curl = curl_easy_init();
        if (curl == NULL)
        {
            cg_http_persistentconnection_unlock();
            return httpReq->httpRes;
        }
#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS
        newCurl = TRUE;
    }
#endif
    method = cg_http_request_getmethod(httpReq);
    uri = cg_http_request_geturi(httpReq);

    /**** method ****/
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);

    /**** url ****/
    if (uri && cg_strstr(uri, CG_NET_URI_PROTOCOL_DELIM) > 0)
    {
        curl_easy_setopt(curl, CURLOPT_URL, uri);
    }
    else
    {
        cg_net_gethosturl(ipaddr, port, uri, url, sizeof(url));
        curl_easy_setopt(curl, CURLOPT_URL, url);
        cg_log_debug_s("\n\nCURL: %s\n\n", url);
    }

    /**** header ****/
    curlHeaderList = NULL;
    headerStr = cg_string_new();
    for (reqHeader = cg_http_request_getheaders(httpReq); reqHeader; reqHeader = cg_http_header_next(reqHeader)) {
        cg_string_setvalue(headerStr, cg_http_header_getname(reqHeader));
        if (cg_string_addvalue(headerStr, CG_HTTP_COLON CG_HTTP_SP) &&
                cg_string_addvalue(headerStr, cg_http_header_getvalue(reqHeader)))
            curlHeaderList = curl_slist_append(curlHeaderList, cg_string_getvalue(headerStr));
    }
    cg_string_delete(headerStr);
    /* Disable Expect header because it causes IOP issues */
    curlHeaderList = curl_slist_append(curlHeaderList, "Expect:");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curlHeaderList);

    /**** content ****/
    /*if (cg_http_request_ispostrequest(httpReq) == TRUE) {*/
    if (cg_http_request_getcontentlength(httpReq) > 0) {
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cg_http_request_getcontent(httpReq));
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, cg_http_request_getcontentlength(httpReq));
    }
    else
    {
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
    }

    /* This has to be enabled for progress callback to be called */
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS,
                     FALSE);

    /* Used for checking stack state during curl easy perform */
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,
                     cg_http_request_progress_callback);

    /**** response header callback ****/
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,
                     cg_http_request_header_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)httpRes);

    /**** response content callback ****/
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
                     cg_http_request_content_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)httpRes);

    /**** useragent ****/
    curl_easy_setopt(curl, CURLOPT_USERAGENT, cg_http_request_getuseragent(httpReq) );

    /**** Prohibit curl from using signals ****/
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

    /**** Set the connection timeout so we don't wait forever ****/
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT,
                     CG_HTTP_CURL_CONNECTTIMEOUT);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT,
                     CG_HTTP_CONN_TIMEOUT);
#ifdef CG_SHOW_TIMINGS
    cg_log_debug_s("\nRequest: %s%s%s\n", method, CG_HTTP_SP, url);
#endif

    /* Get the XML document with CURL */
    res = curl_easy_perform(curl);
    if (res != CURLE_OK)
        cg_log_debug_s("curl_easy_perform: %s\n",
                       curl_easy_strerror(res));

    /* Set the content length, if it wasn't said in the header */
    if (cg_http_response_getcontentlength(httpRes) <= 0)
    {
        cg_http_response_setcontentlength(httpRes,
                                          cg_string_length(httpRes->content));
    }

    curl_slist_free_all(curlHeaderList);

    curl_easy_getinfo (curl, CURLINFO_HTTP_CODE, &retcode);
    cg_http_response_setstatuscode(httpRes, retcode);

#ifdef CG_SHOW_TIMINGS
    gettimeofday(&end_time, NULL);
    timersub(&end_time, &start_time, &elapsed_time);
    cg_log_debug_s("Getting HTTP-response completed. Elapsed time: "
                   "%ld msec\n", ((elapsed_time.tv_sec*1000) +
                                  (elapsed_time.tv_usec/1000)));
    cg_total_elapsed_time += (elapsed_time.tv_sec*1000000)+
                             (elapsed_time.tv_usec);
#endif

#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS
    if (newCurl)
    {
        cg_log_debug_s("Putting new connection into cache: %s %d\n", ipaddr, port);
        cg_http_persistentconnection_put(ipaddr, port, curl);
    }
#else
    curl_easy_cleanup(curl);
#endif
    cg_http_persistentconnection_unlock();

    cg_log_debug_s("Response for HTTP request (Curl)\n");
    cg_http_response_print(httpReq->httpRes);

    return httpReq->httpRes;

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