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
|
/*
**********************************************************************************************
* File: http_decoder_table.c
* Description:
* Authors: LuWenPeng <[email protected]>
* Date: 2022-10-31
* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
***********************************************************************************************
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "http_decoder_table.h"
#include "http_decoder.h"
#include "http_decoder_string.h"
#include "stellar/utils.h"
#define INIT_HEADER_CNT 16
#define MAX_URI_CACHE_SIZE 2048
#define MAX_STATUS_CACHE_SIZE 32
#define MAX_METHOD_CACHE_SIZE 8
#define MAX_VERSION_CACHE_SIZE 4
#define MAX_HEADER_KEY_CACHE_SIZE 4096
#define MAX_HEADER_VALUE_CACHE_SIZE 4096
struct http_decoder_header {
struct http_decoder_string key;
struct http_decoder_string val;
};
struct http_decoder_table {
struct http_decoder_string uri;
struct http_decoder_string status;
struct http_decoder_string method;
struct http_decoder_string version;
struct http_decoder_string body;
nmx_pool_t *ref_mempool;
int header_complete; // flag for all headers parsed completely
size_t header_cnt;
size_t header_index;
size_t header_iter;
struct http_decoder_header *headers;
};
static void http_decoder_table_init(struct http_decoder_table *table)
{
if (NULL == table) {
return;
}
struct http_decoder_header *header = NULL;
assert(table);
http_decoder_string_init(&table->uri, MAX_URI_CACHE_SIZE);
http_decoder_string_init(&table->status, MAX_STATUS_CACHE_SIZE);
http_decoder_string_init(&table->method, MAX_METHOD_CACHE_SIZE);
http_decoder_string_init(&table->version, MAX_METHOD_CACHE_SIZE);
for (size_t i = 0; i < table->header_cnt; i++) {
header = &table->headers[i];
http_decoder_string_init(&header->key, MAX_HEADER_KEY_CACHE_SIZE);
http_decoder_string_init(&header->val, MAX_HEADER_VALUE_CACHE_SIZE);
}
http_decoder_string_init(&table->body, 0);
}
struct http_decoder_table *http_decoder_table_new(nmx_pool_t *mempool)
{
struct http_decoder_table *table =
MEMPOOL_CALLOC(mempool, struct http_decoder_table, 1);
assert(table);
table->ref_mempool = mempool;
table->header_cnt = INIT_HEADER_CNT;
table->headers = MEMPOOL_CALLOC(mempool, struct http_decoder_header,
table->header_cnt);
http_decoder_table_init(table);
return table;
}
void http_decoder_table_free(struct http_decoder_table *table)
{
if (NULL == table) {
return;
}
if (table->uri.cache.str != NULL) {
FREE(table->uri.cache.str);
}
if (table->status.cache.str != NULL) {
FREE(table->status.cache.str);
}
if (table->method.cache.str != NULL) {
FREE(table->method.cache.str);
}
if (table->version.cache.str != NULL) {
FREE(table->version.cache.str);
}
if (table->body.cache.str != NULL) {
FREE(table->body.cache.str);
}
if (table->headers != NULL) {
for (size_t i = 0; i < table->header_cnt; i++) {
if (table->headers[i].key.cache.str != NULL) {
FREE(table->headers[i].key.cache.str);
}
if (table->headers[i].val.cache.str != NULL) {
FREE(table->headers[i].val.cache.str);
}
}
MEMPOOL_FREE(table->ref_mempool, table->headers);
table->headers = NULL;
}
MEMPOOL_FREE(table->ref_mempool, table);
}
enum string_state
http_decoder_table_state(struct http_decoder_table *table, enum http_item type)
{
if (NULL == table) {
return STRING_STATE_INIT;
}
struct http_decoder_header *header = NULL;
enum string_state state = STRING_STATE_INIT;
assert(table);
switch (type) {
case HTTP_ITEM_URI:
state = http_decoder_string_state(&table->uri);
break;
case HTTP_ITEM_STATUS:
state = http_decoder_string_state(&table->status);
break;
case HTTP_ITEM_METHOD:
state = http_decoder_string_state(&table->method);
break;
case HTTP_ITEM_VERSION:
state = http_decoder_string_state(&table->version);
break;
case HTTP_ITEM_HDRKEY:
assert(table->header_index < table->header_cnt);
header = &table->headers[table->header_index];
state = http_decoder_string_state(&header->key);
break;
case HTTP_ITEM_HDRVAL:
assert(table->header_index < table->header_cnt);
header = &table->headers[table->header_index];
state = http_decoder_string_state(&header->val);
break;
case HTTP_ITEM_BODY:
state = http_decoder_string_state(&table->body);
break;
default:
abort();
break;
}
return state;
}
void http_decoder_table_refer(struct http_decoder_table *table, enum http_item type,
const char *at, size_t len)
{
if (NULL == table) {
return;
}
struct http_decoder_header *header = NULL;
assert(table);
switch (type) {
case HTTP_ITEM_URI:
http_decoder_string_refer(&table->uri, at, len);
break;
case HTTP_ITEM_STATUS:
http_decoder_string_refer(&table->status, at, len);
break;
case HTTP_ITEM_METHOD:
http_decoder_string_refer(&table->method, at, len);
break;
case HTTP_ITEM_VERSION:
http_decoder_string_refer(&table->version, at, len);
break;
case HTTP_ITEM_HDRKEY:
assert(table->header_index < table->header_cnt);
header = &table->headers[table->header_index];
http_decoder_string_refer(&header->key, at, len);
break;
case HTTP_ITEM_HDRVAL:
assert(table->header_index < table->header_cnt);
header = &table->headers[table->header_index];
http_decoder_string_refer(&header->val, at, len);
break;
case HTTP_ITEM_BODY:
http_decoder_string_refer(&table->body, at, len);
break;
default:
abort();
break;
}
}
void http_decoder_table_cache(struct http_decoder_table *table, enum http_item type)
{
if (NULL == table) {
return;
}
struct http_decoder_header *header = NULL;
assert(table);
switch (type) {
case HTTP_ITEM_URI:
http_decoder_string_cache(&table->uri);
break;
case HTTP_ITEM_STATUS:
http_decoder_string_cache(&table->status);
break;
case HTTP_ITEM_METHOD:
http_decoder_string_cache(&table->method);
break;
case HTTP_ITEM_VERSION:
http_decoder_string_cache(&table->version);
break;
case HTTP_ITEM_HDRKEY:
assert(table->header_index < table->header_cnt);
header = &table->headers[table->header_index];
http_decoder_string_cache(&header->key);
break;
case HTTP_ITEM_HDRVAL:
assert(table->header_index < table->header_cnt);
header = &table->headers[table->header_index];
http_decoder_string_cache(&header->val);
break;
case HTTP_ITEM_BODY:
http_decoder_string_cache(&table->body);
break;
default:
abort();
break;
}
}
void http_decoder_table_commit(struct http_decoder_table *table, enum http_item type)
{
if (NULL == table) {
return;
}
size_t i = 0;
struct http_decoder_header *header = NULL;
assert(table);
switch (type) {
case HTTP_ITEM_URI:
http_decoder_string_commit(&table->uri);
break;
case HTTP_ITEM_STATUS:
http_decoder_string_commit(&table->status);
break;
case HTTP_ITEM_METHOD:
http_decoder_string_commit(&table->method);
break;
case HTTP_ITEM_VERSION:
http_decoder_string_commit(&table->version);
break;
case HTTP_ITEM_HDRKEY:
assert(table->header_index < table->header_cnt);
header = &table->headers[table->header_index];
http_decoder_string_commit(&header->key);
break;
case HTTP_ITEM_HDRVAL:
header = &table->headers[table->header_index];
http_decoder_string_commit(&header->val);
// inc index
if ((table->header_index + 1) >= table->header_cnt) {
struct http_decoder_header *old_headers = table->headers;
table->headers =
MEMPOOL_CALLOC(table->ref_mempool, struct http_decoder_header,
table->header_cnt * 2);
table->header_cnt *= 2;
for (i = 0; i <= table->header_index; i++) {
table->headers[i] = old_headers[i];
}
MEMPOOL_FREE(table->ref_mempool, old_headers);
for (i = table->header_index + 1; i < table->header_cnt; i++) {
header = &table->headers[i];
memset(header, 0, sizeof(struct http_decoder_header));
http_decoder_string_init(&header->key, MAX_HEADER_KEY_CACHE_SIZE);
http_decoder_string_init(&header->val, MAX_HEADER_VALUE_CACHE_SIZE);
}
}
table->header_index++;
break;
case HTTP_ITEM_BODY:
http_decoder_string_commit(&table->body);
break;
default:
abort();
break;
}
}
void http_decoder_table_reset(struct http_decoder_table *table, enum http_item type)
{
if (NULL == table) {
return;
}
struct http_decoder_header *header = NULL;
assert(table);
switch (type) {
case HTTP_ITEM_URI:
http_decoder_string_reset(&table->uri);
break;
case HTTP_ITEM_STATUS:
http_decoder_string_reset(&table->status);
break;
case HTTP_ITEM_METHOD:
http_decoder_string_reset(&table->method);
break;
case HTTP_ITEM_VERSION:
http_decoder_string_reset(&table->version);
break;
case HTTP_ITEM_HDRKEY:
header = &table->headers[table->header_index];
http_decoder_string_reset(&header->key);
break;
case HTTP_ITEM_HDRVAL:
header = &table->headers[table->header_index];
http_decoder_string_reset(&header->val);
break;
case HTTP_ITEM_BODY:
http_decoder_string_reset(&table->body);
break;
default:
abort();
break;
}
}
void http_decoder_table_reinit(struct http_decoder_table *table)
{
if (NULL == table) {
return;
}
struct http_decoder_header *header = NULL;
assert(table);
http_decoder_string_reinit(&table->uri);
http_decoder_string_reinit(&table->status);
http_decoder_string_reinit(&table->method);
http_decoder_string_reinit(&table->version);
for (size_t i = 0; i < table->header_iter; i++) {
header = &table->headers[i];
http_decoder_string_reinit(&header->key);
http_decoder_string_reinit(&header->val);
}
http_decoder_string_reinit(&table->body);
}
void http_decoder_table_dump(struct http_decoder_table *table)
{
if (NULL == table) {
return;
}
http_decoder_string_dump(&table->uri, "uri");
http_decoder_string_dump(&table->status, "status");
http_decoder_string_dump(&table->method, "method");
http_decoder_string_dump(&table->version, "version");
http_decoder_string_dump(&table->body, "body");
for (size_t i = 0; i < table->header_cnt; i++) {
struct http_decoder_header *header = &table->headers[i];
if (NULL == header) {
continue;
}
http_decoder_string_dump(&header->key, "key");
http_decoder_string_dump(&header->val, "val");
}
}
int http_decoder_table_get_uri(struct http_decoder_table *table, struct hstring *out)
{
if (NULL == table || NULL == out) {
return -1;
}
return http_decoder_string_get(&table->uri, out);
}
int http_decoder_table_get_method(struct http_decoder_table *table, struct hstring *out)
{
if (NULL == table || NULL == out) {
return -1;
}
return http_decoder_string_get(&table->method, out);
}
int http_decoder_table_get_status(struct http_decoder_table *table, struct hstring *out)
{
if (NULL == table || NULL == out) {
return -1;
}
return http_decoder_string_get(&table->status, out);
}
int http_decoder_table_get_version(struct http_decoder_table *table, struct hstring *out)
{
if (NULL == table || NULL == out) {
return -1;
}
return http_decoder_string_get(&table->version, out);
}
int http_decoder_table_get_body(struct http_decoder_table *table, struct hstring *out)
{
if (NULL == table || NULL == out) {
return -1;
}
return http_decoder_string_get(&table->body, out);
}
int http_decoder_table_get_header(struct http_decoder_table *table, struct hstring *key,
struct http_header *hdr_result)
{
if (NULL == table || NULL == key->str || 0 == key->str_len || NULL == hdr_result) {
return -1;
}
for (size_t i = 0; i < table->header_cnt; i++) {
struct http_decoder_header *tmp_header = &table->headers[i];
if (tmp_header->key.commit.str_len != key->str_len) {
continue;
}
if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT &&
http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) {
struct hstring tmp_key;
http_decoder_string_get(&tmp_header->key, &tmp_key);
if (tmp_key.str_len == key->str_len &&
(0 == strncasecmp(tmp_key.str, key->str, key->str_len))) {
http_decoder_string_get(&tmp_header->key, &hdr_result->key);
http_decoder_string_get(&tmp_header->val, &hdr_result->val);
return 0;
}
}
}
return -1;
}
int http_decoder_table_iter_header(struct http_decoder_table *table,
struct http_header *hdr)
{
if (NULL == table || NULL == hdr) {
return -1;
}
if (table->header_iter >= table->header_cnt) {
return -1;
}
struct http_decoder_header *tmp_header = &table->headers[table->header_iter];
if (tmp_header != NULL) {
if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT &&
http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) {
http_decoder_string_get(&tmp_header->key, &hdr->key);
http_decoder_string_get(&tmp_header->val, &hdr->val);
table->header_iter++;
return 0;
}
}
hdr->key.str = NULL;
hdr->key.str_len = 0;
hdr->val.str = NULL;
hdr->val.str_len = 0;
return -1;
}
int http_decoder_table_reset_header_iter(struct http_decoder_table *table)
{
table->header_iter = 0;
return 0;
}
int http_decoder_table_has_parsed_header(struct http_decoder_table *table)
{
if (NULL == table || (table->header_iter == table->header_index)) {
return 0;
}
struct http_decoder_header *tmp_header = &table->headers[table->header_iter];
if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT &&
http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) {
return 1;
}
return 0;
}
int http_decoder_table_header_complete(struct http_decoder_table *table)
{
if (NULL == table) {
return -1;
}
return table->header_complete;
}
void http_decoder_table_set_header_complete(struct http_decoder_table *table)
{
if (NULL == table) {
return;
}
table->header_complete = 1;
}
void http_decoder_table_reset_header_complete(struct http_decoder_table *table)
{
if (NULL == table) {
return;
}
table->header_complete = 0;
}
|