void cg_net_uri_rebuild(CgNetURI *uri) { char portStr[32]; char *path; cg_log_debug_l4("Entering...\n"); cg_string_setvalue(uri->uri, cg_net_uri_getprotocol(uri)); cg_string_addvalue(uri->uri, CG_NET_URI_PROTOCOL_DELIM); cg_string_addvalue(uri->uri, cg_net_uri_gethost(uri)); cg_string_addvalue(uri->uri, CG_NET_URI_COLON_DELIM); cg_string_addvalue(uri->uri, cg_int2str(cg_net_uri_getport(uri), portStr, sizeof(portStr))); if (0 < cg_strlen(cg_net_uri_getpath(uri))) { path = cg_net_uri_getpath(uri); if (path[0] != '/') cg_string_addvalue(uri->uri, CG_NET_URI_SLASH_DELIM); cg_string_addvalue(uri->uri, cg_net_uri_getpath(uri)); if (cg_strchr(cg_net_uri_getpath(uri), "?", 1) == -1 && 0 < cg_strlen(cg_net_uri_getquery(uri))) { cg_string_addvalue(uri->uri, CG_NET_URI_QUESTION_DELIM); cg_string_addvalue(uri->uri, cg_net_uri_getquery(uri)); } } cg_log_debug_l4("Leaving...\n"); }
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_http_request_addtouseragent(CgHttpRequest *httpReq, char *value) { cg_log_debug_l4("Entering...\n"); cg_string_addvalue(httpReq->userAgent, value); cg_log_debug_l4("Leaving...\n"); }
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); }
char *cg_net_uri_getrequest(CgNetURI *uri) { cg_log_debug_l4("Entering...\n"); if (cg_net_uri_hasquery(uri) == FALSE) { return cg_net_uri_getpath(uri); } if (uri->request == NULL) uri->request = cg_string_new(); cg_string_setvalue(uri->request, cg_net_uri_getpath(uri)); cg_string_addvalue(uri->request, CG_NET_URI_QUESTION_DELIM); cg_string_addvalue(uri->request, cg_net_uri_getquery(uri)); cg_log_debug_l4("Leaving...\n"); return cg_string_getvalue(uri->request); }
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"); }
char *cg_string_addrepvalue(CgString *str, const char *value, size_t repeatCnt) { int n; cg_log_debug_l5("Entering...\n"); for (n = 0; n < repeatCnt; n++) cg_string_addvalue(str, value); cg_log_debug_l5("Leaving...\n"); return cg_string_getvalue(str); }
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; }
static char *cg_xml_node_attribute_tostring(CgXmlNode *node, CgString *str) { CgXmlAttribute *attr; char *name; char *value; CgString *valueStr; cg_log_debug_l4("Entering...\n"); valueStr = cg_string_new(); if (valueStr == NULL) return NULL; for (attr = cg_xml_node_getattributes(node); attr != NULL; attr = cg_xml_attribute_next(attr)) { name = cg_xml_attribute_getname(attr); value = cg_xml_attribute_getvalue(attr); cg_string_setvalue(valueStr, value); cg_xml_escapechars(valueStr); /* All the following functions return NULL only when memory allocation fails, so we can check them all */ if (!cg_string_naddvalue(str, " ", 1) || !cg_string_addvalue(str, name) || !cg_string_naddvalue(str, "=\"", 2) || !cg_string_addvalue(str, cg_string_getvalue(valueStr)) || !cg_string_naddvalue(str, "\"", 1)) { /* Memory allocation failed */ cg_string_delete(valueStr); return NULL; } } cg_string_delete(valueStr); cg_log_debug_l4("Leaving...\n"); return cg_string_getvalue(str); }
char *cg_net_uri_escapestring(char *buf, int bufSize, CgString *retBuf) { #if defined(CG_HTTP_CURL) char *tmp; #else int n; unsigned char c; char hexChar[4]; #endif cg_log_debug_l4("Entering...\n"); if (!retBuf) return NULL; #if defined(CG_HTTP_CURL) tmp = (bufSize < 1)?curl_escape(buf, 0):curl_escape(buf, bufSize); if (tmp == NULL) { cg_log_debug_s("Memory allocation problem!\n"); return NULL; } cg_string_addvalue(retBuf, tmp); curl_free(tmp); #else if (bufSize < 1) bufSize = cg_strlen(buf) + 1; for (n=0; n<bufSize; n++) { c = (unsigned char)buf[n]; if (!cg_net_uri_isalphanumchar(c)) { #if defined(HAVE_SNPRINTF) snprintf(hexChar, sizeof(hexChar), "%%%02X", c); #else sprintf(hexChar, "%%%02X", c); #endif cg_string_naddvalue(retBuf, hexChar, 3); } else cg_string_naddvalue(retBuf, buf+n, 1); } #endif cg_log_debug_l4("Leaving...\n"); return cg_string_getvalue(retBuf); }
const char *cg_upnp_event_subscription_totimeoutheaderstring(CgTime time, CgString *buf) { char timeBuf[CG_STRING_LONG_BUFLEN]; cg_log_debug_l4("Entering...\n"); if (time != CG_UPNP_SUBSCRIPTION_INFINITE_VALUE) { cg_string_setvalue(buf, CG_UPNP_SUBSCRIPTION_TIMEOUT_HEADER); cg_string_addvalue(buf, cg_long2str(time, timeBuf, sizeof(timeBuf))); } else cg_string_setvalue(buf, CG_UPNP_SUBSCRIPTION_INFINITE_STRING); return cg_string_getvalue(buf); cg_log_debug_l4("Leaving...\n"); }
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"); }
static char *cg_xml_node_tostring_indent(CgXmlNode *node, int indentLevel, BOOL withChildNode, CgString *str) { char *name; char *value; CgString *valueStr; CgXmlNode *childNode; cg_log_debug_l4("Entering...\n"); name = cg_xml_node_getname(node); value = cg_xml_node_getvalue(node); if (cg_xml_node_haschildnodes(node) == FALSE || withChildNode == FALSE) { cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel); if (!cg_string_naddvalue(str, "<", 1) || !cg_string_addvalue(str, name) || !cg_xml_node_attribute_tostring(node, str)) /* Memory allocation failed */ return NULL; valueStr = cg_string_new(); if (!valueStr) /* Memory allocation failed */ return NULL; cg_string_setvalue(valueStr, value); cg_xml_escapechars(valueStr); if (!cg_string_naddvalue(str, ">", 1) || !cg_string_addvalue(str, cg_string_getvalue(valueStr)) || !cg_string_naddvalue(str, "</", 2) || !cg_string_addvalue(str, name) || !cg_string_naddvalue(str, ">", 1) || !cg_string_addvalue(str, "\n")) { /* Memory allocation failed */ cg_string_delete(valueStr); return NULL; } cg_string_delete(valueStr); return cg_string_getvalue(str); } cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel); if (!cg_string_naddvalue(str, "<", 1) || !cg_string_addvalue(str, name) || !cg_xml_node_attribute_tostring(node, str) || !cg_string_naddvalue(str, ">", 1) || !cg_string_addvalue(str, "\n")) /* Memory allocation failed */ return NULL; for (childNode = cg_xml_node_getchildnodes(node); childNode != NULL; childNode = cg_xml_node_next(childNode)) if (!cg_xml_node_tostring_indent(childNode, indentLevel+1, TRUE, str)) /* Memory allocation failed */ return NULL; cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel); if (!cg_string_naddvalue(str, "</", 2) || !cg_string_addvalue(str, name) || !cg_string_naddvalue(str, ">", 1) || !cg_string_addvalue(str, "\n")) /* Memory allocation failed */ return NULL; cg_log_debug_l4("Leaving...\n"); return cg_string_getvalue(str); }
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; }
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"); }
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"); }
char *cg_net_uri_unescapestring(char *buf, int bufSize, CgString *retBuf) { #if defined(CG_HTTP_CURL) char *tmp; #else int n; char hexStr[3]; long hex; unsigned char c; #endif int idx = 0; #if defined(CG_USE_NET_URI_ESCAPESTRING_SKIP) int tmpIdx = 0; #endif cg_log_debug_l4("Entering...\n"); if (!retBuf) return NULL; /* Check if URI is already escaped */ if (cg_net_uri_isescapedstring(buf + idx, bufSize) == TRUE) return buf; /* We can safely assume that the non-path part is already escaped */ #if defined(CG_USE_NET_URI_ESCAPESTRING_SKIP) idx = cg_strstr(buf, CG_NET_URI_PROTOCOL_DELIM); if (idx > 0) { idx = idx + cg_strlen(CG_NET_URI_PROTOCOL_DELIM); tmpIdx = cg_strstr(buf + idx, CG_NET_URI_SLASH_DELIM); if (tmpIdx > 0) idx += tmpIdx + cg_strlen(CG_NET_URI_SLASH_DELIM); } else { idx = 0; } #endif if (bufSize < 1) bufSize = cg_strlen(buf) + 1; #if defined(CG_HTTP_CURL) tmp = curl_unescape(buf + idx, 0); if (tmp == NULL) return NULL; cg_string_addvalue(retBuf, tmp); cg_log_debug_s("%s ==> %s\n", buf + idx, tmp); curl_free(tmp); #else for (n=0; n<bufSize;) { c = (unsigned char)buf[n]; if (buf[n] == '%' && cg_net_uri_isalphanumchar(buf[n+1]) && cg_net_uri_isalphanumchar(buf[n+2])) { hexStr[0] = buf[n+1]; hexStr[1] = buf[n+2]; hexStr[2] = '\0'; hex = strtol(hexStr, NULL, 16); c = (unsigned char)hex; n += 3; } else n++; cg_string_naddvalue(retBuf, (char *)&c, 1); } #endif cg_log_debug_l4("Leaving...\n"); return cg_string_getvalue(retBuf); }
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"); }