|
|
|
@ -1,8 +1,8 @@
|
|
|
|
|
/* $Id: miniwget.c,v 1.70 2015/07/15 12:41:13 nanard Exp $ */
|
|
|
|
|
/* $Id: miniwget.c,v 1.75 2016/01/24 17:24:36 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",
|
|
|
|
@ -337,7 +362,8 @@ static void *
|
|
|
|
|
miniwget3(const char * host,
|
|
|
|
|
unsigned short port, const char * path,
|
|
|
|
|
int * size, char * addr_str, int addr_str_len,
|
|
|
|
|
const char * httpversion, unsigned int scope_id)
|
|
|
|
|
const char * httpversion, unsigned int scope_id,
|
|
|
|
|
int * status_code)
|
|
|
|
|
{
|
|
|
|
|
char buf[2048];
|
|
|
|
|
int s;
|
|
|
|
@ -435,7 +461,7 @@ miniwget3(const char * host,
|
|
|
|
|
sent += n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
content = getHTTPResponse(s, size);
|
|
|
|
|
content = getHTTPResponse(s, size, status_code);
|
|
|
|
|
closesocket(s);
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
@ -444,18 +470,20 @@ miniwget3(const char * host,
|
|
|
|
|
* Call miniwget3(); retry with HTTP/1.1 if 1.0 fails. */
|
|
|
|
|
static void *
|
|
|
|
|
miniwget2(const char * host,
|
|
|
|
|
unsigned short port, const char * path,
|
|
|
|
|
int * size, char * addr_str, int addr_str_len,
|
|
|
|
|
unsigned int scope_id)
|
|
|
|
|
unsigned short port, const char * path,
|
|
|
|
|
int * size, char * addr_str, int addr_str_len,
|
|
|
|
|
unsigned int scope_id, int * status_code)
|
|
|
|
|
{
|
|
|
|
|
char * respbuffer;
|
|
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
|
respbuffer = miniwget3(host, port, path, size,
|
|
|
|
|
addr_str, addr_str_len, "1.1", scope_id);
|
|
|
|
|
addr_str, addr_str_len, "1.1",
|
|
|
|
|
scope_id, status_code);
|
|
|
|
|
#else
|
|
|
|
|
respbuffer = miniwget3(host, port, path, size,
|
|
|
|
|
addr_str, addr_str_len, "1.0", scope_id);
|
|
|
|
|
addr_str, addr_str_len, "1.0",
|
|
|
|
|
scope_id, status_code);
|
|
|
|
|
if (*size == 0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
@ -463,7 +491,8 @@ miniwget2(const char * host,
|
|
|
|
|
#endif
|
|
|
|
|
free(respbuffer);
|
|
|
|
|
respbuffer = miniwget3(host, port, path, size,
|
|
|
|
|
addr_str, addr_str_len, "1.1", scope_id);
|
|
|
|
|
addr_str, addr_str_len, "1.1",
|
|
|
|
|
scope_id, status_code);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return respbuffer;
|
|
|
|
@ -588,7 +617,8 @@ parseURL(const char * url,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
miniwget(const char * url, int * size, unsigned int scope_id)
|
|
|
|
|
miniwget(const char * url, int * size,
|
|
|
|
|
unsigned int scope_id, int * status_code)
|
|
|
|
|
{
|
|
|
|
|
unsigned short port;
|
|
|
|
|
char * path;
|
|
|
|
@ -601,12 +631,13 @@ miniwget(const char * url, int * size, unsigned int scope_id)
|
|
|
|
|
printf("parsed url : hostname='%s' port=%hu path='%s' scope_id=%u\n",
|
|
|
|
|
hostname, port, path, scope_id);
|
|
|
|
|
#endif
|
|
|
|
|
return miniwget2(hostname, port, path, size, 0, 0, scope_id);
|
|
|
|
|
return miniwget2(hostname, port, path, size, 0, 0, scope_id, status_code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
miniwget_getaddr(const char * url, int * size,
|
|
|
|
|
char * addr, int addrlen, unsigned int scope_id)
|
|
|
|
|
char * addr, int addrlen, unsigned int scope_id,
|
|
|
|
|
int * status_code)
|
|
|
|
|
{
|
|
|
|
|
unsigned short port;
|
|
|
|
|
char * path;
|
|
|
|
@ -621,6 +652,6 @@ miniwget_getaddr(const char * url, int * size,
|
|
|
|
|
printf("parsed url : hostname='%s' port=%hu path='%s' scope_id=%u\n",
|
|
|
|
|
hostname, port, path, scope_id);
|
|
|
|
|
#endif
|
|
|
|
|
return miniwget2(hostname, port, path, size, addr, addrlen, scope_id);
|
|
|
|
|
return miniwget2(hostname, port, path, size, addr, addrlen, scope_id, status_code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|