summaryrefslogtreecommitdiff
path: root/plugin/protocol/http2/src/http2_stream.cpp
blob: a8a89fcc51e924d0c04e39af85af85c1f7889b85 (plain)
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
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
/*************************************************************************
	> File Name: http2_stream.c
	> Author:
	> Mail:
	> Created Time:
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <sys/param.h>
#include <zlib.h>

#include <event2/buffer.h>

#include <tfe_utils.h>
#include <tfe_stream.h>

#include <nghttp2/nghttp2.h>
#include <http2_stream.h>
#include <http2_common.h>

#include <nghttp2_session.h>
#include <nghttp2_map.h>
#include <tfe_stream.h>

/*
---------------------------------------------------------------------------------------------------------------------------------
|No errors     | PROTOCOL_ERROR| INTERNAL_ERROR   | FLOW_CONTROL_ERROR| SETTINGS_TIMEOUT | STREAM_CLOSED      | FRAME_SIZE_ERROR |
---------------------------------------------------------------------------------------------------------------------------------
|0x00          | 0x01          | 0x02             | 0x03              | 0x04             | 0x05               | 0x06             |
---------------------------------------------------------------------------------------------------------------------------------
|REFUSED_STREAM| CANCEL        | COMPRESSION_ERROR| CONNECT_ERROR     | ENHANCE_YOUR_CALM| INADEQUATE_SECURITY| HTTP_1_1_REQUIRED|
---------------------------------------------------------------------------------------------------------------------------------
|0x07          | 0x08          | 0x09             | 0x0a              | 0x0b             | 0x0c               | 0x0d             |
---------------------------------------------------------------------------------------------------------------------------------
*/

static const struct value_string method_vals[] =
{
	{HTTP_REQUEST_METHOD_DELETE,    "DELETE"},
	{HTTP_REQUEST_METHOD_GET,       "GET"},
	{HTTP_REQUEST_METHOD_HEAD, 	    "HEAD"},
	{HTTP_REQUEST_METHOD_POST,      "POST"},
	{HTTP_REQUEST_METHOD_PUT,       "PUT"},
	{HTTP_REQUEST_METHOD_CONNECT,   "CONNECT"},
	{HTTP_REQUEST_METHOD_OPTIONS,   "OPTIONS"},
	{HTTP_REQUEST_METHOD_UNKNOWN,   "unknown"},
};

typedef enum {
  NGHTTP2_USER_SEND  = 0x0b,
  NGHTTP2_USER_COLSE = 0x0c,
} http2_frame_user_type;

struct user_event_dispatch
{
	const struct tfe_stream *tf_stream;
	const struct tfe_http_session * tfe_session;
	unsigned int thread_id;
};

/*up stream */
static struct tfe_h2_session *TAILQ_LIST_FIND(struct tfe_h2_stream *h2_stream_info, int32_t stream_id)
{
	struct tfe_h2_session *stream = NULL, *_next_stream = NULL;

	TAILQ_FOREACH_SAFE(stream, &h2_stream_info->h2_session_list, next, _next_stream)
	{
		if (stream->ngh2_stream_id == stream_id){
			break;
		}
	}
	return stream;
}

static void tfe_h2_header_add_field(struct tfe_h2_header *h2_header, const struct http_field_name * field, const char * value, int at_tail)
{
	struct tfe_h2_field *peer_h2_field = ALLOC(struct tfe_h2_field, 1);
	peer_h2_field->field = http_field_name_duplicate(field);
	if (peer_h2_field->field->field_id == TFE_HTTP_UNKNOWN_FIELD)
	{
		peer_h2_field->nv.name = (uint8_t *)peer_h2_field->field->field_name;
		peer_h2_field->nv.namelen = strlen(peer_h2_field->field->field_name);
	}else
	{
		const char *std_name = http2_header_val_to_str(field->field_id, __str_std_header_field_map, __str_std_header_field_map_size);
		peer_h2_field->nv.name = (uint8_t *)tfe_strdup((const char *)std_name);
		peer_h2_field->nv.namelen = strlen(std_name);
	}
	peer_h2_field->nv.value = (uint8_t *)tfe_strdup((const char *)value);
	peer_h2_field->nv.valuelen = strlen(value);
	h2_header->nvlen++;
	if (at_tail)
		TAILQ_INSERT_TAIL(&h2_header->h2_field_list, peer_h2_field, next);
	else
		TAILQ_INSERT_HEAD(&h2_header->h2_field_list, peer_h2_field, next);
}

static nghttp2_nv* tfe_h2_header_modify_field(struct tfe_h2_header *header, nghttp2_nv *hdrs, const char *field_name, const char *filed_value)
{
	int nvlen = 0;
	struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL;

	TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field)
	{
		hdrs[nvlen].name = h2_field->nv.name;
		hdrs[nvlen].namelen = h2_field->nv.namelen;
		if (filed_value && (0==strcasecmp((const char*)h2_field->nv.name, field_name)))
		{
			hdrs[nvlen].value = (uint8_t *)filed_value;
			hdrs[nvlen].valuelen = strlen(filed_value);
		}
		else
		{
			hdrs[nvlen].value = h2_field->nv.value;
			hdrs[nvlen].valuelen = h2_field->nv.valuelen;
		}
		hdrs[nvlen].flags = h2_field->nv.flags;
		nvlen++;
	}	
	return hdrs;
}

static inline void headers_init(struct tfe_h2_header *header)
{
	header->nvlen = 0;
	header->flag  = 0;
	TAILQ_INIT(&header->h2_field_list);
}

const char * method_idx_to_str(int encode)
{
	switch (encode)
	{
		case HTTP2_CONTENT_ENCODING_GZIP: return "gzip";
		case HTTP2_CONTENT_ENCODING_X_GZIP: return "x-gzip";
		case HTTP2_CONTENT_ENCODING_DEFLATE: return "deflate";
		case HTTP2_CONTENT_ENCODING_BZIP2: return "bzip2";
		case HTTP2_CONTENT_ENCODING_X_BZIP2: return "x-bzip2";
		case HTTP2_CONTENT_ENCODING_BR: return "br";
		case HTTP2_CONTENT_ENCODING_ZSTD: return "zstd";
		default: return "";
	}
}

static int method_to_str_idx(const char * method)
{
	if (strcasestr(method, "gzip") != NULL)
		return HTTP2_CONTENT_ENCODING_GZIP;

	if (strcasestr(method, "x-gzip") != NULL)
		return HTTP2_CONTENT_ENCODING_X_GZIP;

	if (strcasestr(method, "deflate") != NULL)
		return HTTP2_CONTENT_ENCODING_DEFLATE;

	if (strcasestr(method, "bzip2") != NULL)
		return HTTP2_CONTENT_ENCODING_BZIP2;

	if (strcasestr(method, "x-bzip2") != NULL)
		return HTTP2_CONTENT_ENCODING_X_BZIP2;

	if (strcasestr(method, "br") != NULL)
		return HTTP2_CONTENT_ENCODING_BR;

	if (strcasestr(method, "zstd") != NULL)
		return HTTP2_CONTENT_ENCODING_ZSTD;

	return HTTP2_CONTENT_ENCODING_NONE;
}

static nghttp2_nv* tfe_h2_header_convert_nv(struct tfe_h2_header *header, nghttp2_nv *hdrs)
{
	int nvlen = 0;
	struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL;

	TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field)
	{
		hdrs[nvlen].name = h2_field->nv.name;
		hdrs[nvlen].namelen = h2_field->nv.namelen;
		hdrs[nvlen].value = h2_field->nv.value;
		hdrs[nvlen].valuelen = h2_field->nv.valuelen;
		hdrs[nvlen].flags = h2_field->nv.flags;
		nvlen++;
	}
	return hdrs;
}

static enum tfe_http_std_method http2_get_method(struct tfe_h2_half_private *half_private)
{
	struct tfe_http_req_spec *req_spec = &(half_private->half_public.req_spec);

	return req_spec->method;
}

static nghttp2_session * tfe_h2_stream_get_http2_session(struct tfe_h2_stream *connection, enum tfe_conn_dir dir)
{
	return (dir==CONN_DIR_UPSTREAM?connection->http2_server_handle:connection->http2_client_handle);
}

static nghttp2_session * tfe_h2_stream_get_http2_peer_session(struct tfe_h2_stream *connection, enum tfe_conn_dir dir)
{
	return (dir==CONN_DIR_UPSTREAM?connection->http2_client_handle: connection->http2_server_handle);
}

static struct tfe_h2_half_private *tfe_h2_stream_get_half(struct tfe_h2_session *h2_session, enum tfe_conn_dir dir)
{
	return (dir==CONN_DIR_UPSTREAM?h2_session->resp: h2_session->req);
}

static nghttp2_settings_entry* http2_iv_packet(nghttp2_settings settings, nghttp2_settings_entry *out_iv)
{
	int i = 0;

	nghttp2_settings_entry *iv = settings.iv;

	for (i = 0; i < (int)settings.niv; i++){
		out_iv[i].settings_id = iv[i].settings_id;
		out_iv[i].value = iv[i].value;
	}
	return out_iv;
}

static void delete_nv_packet_data(struct tfe_h2_header *header)
{
	struct tfe_h2_field *h2_filed=NULL, *peer_h2_filed=NULL;

	TAILQ_FOREACH_SAFE(h2_filed, &header->h2_field_list, next, peer_h2_filed)
	{
		TAILQ_REMOVE(&header->h2_field_list, h2_filed, next);

		free(h2_filed->nv.name);
		h2_filed->nv.name = NULL;
		h2_filed->nv.namelen = 0;

		free(h2_filed->nv.value);
		h2_filed->nv.value = NULL;
		h2_filed->nv.valuelen = 0;

		free(h2_filed->field);
		h2_filed->field = NULL;

		free(h2_filed);
		h2_filed = NULL;
	}
	header->nvlen = 0;
	header->flag  = 0;
}

static int event_dispatch_cb(struct tfe_h2_half_private * half_private,	enum tfe_http_event ev, const unsigned char * data, size_t len, void * user)
{
	struct user_event_dispatch *event = (struct user_event_dispatch *)user;
	struct http_frame_session_ctx *frame_ctx = half_private->frame_ctx;

	http_frame_raise_event(frame_ctx, event->tf_stream, (struct tfe_http_session *)event->tfe_session,
		ev, data, len, event->thread_id);

	return 0;
}

void half_set_callback(struct tfe_h2_half_private * half_private, void * user, void (* user_deleter)(void *))
{
	half_private->event_cb = event_dispatch_cb;
	half_private->event_cb_user = user;
	half_private->event_cb_user_deleter = user_deleter;
}

const char * h2_half_ops_field_read(const struct tfe_http_half * half, const struct http_field_name * field)
{
	const struct tfe_h2_half_private *half_private = nghttp2_to_half_private(half);

	if (unlikely(half_private == NULL))
			return NULL;

	struct tfe_h2_field *h2_field=NULL, *peer_h2_field=NULL;
	const struct tfe_h2_header *h2_header =&(half_private->header);

	TAILQ_FOREACH(h2_field, &h2_header->h2_field_list, next)
	{
		if (http_field_name_compare(h2_field->field, field) != 0) continue;
			peer_h2_field = h2_field;
		break;
	}

	return peer_h2_field != NULL ? (const char *)peer_h2_field->nv.value : NULL;
}

int h2_half_ops_field_write(struct tfe_http_half * half, const struct http_field_name * field, const char * value)
{
	struct tfe_h2_half_private *half_private = nghttp2_to_half_private(half);
	struct tfe_h2_header *h2_header = &(half_private->header);
	struct tfe_h2_field *h2_field=NULL,*peer_h2_field=NULL;

	if (value != NULL)
	{
		tfe_h2_header_add_field(h2_header, field, value, 1);
	}
	else
	{
		bool delete_success = false;

		TAILQ_FOREACH_SAFE(h2_field, &h2_header->h2_field_list, next, peer_h2_field)
		{
			if (http_field_name_compare(h2_field->field, field) != 0)
				continue;

			TAILQ_REMOVE(&h2_header->h2_field_list, h2_field, next);
            free(h2_field->field);
            free(h2_field->nv.name);
			free(h2_field->nv.value);
			free(h2_field);
			h2_header->nvlen--;
			delete_success = true;
		}

		return delete_success ? 0 : -ENOENT;
	}
	return 0;
}

static struct tfe_http_half *h2_half_ops_allow_write(const struct tfe_http_half * half)
{
	return (struct tfe_http_half *) half;
}

