summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-spdy.c
blob: d15ddcce65e52fa86180cd407415c5ca408b8c21 (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
/* packet-spdy.c
 * Routines for SPDY packet disassembly
 * For now, the protocol spec can be found at
 * http://dev.chromium.org/spdy/spdy-protocol
 *
 * Copyright 2010, Google Inc.
 * Hasan Khalil <hkhalil@google.com>
 * Chris Bentzel <cbentzel@google.com>
 * Eric Shienbrood <ers@google.com>
 *
 * Copyright 2013-2014
 * Alexis La Goutte <alexis.lagoutte@gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Originally based on packet-http.c
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/tap.h>
#include "packet-tcp.h"
#include "packet-tls.h"
#include "packet-media-type.h"

#if defined(HAVE_ZLIB) && !defined(HAVE_ZLIBNG)
#define ZLIB_CONST
#define ZLIB_PREFIX(x) x
#include <zlib.h>
typedef z_stream zlib_stream;
#endif /* HAVE_ZLIB */

#ifdef HAVE_ZLIBNG
#define ZLIB_PREFIX(x) zng_ ## x
#include <zlib-ng.h>
typedef zng_stream zlib_stream;
#endif /* HAVE_ZLIBNG */

void proto_register_spdy(void);
void proto_reg_handoff_spdy(void);

#define MIN_SPDY_VERSION 3

#define SPDY_STREAM_ID_MASK 0x7FFFFFFF

/*
 * Conversation data - used for assembling multi-data-frame
 * entities and for decompressing request & reply header blocks.
 */
typedef struct _spdy_conv_t {
#if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
#ifdef HAVE_ZLIBNG
  zng_streamp rqst_decompressor;
  zng_streamp rply_decompressor;
  uint32_t     dictionary_id;
#else
  z_streamp rqst_decompressor;
  z_streamp rply_decompressor;
  uLong     dictionary_id;
#endif
#endif
  wmem_tree_t  *streams;
} spdy_conv_t;


/* The types of SPDY frames */
#define SPDY_DATA           0
#define SPDY_SYN_STREAM     1
#define SPDY_SYN_REPLY      2
#define SPDY_RST_STREAM     3
#define SPDY_SETTINGS       4
#define SPDY_PING           6
#define SPDY_GOAWAY         7
#define SPDY_HEADERS        8
#define SPDY_WINDOW_UPDATE  9
#define SPDY_CREDENTIAL    10
#define SPDY_INVALID       11

#define SPDY_FLAG_FIN  0x01
#define SPDY_FLAG_UNIDIRECTIONAL 0x02
#define SPDY_FLAG_SETTINGS_CLEAR_SETTINGS 0x01

/* Flags for each setting in a SETTINGS frame. */
#define SPDY_FLAG_SETTINGS_PERSIST_VALUE 0x01
#define SPDY_FLAG_SETTINGS_PERSISTED 0x02

#define TCP_PORT_SPDY 6121

static const value_string frame_type_names[] = {
  { SPDY_DATA,          "DATA" },
  { SPDY_SYN_STREAM,    "SYN_STREAM" },
  { SPDY_SYN_REPLY,     "SYN_REPLY" },
  { SPDY_RST_STREAM,    "RST_STREAM" },
  { SPDY_SETTINGS,      "SETTINGS" },
  { SPDY_PING,          "PING" },
  { SPDY_GOAWAY,        "GOAWAY" },
  { SPDY_HEADERS,       "HEADERS" },
  { SPDY_WINDOW_UPDATE, "WINDOW_UPDATE" },
  { SPDY_CREDENTIAL,    "CREDENTIAL" },
  { SPDY_INVALID,       "INVALID" },
  { 0, NULL }
};

static const value_string rst_stream_status_names[] = {
  { 1,  "PROTOCOL_ERROR" },
  { 2,  "INVALID_STREAM" },
  { 3,  "REFUSED_STREAM" },
  { 4,  "UNSUPPORTED_VERSION" },
  { 5,  "CANCEL" },
  { 6,  "INTERNAL_ERROR" },
  { 7,  "FLOW_CONTROL_ERROR" },
  { 8,  "STREAM_IN_USE" },
  { 9,  "STREAM_ALREADY_CLOSED" },
  { 10, "INVALID_CREDENTIALS" },
  { 11, "FRAME_TOO_LARGE" },
  { 12, "INVALID" },
  { 0, NULL }
};

static const value_string setting_id_names[] = {
  { 1, "UPLOAD_BANDWIDTH" },
  { 2, "DOWNLOAD_BANDWIDTH" },
  { 3, "ROUND_TRIP_TIME" },
  { 4, "MAX_CONCURRENT_STREAMS" },
  { 5, "CURRENT_CWND" },
  { 6, "DOWNLOAD_RETRANS_RATE" },
  { 7, "INITIAL_WINDOW_SIZE" },
  { 0, NULL }
};

static const value_string goaway_status_names[] = {
  { 0,  "OK" },
  { 1,  "PROTOCOL_ERROR" },
  { 11, "INTERNAL_ERROR" },
  { 0, NULL }
};

/*
 * This structure will be tied to each SPDY frame and is used as an argument for
 * dissect_spdy_*_payload() functions.
 */
typedef struct _spdy_control_frame_info_t {
  bool control_bit;
  uint16_t version;
  uint16_t type;
  uint8_t  flags;
  uint32_t length;  /* Actually only 24 bits. */
} spdy_control_frame_info_t;

/*
 * This structure will be tied to each SPDY header frame.
 * Only applies to frames containing headers: SYN_STREAM, SYN_REPLY, HEADERS
 * Note that there may be multiple SPDY frames in one packet.
 */
typedef struct _spdy_header_info_t {
  uint32_t stream_id;
  uint8_t *header_block;
  unsigned   header_block_len;
  uint16_t frame_type;
} spdy_header_info_t;

static wmem_list_t *header_info_list;

/*
 * This structures keeps track of all the data frames
 * associated with a stream, so that they can be
 * reassembled into a single chunk.
 */
typedef struct _spdy_data_frame_t {
  uint8_t *data;
  uint32_t length;
  uint32_t framenum;
} spdy_data_frame_t;

typedef struct _spdy_stream_info_t {
  media_container_type_t container_type;
  char *content_type;
  char *content_type_parameters;
  char *content_encoding;
  wmem_list_t *data_frames;
  tvbuff_t *assembled_data;
  unsigned num_data_frames;
} spdy_stream_info_t;

/* Handles for metadata population. */

static int spdy_tap;
static int spdy_eo_tap;

static int proto_spdy;
static int hf_spdy_data;
static int hf_spdy_control_bit;
static int hf_spdy_version;
static int hf_spdy_type;
static int hf_spdy_flags;
static int hf_spdy_flags_fin;
static int hf_spdy_flags_unidirectional;
static int hf_spdy_flags_clear_settings;
static int hf_spdy_flags_persist_value;
static int hf_spdy_flags_persisted;
static int hf_spdy_length;
static int hf_spdy_header_block;
static int hf_spdy_header;
static int hf_spdy_header_name;
static int hf_spdy_header_value;
static int hf_spdy_streamid;
static int hf_spdy_associated_streamid;
static int hf_spdy_priority;
static int hf_spdy_unused;
static int hf_spdy_slot;
static int hf_spdy_num_headers;
static int hf_spdy_rst_stream_status;
static int hf_spdy_num_settings;
static int hf_spdy_setting;
static int hf_spdy_setting_id;
static int hf_spdy_setting_value;
static int hf_spdy_ping_id;
static int hf_spdy_goaway_last_good_stream_id;
static int hf_spdy_goaway_status;
static int hf_spdy_window_update_delta;

static int ett_spdy;
static int ett_spdy_flags;
static int ett_spdy_header_block;
static int ett_spdy_header;
static int ett_spdy_setting;

static int ett_spdy_encoded_entity;

static expert_field ei_spdy_inflation_failed;
static expert_field ei_spdy_mal_frame_data;
static expert_field ei_spdy_mal_setting_frame;
static expert_field ei_spdy_invalid_rst_stream;
static expert_field ei_spdy_invalid_go_away;
static expert_field ei_spdy_invalid_frame_type;
static expert_field ei_spdy_reassembly_info;

static dissector_handle_t media_handle;
static dissector_handle_t spdy_handle;
static dissector_table_t media_type_subdissector_table;
static dissector_table_t port_subdissector_table;

static bool spdy_assemble_entity_bodies = true;

/*
 * Decompression of zlib encoded entities.
 */
#if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
static bool spdy_decompress_body = true;
static bool spdy_decompress_headers = true;
#else
static bool spdy_decompress_body;
static bool spdy_decompress_headers;
#endif

#if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
static const char spdy_dictionary[] = {
  0x00, 0x00, 0x00, 0x07, 0x6f, 0x70, 0x74, 0x69,  /* - - - - o p t i */
  0x6f, 0x6e, 0x73, 0x00, 0x00, 0x00, 0x04, 0x68,  /* o n s - - - - h */
  0x65, 0x61, 0x64, 0x00, 0x00, 0x00, 0x04, 0x70,  /* e a d - - - - p */
  0x6f, 0x73, 0x74, 0x00, 0x00, 0x00, 0x03, 0x70,  /* o s t - - - - p */
  0x75, 0x74, 0x00, 0x00, 0x00, 0x06, 0x64, 0x65,  /* u t - - - - d e */
  0x6c, 0x65, 0x74, 0x65, 0x00, 0x00, 0x00, 0x05,  /* l e t e - - - - */
  0x74, 0x72, 0x61, 0x63, 0x65, 0x00, 0x00, 0x00,  /* t r a c e - - - */
  0x06, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x00,  /* - a c c e p t - */
  0x00, 0x00, 0x0e, 0x61, 0x63, 0x63, 0x65, 0x70,  /* - - - a c c e p */
  0x74, 0x2d, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65,  /* t - c h a r s e */
  0x74, 0x00, 0x00, 0x00, 0x0f, 0x61, 0x63, 0x63,  /* t - - - - a c c */
  0x65, 0x70, 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f,  /* e p t - e n c o */
  0x64, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x0f,  /* d i n g - - - - */
  0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x6c,  /* a c c e p t - l */
  0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x00,  /* a n g u a g e - */
  0x00, 0x00, 0x0d, 0x61, 0x63, 0x63, 0x65, 0x70,  /* - - - a c c e p */
  0x74, 0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x73,  /* t - r a n g e s */
  0x00, 0x00, 0x00, 0x03, 0x61, 0x67, 0x65, 0x00,  /* - - - - a g e - */
  0x00, 0x00, 0x05, 0x61, 0x6c, 0x6c, 0x6f, 0x77,  /* - - - a l l o w */
  0x00, 0x00, 0x00, 0x0d, 0x61, 0x75, 0x74, 0x68,  /* - - - - a u t h */
  0x6f, 0x72, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f,  /* o r i z a t i o */
  0x6e, 0x00, 0x00, 0x00, 0x0d, 0x63, 0x61, 0x63,  /* n - - - - c a c */
  0x68, 0x65, 0x2d, 0x63, 0x6f, 0x6e, 0x74, 0x72,  /* h e - c o n t r */
  0x6f, 0x6c, 0x00, 0x00, 0x00, 0x0a, 0x63, 0x6f,  /* o l - - - - c o */
  0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,  /* n n e c t i o n */
  0x00, 0x00, 0x00, 0x0c, 0x63, 0x6f, 0x6e, 0x74,  /* - - - - c o n t */
  0x65, 0x6e, 0x74, 0x2d, 0x62, 0x61, 0x73, 0x65,  /* e n t - b a s e */
  0x00, 0x00, 0x00, 0x10, 0x63, 0x6f, 0x6e, 0x74,  /* - - - - c o n t */
  0x65, 0x6e, 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f,  /* e n t - e n c o */
  0x64, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x10,  /* d i n g - - - - */
  0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,  /* c o n t e n t - */
  0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65,  /* l a n g u a g e */
  0x00, 0x00, 0x00, 0x0e, 0x63, 0x6f, 0x6e, 0x74,  /* - - - - c o n t */
  0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67,  /* e n t - l e n g */
  0x74, 0x68, 0x00, 0x00, 0x00, 0x10, 0x63, 0x6f,  /* t h - - - - c o */
  0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x6f,  /* n t e n t - l o */
  0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00,  /* c a t i o n - - */
  0x00, 0x0b, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,  /* - - c o n t e n */
  0x74, 0x2d, 0x6d, 0x64, 0x35, 0x00, 0x00, 0x00,  /* t - m d 5 - - - */
  0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,  /* - c o n t e n t */
  0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x00,  /* - r a n g e - - */
  0x00, 0x0c, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,  /* - - c o n t e n */
  0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x00, 0x00,  /* t - t y p e - - */
  0x00, 0x04, 0x64, 0x61, 0x74, 0x65, 0x00, 0x00,  /* - - d a t e - - */
  0x00, 0x04, 0x65, 0x74, 0x61, 0x67, 0x00, 0x00,  /* - - e t a g - - */
  0x00, 0x06, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74,  /* - - e x p e c t */
  0x00, 0x00, 0x00, 0x07, 0x65, 0x78, 0x70, 0x69,  /* - - - - e x p i */
  0x72, 0x65, 0x73, 0x00, 0x00, 0x00, 0x04, 0x66,  /* r e s - - - - f */
  0x72, 0x6f, 0x6d, 0x00, 0x00, 0x00, 0x04, 0x68,  /* r o m - - - - h */
  0x6f, 0x73, 0x74, 0x00, 0x00, 0x00, 0x08, 0x69,  /* o s t - - - - i */
  0x66, 0x2d, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x00,  /* f - m a t c h - */
  0x00, 0x00, 0x11, 0x69, 0x66, 0x2d, 0x6d, 0x6f,  /* - - - i f - m o */
  0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x2d, 0x73,  /* d i f i e d - s */
  0x69, 0x6e, 0x63, 0x65, 0x00, 0x00, 0x00, 0x0d,  /* i n c e - - - - */
  0x69, 0x66, 0x2d, 0x6e, 0x6f, 0x6e, 0x65, 0x2d,  /* i f - n o n e - */
  0x6d, 0x61, 0x74, 0x63, 0x68, 0x00, 0x00, 0x00,  /* m a t c h - - - */
  0x08, 0x69, 0x66, 0x2d, 0x72, 0x61, 0x6e, 0x67,  /* - i f - r a n g */
  0x65, 0x00, 0x00, 0x00, 0x13, 0x69, 0x66, 0x2d,  /* e - - - - i f - */
  0x75, 0x6e, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69,  /* u n m o d i f i */
  0x65, 0x64, 0x2d, 0x73, 0x69, 0x6e, 0x63, 0x65,  /* e d - s i n c e */
  0x00, 0x00, 0x00, 0x0d, 0x6c, 0x61, 0x73, 0x74,  /* - - - - l a s t */
  0x2d, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65,  /* - m o d i f i e */
  0x64, 0x00, 0x00, 0x00, 0x08, 0x6c, 0x6f, 0x63,  /* d - - - - l o c */
  0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00,  /* a t i o n - - - */
  0x0c, 0x6d, 0x61, 0x78, 0x2d, 0x66, 0x6f, 0x72,  /* - m a x - f o r */
  0x77, 0x61, 0x72, 0x64, 0x73, 0x00, 0x00, 0x00,  /* w a r d s - - - */
  0x06, 0x70, 0x72, 0x61, 0x67, 0x6d, 0x61, 0x00,  /* - p r a g m a - */
  0x00, 0x00, 0x12, 0x70, 0x72, 0x6f, 0x78, 0x79,  /* - - - p r o x y */
  0x2d, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,  /* - a u t h e n t */
  0x69, 0x63, 0x61, 0x74, 0x65, 0x00, 0x00, 0x00,  /* i c a t e - - - */
  0x13, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2d, 0x61,  /* - p r o x y - a */
  0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x61,  /* u t h o r i z a */
  0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x05,  /* t i o n - - - - */
  0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x00, 0x00,  /* r a n g e - - - */
  0x07, 0x72, 0x65, 0x66, 0x65, 0x72, 0x65, 0x72,  /* - r e f e r e r */
  0x00, 0x00, 0x00, 0x0b, 0x72, 0x65, 0x74, 0x72,  /* - - - - r e t r */
  0x79, 0x2d, 0x61, 0x66, 0x74, 0x65, 0x72, 0x00,  /* y - a f t e r - */
  0x00, 0x00, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65,  /* - - - s e r v e */
  0x72, 0x00, 0x00, 0x00, 0x02, 0x74, 0x65, 0x00,  /* r - - - - t e - */
  0x00, 0x00, 0x07, 0x74, 0x72, 0x61, 0x69, 0x6c,  /* - - - t r a i l */
  0x65, 0x72, 0x00, 0x00, 0x00, 0x11, 0x74, 0x72,  /* e r - - - - t r */
  0x61, 0x6e, 0x73, 0x66, 0x65, 0x72, 0x2d, 0x65,  /* a n s f e r - e */
  0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x00,  /* n c o d i n g - */
  0x00, 0x00, 0x07, 0x75, 0x70, 0x67, 0x72, 0x61,  /* - - - u p g r a */
  0x64, 0x65, 0x00, 0x00, 0x00, 0x0a, 0x75, 0x73,  /* d e - - - - u s */
  0x65, 0x72, 0x2d, 0x61, 0x67, 0x65, 0x6e, 0x74,  /* e r - a g e n t */
  0x00, 0x00, 0x00, 0x04, 0x76, 0x61, 0x72, 0x79,  /* - - - - v a r y */
  0x00, 0x00, 0x00, 0x03, 0x76, 0x69, 0x61, 0x00,  /* - - - - v i a - */
  0x00, 0x00, 0x07, 0x77, 0x61, 0x72, 0x6e, 0x69,  /* - - - w a r n i */
  0x6e, 0x67, 0x00, 0x00, 0x00, 0x10, 0x77, 0x77,  /* n g - - - - w w */
  0x77, 0x2d, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e,  /* w - a u t h e n */
  0x74, 0x69, 0x63, 0x61, 0x74, 0x65, 0x00, 0x00,  /* t i c a t e - - */
  0x00, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,  /* - - m e t h o d */
  0x00, 0x00, 0x00, 0x03, 0x67, 0x65, 0x74, 0x00,  /* - - - - g e t - */
  0x00, 0x00, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75,  /* - - - s t a t u */
  0x73, 0x00, 0x00, 0x00, 0x06, 0x32, 0x30, 0x30,  /* s - - - - 2 0 0 */
  0x20, 0x4f, 0x4b, 0x00, 0x00, 0x00, 0x07, 0x76,  /* - O K - - - - v */
  0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x00, 0x00,  /* e r s i o n - - */
  0x00, 0x08, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31,  /* - - H T T P - 1 */
  0x2e, 0x31, 0x00, 0x00, 0x00, 0x03, 0x75, 0x72,  /* - 1 - - - - u r */
  0x6c, 0x00, 0x00, 0x00, 0x06, 0x70, 0x75, 0x62,  /* l - - - - p u b */
  0x6c, 0x69, 0x63, 0x00, 0x00, 0x00, 0x0a, 0x73,  /* l i c - - - - s */
  0x65, 0x74, 0x2d, 0x63, 0x6f, 0x6f, 0x6b, 0x69,  /* e t - c o o k i */
  0x65, 0x00, 0x00, 0x00, 0x0a, 0x6b, 0x65, 0x65,  /* e - - - - k e e */
  0x70, 0x2d, 0x61, 0x6c, 0x69, 0x76, 0x65, 0x00,  /* p - a l i v e - */
  0x00, 0x00, 0x06, 0x6f, 0x72, 0x69, 0x67, 0x69,  /* - - - o r i g i */
  0x6e, 0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x32,  /* n 1 0 0 1 0 1 2 */
  0x30, 0x31, 0x32, 0x30, 0x32, 0x32, 0x30, 0x35,  /* 0 1 2 0 2 2 0 5 */
  0x32, 0x30, 0x36, 0x33, 0x30, 0x30, 0x33, 0x30,  /* 2 0 6 3 0 0 3 0 */
  0x32, 0x33, 0x30, 0x33, 0x33, 0x30, 0x34, 0x33,  /* 2 3 0 3 3 0 4 3 */
  0x30, 0x35, 0x33, 0x30, 0x36, 0x33, 0x30, 0x37,  /* 0 5 3 0 6 3 0 7 */
  0x34, 0x30, 0x32, 0x34, 0x30, 0x35, 0x34, 0x30,  /* 4 0 2 4 0 5 4 0 */
  0x36, 0x34, 0x30, 0x37, 0x34, 0x30, 0x38, 0x34,  /* 6 4 0 7 4 0 8 4 */
  0x30, 0x39, 0x34, 0x31, 0x30, 0x34, 0x31, 0x31,  /* 0 9 4 1 0 4 1 1 */
  0x34, 0x31, 0x32, 0x34, 0x31, 0x33, 0x34, 0x31,  /* 4 1 2 4 1 3 4 1 */
  0x34, 0x34, 0x31, 0x35, 0x34, 0x31, 0x36, 0x34,  /* 4 4 1 5 4 1 6 4 */
  0x31, 0x37, 0x35, 0x30, 0x32, 0x35, 0x30, 0x34,  /* 1 7 5 0 2 5 0 4 */
  0x35, 0x30, 0x35, 0x32, 0x30, 0x33, 0x20, 0x4e,  /* 5 0 5 2 0 3 - N */
  0x6f, 0x6e, 0x2d, 0x41, 0x75, 0x74, 0x68, 0x6f,  /* o n - A u t h o */
  0x72, 0x69, 0x74, 0x61, 0x74, 0x69, 0x76, 0x65,  /* r i t a t i v e */
  0x20, 0x49, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61,  /* - I n f o r m a */
  0x74, 0x69, 0x6f, 0x6e, 0x32, 0x30, 0x34, 0x20,  /* t i o n 2 0 4 - */
  0x4e, 0x6f, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65,  /* N o - C o n t e */
  0x6e, 0x74, 0x33, 0x30, 0x31, 0x20, 0x4d, 0x6f,  /* n t 3 0 1 - M o */
  0x76, 0x65, 0x64, 0x20, 0x50, 0x65, 0x72, 0x6d,  /* v e d - P e r m */
  0x61, 0x6e, 0x65, 0x6e, 0x74, 0x6c, 0x79, 0x34,  /* a n e n t l y 4 */
  0x30, 0x30, 0x20, 0x42, 0x61, 0x64, 0x20, 0x52,  /* 0 0 - B a d - R */
  0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x34, 0x30,  /* e q u e s t 4 0 */
  0x31, 0x20, 0x55, 0x6e, 0x61, 0x75, 0x74, 0x68,  /* 1 - U n a u t h */
  0x6f, 0x72, 0x69, 0x7a, 0x65, 0x64, 0x34, 0x30,  /* o r i z e d 4 0 */
  0x33, 0x20, 0x46, 0x6f, 0x72, 0x62, 0x69, 0x64,  /* 3 - F o r b i d */
  0x64, 0x65, 0x6e, 0x34, 0x30, 0x34, 0x20, 0x4e,  /* d e n 4 0 4 - N */
  0x6f, 0x74, 0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64,  /* o t - F o u n d */
  0x35, 0x30, 0x30, 0x20, 0x49, 0x6e, 0x74, 0x65,  /* 5 0 0 - I n t e */
  0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72,  /* r n a l - S e r */
  0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f,  /* v e r - E r r o */
  0x72, 0x35, 0x30, 0x31, 0x20, 0x4e, 0x6f, 0x74,  /* r 5 0 1 - N o t */
  0x20, 0x49, 0x6d, 0x70, 0x6c, 0x65, 0x6d, 0x65,  /* - I m p l e m e */
  0x6e, 0x74, 0x65, 0x64, 0x35, 0x30, 0x33, 0x20,  /* n t e d 5 0 3 - */
  0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20,  /* S e r v i c e - */
  0x55, 0x6e, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61,  /* U n a v a i l a */
  0x62, 0x6c, 0x65, 0x4a, 0x61, 0x6e, 0x20, 0x46,  /* b l e J a n - F */
  0x65, 0x62, 0x20, 0x4d, 0x61, 0x72, 0x20, 0x41,  /* e b - M a r - A */
  0x70, 0x72, 0x20, 0x4d, 0x61, 0x79, 0x20, 0x4a,  /* p r - M a y - J */
  0x75, 0x6e, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x41,  /* u n - J u l - A */
  0x75, 0x67, 0x20, 0x53, 0x65, 0x70, 0x74, 0x20,  /* u g - S e p t - */
  0x4f, 0x63, 0x74, 0x20, 0x4e, 0x6f, 0x76, 0x20,  /* O c t - N o v - */
  0x44, 0x65, 0x63, 0x20, 0x30, 0x30, 0x3a, 0x30,  /* D e c - 0 0 - 0 */
  0x30, 0x3a, 0x30, 0x30, 0x20, 0x4d, 0x6f, 0x6e,  /* 0 - 0 0 - M o n */
  0x2c, 0x20, 0x54, 0x75, 0x65, 0x2c, 0x20, 0x57,  /* - - T u e - - W */
  0x65, 0x64, 0x2c, 0x20, 0x54, 0x68, 0x75, 0x2c,  /* e d - - T h u - */
  0x20, 0x46, 0x72, 0x69, 0x2c, 0x20, 0x53, 0x61,  /* - F r i - - S a */
  0x74, 0x2c, 0x20, 0x53, 0x75, 0x6e, 0x2c, 0x20,  /* t - - S u n - - */
  0x47, 0x4d, 0x54, 0x63, 0x68, 0x75, 0x6e, 0x6b,  /* G M T c h u n k */
  0x65, 0x64, 0x2c, 0x74, 0x65, 0x78, 0x74, 0x2f,  /* e d - t e x t - */
  0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x69, 0x6d, 0x61,  /* h t m l - i m a */
  0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x2c, 0x69,  /* g e - p n g - i */
  0x6d, 0x61, 0x67, 0x65, 0x2f, 0x6a, 0x70, 0x67,  /* m a g e - j p g */
  0x2c, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x67,  /* - i m a g e - g */
  0x69, 0x66, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69,  /* i f - a p p l i */
  0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78,  /* c a t i o n - x */
  0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69,  /* m l - a p p l i */
  0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78,  /* c a t i o n - x */
  0x68, 0x74, 0x6d, 0x6c, 0x2b, 0x78, 0x6d, 0x6c,  /* h t m l - x m l */
  0x2c, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c,  /* - t e x t - p l */
  0x61, 0x69, 0x6e, 0x2c, 0x74, 0x65, 0x78, 0x74,  /* a i n - t e x t */
  0x2f, 0x6a, 0x61, 0x76, 0x61, 0x73, 0x63, 0x72,  /* - j a v a s c r */
  0x69, 0x70, 0x74, 0x2c, 0x70, 0x75, 0x62, 0x6c,  /* i p t - p u b l */
  0x69, 0x63, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74,  /* i c p r i v a t */
  0x65, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65,  /* e m a x - a g e */
  0x3d, 0x67, 0x7a, 0x69, 0x70, 0x2c, 0x64, 0x65,  /* - g z i p - d e */
  0x66, 0x6c, 0x61, 0x74, 0x65, 0x2c, 0x73, 0x64,  /* f l a t e - s d */
  0x63, 0x68, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65,  /* c h c h a r s e */
  0x74, 0x3d, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x63,  /* t - u t f - 8 c */
  0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x69,  /* h a r s e t - i */
  0x73, 0x6f, 0x2d, 0x38, 0x38, 0x35, 0x39, 0x2d,  /* s o - 8 8 5 9 - */
  0x31, 0x2c, 0x75, 0x74, 0x66, 0x2d, 0x2c, 0x2a,  /* 1 - u t f - - - */
  0x2c, 0x65, 0x6e, 0x71, 0x3d, 0x30, 0x2e         /* - e n q - 0 -   */
};

/* callback function used at the end of file-scope to cleanup zlib's inflate
 * streams to avoid memory leaks.
 * XXX: can we be more aggressive and call this sooner for finished streams?
 */
static bool inflate_end_cb (wmem_allocator_t *allocator _U_,
    wmem_cb_event_t event _U_, void *user_data) {
#ifdef HAVE_ZLIBNG
  ZLIB_PREFIX(inflateEnd)((zng_streamp)user_data);
#else
  ZLIB_PREFIX(inflateEnd)((z_streamp)user_data);
#endif
  return false;
}
#endif

/*
 * Protocol initialization
 */
static void
spdy_init_protocol(void)
{
   header_info_list = NULL;
}

/*
 * Returns conversation data for a given packet. If conversation data can't be
 * found, creates and returns new conversation data.
 */
static spdy_conv_t * get_or_create_spdy_conversation_data(packet_info *pinfo) {
  conversation_t  *conversation;
  spdy_conv_t *conv_data;
#if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
  int retcode;
#endif

  conversation = find_or_create_conversation(pinfo);

  /* Retrieve information from conversation */
  conv_data = (spdy_conv_t *)conversation_get_proto_data(conversation, proto_spdy);
  if (!conv_data) {
    /* Set up the conversation structure itself */
    conv_data = wmem_new0(wmem_file_scope(), spdy_conv_t);

    conv_data->streams = NULL;
    if (spdy_decompress_headers) {
#if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
      conv_data->rqst_decompressor = wmem_new0(wmem_file_scope(), zlib_stream);
      conv_data->rply_decompressor = wmem_new0(wmem_file_scope(), zlib_stream);
      retcode = ZLIB_PREFIX(inflateInit)(conv_data->rqst_decompressor);
      if (retcode == Z_OK) {
        wmem_register_callback(wmem_file_scope(), inflate_end_cb,
            conv_data->rqst_decompressor);
        retcode = ZLIB_PREFIX(inflateInit)(conv_data->rply_decompressor);
        if (retcode == Z_OK) {
          wmem_register_callback(wmem_file_scope(), inflate_end_cb,
              conv_data->rply_decompressor);
        }
      }

      /* XXX - use wsutil/adler32.h? */
      conv_data->dictionary_id = ZLIB_PREFIX(adler32)(0L, Z_NULL, 0);
      conv_data->dictionary_id = ZLIB_PREFIX(adler32)(conv_data->dictionary_id,
                                         spdy_dictionary,
                                         (uInt)sizeof(spdy_dictionary));
#endif
    }

    conversation_add_proto_data(conversation, proto_spdy, conv_data);
  }

  return conv_data;
}

/*
 * Retains state on a given stream.
 */
static void spdy_save_stream_info(spdy_conv_t *conv_data,
                                  uint32_t stream_id,
                                  media_container_type_t container_type,
                                  char *content_type,
                                  char *content_type_params,
                                  char *content_encoding) {
  spdy_stream_info_t *si;

  if (conv_data->streams == NULL) {
    conv_data->streams = wmem_tree_new(wmem_file_scope());
  }

  si = wmem_new(wmem_file_scope(), spdy_stream_info_t);
  si->container_type = container_type;
  si->content_type = content_type;
  si->content_type_parameters = content_type_params;
  si->content_encoding = content_encoding;
  si->data_frames = wmem_list_new(wmem_file_scope());
  si->num_data_frames = 0;
  si->assembled_data = NULL;
  wmem_tree_insert32(conv_data->streams, stream_id, si);
}

/*
 * Retrieves previously saved state on a given stream.
 */
static spdy_stream_info_t* spdy_get_stream_info(spdy_conv_t *conv_data,
                                                uint32_t stream_id)
{
  if (conv_data->streams == NULL)
    return NULL;

  return (spdy_stream_info_t*)wmem_tree_lookup32(conv_data->streams, stream_id);
}

/*
 * Adds a data chunk to a given SPDY conversation/stream.
 */
static void spdy_add_data_chunk(spdy_conv_t *conv_data,
                                uint32_t stream_id,
                                uint32_t frame,
                                uint8_t *data,
                                uint32_t length)
{
  spdy_stream_info_t *si = spdy_get_stream_info(conv_data, stream_id);

  if (si != NULL) {
    spdy_data_frame_t *df = (spdy_data_frame_t *)wmem_new(wmem_file_scope(), spdy_data_frame_t);
    df->data = data;
    df->length = length;
    df->framenum = frame;
    wmem_list_append(si->data_frames, df);
    ++si->num_data_frames;
  }
}

/*
 * Increment the count of DATA frames found on a given stream.
 */
static void spdy_increment_data_chunk_count(spdy_conv_t *conv_data,
                                            uint32_t stream_id) {
  spdy_stream_info_t *si = spdy_get_stream_info(conv_data, stream_id);
  if (si != NULL) {
    ++si->num_data_frames;
  }
}

/*
 * Return the number of data frames saved so far for the specified stream.
 */
static unsigned spdy_get_num_data_frames(spdy_conv_t *conv_data,
                                      uint32_t stream_id) {
  spdy_stream_info_t *si = spdy_get_stream_info(conv_data, stream_id);

  return si == NULL ? 0 : si->num_data_frames;
}

/*
 * Reassembles DATA frames for a given stream into one tvb.
 */
static spdy_stream_info_t* spdy_assemble_data_frames(spdy_conv_t *conv_data,
                                                     uint32_t stream_id) {
  spdy_stream_info_t *si = spdy_get_stream_info(conv_data, stream_id);
  tvbuff_t *tvb;

  if (si == NULL) {
    return NULL;
  }

  /*
   * Compute the total amount of data and concatenate the
   * data chunks, if it hasn't already been done.
   */
  if (si->assembled_data == NULL) {
    spdy_data_frame_t *df;
    uint8_t *data;
    uint32_t datalen;
    uint32_t offset;
    wmem_list_t *dflist = si->data_frames;
    wmem_list_frame_t *frame;
    if (wmem_list_count(dflist) == 0) {
      return si;
    }
    datalen = 0;
    /*
     * It'd be nice to use a composite tvbuff here, but since
     * only a real-data tvbuff can be the child of another
     * tvb, we can't. It would be nice if this limitation
     * could be fixed.
     */
    frame = wmem_list_frame_next(wmem_list_head(dflist));
    while (frame != NULL) {
      df = (spdy_data_frame_t *)wmem_list_frame_data(frame);
      datalen += df->length;
      frame = wmem_list_frame_next(frame);
    }
    if (datalen != 0) {
      data = (uint8_t *)wmem_alloc(wmem_file_scope(), datalen);
      dflist = si->data_frames;
      offset = 0;
      frame = wmem_list_frame_next(wmem_list_head(dflist));
      while (frame != NULL) {
        df = (spdy_data_frame_t *)wmem_list_frame_data(frame);
        memcpy(data+offset, df->data, df->length);
        offset += df->length;
        frame = wmem_list_frame_next(frame);
      }
      tvb = tvb_new_real_data(data, datalen, datalen);
      si->assembled_data = tvb;
    }
  }
  return si;
}

/*
 * Same as dissect_spdy_stream_id below, except with explicit field index.
 */
static void dissect_spdy_stream_id_field(tvbuff_t *tvb,
                                         int offset,
                                         packet_info *pinfo _U_,
                                         proto_tree *frame_tree,
                                         const int hfindex)
{
  uint32_t stream_id = tvb_get_ntohl(tvb, offset) & SPDY_STREAM_ID_MASK;

  /* Add stream id to tree. */
  proto_tree_add_item(frame_tree, hfindex, tvb, offset, 4, ENC_BIG_ENDIAN);

  if (hfindex == hf_spdy_streamid) {
    proto_item_append_text(frame_tree, ", Stream: %u", stream_id);
  }
}

/*
 * Adds flag details to proto tree.
 */
static void dissect_spdy_flags(tvbuff_t *tvb,
                               int offset,
                               proto_tree *frame_tree,
                               const spdy_control_frame_info_t *frame) {
  proto_item *flags_ti;
  proto_tree *flags_tree;

  /* Create flags substree. */
  flags_ti = proto_tree_add_item(frame_tree, hf_spdy_flags, tvb, offset, 1, ENC_BIG_ENDIAN);
  flags_tree = proto_item_add_subtree(flags_ti, ett_spdy_flags);

  /* Add FIN flag for appropriate frames. */
  if (frame->type == SPDY_DATA ||
      frame->type == SPDY_SYN_STREAM ||
      frame->type == SPDY_SYN_REPLY ||
      frame->type == SPDY_HEADERS) {
    /* Add FIN flag. */
    proto_tree_add_item(flags_tree, hf_spdy_flags_fin, tvb, offset, 1, ENC_BIG_ENDIAN);
    if (frame->flags & SPDY_FLAG_FIN) {
      proto_item_append_text(frame_tree, " (FIN)");
      proto_item_append_text(flags_ti, " (FIN)");
    }
  }

  /* Add UNIDIRECTIONAL flag, only applicable for SYN_STREAM. */
  if (frame->type == SPDY_SYN_STREAM) {
    proto_tree_add_item(flags_tree, hf_spdy_flags_unidirectional, tvb,
                        offset, 1, ENC_BIG_ENDIAN);
    if (frame->flags & SPDY_FLAG_UNIDIRECTIONAL) {
      proto_item_append_text(flags_ti, " (UNIDIRECTIONAL)");
    }
  }

  /* Add CLEAR_SETTINGS flag, only applicable for SETTINGS. */
  if (frame->type == SPDY_SETTINGS) {
    proto_tree_add_item(flags_tree, hf_spdy_flags_clear_settings, tvb,
                        offset, 1, ENC_BIG_ENDIAN);
    if (frame->flags & SPDY_FLAG_SETTINGS_CLEAR_SETTINGS) {
      proto_item_append_text(flags_ti, " (CLEAR)");
    }
  }
}

/*
 * Performs DATA frame payload dissection.
 */
static int dissect_spdy_data_payload(tvbuff_t *tvb,
                                     int offset,
                                     packet_info *pinfo,
                                     proto_tree *top_level_tree _U_,
                                     proto_tree *spdy_tree,
                                     proto_item *spdy_proto,
                                     spdy_conv_t *conv_data,
                                     uint32_t stream_id,
                                     const spdy_control_frame_info_t *frame)
{
  dissector_handle_t handle;
  unsigned num_data_frames;
  bool dissected;
  media_content_info_t content_info;

  /* Add frame description. */
  proto_item_append_text(spdy_proto, ", Stream: %d, Length: %d",
                           stream_id,
                           frame->length);

  /* Add data. */
  proto_tree_add_item(spdy_tree, hf_spdy_data, tvb, offset, frame->length, ENC_NA);

  num_data_frames = spdy_get_num_data_frames(conv_data, stream_id);
  if (frame->length != 0 || num_data_frames != 0) {
    /*
     * There's stuff left over; process it.
     */
    tvbuff_t *next_tvb = NULL;
    tvbuff_t    *data_tvb = NULL;
    spdy_stream_info_t *si = NULL;
    uint8_t *copied_data;
    bool is_single_chunk = false;
    bool have_entire_body;
    char *media_str = NULL;

    /*
     * Create a tvbuff for the payload.
     */
    if (frame->length != 0) {
      next_tvb = tvb_new_subset_length(tvb, offset, frame->length);
      is_single_chunk = num_data_frames == 0 &&
          (frame->flags & SPDY_FLAG_FIN) != 0;
      if (!pinfo->fd->visited) {
        if (!is_single_chunk) {
          if (spdy_assemble_entity_bodies) {
            copied_data = (uint8_t *)tvb_memdup(wmem_file_scope(),next_tvb, 0, frame->length);
            spdy_add_data_chunk(conv_data, stream_id, pinfo->num, copied_data, frame->length);
          } else {
            spdy_increment_data_chunk_count(conv_data, stream_id);
          }
        }
      }
    } else {
      is_single_chunk = (num_data_frames == 1);
    }

    if (!(frame->flags & SPDY_FLAG_FIN)) {
      col_set_fence(pinfo->cinfo, COL_INFO);
      proto_item_append_text(spdy_proto, " (partial entity body)");
      /* would like the proto item to say */
      /* " (entity body fragment N of M)" */
      goto body_dissected;
    }
    have_entire_body = is_single_chunk;
    /*
     * On seeing the last data frame in a stream, we can
     * reassemble the frames into one data block.
     */
    si = spdy_assemble_data_frames(conv_data, stream_id);
    if (si == NULL) {
      goto body_dissected;
    }
    data_tvb = si->assembled_data;
    if (spdy_assemble_entity_bodies) {
      have_entire_body = true;
    }

    if (!have_entire_body) {
      goto body_dissected;
    }

    if (data_tvb == NULL) {
      if (next_tvb == NULL)
        goto body_dissected;
      data_tvb = next_tvb;
    } else {
      add_new_data_source(pinfo, data_tvb, "Assembled entity body");
    }

    if (have_entire_body && si->content_encoding != NULL &&
      g_ascii_strcasecmp(si->content_encoding, "identity") != 0) {
      /*
       * We currently can't handle, for example, "compress";
       * just handle them as data for now.
       *
       * After July 7, 2004 the LZW patent expires, so support
       * might be added then.  However, I don't think that
       * anybody ever really implemented "compress", due to
       * the aforementioned patent.
       */
      tvbuff_t *uncomp_tvb = NULL;
      proto_item *e_ti = NULL;
      proto_tree *e_tree = NULL;

      if (spdy_decompress_body &&
          (g_ascii_strcasecmp(si->content_encoding, "gzip") == 0 ||
           g_ascii_strcasecmp(si->content_encoding, "deflate") == 0)) {
        uncomp_tvb = tvb_child_uncompress_zlib(tvb, data_tvb, 0,
                                               tvb_reported_length(data_tvb));
      }
      /*
       * Add the encoded entity to the protocol tree
       */
      e_tree = proto_tree_add_subtree_format(spdy_tree, data_tvb,
                                 0, tvb_reported_length(data_tvb), ett_spdy_encoded_entity, &e_ti,
                                 "Content-encoded entity body (%s): %u bytes",
                                 si->content_encoding,
                                 tvb_reported_length(data_tvb));
      if (si->num_data_frames > 1) {
        wmem_list_t *dflist = si->data_frames;
        wmem_list_frame_t *frame_item;
        spdy_data_frame_t *df;
        uint32_t framenum = 0;
        wmem_strbuf_t *str_frames = wmem_strbuf_new(pinfo->pool, "");

        frame_item = wmem_list_frame_next(wmem_list_head(dflist));
        while (frame_item != NULL) {
          df = (spdy_data_frame_t *)wmem_list_frame_data(frame_item);
          if (framenum != df->framenum) {
            wmem_strbuf_append_printf(str_frames, " #%u", df->framenum);
            framenum = df->framenum;
          }
          frame_item = wmem_list_frame_next(frame_item);
        }

        proto_tree_add_expert_format(e_tree, pinfo, &ei_spdy_reassembly_info, data_tvb, 0,
                                    tvb_reported_length(data_tvb),
                                    "Assembled from %d frames in packet(s)%s",
                                    si->num_data_frames, wmem_strbuf_get_str(str_frames));
      }

      if (uncomp_tvb != NULL) {
        /*
         * Decompression worked
         */

        /* XXX - Don't free this, since it's possible
         * that the data was only partially
         * decompressed, such as when desegmentation
         * isn't enabled.
         *
         tvb_free(next_tvb);
         */
        proto_item_append_text(e_ti, " -> %u bytes", tvb_reported_length(uncomp_tvb));
        data_tvb = uncomp_tvb;
        add_new_data_source(pinfo, data_tvb, "Uncompressed entity body");
      } else {
        if (spdy_decompress_body) {
          proto_item_append_text(e_ti, " [Error: Decompression failed]");
        }
        call_data_dissector(data_tvb, pinfo, e_tree);

        goto body_dissected;
      }
    }

    /*
     * Do subdissector checks.
     *
     * First, check whether some subdissector asked that they
     * be called if something was on some particular port.
     */

    if (have_entire_body && port_subdissector_table != NULL) {
      handle = dissector_get_uint_handle(port_subdissector_table,
                                         pinfo->match_uint);
    } else {
      handle = NULL;
    }
    if (handle == NULL && have_entire_body && si->content_type != NULL &&
      media_type_subdissector_table != NULL) {
      /*
       * We didn't find any subdissector that
       * registered for the port, and we have a
       * Content-Type value.  Is there any subdissector
       * for that content type?
       */
      if (si->content_type_parameters) {
        media_str = wmem_strdup(pinfo->pool, si->content_type_parameters);
      }
      /*
       * Calling the string handle for the media type
       * dissector table will set pinfo->match_string
       * to si->content_type for us.
       */
      pinfo->match_string = si->content_type;
      handle = dissector_get_string_handle(media_type_subdissector_table,
                                           si->content_type);
    }
    content_info.type = si->container_type;
    content_info.media_str = media_str;
    content_info.data = NULL;
    if (handle != NULL) {
      /*
       * We have a subdissector - call it.
       */
      dissected = call_dissector_with_data(handle, data_tvb, pinfo, spdy_tree, &content_info);
    } else {
      dissected = false;
    }

    if (!dissected && have_entire_body && si->content_type != NULL) {
      /*
       * Calling the default media handle if there is a content-type that
       * wasn't handled above.
       */
      call_dissector_with_data(media_handle, next_tvb, pinfo, spdy_tree, &content_info);
    } else {
      /* Call the default data dissector */
      call_data_dissector(next_tvb, pinfo, spdy_tree);
    }

body_dissected:
    /*
     * We've processed frame->length bytes worth of data
     * (which may be no data at all); advance the
     * offset past whatever data we've processed.
     */
    ;
  }
  return frame->length;
}

#if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
/*
 * Performs header decompression.
 *
 * The returned buffer is automatically scoped to the lifetime of the capture
 * (via wmem_memdup() with file scope).
 */
#define DECOMPRESS_BUFSIZE   16384

static uint8_t* spdy_decompress_header_block(tvbuff_t *tvb,
                                            packet_info *pinfo,
#ifdef HAVE_ZLIBNG
                                            zng_streamp decomp,
#else
                                            z_streamp decomp,
#endif
                                            uLong dictionary_id,
                                            int offset,
                                            uint32_t length,
                                            unsigned *uncomp_length) {
  int retcode;
  const uint8_t *hptr = tvb_get_ptr(tvb, offset, length);
  uint8_t *uncomp_block = (uint8_t *)wmem_alloc(pinfo->pool, DECOMPRESS_BUFSIZE);

#ifdef z_const
  decomp->next_in = (z_const Bytef *)hptr;
#else
DIAG_OFF(cast-qual)
  decomp->next_in = (Bytef *)hptr;
DIAG_ON(cast-qual)
#endif
  decomp->avail_in = length;
  decomp->next_out = uncomp_block;
  decomp->avail_out = DECOMPRESS_BUFSIZE;
  retcode = ZLIB_PREFIX(inflate)(decomp, Z_SYNC_FLUSH);
  if (retcode == Z_NEED_DICT) {
    if (decomp->adler == dictionary_id) {
      retcode = ZLIB_PREFIX(inflateSetDictionary)(decomp,
                                     spdy_dictionary,
                                     sizeof(spdy_dictionary));
      if (retcode == Z_OK) {
        retcode = ZLIB_PREFIX(inflate)(decomp, Z_SYNC_FLUSH);
      }
    }
  }

  /* Handle errors. */
  if (retcode != Z_OK) {
    return NULL;
  }

  /* Handle successful inflation. */
  *uncomp_length = DECOMPRESS_BUFSIZE - decomp->avail_out;

  return (uint8_t *)wmem_memdup(wmem_file_scope(), uncomp_block, *uncomp_length);
}
#endif


/*
 * Saves state on header data for a given stream.
 */
static spdy_header_info_t* spdy_save_header_block(packet_info *pinfo _U_,
                                                  uint32_t stream_id,
                                                  uint16_t frame_type,
                                                  uint8_t *header,
                                                  unsigned length) {
  spdy_header_info_t *header_info;

  if (header_info_list == NULL)
    header_info_list = wmem_list_new(wmem_file_scope());

  header_info = wmem_new(wmem_file_scope(), spdy_header_info_t);
  header_info->stream_id = stream_id;
  header_info->header_block = header;
  header_info->header_block_len = length;
  header_info->frame_type = frame_type;
  wmem_list_append(header_info_list, header_info);
  return header_info;
}

/*
 * Retrieves saved state for a given stream.
 */
static spdy_header_info_t* spdy_find_saved_header_block(packet_info *pinfo _U_,
                                                        uint32_t stream_id,
                                                        uint16_t frame_type) {
  wmem_list_frame_t *frame;

  if ((header_info_list == NULL) || (wmem_list_head(header_info_list) == NULL))
      return NULL;

  frame = wmem_list_frame_next(wmem_list_head(header_info_list));
  while (frame != NULL) {
      spdy_header_info_t *hi = (spdy_header_info_t *)wmem_list_frame_data(frame);
      if (hi->stream_id == stream_id && hi->frame_type == frame_type)
          return hi;
      frame = wmem_list_frame_next(frame);
  }
  return NULL;
}

/*
 * Given a content type string that may contain optional parameters,
 * return the parameter string, if any, otherwise return NULL. This
 * also has the side effect of null terminating the content type
 * part of the original string.
 */
static char* spdy_parse_content_type(char *content_type) {
  char *cp = content_type;

  while (*cp != '\0' && *cp != ';' && !g_ascii_isspace(*cp)) {
    *cp = g_ascii_tolower(*cp);
    ++cp;
  }
  if (*cp == '\0') {
    cp = NULL;
  }

  if (cp != NULL) {
    *cp++ = '\0';
    while (*cp == ';' || g_ascii_isspace(*cp)) {
      ++cp;
    }
    if (*cp != '\0') {
      return cp;
    }
  }
  return NULL;
}

static int dissect_spdy_header_payload(
  tvbuff_t *tvb,
  int offset,
  packet_info *pinfo,
  proto_tree *frame_tree,
  const spdy_control_frame_info_t *frame,
  spdy_conv_t *conv_data) {
  uint32_t stream_id;
  int header_block_length = frame->length;
  int hdr_offset = 0;
  tvbuff_t *header_tvb = NULL;
  const char *hdr_method = NULL;
  const char *hdr_path = NULL;
  const char *hdr_version = NULL;
  const char *hdr_host = NULL;
  const char *hdr_scheme = NULL;
  const char *hdr_status = NULL;
  char *content_type = NULL;
  char *content_encoding = NULL;
  uint32_t num_headers = 0;
  proto_item *header_block_item;
  proto_tree *header_block_tree;

  /* Get stream id, which is present in all types of header frames. */
  stream_id = tvb_get_ntohl(tvb, offset) & SPDY_STREAM_ID_MASK;
  dissect_spdy_stream_id_field(tvb, offset, pinfo, frame_tree, hf_spdy_streamid);
  offset += 4;

  /* Get SYN_STREAM-only fields. */
  if (frame->type == SPDY_SYN_STREAM) {
    /* Get associated stream ID. */
    dissect_spdy_stream_id_field(tvb, offset, pinfo, frame_tree, hf_spdy_associated_streamid);
    offset += 4;

    /* Get priority */
    proto_tree_add_item(frame_tree, hf_spdy_priority, tvb, offset, 2, ENC_BIG_ENDIAN);
    proto_tree_add_item(frame_tree, hf_spdy_unused, tvb, offset, 2, ENC_BIG_ENDIAN);
    proto_tree_add_item(frame_tree, hf_spdy_slot, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
  }


  /* Get our header block length. */
  switch (frame->type) {
    case SPDY_SYN_STREAM:
      header_block_length -= 10;
      break;
    case SPDY_SYN_REPLY:
    case SPDY_HEADERS:
      header_block_length -= 4;
      break;
    default:
      /* Unhandled case. This should never happen. */
      DISSECTOR_ASSERT_NOT_REACHED();
  }

  /* Add the header block. */
  header_block_item = proto_tree_add_item(frame_tree,
                                            hf_spdy_header_block,
                                            tvb,
                                            offset,
                                            header_block_length,
                                            ENC_NA);
  header_block_tree = proto_item_add_subtree(header_block_item,
                                               ett_spdy_header_block);

  /* Decompress header block as necessary. */
  if (!spdy_decompress_headers) {
    header_tvb = tvb;
    hdr_offset = offset;
  } else {
    spdy_header_info_t *header_info;

    /* First attempt to find previously decompressed data.
     * This will not work correctly for lower-level frames that contain more
     * than one SPDY frame of the same type. We assume this to never be the
     * case, though. */
    header_info = spdy_find_saved_header_block(pinfo,
                                               stream_id,
                                               frame->type);

    /* Generate decompressed data and store it, since none was found. */
    if (header_info == NULL) {
      uint8_t *uncomp_ptr = NULL;
      unsigned uncomp_length = 0;
#if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
#ifdef HAVE_ZLIBNG
      zng_streamp decomp;
#else
      z_streamp decomp;
#endif

      /* Get our decompressor. */
      if (stream_id % 2 == 0) {
        /* Even streams are server-initiated and should never get a
         * client-initiated header block. Use reply decompressor. */
        decomp = conv_data->rply_decompressor;
      } else if (frame->type == SPDY_HEADERS) {
        /* Odd streams are client-initiated, but may have HEADERS from either
         * side. Currently, no known clients send HEADERS so we assume they are
         * all from the server. */
        decomp = conv_data->rply_decompressor;
      } else if (frame->type == SPDY_SYN_STREAM) {
        decomp = conv_data->rqst_decompressor;
      } else if (frame->type == SPDY_SYN_REPLY) {
        decomp = conv_data->rply_decompressor;
      } else {
        /* Unhandled case. This should never happen. */
        DISSECTOR_ASSERT_NOT_REACHED();
      }

      /* Decompress. */
      uncomp_ptr = spdy_decompress_header_block(tvb,
                                                pinfo,
                                                decomp,
                                                conv_data->dictionary_id,
                                                offset,
                                                header_block_length,
                                                &uncomp_length);

      /* Catch decompression failures. */
      if (uncomp_ptr == NULL) {
        expert_add_info(pinfo, frame_tree, &ei_spdy_inflation_failed);

        proto_item_append_text(frame_tree, " [Error: Header decompression failed]");
        return -1;
      }
#endif

      /* Store decompressed data. */
      header_info = spdy_save_header_block(pinfo, stream_id, frame->type, uncomp_ptr, uncomp_length);
    }

    /* Create a tvb containing the uncompressed data. */
    header_tvb = tvb_new_child_real_data(tvb, header_info->header_block,
                                         header_info->header_block_len,
                                         header_info->header_block_len);
    add_new_data_source(pinfo, header_tvb, "Uncompressed headers");
    hdr_offset = 0;
  }

  /* Get header block details. */
  if (header_tvb == NULL || !spdy_decompress_headers) {
    num_headers = 0;
  } else {
    num_headers = tvb_get_ntohl(header_tvb, hdr_offset);
    /*ti = */ proto_tree_add_item(header_block_tree,
                             hf_spdy_num_headers,
                             header_tvb,
                             hdr_offset,
                             4,
                             ENC_BIG_ENDIAN);
  }
  hdr_offset += 4;

  /* Process headers. */
  while (num_headers--) {
    char *header_name;
    const char *header_value;
    proto_tree *header_tree;
    proto_item *header;
    int header_name_offset;
    int header_value_offset;
    int header_name_length;
    int header_value_length;

    /* Get header name details. */
    if (tvb_reported_length_remaining(header_tvb, hdr_offset) < 4) {
      expert_add_info_format(pinfo, frame_tree, &ei_spdy_mal_frame_data,
                             "Not enough frame data for header name size.");
      break;
    }
    header_name_offset = hdr_offset;
    header_name_length = tvb_get_ntohl(header_tvb, hdr_offset);
    hdr_offset += 4;
    if (tvb_reported_length_remaining(header_tvb, hdr_offset) < header_name_length) {
      expert_add_info_format(pinfo, frame_tree, &ei_spdy_mal_frame_data,
                             "Not enough frame data for header name.");
      break;
    }
    header_name = (char *)tvb_get_string_enc(pinfo->pool, header_tvb,
                                                    hdr_offset,
                                                    header_name_length, ENC_ASCII|ENC_NA);
    hdr_offset += header_name_length;

    /* Get header value details. */
    if (tvb_reported_length_remaining(header_tvb, hdr_offset) < 4) {
      expert_add_info_format(pinfo, frame_tree, &ei_spdy_mal_frame_data,
                             "Not enough frame data for header value size.");
      break;
    }
    header_value_offset = hdr_offset;
    header_value_length = tvb_get_ntohl(header_tvb, hdr_offset);
    hdr_offset += 4;
    if (tvb_reported_length_remaining(header_tvb, hdr_offset) < header_value_length) {
      expert_add_info_format(pinfo, frame_tree, &ei_spdy_mal_frame_data,
                             "Not enough frame data for header value.");
      break;
    }
    header_value = (char *)tvb_get_string_enc(pinfo->pool,header_tvb,
                                                     hdr_offset,
                                                     header_value_length, ENC_ASCII|ENC_NA);
    hdr_offset += header_value_length;

    /* Populate tree with header name/value details. */
    if (frame_tree) {
      /* Add 'Header' subtree with description. */
      header = proto_tree_add_item(frame_tree,
                                   hf_spdy_header,
                                   header_tvb,
                                   header_name_offset,
                                   hdr_offset - header_name_offset,
                                   ENC_NA);
      proto_item_append_text(header, ": %s: %s", header_name, header_value);
      header_tree = proto_item_add_subtree(header, ett_spdy_header);

      /* Add header name. */
      proto_tree_add_item(header_tree, hf_spdy_header_name, header_tvb,
                          header_name_offset, 4, ENC_ASCII|ENC_BIG_ENDIAN);

      /* Add header value. */
      proto_tree_add_item(header_tree, hf_spdy_header_value, header_tvb,
                          header_value_offset, 4, ENC_ASCII|ENC_BIG_ENDIAN);
    }

    /*
     * TODO(ers) check that the header name contains only legal characters.
     */
    /* TODO(hkhalil): Make sure that prohibited headers aren't sent. */
    if (g_strcmp0(header_name, ":method") == 0) {
      hdr_method = header_value;
    } else if (g_strcmp0(header_name, ":path") == 0) {
      hdr_path = header_value;
    } else if (g_strcmp0(header_name, ":version") == 0) {
      hdr_version = header_value;
    } else if (g_strcmp0(header_name, ":host") == 0) {
      hdr_host = header_value;
    } else if (g_strcmp0(header_name, ":scheme") == 0) {
      hdr_scheme = header_value;
    } else if (g_strcmp0(header_name, ":status") == 0) {
      hdr_status = header_value;
    } else if (g_strcmp0(header_name, "content-type") == 0) {
      content_type = wmem_strdup(wmem_file_scope(), header_value);
    } else if (g_strcmp0(header_name, "content-encoding") == 0) {
      content_encoding = wmem_strdup(wmem_file_scope(), header_value);
    }
  }

  /* Set Info column. */
  if (hdr_version != NULL) {
    if (hdr_status == NULL) {
      proto_item_append_text(frame_tree, ", Request: %s %s://%s%s %s",
                      hdr_method, hdr_scheme, hdr_host, hdr_path, hdr_version);
    } else {
      proto_item_append_text(frame_tree, ", Response: %s %s",
                      hdr_status, hdr_version);
    }
  }

  /*
   * If we expect data on this stream, we need to remember the content
   * type and content encoding.
   */
  if (content_type != NULL && !pinfo->fd->visited) {
    char *content_type_params = spdy_parse_content_type(content_type);
    spdy_save_stream_info(conv_data, stream_id,
                          (hdr_status == NULL) ? MEDIA_CONTAINER_HTTP_REQUEST : MEDIA_CONTAINER_HTTP_RESPONSE,
                          content_type, content_type_params, content_encoding);
  }

  return frame->length;
}

static int dissect_spdy_rst_stream_payload(
  tvbuff_t *tvb,
  int offset,
  packet_info *pinfo,
  proto_tree *frame_tree,
  const spdy_control_frame_info_t *frame) {
  uint32_t rst_status;
  proto_item *ti;
  const char* str;

  /* Get stream ID and add to info column and tree. */
  dissect_spdy_stream_id_field(tvb, offset, pinfo, frame_tree, hf_spdy_streamid);
  offset += 4;

  /* Get status. */

  ti = proto_tree_add_item(frame_tree, hf_spdy_rst_stream_status, tvb, offset, 4, ENC_BIG_ENDIAN);
  rst_status = tvb_get_ntohl(tvb, offset);
  if (try_val_to_str(rst_status, rst_stream_status_names) == NULL) {
    /* Handle boundary conditions. */
    expert_add_info_format(pinfo, ti, &ei_spdy_invalid_rst_stream,
                           "Invalid status code for RST_STREAM: %u", rst_status);
  }

  str = val_to_str(rst_status, rst_stream_status_names, "Unknown (%d)");

  proto_item_append_text(frame_tree, ", Status: %s", str);

  return frame->length;
}

static int dissect_spdy_settings_payload(
  tvbuff_t *tvb,
  int offset,
  packet_info *pinfo,
  proto_tree *frame_tree,
  const spdy_control_frame_info_t *frame) {
  uint32_t num_entries;
  proto_item *ti, *ti_setting;
  proto_tree *setting_tree;
  proto_tree *flags_tree;

  /* Make sure that we have enough room for our number of entries field. */
  if (frame->length < 4) {
    expert_add_info(pinfo, frame_tree, &ei_spdy_mal_setting_frame);
    return -1;
  }

  /* Get number of entries, and make sure we have enough room for them. */
  num_entries = tvb_get_ntohl(tvb, offset);
  if (frame->length < num_entries * 8) {
    expert_add_info_format(pinfo, frame_tree, &ei_spdy_mal_setting_frame,
        "SETTINGS frame too small [num_entries=%d]", num_entries);
    return -1;
  }

  proto_tree_add_item(frame_tree, hf_spdy_num_settings, tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  /* Dissect each entry. */
  while (num_entries > 0) {
    const char *setting_id_str;
    uint32_t setting_value;

    /* Create key/value pair subtree. */
    ti_setting = proto_tree_add_item(frame_tree, hf_spdy_setting, tvb, offset, 8, ENC_NA);
    setting_tree = proto_item_add_subtree(ti_setting, ett_spdy_setting);

    /* Set flags. */
    if (setting_tree) {
      ti = proto_tree_add_item(setting_tree, hf_spdy_flags, tvb, offset, 1, ENC_BIG_ENDIAN);

      /* TODO(hkhalil): Prettier output for flags sub-tree description. */
      flags_tree = proto_item_add_subtree(ti, ett_spdy_flags);
      proto_tree_add_item(flags_tree, hf_spdy_flags_persist_value, tvb, offset, 1, ENC_BIG_ENDIAN);
      proto_tree_add_item(flags_tree, hf_spdy_flags_persisted, tvb, offset, 1, ENC_BIG_ENDIAN);
    }
    offset += 1;

    /* Set ID. */
    setting_id_str = val_to_str(tvb_get_ntoh24(tvb, offset), setting_id_names, "Unknown(%d)");

    proto_tree_add_item(setting_tree, hf_spdy_setting_id, tvb, offset, 3, ENC_BIG_ENDIAN);
    offset += 3;

    /* Set Value. */
    setting_value = tvb_get_ntohl(tvb, offset);

    proto_tree_add_item(setting_tree, hf_spdy_setting_value, tvb, offset, 4, ENC_BIG_ENDIAN);
    proto_item_append_text(ti_setting, ", %s: %u", setting_id_str, setting_value);
    proto_item_append_text(frame_tree, ", %s: %u", setting_id_str, setting_value);
    offset += 4;

    /* Increment. */
    --num_entries;
  }

  return frame->length;
}

static int dissect_spdy_ping_payload(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
                                     proto_tree *frame_tree, const spdy_control_frame_info_t *frame)
{
  /* Get ping ID. */
  uint32_t ping_id = tvb_get_ntohl(tvb, offset);

  /* Add proto item for ping ID. */
  proto_tree_add_item(frame_tree, hf_spdy_ping_id, tvb, offset, 4, ENC_BIG_ENDIAN);
  proto_item_append_text(frame_tree, ", ID: %u", ping_id);

  return frame->length;
}

static int dissect_spdy_goaway_payload(tvbuff_t *tvb,
                                       int offset,
                                       packet_info *pinfo,
                                       proto_tree *frame_tree,
                                       const spdy_control_frame_info_t *frame) {
  uint32_t goaway_status;
  proto_item* ti;

  /* Get last good stream ID and add to info column and tree. */
  dissect_spdy_stream_id_field(tvb, offset, pinfo, frame_tree, hf_spdy_goaway_last_good_stream_id);
  offset += 4;

  /* Add proto item for goaway_status. */
  ti = proto_tree_add_item(frame_tree, hf_spdy_goaway_status, tvb, offset, 4, ENC_BIG_ENDIAN);
  goaway_status = tvb_get_ntohl(tvb, offset);

  if (try_val_to_str(goaway_status, goaway_status_names) == NULL) {
    /* Handle boundary conditions. */
    expert_add_info_format(pinfo, ti, &ei_spdy_invalid_go_away,
                           "Invalid status code for GOAWAY: %u", goaway_status);
  }

  /* Add status to info column. */
  proto_item_append_text(frame_tree, " Status=%s)",
                  val_to_str(goaway_status, rst_stream_status_names, "Unknown (%d)"));

  return frame->length;
}

static int dissect_spdy_window_update_payload(
    tvbuff_t *tvb,
    int offset,
    packet_info *pinfo,
    proto_tree *frame_tree,
    const spdy_control_frame_info_t *frame)
{
  uint32_t            window_update_delta;

  /* Get stream ID. */
  dissect_spdy_stream_id_field(tvb, offset, pinfo, frame_tree, hf_spdy_streamid);
  offset += 4;

  /* Get window update delta. */
  window_update_delta = tvb_get_ntohl(tvb, offset) & 0x7FFFFFFF;

  /* Add proto item for window update delta. */
  proto_tree_add_item(frame_tree, hf_spdy_window_update_delta, tvb, offset, 4, ENC_BIG_ENDIAN);
  proto_item_append_text(frame_tree, ", Delta: %u", window_update_delta);

  return frame->length;
}

/*
 * Performs SPDY frame dissection.
 */
static int dissect_spdy_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
  uint8_t             control_bit;
  spdy_control_frame_info_t frame;
  uint32_t            stream_id = 0;
  const char          *frame_type_name;
  proto_tree          *spdy_tree;
  proto_item          *spdy_item, *type_item = NULL;
  int                 offset = 0;
  spdy_conv_t         *conv_data;

  conv_data = get_or_create_spdy_conversation_data(pinfo);

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "SPDY");

  /* Create frame root. */
  spdy_item = proto_tree_add_item(tree, proto_spdy, tvb, offset, -1, ENC_NA);
  spdy_tree = proto_item_add_subtree(spdy_item, ett_spdy);

  /* Add control bit. */
  control_bit = tvb_get_uint8(tvb, offset) & 0x80;
  proto_tree_add_item(spdy_tree, hf_spdy_control_bit, tvb, offset, 2, ENC_NA);

  /* Process first four bytes of frame, formatted depending on control bit. */
  if (control_bit) {
    /* Add version. */
    frame.version = tvb_get_ntohs(tvb, offset) & 0x7FFF;
    proto_tree_add_item(spdy_tree, hf_spdy_version, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    /* Add control frame type. */
    type_item = proto_tree_add_item(spdy_tree, hf_spdy_type, tvb, offset, 2, ENC_BIG_ENDIAN);
    frame.type = tvb_get_ntohs(tvb, offset);
    if (frame.type >= SPDY_INVALID) {
      expert_add_info_format(pinfo, type_item, &ei_spdy_invalid_frame_type,
                             "Invalid SPDY control frame type: %d", frame.type);
      return -1;
    }
    offset += 2;

  } else {
    frame.type = SPDY_DATA;
    frame.version = 0; /* Version doesn't apply to DATA. */

    /* Add stream ID. */
    stream_id = tvb_get_ntohl(tvb, offset) & SPDY_STREAM_ID_MASK;
    proto_tree_add_item(spdy_tree, hf_spdy_streamid, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
  }

  /* Add frame info. */
  frame_type_name = val_to_str(frame.type, frame_type_names, "Unknown(%d)");
  col_append_sep_str(pinfo->cinfo, COL_INFO, ", ", frame_type_name);

  proto_item_append_text(spdy_tree, ": %s", frame_type_name);

  /* Add flags. */
  frame.flags = tvb_get_uint8(tvb, offset);
  if (spdy_tree) {
    dissect_spdy_flags(tvb, offset, spdy_tree, &frame);
  }
  offset += 1;

  /* Add length. */
  frame.length = tvb_get_ntoh24(tvb, offset);

  proto_item_set_len(spdy_item, frame.length + 8);
  proto_tree_add_item(spdy_tree, hf_spdy_length, tvb, offset, 3, ENC_BIG_ENDIAN);
  offset += 3;

  /*
   * Make sure there's as much data as the frame header says there is.
   */
  if ((unsigned)tvb_reported_length_remaining(tvb, offset) < frame.length) {
    expert_add_info_format(pinfo, tree, &ei_spdy_mal_frame_data,
                           "Not enough frame data: %d vs. %d",
                           frame.length, tvb_reported_length_remaining(tvb, offset));
    return -1;
  }

  /* Dissect DATA payload as necessary. */
  if (!control_bit) {
    return offset + dissect_spdy_data_payload(tvb, offset, pinfo, tree, spdy_tree,
                                              spdy_item, conv_data, stream_id, &frame);
  }

  /* Abort here if the version is too low. */
  if (frame.version < MIN_SPDY_VERSION) {
    proto_item_append_text(spdy_item, " [Unsupported Version]");
    return frame.length + 8;
  }

  switch (frame.type) {
    case SPDY_SYN_STREAM:
    case SPDY_SYN_REPLY:
    case SPDY_HEADERS:
      dissect_spdy_header_payload(tvb, offset, pinfo, spdy_tree, &frame, conv_data);
      break;

    case SPDY_RST_STREAM:
      dissect_spdy_rst_stream_payload(tvb, offset, pinfo, spdy_tree, &frame);
      break;

    case SPDY_SETTINGS:
      dissect_spdy_settings_payload(tvb, offset, pinfo, spdy_tree, &frame);
      break;

    case SPDY_PING:
      dissect_spdy_ping_payload(tvb, offset, pinfo, spdy_tree, &frame);
      break;

    case SPDY_GOAWAY:
      dissect_spdy_goaway_payload(tvb, offset, pinfo, spdy_tree, &frame);
      break;

    case SPDY_WINDOW_UPDATE:
      dissect_spdy_window_update_payload(tvb, offset, pinfo, spdy_tree, &frame);
      break;

    case SPDY_CREDENTIAL:
      /* TODO(hkhalil): Show something meaningful. */
      break;

    default:
      expert_add_info_format(pinfo, type_item, &ei_spdy_invalid_frame_type,
                             "Unhandled SPDY frame type: %d", frame.type);
      break;
  }

  /*
   * OK, we've set the Protocol and Info columns for the
   * first SPDY message; set a fence so that subsequent
   * SPDY messages don't overwrite the Info column.
   */
  col_set_fence(pinfo->cinfo, COL_INFO);

  /* Assume that we've consumed the whole frame. */
  return 8 + frame.length;
}

static unsigned get_spdy_message_len(packet_info *pinfo _U_, tvbuff_t *tvb,
                                  int offset, void *data _U_)
{
  return (unsigned)tvb_get_ntoh24(tvb, offset + 5) + 8;
}

/*
 * Wrapper for dissect_spdy_frame, sets fencing and desegments as necessary.
 */
static int dissect_spdy(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
  col_clear(pinfo->cinfo, COL_INFO);

  tcp_dissect_pdus(tvb, pinfo, tree, true, 8, get_spdy_message_len, dissect_spdy_frame, data);
  return tvb_captured_length(tvb);
}

/*
 * Looks for SPDY frame at tvb start.
 * If not enough data for either, requests more via desegment struct.
 */
static bool dissect_spdy_heur(tvbuff_t *tvb,
                                  packet_info *pinfo,
                                  proto_tree *tree,
                                   void *data _U_)
{
  /*
   * The first byte of a SPDY frame must be either 0 or
   * 0x80. If it's not, assume that this is not SPDY.
   * (In theory, a data frame could have a stream ID
   * >= 2^24, in which case it won't have 0 for a first
   * byte, but this is a pretty reliable heuristic for
   * now.)
   */
  uint8_t first_byte = tvb_get_uint8(tvb, 0);
  if (first_byte != 0x80 && first_byte != 0x0) {
    return false;
  }

  /* Attempt dissection. */
  if (dissect_spdy(tvb, pinfo, tree, NULL) != 0) {
    return true;
  }

  return false;
}

/*
 * Performs plugin registration.
 */
void proto_register_spdy(void)
{
  static hf_register_info hf[] = {
    { &hf_spdy_data,
      { "Data",           "spdy.data",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        NULL, HFILL
      }
    },
    { &hf_spdy_control_bit,
      { "Control frame",    "spdy.control_bit",
        FT_BOOLEAN, 16, TFS(&tfs_yes_no), 0x8000,
        "true if SPDY control frame", HFILL
      }
    },
    { &hf_spdy_version,
      { "Version",        "spdy.version",
        FT_UINT16, BASE_DEC, NULL, 0x7FFF,
        NULL, HFILL
      }
    },
    { &hf_spdy_type,
      { "Type",           "spdy.type",
        FT_UINT16, BASE_DEC,
        VALS(frame_type_names), 0x0,
        NULL, HFILL
      }
    },
    { &hf_spdy_flags,
      { "Flags",          "spdy.flags",
        FT_UINT8, BASE_HEX, NULL, 0x0,
        NULL, HFILL
      }
    },
    { &hf_spdy_flags_fin,
      { "FIN",            "spdy.flags.fin",
        FT_BOOLEAN, 8,
        TFS(&tfs_set_notset), SPDY_FLAG_FIN,
        NULL, HFILL
      }
    },
    { &hf_spdy_flags_unidirectional,
      { "Unidirectional", "spdy.flags.unidirectional",
        FT_BOOLEAN, 8,
        TFS(&tfs_set_notset), SPDY_FLAG_UNIDIRECTIONAL,
        NULL, HFILL
      }
    },
    { &hf_spdy_flags_clear_settings,
      { "Persist Value",  "spdy.flags.clear_settings",
        FT_BOOLEAN, 8,
        TFS(&tfs_set_notset), SPDY_FLAG_SETTINGS_CLEAR_SETTINGS,
        NULL, HFILL
      }
    },
    { &hf_spdy_flags_persist_value,
      { "Persist Value",  "spdy.flags.persist_value",
        FT_BOOLEAN, 8,
        TFS(&tfs_set_notset), SPDY_FLAG_SETTINGS_PERSIST_VALUE,
        NULL, HFILL
      }
    },
    { &hf_spdy_flags_persisted,
      { "Persisted",      "spdy.flags.persisted",
        FT_BOOLEAN, 8,
        TFS(&tfs_set_notset), SPDY_FLAG_SETTINGS_PERSISTED,
        NULL, HFILL
      }
    },
    { &hf_spdy_length,
      { "Length",         "spdy.length",
        FT_UINT24, BASE_DEC, NULL, 0x0,
        NULL, HFILL
      }
    },
    { &hf_spdy_header_block,
      { "Header block", "spdy.header_block",
          FT_BYTES, BASE_NONE, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_header,
      { "Header",         "spdy.header",
        FT_NONE, BASE_NONE, NULL, 0x0,
        NULL, HFILL
      }
    },
    { &hf_spdy_header_name,
      { "Name",           "spdy.header.name",
          FT_UINT_STRING, BASE_NONE, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_header_value,
      { "Value",          "spdy.header.value",
          FT_UINT_STRING, BASE_NONE, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_streamid,
      { "Stream ID",      "spdy.streamid",
          FT_UINT32, BASE_DEC, NULL, SPDY_STREAM_ID_MASK,
          NULL, HFILL
      }
    },
    { &hf_spdy_associated_streamid,
      { "Associated Stream ID",   "spdy.associated.streamid",
          FT_UINT32, BASE_DEC, NULL, SPDY_STREAM_ID_MASK,
          NULL, HFILL
      }
    },
    { &hf_spdy_priority,
      { "Priority",       "spdy.priority",
          FT_UINT16, BASE_DEC, NULL, 0xE000,
          NULL, HFILL
      }
    },
    { &hf_spdy_unused,
      { "Unused",       "spdy.unused",
          FT_UINT16, BASE_HEX, NULL, 0x1F00,
          "Reserved for future use", HFILL
      }
    },
    { &hf_spdy_slot,
      { "Slot",       "spdy.slot",
          FT_UINT16, BASE_DEC, NULL, 0x00FF,
          "Specifying the index in the server's CREDENTIAL vector of the client certificate to be used for this request", HFILL
      }
    },
    { &hf_spdy_num_headers,
      { "Number of headers", "spdy.numheaders",
          FT_UINT32, BASE_DEC, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_rst_stream_status,
      { "Reset Status",   "spdy.rst_stream_status",
          FT_UINT32, BASE_DEC, VALS(rst_stream_status_names), 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_num_settings,
      { "Number of Settings", "spdy.num_settings",
          FT_UINT32, BASE_DEC, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_setting,
      { "Setting",        "spdy.setting",
          FT_NONE, BASE_NONE, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_setting_id,
      { "ID",             "spdy.setting.id",
          FT_UINT24, BASE_DEC, VALS(setting_id_names), 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_setting_value,
      { "Value",          "spdy.setting.value",
          FT_UINT32, BASE_DEC, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_ping_id,
      { "Ping ID",        "spdy.ping_id",
          FT_UINT32, BASE_DEC, NULL, 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_goaway_last_good_stream_id,
      { "Last Good Stream ID", "spdy.goaway_last_good_stream_id",
          FT_UINT32, BASE_DEC, NULL, SPDY_STREAM_ID_MASK,
          NULL, HFILL
      }
    },
    { &hf_spdy_goaway_status,
      { "Go Away Status", "spdy.goaway_status",
          FT_UINT32, BASE_DEC, VALS(goaway_status_names), 0x0,
          NULL, HFILL
      }
    },
    { &hf_spdy_window_update_delta,
      { "Window Update Delta", "spdy.window_update_delta",
          FT_UINT32, BASE_DEC, NULL, 0x7FFFFFFF,
          NULL, HFILL
      }
    },
  };
  static int *ett[] = {
    &ett_spdy,
    &ett_spdy_flags,
    &ett_spdy_header_block,
    &ett_spdy_header,
    &ett_spdy_setting,
    &ett_spdy_encoded_entity,
  };

  static ei_register_info ei[] = {
    { &ei_spdy_inflation_failed, { "spdy.inflation_failed", PI_UNDECODED, PI_ERROR, "Inflation failed. Aborting.", EXPFILL }},
    { &ei_spdy_mal_frame_data, { "spdy.malformed.frame_data", PI_MALFORMED, PI_ERROR, "Not enough frame data", EXPFILL }},
    { &ei_spdy_mal_setting_frame, { "spdy.malformed.setting_frame", PI_MALFORMED, PI_ERROR, "SETTINGS frame too small for number of entries field.", EXPFILL }},
    { &ei_spdy_invalid_rst_stream, { "spdy.rst_stream.invalid", PI_PROTOCOL, PI_WARN, "Invalid status code for RST_STREAM", EXPFILL }},
    { &ei_spdy_invalid_go_away, { "spdy.goaway.invalid", PI_PROTOCOL, PI_WARN, "Invalid status code for GOAWAY", EXPFILL }},
    { &ei_spdy_invalid_frame_type, { "spdy.type.invalid", PI_PROTOCOL, PI_WARN, "Invalid SPDY frame type", EXPFILL }},
    { &ei_spdy_reassembly_info, { "spdy.reassembly_info", PI_REASSEMBLE, PI_CHAT, "Assembled from frames in packet(s)", EXPFILL }},
  };

  module_t *spdy_module;
  expert_module_t* expert_spdy;

  proto_spdy = proto_register_protocol("SPDY", "SPDY", "spdy");
  proto_register_field_array(proto_spdy, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  expert_spdy = expert_register_protocol(proto_spdy);
  expert_register_field_array(expert_spdy, ei, array_length(ei));

  spdy_handle = register_dissector("spdy", dissect_spdy, proto_spdy);

  spdy_module = prefs_register_protocol(proto_spdy, NULL);
  prefs_register_bool_preference(spdy_module, "assemble_data_frames",
                                 "Assemble SPDY bodies that consist of multiple DATA frames",
                                 "Whether the SPDY dissector should reassemble multiple "
                                 "data frames into an entity body.",
                                 &spdy_assemble_entity_bodies);

  prefs_register_bool_preference(spdy_module, "decompress_headers",
                                 "Uncompress SPDY headers",
                                 "Whether to uncompress SPDY headers.",
                                 &spdy_decompress_headers);
  prefs_register_bool_preference(spdy_module, "decompress_body",
                                 "Uncompress entity bodies",
                                 "Whether to uncompress entity bodies that are compressed "
                                 "using \"Content-Encoding: \"",
                                 &spdy_decompress_body);

  register_init_routine(&spdy_init_protocol);

  /*
   * Register for tapping
   */
  spdy_tap = register_tap("spdy"); /* SPDY statistics tap */
  spdy_eo_tap = register_tap("spdy_eo"); /* SPDY Export Object tap */
}

void proto_reg_handoff_spdy(void) {

  dissector_add_uint_with_preference("tcp.port", TCP_PORT_SPDY, spdy_handle);
  /* Use "0" to avoid overwriting HTTPS port and still offer support over TLS */
  ssl_dissector_add(0, spdy_handle);
  dissector_add_string("http.upgrade", "spdy", spdy_handle);

  media_handle = find_dissector_add_dependency("media", proto_spdy);
  port_subdissector_table = find_dissector_table("http.port");
  media_type_subdissector_table = find_dissector_table("media_type");

  /* Weak heuristic, so disabled by default */
  heur_dissector_add("tcp", dissect_spdy_heur, "SPDY over TCP", "spdy_tcp", proto_spdy, HEURISTIC_DISABLE);
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */