This repository was archived by the owner on Mar 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlj_http_parser.lua
99 lines (75 loc) · 2.58 KB
/
lj_http_parser.lua
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
--------------------------------------------------------------------------------
local ffi = require 'ffi'
--------------------------------------------------------------------------------
local core = ffi.load("lj_http_parser")
--------------------------------------------------------------------------------
ffi.cdef [[
// from http_parser.h
typedef struct http_parser http_parser;
typedef struct http_parser_settings http_parser_settings;
struct http_parser {
/** PRIVATE **/
unsigned char type : 2; /* enum http_parser_type */
unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
unsigned char state; /* enum state from http_parser.c */
unsigned char header_state; /* enum header_state from http_parser.c */
unsigned char index; /* index into current matcher */
uint32_t nread; /* # bytes read in various scenarios */
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
/** READ-ONLY **/
unsigned short http_major;
unsigned short http_minor;
unsigned short status_code; /* responses only */
unsigned char method; /* requests only */
unsigned char http_errno : 7;
/* 1 = Upgrade header was present and the parser has exited because of that.
* 0 = No upgrade header present.
* Should be checked when http_parser_execute() returns in addition to
* error checking.
*/
unsigned char upgrade : 1;
/** PUBLIC **/
void *data; /* A pointer to get hook to the "connection" or "socket" object */
};
// from lj_http_parser.h
typedef struct ljhp_buffer
{
size_t size;
const void* data;
} ljhp_buffer;
typedef struct ljhp_header_entry
{
int type;
ljhp_buffer value;
struct ljhp_header_entry* next;
} ljhp_header_entry;
typedef struct ljhp_body_entry
{
ljhp_buffer value;
struct ljhp_body_entry* next;
} ljhp_body_entry;
typedef struct ljhp_http_parser
{
http_parser http_parser_;
http_parser_settings* http_parser_settings_;
ljhp_buffer url;
ljhp_header_entry* first;
ljhp_header_entry* last;
ljhp_body_entry* first_body;
ljhp_body_entry* last_body;
int32_t status_complete;
int32_t headers_complete;
int32_t message_complete;
} ljhp_http_parser;
ljhp_http_parser* ljhp_create_request_parser();
ljhp_http_parser* ljhp_create_response_parser();
void ljhp_http_parser_reset(ljhp_http_parser* parser);
void ljhp_http_parser_destroy(ljhp_http_parser* parser);
size_t ljhp_http_parser_execute(
ljhp_http_parser *parser,
const char *data,
size_t len
);
]]
--------------------------------------------------------------------------------
return core