const char * h2_half_ops_field_iterate(const struct tfe_http_half * half, void ** iter, struct http_field_name * field)
{
	struct tfe_h2_field **h2_filed = (struct tfe_h2_field **)iter;
	const struct tfe_h2_half_private *half_private = nghttp2_to_half_private(half);
    const struct tfe_h2_header *header = &half_private->header;

	if (*h2_filed == NULL)
	{
		*h2_filed = TAILQ_FIRST(&header->h2_field_list);
	}
	else
	{
		*h2_filed = TAILQ_NEXT(*h2_filed, next);
	}

	if (*h2_filed == NULL) return NULL;

	/* Reference of inner data, user should copy it */
	field->field_id = (*h2_filed)->field->field_id;
	field->field_name = (*h2_filed)->field->field_name;
	return (const char *)(*h2_filed)->nv.value;
}

static int h2_half_ops_append_body(struct tfe_http_half * half, char * buff, size_t size, int flag)
{
	int xret = -1;
	struct tfe_h2_half_private * resp = nghttp2_to_half_private(half);
	struct tfe_h2_payload *body = &resp->h2_payload;

	if (buff == NULL || size == 0){
		if (body->encode_type != HTTP2_CONTENT_ENCODING_NONE){
			xret = http2_compress_stream(&body->deflate, NULL, 0, resp->h2_payload.evbuf_body, body->encode_type, 1);
		}
		resp->message_state = H2_READ_STATE_COMPLETE;
		goto finish;
	}

	if (resp->h2_payload.evbuf_body == NULL){
		resp->h2_payload.evbuf_body = evbuffer_new();
	}

	if (body->encode_type != HTTP2_CONTENT_ENCODING_NONE){
		xret = http2_compress_stream(&body->deflate, (const uint8_t *)buff, size,
								resp->h2_payload.evbuf_body, body->encode_type, 0);
	}else{
		xret = evbuffer_add(resp->h2_payload.evbuf_body, buff, size);
	}

finish:
	return xret;
}

static void delete_http_req_spec(struct tfe_http_req_spec *req_spec)
{
	if (req_spec->uri)
		free((char *)req_spec->uri);
	if (req_spec->host)
		free((char *)req_spec->host);
	if (req_spec->url)
		free((char *)req_spec->url);
}

static void delete_http_resp_spec(struct tfe_http_resp_spec *resp_spec)
{
	if (resp_spec->content_encoding)
		free((char *)resp_spec->content_encoding);
	if (resp_spec->content_type)
		free((char *)resp_spec->content_type);
	if (resp_spec->content_length)
		free((char *)resp_spec->content_length);
}

void delete_stream_half_data(struct tfe_h2_half_private **data,
							int body_flag, enum tfe_conn_dir dir)
{
	if (*data){

		struct tfe_h2_payload *body = &((*data)->h2_payload);

		http2_compress_finished(&body->inflate);
		http2_decompress_finished(&body->deflate);
		if (body->evbuf_body && body_flag){
			evbuffer_free(body->evbuf_body);
			body->evbuf_body = NULL;
		}
		if ((*data)->url_storage)
			FREE(&((*data)->url_storage));
		delete_nv_packet_data(&((*data)->header));

		if (dir == CONN_DIR_DOWNSTREAM)
		{
			struct tfe_http_req_spec *req_spec = &((*data)->half_public.req_spec);
			delete_http_req_spec(req_spec);
		}
		if (dir == CONN_DIR_UPSTREAM)
		{
			struct tfe_http_resp_spec *resp_spec = &((*data)->half_public.resp_spec);
			delete_http_resp_spec(resp_spec);
		}

		if((*data)->event_cb_user_deleter != NULL)
			(*data)->event_cb_user_deleter((*data)->event_cb_user);
		free(*data);
		*data = NULL;
	}
	return;
}

void h2_half_ops_free(struct tfe_http_half * half)
{
	struct tfe_h2_half_private * h2_private = nghttp2_to_half_private(half);

	delete_stream_half_data(&h2_private, 1, CONN_DIR_DOWNSTREAM);

	return;
}

int h2_half_ops_body_begin(struct tfe_http_half * half, int by_stream)
{
	struct tfe_h2_half_private * resp = nghttp2_to_half_private(half);
	struct tfe_h2_payload *body = &resp->h2_payload;

	assert(body->evbuf_body == NULL);

	if (by_stream)
	{
		if (body->inflate){
			http2_compress_finished(&body->inflate);
		}
		if (body->deflate){
			http2_decompress_finished(&body->deflate);
		}

		body->encode_type = HTTP2_CONTENT_ENCODING_NONE;
		resp->message_state = H2_READ_STATE_READING;
		resp->by_stream = by_stream;
	}

	body->evbuf_body = evbuffer_new();
	return 0;
}

uint32_t tfe_h2_half_get_ngh2_stream_id(struct tfe_h2_half_private *h2_half)
{
	struct tfe_h2_session* father_session=h2_half->father_session;
	return father_session->ngh2_stream_id;
}

int h2_half_ops_body_data(struct tfe_http_half * h2_response, const unsigned char * data, size_t sz_data)
{
	int xret = -1;
	struct tfe_h2_half_private * h2_resp_priv = nghttp2_to_half_private(h2_response);
	struct tfe_h2_payload *body = &h2_resp_priv->h2_payload;

	if (body->encode_type != HTTP2_CONTENT_ENCODING_NONE){

		xret = http2_compress_stream(&body->deflate, (const uint8_t *)data, sz_data,
							 h2_resp_priv->h2_payload.evbuf_body, body->encode_type, 0);
	}else{
		xret = evbuffer_add(h2_resp_priv->h2_payload.evbuf_body, data, sz_data);
	}
	return xret;
}

int h2_half_ops_body_end(struct tfe_http_half * half)
{
	struct tfe_h2_half_private * resp = nghttp2_to_half_private(half);

	resp->body_state =  H2_READ_STATE_COMPLETE;
	resp->message_state = H2_READ_STATE_COMPLETE;

	return 0;
}

struct tfe_http_half_ops h2_half_ops =
{
	.ops_http_field_read	= h2_half_ops_field_read,
	.ops_http_field_write	= h2_half_ops_field_write,
	.ops_http_allow_write 	= h2_half_ops_allow_write,
	.ops_http_field_iterate	= h2_half_ops_field_iterate,
	.ops_append_body		= h2_half_ops_append_body,
	.ops_body_begin			= h2_half_ops_body_begin,
	.ops_body_data 			= h2_half_ops_body_data,
	.ops_body_end			= h2_half_ops_body_end,
	.ops_free				= h2_half_ops_free
};

static struct tfe_http_session* h2_ops_allow_write(const struct tfe_http_session * session)
{
	struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session);

	if ( http_frame_currect_plugin_preempt(stream_data->frame_ctx) == 0){
		return (struct tfe_http_session *)session;
	}

	return NULL;
}

void h2_ops_detach(const struct tfe_http_session * session)
{
	struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session);

	return http_frame_currect_plugin_detach(stream_data->frame_ctx);
}

void h2_ops_drop(struct tfe_http_session * session)
{
	return;
}

void h2_ops_suspend(struct tfe_http_session * session)
{
#ifdef TFE_CACHE
	struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session);
	stream_data->cache.spd_set = 1;
#endif
}

void h2_ops_resume(struct tfe_http_session * session)
{
#ifdef TFE_CACHE
	struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session);
	if (stream_data->cache.spd_valid){

		tfe_stream_resume(stream_data->tf_stream);
		stream_data->cache.rse_set = 1;
	}
#endif
}

void h2_ops_request_set(struct tfe_http_session * session, struct tfe_http_half * req_user)
{
	struct tfe_h2_session *stream_data = nghttp2_to_stream_data(session);
	struct tfe_h2_half_private *half_user = nghttp2_to_half_private(req_user);
	
	half_user->header.flag = stream_data->req->header.flag;

	stream_data->plugin_built_req = half_user;
}

void h2_ops_response_set(struct tfe_http_session * session, struct tfe_http_half * resp)
{
	struct tfe_h2_session *stream_data = nghttp2_to_stream_data(session);
	struct tfe_h2_half_private *half_user = nghttp2_to_half_private(resp);

	stream_data->plugin_built_resp = half_user;
}

static struct tfe_h2_half_private* tfe_half_private_init(enum tfe_http_direction direction, int32_t stream_id, nghttp2_session *session)
{
	struct tfe_h2_half_private *half_private = ALLOC(struct tfe_h2_half_private, 1);
	assert(half_private);

	half_private->half_public.direction = direction;
	half_private->half_public.ops = &h2_half_ops;

	headers_init(&half_private->header);
	headers_init(&half_private->promised);
	half_private->h2_payload.inflate = NULL;
	half_private->h2_payload.deflate = NULL;
	half_private->h2_payload.evbuf_body = evbuffer_new();
	half_private->h2_payload.encode_type = HTTP2_CONTENT_ENCODING_NONE;
	half_private->h2_payload.padlen = 0;

	half_private->stream_id = stream_id;
	half_private->session   = session;

	half_private->body_state 	 = H2_READ_STATE_BEGIN;
	half_private->message_state = H2_READ_STATE_BEGIN;

	return half_private;
}

struct tfe_http_half * h2_ops_request_create(struct tfe_http_session * session, enum tfe_http_std_method method, const char * uri)
{
	struct tfe_h2_half_private * req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL);

	req->method_or_status = method;
	req->url_storage = tfe_strdup(uri);

	return &req->half_public;
}

struct tfe_http_half * h2_ops_response_create(struct tfe_http_session * session, int resp_code)
{
	struct tfe_h2_session *stream = nghttp2_to_stream_data(session);
	struct tfe_h2_half_private * resp = tfe_half_private_init(TFE_HTTP_RESPONSE, stream->ngh2_stream_id,
															 stream->session);
	resp->method_or_status = resp_code;

	if (stream->resp)
		resp->h2_payload.encode_type = stream->resp->h2_payload.encode_type;

	return &resp->half_public;
}

void h2_ops_kill(struct tfe_http_session * session)
{
	struct tfe_h2_session *h2_session = nghttp2_to_stream_data(session);
	struct tfe_h2_stream *h2_stream = h2_session->father_stream;
	h2_stream->kill_signal = 1;
}

struct tfe_http_session_ops http2_session_ops =
{
	.ops_allow_write      = h2_ops_allow_write,
	.ops_detach           = h2_ops_detach,
	.ops_drop             = h2_ops_drop,
	.ops_suspend          = h2_ops_suspend,
	.ops_resume           = h2_ops_resume,
	.ops_kill             = h2_ops_kill,
	.ops_request_set      = h2_ops_request_set,
	.ops_response_set     = h2_ops_response_set,
	.ops_request_create   = h2_ops_request_create,
	.ops_response_create  = h2_ops_response_create
};

static ssize_t no_data_read_callback(nghttp2_session *session, int32_t stream_id, uint8_t *buf, size_t length,
                      uint32_t *data_flags, nghttp2_data_source *source, void *user_data)
{
	(void)session;
	(void)stream_id;
	(void)buf;
	(void)length;
	(void)source;
	(void)user_data;

	*data_flags |= NGHTTP2_DATA_FLAG_EOF;
	return 0;
}

static ssize_t upstream_read_callback(nghttp2_session *session, int32_t stream_id, uint8_t *buf, size_t length,
                       uint32_t *data_flags, nghttp2_data_source *source,void *user_data)
{
	unsigned char *input = NULL;
	ssize_t datalen = 0, inputlen=0;	
	
	struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;
	struct tfe_h2_session *h2_session = TAILQ_LIST_FIND(h2_stream_info, stream_id);
	if (h2_session == NULL)
	{
		TFE_LOG_ERROR(logger()->handle, "Failed to send data, stream_id %d", stream_id);
		*data_flags |= NGHTTP2_DATA_FLAG_EOF;
		return 0;
	}
	struct tfe_h2_payload *to_send_body = (struct tfe_h2_payload *)source->ptr;
	if (!to_send_body->evbuf_body || 0==(inputlen = evbuffer_get_length(to_send_body->evbuf_body))
		||!(input = evbuffer_pullup(to_send_body->evbuf_body, MIN(length, inputlen))))
	{
		if ((to_send_body->flags & NGHTTP2_FLAG_END_STREAM) == 0)
		{
			*data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
		}
		*data_flags |= NGHTTP2_DATA_FLAG_EOF;
		return 0;
	}
	datalen=MIN(length, inputlen);
    memcpy(buf, input, datalen);

	evbuffer_drain(to_send_body->evbuf_body, datalen);
    return datalen;
}

static int http_session_update_window_size(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session, int32_t buffer_length)
{
	nghttp2_stream *stream = nghttp2_session_find_stream(h2_stream_info->http2_server_handle, h2_session->ngh2_stream_id);
	if(stream == NULL)
	{
		return 0;
	}

	if(stream->remote_window_size < buffer_length)
	{
		stream->remote_window_size += ((buffer_length - stream->remote_window_size) + 9);
	}
	int32_t remote_window_size = nghttp2_session_get_remote_window_size(h2_stream_info->http2_server_handle);
	if(remote_window_size < buffer_length)
	{
		h2_stream_info->http2_server_handle->remote_window_size += ((buffer_length - remote_window_size) + 9);
	}
	return 1;
}

static void http2_client_submit_settings(nghttp2_session *http2_client_handle, nghttp2_session *http2_server_handle)
{
	/*Check the current setting frame state is NGHTTP2_IB_READ_FIRST_SETTINGS**/
	if (http2_client_handle->iframe.state != NGHTTP2_IB_READ_FIRST_SETTINGS)
	{
		return;
	}
	nghttp2_settings_entry iv[2] = {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100},{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 65535}};
	int xret = nghttp2_submit_settings(http2_server_handle, NGHTTP2_FLAG_NONE, iv, 2);
	if (xret != 0)
	{
		TFE_LOG_ERROR(logger()->handle, "Submit settings error: %s\n", nghttp2_strerror(xret));
	}
	xret = nghttp2_session_send(http2_server_handle);
	if (xret != 0) {
		TFE_LOG_ERROR(logger()->handle, "Fatal send error: %s\n", nghttp2_strerror(xret));
	}
	return;
}

static enum tfe_stream_action http2_frame_submit_built_resp(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session)
{
    int rv = -1;
    struct tfe_h2_header *h2_header = NULL;
	struct tfe_h2_half_private *pangu_resp = NULL, *resp = NULL;
	
	resp = h2_session->resp;
	pangu_resp = h2_session->plugin_built_resp;

	if (pangu_resp->message_state != H2_READ_STATE_COMPLETE && (evbuffer_get_length(resp->h2_payload.evbuf_body) > 0))
	{
		return (enum tfe_stream_action)ACTION_USER_DATA;
	}
	h2_header = &pangu_resp->header;
	if (h2_header->nvlen <= 0)
		return ACTION_FORWARD_DATA;

	char value[128] = {0}; struct http_field_name field;
	field.field_id = TFE_HTTP_UNKNOWN_FIELD;
	field.field_name = ":status";
	snprintf(value, sizeof(value), "%d", pangu_resp->method_or_status);
	tfe_http_nonstd_field_write(&pangu_resp->half_public, ":status", NULL);
	tfe_h2_header_add_field(h2_header, &field, (const char *)value, 0);
	pangu_resp->message_state = H2_READ_STATE_COMPLETE;

	struct tfe_h2_payload *body = &pangu_resp->h2_payload;
	body->flags |= NGHTTP2_FLAG_END_STREAM;
	char str_sz_evbuf_body[TFE_STRING_MAX];
	snprintf(str_sz_evbuf_body, sizeof(str_sz_evbuf_body) - 1, "%lu", evbuffer_get_length(body->evbuf_body));

	const static struct http_field_name cont_field = {TFE_HTTP_CONT_LENGTH, NULL};
	tfe_http_field_write(&pangu_resp->half_public, &cont_field, NULL);
	tfe_http_field_write(&pangu_resp->half_public, &cont_field, str_sz_evbuf_body);

	if (body->encode_type != HTTP2_CONTENT_ENCODING_NONE)
	{
		const static struct http_field_name encoding_field = {TFE_HTTP_CONT_ENCODING, NULL};
		const char *content_encoding = method_idx_to_str(body->encode_type);
		tfe_http_field_write(&pangu_resp->half_public, &encoding_field, NULL);
		tfe_http_field_write(&pangu_resp->half_public, &encoding_field, content_encoding);
	}
	http_session_update_window_size(h2_stream_info, h2_session, evbuffer_get_length(body->evbuf_body));
	http2_client_submit_settings(h2_stream_info->http2_client_handle,h2_stream_info->http2_server_handle);

	nghttp2_data_provider data_prd;
	data_prd.source.ptr    = (void *)body;
	data_prd.read_callback = upstream_read_callback;

	nghttp2_nv hdrs[h2_header->nvlen];
	/*Adapt Http uri Settings**/

	rv = nghttp2_submit_response(h2_stream_info->http2_server_handle, h2_session->ngh2_stream_id, tfe_h2_header_convert_nv(h2_header, hdrs),
                               h2_header->nvlen, &data_prd);
	if (rv != 0){
		return ACTION_FORWARD_DATA;
	}

	return ACTION_DROP_DATA;
}

static enum tfe_stream_action http2_frame_submit_built_req(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session)
{
	int32_t stream_id    = -1;
    struct tfe_h2_header *h2_header = NULL;
	struct tfe_h2_half_private *plugin_built_req = h2_session->plugin_built_req;

	if (plugin_built_req->message_state != H2_READ_STATE_COMPLETE){
		return (enum tfe_stream_action)ACTION_USER_DATA;
	}
	h2_header = &plugin_built_req->header;
	if (h2_header->nvlen <= 0)
		return ACTION_FORWARD_DATA;

	struct tfe_h2_payload *body = &plugin_built_req->h2_payload;
	body->flags = NGHTTP2_FLAG_END_STREAM;
	char str_sz_evbuf_body[TFE_STRING_MAX];
	snprintf(str_sz_evbuf_body, sizeof(str_sz_evbuf_body) - 1, "%lu", evbuffer_get_length(body->evbuf_body));

	const static struct http_field_name encoding_field = {TFE_HTTP_CONT_LENGTH, NULL};	
	tfe_http_field_write(&plugin_built_req->half_public, &encoding_field, NULL);
	tfe_http_field_write(&plugin_built_req->half_public, &encoding_field, str_sz_evbuf_body);

	nghttp2_data_provider data_prd;
	data_prd.source.ptr    = (void *)body;
	data_prd.read_callback = upstream_read_callback;

	nghttp2_nv hdrs[h2_header->nvlen];
	/*Adapt Http uri Settings**/
	nghttp2_session_set_next_stream_id(h2_stream_info->http2_client_handle, h2_session->ngh2_stream_id);
	stream_id = nghttp2_submit_request(h2_stream_info->http2_client_handle, NULL,
								  	   tfe_h2_header_modify_field(h2_header, hdrs, ":path", plugin_built_req->url_storage),
								   	   h2_header->nvlen, &data_prd, h2_session);

	if (stream_id < 0){
		TFE_LOG_ERROR(logger()->handle, "Could not submit request: %s",
					  nghttp2_strerror(stream_id));
        return ACTION_FORWARD_DATA;
    }

	return ACTION_DROP_DATA;
}

static void http2_submit_end_data_by_h2_half(struct tfe_h2_stream *h2_stream_info, int32_t stream_id, enum tfe_conn_dir dir)
{
	int xret = -1;
	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(h2_stream_info, dir);

	nghttp2_data_provider data_provider;
	data_provider.read_callback = no_data_read_callback;

	xret = nghttp2_submit_data(ngh2_session, NGHTTP2_FLAG_END_STREAM,
	                           stream_id, &data_provider);
	if (xret != 0){
		return;
	}
	xret = nghttp2_session_send(ngh2_session);
	if (xret != 0) {
		TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret));
	}

	return;
}

static enum tfe_stream_action http2_submit_data_by_h2_half(struct tfe_h2_stream *connection, struct tfe_h2_session *h2_session, enum tfe_conn_dir dir, int compress_result)
{
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;
	struct tfe_h2_half_private *h2_half = tfe_h2_stream_get_half(h2_session, dir);

	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

	if (h2_session->plugin_built_resp && compress_result)
	{
		stream_action = http2_frame_submit_built_resp(connection, h2_session);
	}
	else if (h2_session->plugin_built_req && compress_result)
	{
		stream_action = http2_frame_submit_built_req(connection, h2_session);
	}
	else
	{
		int rv = -1;
		struct tfe_h2_payload *body = &h2_half->h2_payload;

		nghttp2_data_provider upstream_data_provider;
		upstream_data_provider.source.ptr    = (void *)body;
		upstream_data_provider.read_callback = upstream_read_callback;

		int remote_window_size = nghttp2_session_get_stream_remote_window_size(ngh2_session, h2_session->ngh2_stream_id);
		if(remote_window_size == 0)
		{
			stream_action = ACTION_DROP_DATA;
			return stream_action;
		}
		rv = nghttp2_submit_data(ngh2_session, body->flags, h2_session->ngh2_stream_id, &upstream_data_provider);
		if (rv != 0)
		{
			stream_action = ACTION_FORWARD_DATA;
		}
	}
	return stream_action;
}

typedef int (*http2_frame_callback) (struct tfe_h2_stream *, const nghttp2_frame *, enum tfe_conn_dir dir);

typedef int (*http2_callback)	(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name, size_t namelen, const uint8_t *value,
                 		 		 size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir);

static int http2_submit_frame_priority(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
	int xret = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	const nghttp2_priority *priority = &frame->priority;

	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

	int rv = nghttp2_submit_priority(ngh2_session, priority->hd.flags, priority->hd.stream_id,
									 &(priority->pri_spec));
	if (rv != 0){
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit priority error: %s\n",
										 			 dir, nghttp2_strerror(rv));
		return 0;
	}
	xret = nghttp2_session_send(ngh2_session);
	if (xret != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n",
													  dir, nghttp2_strerror(xret));
	}
	//ngh2_session->last_sent_stream_id = MAX(ngh2_session->last_sent_stream_id, frame->hd.stream_id);
	connection->stream_action = stream_action;
	return 0;
}

static int http2_submit_frame_rst_stream(struct tfe_h2_stream *connection,const nghttp2_frame *frame,	enum tfe_conn_dir dir)
{
	int xret = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	const nghttp2_rst_stream *rst_stream = &frame->rst_stream;

	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

	int rv = nghttp2_submit_rst_stream(ngh2_session, rst_stream->hd.flags,
									   rst_stream->hd.stream_id, rst_stream->error_code);
	if (rv != 0){
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit rst error: %s\n",
										 			 dir, nghttp2_strerror(rv));
		return 0;
	}
	xret = nghttp2_session_send(ngh2_session);
    if (xret != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n",
													  dir, nghttp2_strerror(xret));
	}
	connection->stream_action = stream_action;
	return 0;
}

static int http2_submit_frame_settings(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
	int xret = -1, rv = -1;
	nghttp2_settings_entry iv[6] = {0};
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	nghttp2_settings settings   = frame->settings;

	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

    rv = nghttp2_submit_settings(ngh2_session, settings.hd.flags, http2_iv_packet(settings, iv), settings.niv);
    if (rv != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit settings error: %s\n", dir, nghttp2_strerror(rv));
		return 0;
    }
	xret = nghttp2_session_send(ngh2_session);
	if (xret != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret));
	}
	connection->stream_action = stream_action;
	#ifdef TFE_LOG_HTTP2
	TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit setting, stream_id:%d, action:%d", connection->tf_stream->str_stream_info,
				  dir, frame->hd.stream_id, connection->stream_action);
	#endif
	return 0;
}

static int http2_submit_frame_ping(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
	int xret = -1 ,rv = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	const nghttp2_ping *ping = &frame->ping;

	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

	rv = nghttp2_submit_ping(ngh2_session, ping->hd.flags, ping->opaque_data);
	if (rv != 0)
	{
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit ping error: %s\n", dir, nghttp2_strerror(rv));
		return 0;
	}
	xret = nghttp2_session_send(ngh2_session);
	if (xret != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret));
	}
	connection->stream_action = stream_action;
	return 0;
}

