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 b116d10f37)
This commit is contained in:
Fabrice Fontaine
2012-03-14 22:37:10 +01:00
committed by Marcelo Roberto Jimenez
parent 9a1ca957a7
commit b9944242cf
5 changed files with 52 additions and 22 deletions

View File

@@ -318,6 +318,21 @@ Version 1.8.0
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>
Use switch instead of if with enums in upnpapi.c

View File

@@ -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);

View File

@@ -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;

View File

@@ -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) {
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;
}

View File

@@ -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 );