-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_parser_meth_two.h
422 lines (391 loc) · 17 KB
/
http_parser_meth_two.h
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
// poc-http-parser
// Copyright © Harsath <[email protected]>
// The software is licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef HTTP_PARSER_TWO_H
#define HTTP_PARSER_TWO_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// NOTE: 'mt' suffix 'Method-two' it's named that way due to name collision in
// benchmarks/tests
#ifndef DOXYGEN_SHOULD_SKIP_THIS
#define HEADER_NAME_BUFFER_SIZE_MT 1024
#define HEADER_VALUE_BUFFER_SIZE_MT 1024
#define POC_ALLOCATOR_MT(type, num_of_elements) \
calloc(num_of_elements, sizeof(type))
// Fast strcmp helpers
#define str4cmp_macro_mt(ptr, c0, c1, c2, c3) \
*(ptr + 0) == c0 &&*(ptr + 1) == c1 &&*(ptr + 2) == c2 &&*(ptr + 3) == \
c3
static bool str4cmp_mt(const char *ptr, const char *cmp) {
return str4cmp_macro_mt(ptr, *(cmp + 0), *(cmp + 1), *(cmp + 2),
*(cmp + 3));
}
#define str5cmp_macro_mt(ptr, c0, c1, c2, c3, c4) \
*(ptr + 0) == c0 &&*(ptr + 1) == c1 &&*(ptr + 2) == c2 &&*(ptr + 3) == \
c3 &&*(ptr + 4) == c4
static bool str5cmp_mt(const char *ptr, const char *cmp) {
return str5cmp_macro_mt(ptr, *(cmp + 0), *(cmp + 1), *(cmp + 2),
*(cmp + 3), *(cmp + 4));
}
#define str7cmp_macro_mt(ptr, c0, c1, c2, c3, c4, c5, c6) \
*(ptr + 0) == c0 &&*(ptr + 1) == c1 &&*(ptr + 2) == c2 &&*(ptr + 3) == \
c3 &&*(ptr + 4) == c4 &&*(ptr + 5) == c5 &&*(ptr + 6) == c6
static bool str7cmp_mt(const char *ptr, const char *cmp) {
return str7cmp_macro_mt(ptr, *(cmp + 0), *(cmp + 1), *(cmp + 2),
*(cmp + 3), *(cmp + 4), *(cmp + 5), *(cmp + 6));
}
#define str10cmp_macro_mt(ptr, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) \
*(ptr + 0) == c0 &&*(ptr + 1) == c1 &&*(ptr + 2) == c2 &&*(ptr + 3) == \
c3 &&*(ptr + 4) == c4 &&*(ptr + 5) == c5 &&*(ptr + 6) == \
c6 &&*(ptr + 7) == c7 &&*(ptr + 8) == c8 &&*(ptr + 9) == c9
static bool str10cmp_mt(const char *ptr, const char *cmp) {
return str10cmp_macro_mt(ptr, *(cmp + 0), *(cmp + 1), *(cmp + 2),
*(cmp + 3), *(cmp + 4), *(cmp + 5), *(cmp + 6),
*(cmp + 7), *(cmp + 8), *(cmp + 9));
}
typedef enum {
POC_HTTP_GET_mt = 10,
POC_HTTP_POST_mt,
POC_HTTP_PUT_mt,
POC_HTTP_DELETE_mt,
POC_HTTP_HEAD_mt,
} poc_HTTP_Method_mt;
typedef enum {
CR_mt = 0x0D,
LF_mt = 0x0A,
SP_mt = 0x20,
HT_mt = 0x09
} LexConst_mt;
typedef enum {
ERROR_UNSUPPORTED_HTTP_METHOD_mt = 10,
ERROR_UNSUPPORTED_HTTP_VERSION_mt,
ERROR_PROTOCOL_ERROR_mt,
ERROR_BUFFER_OVERFLOW_mt
} poc_Parser_Error_mt;
typedef enum { HTTP_1_1 = 10, HTTP_1_0 } poc_HTTP_Version_mt;
typedef struct {
char header_name[HEADER_NAME_BUFFER_SIZE_MT];
char header_value[HEADER_VALUE_BUFFER_SIZE_MT];
size_t header_name_current_index;
size_t header_value_current_index;
} poc_Header_Pair_mt;
typedef struct {
poc_Header_Pair_mt *http_header_pairs;
size_t total_header_pair;
size_t current_index;
} poc_Header_mt;
typedef struct {
unsigned char *buffer;
size_t total_memory;
size_t current_index;
} poc_Buffer_mt;
#endif // !DOXYGEN_SHOULD_SKIP_THIS
/**
* Allocate a poc_Buffer_mt object
*
* @param buffer_size Size of the buffer to be allocated
* @return Dynamically allocated poc_Buffer_mt object.
*/
static poc_Buffer_mt *poc_allocate_http_buffer_mt(size_t buffer_size) {
poc_Buffer_mt *http_buffer =
(poc_Buffer_mt *)POC_ALLOCATOR_MT(poc_Buffer_mt, 1);
http_buffer->buffer =
(unsigned char *)POC_ALLOCATOR_MT(unsigned char, buffer_size);
http_buffer->current_index = 0;
http_buffer->total_memory = buffer_size;
return http_buffer;
}
/**
* Allocate poc_Header_mt object for HTTP header parsing
*
* @param number_of_headers Pre-defined size of headers(must not overflow)
* @return Dynamically allocated poc_Header_mt object.
*/
static poc_Header_mt *poc_allocate_http_header_mt(size_t number_of_headers) {
poc_Header_mt *http_header =
(poc_Header_mt *)POC_ALLOCATOR_MT(poc_Header_mt, 1);
http_header->http_header_pairs = (poc_Header_Pair_mt *)POC_ALLOCATOR_MT(
poc_Header_Pair_mt, number_of_headers);
http_header->total_header_pair = number_of_headers;
http_header->current_index = 0;
for (size_t i = 0; i < number_of_headers; i++) {
memset(http_header->http_header_pairs[i].header_name, 0,
HEADER_NAME_BUFFER_SIZE_MT);
memset(http_header->http_header_pairs[i].header_value, 0,
HEADER_VALUE_BUFFER_SIZE_MT);
http_header->http_header_pairs[i].header_name_current_index = 0;
http_header->http_header_pairs[i].header_value_current_index =
0;
}
return http_header;
}
/**
* Free a poc_Buffer_mt object, previously allocated through helper function
*
* @param buffer Takes a poc_Buffer_mt object to be freed
*/
static void poc_free_buffer_mt(poc_Buffer_mt *buffer) {
free(buffer->buffer);
free(buffer);
}
/**
* Frees poc_Header_mt object, previously allocated through helper function
*
* @param http_header Takes poc_Header_mt object to be freed
*/
static void poc_free_http_header_mt(poc_Header_mt *http_header) {
free(http_header->http_header_pairs);
free(http_header);
}
#ifndef DOXYGEN_SHOULD_SKIP_THIS
#define POC_IS_SEPERATOR_MT(CHAR_VALUE) \
((CHAR_VALUE == '(') || (CHAR_VALUE == ')') || (CHAR_VALUE == '<') || \
(CHAR_VALUE == '>') || (CHAR_VALUE == '@') || (CHAR_VALUE == ',') || \
(CHAR_VALUE == ';') || (CHAR_VALUE == ':') || (CHAR_VALUE == '\\') || \
(CHAR_VALUE == '"') || (CHAR_VALUE == '/') || (CHAR_VALUE == '[') || \
(CHAR_VALUE == ']') || (CHAR_VALUE == '?') || (CHAR_VALUE == '=') || \
(CHAR_VALUE == '{') || (CHAR_VALUE == '}') || \
(CHAR_VALUE == (char)SP_mt) || (CHAR_VALUE == (char)HT_mt))
#define POC_IS_PRINTABLE_CHAR_MT(CHAR_VALUE) \
(((unsigned)CHAR_VALUE >= 0x20) && ((unsigned)CHAR_VALUE < 0x7F))
#define POC_IS_CHAR_MT(CHAR_VALUE) ((unsigned)CHAR_VALUE <= 127)
#define POC_IS_CONTROL_MT(CHAR_VALUE) \
((CHAR_VALUE >= 0 && CHAR_VALUE <= 31) || (CHAR_VALUE == 127))
#define POC_IS_TOKEN_MT(CHAR_VALUE) \
(POC_IS_CHAR_MT(CHAR_VALUE) && \
!(POC_IS_CONTROL_MT(CHAR_VALUE) || POC_IS_SEPERATOR_MT(CHAR_VALUE)))
#define POC_IS_TEXT_MT(CHAR_VALUE) \
(!POC_IS_CONTROL_MT(CHAR_VALUE) || (CHAR_VALUE) == (char)SP_mt || \
(CHAR_VALUE) == HT_mt)
#endif // !DOXYGEN_SHOULD_SKIP_THIS
/**
* Parse a HTTP 1.x Request-Message
*
* @param http_body Buffer for storing parsed HTTP message's body
* @param http_header Buffer for storing parsed HTTP message's headers
* @param raw_input_buffer Raw HTTP request message bytes read from the wire
* @param input_buffer_size Size of the raw HTTP request message bytes
* @param http_method Buffer for string parsed HTTP message's request method
* @param request_resource Buffer for storing parsed HTTP Message's request
* resource
* @param http_version Buffer for storing parsed HTTP message's request HTTP
* version
* @param http_parser_error If parser failed to parse the given HTTP request
* message, the error code will be stored in here.
* @return True if parsed successfully else, returns False.
*/
static bool
poc_http_parser_mt(poc_Buffer_mt *http_body, poc_Header_mt *http_header,
const char *raw_input_buffer, size_t input_buffer_size,
poc_HTTP_Method_mt *http_method, char *request_resource,
poc_HTTP_Version_mt *http_version,
poc_Parser_Error_mt *http_parser_error) {
size_t current_buffer_index = 0;
#ifndef DOXYGEN_SHOULD_SKIP_THIS
#define POC_INCREMENT_BUFFER_OFFSET_MT(offset_length) \
raw_input_buffer += offset_length; \
current_buffer_index += offset_length
#define CHECK_IF_GET_MT(input_buffer) \
do { \
if (str4cmp_mt(input_buffer, "GET ")) { \
*http_method = POC_HTTP_GET_mt; \
POC_INCREMENT_BUFFER_OFFSET_MT(4); \
goto PARSE_HTTP_REQUEST_METHOD; \
} \
} while (0)
#define CHECK_IF_POST_MT(input_buffer) \
do { \
if (str5cmp_mt(input_buffer, "POST ")) { \
*http_method = POC_HTTP_POST_mt; \
POC_INCREMENT_BUFFER_OFFSET_MT(5); \
goto PARSE_HTTP_REQUEST_METHOD; \
} \
} while (0)
#define CHECK_IF_HEAD_MT(input_buffer) \
do { \
if (str5cmp_mt(input_buffer, "HEAD ")) { \
*http_method = POC_HTTP_HEAD_mt; \
POC_INCREMENT_BUFFER_OFFSET_MT(5); \
goto PARSE_HTTP_REQUEST_METHOD; \
} \
} while (0)
#define CHECK_IF_PUT_MT(input_buffer) \
do { \
if (str4cmp_mt(input_buffer, "PUT ")) { \
*http_method = POC_HTTP_PUT_mt; \
POC_INCREMENT_BUFFER_OFFSET_MT(4); \
goto PARSE_HTTP_REQUEST_METHOD; \
} \
} while (0)
#define CHECK_IF_DELETE_MT(input_buffer) \
do { \
if (str7cmp_mt(input_buffer, "DELETE ")) { \
*http_method = POC_HTTP_DELETE_mt; \
POC_INCREMENT_BUFFER_OFFSET_MT(7); \
goto PARSE_HTTP_REQUEST_METHOD; \
} \
} while (0)
#define HANDLE_UNSUPPORTED_METHOD_MT() \
do { \
*http_parser_error = ERROR_UNSUPPORTED_HTTP_METHOD_mt; \
return false; \
} while (0)
#define CHECK_IF_HTTP_VERSION_SUPPORTED_MT(input_buffer) \
do { \
if (str10cmp_mt(input_buffer, "HTTP/1.1\r\n")) { \
*http_version = HTTP_1_1; \
POC_INCREMENT_BUFFER_OFFSET_MT(10); \
goto PARSE_HTTP_HEADERS; \
} else { \
*http_parser_error = \
ERROR_UNSUPPORTED_HTTP_VERSION_mt; \
return false; \
} \
} while (0)
#define POC_APPEND_CHAR_TO_HEADER_NAME_MT(CHAR_VALUE, header) \
do { \
size_t header_name_current_index = \
header->http_header_pairs[header->current_index] \
.header_name_current_index++; \
if (header->current_index >= header->total_header_pair) { \
*http_parser_error = ERROR_BUFFER_OVERFLOW_mt; \
return false; \
} \
if (header_name_current_index >= HEADER_NAME_BUFFER_SIZE_MT) { \
*http_parser_error = ERROR_BUFFER_OVERFLOW_mt; \
return false; \
} \
header->http_header_pairs[header->current_index] \
.header_name[header_name_current_index] = CHAR_VALUE; \
} while (0)
#define POC_APPEND_CHAR_TO_HEADER_VALUE_MT(CHAR_VALUE, header) \
do { \
size_t header_value_current_index = \
header->http_header_pairs[header->current_index] \
.header_value_current_index++; \
if (header->current_index >= header->total_header_pair) { \
*http_parser_error = ERROR_BUFFER_OVERFLOW_mt; \
return false; \
} \
if (header_value_current_index >= \
HEADER_VALUE_BUFFER_SIZE_MT) { \
*http_parser_error = ERROR_BUFFER_OVERFLOW_mt; \
return false; \
} \
header->http_header_pairs[header->current_index] \
.header_value[header_value_current_index] = CHAR_VALUE; \
} while (0)
#define POC_PROTOCOL_ERROR_HANDLER_MT() \
*http_parser_error = ERROR_PROTOCOL_ERROR_mt; \
return false
#endif // !DOXYGEN_SHOULD_SKIP_THIS
CHECK_IF_GET_MT(raw_input_buffer);
CHECK_IF_POST_MT(raw_input_buffer);
CHECK_IF_HEAD_MT(raw_input_buffer);
CHECK_IF_PUT_MT(raw_input_buffer);
CHECK_IF_DELETE_MT(raw_input_buffer);
HANDLE_UNSUPPORTED_METHOD_MT();
PARSE_HTTP_REQUEST_METHOD:
while (*raw_input_buffer != (char)SP_mt &&
POC_IS_PRINTABLE_CHAR_MT(*raw_input_buffer)) {
*request_resource++ = *raw_input_buffer;
POC_INCREMENT_BUFFER_OFFSET_MT(1);
}
if (*raw_input_buffer == (char)SP_mt) {
POC_INCREMENT_BUFFER_OFFSET_MT(1);
goto PARSE_HTTP_VERSION;
}
PARSE_HTTP_VERSION:
CHECK_IF_HTTP_VERSION_SUPPORTED_MT(raw_input_buffer);
PARSE_HTTP_HEADERS:;
// Small internal state-machine for parsing headers
typedef enum {
_HEADER_NAME,
_HEADER_VALUE,
_HEADER_LF,
_HEADER_END_LF
} _Header_Parsing_State;
_Header_Parsing_State current_state = _HEADER_NAME;
for (;;) {
switch (current_state) {
case _HEADER_NAME:
if (*raw_input_buffer == (char)CR_mt) {
POC_INCREMENT_BUFFER_OFFSET_MT(1);
current_state = _HEADER_END_LF;
} else if (POC_IS_TOKEN_MT(*raw_input_buffer)) {
POC_APPEND_CHAR_TO_HEADER_NAME_MT(
*raw_input_buffer, http_header);
POC_INCREMENT_BUFFER_OFFSET_MT(1);
} else if (*raw_input_buffer == ':') {
current_state = _HEADER_VALUE;
POC_INCREMENT_BUFFER_OFFSET_MT(1);
} else {
POC_PROTOCOL_ERROR_HANDLER_MT();
}
break;
case _HEADER_VALUE:
if (*raw_input_buffer == (char)CR_mt) {
POC_INCREMENT_BUFFER_OFFSET_MT(1);
current_state = _HEADER_LF;
} else if (*raw_input_buffer == (char)SP_mt &&
*(raw_input_buffer - 1) == ':') {
POC_INCREMENT_BUFFER_OFFSET_MT(1);
} else if (POC_IS_TEXT_MT(*raw_input_buffer)) {
POC_APPEND_CHAR_TO_HEADER_VALUE_MT(
*raw_input_buffer, http_header);
POC_INCREMENT_BUFFER_OFFSET_MT(1);
} else {
POC_PROTOCOL_ERROR_HANDLER_MT();
}
break;
case _HEADER_LF:
if (*raw_input_buffer == (char)LF_mt) {
POC_INCREMENT_BUFFER_OFFSET_MT(1);
current_state = _HEADER_NAME;
http_header->current_index++;
} else {
POC_PROTOCOL_ERROR_HANDLER_MT();
}
break;
case _HEADER_END_LF:
if (*raw_input_buffer == (char)LF_mt) {
POC_INCREMENT_BUFFER_OFFSET_MT(1);
goto PARSE_HTTP_BODY;
} else {
POC_PROTOCOL_ERROR_HANDLER_MT();
}
break;
}
}
PARSE_HTTP_BODY:;
size_t total_remaining_size = (input_buffer_size)-current_buffer_index;
if (total_remaining_size >= http_body->total_memory) {
*http_parser_error = ERROR_BUFFER_OVERFLOW_mt;
return false;
}
memcpy(http_body->buffer, raw_input_buffer, total_remaining_size);
http_body->current_index = (total_remaining_size - 1);
return true;
}
#endif // !HTTP_PARSER_TWO_H