void http2_write_access_log(struct tfe_h2_session *h2_session, const char * str_stream_info)
{
	/* Request */
	struct tfe_h2_half_private *req = h2_session->req;
	/* Response */
	struct tfe_h2_half_private *resp = h2_session->resp;
	/* Req-Public */
	struct tfe_http_req_spec *req_spec = req ? &(req->half_public.req_spec) : NULL;
	/* Resp-Public */
	struct tfe_http_resp_spec *resp_spec = resp ? &(resp->half_public.resp_spec) : NULL;

	const char * method = req_spec ? val_to_str(req_spec->method, method_vals) : "-";
	const char * url = req_spec ? req_spec->url : "-";
	char resp_code[TFE_STRING_MAX];
	if (resp_spec)
		snprintf(resp_code, sizeof(resp_code) - 1, "%d", resp_spec->resp_code);
	else
		snprintf(resp_code, sizeof(resp_code) - 1, "%s", "-");

	const char * cont_type = resp_spec ? resp_spec->content_type != NULL ? resp_spec->content_type : "-" : "-";
	const char * cont_encoding =
		resp_spec ? resp_spec->content_encoding != NULL ? resp_spec->content_encoding : "-" : "-";
	const char * pangu_req = h2_session->plugin_built_req ? "USER/REQ" : "-";
	const char * pangu_resp = h2_session->plugin_built_resp ? "USER/RESP" : "-";
	//const char * __str_suspend = h2_session->suspend_counter > 0 ? "SUSPEND" : "-";

	char *access_log;
	asprintf(&access_log, "%s %d %s %s HTTP2.0 %s %s %s %s %s", str_stream_info, h2_session->tfe_session.session_id,
		     method, url, resp_code, cont_type, cont_encoding, pangu_req, pangu_resp);

	TFE_LOG_INFO(logger()->handle, "%s", access_log);
	free(access_log);
}

void delete_http2_stream_data(struct tfe_h2_session *h2_session, const struct tfe_stream *tf_stream, int body_flag)
{
	delete_stream_half_data(&h2_session->req, body_flag, CONN_DIR_DOWNSTREAM);

	delete_stream_half_data(&h2_session->resp, body_flag, CONN_DIR_UPSTREAM);

	delete_stream_half_data(&h2_session->plugin_built_req, body_flag, CONN_DIR_DOWNSTREAM);

	delete_stream_half_data(&h2_session->plugin_built_resp, body_flag, CONN_DIR_UPSTREAM);
}

void http2_disect_goaway(struct tfe_h2_stream *h2_stream_info)
{
	unsigned int thread_id           = h2_stream_info->thread_id;
	const struct tfe_stream * stream = h2_stream_info->tf_stream;

	struct tfe_h2_session *h2_session  = NULL;
	struct tfe_h2_session *peer_h2_stream = NULL;

	TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream){
		TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next);
		if (h2_session->frame_ctx){
			http_frame_raise_session_end(h2_session->frame_ctx, stream, &h2_session->tfe_session,
										 thread_id);
			h2_session->frame_ctx = NULL;
		}
		delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1);
		free(h2_session);
		h2_session = NULL;
	}
	if (h2_stream_info->http2_client_handle){
		nghttp2_session_del(h2_stream_info->http2_client_handle);
		h2_stream_info->http2_client_handle = NULL;
	}
	if (h2_stream_info->http2_server_handle){
		nghttp2_session_del(h2_stream_info->http2_server_handle);
		h2_stream_info->http2_server_handle = NULL;
	}
}

static int http2_submit_frame_goaway(struct tfe_h2_stream *connection, const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
	int xret = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	const nghttp2_goaway *goaway = &frame->goaway;
	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

	int rv = nghttp2_submit_goaway(ngh2_session, goaway->hd.flags, goaway->last_stream_id,
							       goaway->error_code, goaway->opaque_data, goaway->opaque_data_len);
	if (rv != 0){
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit goaway error: %s\n",
										 			  dir, nghttp2_strerror(rv));
		goto finish;
	}
	xret = nghttp2_session_send(ngh2_session);
	if (xret != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n",
													  dir, nghttp2_strerror(xret));
	}
finish:
	TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit goaway, stream_id:%d, action:%d, errod_code:%d, data:%.*s", connection->tf_stream->str_stream_info,
				  dir, goaway->last_stream_id, connection->stream_action, goaway->error_code, (int)goaway->opaque_data_len, goaway->opaque_data);
	connection->goaway = 1;
	connection->stream_action = stream_action;
	return 0;
}

/* If the data sent by the client is deferred, After receive the WINDOW_UPDATE frame, it resumes and needs to resend the data that you deferred last time**/
static int nghttp2_send_resumes_data(struct tfe_h2_stream *connection, int32_t stream_id, enum tfe_conn_dir dir)
{
	int xret = -1;
	nghttp2_session *session = tfe_h2_stream_get_http2_peer_session(connection, dir);
	nghttp2_stream *stream = nghttp2_session_find_stream(session, stream_id);
	if(stream->item)
	{
		xret = nghttp2_session_send(session);
		if (xret != 0)
		{
			TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret));
		}
	}
	return 0;
}

static int http2_submit_frame_window_update(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
	int xret = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	const nghttp2_window_update *window_update = &(frame->window_update);
	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

	nghttp2_send_resumes_data(connection, window_update->hd.stream_id, dir);

	int rv = nghttp2_submit_window_update(ngh2_session, window_update->hd.flags,window_update->hd.stream_id,
								 	      window_update->window_size_increment);
	if (rv != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit window error: %s\n",
										  dir, nghttp2_strerror(rv));
		return 0;
	}
	xret = nghttp2_session_send(ngh2_session);
	if (xret != 0) {
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret));
	}
	connection->stream_action = stream_action;
	return 0;
}

static int http2_submit_header_by_not_modify(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session)
{
	int xret = -1;
	int32_t stream_id = 0;
	struct tfe_h2_header headers;
	struct tfe_h2_half_private *resp = h2_session->resp;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	headers = resp->header;
	if (headers.nvlen <= 0 || ((headers.flag & NGHTTP2_FLAG_END_STREAM)!=1) ){
		return 0;
	}

	nghttp2_nv hdrs[headers.nvlen];
    stream_id = nghttp2_submit_headers(h2_stream_info->http2_server_handle, headers.flag,
                                	 h2_session->ngh2_stream_id, NULL, tfe_h2_header_convert_nv(&headers, hdrs),
                                     headers.nvlen, h2_session);
    if (stream_id < 0){
        printf("Fatal headers error: %s\n", nghttp2_strerror(stream_id));
		stream_action = ACTION_FORWARD_DATA;
    }

	if (stream_action == ACTION_DROP_DATA){
		xret = nghttp2_session_send(h2_stream_info->http2_server_handle);
		if (xret != 0) {
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s, %d\n",nghttp2_strerror(xret), __LINE__);
		}
	}
	h2_stream_info->stream_action = stream_action;
	return 0;
}

static int http2_submit_complete_data(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session, enum tfe_conn_dir dir)
{
	int xret = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	struct tfe_h2_half_private *h2_half = tfe_h2_stream_get_half(h2_session, dir);
	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(h2_stream_info, dir);


	enum tfe_http_event http_body_event = (dir==CONN_DIR_UPSTREAM?EV_HTTP_RESP_BODY_END: EV_HTTP_REQ_BODY_END);
	enum tfe_http_event http_event = (dir==CONN_DIR_UPSTREAM?EV_HTTP_RESP_END: EV_HTTP_REQ_END);

	if (h2_half->body_state != H2_READ_STATE_BEGIN)
	{
		if (h2_half->event_cb) 
		{
			h2_half->event_cb(h2_half, http_body_event, NULL, 0,
						   	  h2_half->event_cb_user);
		}
		if (h2_half->event_cb)
		{
			h2_half->event_cb(h2_half, http_event, NULL, 0,
						   h2_half->event_cb_user);
		}
	}
	struct tfe_h2_payload *payload = &h2_half->h2_payload;
	payload->flags |= NGHTTP2_FLAG_END_STREAM;
	h2_half->body_state = H2_READ_STATE_COMPLETE;
	h2_half->message_state = H2_READ_STATE_COMPLETE;

	stream_action = http2_submit_data_by_h2_half(h2_stream_info, h2_session, dir, 1);
	if (stream_action == ACTION_DROP_DATA)
	{
		xret = nghttp2_session_send(ngh2_session);
		if (xret != 0) 
		{
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s %d\n",nghttp2_strerror(xret), __LINE__);
		}
	}
	if (stream_action == ACTION_USER_DATA)
		stream_action = ACTION_DROP_DATA;
	h2_stream_info->stream_action = stream_action;
	return 1;
}

static int http2_submit_frame_data(struct tfe_h2_stream *h2_stream_info,const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
    struct tfe_h2_session *h2_session = NULL;

	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_peer_session(h2_stream_info, dir);
	h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(ngh2_session, frame->hd.stream_id);
	if (h2_session == NULL)
	{
		http2_submit_end_data_by_h2_half(h2_stream_info, frame->hd.stream_id, dir);
		h2_stream_info->stream_action = ACTION_DROP_DATA;
		return 0;
	}
	struct tfe_h2_half_private *h2_half = tfe_h2_stream_get_half(h2_session, dir);
	/*HEAD STREMA_END + DATA(9)**/
	if (h2_half == NULL)
	{
		http2_submit_end_data_by_h2_half(h2_stream_info, frame->hd.stream_id, dir);
		h2_stream_info->stream_action = ACTION_DROP_DATA;
		return 0;
	}
	if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)
	{
		if (dir == CONN_DIR_UPSTREAM)
			h2_half->h2_payload.padlen = frame->data.padlen;

		if (h2_half->body_state != H2_READ_STATE_COMPLETE)
		{
			http2_submit_complete_data(h2_stream_info, h2_session, dir);
		}
	}
	return 0;
}

static int tfe_half_session_init(struct tfe_h2_session *h2_session, int32_t stream_id, enum tfe_http_direction direction)
{
	struct tfe_http_session *tfe_session = &h2_session->tfe_session;

	tfe_session->major_version = 2;
	if (direction == TFE_HTTP_REQUEST){
		struct tfe_h2_half_private *req = h2_session->req;
		tfe_session->ops = &http2_session_ops;
		tfe_session->req = &req->half_public;
		gettimeofday(&tfe_session->start_time, NULL);
		tfe_session->session_id = stream_id;

	}
	if (direction == TFE_HTTP_RESPONSE){
		struct tfe_h2_half_private *resp = h2_session->resp;
		tfe_session->resp = &resp->half_public;
		tfe_session->session_id = stream_id;
	}
	return 0;
}

static void upstream_create_req(struct tfe_h2_stream *h2_stream_info, nghttp2_session *as_server, struct tfe_h2_session *h2_session, int32_t stream_id)
{
	struct user_event_dispatch *event = NULL;
	struct tfe_h2_half_private *half_private = NULL;

    h2_session->ngh2_stream_id = stream_id;
	h2_session->session = as_server;

	h2_session->req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL);
	tfe_half_session_init(h2_session, stream_id, TFE_HTTP_REQUEST);

	event = ALLOC(struct user_event_dispatch, 1);
	assert(event);
	event->thread_id = h2_stream_info->thread_id;
	event->tf_stream = h2_stream_info->tf_stream;
	event->tfe_session = &h2_session->tfe_session;

	half_set_callback(h2_session->req, event, free);

	/* Call business plugin */
    half_private = h2_session->req;

	half_private->frame_ctx = http_frame_alloc();
	if (half_private->frame_ctx == NULL){
		TFE_STREAM_LOG_ERROR(h2_session, "Failed at raising session begin event. ");
		goto finish;
	}
	http_frame_raise_session_begin(half_private->frame_ctx, h2_stream_info->tf_stream,
								   &h2_session->tfe_session, h2_stream_info->thread_id);
	h2_session->frame_ctx = half_private->frame_ctx;
	h2_session->father_stream = h2_stream_info;

	TAILQ_INSERT_TAIL(&h2_stream_info->h2_session_list, h2_session, next);
	nghttp2_session_set_stream_user_data(as_server, stream_id, h2_session);
finish:
	return;
}

static enum tfe_stream_action http2_server_frame_submit_push_promise(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session)
{
	int32_t stream_id = -1;
    struct tfe_h2_header *headers = NULL;
    struct tfe_h2_session *peer_h2_stream = NULL;
    struct tfe_h2_half_private *resp = NULL;
	enum tfe_stream_action stream_action = ACTION_FORWARD_DATA;

	resp = h2_session->resp;
	if (resp == NULL)
		return stream_action;

	headers = &resp->promised;
	if (headers->nvlen <= 0)
		return stream_action;

	/* Create s' half req*/
	peer_h2_stream = (struct tfe_h2_session *)ALLOC(struct tfe_h2_session, 1);
	assert(peer_h2_stream);

