-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
http.c
286 lines (240 loc) · 6.98 KB
/
http.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/******************************************************************************
* HTTP module for NodeMCU
* vowstar@gmail.com
* 2015-12-29
*******************************************************************************/
#include <string.h>
#include <stdlib.h>
#include "module.h"
#include "lauxlib.h"
#include "platform.h"
#include "cpu_esp8266.h"
#include "http/httpclient.h"
#include <ctype.h>
static int http_callback_registry = LUA_NOREF;
static void http_callback( char * response, int http_status, char ** full_response_p, int body_size )
{
const char *full_response = full_response_p ? *full_response_p : NULL;
#if defined(HTTPCLIENT_DEBUG_ON)
dbg_printf( "http_status=%d\n", http_status );
if ( http_status != HTTP_STATUS_GENERIC_ERROR )
{
if (full_response != NULL) {
dbg_printf( "strlen(full_response)=%d\n", strlen( full_response ) );
}
dbg_printf( "response=%s<EOF>\n", response );
}
#endif
if (http_callback_registry != LUA_NOREF)
{
lua_State *L = lua_getstate();
lua_rawgeti(L, LUA_REGISTRYINDEX, http_callback_registry);
lua_pushinteger(L, http_status);
if ( http_status != HTTP_STATUS_GENERIC_ERROR && response)
{
lua_pushlstring(L, response, (size_t)body_size);
lua_newtable(L);
const char *p = full_response;
// Need to skip the HTTP/1.1 header line
while (*p && *p != '\n') {
p++;
}
if (*p == '\n') {
p++;
}
while (*p && *p != '\r' && *p != '\n') {
const char *eol = p;
while (*eol && *eol != '\r') {
eol++;
}
const char *colon = p;
while (*colon != ':' && colon < eol) {
colon++;
}
if (*colon != ':') {
break;
}
const char *value = colon + 1;
while (*value == ' ') {
value++;
}
luaL_Buffer b;
luaL_buffinit(L, &b);
while (p < colon) {
luaL_addchar(&b, tolower((unsigned char) *p));
p++;
}
luaL_pushresult(&b);
lua_pushlstring(L, value, eol - value);
lua_settable(L, -3);
p = eol + 1;
if (*p == '\n') {
p++;
}
}
}
else
{
lua_pushnil(L);
lua_pushnil(L);
}
if (full_response_p && *full_response_p) {
free(*full_response_p);
*full_response_p = NULL;
}
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
http_callback_registry = LUA_NOREF;
luaL_pcallx(L, 3, 0); // With 3 arguments and 0 result
}
}
// Lua: http.request( url, method, header, body, function(status, reponse) end )
static int http_lapi_request( lua_State *L )
{
int length;
const char * url = luaL_checklstring(L, 1, &length);
const char * method = luaL_checklstring(L, 2, &length);
const char * headers = NULL;
const char * body = NULL;
// Check parameter
if ((url == NULL) || (method == NULL))
{
return luaL_error( L, "wrong arg type" );
}
if (lua_isstring(L, 3))
{
headers = luaL_checklstring(L, 3, &length);
}
if (lua_isstring(L, 4))
{
body = luaL_checklstring(L, 4, &length);
}
if (lua_isfunction(L, 5)) {
lua_pushvalue(L, 5); // copy argument (func) to the top of stack
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
}
http_request(url, method, headers, body, http_callback, 0);
return 0;
}
// Lua: http.post( url, header, body, function(status, reponse) end )
static int http_lapi_post( lua_State *L )
{
int length;
const char * url = luaL_checklstring(L, 1, &length);
const char * headers = NULL;
const char * body = NULL;
// Check parameter
if ((url == NULL))
{
return luaL_error( L, "wrong arg type" );
}
if (lua_isstring(L, 2))
{
headers = luaL_checklstring(L, 2, &length);
}
if (lua_isstring(L, 3))
{
body = luaL_checklstring(L, 3, &length);
}
if (lua_isfunction(L, 4)) {
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);
}
http_post(url, headers, body, http_callback);
return 0;
}
// Lua: http.put( url, header, body, function(status, reponse) end )
static int http_lapi_put( lua_State *L )
{
int length;
const char * url = luaL_checklstring(L, 1, &length);
const char * headers = NULL;
const char * body = NULL;
// Check parameter
if ((url == NULL))
{
return luaL_error( L, "wrong arg type" );
}
if (lua_isstring(L, 2))
{
headers = luaL_checklstring(L, 2, &length);
}
if (lua_isstring(L, 3))
{
body = luaL_checklstring(L, 3, &length);
}
if (lua_isfunction(L, 4)) {
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);
}
http_put(url, headers, body, http_callback);
return 0;
}
// Lua: http.delete( url, header, body, function(status, reponse) end )
static int http_lapi_delete( lua_State *L )
{
int length;
const char * url = luaL_checklstring(L, 1, &length);
const char * headers = NULL;
const char * body = NULL;
// Check parameter
if ((url == NULL))
{
return luaL_error( L, "wrong arg type" );
}
if (lua_isstring(L, 2))
{
headers = luaL_checklstring(L, 2, &length);
}
if (lua_isstring(L, 3))
{
body = luaL_checklstring(L, 3, &length);
}
if (lua_isfunction(L, 4)) {
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);
}
http_delete(url, headers, body, http_callback);
return 0;
}
// Lua: http.get( url, header, function(status, reponse) end )
static int http_lapi_get( lua_State *L )
{
int length;
const char * url = luaL_checklstring(L, 1, &length);
const char * headers = NULL;
// Check parameter
if ((url == NULL))
{
return luaL_error( L, "wrong arg type" );
}
if (lua_isstring(L, 2))
{
headers = luaL_checklstring(L, 2, &length);
}
if (lua_isfunction(L, 3)) {
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);
}
http_get(url, headers, http_callback);
return 0;
}
// Module function map
LROT_BEGIN(http, NULL, 0)
LROT_FUNCENTRY( request, http_lapi_request )
LROT_FUNCENTRY( post, http_lapi_post )
LROT_FUNCENTRY( put, http_lapi_put )
LROT_FUNCENTRY( delete, http_lapi_delete )
LROT_FUNCENTRY( get, http_lapi_get )
LROT_NUMENTRY( OK, 0 )
LROT_NUMENTRY( ERROR, HTTP_STATUS_GENERIC_ERROR )
LROT_END(http, NULL, 0)
NODEMCU_MODULE(HTTP, "http", http, NULL);