/* Get the HTTP Content length from the HTTP Header. If content length can't be found in the header, it will return -1 in the content_length parameter. */ static int Get_Http_Content_Length(char * http_header, int * content_length) { char * cl_loc; char * http_upper_header = (char *)malloc(strlen(http_header)+NULL_TERM_LEN); if(NULL == http_upper_header) { return -1; } (void)LNat_Str_To_Upper(http_header, http_upper_header); cl_loc = strstr(http_upper_header, HTTP_CONTENT_LEN_TAG); if(NULL == cl_loc) { *content_length = MAX_HTTP_BODY_LEN; free(http_upper_header); return OK; } /* put the cl_loc in the correct position in the original http_header */ cl_loc = http_header + (cl_loc - http_upper_header) + strlen(HTTP_CONTENT_LEN_TAG); if(sscanf(cl_loc, HTTP_CONTENT_LEN_SEARCH, content_length) != 1) { *content_length = MAX_HTTP_BODY_LEN; free(http_upper_header); return OK; } if(*content_length > MAX_HTTP_BODY_LEN) { *content_length = MAX_HTTP_BODY_LEN; } free(http_upper_header); return OK; }
/* Get the url to retrieve the upnp description from. This URL is parsed out of the ssdp response retrieved from an ssdp request. */ static int Get_Description_Url(const char * ssdp_response, char ** desc_url) { const char * fir_rn; const char * sec_rn; const char * end_loc_tag; int end_loc_offset; int desc_url_malloc_size; char * ssdp_upper_response = (char *)malloc(strlen(ssdp_response) + NULL_TERM_LEN); if(NULL == ssdp_upper_response) { return BAD_MALLOC; } /* parsing done on all caps version */ (void)LNat_Str_To_Upper(ssdp_response, ssdp_upper_response); /* if we didnt receive an HTTP OK response, return error */ if(NULL == strstr(ssdp_response, HTTP_OK)) { free(ssdp_upper_response); return SSDP_HEADER_NOT_OK; } /* we will scan for successive pairs of /r/n's, and evaluate the data between them to see if it is the location tag. if it is the location tag, then copy the data between that, and the last /r/n. */ fir_rn = ssdp_upper_response; while(1) { if((fir_rn = strstr(fir_rn, "\r\n")) == NULL) { break; } fir_rn += strlen("\r\n"); if((sec_rn = strstr(fir_rn, "\r\n")) == NULL) { break; } end_loc_tag = fir_rn + strlen(LOCATION_TAG); end_loc_offset = end_loc_tag - ssdp_upper_response; if(!strncmp(fir_rn, LOCATION_TAG, strlen(LOCATION_TAG))) { desc_url_malloc_size = (sec_rn - end_loc_tag) + NULL_TERM_LEN; *desc_url = (char *)malloc(desc_url_malloc_size); if(NULL == *desc_url) { free(ssdp_upper_response); return BAD_MALLOC; } sscanf(ssdp_response + end_loc_offset, "%s", *desc_url); free(ssdp_upper_response); return OK; } } free(ssdp_upper_response); /* now make sure that the url size is appropriate */ if(strlen(*desc_url)+NULL_TERM_LEN > MAX_URL_LEN) { free(*desc_url); return UPNP_URL_OVER_MAX_LEN; } return SSDP_NO_LOCATION; }