	nghttp2_nv hdrs[headers->nvlen];
	stream_id = nghttp2_submit_push_promise(h2_stream_info->http2_server_handle, headers->flag,
                                         h2_session->ngh2_stream_id, tfe_h2_header_convert_nv(headers, hdrs),
                                         headers->nvlen, peer_h2_stream);
	if (stream_id < 0){
		free(peer_h2_stream);
		peer_h2_stream = NULL;
		TFE_STREAM_LOG_ERROR(h2_session, "Failed to submit push promise: %s", nghttp2_strerror(stream_id));
		goto finish;
    }
	upstream_create_req(h2_stream_info, h2_stream_info->http2_server_handle, peer_h2_stream, stream_id);
	/*clean header message **/
	delete_nv_packet_data(headers);

	stream_action = ACTION_DROP_DATA;
finish:
	return stream_action;
}

static int http2_submit_frame_push_promise(struct tfe_h2_stream *h2_stream_info,const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
	int xret = -1;
    struct tfe_h2_session *h2_session = NULL;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	if (dir == CONN_DIR_DOWNSTREAM)
		goto finish;

	h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(h2_stream_info->http2_client_handle,
																	        	frame->hd.stream_id);
	if (!h2_session){
		TFE_LOG_ERROR(logger()->handle, "Upstream id %d, can't find stream information(addr = %p)",
													 frame->hd.stream_id, h2_stream_info);
		goto finish;
	}

	stream_action = http2_server_frame_submit_push_promise(h2_stream_info, h2_session);
	if (stream_action == ACTION_DROP_DATA){
		xret = nghttp2_session_send(h2_stream_info->http2_server_handle);
		if (xret != 0) {
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n",
													  	  dir, nghttp2_strerror(xret));
		}
	}
	h2_stream_info->stream_action = stream_action;
finish:
	#ifdef TFE_LOG_HTTP2
	TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit push promise, stream_id:%d, action:%d", h2_stream_info->tf_stream->str_stream_info,
				  dir, frame->hd.stream_id, h2_stream_info->stream_action);
	#endif
	return 0;
}

#ifdef TFE_CACHE
static int suspend_start(struct tfe_h2_session *h2_session, struct tfe_h2_half_private *half, const struct tfe_stream *stream)
{
	if (h2_session->cache.spd_valid != 1){
		return 0;
	}

	tfe_stream_resume(stream);

	enum tfe_http_event spd_event = h2_session->cache.spd_event;

	h2_session->cache.spd_event = (enum tfe_http_event)0;
	h2_session->cache.spd_valid = 0;
	h2_session->cache.spd_set_cnt--;
	h2_session->cache.spd_cnt++;

	/* Call user callback, tell user we resume from suspend */
	h2_session->cache.rse_set = 0;
	half->event_cb(half, spd_event, NULL, 0, half->event_cb_user);

	return 1;
}
#endif

static void fill_resp_spec_from_handle(struct tfe_h2_half_private *half_private)
{
	struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL;
    struct tfe_h2_header *header = &half_private->header;
	struct tfe_http_resp_spec *resp_spec = &(half_private->half_public.resp_spec);

	TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field){
		if (!strncasecmp((char *)(h2_field->nv.name), ":status", strlen(":status"))){
			resp_spec->resp_code = atoi((const char *)h2_field->nv.value);
			continue;
		}
		if (!strncasecmp((char *)(h2_field->nv.name), "content-type", strlen("content-type"))){
			resp_spec->content_type = tfe_strdup((const char *)(h2_field->nv.value));;
			continue;
		}
		if (!strncasecmp((char *)(h2_field->nv.name), "content-encoding", strlen("content-encoding"))){
			resp_spec->content_encoding = tfe_strdup((const char *)(h2_field->nv.value));;
			continue;
		}
		if (!strncasecmp((char *)(h2_field->nv.name), "content-length", strlen("content-length"))){
			resp_spec->content_length = tfe_strdup((const char *)(h2_field->nv.value));;
			continue;
		}
	}
	return;
}

int http2_write_log(struct tfe_h2_session *h2_session, const char * str_stream_info, int dir)
{
	/* Request */
	struct tfe_h2_half_private *req = h2_session->req;
	/* Response */
	struct tfe_h2_half_private *resp = h2_session->resp;
	/* Req-Public */
	struct tfe_http_req_spec *req_spec = req ? &(req->half_public.req_spec) : NULL;
	/* Resp-Public */
	struct tfe_http_resp_spec *resp_spec = resp ? &(resp->half_public.resp_spec) : NULL;

	const char * method = req_spec ? val_to_str(req_spec->method, method_vals) : "-";
	const char * url = req_spec ? req_spec->url : "-";
	char resp_code[TFE_STRING_MAX];
	if (resp_spec)
		snprintf(resp_code, sizeof(resp_code) - 1, "%d", resp_spec->resp_code);
	else
		snprintf(resp_code, sizeof(resp_code) - 1, "%s", "-");

	const char * cont_type = resp_spec ? resp_spec->content_type != NULL ? resp_spec->content_type : "-" : "-";
	const char * cont_encoding =
		resp_spec ? resp_spec->content_encoding != NULL ? resp_spec->content_encoding : "-" : "-";

	const char *hmsg = (dir == CONN_DIR_UPSTREAM) ? "response" : "request";

	const char * panggu_req = h2_session->plugin_built_req ? "USER/REQ" : "-";
	const char * panggu_resp = h2_session->plugin_built_resp ? "USER/RESP" : "-";

	/* SUSPEND */
	const char * suspend = h2_session->cache.spd_cnt > 0 ? "SUSPEND" : "-";

	char *access_log;
	asprintf(&access_log, "%s %d %s stream_id:%d %s %s HTTP2.0 %s %s %s %s %s %s", str_stream_info, dir, hmsg, h2_session->tfe_session.session_id,
		     method, url, resp_code, cont_type, cont_encoding, panggu_req, panggu_resp, suspend);

	TFE_LOG_INFO(logger()->handle, "%s", access_log);
	free(access_log);
	return 0;
}

static enum tfe_stream_action http2_submit_built_response(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session)
{
	int xret = -1;
	char value[128] = {0};
	enum tfe_stream_action stream_action = ACTION_FORWARD_DATA;
    struct tfe_h2_half_private *resp      = h2_session->plugin_built_resp;

	struct http_field_name field;
	field.field_id = TFE_HTTP_UNKNOWN_FIELD;
	field.field_name = "X-TG-Construct-By";
	snprintf(value, sizeof(value), "tfe/%s", tfe_version());
	tfe_h2_header_add_field(&resp->header, &field, (const char *)value, 0);

	stream_action = http2_frame_submit_built_resp(h2_stream_info, h2_session);
	if (stream_action == ACTION_DROP_DATA)
	{
		xret = nghttp2_session_send(h2_stream_info->http2_server_handle);
		if (xret != 0) {
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "Fatal downstream send error: %s\n",
						  nghttp2_strerror(xret));
		}
	}
	if (stream_action == ACTION_DROP_DATA)
	{
		stream_action = (enum tfe_stream_action)ACTION_USER_DATA;
	}
	return stream_action;
}

static void h2_delete_resp_hdrs_filter(struct tfe_h2_header *headers, nghttp2_nv *hdrs)
{
	if((headers->nvlen > 0) && strncasecmp((char *)hdrs[0].name, ":status", strlen((char *)hdrs[0].name)) == 0)
	{
		int resp_code = atoi((const char *)hdrs[0].value);
		switch(resp_code)
		{
			case HTTP_RESP_CODE_100:
			case HTTP_RESP_CODE_101:
			case HTTP_RESP_CODE_102:
			case HTTP_RESP_CODE_103:
				delete_nv_packet_data(headers);
				break;
			default:
				break;
		}
	}
	return;
}

static enum tfe_stream_action http2_server_frame_submit_header(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session)
{
	int32_t xret = 0;
	struct tfe_h2_header *headers = NULL;
	struct tfe_h2_half_private *resp = NULL;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	if (h2_session->plugin_built_resp != NULL){
		struct tfe_h2_payload *h2_payload = &(h2_session->plugin_built_resp->h2_payload);
		if (h2_payload->evbuf_body != NULL && (evbuffer_get_length(h2_payload->evbuf_body) > 0))
		{
			stream_action =	http2_submit_built_response(h2_stream_info, h2_session);
		}
		else
		{
			stream_action = (enum tfe_stream_action)ACTION_USER_DATA;
		}
		return stream_action;
	}
	resp = h2_session->resp;
	if (resp == NULL){
		return ACTION_FORWARD_DATA;
	}
	headers = &resp->header;
	if (headers->nvlen <= 0){
		return ACTION_FORWARD_DATA;
	}
	nghttp2_nv hdrs[headers->nvlen];
    xret = nghttp2_submit_headers(h2_stream_info->http2_server_handle, headers->flag,
                                  h2_session->ngh2_stream_id, NULL, tfe_h2_header_convert_nv(headers, hdrs),
                                  headers->nvlen, h2_session);
    if (xret < 0){
        printf("Fatal headers error: %s\n", nghttp2_strerror(xret));
    }

	h2_delete_resp_hdrs_filter(headers, hdrs);

	return stream_action;
}

static int http2_server_submit_header(struct tfe_h2_stream *h2_stream_info, int32_t stream_id)
{
	int xret = -1;
	struct tfe_h2_half_private *resp    = NULL;
	struct tfe_h2_session *h2_session = NULL;

	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(h2_stream_info->http2_client_handle, stream_id);
	if (!h2_session)
	{
		stream_action = ACTION_FORWARD_DATA;
		TFE_LOG_ERROR(logger()->handle, "Upstream id %d, can't find stream information(addr = %p)",
													  stream_id, h2_stream_info);
		goto finish;
	}
	assert(h2_session->ngh2_stream_id == stream_id);

	resp = h2_session->resp;
	fill_resp_spec_from_handle(h2_session->resp);

	resp->event_cb(resp, EV_HTTP_RESP_HDR, NULL, 0, resp->event_cb_user);
	if (h2_session->cache.spd_set){
		h2_session->cache.spd_event = EV_HTTP_RESP_HDR;
		h2_session->cache.spd_valid = 1;
		h2_session->cache.spd_set = 0;
		h2_session->cache.spd_set_cnt++;
		h2_session->cache.spd_cnt++;
		tfe_stream_suspend(h2_stream_info->tf_stream, CONN_DIR_UPSTREAM);

		stream_action = ACTION_DEFER_DATA;
		goto finish;
	}

	// int googletest,  h2_stream_info->tf_stream is NULL
	if (h2_session && h2_stream_info && h2_stream_info->tf_stream) {
		http2_write_log(h2_session, h2_stream_info->tf_stream->str_stream_info, CONN_DIR_UPSTREAM);
	}

	stream_action = http2_server_frame_submit_header(h2_stream_info, h2_session);
	if (stream_action == ACTION_DROP_DATA){
		xret = nghttp2_session_send(h2_stream_info->http2_server_handle);
		if (xret != 0) {
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n", nghttp2_strerror(xret));
		}
	}
	if (stream_action == ACTION_USER_DATA)
		stream_action = ACTION_DROP_DATA;
finish:
	h2_stream_info->stream_action = stream_action;
	return 0;
}

static void fill_req_spec_from_handle(struct tfe_h2_half_private *half_private)
{
	int urllen = 0;
	struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL;
    struct tfe_h2_header *header = &half_private->header;
	struct tfe_http_req_spec *req_spec = &(half_private->half_public.req_spec);

	TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field){
		if (!strncasecmp((char *)(h2_field->nv.name), ":method", strlen(":method"))){
			req_spec->method = (enum tfe_http_std_method)str_to_val((const char *)(h2_field->nv.value), method_vals);
			continue;
		}
		if (!strncasecmp((char *)(h2_field->nv.name), ":authority", strlen(":authority"))){
			req_spec->host = tfe_strdup((const char *)(h2_field->nv.value));
			urllen += h2_field->nv.valuelen;
			continue;
		}
		if (!strncasecmp((char *)(h2_field->nv.name), ":path", strlen(":path"))){
			req_spec->uri = tfe_strdup((const char*)(h2_field->nv.value));
			urllen += h2_field->nv.valuelen;
			continue;
		}
	}
	char *urltmp = ALLOC(char, urllen + 1);
	if(urltmp){
		sprintf(urltmp, "%s%s", (char *)req_spec->host, (char *)req_spec->uri);
		req_spec->url = urltmp;
	}
	return;
}

#ifdef TFE_CACHE
static int suspend_stop(struct tfe_h2_session *h2_session, const struct tfe_stream *tf_stream, enum tfe_conn_dir dir)
{
	int xret = -1;

	if (h2_session->cache.spd_set){
		h2_session->cache.spd_valid = 1;
		h2_session->cache.spd_set = 0;
		tfe_stream_suspend(tf_stream, dir);
		xret = 0;
	}
	return xret;
}
#endif

