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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
|
#include <stdlib.h>
#include <http_common.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <sys/param.h>
#include <event2/buffer.h>
#include <tfe_http.h>
#include <tfe_utils.h>
#include <http_half.h>
#include <http_convert.h>
#include <event.h>
#define __PARSER_TO_HF_PRIVATE(_parser) ((struct http_half_private *)(_parser->data))
static enum tfe_http_std_field __str_header_field_to_std_field_id(const char * str_field, size_t len)
{
/* TODO: store the header text in hash table or rbtree, or use AC multistring search algo. */
for (unsigned int i = 0; i < __str_std_header_field_map_size; i++)
{
const char * std_header_field_iter = __str_std_header_field_map[i];
if (std_header_field_iter == NULL)
continue;
/* std_header_field_iter must contains '\0' */
size_t std_field_length = strlen(std_header_field_iter);
/* but the str_field may don't contains '\0', so must use strnlen */
size_t field_length = strnlen(str_field, len);
if (std_field_length != field_length)
continue;
if (strncasecmp(std_header_field_iter, str_field, std_field_length) != 0)
continue;
return (enum tfe_http_std_field) i;
}
return TFE_HTTP_UNKNOWN_FIELD;
}
uint16_t __hf_content_encoding_parse(const char * str_content_encoding)
{
if (strcasestr(str_content_encoding, "gzip") != NULL)
{
return HTTP_ACCEPT_ENCODING_GZIP;
}
if (strcasestr(str_content_encoding, "x-gzip") != NULL)
{
return HTTP_ACCEPT_ENCODING_X_GZIP;
}
if (strcasestr(str_content_encoding, "deflate") != NULL)
{
return HTTP_ACCEPT_ENCODING_DEFLATE;
}
if (strcasestr(str_content_encoding, "bzip2") != NULL)
{
return HTTP_ACCEPT_ENCODING_BZIP2;
}
if (strcasestr(str_content_encoding, "x-bzip2") != NULL)
{
return HTTP_ACCEPT_ENCODING_X_BZIP2;
}
if(strcasestr(str_content_encoding, "br") != NULL)
{
return HTTP_ACCEPT_ENCODING_BR;
}
return HTTP_ACCEPT_ENCODING_NONE;
}
const char * __hf_content_encoding_to_str(unsigned int encode)
{
switch (encode)
{
case HTTP_ACCEPT_ENCODING_GZIP: return "gzip";
case HTTP_ACCEPT_ENCODING_X_GZIP: return "x-gzip";
case HTTP_ACCEPT_ENCODING_DEFLATE: return "deflate";
case HTTP_ACCEPT_ENCODING_BZIP2: return "bzip2";
case HTTP_ACCEPT_ENCODING_X_BZIP2: return "x-bzip2";
case HTTP_ACCEPT_ENCODING_BR: return "br";
default: return "";
}
}
/* To flush header field and value which stash in evbuffer */
static void __http_half_header_kv_complete(struct http_half_private * hf_private)
{
size_t sz_evbuf_field = evbuffer_get_length(hf_private->evbuf_header_field);
size_t sz_evbuf_value = evbuffer_get_length(hf_private->evbuf_header_value);
enum tfe_http_std_field std_field_id;
struct http_field_name compact_field;
static const char __zero = 0;
const char * str_field;
const char * str_value;
/* No header field or length of header field is zero, ignore this field-value pair. */
if (evbuffer_get_length(hf_private->evbuf_header_field) == 0)
{
goto __clear_buffer;
}
/* Write a '\0' for evbuffers */
evbuffer_add(hf_private->evbuf_header_field, &__zero, sizeof(__zero));
evbuffer_add(hf_private->evbuf_header_value, &__zero, sizeof(__zero));
sz_evbuf_field = evbuffer_get_length(hf_private->evbuf_header_field);
sz_evbuf_value = evbuffer_get_length(hf_private->evbuf_header_value);
/* Convert evbuffer to const char * pair */
str_field = (const char *) evbuffer_pullup(hf_private->evbuf_header_field, sz_evbuf_field);
str_value = (const char *) evbuffer_pullup(hf_private->evbuf_header_value, sz_evbuf_value);
std_field_id = __str_header_field_to_std_field_id(str_field, sz_evbuf_field);
if (std_field_id != TFE_HTTP_UNKNOWN_FIELD)
{
compact_field.field_id = std_field_id;
compact_field.field_name = NULL;
}
else
{
compact_field.field_id = TFE_HTTP_UNKNOWN_FIELD;
compact_field.field_name = (char *) str_field;
}
tfe_http_field_write(&hf_private->hf_public, &compact_field, str_value);
goto __clear_buffer;
__clear_buffer:
evbuffer_drain(hf_private->evbuf_header_field, sz_evbuf_field);
evbuffer_drain(hf_private->evbuf_header_value, sz_evbuf_value);
hf_private->is_evbuf_header_value_set = false;
hf_private->is_evbuf_header_field_set = false;
}
void __hf_public_req_fill_from_private(struct http_half_private * hf_private, struct http_parser * parser)
{
struct tfe_http_half * hf_public = &hf_private->hf_public;
struct tfe_http_req_spec * hf_req_spec = &hf_public->req_spec;
/* accept-encoding, host is located in header's K-V structure */
hf_req_spec->method = (enum tfe_http_std_method) parser->method;
hf_private->method_or_status = (enum tfe_http_std_method) parser->method;
const static struct http_field_name __host_field_name = {TFE_HTTP_HOST, NULL};
char *host= (char *)tfe_http_field_read(hf_public, &__host_field_name);
if(host)
{
hf_private->host = strdup(host);
hf_req_spec->host = hf_private->host;
}
/* uri is stored in underlay evbuffer, we need to append a terminal zero */
static const char __zero = 0;
evbuffer_add(hf_private->evbuf_uri, &__zero, sizeof(__zero));
hf_req_spec->uri = (char *) evbuffer_pullup(hf_private->evbuf_uri, evbuffer_get_length(hf_private->evbuf_uri));
/* TODO: url is more complex. need to review RFC */
if (hf_req_spec->uri[0] != '\0' && hf_req_spec->uri[0] != '/')
{
asprintf(&hf_private->url_storage, "%s/%s", hf_req_spec->host, hf_req_spec->uri);
}
else
{
asprintf(&hf_private->url_storage, "%s%s", hf_req_spec->host, hf_req_spec->uri);
}
hf_req_spec->url = hf_private->url_storage;
assert(hf_req_spec->url != NULL);
/* Accept-Encoding */
const static struct http_field_name __accept_encoding_name = {TFE_HTTP_ACCEPT_ENCODING, NULL};
const char * __str_accept_encoding = tfe_http_field_read(hf_public, &__accept_encoding_name);
if (__str_accept_encoding != NULL)
{
hf_private->accept_content_encoding = __hf_content_encoding_parse(__str_accept_encoding);
}
else
{
hf_private->accept_content_encoding = HTTP_ACCEPT_ENCODING_NONE;
}
}
void __hf_public_resp_fill_from_private(struct http_half_private * hf_private, struct http_parser * parser)
{
struct tfe_http_half * hf_public = &hf_private->hf_public;
struct tfe_http_resp_spec * hf_resp_spec = &hf_public->resp_spec;
/* Status Code */
hf_resp_spec->resp_code = parser->status_code;
/* Content Type */
const static struct http_field_name __cont_encoding_type_name = {TFE_HTTP_CONT_TYPE, NULL};
hf_resp_spec->content_type = (char *) tfe_http_field_read(hf_public, &__cont_encoding_type_name);
/* Content Length */
const static struct http_field_name __cont_encoding_length_name = {TFE_HTTP_CONT_LENGTH, NULL};
hf_resp_spec->content_length = (char *) tfe_http_field_read(hf_public, &__cont_encoding_length_name);
/* Content Encoding */
const static struct http_field_name __cont_encoding_field_name = {TFE_HTTP_CONT_ENCODING, NULL};
hf_resp_spec->content_encoding = (char *) tfe_http_field_read(hf_public, &__cont_encoding_field_name);
if (hf_resp_spec->content_encoding != NULL)
{
hf_private->content_encoding = __hf_content_encoding_parse(hf_resp_spec->content_encoding);
}
else
{
hf_private->content_encoding = HTTP_ACCEPT_ENCODING_NONE;
}
}
/* ==================================================================================================================
* REQUEST PARSER CALLBACKS
* ================================================================================================================== */
#define __HF_PRIVATE_CHANGE_STATUS(_status, _now, _to) \
do { assert(_status == _now); _status = _to; } while(0)
static int __parser_callback_on_message_begin(struct http_parser * parser)
{
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
assert(hf_private->evbuf_uri == NULL && hf_private->evbuf_body == NULL);
assert(hf_private->evbuf_header_field == NULL && hf_private->evbuf_header_value == NULL);
hf_private->evbuf_header_field = evbuffer_new();
hf_private->evbuf_header_value = evbuffer_new();
hf_private->is_evbuf_header_field_set = false;
hf_private->is_evbuf_header_value_set = false;
hf_private->evbuf_uri = evbuffer_new();
hf_private->evbuf_body = evbuffer_new();
hf_private->body_status = STATUS_INIT;
hf_private->message_status = STATUS_READING;
/* Never call user's callback, need to defer data */
hf_private->stream_action = ACTION_DEFER_DATA;
return 0;
}
static int __parser_callback_on_uri_field(struct http_parser * parser, const char * at, size_t length)
{
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
return evbuffer_add(hf_private->evbuf_uri, at, length);
}
static int __parser_callback_on_header_field(struct http_parser * parser, const char * at, size_t length)
{
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
if (hf_private->is_evbuf_header_field_set && hf_private->is_evbuf_header_value_set)
{
__http_half_header_kv_complete(hf_private);
}
hf_private->is_evbuf_header_field_set = true;
return evbuffer_add(hf_private->evbuf_header_field, at, length);
}
static int __parser_callback_on_header_value(struct http_parser * parser, const char * at, size_t length)
{
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
hf_private->is_evbuf_header_value_set = true;
return evbuffer_add(hf_private->evbuf_header_value, at, length);
}
static int __parser_callback_on_headers_complete(http_parser * parser)
{
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
enum tfe_http_direction hf_direction = hf_private->hf_public.direction;
struct http_session_private * hs_private = hf_private->session;
if (evbuffer_get_length(hf_private->evbuf_header_field) != 0)
{
__http_half_header_kv_complete(hf_private);
}
struct tfe_http_half * hf_public = &hf_private->hf_public;
hf_public->major_version = parser->http_major;
hf_public->minor_version = parser->http_minor;
/* Copy version to session */
if (hs_private != NULL)
{
to_hs_public(hs_private)->major_version = hf_public->major_version;
to_hs_public(hs_private)->minor_version = hf_public->minor_version;
}
if (hf_direction == TFE_HTTP_REQUEST)
{
__hf_public_req_fill_from_private(hf_private, parser);
}
else
{
__hf_public_resp_fill_from_private(hf_private, parser);
}
/* for POST, must contains 'content-length' */
if (hf_direction == TFE_HTTP_REQUEST && hf_private->method_or_status == TFE_HTTP_METHOD_POST)
{
const static struct http_field_name __cont_encoding_field_name = {TFE_HTTP_CONT_LENGTH, NULL};
char * __str_content_length = (char *) tfe_http_field_read(hf_public, &__cont_encoding_field_name);
/* Does not contain a content-length, passthrough the whole TCP connection */
if (unlikely(__str_content_length == NULL))
{
const struct http_field_name __transfer_encoding_field_name = {TFE_HTTP_TRANSFER_ENCODING, NULL};
char *__str_transfer_encoding = (char *)tfe_http_field_read(hf_public, &__transfer_encoding_field_name);
if (!(__str_transfer_encoding && strncasecmp(__str_transfer_encoding, "chunked", strlen("chunked")) == 0))
{
const char * __str_stream = hf_private->session->hc_private->stream->str_stream_info;
TFE_LOG_ERROR(g_http_plugin->logger, "the content-length and transfer-encoding field not set, passthrough the whole tcp connection: %s. ", __str_stream);
hf_private->is_passthrough = true;
return -1;
}
}
}
tfe_http_event event = (hf_direction == TFE_HTTP_REQUEST) ? EV_HTTP_REQ_HDR : EV_HTTP_RESP_HDR;
if (hf_private->event_cb)
{
hf_private->event_cb(hf_private, event, NULL, 0, hf_private->event_cb_user);
}
/* The setup of user stream option indicates that the way to handle the request/response has
* been decided, we should conform the resolution */
if (hf_private->is_user_stream_action_set)
{
assert(hf_private->stream_action == ACTION_DEFER_DATA);
hf_private->stream_action = hf_private->user_stream_action;
}
/* user's suspend tag is set, which indicate that the way to handle request/response
* cannot be determinate at now, need to defer */
else if (hs_private && hs_private->suspend_tag_signal)
{
/* Pause parser, prevent to parse request/response body,
* The body should be parsed after resume() */
http_parser_pause(parser, 1);
/* Record the event, this event will be trigger again at resume() */
hs_private->suspend_event = event;
/* Suspend must be setup at DEFER status.
* The request/response cannot be suspend once any bytes has been forwarded to upstream */
assert(hf_private->stream_action == ACTION_DEFER_DATA);
}
/* Otherwise, forward the request/response */
else
{
hf_private->stream_action = ACTION_FORWARD_DATA;
}
return 0;
}
static int __parser_callback_on_body(struct http_parser * parser, const char * at, size_t length)
{
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
enum tfe_http_direction hf_direction = hf_private->hf_public.direction;
enum tfe_http_event ev_body_begin;
enum tfe_http_event ev_body_cont;
if (hf_direction == TFE_HTTP_REQUEST)
{
ev_body_begin = EV_HTTP_REQ_BODY_BEGIN;
ev_body_cont = EV_HTTP_REQ_BODY_CONT;
}
else
{
ev_body_begin = EV_HTTP_RESP_BODY_BEGIN;
ev_body_cont = EV_HTTP_RESP_BODY_CONT;
}
if (hf_private->body_status == STATUS_INIT && hf_private->event_cb)
{
/* Create ungzip context */
if (hf_private->content_encoding != HTTP_ACCEPT_ENCODING_NONE)
{
hf_private->cv_uncompress_object = hf_content_uncompress_create(
hf_private->content_encoding, hf_private->event_cb, hf_private->event_cb_user);
if (unlikely(hf_private->cv_uncompress_object == NULL)) assert(0);
}
hf_private->event_cb(hf_private, ev_body_begin, NULL, parser->content_length, hf_private->event_cb_user);
hf_private->body_status = STATUS_READING;
}
int ret = 0;
if (hf_private->event_cb && length != 0)
{
if (hf_private->cv_uncompress_object != NULL)
{
ret = hf_content_uncompress_write(hf_private->cv_uncompress_object, hf_private, ev_body_cont,
(const unsigned char *) at, length);
}
else
{
ret = hf_private->event_cb(hf_private, ev_body_cont, (const unsigned char *) at,
length, hf_private->event_cb_user);
}
if (unlikely(ret < 0))
{
static const char * __str_error_uncompress = "Failed at uncompress HTTP body";
static const char * __str_error_event = "Failed at calling user's callback for HTTP body";
const char * __what = hf_private->cv_uncompress_object != NULL ? __str_error_uncompress : __str_error_event;
const char * __str_stream = hf_private->session->hc_private->stream->str_stream_info;
const char * __str_url = hf_private->session->hs_public.req->req_spec.url;
TFE_LOG_ERROR(g_http_plugin->logger, "%s %s %s, ret = %d. ", __str_stream, __str_url, __what, ret);
hf_private->is_passthrough = true; return -1;
}
}
return 0;
}
static int __parser_callback_on_message_complete(http_parser * parser)
{
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
enum tfe_http_direction hf_direction = hf_private->hf_public.direction;
enum tfe_http_event ev_message_end;
enum tfe_http_event ev_body_end;
if (hf_direction == TFE_HTTP_REQUEST)
{
ev_message_end = EV_HTTP_REQ_END;
ev_body_end = EV_HTTP_REQ_BODY_END;
}
else
{
ev_message_end = EV_HTTP_RESP_END;
ev_body_end = EV_HTTP_RESP_BODY_END;
}
if (hf_private->event_cb && hf_private->body_status == STATUS_READING)
{
hf_private->event_cb(hf_private, ev_body_end, NULL, 0, hf_private->event_cb_user);
}
if (hf_private->event_cb)
{
hf_private->event_cb(hf_private, ev_message_end, NULL, 0, hf_private->event_cb_user);
}
hf_private->body_status = STATUS_COMPLETE;
hf_private->message_status = STATUS_COMPLETE;
http_parser_pause(parser, 1);
return 0;
}
static http_parser_settings __http_half_parse_setting =
{
.on_message_begin = __parser_callback_on_message_begin,
.on_url = __parser_callback_on_uri_field,
.on_status = NULL,
.on_header_field = __parser_callback_on_header_field,
.on_header_value = __parser_callback_on_header_value,
.on_headers_complete = __parser_callback_on_headers_complete,
.on_body = __parser_callback_on_body,
.on_message_complete = __parser_callback_on_message_complete,
.on_chunk_header = NULL,
.on_chunk_complete = NULL
};
const char * hf_ops_field_read(const struct tfe_http_half * half, const struct http_field_name * field)
{
const struct http_half_private * hf_private = to_hf_private(half);
assert(hf_private->major == 0 || hf_private->major == 1);
struct http_header_private * __header_iter = NULL;
struct http_header_private * __header_found = NULL;
TAILQ_FOREACH(__header_iter, &hf_private->header_list, next)
{
if (http_field_name_compare(__header_iter->field, field) != 0) continue;
__header_found = __header_iter;
break;
}
return __header_found != NULL ? __header_iter->value : NULL;
}
int hf_ops_field_write(struct tfe_http_half * half, const struct http_field_name * field, const char * value)
{
struct http_half_private * hf_private = to_hf_private(half);
assert(hf_private->major == 0 || hf_private->major == 1);
/* Add a k-v in the tail of the header list */
if (value != NULL)
{
struct http_header_private * new_header = ALLOC(struct http_header_private, 1);
new_header->field = http_field_name_duplicate(field);
new_header->value = tfe_strdup(value);
TAILQ_INSERT_TAIL(&hf_private->header_list, new_header, next);
}
/* Delete a value */
else
{
struct http_header_private * header_iter = NULL;
struct http_header_private * header_titer = NULL;
bool delete_success = false;
TAILQ_FOREACH_SAFE(header_iter, &hf_private->header_list, next, header_titer)
{
if (http_field_name_compare(header_iter->field, field) != 0)
continue;
TAILQ_REMOVE(&hf_private->header_list, header_iter, next);
http_field_name_destory(header_iter->field);
free(header_iter->value);
free(header_iter);
delete_success = true;
}
return delete_success ? 0 : -ENOENT;
}
return 0;
}
struct tfe_http_half * hf_ops_allow_write(const struct tfe_http_half * half)
{
return (struct tfe_http_half *) half;
}
const char * hf_ops_field_iterate(const struct tfe_http_half * half, void ** iter, struct http_field_name * field)
{
struct http_header_private ** __header_iter = (struct http_header_private **) iter;
const struct http_half_private * hf_private = to_hf_private(half);
if (*__header_iter == NULL)
{
*__header_iter = TAILQ_FIRST(&hf_private->header_list);
}
else
{
*__header_iter = TAILQ_NEXT(*__header_iter, next);
}
if (*__header_iter == NULL) return NULL;
/* Reference of inner data, user should copy it */
field->field_id = (*__header_iter)->field->field_id;
field->field_name = (*__header_iter)->field->field_name;
return (*__header_iter)->value;
}
int hf_ops_append_body(struct tfe_http_half * half, char * buff, size_t size, int flag)
{
struct http_half_private * hf_private = to_hf_private(half);
/* Indicate the body is finished */
if (buff == NULL && size == 0)
{
hf_private->message_status = STATUS_COMPLETE;
return 0;
}
if (hf_private->evbuf_body == NULL)
{
hf_private->evbuf_body = evbuffer_new();
}
int ret = 0;
if (hf_private->cv_compress_object)
{
ret = hf_content_compress_write(hf_private->cv_compress_object,
(const unsigned char *) buff, size, hf_private->evbuf_body, 0);
}
else
{
ret = evbuffer_add(hf_private->evbuf_body, buff, size);
}
return ret;
}
int hf_ops_body_begin(struct tfe_http_half * half, int by_stream)
{
struct http_half_private * hf_private = to_hf_private(half);
struct http_session_private * hs_private = hf_private->session;
assert(hf_private->evbuf_body == NULL);
if (by_stream)
{
/* By stream, we do not support content-encoding for now,
* send body directly without compression */
if (hf_private->cv_compress_object != NULL)
{
hf_content_compress_destroy(hf_private->cv_compress_object);
hf_private->cv_compress_object = NULL;
}
hf_private->content_encoding = HTTP_ACCEPT_ENCODING_NONE;
hf_private->message_status = STATUS_READING;
hf_private->is_setup_by_stream = true;
hs_private->release_lock++;
}
hf_private->evbuf_body = evbuffer_new();
return 0;
}
int hf_ops_body_data(struct tfe_http_half * half, const unsigned char * data, size_t sz_data)
{
struct http_half_private * hf_private = to_hf_private(half);
struct evbuffer * __tmp_output_buffer = evbuffer_new();
int ret = 0;
/* Need to compress output */
if (hf_private->cv_compress_object)
{
ret = hf_content_compress_write(hf_private->cv_compress_object, data, sz_data, __tmp_output_buffer, 0);
}
else
{
ret = evbuffer_add(__tmp_output_buffer, data, sz_data);
}
if (ret < 0) goto __out;
ret = evbuffer_add_buffer(hf_private->evbuf_body, __tmp_output_buffer);
/* Have write ctx, should write the body to TCP stream immediately but not to store in evbuf_body */
if (hf_private->write_ctx)
{
/* Perpare to write data, TODO: write to TCP stream by transfer evbuffer */
const unsigned char * ptr_write_data = evbuffer_pullup(hf_private->evbuf_body, -1);
size_t sz_write_data = evbuffer_get_length(hf_private->evbuf_body);
assert(ptr_write_data != NULL && sz_write_data >= 0);
tfe_stream_write_frag(hf_private->write_ctx, ptr_write_data, sz_write_data);
/* Need to drain all data */
evbuffer_drain(hf_private->evbuf_body, sz_write_data);
}
__out:
evbuffer_free(__tmp_output_buffer);
return ret;
}
int hf_ops_body_end(struct tfe_http_half * half)
{
struct http_half_private * hf_private = to_hf_private(half);
struct http_session_private * hs_private = hf_private->session;
if (hf_private->write_ctx)
{
tfe_stream_write_frag_end(hf_private->write_ctx);
hf_private->write_ctx = NULL;
}
hf_private->body_status = STATUS_COMPLETE;
hf_private->message_status = STATUS_COMPLETE;
if (hf_private->is_setup_by_stream)
{
hs_private->release_lock--;
}
return 0;
}
void hf_private_destory(struct http_half_private * hf_private)
{
if (hf_private->parse_object != NULL)
{
free(hf_private->parse_object);
hf_private->parse_object = NULL;
}
struct http_header_private * header_iter = NULL;
struct http_header_private * header_titer = NULL;
TAILQ_FOREACH_SAFE(header_iter, &hf_private->header_list, next, header_titer)
{
TAILQ_REMOVE(&hf_private->header_list, header_iter, next);
http_field_name_destory(header_iter->field);
free(header_iter->value);
free(header_iter);
}
if (hf_private->event_cb_user_deleter != NULL)
{
hf_private->event_cb_user_deleter(hf_private->event_cb_user);
}
if (hf_private->evbuf_uri != NULL)
{
evbuffer_free(hf_private->evbuf_uri);
hf_private->evbuf_uri = NULL;
}
if(hf_private->host)
{
free(hf_private->host);
hf_private->host = NULL;
}
if (hf_private->url_storage)
{
free(hf_private->url_storage);
hf_private->url_storage = NULL;
}
if (hf_private->cv_uncompress_object)
{
hf_content_uncompress_destroy(hf_private->cv_uncompress_object);
hf_private->cv_uncompress_object = NULL;
}
if (hf_private->cv_compress_object)
{
hf_content_compress_destroy(hf_private->cv_compress_object);
hf_private->cv_compress_object = NULL;
}
if (hf_private->evbuf_header_field)
{
evbuffer_free(hf_private->evbuf_header_field);
hf_private->evbuf_header_field = NULL;
}
if (hf_private->evbuf_header_value)
{
evbuffer_free(hf_private->evbuf_header_value);
hf_private->evbuf_header_value = NULL;
}
if (hf_private->evbuf_body)
{
evbuffer_free(hf_private->evbuf_body);
hf_private->evbuf_body = NULL;
}
if (hf_private->evbuf_raw)
{
evbuffer_free(hf_private->evbuf_raw);
hf_private->evbuf_raw = NULL;
}
free(hf_private);
}
void hf_ops_free(struct tfe_http_half * half)
{
return hf_private_destory(to_hf_private(half));
}
struct tfe_http_half_ops __http_half_ops =
{
.ops_http_field_read = hf_ops_field_read,
.ops_http_field_write = hf_ops_field_write,
.ops_http_allow_write = hf_ops_allow_write,
.ops_http_field_iterate = hf_ops_field_iterate,
.ops_append_body = hf_ops_append_body,
.ops_body_begin = hf_ops_body_begin,
.ops_body_data = hf_ops_body_data,
.ops_body_end = hf_ops_body_end,
.ops_free = hf_ops_free
};
struct http_half_private * hf_private_create(tfe_http_direction ht_dir, short major, short minor)
{
struct http_half_private * hf_private = ALLOC(struct http_half_private, 1);
assert(hf_private != NULL && (major == 0 || major == 1) && (minor == 0 || minor == 1));
assert(ht_dir == TFE_HTTP_REQUEST || ht_dir == TFE_HTTP_RESPONSE);
/* PUBLIC */
hf_private->hf_public.major_version = major;
hf_private->hf_public.minor_version = minor;
hf_private->hf_public.direction = ht_dir;
hf_private->hf_public.ops = &__http_half_ops;
/* PRIVATE */
hf_private->parse_object = ALLOC(struct http_parser, 1);
assert(hf_private->parse_object != NULL);
if (ht_dir == TFE_HTTP_REQUEST)
{
http_parser_init(hf_private->parse_object, HTTP_REQUEST);
}
else
{
http_parser_init(hf_private->parse_object, HTTP_RESPONSE);
}
hf_private->parse_settings = &__http_half_parse_setting;
hf_private->parse_object->data = hf_private;
hf_private->major = major;
hf_private->minor = minor;
TAILQ_INIT(&hf_private->header_list);
return hf_private;
}
void hf_private_set_callback(struct http_half_private * hf_private, hf_private_cb * cb,
void * user, void (* user_deleter)(void *))
{
hf_private->event_cb = cb;
hf_private->event_cb_user = user;
hf_private->event_cb_user_deleter = user_deleter;
}
void hf_private_set_session(struct http_half_private * hf_private, struct http_session_private * hs_private)
{
hf_private->session = hs_private;
}
int hf_private_parse(struct http_half_private * hf_private, const unsigned char * data, size_t len)
{
assert(hf_private->parse_cursor <= len);
/* Caculate the memory zones to scan. The zone from data to data + cursor has been scaned
* at last construct procedure, so we don't need to scan again. */
const char * __data_with_offset = (const char *) TFE_PTR_ADD(data, hf_private->parse_cursor);
size_t __len_with_offset = len - hf_private->parse_cursor;
/* Scan the memory zone */
size_t sz_parsed = http_parser_execute(hf_private->parse_object,
&__http_half_parse_setting, __data_with_offset, __len_with_offset);
bool __is_paused = false;
if (HTTP_PARSER_ERRNO(hf_private->parse_object) == HPE_PAUSED)
{
http_parser_pause(hf_private->parse_object, 0);
__is_paused = true;
}
if (hf_private->parse_object->upgrade)
{
hf_private->is_upgrade = true;
}
if (sz_parsed == __len_with_offset)
{
hf_private->parse_cursor += sz_parsed;
return __is_paused ? 1 : 0;
}
/* The paused parsar indicate the message boundary has been touched, we should return.
* resume it to normal status */
if (__is_paused)
{
hf_private->parse_cursor += sz_parsed;
return 1;
}
hf_private->parse_errno = HTTP_PARSER_ERRNO(hf_private->parse_object);
assert(hf_private->parse_errno != HPE_OK);
return -1;
}
static struct tfe_http_session * hs_ops_allow_write(const struct tfe_http_session * session)
{
struct http_session_private * hs_private = to_hs_private((struct tfe_http_session *) session);
return http_frame_currect_plugin_preempt(hs_private->ht_frame) == 0 ? (struct tfe_http_session *) session : NULL;
}
void hs_ops_detach(const struct tfe_http_session * session)
{
struct http_session_private * hs_private = to_hs_private((struct tfe_http_session *) session);
return http_frame_currect_plugin_detach(hs_private->ht_frame);
}
void hs_ops_drop(struct tfe_http_session * session)
{}
void hs_ops_kill(struct tfe_http_session * session)
{
struct http_session_private * hs_private = to_hs_private((struct tfe_http_session *) session);
hs_private->kill_signal = true;
}
void hs_ops_suspend(struct tfe_http_session * session)
{
struct http_session_private * hs_private = to_hs_private(session);
hs_private->suspend_tag_signal = true;
}
void hs_ops_resume(struct tfe_http_session * session)
{
struct http_session_private * hs_private = to_hs_private(session);
struct http_connection_private * hc_private = hs_private->hc_private;
if (hs_private->suspend_tag_effective)
{
tfe_stream_resume(hc_private->stream);
hs_private->resume_tag_singal = true;
}
}
// TODO: change the return type to int, there is something happend where -1 returned.
void hs_ops_request_set(struct tfe_http_session * session, struct tfe_http_half * req_user)
{
struct http_session_private * hs_private = to_hs_private(session);
struct http_half_private * hf_in_private = to_hf_request_private(hs_private);
struct http_half_private * hf_user_private = to_hf_private(req_user);
if (hf_in_private != NULL)
{
if (hf_in_private->stream_action == ACTION_DEFER_DATA)
{
hf_in_private->user_stream_action = ACTION_DROP_DATA;
hf_in_private->is_user_stream_action_set = true;
}
else
{
assert(0);
}
}
assert(hs_private->hf_private_req_user == NULL);
hs_private->hf_private_req_user = hf_user_private;
hs_private->hf_private_req_user->session = hs_private;
}
void hs_ops_response_set(struct tfe_http_session * session, struct tfe_http_half * resp)
{
struct http_half_private * hf_private = to_hf_private(resp);
struct http_session_private * hs_private = to_hs_private(session);
struct http_half_private * hf_in_req_private = to_hf_request_private(hs_private);
struct http_half_private * hf_in_resp_private = to_hf_response_private(hs_private);
/* Call at request's callback, early response */
if (hf_in_req_private != NULL && hf_in_resp_private == NULL)
{
/* Drop the incoming request */
if (hf_in_req_private->stream_action == ACTION_DEFER_DATA)
{
hf_in_req_private->user_stream_action = ACTION_DROP_DATA;
hf_in_req_private->is_user_stream_action_set = true;
}
}
/* Call at response's callback, replace incoming response */
if (hf_in_req_private != NULL && hf_in_resp_private != NULL)
{
if (hf_in_resp_private->stream_action == ACTION_DEFER_DATA)
{
hf_in_resp_private->user_stream_action = ACTION_DROP_DATA;
hf_in_resp_private->is_user_stream_action_set = true;
}
}
assert(hs_private->hf_private_resp_user == NULL);
hs_private->hf_private_resp_user = hf_private;
hs_private->hf_private_resp_user->session = hs_private;
}
struct tfe_http_half * hs_ops_request_create(struct tfe_http_session * session,
enum tfe_http_std_method method, const char * uri)
{
struct http_half_private * hf_req_private = hf_private_create(TFE_HTTP_REQUEST,
session->major_version, session->minor_version);
hf_req_private->method_or_status = method;
hf_req_private->url_storage = tfe_strdup(uri);
hf_req_private->is_setup_by_user = true;
return to_hf_public(hf_req_private);
}
struct tfe_http_half * hs_ops_response_create(struct tfe_http_session * session, int resp_code)
{
struct http_half_private * hf_resp_private = hf_private_create(TFE_HTTP_RESPONSE,
session->major_version, session->minor_version);
hf_resp_private->method_or_status = resp_code;
hf_resp_private->is_setup_by_user = true;
/* Inherit from request in currect session */
struct http_session_private * ss_private = to_hs_private(session);
struct http_half_private * hf_req_private = to_hf_request_private(ss_private);
/* we must send content in encode that client asked,
* but dont need to be accordance with upstream server's response */
unsigned int content_encoding = HTTP_ACCEPT_ENCODING_NONE;
if (hf_req_private != NULL)
{
hf_resp_private->content_encoding = hf_req_private->accept_content_encoding;
content_encoding = hf_resp_private->content_encoding;
}
/* Create content compress content */
if (content_encoding != HTTP_ACCEPT_ENCODING_NONE)
{
hf_resp_private->cv_compress_object = hf_content_compress_create(content_encoding);
assert(hf_resp_private->cv_compress_object != NULL);
}
return to_hf_public(hf_resp_private);
}
struct tfe_http_session_ops __http_session_ops =
{
.ops_allow_write = hs_ops_allow_write,
.ops_detach = hs_ops_detach,
.ops_drop = hs_ops_drop,
.ops_suspend = hs_ops_suspend,
.ops_resume = hs_ops_resume,
.ops_kill = hs_ops_kill,
.ops_request_set = hs_ops_request_set,
.ops_response_set = hs_ops_response_set,
.ops_request_create = hs_ops_request_create,
.ops_response_create = hs_ops_response_create
};
void __construct_request_line(struct http_half_private * hf_private)
{
enum tfe_http_std_method __std_method = (enum tfe_http_std_method) hf_private->method_or_status;
const char * __str_method = http_std_method_to_string(__std_method);
if (__str_method == NULL)
{
__str_method = "";
}
evbuffer_add_printf(hf_private->evbuf_raw, "%s %s HTTP/%d.%d\r\n",
__str_method, hf_private->url_storage, hf_private->major, hf_private->minor);
}
void __construct_response_line(struct http_half_private * hf_private)
{
enum tfe_http_std_status __resp_code = (enum tfe_http_std_status) hf_private->method_or_status;
const char * __str_resp_code = http_std_status_to_string(__resp_code);
if (__str_resp_code == NULL)
{
__str_resp_code = "";
}
evbuffer_add_printf(hf_private->evbuf_raw, "HTTP/%d.%d %d %s\r\n",
hf_private->major, hf_private->minor, __resp_code, __str_resp_code);
}
void hf_private_construct(struct http_half_private * hf_private)
{
assert(hf_private->is_setup_by_user);
struct tfe_http_half * hf_public = to_hf_public(hf_private);
/* Clear the output buffer */
if (hf_private->evbuf_raw == NULL)
{
hf_private->evbuf_raw = evbuffer_new();
assert(hf_private->evbuf_raw != NULL);
}
else
{
size_t __buf_length = evbuffer_get_length(hf_private->evbuf_raw);
evbuffer_drain(hf_private->evbuf_raw, __buf_length);
}
/* Terminal the end of gzip stream */
if (hf_private->evbuf_body && hf_private->cv_compress_object)
{
hf_content_compress_write(hf_private->cv_compress_object, NULL, 0, hf_private->evbuf_body, 1);
}
if (hf_private->evbuf_body && hf_private->content_encoding != HTTP_ACCEPT_ENCODING_NONE)
{
const char * __str_content_encoding = __hf_content_encoding_to_str(hf_private->content_encoding);
const static struct http_field_name __cont_encoding_type_name = {TFE_HTTP_CONT_ENCODING, NULL};
/* Delete the origin encoding type, and add a new one */
tfe_http_field_write(hf_public, &__cont_encoding_type_name, NULL);
tfe_http_field_write(hf_public, &__cont_encoding_type_name, __str_content_encoding);
}
/* have body, write content-length and content-type */
if (hf_private->evbuf_body && !hf_private->is_setup_by_stream)
{
/* To string */
char str_sz_evbuf_body[TFE_STRING_MAX];
snprintf(str_sz_evbuf_body, sizeof(str_sz_evbuf_body) - 1, "%lu", evbuffer_get_length(hf_private->evbuf_body));
const static struct http_field_name __cont_encoding_length_name = {TFE_HTTP_CONT_LENGTH, NULL};
tfe_http_field_write(hf_public, &__cont_encoding_length_name, NULL);
tfe_http_field_write(hf_public, &__cont_encoding_length_name, str_sz_evbuf_body);
/* If origin is chunked, now delete chunked tag */
const static struct http_field_name __cont_transfer_encoding_name = {TFE_HTTP_TRANSFER_ENCODING, NULL};
tfe_http_field_write(hf_public, &__cont_transfer_encoding_name, NULL);
}
/* HTTP Request/Response first line */
if (hf_public->direction == TFE_HTTP_REQUEST) __construct_request_line(hf_private);
else __construct_response_line(hf_private);
/* Headers */
void * iterator = NULL;
struct http_field_name field_name{};
for (const char * str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name);
str_value != NULL; str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name))
{
const char * str_field = http_field_name_to_string(&field_name);
evbuffer_add_printf(hf_private->evbuf_raw, "%s: %s\r\n", str_field, str_value);
}
/* Trace Tags */
evbuffer_add_printf(hf_private->evbuf_raw, "%s: tfe/%s\r\n", "X-TG-Construct-By", tfe_version());
/* delimitor between header and body */
evbuffer_add_printf(hf_private->evbuf_raw, "\r\n");
/* add body */
if (hf_private->evbuf_body)
{
evbuffer_add_buffer(hf_private->evbuf_raw, hf_private->evbuf_body);
}
return;
}
struct http_session_private * hs_private_create(struct http_connection_private * hc_private, unsigned int thread_id,
struct http_half_private * hf_private_req, struct http_half_private * hf_private_resp)
{
struct http_session_private * __hs_private = ALLOC(struct http_session_private, 1);
__hs_private->thread_id = thread_id;
/* HS-PUBLIC */
__hs_private->hs_public.ops = &__http_session_ops;
__hs_private->hs_public.req = hf_private_req != NULL ? to_hf_public(hf_private_req) : NULL;
__hs_private->hs_public.resp = hf_private_req != NULL ? to_hf_public(hf_private_resp) : NULL;
__hs_private->hs_public.session_id = hc_private->session_id_counter++;
gettimeofday(&(__hs_private->hs_public.start_time), NULL);
/* HS-PRIVATE*/
__hs_private->hc_private = hc_private;
__hs_private->str_stream_info = tfe_strdup(hc_private->stream->str_stream_info);
return __hs_private;
}
void __write_access_log(struct http_session_private * hs_private)
{
/* Prepare to write session access log */
/* Request */
struct http_half_private * request = to_hf_request_private(hs_private);
/* Response */
struct http_half_private * response = to_hf_response_private(hs_private);
/* Req-Public */
struct tfe_http_req_spec * req_spec = request ? &to_hf_public(request)->req_spec : NULL;
/* Resp-Public */
struct tfe_http_resp_spec * resp_spec = response ? &to_hf_public(response)->resp_spec : NULL;
/* Method */
const char * __str_method = req_spec ? http_std_method_to_string(req_spec->method) : "-";
/* URL */
const char * __str_url = req_spec ? req_spec->url : "-";
/* Resp code */
char __str_resp_code[TFE_STRING_MAX];
if (resp_spec)
{
snprintf(__str_resp_code, sizeof(__str_resp_code) - 1, "%d", resp_spec->resp_code);
}
else
{
snprintf(__str_resp_code, sizeof(__str_resp_code) - 1, "%s", "-");
}
/* Content Type */
const char * __str_cont_type = resp_spec ? resp_spec->content_type != NULL ? resp_spec->content_type : "-" : "-";
/* Content Encoding */
const char * __str_cont_encoding =
resp_spec ? resp_spec->content_encoding != NULL ? resp_spec->content_encoding : "-" : "-";
/* Upgrade Tag */
const char * __str_upgrade = response ? response->is_upgrade ? "UPGRADE" : "-" : "-";
/* PASSTHROUGH */
const char * __str_req_passthrough = request ? request->is_passthrough ? "PASS-THROUGH/REQ" : "-" : "-";
const char * __str_resp_passthrough = response ? response->is_passthrough ? "PASS-THROUGH/REP" : "-" : "-";
/* USER SETUP REQUEST/RESPONSE */
const char * __str_user_req = hs_private->hf_private_req_user ? "USER/REQ" : "-";
const char * __str_user_resp = hs_private->hf_private_resp_user ? "USER/RESP" : "-";
/* SUSPEND */
const char * __str_suspend = hs_private->suspend_counter > 0 ? "SUSPEND" : "-";
char * __access_log;
asprintf(&__access_log, "%s %d %s %s HTTP/%d.%d %s %s %s %s %s %s %s %s %s", hs_private->str_stream_info,
hs_private->hs_public.session_id, __str_method, __str_url,
request->major, request->minor, __str_resp_code, __str_cont_type, __str_cont_encoding,
__str_upgrade, __str_req_passthrough, __str_resp_passthrough, __str_user_req, __str_user_resp, __str_suspend);
TFE_LOG_INFO(g_http_plugin->logger, "%s", __access_log);
free(__access_log);
}
void hs_private_destroy(struct http_session_private * hs_private)
{
struct http_half_private * hf_req = to_hf_request_private(hs_private);
struct http_half_private * hf_resp = to_hf_response_private(hs_private);
__write_access_log(hs_private);
if (hf_req != NULL)
{
hf_private_destory(hf_req);
}
if (hf_resp != NULL)
{
hf_private_destory(hf_resp);
}
if (hs_private->str_stream_info)
{
free(hs_private->str_stream_info);
}
if (hs_private->hf_private_req_user)
{
assert(hs_private->hf_private_req_user->message_status == STATUS_COMPLETE);
hf_private_destory(hs_private->hf_private_req_user);
}
if (hs_private->hf_private_resp_user)
{
assert(hs_private->hf_private_resp_user->message_status == STATUS_COMPLETE);
hf_private_destory(hs_private->hf_private_resp_user);
}
assert(hs_private->ht_frame == NULL);
free(hs_private);
}
void hs_private_gc_destroy(struct http_session_private * hs_private, struct hs_private_list * gc_list)
{
if (hs_private->release_lock > 0)
{
TAILQ_INSERT_TAIL(gc_list, hs_private, next);
hs_private->hc_private = NULL;
hs_private->in_gc_queue = true;
}
else
{
return hs_private_destroy(hs_private);
}
}
bool hs_private_can_destroy(struct http_session_private * hs_private)
{
return hs_private->release_lock <= 0;
}
void hs_private_hf_private_set(struct http_session_private * hs_private,
struct http_half_private * hf, enum tfe_http_direction direction)
{
struct tfe_http_half ** ref_old_half_public;
struct http_half_private * old_half_private;
if (direction == TFE_HTTP_REQUEST)
{
ref_old_half_public = &hs_private->hs_public.req;
old_half_private = to_hf_private(*ref_old_half_public);
}
else
{
ref_old_half_public = &hs_private->hs_public.resp;
old_half_private = to_hf_private(*ref_old_half_public);
}
if (old_half_private != NULL)
{
hf_private_destory(old_half_private);
*ref_old_half_public = to_hf_public(hf);
}
else
{
*ref_old_half_public = to_hf_public(hf);
}
return;
}
struct http_half_private * hs_private_hf_private_release(struct http_session_private * hs_private,
enum tfe_http_direction)
{
return nullptr;
}
|