BOOL cg_bittorrent_metainfo_save(CgBittorrentMetainfo *cbm, char *fileName) { FILE *fp; CgString *str; if (!cbm) return FALSE; str = cg_string_new(); if (!str) return FALSE; if (!cg_bittorrent_metainfo_tostring(cbm, str)) { cg_string_delete(str); return FALSE; } fp = fopen(fileName, "wb"); if (fp) { fwrite(cg_string_getvalue(str), sizeof(char), cg_string_length(str), fp); fclose(fp); } cg_string_delete(str); return (fp ? TRUE : FALSE); }
char *cg_string_replace(CgString *str, char *fromStr[], char *toStr[], size_t fromStrCnt) { char *orgValue = NULL; size_t orgValueLen = 0; int n = 0; int copyPos = 0; size_t *fromStrLen = NULL; CgString *repValue = NULL; BOOL isReplaced = FALSE; cg_log_debug_l5("Entering...\n"); if (NULL == str ) return NULL; repValue = cg_string_new(); fromStrLen = (size_t *)malloc(sizeof(size_t) * fromStrCnt); if ( NULL == fromStrLen ) { cg_string_delete(repValue); cg_log_debug_s("Memory allocation failure!\n"); return NULL; } for (n=0; n<fromStrCnt; n++) fromStrLen[n] = cg_strlen(fromStr[n]); orgValue = cg_string_getvalue(str); orgValueLen = cg_string_length(str); copyPos = 0; while (copyPos<orgValueLen) { isReplaced = FALSE; for (n=0; n<fromStrCnt; n++) { if (strncmp(fromStr[n], orgValue + copyPos, fromStrLen[n]) == 0) { cg_string_addvalue(repValue, toStr[n]); copyPos += fromStrLen[n]; isReplaced = TRUE; continue; } } if (isReplaced == TRUE) continue; cg_string_naddvalue(repValue, orgValue + copyPos, 1); copyPos++; } free(fromStrLen); cg_string_setvalue(str, cg_string_getvalue(repValue)); cg_string_delete(repValue); cg_log_debug_l5("Leaving...\n"); return cg_string_getvalue(str); }
void cg_upnpav_resource_updateattributes(CgUpnpAvResource *res) { CgUpnpAvResourceData *nodeData; CgString *resAttr; char *mimeType; char *dlnaAttr; nodeData = (CgUpnpAvResourceData *)cg_xml_node_getuserdata(res); mimeType = (0 < cg_string_length(nodeData->mimeType)) ? cg_string_getvalue(nodeData->mimeType) : "*/*"; dlnaAttr = (0 < cg_string_length(nodeData->dlnaAttr)) ? cg_string_getvalue(nodeData->dlnaAttr) : "*"; resAttr = cg_string_new(); cg_string_addvalue(resAttr, "http-get:*:"); cg_string_addvalue(resAttr, mimeType); cg_string_addvalue(resAttr, ":"); cg_string_addvalue(resAttr, dlnaAttr); cg_xml_node_setattribute(res, CG_UPNPAV_RESOURCE_PROTOCOLINFO, cg_string_getvalue(resAttr)); cg_string_delete(resAttr); }
BOOL cg_upnp_ssdp_socket_postresponse(CgUpnpSSDPSocket *ssdpSock, CgUpnpSSDPResponse *ssdpRes, const char *host, int port) { CgString *ssdpMsg; size_t ssdpMsgLen; BOOL postSuccess; cg_log_debug_l4("Entering...\n"); ssdpMsg = cg_string_new(); cg_upnp_ssdpresponse_tostring(ssdpRes, ssdpMsg); ssdpMsgLen = cg_string_length(ssdpMsg); postSuccess = (cg_socket_sendto(ssdpSock, host, port, cg_string_getvalue(ssdpMsg), ssdpMsgLen) == ssdpMsgLen) ? TRUE : FALSE; cg_string_delete(ssdpMsg); cg_log_debug_l4("Leaving...\n"); return postSuccess; }
void cg_upnpav_dmr_addprotocolinfo(CgUpnpAvRenderer* dmr, CgUpnpAvProtocolInfo* info) { CgString* protocolInfos; CgUpnpAvProtocolInfo* protocolInfo; CgUpnpService* service; CgUpnpStateVariable* stateVar; cg_upnpav_protocolinfolist_add(dmr->protocolInfoList, info); protocolInfos = cg_string_new(); for (protocolInfo = cg_upnpav_dmr_getprotocolinfos(dmr); protocolInfo; protocolInfo = cg_upnpav_protocolinfo_next(protocolInfo)) { if (0 < cg_string_length(protocolInfos)) cg_string_addvalue(protocolInfos, ","); cg_string_addvalue(protocolInfos, cg_upnpav_protocolinfo_getstring(protocolInfo)); } service = cg_upnp_device_getservicebyexacttype(dmr->dev, CG_UPNPAV_DMR_CONNECTIONMANAGER_SERVICE_TYPE); stateVar = cg_upnp_service_getstatevariablebyname(service, CG_UPNPAV_DMR_CONNECTIONMANAGER_SINKPROTOCOLINFO); cg_upnp_statevariable_setvalue(stateVar, cg_string_getvalue(protocolInfos)); cg_string_delete(protocolInfos); }
BOOL cg_upnpav_dms_conmgr_actionreceived(CgUpnpAction* action) { CgUpnpAvServer* dms; CgUpnpDevice* dev; char* actionName; CgUpnpArgument* arg; CgString* protocolInfos; CgUpnpAvProtocolInfo* protocolInfo; actionName = (char*)cg_upnp_action_getname(action); if (cg_strlen(actionName) <= 0) return FALSE; dev = (CgUpnpDevice*)cg_upnp_service_getdevice(cg_upnp_action_getservice(action)); if (!dev) return FALSE; dms = (CgUpnpAvServer*)cg_upnp_device_getuserdata(dev); if (!dms) return FALSE; /* GetProtocolInfo*/ if (cg_streq(actionName, CG_UPNPAV_DMS_CONNECTIONMANAGER_GET_PROTOCOL_INFO)) { arg = cg_upnp_action_getargumentbyname(action, CG_UPNPAV_DMS_CONNECTIONMANAGER_SOURCE); if (!arg) return FALSE; protocolInfos = cg_string_new(); for (protocolInfo = cg_upnpav_dms_getprotocolinfos(dms); protocolInfo; protocolInfo = cg_upnpav_protocolinfo_next(protocolInfo)) { if (0 < cg_string_length(protocolInfos)) cg_string_addvalue(protocolInfos, ","); cg_string_addvalue(protocolInfos, cg_upnpav_protocolinfo_getstring(protocolInfo)); } cg_upnp_argument_setvalue(arg, cg_string_getvalue(protocolInfos)); cg_string_delete(protocolInfos); return TRUE; } return FALSE; }
void cg_soap_request_setcontent(CgSoapRequest *soapReq, CgXmlNode *node) { CgHttpRequest *httpReq; cg_log_debug_l4("Entering...\n"); httpReq = cg_soap_request_gethttprequest(soapReq); /**** content type ****/ cg_http_request_setcontenttype(httpReq, CG_XML_CONTENT_TYPE); /**** content ****/ cg_http_request_appendncontent(httpReq, CG_SOAP_VERSION_HEADER, cg_strlen(CG_SOAP_VERSION_HEADER)); cg_http_request_appendncontent(httpReq, CG_XML_CONTENT_LF, cg_strlen(CG_XML_CONTENT_LF)); cg_xml_node_tostring(node, TRUE, httpReq->content); /**** content length ****/ cg_http_request_setcontentlength(httpReq, cg_string_length(httpReq->content)); cg_log_debug_l4("Leaving...\n"); }
void cg_net_uri_setvalue(CgNetURI *uri, char *value) { char *protocol; int uriLen; int currIdx; int protoIdx; int atIdx; int colonIdx; int shashIdx; char *host; int eblacketIdx; CgString *hostStr; CgString *portStr; int hostLen; int sharpIdx; int questionIdx; int queryLen; cg_log_debug_l4("Entering...\n"); uriLen = cg_strlen(value); cg_net_uri_clear(uri); cg_net_uri_seturi(uri, value); currIdx = 0; /*** Protocol ****/ protoIdx = cg_strstr(value, CG_NET_URI_PROTOCOL_DELIM); if (0 < protoIdx) { cg_string_setnvalue(uri->protocol, value, protoIdx); currIdx += protoIdx + cg_strlen(CG_NET_URI_PROTOCOL_DELIM); } /*** User (Password) ****/ atIdx = cg_strstr(value+currIdx, CG_NET_URI_USER_DELIM); if (0 < atIdx) { colonIdx = cg_strstr(value+currIdx, CG_NET_URI_COLON_DELIM); /**** Thanks for Theo Beisch (2005/08/25) ****/ if (0 < colonIdx && colonIdx<atIdx) { cg_string_setnvalue(uri->user, value+currIdx, colonIdx); cg_string_setnvalue(uri->password, value+currIdx+colonIdx+1, atIdx-(colonIdx+1)); } else cg_string_setnvalue(uri->user, value+currIdx, atIdx - currIdx); currIdx += atIdx + 1; } /*** Host (Port) ****/ shashIdx = cg_strstr(value+currIdx, CG_NET_URI_SLASH_DELIM); if (0 < shashIdx) cg_string_setnvalue(uri->host, value+currIdx, shashIdx); else if (cg_net_uri_isabsolute(uri) == TRUE) cg_string_setnvalue(uri->host, value+currIdx, cg_strlen(value) - currIdx); host = cg_net_uri_gethost(uri); colonIdx = cg_strrchr(host, CG_NET_URI_COLON_DELIM, 1); eblacketIdx = cg_strrchr(host, CG_NET_URI_EBLACET_DELIM, 1); if (0 < colonIdx && eblacketIdx < colonIdx) { hostStr = cg_string_new(); cg_string_setvalue(hostStr, host); hostLen = cg_string_length(hostStr); /**** host ****/ cg_string_setnvalue(uri->host, cg_string_getvalue(hostStr), colonIdx); host = cg_net_uri_gethost(uri); if (0 < hostLen) { if (host[0] == '[' && host[hostLen-1] == ']') cg_string_setnvalue(uri->host, cg_string_getvalue(hostStr)+1, colonIdx-2); } /**** port ****/ portStr = cg_string_new(); cg_string_setnvalue(portStr, cg_string_getvalue(hostStr)+colonIdx+1, hostLen- colonIdx-1); uri->port = atoi(cg_string_getvalue(portStr)); cg_string_delete(portStr); cg_string_delete(hostStr); } else { uri->port = CG_NET_URI_KNKOWN_PORT; protocol = cg_net_uri_getprotocol(uri); if (cg_strcmp(protocol, CG_NET_URI_PROTOCOL_HTTP) == 0) uri->port = CG_NET_URI_DEFAULT_HTTP_PORT; if (cg_strcmp(protocol, CG_NET_URI_PROTOCOL_FTP) == 0) uri->port = CG_NET_URI_DEFAULT_FTP_PORT; } if (shashIdx > 0) currIdx += shashIdx; /* Handle relative URL */ if (cg_net_uri_isabsolute(uri) == FALSE) { cg_string_addvalue(uri->path, value); } else { /* First set path simply to the rest of URI */ cg_string_setnvalue(uri->path, value+currIdx, uriLen-currIdx); } /**** Path (Query/Fragment) ****/ sharpIdx = cg_strstr(value+currIdx, CG_NET_URI_SHARP_DELIM); if (0 < sharpIdx) { cg_string_setnvalue(uri->path, value+currIdx, sharpIdx); cg_string_setnvalue(uri->fragment, value+currIdx+sharpIdx+1, uriLen-(currIdx+sharpIdx+1)); } questionIdx = cg_strstr(value+currIdx, CG_NET_URI_QUESTION_DELIM); if (0 < questionIdx) { cg_string_setnvalue(uri->path, value+currIdx, questionIdx); queryLen = uriLen-(currIdx+questionIdx+1); if (0 < sharpIdx) queryLen -= uriLen - (currIdx+sharpIdx); cg_string_setnvalue(uri->query, value+currIdx+questionIdx+1, queryLen); } cg_log_debug_l4("Leaving...\n"); }
BOOL cg_upnp_ssdpresponse_server_post(CgUpnpSSDPResponseServer *server, CgUpnpSSDPRequest *ssdpReq) { CgUpnpHttpUSocket *httpuSock; char *ifAddr; const char *ssdpAddr; CgString *ssdpMsg; size_t sentLen = 0; cg_log_debug_l4("Entering...\n"); httpuSock = cg_upnp_ssdpresponse_server_getsocket(server); ifAddr = cg_socket_getaddress(httpuSock); ssdpAddr = cg_upnp_ssdp_gethostaddress(ifAddr); cg_upnp_ssdprequest_sethost(ssdpReq, ssdpAddr, CG_UPNP_SSDP_PORT); ssdpMsg = cg_string_new(); cg_upnp_ssdprequest_tostring(ssdpReq, ssdpMsg); sentLen = cg_socket_sendto(httpuSock, ssdpAddr, CG_UPNP_SSDP_PORT, cg_string_getvalue(ssdpMsg), cg_string_length(ssdpMsg)); cg_string_delete(ssdpMsg); cg_log_debug_l4("Leaving...\n"); return (sentLen > 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"); }
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 BOOL cg_upnp_ssdp_socket_notify(CgUpnpSSDPSocket *ssdpSock, CgUpnpSSDPRequest *ssdpReq, const char *ssdpAddr) { CgString *ssdpMsg; size_t sentLen; cg_log_debug_l4("Entering...\n"); cg_upnp_ssdprequest_setmethod(ssdpReq, CG_HTTP_NOTIFY); ssdpMsg = cg_string_new(); cg_upnp_ssdprequest_tostring(ssdpReq, ssdpMsg); sentLen = cg_socket_sendto(ssdpSock, ssdpAddr, CG_UPNP_SSDP_PORT, cg_string_getvalue(ssdpMsg), cg_string_length(ssdpMsg)); cg_string_delete(ssdpMsg); cg_log_debug_l4("Leaving...\n"); return (0 < sentLen) ? TRUE : FALSE; }