static void downstream_create_resp(struct tfe_h2_session *h2_session, nghttp2_session *as_client,
					   nghttp2_session *as_server, const struct tfe_stream *tf_stream, unsigned int thread_id)
{
	struct user_event_dispatch *event = NULL;

	if (h2_session->resp)
		goto finish;
	h2_session->resp = tfe_half_private_init(TFE_HTTP_RESPONSE, h2_session->ngh2_stream_id, as_server);
	tfe_half_session_init(h2_session, h2_session->ngh2_stream_id, TFE_HTTP_RESPONSE);

	event = ALLOC(struct user_event_dispatch, 1);
	assert(event);
	event->thread_id = thread_id;
	event->tf_stream = tf_stream;
	event->tfe_session = &h2_session->tfe_session;

	half_set_callback(h2_session->resp, event, free);

	h2_session->resp->frame_ctx = h2_session->frame_ctx;

    nghttp2_session_set_stream_user_data(as_client, h2_session->ngh2_stream_id, h2_session);
finish:
	return;
}

static enum tfe_stream_action http2_client_frame_submit_header(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session)
{
	int32_t stream_id    = -1;
    struct tfe_h2_header *headers   = NULL;
    struct tfe_h2_half_private *req  = NULL;
	enum tfe_http_std_method method = (enum tfe_http_std_method)HTTP_REQUEST_METHOD_UNKNOWN;
	enum tfe_stream_action stream_action = ACTION_FORWARD_DATA;

	req = h2_session->plugin_built_req != NULL ? h2_session->plugin_built_req : h2_session->req;
	if (req == NULL){
		return ACTION_FORWARD_DATA;
	}
	/*Create C' half_private_resp**/
	downstream_create_resp(h2_session, h2_stream_info->http2_client_handle, h2_stream_info->http2_server_handle,
						   h2_stream_info->tf_stream, h2_stream_info->thread_id);
	/*Adapt inconsistent client and server stream ids ***/
	if (h2_session->plugin_built_resp)
	{
		stream_action = http2_submit_built_response(h2_stream_info, h2_session);
		if(stream_action == ACTION_USER_DATA)
		{
			nghttp2_session_terminate_session(h2_stream_info->http2_client_handle, 0);
		}
		return stream_action;
	}
	headers = &req->header;
	if (headers->nvlen <= 0)
	{
		return ACTION_FORWARD_DATA;
	}

	method = http2_get_method(h2_session->req);
	if (method == (enum tfe_http_std_method)HTTP_REQUEST_METHOD_POST || method == (enum tfe_http_std_method)HTTP_REQUEST_METHOD_PUT)
	{
		const struct http_field_name field = {TFE_HTTP_CONT_LENGTH, NULL};
		const char *content_length = h2_half_ops_field_read(&(h2_session->req->half_public), &field);
		if ((h2_session->plugin_built_req != NULL) && (atoi(content_length) != 0))
		{
			stream_action = (enum tfe_stream_action)ACTION_USER_DATA;
			return stream_action;
		}
	}	
	nghttp2_nv hdrs[headers->nvlen];
	/**Register the stream id as -1 and read the next stream id */
	nghttp2_session_set_next_stream_id(h2_stream_info->http2_client_handle, h2_session->ngh2_stream_id);
	stream_id = nghttp2_submit_headers(h2_stream_info->http2_client_handle, headers->flag,
									   -1, NULL, tfe_h2_header_modify_field(headers, hdrs, ":path", req->url_storage),
									  headers->nvlen, h2_session);
							  
	if (stream_id < 0){
		TFE_LOG_ERROR(logger()->handle, "Could not submit request: %s", nghttp2_strerror(stream_id));
        stream_action = ACTION_FORWARD_DATA;
        goto finish;
    }
	stream_action = ACTION_DROP_DATA;
finish:
	return stream_action;
}

static int http2_client_submit_header(struct tfe_h2_stream *h2_stream_info, int32_t stream_id)
{
	int xret = -1;
	struct tfe_h2_half_private *req = NULL;
    struct tfe_h2_session *h2_session = NULL;

	enum tfe_stream_action stream_action = ACTION_DROP_DATA;

	h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(h2_stream_info->http2_server_handle,
																			    stream_id);
	if (!h2_session){
		stream_action = ACTION_FORWARD_DATA;
		goto finish;
	}
	assert(h2_session->ngh2_stream_id == stream_id);

	req = h2_session->req;
	fill_req_spec_from_handle(h2_session->req);	
	//h2_stream_info->as_client->last_sent_stream_id = MIN(h2_stream_info->as_client->last_sent_stream_id, stream_id) - 1;	

	req->event_cb(req, EV_HTTP_REQ_HDR, NULL, 0, req->event_cb_user);

	// int googletest,  h2_stream_info->tf_stream is NULL
	if (h2_session && h2_stream_info && h2_stream_info->tf_stream) {
		http2_write_log(h2_session, h2_stream_info->tf_stream->str_stream_info, CONN_DIR_DOWNSTREAM);
	}
	stream_action = http2_client_frame_submit_header(h2_stream_info, h2_session);
	if (stream_action == ACTION_DROP_DATA){
		xret = nghttp2_session_send(h2_stream_info->http2_client_handle);
		if (xret != 0) {
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "Fatal downstream send error: %s\n", nghttp2_strerror(xret));
		}
	}
	if (stream_action == ACTION_USER_DATA)
		stream_action = ACTION_DROP_DATA;

finish:
	h2_stream_info->stream_action = stream_action;
	return 0;
}

static int http2_submit_frame_header(struct tfe_h2_stream *h2_stream_info,const nghttp2_frame *frame, enum tfe_conn_dir dir)
{
	int xret = 0;

	if (frame->hd.flags & NGHTTP2_FLAG_END_HEADERS)
	{
		if (dir == CONN_DIR_UPSTREAM)
		{
			xret = http2_server_submit_header(h2_stream_info, frame->hd.stream_id);
		}
		if (dir == CONN_DIR_DOWNSTREAM)
		{
			xret = http2_client_submit_header(h2_stream_info, frame->hd.stream_id);
		}
	}
	return xret;
}

http2_frame_callback http2_frame_callback_array[] = {
    [NGHTTP2_DATA]    		= http2_submit_frame_data,
    [NGHTTP2_HEADERS] 		= http2_submit_frame_header,
    [NGHTTP2_PRIORITY]      = http2_submit_frame_priority,
    [NGHTTP2_RST_STREAM]    = http2_submit_frame_rst_stream,
    [NGHTTP2_SETTINGS]      = http2_submit_frame_settings,
    [NGHTTP2_PUSH_PROMISE]  = http2_submit_frame_push_promise,
    [NGHTTP2_PING]      	= http2_submit_frame_ping,
    [NGHTTP2_GOAWAY]        = http2_submit_frame_goaway,
    [NGHTTP2_WINDOW_UPDATE]	= http2_submit_frame_window_update,
    [NGHTTP2_CONTINUATION]  = NULL,
    [NGHTTP2_ALTSVC] 		= NULL,
};

static int http2_fill_up_header(nghttp2_session *ngh2_session, const nghttp2_frame *frame, const uint8_t *name,
                       		size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir)
{
    struct tfe_h2_header *h2_header    = NULL;

	if (dir == CONN_DIR_DOWNSTREAM &&
		frame->headers.cat != NGHTTP2_HCAT_REQUEST){
		return 0;
	}
	struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(ngh2_session, frame->hd.stream_id);
	if (!h2_session){
		TFE_LOG_ERROR(logger()->handle, "Header stream id %d, can't find stream information",
													 frame->hd.stream_id);
		return 0;
	}	
	struct tfe_h2_half_private *half = (dir == CONN_DIR_UPSTREAM) ? h2_session->resp : h2_session->req;

	struct http_field_name field;
	// "name" may not terminated with '\0',  so use "namelen"
	field.field_id = (enum tfe_http_std_field)http2_header_str_to_val((const char *)name, namelen, __str_std_header_field_map, __str_std_header_field_map_size);
	if (field.field_id == TFE_HTTP_UNKNOWN_FIELD)
	{
		field.field_name = (const char *)name;
	}
	if (field.field_id == TFE_HTTP_CONT_ENCODING)
	{
		half->h2_payload.encode_type = method_to_str_idx((const char *)value);
	}
	h2_header = &half->header;
	tfe_h2_header_add_field(h2_header, &field, (const char *)value, 1);
	h2_header->flag = frame->hd.flags;
	return 0;
}

static int http2_fill_up_promise(nghttp2_session *ngh2_session, const nghttp2_frame *frame, const uint8_t *name,
                        	size_t namelen, const uint8_t *value,size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir)
{
	struct tfe_h2_header *headers = NULL;
	struct tfe_h2_half_private *resp = NULL;

	if (dir == CONN_DIR_DOWNSTREAM)
		return 0;

	struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(ngh2_session, frame->hd.stream_id);
    if (!h2_session){
		TFE_LOG_ERROR(logger()->handle, "Promise stream id %d, can't find stream information",
		  			  frame->hd.stream_id);
		return 0;
    }
	resp = h2_session->resp;
	struct http_field_name field;
	// "name" may not terminated with '\0',  so use "namelen"
	field.field_id = (enum tfe_http_std_field)http2_header_str_to_val((const char *)name, namelen, __str_std_header_field_map, __str_std_header_field_map_size);
	if (field.field_id == TFE_HTTP_UNKNOWN_FIELD)
	{
		field.field_name = (const char *)name;
	}
	if (field.field_id == TFE_HTTP_CONT_ENCODING)
	{
		resp->h2_payload.encode_type = method_to_str_idx((const char *)value);
	}
	headers = &resp->promised;
	tfe_h2_header_add_field(headers, &field, (const char *)value, 1);
	headers->flag = frame->hd.flags;
	return 0;
}

static int http2_data_send(nghttp2_session *ngh2_session, const nghttp2_frame *frame, const uint8_t *data, size_t length, const uint8_t *value,
 		 		  	 size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir)

{
	int ret = -1;

	struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;

	ret = tfe_stream_write(h2_stream_info->tf_stream, dir, data, length);
	if (unlikely(ret < 0)){
		assert(0);
	}
    return (ssize_t)length;
}

static int http2_on_stream_close(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name,
                 			size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir)
{
    struct tfe_h2_session *h2_session = NULL;
	struct tfe_h2_half_private *resp    = NULL;

	int32_t stream_id   = frame->hd.stream_id;
	uint32_t error_code = frame->goaway.error_code;

	struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;

	h2_session = TAILQ_LIST_FIND(h2_stream_info, stream_id);
    if (!h2_session)
    {
		return 0;
	}
	if (error_code != 0){
		const char *str = (dir == CONN_DIR_UPSTREAM) ? "Simulation s" : "Simulation c";
		TFE_LOG_DEBUG(logger()->handle, "%s close, id = %d, error_code = %d", str,
												      stream_id, error_code);
	}
	if (dir == CONN_DIR_DOWNSTREAM)
		goto finish;

	resp = h2_session->resp;
	if (error_code == 0 && resp->body_state != H2_READ_STATE_COMPLETE){
		if (resp->body_state == H2_READ_STATE_BEGIN &&
			h2_stream_info->stream_action != ACTION_DEFER_DATA)
			http2_submit_header_by_not_modify(h2_stream_info, h2_session);
		goto end;
	}
finish:
	if((dir == CONN_DIR_DOWNSTREAM && (h2_session->resp == NULL || h2_session->plugin_built_resp != NULL) )|| dir == CONN_DIR_UPSTREAM)
	{
		TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next);
	}
	if ((dir == CONN_DIR_DOWNSTREAM && h2_session->frame_ctx && h2_session->plugin_built_resp != NULL)
		||(dir == CONN_DIR_UPSTREAM && h2_session->frame_ctx))
	{
		http_frame_raise_session_end(h2_session->frame_ctx, h2_session->tf_stream, &h2_session->tfe_session,
									 h2_stream_info->thread_id);
		h2_session->frame_ctx = NULL;
		delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1);
		/*The stream connection is closed, Force clear context**/
		nghttp2_session_set_stream_user_data(h2_stream_info->http2_client_handle, stream_id, NULL);
		nghttp2_session_set_stream_user_data(h2_stream_info->http2_server_handle, stream_id, NULL);
		free(h2_session);
		h2_session = NULL;
	}
end:
	return 0;
}

