From b9944242cf833523059eebcc1b3289c60a459566 Mon Sep 17 00:00:00 2001 From: Fabrice Fontaine Date: Wed, 14 Mar 2012 22:37:10 +0100 Subject: [PATCH] Improve upnp/genlib/net Change ret_code from int to parse_status_t in match. Set back return code of ReadResponseLineAndHeaders from parse_status_t to int as this function can return UPNP_E_BAD_HTTPMSG. As a result, do not cast the result of this function into parse_status_t in http_OpenHttpGetProxy and http_OpenHttpGetEx. Use switch with PARSE_OK in parsetools.c. Add missing explicit casts of integer constants in uri.c and httpreadwrite.c. Use switch, int and sa_family_t with AF_INET in uri.c. Print an error in http_Download if realloc failed. (forward port of commit b116d10f3700ed462af117055ddcbe65e1729a0a) --- ChangeLog | 15 +++++++++++ upnp/src/genlib/net/http/httpparser.c | 2 +- upnp/src/genlib/net/http/httpreadwrite.c | 13 ++++++---- upnp/src/genlib/net/http/parsetools.c | 11 +++++--- upnp/src/genlib/net/uri/uri.c | 33 ++++++++++++++---------- 5 files changed, 52 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc021bc..da3d348 100644 --- a/ChangeLog +++ b/ChangeLog @@ -318,6 +318,21 @@ Version 1.8.0 Version 1.6.16 ******************************************************************************* +2012-03-15 Fabrice Fontaine + + Improve upnp/genlib/net + + Change ret_code from int to parse_status_t in match. + Set back return code of ReadResponseLineAndHeaders from parse_status_t + to int as this function can return UPNP_E_BAD_HTTPMSG. As a result, do + not cast the result of this function into parse_status_t in + http_OpenHttpGetProxy and http_OpenHttpGetEx. + Use switch with PARSE_OK in parsetools.c. + Add missing explicit casts of integer constants in uri.c and + httpreadwrite.c. + Use switch, int and sa_family_t with AF_INET in uri.c. + Print an error in http_Download if realloc failed. + 2012-03-14 Fabrice Fontaine Use switch instead of if with enums in upnpapi.c diff --git a/upnp/src/genlib/net/http/httpparser.c b/upnp/src/genlib/net/http/httpparser.c index 5405205..8a2681e 100644 --- a/upnp/src/genlib/net/http/httpparser.c +++ b/upnp/src/genlib/net/http/httpparser.c @@ -1140,7 +1140,7 @@ static parse_status_t match( IN const char *fmt, ...) { - int ret_code; + parse_status_t ret_code; va_list args; va_start(args, fmt); diff --git a/upnp/src/genlib/net/http/httpreadwrite.c b/upnp/src/genlib/net/http/httpreadwrite.c index f579a55..bd43d0a 100644 --- a/upnp/src/genlib/net/http/httpreadwrite.c +++ b/upnp/src/genlib/net/http/httpreadwrite.c @@ -513,7 +513,7 @@ int http_SendMessage(SOCKINFO *info, int *TimeOut, const char *fmt, ...) if (Instr && Instr->IsChunkActive) { int rc; /* Copy CRLF at the end of the chunk */ - memcpy(file_buf + num_read, "\r\n", 2); + memcpy(file_buf + num_read, "\r\n", (size_t)2); /* Hex length for the chunk size. */ memset(Chunk_Header, 0, sizeof(Chunk_Header)); @@ -784,6 +784,10 @@ int http_Download( IN const char *url_str, /* shrink can't fail */ assert(msg_length > *doc_length); assert(*document != NULL); + if (msg_length <= *doc_length || *document == NULL) + UpnpPrintf(UPNP_INFO, HTTP, __FILE__, __LINE__, + "msg_length(%d) <= *doc_length(%d) or document" + " is NULL", msg_length, *doc_length); } if (response.msg.status_code == HTTP_OK) { ret_code = 0; /* success */ @@ -907,7 +911,7 @@ typedef struct HTTPCONNECTIONHANDLE { * \li \c PARSE_FAILURE - Failure to parse data correctly * \li \c UPNP_E_BAD_HTTPMSG - Socker read() returns an error */ -static parse_status_t ReadResponseLineAndHeaders( +static int ReadResponseLineAndHeaders( /*! Socket information object. */ IN SOCKINFO *info, /*! HTTP Parser object. */ @@ -1896,9 +1900,8 @@ int http_OpenHttpGetEx( free(handle); break; } - status = ReadResponseLineAndHeaders(&handle->sock_info, - &handle->response, &timeout, &http_error_code); - if (status != (parse_status_t)PARSE_OK) { + if (ReadResponseLineAndHeaders(&handle->sock_info, + &handle->response, &timeout, &http_error_code) != (int)PARSE_OK) { errCode = UPNP_E_BAD_RESPONSE; free(handle); break; diff --git a/upnp/src/genlib/net/http/parsetools.c b/upnp/src/genlib/net/http/parsetools.c index 0b294e9..b1909ec 100644 --- a/upnp/src/genlib/net/http/parsetools.c +++ b/upnp/src/genlib/net/http/parsetools.c @@ -2,6 +2,7 @@ * * Copyright (c) 2000-2003 Intel Corporation * All rights reserved. + * Copyright (c) 2012 France Telecom All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -51,9 +52,13 @@ int has_xml_content_type(http_message_t *hmsg) assert(hmsg); /* find 'content-type' header which must have text/xml */ - if (httpmsg_find_hdr(hmsg, HDR_CONTENT_TYPE, &hdr_value) && - matchstr(hdr_value.buf, hdr_value.length, "%itext%w/%wxml" ) == PARSE_OK) { - return TRUE; + if (httpmsg_find_hdr(hmsg, HDR_CONTENT_TYPE, &hdr_value)) { + switch (matchstr(hdr_value.buf, hdr_value.length, "%itext%w/%wxml" )) { + case PARSE_OK: + return TRUE; + default: + break; + } } return FALSE; } diff --git a/upnp/src/genlib/net/uri/uri.c b/upnp/src/genlib/net/uri/uri.c index 8d98fea..16d5596 100644 --- a/upnp/src/genlib/net/uri/uri.c +++ b/upnp/src/genlib/net/uri/uri.c @@ -136,8 +136,8 @@ int replace_escaped(char *in, size_t index, size_t *max) { int tempInt = 0; char tempChar = 0; - size_t i = 0; - size_t j = 0; + size_t i = (size_t)0; + size_t j = (size_t)0; if (in[index] == '%' && isxdigit(in[index + (size_t)1]) && isxdigit(in[index + (size_t)2])) { /* Note the "%2x", makes sure that we convert a maximum of two @@ -327,7 +327,7 @@ int parse_hostport( char *srvport = NULL; char *last_dot = NULL; unsigned short int port; - unsigned short af = AF_UNSPEC; + int af = AF_UNSPEC; size_t hostport_size; int has_port = 0; int ret; @@ -379,13 +379,17 @@ int parse_hostport( ret = getaddrinfo(srvname, NULL, &hints, &res0); if (ret == 0) { - for (res = res0; res; res = res->ai_next) { - if (res->ai_family == AF_INET || - res->ai_family == AF_INET6) { + for (res = res0; res && !ret; res = res->ai_next) { + switch (res->ai_family) { + case AF_INET: + case AF_INET6: /* Found a valid IPv4 or IPv6 address. */ memcpy(&out->IPaddress, res->ai_addr, res->ai_addrlen); + ret=1; + break; + default: break; } } @@ -415,19 +419,22 @@ int parse_hostport( /* subtracting pointers. */ hostport_size = (size_t)c - (size_t)workbuf; /* Fill in the 'out' information. */ - if (af == (unsigned short)AF_INET) { - sai4->sin_family = af; + switch (af) { + case AF_INET: + sai4->sin_family = (sa_family_t)af; sai4->sin_port = htons(port); ret = inet_pton(AF_INET, srvname, &sai4->sin_addr); - } else if (af == (unsigned short)AF_INET6) { - sai6->sin6_family = af; + break; + case AF_INET6: + sai6->sin6_family = (sa_family_t)af; sai6->sin6_port = htons(port); sai6->sin6_scope_id = gIF_INDEX; ret = inet_pton(AF_INET6, srvname, &sai6->sin6_addr); - } else { + break; + default: /* IP address was set by the hostname (getaddrinfo). */ /* Override port: */ - if (out->IPaddress.ss_family == (unsigned short)AF_INET) + if (out->IPaddress.ss_family == (sa_family_t)AF_INET) sai4->sin_port = htons(port); else sai6->sin6_port = htons(port); @@ -619,7 +626,7 @@ char *resolve_rel_url(char *base_url, char *rel_url) if( base.hostport.text.size > (size_t)0 ) { assert( base.scheme.size + (size_t)1 + base.hostport.text.size + (size_t)2 /* "//" */ <= strlen ( base_url ) ); - memcpy( out_finger, "//", 2 ); + memcpy( out_finger, "//", (size_t)2 ); out_finger += 2; memcpy( out_finger, base.hostport.text.buff, base.hostport.text.size );