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"); }
void cg_http_request_copy(CgHttpRequest *destHttpReq, CgHttpRequest *srcHttpReq) { cg_log_debug_l4("Entering...\n"); cg_http_request_setmethod(destHttpReq, cg_http_request_getmethod(srcHttpReq)); cg_http_request_seturi(destHttpReq, cg_http_request_geturi(srcHttpReq)); cg_http_request_setversion(destHttpReq, cg_http_request_getversion(srcHttpReq)); cg_http_packet_copy((CgHttpPacket *)destHttpReq, (CgHttpPacket *)srcHttpReq); cg_log_debug_l4("Leaving...\n"); }
void cg_http_request_print(CgHttpRequest *httpReq) { cg_log_debug_l4("Entering...\n"); cg_log_debug_s("%s %s %s\n", cg_http_request_getmethod(httpReq), cg_http_request_geturi(httpReq), cg_http_request_getversion(httpReq)); cg_http_packet_print((CgHttpPacket *)httpReq); cg_log_debug_l4("Leaving...\n"); }
CgHttpResponse *cg_http_request_post_main(CgHttpRequest *httpReq, char *ipaddr, int port, BOOL isSecure) { CgSocket *sock; char *method, *uri, *version; #ifdef CG_SHOW_TIMINGS struct timeval start_time, end_time, elapsed_time; #endif CgString *firstLine; cg_log_debug_l4("Entering...\n"); #ifdef CG_SHOW_TIMINGS gettimeofday(&start_time, NULL); #endif cg_http_response_clear(httpReq->httpRes); cg_log_debug_s("(HTTP) Posting:\n"); cg_http_request_print(httpReq); #if defined(CG_USE_OPENSSL) if (isSecure == FALSE) sock = cg_socket_stream_new(); else sock = cg_socket_ssl_new(); #else sock = cg_socket_stream_new(); #endif cg_socket_settimeout(sock, cg_http_request_gettimeout(httpReq)); if (cg_socket_connect(sock, ipaddr, port) == FALSE) { cg_socket_delete(sock); return httpReq->httpRes; } cg_http_request_sethost(httpReq, ipaddr, port); cg_http_packet_setheadervalue((CgHttpPacket*)httpReq, CG_HTTP_USERAGENT, cg_http_request_getuseragent(httpReq)); method = cg_http_request_getmethod(httpReq); uri = cg_http_request_geturi(httpReq); version = cg_http_request_getversion(httpReq); if (method == NULL || uri == NULL || version == NULL) { cg_socket_close(sock); cg_socket_delete(sock); return httpReq->httpRes; } #ifdef CG_SHOW_TIMINGS cg_log_debug_s("\nRequest: %s%s%s:%d%s%s%s\n", method, CG_HTTP_SP, ipaddr, port, uri, CG_HTTP_SP, version); #endif /**** send first line ****/ firstLine = cg_string_new(); cg_string_addvalue(firstLine, method); cg_string_addvalue(firstLine, CG_HTTP_SP); cg_string_addvalue(firstLine, uri); cg_string_addvalue(firstLine, CG_HTTP_SP); cg_string_addvalue(firstLine, version); cg_string_addvalue(firstLine, CG_HTTP_CRLF); cg_socket_write(sock, cg_string_getvalue(firstLine), cg_string_length(firstLine)); cg_string_delete(firstLine); /**** send header and content ****/ cg_http_packet_post((CgHttpPacket *)httpReq, sock); /**** read response ****/ cg_http_response_read(httpReq->httpRes, sock, cg_http_request_isheadrequest(httpReq)); #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 cg_socket_close(sock); cg_socket_delete(sock); cg_http_response_print(httpReq->httpRes); cg_log_debug_l4("Leaving...\n"); return httpReq->httpRes; }
static void cg_http_server_clientthread(CgThread *thread) { CgHttpServerClientData *clientData; CgHttpServer *httpServer; CgSocket *clientSock; void *httpServerUserData; CgHttpRequest *httpReq; char *version = NULL; cg_log_debug_l4("Entering...\n"); clientData = (CgHttpServerClientData *)cg_thread_getuserdata(thread); httpServer = clientData->httpServer; clientSock = clientData->clientSock; httpServerUserData = cg_http_server_getuserdata(httpServer); httpReq = cg_http_request_new(); cg_http_request_setsocket(httpReq, clientSock); /**** Thanks for Makela Aapo (10/31/05) ****/ while (cg_http_request_read(httpReq, clientSock) == TRUE && cg_thread_isrunnable(thread) == TRUE) { /* Check some validity of the request */ version = cg_http_request_getversion(httpReq); if (cg_strcmp(version, CG_HTTP_VER11) == 0) { /* According to HTTP/1.1 spec, we must not tolerate HTTP/1.1 request without HOST-header */ if (cg_http_request_gethost(httpReq) == NULL) { cg_http_request_postbadrequest(httpReq); continue; } } if (httpServer->listener != NULL) { cg_http_request_setuserdata(httpReq, httpServerUserData); httpServer->listener(httpReq); } /* Close connection according to HTTP version and headers */ if (cg_strcmp(version, CG_HTTP_VER10) == 0) { /* Terminate connection after HTTP/1.0 request */ break; } /* We are having HTTP/1.1 or better => terminate, if requested */ if (cg_http_request_iskeepaliveconnection(httpReq) == FALSE) { break; } } cg_log_debug_s("Dropping HTTP client\n"); cg_http_request_delete(httpReq); cg_socket_close(clientSock); cg_socket_delete(clientSock); cg_http_server_clientdata_delete(clientData); cg_thread_setuserdata(thread, NULL); // This code frequently crashes. mutex lock referencing free'd memory. cg_http_server_lock(httpServer); cg_thread_remove(thread); cg_http_server_unlock(httpServer); cg_log_debug_l4("Leaving...\n"); cg_thread_delete(thread); }