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.
This commit is contained in:
Fabrice Fontaine 2012-03-14 22:37:10 +01:00
parent 1a083479a9
commit b116d10f37
5 changed files with 56 additions and 27 deletions

View File

@ -2,6 +2,21 @@
Version 1.6.16 Version 1.6.16
******************************************************************************* *******************************************************************************
2012-03-15 Fabrice Fontaine <fabrice.fontaine(at)orange.com>
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 <fabrice.fontaine(at)orange.com> 2012-03-14 Fabrice Fontaine <fabrice.fontaine(at)orange.com>
Use switch instead of if with enums in upnpapi.c Use switch instead of if with enums in upnpapi.c

View File

@ -1138,7 +1138,7 @@ static parse_status_t match(
IN const char *fmt, IN const char *fmt,
...) ...)
{ {
int ret_code; parse_status_t ret_code;
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);

View File

@ -453,7 +453,7 @@ int http_SendMessage(SOCKINFO *info, int *TimeOut, const char *fmt, ...)
if (Instr && Instr->IsChunkActive) { if (Instr && Instr->IsChunkActive) {
int rc; int rc;
/* Copy CRLF at the end of the chunk */ /* 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. */ /* Hex length for the chunk size. */
memset(Chunk_Header, 0, memset(Chunk_Header, 0,
sizeof(Chunk_Header)); sizeof(Chunk_Header));
@ -724,6 +724,10 @@ int http_Download( IN const char *url_str,
/* shrink can't fail */ /* shrink can't fail */
assert(msg_length > *doc_length); assert(msg_length > *doc_length);
assert(*document != NULL); 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) { if (response.msg.status_code == HTTP_OK) {
ret_code = 0; /* success */ ret_code = 0; /* success */
@ -874,7 +878,7 @@ int http_WriteHttpPost( IN void *Handle,
sprintf(tempbuf, "%" PRIzx "\r\n", *size); sprintf(tempbuf, "%" PRIzx "\r\n", *size);
tempSize = strlen(tempbuf); tempSize = strlen(tempbuf);
memcpy(tempbuf + tempSize, buf, *size); memcpy(tempbuf + tempSize, buf, *size);
memcpy(tempbuf + tempSize + *size, "\r\n", 2); memcpy(tempbuf + tempSize + *size, "\r\n", (size_t)2);
/* end of chunk */ /* end of chunk */
tempbufSize = tempSize + *size + (size_t)2; tempbufSize = tempSize + *size + (size_t)2;
freeTempbuf = 1; freeTempbuf = 1;
@ -1113,7 +1117,7 @@ int MakeGetMessage(const char *url_str, const char *proxy_str,
* \li \c PARSE_FAILURE - Failure to parse data correctly * \li \c PARSE_FAILURE - Failure to parse data correctly
* \li \c UPNP_E_BAD_HTTPMSG - Socker read() returns an error * \li \c UPNP_E_BAD_HTTPMSG - Socker read() returns an error
*/ */
static parse_status_t ReadResponseLineAndHeaders( static int ReadResponseLineAndHeaders(
/*! Socket information object. */ /*! Socket information object. */
IN SOCKINFO *info, IN SOCKINFO *info,
/*! HTTP Parser object. */ /*! HTTP Parser object. */
@ -1511,10 +1515,9 @@ int http_OpenHttpGetProxy(const char *url_str, const char *proxy_str,
sock_destroy(&handle->sock_info, SD_BOTH); sock_destroy(&handle->sock_info, SD_BOTH);
goto errorHandler; goto errorHandler;
} }
status = ReadResponseLineAndHeaders(&handle->sock_info, if (ReadResponseLineAndHeaders(&handle->sock_info,
&handle->response, &timeout, &handle->response, &timeout,
&http_error_code); &http_error_code) != (int)PARSE_OK) {
if (status != (parse_status_t)PARSE_OK) {
ret_code = UPNP_E_BAD_RESPONSE; ret_code = UPNP_E_BAD_RESPONSE;
goto errorHandler; goto errorHandler;
} }
@ -2055,9 +2058,8 @@ int http_OpenHttpGetEx(
free(handle); free(handle);
break; break;
} }
status = ReadResponseLineAndHeaders(&handle->sock_info, if (ReadResponseLineAndHeaders(&handle->sock_info,
&handle->response, &timeout, &http_error_code); &handle->response, &timeout, &http_error_code) != (int)PARSE_OK) {
if (status != (parse_status_t)PARSE_OK) {
errCode = UPNP_E_BAD_RESPONSE; errCode = UPNP_E_BAD_RESPONSE;
free(handle); free(handle);
break; break;

View File

@ -2,6 +2,7 @@
* *
* Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2000-2003 Intel Corporation
* All rights reserved. * All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * 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); assert(hmsg);
/* find 'content-type' header which must have text/xml */ /* find 'content-type' header which must have text/xml */
if (httpmsg_find_hdr(hmsg, HDR_CONTENT_TYPE, &hdr_value) && if (httpmsg_find_hdr(hmsg, HDR_CONTENT_TYPE, &hdr_value)) {
matchstr(hdr_value.buf, hdr_value.length, "%itext%w/%wxml" ) == PARSE_OK) { switch (matchstr(hdr_value.buf, hdr_value.length, "%itext%w/%wxml" )) {
return TRUE; case PARSE_OK:
return TRUE;
default:
break;
}
} }
return FALSE; return FALSE;
} }

View File

@ -136,8 +136,8 @@ int replace_escaped(char *in, size_t index, size_t *max)
{ {
int tempInt = 0; int tempInt = 0;
char tempChar = 0; char tempChar = 0;
size_t i = 0; size_t i = (size_t)0;
size_t j = 0; size_t j = (size_t)0;
if (in[index] == '%' && isxdigit(in[index + (size_t)1]) && isxdigit(in[index + (size_t)2])) { 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 /* Note the "%2x", makes sure that we convert a maximum of two
@ -326,7 +326,7 @@ int parse_hostport(
char *srvport = NULL; char *srvport = NULL;
char *last_dot = NULL; char *last_dot = NULL;
unsigned short int port; unsigned short int port;
unsigned short af = AF_UNSPEC; int af = AF_UNSPEC;
size_t hostport_size; size_t hostport_size;
int has_port = 0; int has_port = 0;
int ret; int ret;
@ -378,13 +378,17 @@ int parse_hostport(
ret = getaddrinfo(srvname, NULL, &hints, &res0); ret = getaddrinfo(srvname, NULL, &hints, &res0);
if (ret == 0) { if (ret == 0) {
for (res = res0; res; res = res->ai_next) { for (res = res0; res && !ret; res = res->ai_next) {
if (res->ai_family == AF_INET || switch (res->ai_family) {
res->ai_family == AF_INET6) { case AF_INET:
case AF_INET6:
/* Found a valid IPv4 or IPv6 address. */ /* Found a valid IPv4 or IPv6 address. */
memcpy(&out->IPaddress, memcpy(&out->IPaddress,
res->ai_addr, res->ai_addr,
res->ai_addrlen); res->ai_addrlen);
ret=1;
break;
default:
break; break;
} }
} }
@ -414,19 +418,22 @@ int parse_hostport(
/* subtracting pointers. */ /* subtracting pointers. */
hostport_size = (size_t)c - (size_t)workbuf; hostport_size = (size_t)c - (size_t)workbuf;
/* Fill in the 'out' information. */ /* Fill in the 'out' information. */
if (af == (unsigned short)AF_INET) { switch (af) {
sai4->sin_family = af; case AF_INET:
sai4->sin_family = (sa_family_t)af;
sai4->sin_port = htons(port); sai4->sin_port = htons(port);
ret = inet_pton(AF_INET, srvname, &sai4->sin_addr); ret = inet_pton(AF_INET, srvname, &sai4->sin_addr);
} else if (af == (unsigned short)AF_INET6) { break;
sai6->sin6_family = af; case AF_INET6:
sai6->sin6_family = (sa_family_t)af;
sai6->sin6_port = htons(port); sai6->sin6_port = htons(port);
sai6->sin6_scope_id = gIF_INDEX; sai6->sin6_scope_id = gIF_INDEX;
ret = inet_pton(AF_INET6, srvname, &sai6->sin6_addr); ret = inet_pton(AF_INET6, srvname, &sai6->sin6_addr);
} else { break;
default:
/* IP address was set by the hostname (getaddrinfo). */ /* IP address was set by the hostname (getaddrinfo). */
/* Override port: */ /* 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); sai4->sin_port = htons(port);
else else
sai6->sin6_port = htons(port); sai6->sin6_port = htons(port);
@ -618,7 +625,7 @@ char *resolve_rel_url(char *base_url, char *rel_url)
if( base.hostport.text.size > (size_t)0 ) { if( base.hostport.text.size > (size_t)0 ) {
assert( base.scheme.size + (size_t)1 assert( base.scheme.size + (size_t)1
+ base.hostport.text.size + (size_t)2 /* "//" */ <= strlen ( base_url ) ); + base.hostport.text.size + (size_t)2 /* "//" */ <= strlen ( base_url ) );
memcpy( out_finger, "//", 2 ); memcpy( out_finger, "//", (size_t)2 );
out_finger += 2; out_finger += 2;
memcpy( out_finger, base.hostport.text.buff, memcpy( out_finger, base.hostport.text.buff,
base.hostport.text.size ); base.hostport.text.size );