Minimal changes to handle bad bodies (#1212)
Fix failure to compile Move the check into the http module Reverted change
This commit is contained in:
parent
b32a161bc1
commit
61c2b4dfe3
|
@ -18,6 +18,7 @@
|
|||
#include "mem.h"
|
||||
#include "limits.h"
|
||||
#include "httpclient.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
/* Internal state. */
|
||||
typedef struct request_args_t {
|
||||
|
@ -80,110 +81,6 @@ esp_isdigit( char c )
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
* Convert a string to a long integer.
|
||||
*
|
||||
* Ignores `locale' stuff. Assumes that the upper and lower case
|
||||
* alphabets and digits are each contiguous.
|
||||
*/
|
||||
long ICACHE_FLASH_ATTR
|
||||
esp_strtol( nptr, endptr, base )
|
||||
const char *nptr;
|
||||
|
||||
|
||||
char **endptr;
|
||||
int base;
|
||||
{
|
||||
const char *s = nptr;
|
||||
unsigned long acc;
|
||||
int c;
|
||||
unsigned long cutoff;
|
||||
int neg = 0, any, cutlim;
|
||||
|
||||
|
||||
/*
|
||||
* Skip white space and pick up leading +/- sign if any.
|
||||
* If base is 0, allow 0x for hex and 0 for octal, else
|
||||
* assume decimal; if base is already 16, allow 0x.
|
||||
*/
|
||||
do
|
||||
{
|
||||
c = *s++;
|
||||
}
|
||||
while ( esp_isspace( c ) );
|
||||
if ( c == '-' )
|
||||
{
|
||||
neg = 1;
|
||||
c = *s++;
|
||||
} else if ( c == '+' )
|
||||
c = *s++;
|
||||
if ( (base == 0 || base == 16) &&
|
||||
c == '0' && (*s == 'x' || *s == 'X') )
|
||||
{
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 16;
|
||||
} else if ( (base == 0 || base == 2) &&
|
||||
c == '0' && (*s == 'b' || *s == 'B') )
|
||||
{
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 2;
|
||||
}
|
||||
if ( base == 0 )
|
||||
base = c == '0' ? 8 : 10;
|
||||
|
||||
|
||||
/*
|
||||
* Compute the cutoff value between legal numbers and illegal
|
||||
* numbers. That is the largest legal value, divided by the
|
||||
* base. An input number that is greater than this value, if
|
||||
* followed by a legal input character, is too big. One that
|
||||
* is equal to this value may be valid or not; the limit
|
||||
* between valid and invalid numbers is then based on the last
|
||||
* digit. For instance, if the range for longs is
|
||||
* [-2147483648..2147483647] and the input base is 10,
|
||||
* cutoff will be set to 214748364 and cutlim to either
|
||||
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
|
||||
* a value > 214748364, or equal but the next digit is > 7 (or 8),
|
||||
* the number is too big, and we will return a range error.
|
||||
*
|
||||
* Set any if any `digits' consumed; make it negative to indicate
|
||||
* overflow.
|
||||
*/
|
||||
cutoff = neg ? -(unsigned long) LONG_MIN : LONG_MAX;
|
||||
cutlim = cutoff % (unsigned long) base;
|
||||
cutoff /= (unsigned long) base;
|
||||
for ( acc = 0, any = 0;; c = *s++ )
|
||||
{
|
||||
if ( esp_isdigit( c ) )
|
||||
c -= '0';
|
||||
else if ( esp_isalpha( c ) )
|
||||
c -= esp_isupper( c ) ? 'A' - 10 : 'a' - 10;
|
||||
else
|
||||
break;
|
||||
if ( c >= base )
|
||||
break;
|
||||
if ( any < 0 || acc > cutoff || acc == cutoff && c > cutlim )
|
||||
any = -1;
|
||||
else
|
||||
{
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
if ( any < 0 )
|
||||
{
|
||||
acc = neg ? LONG_MIN : LONG_MAX;
|
||||
/* errno = ERANGE; */
|
||||
} else if ( neg )
|
||||
acc = -acc;
|
||||
if ( endptr != 0 )
|
||||
*endptr = (char *) (any ? s - 1 : nptr);
|
||||
return(acc);
|
||||
}
|
||||
|
||||
static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * decode )
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
|
@ -191,9 +88,9 @@ static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * d
|
|||
char *str = (char *) chunked;
|
||||
do
|
||||
{
|
||||
char * endstr = NULL;
|
||||
char * endstr;
|
||||
/* [chunk-size] */
|
||||
i = esp_strtol( str + j, endstr, 16 );
|
||||
i = strtoul( str + j, NULL, 16 );
|
||||
HTTPCLIENT_DEBUG( "Chunk Size:%d\r\n", i );
|
||||
if ( i <= 0 )
|
||||
break;
|
||||
|
@ -349,7 +246,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
|
|||
|
||||
if ( req->buffer == NULL )
|
||||
{
|
||||
HTTPCLIENT_DEBUG( "Buffer shouldn't be NULL\n" );
|
||||
HTTPCLIENT_DEBUG( "Buffer probably shouldn't be NULL\n" );
|
||||
}
|
||||
else if ( req->buffer[0] != '\0' )
|
||||
{
|
||||
|
@ -364,7 +261,18 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
|
|||
else
|
||||
{
|
||||
http_status = atoi( req->buffer + strlen( version_1_0 ) );
|
||||
body = (char *) os_strstr( req->buffer, "\r\n\r\n" ) + 4;
|
||||
body = (char *) os_strstr(req->buffer, "\r\n\r\n");
|
||||
|
||||
if (NULL == body) {
|
||||
/* Find missing body */
|
||||
HTTPCLIENT_DEBUG("Body shouldn't be NULL\n");
|
||||
/* To avoid NULL body */
|
||||
body = "";
|
||||
} else {
|
||||
/* Skip CR & LF */
|
||||
body = body + 4;
|
||||
}
|
||||
|
||||
if ( os_strstr( req->buffer, "Transfer-Encoding: chunked" ) )
|
||||
{
|
||||
int body_size = req->buffer_size - (body - req->buffer);
|
||||
|
@ -380,7 +288,9 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
|
|||
{
|
||||
req->callback_handle( body, http_status, req->buffer );
|
||||
}
|
||||
os_free( req->buffer );
|
||||
if (req->buffer) {
|
||||
os_free( req->buffer );
|
||||
}
|
||||
os_free( req->hostname );
|
||||
os_free( req->method );
|
||||
os_free( req->path );
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
#include "httpclient.h"
|
||||
|
||||
static int http_callback_registry = LUA_NOREF;
|
||||
static lua_State * http_client_L = NULL;
|
||||
|
||||
static void http_callback( char * response, int http_status, char * full_response )
|
||||
{
|
||||
|
@ -18,26 +17,30 @@ static void http_callback( char * response, int http_status, char * full_respons
|
|||
c_printf( "http_status=%d\n", http_status );
|
||||
if ( http_status != HTTP_STATUS_GENERIC_ERROR )
|
||||
{
|
||||
c_printf( "strlen(full_response)=%d\n", strlen( full_response ) );
|
||||
if (full_response != NULL) {
|
||||
c_printf( "strlen(full_response)=%d\n", strlen( full_response ) );
|
||||
}
|
||||
c_printf( "response=%s<EOF>\n", response );
|
||||
}
|
||||
#endif
|
||||
if (http_callback_registry != LUA_NOREF)
|
||||
{
|
||||
lua_rawgeti(http_client_L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
lua_State *L = lua_getstate();
|
||||
|
||||
lua_pushnumber(http_client_L, http_status);
|
||||
if ( http_status != HTTP_STATUS_GENERIC_ERROR )
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
|
||||
lua_pushnumber(L, http_status);
|
||||
if ( http_status != HTTP_STATUS_GENERIC_ERROR && response)
|
||||
{
|
||||
lua_pushstring(http_client_L, response);
|
||||
lua_pushstring(L, response);
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushnil(http_client_L);
|
||||
lua_pushnil(L);
|
||||
}
|
||||
lua_call(http_client_L, 2, 0); // With 2 arguments and 0 result
|
||||
lua_call(L, 2, 0); // With 2 arguments and 0 result
|
||||
|
||||
luaL_unref(http_client_L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
http_callback_registry = LUA_NOREF;
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +49,6 @@ static void http_callback( char * response, int http_status, char * full_respons
|
|||
static int http_lapi_request( lua_State *L )
|
||||
{
|
||||
int length;
|
||||
http_client_L = L;
|
||||
const char * url = luaL_checklstring(L, 1, &length);
|
||||
const char * method = luaL_checklstring(L, 2, &length);
|
||||
const char * headers = NULL;
|
||||
|
@ -68,7 +70,7 @@ static int http_lapi_request( lua_State *L )
|
|||
}
|
||||
|
||||
if (lua_type(L, 5) == LUA_TFUNCTION || lua_type(L, 5) == LUA_TLIGHTFUNCTION) {
|
||||
lua_pushvalue(L, 5); // copy argument (func) to the top of stack
|
||||
lua_pushvalue(L, 5); // copy argument (func) to the top of stack
|
||||
if (http_callback_registry != LUA_NOREF)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
|
@ -82,7 +84,6 @@ static int http_lapi_request( lua_State *L )
|
|||
static int http_lapi_post( lua_State *L )
|
||||
{
|
||||
int length;
|
||||
http_client_L = L;
|
||||
const char * url = luaL_checklstring(L, 1, &length);
|
||||
const char * headers = NULL;
|
||||
const char * body = NULL;
|
||||
|
@ -103,7 +104,7 @@ static int http_lapi_post( lua_State *L )
|
|||
}
|
||||
|
||||
if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
|
||||
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
||||
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
||||
if (http_callback_registry != LUA_NOREF)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
|
@ -117,7 +118,6 @@ static int http_lapi_post( lua_State *L )
|
|||
static int http_lapi_put( lua_State *L )
|
||||
{
|
||||
int length;
|
||||
http_client_L = L;
|
||||
const char * url = luaL_checklstring(L, 1, &length);
|
||||
const char * headers = NULL;
|
||||
const char * body = NULL;
|
||||
|
@ -138,7 +138,7 @@ static int http_lapi_put( lua_State *L )
|
|||
}
|
||||
|
||||
if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
|
||||
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
||||
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
||||
if (http_callback_registry != LUA_NOREF)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
|
@ -152,7 +152,6 @@ static int http_lapi_put( lua_State *L )
|
|||
static int http_lapi_delete( lua_State *L )
|
||||
{
|
||||
int length;
|
||||
http_client_L = L;
|
||||
const char * url = luaL_checklstring(L, 1, &length);
|
||||
const char * headers = NULL;
|
||||
const char * body = NULL;
|
||||
|
@ -173,7 +172,7 @@ static int http_lapi_delete( lua_State *L )
|
|||
}
|
||||
|
||||
if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
|
||||
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
||||
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
|
||||
if (http_callback_registry != LUA_NOREF)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
|
@ -187,7 +186,6 @@ static int http_lapi_delete( lua_State *L )
|
|||
static int http_lapi_get( lua_State *L )
|
||||
{
|
||||
int length;
|
||||
http_client_L = L;
|
||||
const char * url = luaL_checklstring(L, 1, &length);
|
||||
const char * headers = NULL;
|
||||
|
||||
|
@ -203,7 +201,7 @@ static int http_lapi_get( lua_State *L )
|
|||
}
|
||||
|
||||
if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
|
||||
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
|
||||
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
|
||||
if (http_callback_registry != LUA_NOREF)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
|
||||
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
|
|
Loading…
Reference in New Issue