/**************************************** * cg_upnp_event_notify_request_setpropertysetnode ****************************************/ BOOL cg_upnp_event_notify_request_setpropertysetnode(CgUpnpNotifyRequest *notifyReq, CgUpnpSubscriber *sub, /* CgUpnpService */void *pservice, CgUpnpStateVariable *statVar) { CgHttpRequest *httpReq; CgXmlNode *propSetNode; CgUpnpService* service; char server[CG_UPNP_SEVERNAME_MAXLEN]; cg_log_debug_l4("Entering...\n"); service = (CgUpnpService *)pservice; httpReq = cg_soap_request_gethttprequest(notifyReq); cg_http_request_setmethod(httpReq, CG_HTTP_NOTIFY); cg_http_request_setconnection(httpReq, CG_HTTP_CLOSE); cg_http_request_seturi(httpReq, cg_upnp_subscriber_getdeliverypath(sub)); cg_http_request_sethost(httpReq, cg_upnp_subscriber_getdeliveryhost(sub), cg_upnp_subscriber_getdeliveryport(sub)); cg_upnp_getservername(server, sizeof(server)); cg_http_packet_setheadervalue((CgHttpPacket*)httpReq, CG_HTTP_SERVER, server); cg_upnp_event_notify_request_setnt(notifyReq, CG_UPNP_NT_EVENT); cg_upnp_event_notify_request_setnts(notifyReq, CG_UPNP_NTS_PROPCHANGE); cg_upnp_event_notify_request_setsid(notifyReq, cg_upnp_subscriber_getsid(sub)); cg_upnp_event_notify_request_setseq(notifyReq, cg_upnp_subscriber_getnotifycount(sub)); propSetNode = cg_upnp_event_notify_request_createpropertysetnode(service, statVar); cg_soap_request_setcontent(notifyReq, propSetNode); cg_xml_node_delete(propSetNode); cg_log_debug_l4("Leaving...\n"); return TRUE; }
void cg_http_packet_setheaderssizet(CgHttpPacket *httpPkt, const char* name, ssize_t value) { char svalue[CG_STRING_LONG_BUFLEN]; cg_log_debug_l4("Entering...\n"); cg_http_packet_setheadervalue(httpPkt, name, cg_ssizet2str(value, svalue, sizeof(svalue))); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_event_notify_request_setsid(CgUpnpNotifyRequest *soapReq, char *sid) { char buf[CG_UPNP_SUBSCRIPTION_SID_HEADER_SIZE]; cg_log_debug_l4("Entering...\n"); cg_http_packet_setheadervalue((CgHttpPacket*)(soapReq->httpReq), CG_HTTP_SID, cg_upnp_event_subscription_tosidheaderstring(sid, buf, sizeof(buf))); cg_log_debug_l4("Leaving...\n"); }
void cg_http_packet_setheaderinteger(CgHttpPacket *httpPkt, const char* name, int value) { char svalue[CG_STRING_INTEGER_BUFLEN]; cg_log_debug_l4("Entering...\n"); cg_http_packet_setheadervalue(httpPkt, name, cg_int2str(value, svalue, sizeof(svalue))); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_ssdprequest_setleasetime(CgUpnpSSDPRequest *ssdpReq, CgTime value) { char buf[sizeof(CG_HTTP_MAX_AGE) + 1 + CG_STRING_INTEGER_BUFLEN]; cg_log_debug_l4("Entering...\n"); sprintf(buf, "%s=%ld", CG_HTTP_MAX_AGE, (long)value); cg_http_packet_setheadervalue((CgHttpPacket*)ssdpReq, CG_HTTP_CACHE_CONTROL, buf); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_ssdpresponse_setleasetime(CgUpnpSSDPResponse *ssdpRes, CgTime value) { char buf[CG_UPNP_SSDP_MAXAGE_LEN + 1 + CG_STRING_INTEGER_BUFLEN]; cg_log_debug_l4("Entering...\n"); sprintf(buf, "%s=%d", CG_HTTP_MAX_AGE, (int)value); cg_http_packet_setheadervalue((CgHttpPacket*)ssdpRes, CG_HTTP_CACHE_CONTROL, buf); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_event_subscription_request_settimeout(void *subReq, long timeout) { CgString *timeoutBuf; cg_log_debug_l4("Entering...\n"); timeoutBuf = cg_string_new(); cg_http_packet_setheadervalue(((CgHttpPacket*)subReq), CG_HTTP_TIMEOUT, cg_upnp_event_subscription_totimeoutheaderstring(timeout, timeoutBuf)); cg_string_delete(timeoutBuf); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_event_subscription_response_settimeout(CgUpnpSubscriptionResponse *subRes, long value) { CgString *buf; cg_log_debug_l4("Entering...\n"); buf = cg_string_new(); cg_http_packet_setheadervalue((CgHttpPacket*)subRes, CG_HTTP_TIMEOUT, cg_upnp_event_subscription_totimeoutheaderstring(value, buf)); cg_string_delete(buf); cg_log_debug_l4("Leaving...\n"); }
void cg_http_packet_sethost(CgHttpPacket *httpPkt, const char *addr, int port) { char *host; size_t hostMaxLen; cg_log_debug_l4("Entering...\n"); if (addr == NULL) return; hostMaxLen = cg_strlen(addr) + CG_NET_IPV6_ADDRSTRING_MAXSIZE + CG_STRING_INTEGER_BUFLEN; host = malloc(sizeof(char) * hostMaxLen); if (host == NULL) /* Memory allocation failure */ return; #if defined(HAVE_SNPRINTF) if (0 < port && port != CG_HTTP_DEFAULT_PORT) { if (cg_net_isipv6address(addr) == TRUE) snprintf(host, hostMaxLen, "[%s]:%d", addr, port); else snprintf(host, hostMaxLen, "%s:%d", addr, port); } else { if (cg_net_isipv6address(addr) == TRUE) snprintf(host, hostMaxLen, "[%s]", addr); else snprintf(host, hostMaxLen, "%s", addr); } #else if (0 < port && port != CG_HTTP_DEFAULT_PORT) { if (cg_net_isipv6address(addr) == TRUE) sprintf(host, "[%s]:%d", addr, port); else sprintf(host, "%s:%d", addr, port); } else { if (cg_net_isipv6address(addr) == TRUE) sprintf(host, "[%s]", addr); else sprintf(host, "%s", addr); } #endif cg_http_packet_setheadervalue(httpPkt, CG_HTTP_HOST, host); free(host); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_event_subscription_subscriberesponse_setresponse(CgUpnpSubscriptionResponse *subRes, int code) { char *server[CG_UPNP_SEVERNAME_MAXLEN]; cg_log_debug_l4("Entering...\n"); cg_http_response_setstatuscode(subRes, code); cg_upnp_getservername(server, sizeof(server)); cg_http_packet_setheadervalue(((CgHttpPacket*)subRes), CG_HTTP_SERVER, server); cg_http_response_setcontentlength(subRes, 0); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_event_subscription_request_setsid(CgUpnpSubscriptionRequest *subReq, const char *sid) { CgString *headerSID; ssize_t uuidIdx; cg_log_debug_l4("Entering...\n"); headerSID = cg_string_new(); uuidIdx = cg_strstr(sid, CG_UPNP_ST_UUID_DEVICE); if (uuidIdx < 0) cg_string_addvalue(headerSID, CG_UPNP_ST_UUID_DEVICE ":"); cg_string_addvalue(headerSID, sid); cg_http_packet_setheadervalue(((CgHttpPacket*)subReq), CG_HTTP_SID, cg_string_getvalue(headerSID)); cg_string_delete(headerSID); cg_log_debug_l4("Leaving...\n"); }
void cg_upnp_dms_youtube_http_listener(CgHttpRequest *httpReq) { CgUpnpMediaServer *dms; CgUpnpDevice *dev; char *httpURI; int contentMD5Idx; char *contentMd5; CgHttpResponse *httpRes; CgSocket *sock; char chunkedChar[32]; BOOL isHeadRequest; struct stat fileStat; off_t fileSize; FILE *fp; char readBuf[CG_FILE_READ_CHUNK_SIZE]; off_t nRead; off_t nReadCnt; off_t nWroteCnt; char contentFile[CG_MD5_STRING_BUF_SIZE+8]; dev = (CgUpnpDevice *)cg_http_request_getuserdata(httpReq); if (!dev) { cg_http_request_postbadrequest(httpReq); return; } dms = (CgUpnpMediaServer *)cg_upnp_device_getuserdata(dev); if (!dms) { cg_http_request_postbadrequest(httpReq); return; } httpURI = cg_http_request_geturi(httpReq); if (cg_strlen(httpURI) <= 0) { cg_http_request_postbadrequest(httpReq); return; } if (cg_strstr(httpURI, CG_UPNP_MEDIA_YOUTUBE_RESURL_PATH) < 0) { cg_upnp_device_httprequestrecieved(httpReq); return; } contentMD5Idx = cg_strrchr(httpURI, "/", 1); if (contentMD5Idx < 0) { cg_http_request_postbadrequest(httpReq); return; } contentMd5 = httpURI + contentMD5Idx + 1; cg_strcpy(contentFile, contentMd5); cg_strcat(contentFile, "." CG_UPNP_MEDIA_YOUTUBE_TRANSCODE_FILEEXT); cg_upnp_dms_lock(dms); isHeadRequest = cg_http_request_isheadrequest(httpReq); httpRes = cg_http_response_new(); #if defined(CG_USE_CHUNKED_STREAM) cg_http_response_setversion(httpRes, CG_HTTP_VER11); #else cg_http_response_setversion(httpRes, CG_HTTP_VER10); #endif cg_http_response_setstatuscode(httpRes, CG_HTTP_STATUS_OK); cg_http_response_setcontenttype(httpRes, CG_UPNP_MEDIA_YOUTUBE_CONTENT_MIMETYPE); sock = cg_http_request_getsocket(httpReq); cg_socket_settimeout(sock, 0); fileSize = 0; if (stat(contentFile, &fileStat) == 0) fileSize = fileStat.st_size; #if defined(CG_USE_CHUNKED_STREAM) cg_http_packet_setheadervalue((CgHttpPacket*)httpRes, "Transfer-Encoding", "chunked"); #else cg_http_response_setcontentlength(httpRes, fileSize); #endif cg_http_request_postresponse(httpReq, httpRes); if (0 < fileSize) { nReadCnt = 0; nWroteCnt = 0; fp = fopen(contentFile, "rb"); if (fp) { nRead = fread(readBuf, sizeof(char), CG_FILE_READ_CHUNK_SIZE, fp); while (nReadCnt < fileSize && 0 < nRead) { nReadCnt += nRead; #if defined(CG_USE_CHUNKED_STREAM) sprintf(chunkedChar, "%x%s", nRead, CG_HTTP_CRLF); cg_socket_write(sock, chunkedChar, cg_strlen(chunkedChar)); #endif nWroteCnt += cg_socket_write(sock, readBuf, nRead); #if defined(CG_USE_CHUNKED_STREAM) cg_socket_write(sock, CG_HTTP_CRLF, sizeof(CG_HTTP_CRLF)-1); #endif nRead = fread(readBuf, sizeof(char), CG_FILE_READ_CHUNK_SIZE, fp); } fclose(fp); } } #if defined(CG_USE_CHUNKED_STREAM) sprintf(chunkedChar, "%x%s", 0, CG_HTTP_CRLF); cg_socket_write(sock, chunkedChar, cg_strlen(chunkedChar)); #endif cg_socket_close(sock); cg_http_response_delete(httpRes); cg_upnp_dms_unlock(dms); }
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; }
void cg_http_packet_setheaderlonglong(CgHttpPacket *httpPkt, char* name, CgInt64 value) { char svalue[CG_STRING_LONGLONG_BUFLEN]; cg_http_packet_setheadervalue(httpPkt, name, cg_longlong2str(value, svalue, sizeof(svalue))); }