parse HTTP response status line in miniwget.c

This commit is contained in:
Thomas Bernard 2016-01-22 16:53:19 +01:00
parent 2808e5c1c8
commit d1243e157d
3 changed files with 42 additions and 10 deletions

View File

@ -1,8 +1,9 @@
$Id: Changelog.txt,v 1.220 2016/01/22 14:19:54 nanard Exp $
$Id: Changelog.txt,v 1.221 2016/01/22 15:07:27 nanard Exp $
miniUPnP client Changelog.
2016/01/22:
Improve UPNPIGD_IsConnected() to check if WAN address is not private.
parse HTTP response status line in miniwget.c
2015/10/26:
snprintf() overflow check. check overflow in simpleUPnPcommand2()

View File

@ -1,4 +1,4 @@
/* $Id: miniupnpc.c,v 1.144 2016/01/22 14:19:55 nanard Exp $ */
/* $Id: miniupnpc.c,v 1.146 2016/01/22 15:19:42 nanard Exp $ */
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* Project : miniupnp
* Web : http://miniupnp.free.fr/
@ -126,6 +126,7 @@ char * simpleUPnPcommand2(int s, const char * url, const char * service,
int soapbodylen;
char * buf;
int n;
int status_code;
*bufsize = 0;
snprintf(soapact, sizeof(soapact), "%s#%s", service, action);
@ -229,11 +230,15 @@ char * simpleUPnPcommand2(int s, const char * url, const char * service,
return NULL;
}
buf = getHTTPResponse(s, bufsize);
buf = getHTTPResponse(s, bufsize, &status_code);
#ifdef DEBUG
if(*bufsize > 0 && buf)
{
printf("SOAP Response :\n%.*s\n", *bufsize, buf);
printf("HTTP %d SOAP Response :\n%.*s\n", status_code, *bufsize, buf);
}
else
{
printf("HTTP %d, empty SOAP response. size=%d\n", status_code, *bufsize);
}
#endif
closesocket(s);

View File

@ -1,8 +1,8 @@
/* $Id: miniwget.c,v 1.70 2015/07/15 12:41:13 nanard Exp $ */
/* $Id: miniwget.c,v 1.74 2016/01/22 15:19:43 nanard Exp $ */
/* Project : miniupnp
* Website : http://miniupnp.free.fr/
* Author : Thomas Bernard
* Copyright (c) 2005-2015 Thomas Bernard
* Copyright (c) 2005-2016 Thomas Bernard
* This software is subject to the conditions detailed in the
* LICENCE file provided in this distribution. */
@ -65,7 +65,7 @@
* to the length parameter.
*/
void *
getHTTPResponse(int s, int * size)
getHTTPResponse(int s, int * size, int * status_code)
{
char buf[2048];
int n;
@ -83,7 +83,10 @@ getHTTPResponse(int s, int * size)
unsigned int content_buf_used = 0;
char chunksize_buf[32];
unsigned int chunksize_buf_index;
char * reason_phrase = NULL;
int reason_phrase_len = 0;
if(status_code) *status_code = -1;
header_buf = malloc(header_buf_len);
if(header_buf == NULL)
{
@ -155,7 +158,7 @@ getHTTPResponse(int s, int * size)
continue;
/* parse header lines */
for(i = 0; i < endofheaders - 1; i++) {
if(colon <= linestart && header_buf[i]==':')
if(linestart > 0 && colon <= linestart && header_buf[i]==':')
{
colon = i;
while(i < (endofheaders-1)
@ -166,7 +169,29 @@ getHTTPResponse(int s, int * size)
/* detecting end of line */
else if(header_buf[i]=='\r' || header_buf[i]=='\n')
{
if(colon > linestart && valuestart > colon)
if(linestart == 0 && status_code)
{
/* Status line
* HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
int sp;
for(sp = 0; sp < i; sp++)
if(header_buf[sp] == ' ')
{
if(*status_code < 0)
*status_code = atoi(header_buf + sp + 1);
else
{
reason_phrase = header_buf + sp + 1;
reason_phrase_len = i - sp - 1;
break;
}
}
#ifdef DEBUG
printf("HTTP status code = %d, Reason phrase = %.*s\n",
*status_code, reason_phrase_len, reason_phrase);
#endif
}
else if(colon > linestart && valuestart > colon)
{
#ifdef DEBUG
printf("header='%.*s', value='%.*s'\n",
@ -345,6 +370,7 @@ miniwget3(const char * host,
int len;
int sent;
void * content;
int status_code;
*size = 0;
s = connecthostport(host, port, scope_id);
@ -435,7 +461,7 @@ miniwget3(const char * host,
sent += n;
}
}
content = getHTTPResponse(s, size);
content = getHTTPResponse(s, size, &status_code);
closesocket(s);
return content;
}