Skip to content

Commit 93c82f1

Browse files
HHHartmannmarcelstoer
authored andcommitted
Fix binary and chunked HTTP downloads (#2985)
Original sources by @anod221
1 parent 7140894 commit 93c82f1

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

app/http/httpclient.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * d
122122
*
123123
*/
124124

125-
return(j);
125+
return(decode_size);
126126
}
127127

128128

@@ -306,6 +306,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
306306
request_args_t * req = (request_args_t *) conn->reverse;
307307
int http_status = -1;
308308
char * body = "";
309+
int body_size = 0;
309310

310311
// Turn off timeout timer
311312
os_timer_disarm( &(req->timeout_timer) );
@@ -411,15 +412,17 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
411412
body = body + 4;
412413
}
413414

415+
body_size = req->buffer_size - (body - req->buffer);
414416
if ( strcasestr( req->buffer, "Transfer-Encoding: chunked" ) )
415417
{
416-
int body_size = req->buffer_size - (body - req->buffer);
417-
char chunked_decode_buffer[body_size];
418+
char *chunked_decode_buffer = os_malloc(body_size);
418419
os_memset( chunked_decode_buffer, 0, body_size );
419420
/* Chuncked data */
420-
http_chunked_decode( body, chunked_decode_buffer );
421+
body_size = http_chunked_decode( body, chunked_decode_buffer );
421422
os_memcpy( body, chunked_decode_buffer, body_size );
423+
os_free( chunked_decode_buffer );
422424
}
425+
else --body_size;
423426
}
424427
}
425428
}
@@ -432,7 +435,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
432435

433436
http_free_req( req );
434437

435-
req_callback( body, http_status, &req_buffer );
438+
req_callback( body, http_status, &req_buffer, body_size );
436439
if (req_buffer) {
437440
os_free(req_buffer);
438441
}
@@ -498,7 +501,7 @@ static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_
498501
HTTPCLIENT_ERR( "DNS failed for %s", hostname );
499502
if ( req->callback_handle != NULL )
500503
{
501-
req->callback_handle( "", -1, NULL );
504+
req->callback_handle( "", -1, NULL, 0 );
502505
}
503506
http_free_req( req );
504507
}

app/http/httpclient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static const char log_prefix[] = "HTTP client: ";
5353
* A successful request corresponds to an HTTP status code of 200 (OK).
5454
* More info at http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
5555
*/
56-
typedef void (* http_callback_t)(char * response_body, int http_status, char ** full_response_p);
56+
typedef void (* http_callback_t)(char * response_body, int http_status, char ** full_response_p, int body_size);
5757

5858
/*
5959
* Call this function to skip URL parsing if the arguments are already in separate variables.

app/modules/http.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <ctype.h>
1414
static int http_callback_registry = LUA_NOREF;
1515

16-
static void http_callback( char * response, int http_status, char ** full_response_p )
16+
static void http_callback( char * response, int http_status, char ** full_response_p, int body_size )
1717
{
1818
const char *full_response = full_response_p ? *full_response_p : NULL;
1919

@@ -36,7 +36,7 @@ static void http_callback( char * response, int http_status, char ** full_respon
3636
lua_pushnumber(L, http_status);
3737
if ( http_status != HTTP_STATUS_GENERIC_ERROR && response)
3838
{
39-
lua_pushstring(L, response);
39+
lua_pushlstring(L, response, (size_t)body_size);
4040
lua_newtable(L);
4141

4242
const char *p = full_response;

0 commit comments

Comments
 (0)