This repository has been 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
/
lj_http_parser.c
172 lines (153 loc) · 3.73 KB
/
lj_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
#include "lj_http_parser.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
// only for use within on_message_* callbacks
#define LJHP_PARSER ((ljhp_http_parser*)parser->data)
int on_message_begin(http_parser* parser)
{
return 0;
}
int on_url(http_parser* parser, const char *at, size_t length)
{
LJHP_PARSER->url.data = at;
LJHP_PARSER->url.size = length;
return 0;
}
int on_status_complete(http_parser* parser)
{
LJHP_PARSER->status_complete = 1;
return 0;
}
int on_header_field(http_parser* parser, const char *at, size_t length)
{
ljhp_header_entry *entry = malloc(sizeof(ljhp_header_entry));
if( LJHP_PARSER->last )
{
LJHP_PARSER->last->next = entry;
}
entry->value.data = at;
entry->value.size = length;
entry->next = 0;
entry->type = LJHP_HEADER_ENTRY_FIELD;
LJHP_PARSER->last = entry;
if( !LJHP_PARSER->first )
{
LJHP_PARSER->first = entry;
}
return 0;
}
int on_header_value(http_parser* parser, const char *at, size_t length)
{
assert(LJHP_PARSER->first);
ljhp_header_entry *entry = malloc(sizeof(ljhp_header_entry));
LJHP_PARSER->last->next = entry;
entry->value.data = at;
entry->value.size = length;
entry->next = 0;
entry->type = LJHP_HEADER_ENTRY_VALUE;
LJHP_PARSER->last = entry;
return 0;
}
int on_headers_complete(http_parser* parser)
{
LJHP_PARSER->headers_complete = 1;
return 0;
}
int on_body(http_parser* parser, const char *at, size_t length)
{
ljhp_body_entry *entry = malloc(sizeof(ljhp_body_entry));
LJHP_PARSER->last_body->next = entry;
entry->value.data = at;
entry->value.size = length;
entry->next = 0;
LJHP_PARSER->last_body = entry;
if( !LJHP_PARSER->first_body )
{
LJHP_PARSER->first_body = entry;
}
return 0;
}
int on_message_complete(http_parser* parser)
{
LJHP_PARSER->message_complete = 1;
return 0;
}
#undef PARSER
http_parser_settings callbacks =
{
.on_message_begin = on_message_begin,
.on_url = on_url,
.on_status_complete = on_status_complete,
.on_header_field = on_header_field,
.on_header_value = on_header_value,
.on_headers_complete = on_headers_complete,
.on_body = on_body,
.on_message_complete = on_message_complete
};
ljhp_http_parser* ljhp_http_parser_create(enum http_parser_type type)
{
ljhp_http_parser *parser = malloc(sizeof(ljhp_http_parser));
if( !parser )
{
return parser;
}
parser->http_parser_.data = parser;
parser->http_parser_.type = type;
parser->http_parser_settings_ = &callbacks;
parser->last = 0;
parser->first = 0;
parser->last_body = 0;
parser->first_body = 0;
ljhp_http_parser_reset(parser);
return parser;
}
ljhp_http_parser* ljhp_create_request_parser()
{
return ljhp_http_parser_create(HTTP_REQUEST);
}
ljhp_http_parser* ljhp_create_response_parser()
{
return ljhp_http_parser_create(HTTP_RESPONSE);
}
void ljhp_http_parser_reset(ljhp_http_parser* parser)
{
http_parser_init(&parser->http_parser_, parser->http_parser_.type);
parser->url.size = 0;
parser->status_complete = 0;
parser->headers_complete = 0;
parser->message_complete = 0;
ljhp_header_entry* h = parser->first;
while( h )
{
ljhp_header_entry* tmp = h->next;
free(h);
h = tmp;
}
parser->last = 0;
parser->first = 0;
ljhp_body_entry* b = parser->first_body;
while( h )
{
ljhp_body_entry* tmp = b->next;
free(b);
b = tmp;
}
parser->last_body = 0;
parser->first_body = 0;
}
void ljhp_http_parser_destroy(ljhp_http_parser* parser)
{
ljhp_http_parser_reset(parser);
free(parser);
}
size_t ljhp_http_parser_execute(ljhp_http_parser *parser,
const char *data,
size_t len)
{
return http_parser_execute(
&parser->http_parser_,
parser->http_parser_settings_,
data, len
);
}