forked from brimworks/lua-http-parser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlua-http-parser.c
585 lines (475 loc) · 17.6 KB
/
lua-http-parser.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include <assert.h>
#include <lauxlib.h>
#include <lua.h>
#include "http-parser/http_parser.h"
#define PARSER_MT "http.parser{parser}"
#define check_parser(L, narg) \
((lhttp_parser*)luaL_checkudata((L), (narg), PARSER_MT))
/* The Lua stack indices */
#define ST_FENV_IDX 3
#define ST_BUFFER_IDX 4
#define ST_LEN ST_BUFFER_IDX
/* Callback identifiers are indices into the fenv table where the
* callback is saved. If you add/remove/change anything about these,
* be sure to update lhp_callback_names and FLAG_GET_BUF_CB_ID.
*/
#define CB_ON_MESSAGE_BEGIN 1
#define CB_ON_URL 2
#define CB_ON_HEADER 3
#define CB_ON_HEADERS_COMPLETE 4
#define CB_ON_BODY 5
#define CB_ON_MESSAGE_COMPLETE 6
#define CB_LEN (sizeof(lhp_callback_names)/sizeof(*lhp_callback_names))
static const char *lhp_callback_names[] = {
/* The MUST be in the same order as the above callbacks */
"on_message_begin",
"on_url",
"on_header",
"on_headers_complete",
"on_body",
"on_message_complete",
};
/* Non-callback FENV indices. */
#define FENV_BUFFER_IDX CB_LEN + 1
#define FENV_LEN FENV_BUFFER_IDX
#define FLAGS_BUF_CB_ID_BITS 3
#define FLAGS_BUF_CB_ID_MASK ((1<<(FLAGS_BUF_CB_ID_BITS))-1)
/* Get the cb_id that has information stored in buff */
#define FLAG_GET_BUF_CB_ID(flags) ((flags) & FLAGS_BUF_CB_ID_MASK)
#define FLAGS_CB_ID_FIRST_BIT (1<<(FLAGS_BUF_CB_ID_BITS))
#define CB_ID_TO_CB_BIT(cb_id) ((FLAGS_CB_ID_FIRST_BIT)<<(cb_id))
/* Test/set/remove a bit from the flags field of lhttp_parser. The
* FLAG_*_CB() macros test/set/remove the bit that signifies that a
* callback with that id has been registered in the FENV.
*
* The FLAG_*_BUF() macros test/set/remove the bit that signifies that
* data is buffered for that callback.
*
* The FLAG_*_HFIELD() macros test/set/remove the bit that signifies
* that the first element of the buffer is the header field key.
*/
#define FLAG_HAS_CB(flags, cb_id) ( (flags) & CB_ID_TO_CB_BIT(cb_id) )
#define FLAG_SET_CB(flags, cb_id) ( (flags) |= CB_ID_TO_CB_BIT(cb_id) )
#define FLAG_RM_CB(flags, cb_id) ( (flags) &= ~CB_ID_TO_CB_BIT(cb_id) )
#define FLAG_HAS_BUF(flags, cb_id) ( FLAG_GET_BUF_CB_ID(flags) == cb_id )
#define FLAG_SET_BUF(flags, cb_id) ( (flags) = (((flags) & ~FLAGS_BUF_CB_ID_MASK) \
| ((cb_id) & FLAGS_BUF_CB_ID_MASK)) )
#define FLAG_RM_BUF(flags) ( (flags) &= ~FLAGS_BUF_CB_ID_MASK )
#define FLAG_HAS_HFIELD(flags) ( ((flags) & FLAGS_CB_ID_FIRST_BIT) >> FLAGS_BUF_CB_ID_BITS )
#define FLAG_SET_HFIELD(flags) ( (flags) |= FLAGS_CB_ID_FIRST_BIT )
#define FLAG_RM_HFIELD(flags) ( (flags) &= ~FLAGS_CB_ID_FIRST_BIT )
typedef struct lhttp_parser {
http_parser parser; /* embedded http_parser. */
int flags; /* See above flag test/set/remove macros. */
int buf_len; /* number of buffered chunks for current callback. */
} lhttp_parser;
/* Concatinate and remove elements from the table at idx starting at
* element begin and going to length len. The final concatinated string
* is on the top of the stack.
*/
static int lhp_table_concat_and_clear(lua_State *L, int idx, int begin, int len) {
luaL_Buffer buff;
int real_len = len-begin+1;
/* Empty table? */
if ( !real_len ) {
lua_pushliteral(L, "");
return 0;
}
/* One element? */
if ( 1 == real_len ) {
lua_rawgeti(L, idx, begin);
/* remove values from buffer. */
lua_pushnil(L);
lua_rawseti(L, idx, begin);
return 0;
}
/* do a table concat. */
luaL_buffinit(L, &buff);
for(; begin <= len; begin++) {
lua_rawgeti(L, idx, begin);
luaL_addvalue(&buff);
/* remove values from buffer. */
lua_pushnil(L);
lua_rawseti(L, idx, begin);
}
luaL_pushresult(&buff);
return 0;
}
/* Clear all elements in the table at idx starting at
* element begin and going to length len.
*/
static int lhp_table_clear(lua_State *L, int idx, int begin, int len) {
/* nil all elements. */
for(; begin <= len; begin++) {
/* remove element from table. */
lua_pushnil(L);
lua_rawseti(L, idx, begin);
}
return 0;
}
/* "Flush" the buffer for the callback identified by cb_id. The
* CB_ON_HEADER cb_id is flushed by inspecting FLAG_HAS_HFIELD().
* If that bit is not set, then the buffer is concatinated into
* a single string element in the buffer and nothing is pushed
* on the Lua stack. Otherwise the buffer table is cleared after
* pushing the following onto the Lua stack:
*
* CB_ON_HEADER function,
* first element of the buffer,
* second - length element of the buffer concatinated
*
* If cb_id is not CB_ON_HEADER then the buffer table is cleared after
* pushing the following onto the Lua stack:
*
* cb_id function,
* first - length elements of the buffer concatinated
*
*/
static int lhp_flush(lhttp_parser* lparser, int cb_id) {
lua_State* L = (lua_State*)lparser->parser.data;
int begin, len, result, top, save;
assert(cb_id);
assert(FLAG_HAS_BUF(lparser->flags, cb_id));
if ( ! lua_checkstack(L, 7) ) return -1;
len = lparser->buf_len;
begin = 1;
top = lua_gettop(L);
FLAG_RM_BUF(lparser->flags);
if ( CB_ON_HEADER == cb_id ) {
if ( FLAG_HAS_HFIELD(lparser->flags) ) {
/* Push <func>, <arg1>[, <arg2>] */
lua_rawgeti(L, ST_FENV_IDX, cb_id);
lua_rawgeti(L, ST_BUFFER_IDX, 1);
lua_pushnil(L);
lua_rawseti(L, ST_BUFFER_IDX, 1);
begin = 2;
save = 0;
lparser->buf_len = 0;
FLAG_RM_HFIELD(lparser->flags);
} else {
/* Save */
begin = 1;
save = 1;
lparser->buf_len = 1;
}
} else {
/* Push <func>[, <arg1> */
lua_rawgeti(L, ST_FENV_IDX, cb_id);
begin = 1;
save = 0;
lparser->buf_len = 0;
}
result = lhp_table_concat_and_clear(L, ST_BUFFER_IDX, begin, len);
if ( 0 != result ) {
lua_settop(L, top);
return result;
}
if ( save ) lua_rawseti(L, ST_BUFFER_IDX, 1);
return 0;
}
/* Puts the str of length len into the buffer table and
* updates buf_len. It also sets the buf flag for cb_id.
*/
static int lhp_buffer(lhttp_parser* lparser, int cb_id, const char* str, size_t len, int hfield) {
lua_State* L = (lua_State*)lparser->parser.data;
assert(cb_id);
assert(FLAG_HAS_CB(lparser->flags, cb_id));
/* insert event chunk into buffer. */
FLAG_SET_BUF(lparser->flags, cb_id);
if ( hfield ) {
FLAG_SET_HFIELD(lparser->flags);
}
lua_pushlstring(L, str, len);
lua_rawseti(L, ST_BUFFER_IDX, ++(lparser->buf_len));
return 0;
}
/* Push the zero argument event for cb_id. Post condition:
* Lua stack contains <func>, nil
*/
static int lhp_push_nil_event(lhttp_parser* lparser, int cb_id) {
lua_State* L = (lua_State*)lparser->parser.data;
assert(FLAG_HAS_CB(lparser->flags, cb_id));
if ( ! lua_checkstack(L, 5) ) return -1;
lua_rawgeti(L, ST_FENV_IDX, cb_id);
lua_pushnil(L);
return 0;
}
/* Flush the buffer as long as it is not the except_cb_id being buffered.
*/
static int lhp_flush_except(lhttp_parser* lparser, int except_cb_id, int hfield) {
int flush = 0;
int cb_id = FLAG_GET_BUF_CB_ID(lparser->flags);
/* flush previous event and/or url */
if ( cb_id ) {
if ( cb_id == CB_ON_HEADER ) {
flush = hfield ^ FLAG_HAS_HFIELD(lparser->flags);
} else if ( cb_id != except_cb_id ) {
flush = 1;
}
}
if ( flush ) {
int result = lhp_flush(lparser, cb_id);
if ( 0 != result ) return result;
}
return 0;
}
/* The event for cb_id where cb_id takes a string argument.
*/
static int lhp_http_data_cb(http_parser* parser, int cb_id, const char* str, size_t len, int hfield) {
lhttp_parser* lparser = (lhttp_parser*)parser;
int result = lhp_flush_except(lparser, cb_id, hfield);
if ( 0 != result ) return result;
if ( ! FLAG_HAS_CB(lparser->flags, cb_id) ) return 0;
return lhp_buffer(lparser, cb_id, str, len, hfield);
}
static int lhp_http_cb(http_parser* parser, int cb_id) {
lhttp_parser* lparser = (lhttp_parser*)parser;
int result = lhp_flush_except(lparser, cb_id, 0);
if ( 0 != result ) return result;
if ( ! FLAG_HAS_CB(lparser->flags, cb_id) ) return 0;
return lhp_push_nil_event(lparser, cb_id);
}
static int lhp_message_begin_cb(http_parser* parser) {
return lhp_http_cb(parser, CB_ON_MESSAGE_BEGIN);
}
static int lhp_url_cb(http_parser* parser, const char* str, size_t len) {
return lhp_http_data_cb(parser, CB_ON_URL, str, len, 0);
}
static int lhp_header_field_cb(http_parser* parser, const char* str, size_t len) {
return lhp_http_data_cb(parser, CB_ON_HEADER, str, len, 0);
}
static int lhp_header_value_cb(http_parser* parser, const char* str, size_t len) {
return lhp_http_data_cb(parser, CB_ON_HEADER, str, len, 1);
}
static int lhp_headers_complete_cb(http_parser* parser) {
return lhp_http_cb(parser, CB_ON_HEADERS_COMPLETE);
}
static int lhp_body_cb(http_parser* parser, const char* str, size_t len) {
/* on_headers_complete did any flushing, so just push the cb */
lhttp_parser* lparser = (lhttp_parser*)parser;
lua_State* L = (lua_State*)lparser->parser.data;
if ( ! FLAG_HAS_CB(lparser->flags, CB_ON_BODY) ) return 0;
if ( ! lua_checkstack(L, 5) ) return -1;
lua_rawgeti(L, ST_FENV_IDX, CB_ON_BODY);
lua_pushlstring(L, str, len);
return 0;
}
static int lhp_message_complete_cb(http_parser* parser) {
/* Send on_body(nil) message to comply with LTN12 */
int result = lhp_push_nil_event((lhttp_parser*)parser, CB_ON_BODY);
if ( 0 != result ) return result;
return lhp_http_cb(parser, CB_ON_MESSAGE_COMPLETE);
}
static int lhp_init(lua_State* L, enum http_parser_type type) {
int cb_id;
luaL_checktype(L, 1, LUA_TTABLE);
/* Stack: callbacks */
lhttp_parser* lparser = (lhttp_parser*)lua_newuserdata(L, sizeof(lhttp_parser));
http_parser* parser = &(lparser->parser);
assert(NULL != parser);
/* Stack: callbacks, userdata */
lparser->flags = 0;
lparser->buf_len = 0;
/* Get the metatable: */
luaL_getmetatable(L, PARSER_MT);
assert(!lua_isnil(L, -1)/* PARSER_MT found? */);
/* Stack: callbacks, userdata, metatable */
/* Copy functions to new fenv table */
lua_createtable(L, FENV_LEN, 0);
/* Stack: callbacks, userdata, metatable, fenv */
for (cb_id = 1; cb_id <= CB_LEN; cb_id++ ) {
lua_getfield(L, 1, lhp_callback_names[cb_id-1]);
if ( lua_isfunction(L, -1) ) {
lua_rawseti(L, -2, cb_id); /* fenv[cb_id] = callback */
FLAG_SET_CB(lparser->flags, cb_id);
} else {
lua_pop(L, 1); /* pop non-function value. */
}
}
/* Create buffer table and add it to the fenv table. */
lua_createtable(L, 1, 0);
lua_rawseti(L, -2, FENV_BUFFER_IDX);
/* Stack: callbacks, userdata, metatable, fenv */
lua_setfenv(L, -3);
/* Stack: callbacks, userdata, metatable */
http_parser_init(parser, type);
parser->data = NULL;
lua_setmetatable(L, -2);
return 1;
}
static int lhp_request(lua_State* L) {
return lhp_init(L, HTTP_REQUEST);
}
static int lhp_response(lua_State* L) {
return lhp_init(L, HTTP_RESPONSE);
}
static int lhp_execute(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
http_parser* parser = &(lparser->parser);
size_t len;
size_t result;
const char* str = luaL_checklstring(L, 2, &len);
static const http_parser_settings settings = {
.on_message_begin = lhp_message_begin_cb,
.on_url = lhp_url_cb,
.on_header_field = lhp_header_field_cb,
.on_header_value = lhp_header_value_cb,
.on_headers_complete = lhp_headers_complete_cb,
.on_body = lhp_body_cb,
.on_message_complete = lhp_message_complete_cb
};
/* truncate stack to (userdata, string) */
lua_settop(L, 2);
lua_getfenv(L, 1);
assert(lua_istable(L, -1));
assert(lua_gettop(L) == ST_FENV_IDX);
lua_rawgeti(L, ST_FENV_IDX, FENV_BUFFER_IDX);
assert(lua_istable(L, -1));
assert(lua_gettop(L) == ST_BUFFER_IDX);
assert(lua_gettop(L) == ST_LEN);
lua_pushnil(L);
/* Stack: (userdata, string, fenv, buffer, url, nil) */
parser->data = L;
result = http_parser_execute(parser, &settings, str, len);
parser->data = NULL;
/* replace nil place-holder with 'result' code. */
lua_pushnumber(L, result);
lua_replace(L, ST_LEN+1);
/* Transform the stack into a table: */
len = lua_gettop(L) - ST_LEN;
return len;
}
static int lhp_should_keep_alive(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
lua_pushboolean(L, http_should_keep_alive(&lparser->parser));
return 1;
}
static int lhp_is_upgrade(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
lua_pushboolean(L, lparser->parser.upgrade);
return 1;
}
static int lhp_method(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
switch(lparser->parser.method) {
case HTTP_DELETE: lua_pushliteral(L, "DELETE"); break;
case HTTP_GET: lua_pushliteral(L, "GET"); break;
case HTTP_HEAD: lua_pushliteral(L, "HEAD"); break;
case HTTP_POST: lua_pushliteral(L, "POST"); break;
case HTTP_PUT: lua_pushliteral(L, "PUT"); break;
case HTTP_CONNECT: lua_pushliteral(L, "CONNECT"); break;
case HTTP_OPTIONS: lua_pushliteral(L, "OPTIONS"); break;
case HTTP_TRACE: lua_pushliteral(L, "TRACE"); break;
case HTTP_COPY: lua_pushliteral(L, "COPY"); break;
case HTTP_LOCK: lua_pushliteral(L, "LOCK"); break;
case HTTP_MKCOL: lua_pushliteral(L, "MKCOL"); break;
case HTTP_MOVE: lua_pushliteral(L, "MOVE"); break;
case HTTP_PROPFIND: lua_pushliteral(L, "PROPFIND"); break;
case HTTP_PROPPATCH: lua_pushliteral(L, "PROPPATCH"); break;
case HTTP_UNLOCK: lua_pushliteral(L, "UNLOCK"); break;
default:
lua_pushnumber(L, lparser->parser.method);
}
return 1;
}
static int lhp_version(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
lua_pushnumber(L, lparser->parser.http_major);
lua_pushnumber(L, lparser->parser.http_minor);
return 2;
}
static int lhp_status_code(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
lua_pushnumber(L, lparser->parser.status_code);
return 1;
}
static int lhp_error(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
enum http_errno http_errno = lparser->parser.http_errno;
lua_pushnumber(L, http_errno);
lua_pushstring(L, http_errno_name(http_errno));
lua_pushstring(L, http_errno_description(http_errno));
return 3;
}
static int lhp_reset(lua_State* L) {
lhttp_parser* lparser = check_parser(L, 1);
http_parser* parser = &(lparser->parser);
/* truncate stack to (userdata) */
lua_settop(L, 1);
/* re-initialize http-parser. */
http_parser_init(parser, parser->type);
/* clear buffer */
lua_getfenv(L, 1);
lua_rawgeti(L, 2, FENV_BUFFER_IDX);
lhp_table_clear(L, 3, 1, lparser->buf_len);
/* reset buffer length and flags. */
lparser->buf_len = 0;
FLAG_RM_BUF(lparser->flags);
FLAG_RM_HFIELD(lparser->flags);
return 0;
}
static int lhp_is_function(lua_State* L) {
lua_pushboolean(L, lua_isfunction(L, 1));
return 1;
}
/* The execute method has a "lua based stub" so that callbacks
* can yield without having to apply the CoCo patch to Lua. */
static const char* lhp_execute_lua =
"local c_execute, is_function = ...\n"
"local function execute(result, cb, arg1, arg2, ...)\n"
" if ( not cb ) then\n"
" return result\n"
" end\n"
" if ( is_function(arg2) ) then\n"
" cb(arg1)\n"
" return execute(result, arg2, ...)"
" end\n"
" cb(arg1, arg2)\n"
" return execute(result, ...)\n"
"end\n"
"return function(...)\n"
" return execute(c_execute(...))\n"
"end";
static void lhp_push_execute_fn(lua_State* L) {
#ifndef NDEBUG
int top = lua_gettop(L);
#endif
int err = luaL_loadstring(L, lhp_execute_lua);
if ( err ) lua_error(L);
lua_pushcfunction(L, lhp_execute);
lua_pushcfunction(L, lhp_is_function);
lua_call(L, 2, 1);
/* Compiled lua function should be at the top of the stack now. */
assert(lua_gettop(L) == top + 1);
assert(lua_isfunction(L, -1));
}
LUALIB_API int luaopen_http_parser(lua_State* L) {
/* parser metatable init */
luaL_newmetatable(L, PARSER_MT);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lhp_is_upgrade);
lua_setfield(L, -2, "is_upgrade");
lua_pushcfunction(L, lhp_method);
lua_setfield(L, -2, "method");
lua_pushcfunction(L, lhp_version);
lua_setfield(L, -2, "version");
lua_pushcfunction(L, lhp_status_code);
lua_setfield(L, -2, "status_code");
lua_pushcfunction(L, lhp_error);
lua_setfield(L, -2, "error");
lua_pushcfunction(L, lhp_should_keep_alive);
lua_setfield(L, -2, "should_keep_alive");
lhp_push_execute_fn(L);
lua_setfield(L, -2, "execute");
lua_pushcfunction(L, lhp_reset);
lua_setfield(L, -2, "reset");
lua_pop(L, 1);
/* export http.parser */
lua_newtable(L); /* Stack: table */
lua_pushcfunction(L, lhp_request);
lua_setfield(L, -2, "request");
lua_pushcfunction(L, lhp_response);
lua_setfield(L, -2, "response");
return 1;
}