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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
/*
**********************************************************************************************
* File: http_decoder_entry.c
* Description:
* Authors: LuWenPeng <[email protected]>
* Date: 2022-10-31
* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
***********************************************************************************************
*/
#include <assert.h>
#include <stdio.h>
#include "stellar/utils.h"
#include "stellar/session.h"
#include "stellar/session_mq.h"
#include "stellar/session_exdata.h"
#include "http_decoder.h"
#include "http_decoder_half.h"
#include "http_decoder_table.h"
#include "llhttp.h"
#define HTTP_IDENTIFY_LEN 16
#define HTTP_DECODER_RESULT_QUEUE_SIZE 16
#define HD_IS_CACHE_BODY 1
const char *http_decoder_topic = "HTTP_DECODER_MESSAGE";
struct http_decoder_result {
struct http_decoder_half_data *req_data;
struct http_decoder_half_data *res_data;
};
struct http_decoder_result_queue {
struct session *ref_session;
size_t req_index;
size_t res_index;
size_t del_index;
size_t queue_size;
struct http_decoder_result **array;
};
/**
* NOTE: http_message don't have the ownership of data
*/
struct http_message {
enum http_message_type type;
struct http_decoder_half_data *data;
};
struct http_decoder {
struct http_decoder_half *c2s_half;
struct http_decoder_half *s2c_half;
};
struct http_event_context {
int topic_id;
struct session *ref_session;
struct http_decoder_result_queue *ref_queue;
};
struct http_decoder_context {
int plugin_id;
int topic_id;
int ex_data_idx;
struct stellar *st;
struct http_decoder *decoder;
struct http_event_context *http_ev_ctx;
};
static struct http_decoder_result *
http_decoder_result_new()
{
struct http_decoder_result *result =
CALLOC(struct http_decoder_result, 1);
assert(result);
result->req_data = NULL;
result->res_data = NULL;
return result;
}
static void
http_decoder_result_free(struct http_decoder_result *result)
{
if (NULL == result) {
return;
}
if (result->req_data != NULL) {
http_decoder_half_data_free(result->req_data);
result->req_data = NULL;
}
if (result->res_data != NULL) {
http_decoder_half_data_free(result->res_data);
result->res_data = NULL;
}
FREE(result);
}
// Create a new http result and add it to the queue
static void
http_decoder_result_queue_push(struct http_decoder_result_queue *queue,
size_t index)
{
assert(queue);
assert(index < queue->queue_size);
if (queue->array[index] == NULL) {
queue->array[index] = http_decoder_result_new();
assert(queue->array[index]);
}
}
// Remove the http result from the queue but not destroy it
static void
http_decoder_result_queue_pop(struct http_decoder_result_queue *queue,
size_t index)
{
assert(queue);
assert(index < queue->queue_size);
if (queue->array[index] != NULL) {
http_decoder_result_free(queue->array[index]);
queue->array[index] = NULL;
}
}
static void
http_decoder_result_queue_inc_req_index(struct http_decoder_result_queue *queue)
{
assert(queue);
queue->req_index++;
queue->req_index = queue->req_index % queue->queue_size;
}
static void
http_decoder_result_queue_inc_res_index(struct http_decoder_result_queue *queue)
{
assert(queue);
queue->res_index++;
queue->res_index = queue->res_index % queue->queue_size;
}
static void
http_decoder_result_queue_inc_del_index(struct http_decoder_result_queue *queue)
{
assert(queue);
queue->del_index++;
queue->del_index = queue->del_index % queue->queue_size;
}
static void
http_decoder_result_queue_gc(struct http_decoder_result_queue *queue,
size_t index)
{
assert(queue);
assert(index < queue->queue_size);
if (index == queue->del_index) {
http_decoder_result_queue_pop(queue, index);
http_decoder_result_queue_inc_del_index(queue);
}
}
static void
http_event_handler(enum http_event event, struct http_decoder_half_data **data,
void *http_ev_ctx)
{
struct http_event_context *ctx = (struct http_event_context *)http_ev_ctx;
assert(ctx);
struct http_decoder_result_queue *queue = ctx->ref_queue;
struct http_message *msg = NULL;
switch (event) {
case HTTP_EVENT_REQ_INIT:
queue->array[queue->req_index]->req_data = http_decoder_half_data_new();
*data = queue->array[queue->req_index]->req_data;
break;
case HTTP_EVENT_REQ_LINE:
msg = CALLOC(struct http_message, 1);
msg->type = HTTP_MESSAGE_REQ_LINE;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
case HTTP_EVENT_REQ_HDR:
break;
case HTTP_EVENT_REQ_HDR_END:
msg = CALLOC(struct http_message, 1);
msg->type = HTTP_MESSAGE_REQ_HEADER;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
case HTTP_EVENT_REQ_BODY_BEGIN:
break;
case HTTP_EVENT_REQ_BODY_DATA:
msg = CALLOC(struct http_message, 1);
msg->type = HTTP_MESSAGE_REQ_BODY;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
case HTTP_EVENT_REQ_BODY_END:
break;
case HTTP_EVENT_REQ_END:
http_decoder_result_queue_inc_req_index(queue);
http_decoder_result_queue_gc(queue, queue->req_index);
break;
case HTTP_EVENT_RES_INIT:
queue->array[queue->res_index]->res_data = http_decoder_half_data_new();
*data = queue->array[queue->res_index]->res_data;
break;
case HTTP_EVENT_RES_LINE:
msg = CALLOC(struct http_message, 1);
msg->type = HTTP_MESSAGE_RES_LINE;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
case HTTP_EVENT_RES_HDR:
break;
case HTTP_EVENT_RES_HDR_END:
msg = CALLOC(struct http_message, 1);
msg->type = HTTP_MESSAGE_RES_HEADER;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
case HTTP_EVENT_RES_BODY_BEGIN:
break;
case HTTP_EVENT_RES_BODY_DATA:
msg = CALLOC(struct http_message, 1);
msg->type = HTTP_MESSAGE_RES_BODY;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
case HTTP_EVENT_RES_BODY_END:
break;
case HTTP_EVENT_RES_END:
http_decoder_result_queue_inc_res_index(queue);
http_decoder_result_queue_gc(queue, queue->res_index);
break;
default:
assert(0);
break;
}
}
static struct http_decoder *
http_decoder_new(http_event_cb *ev_cb, int is_cache_body)
{
struct http_decoder *decoder = CALLOC(struct http_decoder, 1);
assert(decoder);
decoder->c2s_half = http_decoder_half_new(ev_cb, is_cache_body);
decoder->s2c_half = http_decoder_half_new(ev_cb, is_cache_body);
return decoder;
}
static void
http_decoder_free(struct http_decoder *decoder)
{
if (NULL == decoder) {
return;
}
if (decoder->c2s_half != NULL) {
http_decoder_half_free(decoder->c2s_half);
decoder->c2s_half = NULL;
}
if (decoder->s2c_half != NULL) {
http_decoder_half_free(decoder->s2c_half);
decoder->s2c_half = NULL;
}
FREE(decoder);
}
static struct http_decoder_result_queue *
http_decoder_result_queue_new(size_t queue_size)
{
struct http_decoder_result_queue *queue =
CALLOC(struct http_decoder_result_queue, 1);
assert(queue);
queue->del_index = 0;
queue->req_index = 0;
queue->res_index = 0;
queue->queue_size = queue_size;
queue->array = CALLOC(struct http_decoder_result *, queue->queue_size);
assert(queue->array);
for (size_t i = 0; i < queue->queue_size; i++) {
queue->array[i] = CALLOC(struct http_decoder_result, 1);
}
return queue;
}
static void
http_decoder_result_queue_free(struct http_decoder_result_queue *queue)
{
if (NULL == queue) {
return;
}
if (queue->array != NULL) {
for (size_t i = 0; i < queue->queue_size; i++) {
if (queue->array[i] != NULL) {
http_decoder_result_free(queue->array[i]);
queue->array[i] = NULL;
}
}
FREE(queue->array);
}
FREE(queue);
}
static int
http_protocol_identify(const char *data, size_t data_len)
{
enum llhttp_type type = HTTP_BOTH;
llhttp_t parser;
llhttp_settings_t settings;
enum llhttp_errno error;
llhttp_settings_init(&settings);
llhttp_init(&parser, type, &settings);
error = llhttp_execute(&parser, data, data_len);
if (error != HPE_OK) {
return -1;
}
return 0;
}
int http_decoder_entry(struct session *sess, int events,
const struct packet *pkt, void *cb_arg)
{
struct http_decoder_context *ctx = (struct http_decoder_context *)cb_arg;
size_t payload_len = 0;
uint64_t inner_flag = 0;
int ret = session_is_inner_most(sess, &inner_flag);
if (0 == ret) {
return 0;
}
struct http_decoder_result_queue *queue =
session_get_ex_data(sess, ctx->ex_data_idx);;
if (events & SESS_EV_CLOSING) {
if (queue != NULL) {
http_decoder_result_queue_free(queue);
session_set_ex_data(sess, ctx->ex_data_idx, NULL);
}
return 0;
}
const char *payload = session_get0_current_payload(sess, &payload_len);
if (events & SESS_EV_OPENING) {
if (queue != NULL) {
fprintf(stderr,
"http_decoder_result_queue should be null for new session\n");
return -1;
}
//If not http, ignore this session
if (payload_len > 0) {
size_t http_identify_len =
payload_len > HTTP_IDENTIFY_LEN ? HTTP_IDENTIFY_LEN : payload_len;
ret = http_protocol_identify(payload, http_identify_len);
if (ret < 0) {
// ignore this session's event
struct session_event *s_event =
session_get_intrinsic_event(sess, ctx->plugin_id);
session_event_assign(s_event, ctx->st, sess, 0,
http_decoder_entry, ctx);
return 0;
}
}
queue = http_decoder_result_queue_new(HTTP_DECODER_RESULT_QUEUE_SIZE);
queue->ref_session = sess;
session_set_ex_data(sess, ctx->ex_data_idx, queue);
}
if (0 == payload_len || NULL == queue) {
return 0;
}
int dir = packet_get_direction(pkt);
if (dir < 0) {
return -1;
}
if (NULL == ctx->decoder) {
ctx->decoder = http_decoder_new(http_event_handler, 0);
}
struct http_decoder_half *cur_half = NULL;
if (dir == PACKET_DIRECTION_C2S) {
cur_half = ctx->decoder->c2s_half;
} else {
cur_half = ctx->decoder->s2c_half;
}
ctx->http_ev_ctx->topic_id = ctx->topic_id;
ctx->http_ev_ctx->ref_queue = queue;
ctx->http_ev_ctx->ref_session = sess;
ret = http_decoder_half_parse(cur_half, ctx->http_ev_ctx, payload,
payload_len);
if (ret < 0) {
if (dir == PACKET_DIRECTION_C2S) {
http_decoder_result_queue_pop(queue, queue->req_index);
} else {
http_decoder_result_queue_pop(queue, queue->res_index);
}
}
return 0;
}
static void
http_decoder_ex_data_free(struct session *s, int idx, void *ex_ptr, void *arg)
{
if (ex_ptr != NULL) {
FREE(ex_ptr);
}
}
static void
http_message_free(void *msg, void *cb_arg)
{
if (NULL == msg) {
return;
}
struct http_message *message = (struct http_message *)msg;
message->data = NULL; //don't have memory's ownership
FREE(message);
}
void *http_decoder_init(struct stellar *st)
{
struct http_decoder_context *ctx = CALLOC(struct http_decoder_context, 1);
ctx->http_ev_ctx = CALLOC(struct http_event_context, 1);
ctx->st = st;
ctx->ex_data_idx = stellar_session_get_ex_new_index(st, "HTTP_DECODER",
http_decoder_ex_data_free,
NULL);
int plugin_id = stellar_plugin_register(st, SESS_EV_TCP|SESS_EV_CLOSING,
http_decoder_entry, ctx);
if (plugin_id >= 0) {
ctx->plugin_id = plugin_id;
}
int topic_id = session_mq_get_topic_id(st, http_decoder_topic);
if (topic_id < 0) {
topic_id = session_mq_create_topic(st, http_decoder_topic,
http_message_free, NULL);
}
ctx->topic_id = topic_id;
printf("http_decoder_init: ex_data_idx:%d, plugin_id:%d, topic_id:%d\n",
ctx->ex_data_idx, ctx->plugin_id, ctx->topic_id);
return ctx;
}
void http_decoder_exit(void *decoder_ctx)
{
if (NULL == decoder_ctx) {
return;
}
struct http_decoder_context *ctx =
(struct http_decoder_context *)decoder_ctx;
if (ctx->http_ev_ctx != NULL) {
FREE(ctx->http_ev_ctx);
}
if (ctx->decoder != NULL) {
http_decoder_free(ctx->decoder);
ctx->decoder = NULL;
}
if (ctx->topic_id >= 0) {
session_mq_destroy_topic(ctx->st, ctx->topic_id);
}
FREE(decoder_ctx);
}
enum http_message_type
http_message_type(struct http_message *msg)
{
if (NULL == msg) {
return HTTP_MESSAGE_MAX;
}
return msg->type;
}
int
http_message_get_request_line(struct http_message *msg,
struct http_request_line *line)
{
if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_LINE || NULL == line) {
return -1;
}
return http_decoder_half_data_get_request_line(msg->data, line);
}
int
http_message_get_response_line(struct http_message *msg,
struct http_response_line *line)
{
if (NULL == msg || msg->type != HTTP_MESSAGE_RES_LINE || NULL == line) {
return -1;
}
return http_decoder_half_data_get_response_line(msg->data, line);
}
int
http_message_get_request_header(struct http_message *msg, struct hstring *key,
struct http_header *header_array, size_t array_size)
{
if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER
|| NULL == key || NULL == header_array || 0 == array_size) {
return -1;
}
return http_decoder_half_data_get_header(msg->data, key, header_array,
array_size);
}
int
http_message_get_response_header(struct http_message *msg, struct hstring *key,
struct http_header *header_array, size_t array_size)
{
if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER
|| NULL == key || NULL == header_array
|| 0 == array_size) {
return -1;
}
return http_decoder_half_data_get_header(msg->data, key, header_array,
array_size);
}
int
http_message_request_header_next(struct http_message *msg,
struct http_header *header)
{
if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER
|| NULL == header) {
return -1;
}
return http_decoder_half_data_iter_header(msg->data, header);
}
int
http_message_response_header_next(struct http_message *msg,
struct http_header *header)
{
if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER
|| NULL == header) {
return -1;
}
return http_decoder_half_data_iter_header(msg->data, header);
}
int
http_message_get_request_raw_body(struct http_message *msg,
struct hstring *body)
{
if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY)
|| NULL == body) {
return -1;
}
return http_decoder_half_data_get_raw_body(msg->data, body);
}
int
http_message_get_response_raw_body(struct http_message *msg,
struct hstring *body)
{
if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY)
|| NULL == body) {
return -1;
}
return http_decoder_half_data_get_raw_body(msg->data, body);
}
int
http_message_get_request_decompress_body(struct http_message *msg,
struct hstring *body)
{
if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY)
|| NULL == body) {
return -1;
}
return http_decoder_half_data_get_decompress_body(msg->data, body);
}
int
http_message_get_response_decompress_body(struct http_message *msg,
struct hstring *body)
{
if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY)
|| NULL == body) {
return -1;
}
return http_decoder_half_data_get_decompress_body(msg->data, body);
}
|