http2_callback http2_callback_array[] = {
	[NGHTTP2_DATA]    		= NULL,
    [NGHTTP2_HEADERS] 		= http2_fill_up_header,
    [NGHTTP2_PRIORITY]      = NULL,
    [NGHTTP2_RST_STREAM]    = NULL,
    [NGHTTP2_SETTINGS]      = NULL,
    [NGHTTP2_PUSH_PROMISE]  = http2_fill_up_promise,
    [NGHTTP2_PING]      	= NULL,
    [NGHTTP2_GOAWAY]        = NULL,
    [NGHTTP2_WINDOW_UPDATE]	= NULL,
    [NGHTTP2_CONTINUATION]  = NULL,
    [NGHTTP2_ALTSVC] 		= NULL,
	[NGHTTP2_USER_SEND]     = http2_data_send,
	[NGHTTP2_USER_COLSE]    = http2_on_stream_close
};

static ssize_t http2_client_send(nghttp2_session *session, const uint8_t *data, size_t length, int flags, void *user_data)
{
	if ( http2_callback_array[NGHTTP2_USER_SEND] != NULL){
		return (ssize_t)http2_callback_array[NGHTTP2_USER_SEND](session, NULL, data, length,
											   NULL, 0, flags, user_data, CONN_DIR_UPSTREAM);
	}
    return (ssize_t)length;
}

static int http2_client_on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
{
	struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;

	if ( http2_frame_callback_array[frame->hd.type] != NULL){
		return http2_frame_callback_array[frame->hd.type](h2_stream_info, frame, CONN_DIR_UPSTREAM);
	}
	return 0;
}

static enum tfe_stream_action http2_submit_data_by_user(struct tfe_h2_stream *connection,struct tfe_h2_session *h2_session, enum tfe_conn_dir dir)
{
	int rv = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;	
	
	struct tfe_h2_half_private *h2_half = tfe_h2_stream_get_half(h2_session, dir);
	nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir);

	struct tfe_h2_payload *body = &h2_half->h2_payload;

	nghttp2_data_provider upstream_data_provider;
	upstream_data_provider.source.ptr    = (void *)body;
	upstream_data_provider.read_callback = upstream_read_callback;

	rv = nghttp2_submit_data(ngh2_session, body->flags,
	                       h2_session->ngh2_stream_id, &upstream_data_provider);
	if (rv != 0){
		stream_action = ACTION_FORWARD_DATA;
	    //printf("Fatal server submit data error: %s\n", nghttp2_strerror(rv));
	}
	return stream_action;
}

static int http2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, int32_t stream_id, const uint8_t *input,
										size_t input_len, void *user_data)
{
	size_t len;
    char *uncompr = NULL;
	int xret = -1, compress_result = 1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;
	int uncompr_len = 0, __attribute__((__unused__))ret = 0;
	const unsigned char *data;
	struct tfe_h2_half_private *resp=NULL, *pangu_resp=NULL;

    struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;

	/*proc build resp*/
	/*when input_len == 1 is end_stream, Nghttp2 sends this frame, drop it*/
	struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(session, stream_id);
	if (h2_session == NULL || ((flags & NGHTTP2_FLAG_END_STREAM) && input_len == 1))
	{
		return 0;
	}

	resp = h2_session->resp;
	if (resp == NULL)
	{
		h2_stream_info->stream_action = ACTION_DROP_DATA;
		return 0;
	}

	pangu_resp = h2_session->plugin_built_resp;
	if(pangu_resp != NULL && pangu_resp->message_state == H2_READ_STATE_COMPLETE)
	{
		h2_stream_info->stream_action = ACTION_DROP_DATA;
		return 0;
	}

	evbuffer_add(resp->h2_payload.evbuf_body, input, input_len);
	if (resp->h2_payload.encode_type != HTTP2_CONTENT_ENCODING_NONE)
	{
		ret = http2_decompress_stream(input, input_len, &uncompr, &uncompr_len, &resp->h2_payload.inflate, resp->h2_payload.encode_type);
		if (((ret == Z_STREAM_END) || (ret == Z_OK)) && uncompr_len > 0)
		{
			input      = (const uint8_t*)uncompr;
			input_len  = uncompr_len;
		}
		else
		{
			compress_result = 0;
			/*if input is end_stream, send by nghttp2_submit_frame_data **/
			if (flags != NGHTTP2_FLAG_END_STREAM && h2_session->plugin_built_resp == NULL)
			{
				/**Decompression failed, send this data**/		
				stream_action = http2_submit_data_by_user(h2_stream_info, h2_session, CONN_DIR_UPSTREAM);
				if (stream_action == ACTION_DROP_DATA)
				{
					xret = nghttp2_session_send(h2_stream_info->http2_server_handle);
					if (xret != 0) 
					{
						stream_action = ACTION_FORWARD_DATA;
						TFE_LOG_ERROR(logger()->handle, "Fatal upstream(%d) send error: %s\n",stream_id, nghttp2_strerror(xret));
					}
				}
				if(uncompr)
				{
					FREE(&uncompr);
				}
				h2_stream_info->stream_action = stream_action;
				return 0;
			}
		}
	}
	data = input;
	len  = input_len;

	if (resp->body_state == H2_READ_STATE_BEGIN && compress_result)
	{
		if (resp->event_cb)
		{
			resp->event_cb(resp, EV_HTTP_RESP_BODY_BEGIN, NULL, len,
								   resp->event_cb_user);
		}
		if (flags & NGHTTP2_FLAG_END_STREAM)
		{
			resp->h2_payload.flags = NGHTTP2_FLAG_NONE;
		}else
		{
			resp->h2_payload.flags = flags;
		}
		resp->body_state = H2_READ_STATE_READING;
	}
	if (resp->body_state == H2_READ_STATE_READING && compress_result)
	{
		if (resp->event_cb)
		{
			resp->event_cb(resp, EV_HTTP_RESP_BODY_CONT, data, len, resp->event_cb_user);
		}
		if (flags & NGHTTP2_FLAG_END_STREAM)
		{
			resp->h2_payload.flags = NGHTTP2_FLAG_NONE;
		}else
		{
			resp->h2_payload.flags = flags;
		}
	}
	if (uncompr_len)
	{
		FREE(&uncompr);
	}
	stream_action = http2_submit_data_by_h2_half(h2_stream_info, h2_session, CONN_DIR_UPSTREAM, compress_result);
	if (stream_action == ACTION_DROP_DATA)
	{
		xret = nghttp2_session_send(h2_stream_info->http2_server_handle);
		if (xret != 0)
		{
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "Fatal upstream(%d) send error: %s\n",stream_id, nghttp2_strerror(xret));
		}
	}
	if (stream_action == ACTION_USER_DATA)
		stream_action = ACTION_DROP_DATA;
	h2_stream_info->stream_action = stream_action;
	return 0;
}

static int http2_client_on_stream_close(nghttp2_session *session, int32_t stream_id, uint32_t error_code, void *user_data)
{
	nghttp2_frame frame;
	memset(&frame, 0, sizeof(frame));

	frame.hd.stream_id = stream_id;
	frame.goaway.error_code = error_code;
	if ( http2_callback_array[NGHTTP2_USER_COLSE] != NULL){
		return http2_callback_array[NGHTTP2_USER_COLSE](session, &frame, NULL, 0,
											   			  NULL, 0, 0, user_data, CONN_DIR_UPSTREAM);
	}
    return 0;
}

static int http2_client_on_header(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name,
                     	 	size_t namelen, const uint8_t *value,size_t valuelen, uint8_t flags, void *user_data)
{
	if ( http2_callback_array[frame->hd.type] != NULL){
		return http2_callback_array[frame->hd.type](session, frame, name, namelen,
												      value, valuelen, flags, user_data, CONN_DIR_UPSTREAM);
	}

    return 0;
}

static struct tfe_h2_session* create_upstream_data(nghttp2_session *session, int32_t stream_id, struct tfe_h2_stream *h2_stream_info)
{
    struct tfe_h2_session *h2_session = NULL;
	struct user_event_dispatch *event = NULL;
	struct tfe_h2_half_private *half_private = NULL;

	h2_session = TAILQ_LIST_FIND(h2_stream_info, stream_id);
    if (h2_session == NULL){
        goto resp;
    }
	if (h2_session->resp){
		nghttp2_session_set_stream_user_data(session, stream_id, h2_session);
		/** todo:When the data of the reply is pushed as promised,
		there is no stream id at the reply end. to create it*/
		goto finish;
	}
resp:
	h2_session = (struct tfe_h2_session *)ALLOC(struct tfe_h2_session, 1);
	assert(h2_session);
	h2_session->ngh2_stream_id = stream_id;
	h2_session->tf_stream = h2_stream_info->tf_stream;

	h2_session->resp = tfe_half_private_init(TFE_HTTP_RESPONSE, stream_id, h2_stream_info->http2_server_handle);
	tfe_half_session_init(h2_session, stream_id, TFE_HTTP_RESPONSE);

	event = ALLOC(struct user_event_dispatch, 1);
	assert(event);
	event->thread_id = h2_stream_info->thread_id;
	event->tf_stream = h2_stream_info->tf_stream;
	event->tfe_session = &h2_session->tfe_session;

	half_set_callback(h2_session->resp, event, free);
    half_private = h2_session->resp;
	if (h2_session->frame_ctx == NULL)
	{
		half_private->frame_ctx = http_frame_alloc();
		if (half_private->frame_ctx == NULL){
			TFE_STREAM_LOG_ERROR(h2_session, "Failed at raising session begin event. ");
			goto finish;
		}
		http_frame_raise_session_begin(half_private->frame_ctx, h2_stream_info->tf_stream,
									   &h2_session->tfe_session, h2_stream_info->thread_id);
		h2_session->frame_ctx = half_private->frame_ctx;

	}
	h2_session->resp->frame_ctx = h2_session->frame_ctx;
    nghttp2_session_set_stream_user_data(session, stream_id, h2_session);

finish:
    return h2_session;
}

static ssize_t http2_client_select_padding_callback(nghttp2_session *session,const nghttp2_frame *frame,size_t max_payloadlen, void *user_data)
{
    struct tfe_h2_half_private *resp    = NULL;
	struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;
	
	struct tfe_h2_session *h2_session = TAILQ_LIST_FIND(h2_stream_info, frame->hd.stream_id);
	if (h2_session == NULL)		return frame->hd.length;
		
	resp = h2_session->resp;
	if (resp == NULL)	
		return frame->hd.length;
	
  	return (ssize_t)MIN(max_payloadlen, frame->hd.length + (resp->h2_payload.padlen));
}

static int http2_client_on_begin_headers(nghttp2_session * session, const nghttp2_frame * frame, void * user_data)
{
    (void)session;

    struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;
    switch(frame->hd.type){
        case NGHTTP2_HEADERS:
            create_upstream_data(session, frame->hd.stream_id, h2_stream_info);
            break;
        default:
            break;
    }
    return 0;
}

static void client_session_init(struct tfe_h2_stream *h2_stream_info)
{
    nghttp2_session_callbacks *callbacks;

    nghttp2_session_callbacks_new(&callbacks);

    nghttp2_session_callbacks_set_send_callback(callbacks, http2_client_send);

    nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, http2_client_on_frame_recv);

    nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, http2_client_on_data_chunk_recv);

	nghttp2_session_callbacks_set_select_padding_callback(callbacks, http2_client_select_padding_callback);

    nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, http2_client_on_stream_close);

    nghttp2_session_callbacks_set_on_header_callback(callbacks, http2_client_on_header);

    nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, http2_client_on_begin_headers);

    nghttp2_session_client_new(&h2_stream_info->http2_client_handle, callbacks, h2_stream_info);

    nghttp2_session_callbacks_del(callbacks);
}

static ssize_t http2_server_send(nghttp2_session *session, const uint8_t *data, size_t length, int flags, void *user_data)
{
	if ( http2_callback_array[NGHTTP2_USER_SEND] != NULL){
		return (ssize_t)http2_callback_array[NGHTTP2_USER_SEND](session, NULL, data, length,
											   NULL, 0, flags, user_data, CONN_DIR_DOWNSTREAM);
	}
    return (ssize_t)length;
}

static int http2_server_on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
{
	struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;

	if ( http2_frame_callback_array[frame->hd.type] != NULL){
		return http2_frame_callback_array[frame->hd.type](h2_stream_info, frame, CONN_DIR_DOWNSTREAM);
	}

	return 0;
}

