Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
61c2b4df
Commit
61c2b4df
authored
Apr 09, 2016
by
Philip Gladstone
Committed by
Marcel Stör
Apr 09, 2016
Browse files
Minimal changes to handle bad bodies (#1212)
Fix failure to compile Move the check into the http module Reverted change
parent
b32a161b
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/http/httpclient.c
View file @
61c2b4df
...
@@ -18,6 +18,7 @@
...
@@ -18,6 +18,7 @@
#include "mem.h"
#include "mem.h"
#include "limits.h"
#include "limits.h"
#include "httpclient.h"
#include "httpclient.h"
#include "stdlib.h"
/* Internal state. */
/* Internal state. */
typedef
struct
request_args_t
{
typedef
struct
request_args_t
{
...
@@ -80,110 +81,6 @@ esp_isdigit( char c )
...
@@ -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
)
static
int
ICACHE_FLASH_ATTR
http_chunked_decode
(
const
char
*
chunked
,
char
*
decode
)
{
{
int
i
=
0
,
j
=
0
;
int
i
=
0
,
j
=
0
;
...
@@ -191,9 +88,9 @@ static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * d
...
@@ -191,9 +88,9 @@ static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * d
char
*
str
=
(
char
*
)
chunked
;
char
*
str
=
(
char
*
)
chunked
;
do
do
{
{
char
*
endstr
=
NULL
;
char
*
endstr
;
/* [chunk-size] */
/* [chunk-size] */
i
=
esp_
strtol
(
str
+
j
,
endstr
,
16
);
i
=
strto
u
l
(
str
+
j
,
NULL
,
16
);
HTTPCLIENT_DEBUG
(
"Chunk Size:%d
\r\n
"
,
i
);
HTTPCLIENT_DEBUG
(
"Chunk Size:%d
\r\n
"
,
i
);
if
(
i
<=
0
)
if
(
i
<=
0
)
break
;
break
;
...
@@ -349,7 +246,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
...
@@ -349,7 +246,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
if
(
req
->
buffer
==
NULL
)
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'
)
else
if
(
req
->
buffer
[
0
]
!=
'\0'
)
{
{
...
@@ -364,7 +261,18 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
...
@@ -364,7 +261,18 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
else
else
{
{
http_status
=
atoi
(
req
->
buffer
+
strlen
(
version_1_0
)
);
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"
)
)
if
(
os_strstr
(
req
->
buffer
,
"Transfer-Encoding: chunked"
)
)
{
{
int
body_size
=
req
->
buffer_size
-
(
body
-
req
->
buffer
);
int
body_size
=
req
->
buffer_size
-
(
body
-
req
->
buffer
);
...
@@ -380,7 +288,9 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
...
@@ -380,7 +288,9 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
{
{
req
->
callback_handle
(
body
,
http_status
,
req
->
buffer
);
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
->
hostname
);
os_free
(
req
->
method
);
os_free
(
req
->
method
);
os_free
(
req
->
path
);
os_free
(
req
->
path
);
...
...
app/modules/http.c
View file @
61c2b4df
...
@@ -10,7 +10,6 @@
...
@@ -10,7 +10,6 @@
#include "httpclient.h"
#include "httpclient.h"
static
int
http_callback_registry
=
LUA_NOREF
;
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
)
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
...
@@ -18,26 +17,30 @@ static void http_callback( char * response, int http_status, char * full_respons
c_printf
(
"http_status=%d
\n
"
,
http_status
);
c_printf
(
"http_status=%d
\n
"
,
http_status
);
if
(
http_status
!=
HTTP_STATUS_GENERIC_ERROR
)
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
);
c_printf
(
"response=%s<EOF>
\n
"
,
response
);
}
}
#endif
#endif
if
(
http_callback_registry
!=
LUA_NOREF
)
if
(
http_callback_registry
!=
LUA_NOREF
)
{
{
lua_rawgeti
(
http_client_L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
lua_State
*
L
=
lua_getstate
();
lua_rawgeti
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
lua_pushnumber
(
http_client_
L
,
http_status
);
lua_pushnumber
(
L
,
http_status
);
if
(
http_status
!=
HTTP_STATUS_GENERIC_ERROR
)
if
(
http_status
!=
HTTP_STATUS_GENERIC_ERROR
&&
response
)
{
{
lua_pushstring
(
http_client_
L
,
response
);
lua_pushstring
(
L
,
response
);
}
}
else
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
;
http_callback_registry
=
LUA_NOREF
;
}
}
}
}
...
@@ -46,7 +49,6 @@ static void http_callback( char * response, int http_status, char * full_respons
...
@@ -46,7 +49,6 @@ static void http_callback( char * response, int http_status, char * full_respons
static
int
http_lapi_request
(
lua_State
*
L
)
static
int
http_lapi_request
(
lua_State
*
L
)
{
{
int
length
;
int
length
;
http_client_L
=
L
;
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
method
=
luaL_checklstring
(
L
,
2
,
&
length
);
const
char
*
method
=
luaL_checklstring
(
L
,
2
,
&
length
);
const
char
*
headers
=
NULL
;
const
char
*
headers
=
NULL
;
...
@@ -68,7 +70,7 @@ static int http_lapi_request( lua_State *L )
...
@@ -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
)
{
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
)
if
(
http_callback_registry
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
...
@@ -82,7 +84,6 @@ static int http_lapi_request( lua_State *L )
...
@@ -82,7 +84,6 @@ static int http_lapi_request( lua_State *L )
static
int
http_lapi_post
(
lua_State
*
L
)
static
int
http_lapi_post
(
lua_State
*
L
)
{
{
int
length
;
int
length
;
http_client_L
=
L
;
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
headers
=
NULL
;
const
char
*
headers
=
NULL
;
const
char
*
body
=
NULL
;
const
char
*
body
=
NULL
;
...
@@ -103,7 +104,7 @@ static int http_lapi_post( lua_State *L )
...
@@ -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
)
{
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
)
if
(
http_callback_registry
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
...
@@ -117,7 +118,6 @@ static int http_lapi_post( lua_State *L )
...
@@ -117,7 +118,6 @@ static int http_lapi_post( lua_State *L )
static
int
http_lapi_put
(
lua_State
*
L
)
static
int
http_lapi_put
(
lua_State
*
L
)
{
{
int
length
;
int
length
;
http_client_L
=
L
;
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
headers
=
NULL
;
const
char
*
headers
=
NULL
;
const
char
*
body
=
NULL
;
const
char
*
body
=
NULL
;
...
@@ -138,7 +138,7 @@ static int http_lapi_put( lua_State *L )
...
@@ -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
)
{
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
)
if
(
http_callback_registry
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
...
@@ -152,7 +152,6 @@ static int http_lapi_put( lua_State *L )
...
@@ -152,7 +152,6 @@ static int http_lapi_put( lua_State *L )
static
int
http_lapi_delete
(
lua_State
*
L
)
static
int
http_lapi_delete
(
lua_State
*
L
)
{
{
int
length
;
int
length
;
http_client_L
=
L
;
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
headers
=
NULL
;
const
char
*
headers
=
NULL
;
const
char
*
body
=
NULL
;
const
char
*
body
=
NULL
;
...
@@ -173,7 +172,7 @@ static int http_lapi_delete( lua_State *L )
...
@@ -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
)
{
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
)
if
(
http_callback_registry
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
...
@@ -187,7 +186,6 @@ static int http_lapi_delete( lua_State *L )
...
@@ -187,7 +186,6 @@ static int http_lapi_delete( lua_State *L )
static
int
http_lapi_get
(
lua_State
*
L
)
static
int
http_lapi_get
(
lua_State
*
L
)
{
{
int
length
;
int
length
;
http_client_L
=
L
;
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
url
=
luaL_checklstring
(
L
,
1
,
&
length
);
const
char
*
headers
=
NULL
;
const
char
*
headers
=
NULL
;
...
@@ -203,7 +201,7 @@ static int http_lapi_get( lua_State *L )
...
@@ -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
)
{
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
)
if
(
http_callback_registry
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
http_callback_registry
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment