-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmk_http_parser.c
463 lines (424 loc) · 15.4 KB
/
mk_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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Monkey HTTP Server
* ==================
* Copyright 2001-2014 Monkey Software LLC <eduardo@monkey.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <limits.h>
#include "mk_http_parser.h"
#define mark_end() \
req->end = req->i; \
req->chars = -1; \
eval_field(req, buffer)
#define parse_next() \
req->start = req->i + 1; \
continue
#define field_len() (req->end - req->start)
#define header_scope_eq(req, x) req->header_min = req->header_max = x
struct header_entry {
int len;
const char name[32];
};
struct header_entry headers_table[] = {
{ 6, "Accept" },
{ 14, "Accept-Charset" },
{ 15, "Accept-Encoding" },
{ 15, "Accept-Language" },
{ 13, "Authorization" },
{ 6, "Cookie" },
{ 10, "Connection" },
{ 14, "Content-Length" },
{ 13, "Content-Range" },
{ 12, "Content-Type" },
{ 17, "If-Modified-Since" },
{ 4, "Host" },
{ 13, "Last-Modified" },
{ 19, "Last-Modified-Since" },
{ 7, "Referer" },
{ 5, "Range" },
{ 10, "User-Agent" }
};
/* Macro just for testing the parser on specific locations */
#define remaining() \
{ \
printf("%s** Line: %i / Chars: %i%s / remaining:\n", \
ANSI_BOLD, __LINE__, req->chars, ANSI_RESET); \
int x = 0; \
for (x = i; x < len; x++) { \
if (buffer[x] == '\n') { \
printf("\\ n\n"); \
} \
else if (buffer[x] == '\r') { \
printf("\\ r\n"); \
} \
else { \
printf(" %c\n", buffer[x]); \
} \
\
} \
}
static inline int header_lookup(struct mk_http_parser *req, char *buffer)
{
int i;
int len;
struct mk_http_header *header;
struct header_entry *h;
len = (req->header_sep - req->header_key);
for (i = req->header_min; i <= req->header_max; i++) {
h = &headers_table[i];
/* Check string length first */
if (h->len != len) {
continue;
}
if (strncmp(buffer + req->header_key + 1,
h->name + 1,
len - 1) == 0) {
/* We got a header match, register the header index */
header = &req->headers[i];
header->type = i;
header->key.data = buffer + req->header_key;
header->key.len = len;
header->val.data = buffer + req->header_val;
header->val.len = req->end - req->header_val;
if (i == MK_HEADER_CONTENT_LENGTH) {
long val;
char *endptr;
val = strtol(header->val.data, &endptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0)) {
return -1;
}
if (endptr == header->val.data) {
return -1;
}
if (val < 0) {
return -1;
}
req->header_content_length = val;
}
printf(" ===> %sMATCH%s '%s' = '",
ANSI_YELLOW, ANSI_RESET, h->name);
int z;
for (z = req->header_val; z < req->end; z++) {
printf("%c", buffer[z]);
}
printf("'\n");
/* FIXME: register header value */
return 0;
}
}
printf(" ===> %sunknown header key%s\n",
ANSI_RED, ANSI_RESET);
return 0;
}
/*
* Parse the protocol and point relevant fields, don't take logic decisions
* based on this, just parse to locate things.
*/
int mk_http_parser(struct mk_http_parser *req, char *buffer, int len)
{
int i;
int ret;
int limit;
limit = len + req->i;
for (i = req->i; i < limit; req->i++, req->chars++, i++) {
/* FIRST LINE LEVEL: Method, URI & Protocol */
if (req->level == REQ_LEVEL_FIRST) {
switch (req->status) {
case MK_ST_REQ_METHOD: /* HTTP Method */
if (buffer[i] == ' ') {
mark_end();
req->status = MK_ST_REQ_URI;
if (req->end < 2) {
return MK_HTTP_ERROR;
}
parse_next();
}
break;
case MK_ST_REQ_URI: /* URI */
if (buffer[i] == ' ') {
mark_end();
req->status = MK_ST_REQ_PROT_VERSION;
if (field_len() < 1) {
return MK_HTTP_ERROR;
}
parse_next();
}
else if (buffer[i] == '?') {
mark_end();
req->status = MK_ST_REQ_QUERY_STRING;
parse_next();
}
break;
case MK_ST_REQ_QUERY_STRING: /* Query string */
if (buffer[i] == ' ') {
mark_end();
req->status = MK_ST_REQ_PROT_VERSION;
parse_next();
}
break;
case MK_ST_REQ_PROT_VERSION: /* Protocol Version */
if (buffer[i] == '\r') {
mark_end();
if (field_len() != 8) {
return MK_HTTP_ERROR;
}
req->status = MK_ST_FIRST_FINALIZING;
continue;
}
break;
case MK_ST_FIRST_FINALIZING: /* New Line */
if (buffer[i] == '\n') {
req->level = REQ_LEVEL_CONTINUE;
parse_next();
}
else {
return MK_HTTP_ERROR;
}
break;
case MK_ST_BLOCK_END:
if (buffer[i] == '\n') {
return MK_HTTP_OK;
}
else {
return MK_HTTP_ERROR;
}
break;
};
}
else if (req->level == REQ_LEVEL_CONTINUE) {
if (buffer[i] == '\r') {
req->level = REQ_LEVEL_FIRST;
req->status = MK_ST_BLOCK_END;
continue;
}
else {
req->level = REQ_LEVEL_HEADERS;
req->status = MK_ST_HEADER_KEY;
req->chars = 0;
}
}
/* HEADERS: all headers stuff */
if (req->level == REQ_LEVEL_HEADERS) {
/* Expect a Header key */
if (req->status == MK_ST_HEADER_KEY) {
if (buffer[i] == '\r') {
if (req->chars == 0) {
req->level = REQ_LEVEL_END;
parse_next();
}
else {
return MK_HTTP_ERROR;
}
}
if (req->chars == 0) {
/*
* We reach the start of a Header row, lets catch the most
* probable header. Note that we don't accept headers starting
* in lowercase.
*
* The goal of this 'first row character lookup', is to define a
* small range set of probable headers comparison once we catch
* a header end.
*/
switch (buffer[i]) {
case 'A':
req->header_min = MK_HEADER_ACCEPT;
req->header_max = MK_HEADER_AUTHORIZATION;
break;
case 'C':
req->header_min = MK_HEADER_COOKIE;
req->header_max = MK_HEADER_CONTENT_TYPE;
break;
case 'I':
header_scope_eq(req, MK_HEADER_IF_MODIFIED_SINCE);
break;
case 'H':
header_scope_eq(req, MK_HEADER_HOST);
break;
case 'L':
req->header_min = MK_HEADER_LAST_MODIFIED;
req->header_max = MK_HEADER_LAST_MODIFIED_SINCE;
break;
case 'R':
req->header_min = MK_HEADER_REFERER;
req->header_max = MK_HEADER_RANGE;
break;
case 'U':
header_scope_eq(req, MK_HEADER_USER_AGENT);
break;
default:
req->header_key = -1;
req->header_sep = -1;
req->header_min = -1;
req->header_max = -1;
};
req->header_key = i;
}
/* Found key/value separator */
if (buffer[i] == ':') {
/* Set the key/value middle point */
req->header_sep = i;
/* validate length */
mark_end();
if (field_len() < 1) {
return MK_HTTP_ERROR;
}
/* Wait for a value */
req->status = MK_ST_HEADER_VALUE;
parse_next();
}
}
/* Parsing the header value */
else if (req->status == MK_ST_HEADER_VALUE) {
/* Trim left, set starts only when found something != ' ' */
if (buffer[i] == '\r' || buffer[i] == '\n') {
return MK_HTTP_ERROR;
}
else if (buffer[i] != ' ') {
req->status = MK_ST_HEADER_VAL_STARTS;
req->start = req->header_val = i;
}
continue;
}
/* New header row starts */
else if (req->status == MK_ST_HEADER_VAL_STARTS) {
/* Maybe there is no more headers and we reach the end ? */
if (buffer[i] == '\r') {
mark_end();
if (field_len() <= 0) {
return MK_HTTP_ERROR;
}
req->status = MK_ST_HEADER_END;
/*
* A header row has ended, lets lookup the header and populate
* our headers table index.
*/
ret = header_lookup(req, buffer);
if (ret != 0) {
return MK_HTTP_ERROR;
}
parse_next();
}
else if (buffer[i] == '\n' && buffer[i - 1] != '\r') {
return MK_HTTP_ERROR;
}
continue;
}
else if (req->status == MK_ST_HEADER_END) {
if (buffer[i] == '\n') {
req->status = MK_ST_HEADER_KEY;
req->chars = -1;
parse_next();
}
else {
return MK_HTTP_ERROR;
}
}
}
else if (req->level == REQ_LEVEL_END) {
if (buffer[i] == '\n') {
req->level = REQ_LEVEL_BODY;
req->chars = -1;
parse_next();
}
else {
return MK_HTTP_ERROR;
}
}
else if (req->level == REQ_LEVEL_BODY) {
/*
* Reaching this level can means two things:
*
* - A Pipeline Request
* - A Body content (POST/PUT methods
*/
if (req->header_content_length > 0) {
req->body_received += (limit - i);
if (req->body_received == req->header_content_length) {
return MK_HTTP_OK;
}
else {
return MK_HTTP_PENDING;
}
}
return MK_HTTP_OK;
}
}
if (req->level == REQ_LEVEL_FIRST) {
if (req->status == MK_ST_REQ_METHOD) {
if (req->i > 10) {
return MK_HTTP_ERROR;
}
else {
return MK_HTTP_PENDING;
}
}
}
else if (req->level == REQ_LEVEL_HEADERS) {
if (req->status == MK_ST_HEADER_KEY) {
return MK_HTTP_PENDING;
}
else if (req->status == MK_ST_HEADER_VALUE) {
if (field_len() < 0) {
return MK_HTTP_PENDING;
}
}
}
else if (req->level == REQ_LEVEL_BODY) {
if (req->header_content_length > 0) {
req->body_received += (limit - i);
if (req->header_content_length == req->body_received) {
return MK_HTTP_OK;
}
else {
return MK_HTTP_PENDING;
}
}
if (req->header_content_length > 0 &&
req->body_received <= req->header_content_length) {
return MK_HTTP_PENDING;
}
else if (req->chars == 0) {
return MK_HTTP_OK;
}
else {
}
}
return MK_HTTP_PENDING;
}
struct mk_http_parser *mk_http_parser_new()
{
struct mk_http_parser *req;
req = malloc(sizeof(struct mk_http_parser));
req->i = 0;
req->level = REQ_LEVEL_FIRST;
req->status = MK_ST_REQ_METHOD;
req->length = 0;
req->start = 0;
req->end = 0;
req->chars = -1;
/* init headers */
req->header_min = -1;
req->header_max = -1;
req->header_sep = -1;
req->body_received = 0;
req->header_content_length = -1;
return req;
}