static int http2_server_on_stream_close(nghttp2_session *session, int32_t stream_id, uint32_t error_code, void *user_data)
{
	nghttp2_frame frame;
	memset(&frame, 0, sizeof(frame));

	frame.hd.stream_id = stream_id;
	frame.goaway.error_code = error_code;
	if ( http2_callback_array[NGHTTP2_USER_COLSE] != NULL){
		return http2_callback_array[NGHTTP2_USER_COLSE](session, &frame, NULL, 0,
											   			  NULL, 0, 0, user_data, CONN_DIR_DOWNSTREAM);
	}
    return 0;
}

static int http2_server_on_header(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name,
                         	size_t namelen, const uint8_t *value,size_t valuelen, uint8_t flags, void *user_data)
{
	if ( http2_callback_array[frame->hd.type] != NULL){
		return http2_callback_array[frame->hd.type](session, frame, name, namelen, value,
												      valuelen, flags, user_data,CONN_DIR_DOWNSTREAM);
	}
	return 0;
}

static void create_serv_stream_data(nghttp2_session *session, int32_t stream_id, struct tfe_h2_stream *h2_stream_info)
{
    struct tfe_h2_session *h2_session = NULL;
	struct user_event_dispatch *event       = NULL;
	struct tfe_h2_half_private *half_private = NULL;

	h2_session = TAILQ_LIST_FIND(h2_stream_info, stream_id);
    if (h2_session != NULL){
		nghttp2_session_set_stream_user_data(session, stream_id, h2_session);
        goto finish;
    }

    h2_session = (struct tfe_h2_session *)ALLOC(struct tfe_h2_session, 1);
	assert(h2_session);
    h2_session->ngh2_stream_id = stream_id;
	h2_session->session = h2_stream_info->http2_server_handle;
	h2_session->tf_stream = h2_stream_info->tf_stream;

	h2_session->req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL);
	tfe_half_session_init(h2_session, stream_id, TFE_HTTP_REQUEST);

	event = ALLOC(struct user_event_dispatch, 1);
	assert(event);
	event->thread_id = h2_stream_info->thread_id;
	event->tf_stream = h2_stream_info->tf_stream;
	event->tfe_session = &h2_session->tfe_session;

	half_set_callback(h2_session->req, event, free);

	/* Call business plugin */
    half_private = h2_session->req;

	half_private->frame_ctx = http_frame_alloc();
	if (half_private->frame_ctx == NULL){
		TFE_LOG_ERROR(logger()->handle, "Failed at raising session begin event. ");
		goto finish;
	}
	h2_session->frame_ctx = half_private->frame_ctx;
	http_frame_raise_session_begin(half_private->frame_ctx, h2_stream_info->tf_stream,
								   &h2_session->tfe_session, h2_stream_info->thread_id);
	h2_session->father_stream = h2_stream_info;

	TAILQ_INSERT_TAIL(&h2_stream_info->h2_session_list, h2_session, next);
   	nghttp2_session_set_stream_user_data(session, stream_id, h2_session);
finish:
    return;
}

static int http2_server_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, int32_t stream_id, const uint8_t *input,
								  		size_t input_len, void *user_data)
{
	size_t __attribute__((__unused__))len;
    char *uncompr = NULL;
	int xret = -1;
	enum tfe_stream_action stream_action = ACTION_DROP_DATA;
	int uncompr_len = 0, __attribute__((__unused__))ret = 0;
	const unsigned char __attribute__((__unused__))*data;
	struct tfe_h2_half_private * req = NULL;

    struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;

	struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(session, stream_id);
	if (!h2_session){
		TFE_LOG_ERROR(logger()->handle, "On data callback can't get downstream information, id = %d", stream_id);
		goto finish;
	}
	req = h2_session->req;
	req->h2_payload.flags = flags;
	evbuffer_add(req->h2_payload.evbuf_body, input, input_len);

	if (req->h2_payload.encode_type != HTTP2_CONTENT_ENCODING_NONE){
		ret = http2_decompress_stream(input, input_len, &uncompr, &uncompr_len,
						   &req->h2_payload.inflate, req->h2_payload.encode_type);
		if (((ret == Z_STREAM_END) || (ret == Z_OK)) && uncompr > 0){
			input      = (const uint8_t*)uncompr;
			input_len  = uncompr_len;
		}
		else
		{
			if (uncompr_len)	FREE(&uncompr);
		}
	}
	data = input;
	len  = input_len;
	/*todo post data scan**/
	if (req->body_state == H2_READ_STATE_BEGIN){
		if (req->event_cb) 
		{
			req->event_cb(req, EV_HTTP_REQ_BODY_BEGIN, NULL, len,
								   req->event_cb_user);
		}
		if (flags & NGHTTP2_FLAG_END_STREAM)
		{
			req->h2_payload.flags = NGHTTP2_FLAG_NONE;
		}else
		{
			req->h2_payload.flags = flags;
		}
		req->body_state = H2_READ_STATE_READING;
	}
	if (req->body_state == H2_READ_STATE_READING)
	{
		if (req->event_cb)
		{
			req->event_cb(req, EV_HTTP_REQ_BODY_CONT, data, len,
								   req->event_cb_user);
		}
		if (flags & NGHTTP2_FLAG_END_STREAM){
			req->h2_payload.flags = NGHTTP2_FLAG_NONE;
		}else{
			req->h2_payload.flags = flags;
		}
	}
	if (uncompr_len)	FREE(&uncompr);

	stream_action = http2_submit_data_by_h2_half(h2_stream_info, h2_session, CONN_DIR_DOWNSTREAM, 1);
	if (stream_action == ACTION_DROP_DATA){
		xret = nghttp2_session_send(h2_stream_info->http2_client_handle);
		if (xret != 0)
		{
			stream_action = ACTION_FORWARD_DATA;
			TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s, %d\n",nghttp2_strerror(xret), __LINE__);
		}
	}
	if (stream_action == ACTION_USER_DATA)
		stream_action = ACTION_DROP_DATA;
	h2_stream_info->stream_action = stream_action;
finish:
	return 0;
}

static int http2_server_on_begin_headers(nghttp2_session *session, const nghttp2_frame *frame,void *user_data)
{
    struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data;

    if (frame->hd.type != NGHTTP2_HEADERS ||
        frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
        return 0;
    }
    create_serv_stream_data(session, frame->hd.stream_id, h2_stream_info);

    return 0;
}

static void server_session_init(struct tfe_h2_stream *h2_stream_info)
{
    nghttp2_session_callbacks *callbacks;

    nghttp2_session_callbacks_new(&callbacks);

    nghttp2_session_callbacks_set_send_callback(callbacks, http2_server_send);

    nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, http2_server_on_frame_recv);

    nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, http2_server_on_data_chunk_recv);

    nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, http2_server_on_stream_close);

    nghttp2_session_callbacks_set_on_header_callback(callbacks,http2_server_on_header);

    nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, http2_server_on_begin_headers);

    nghttp2_session_server_new(&h2_stream_info->http2_server_handle, callbacks, h2_stream_info);

	h2_stream_info->http2_server_handle->opt_flags |= NGHTTP2_OPTMASK_NO_CLOSED_STREAMS;

    nghttp2_session_callbacks_del(callbacks);
}

static void delete_server_session_data(struct tfe_h2_stream *h2_stream_info)
{
    struct tfe_h2_session *h2_session;
	struct tfe_h2_session *peer_h2_stream;

    nghttp2_session_del(h2_stream_info->http2_server_handle);
    h2_stream_info->http2_server_handle = NULL;

	TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream)
	{
		TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next);
		if (h2_session->frame_ctx){
			http_frame_raise_session_end(h2_session->frame_ctx, h2_stream_info->tf_stream, &h2_session->tfe_session,
										 h2_stream_info->thread_id);
			h2_session->frame_ctx = NULL;
		}
		delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1);
		free(h2_session);
		h2_session = NULL;
	}
}

static void delete_client_session_data(struct tfe_h2_stream *h2_stream_info)
{
    struct tfe_h2_session *h2_session = NULL;
	struct tfe_h2_session *peer_h2_stream;

    nghttp2_session_del(h2_stream_info->http2_client_handle);
    h2_stream_info->http2_client_handle = NULL;

	TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream){
		TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next);
		if (h2_session->frame_ctx){
			http_frame_raise_session_end(h2_session->frame_ctx, h2_stream_info->tf_stream, &h2_session->tfe_session,
										 h2_stream_info->thread_id);
			h2_session->frame_ctx = NULL;
		}
		delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1);
		free(h2_session);
		h2_session = NULL;
	}
}

enum tfe_stream_action detect_up_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream,
					      					unsigned int thread_id, const unsigned char *data, size_t len)
{
	int readlen = 0;
	enum tfe_stream_action stream_action = ACTION_FORWARD_DATA;

	h2_stream_info->tf_stream = tfe_stream;
	h2_stream_info->thread_id = thread_id;

	if (!h2_stream_info->http2_client_handle)
		goto forward;
	readlen = nghttp2_session_mem_recv(h2_stream_info->http2_client_handle, data, len);
	if (readlen < 0){
		TFE_LOG_ERROR(logger()->handle, "Failed to process server requests. Link message %s",
													 tfe_stream->str_stream_info);
		delete_client_session_data(h2_stream_info);
		goto forward;
	}
	stream_action = h2_stream_info->stream_action;
	h2_stream_info->stream_action = ACTION_DROP_DATA;
	if (h2_stream_info->goaway){
		http2_disect_goaway(h2_stream_info);
		h2_stream_info->goaway = 0;
	}
	if (h2_stream_info->kill_signal)
	{
		http2_disect_goaway(h2_stream_info);
		tfe_stream_kill(tfe_stream);
	}

	if (stream_action == ACTION_DROP_DATA){
		tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len));
		return ACTION_DROP_DATA;
	}

forward:
	if (stream_action == ACTION_FORWARD_DATA){
		tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_FOWARD_BYTES, &len, sizeof(len));
		return ACTION_FORWARD_DATA;
	}
	return stream_action;
}

enum tfe_stream_action detect_down_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream,
					      					unsigned int thread_id, const unsigned char *data, size_t len)
{
	int readlen = 0;
	enum tfe_stream_action stream_action = ACTION_FORWARD_DATA;

	h2_stream_info->tf_stream = tfe_stream;
	h2_stream_info->thread_id = thread_id;

	if (!h2_stream_info->http2_server_handle)
		goto forward;

	readlen = nghttp2_session_mem_recv(h2_stream_info->http2_server_handle, data, len);
	if (readlen < 0){
		TFE_LOG_ERROR(logger()->handle, "Failed to process client requests. Link message %s",
					 								 tfe_stream->str_stream_info);
	    delete_server_session_data(h2_stream_info);
		goto forward;
	}
	stream_action = h2_stream_info->stream_action;
	h2_stream_info->stream_action = ACTION_DROP_DATA;
	if (h2_stream_info->goaway){
		http2_disect_goaway(h2_stream_info);
		h2_stream_info->goaway = 0;
	}
	if (stream_action == ACTION_DROP_DATA){
		tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len));
		return ACTION_DROP_DATA;
	}
forward:
	if (stream_action == ACTION_FORWARD_DATA){
		tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_FOWARD_BYTES, &len, sizeof(len));
		return ACTION_FORWARD_DATA;
	}
	return stream_action;
}

void sess_data_ctx_fini(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream * stream, unsigned int thread_id)
{
	struct tfe_h2_session *h2_session  = NULL;
	struct tfe_h2_session *peer_h2_stream = NULL;

	TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream){
		TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next);
		if (h2_session->frame_ctx){
			http_frame_raise_session_end(h2_session->frame_ctx, stream, &h2_session->tfe_session,
										 thread_id);
			h2_session->frame_ctx = NULL;
		}
		delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1);
		free(h2_session);
		h2_session = NULL;
	}
	if (h2_stream_info->http2_client_handle){
		nghttp2_session_del(h2_stream_info->http2_client_handle);
		h2_stream_info->http2_client_handle = NULL;
	}
	if (h2_stream_info->http2_server_handle){
		nghttp2_session_del(h2_stream_info->http2_server_handle);
		h2_stream_info->http2_server_handle = NULL;
	}
}

struct tfe_h2_stream* tfe_session_info_init()
{
	struct tfe_h2_stream *h2_stream_info = NULL;

	h2_stream_info = ALLOC(struct tfe_h2_stream, 1);
	assert(h2_stream_info);

	memset(h2_stream_info, 0, sizeof(struct tfe_h2_stream));

	h2_stream_info->stream_action = ACTION_DROP_DATA;

	h2_stream_info->goaway = 0;

	server_session_init(h2_stream_info);

	client_session_init(h2_stream_info);

	TAILQ_INIT(&h2_stream_info->h2_session_list);

	return h2_stream_info;
}