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
//! This crate provides types for parsing and generating the headers of
//! Internet messages, such as e-mail, HTTP requests or responses, and SIP
//! messages.  The typical format of these headers is described in [IETF RFC
//! 5322](https://tools.ietf.org/html/rfc5322).  Note that specific Internet
//! standards may leverage this format but make exceptions, extensions, or
//! other changes to it.
//!
//! To parse a message, construct a [`MessageHeaders`] value using
//! [`MessageHeaders::new`], and then start feeding it input text via
//! [`MessageHeaders::parse`].  Each call will consume zero or more of the
//! input characters.  Input is only consumed if enough is provided for the
//! parser to determine when all of one or more headers has been provided.  Any
//! unused input should be included in the next call along with additional
//! input to continue parsing.  Parsing is complete once the message body is
//! found.
//!
//! To generate a message, construct a [`MessageHeaders`] value using
//! [`MessageHeaders::new`], fill in headers using
//! [`MessageHeaders::add_header`] and other related functions, and emit
//! the final text using [`MessageHeaders::generate`].
//!
//! By default, there is no constraint set on the lengths of header lines.
//! This can cause issues in some places, such as servers receiving input from
//! untrusted connections.  An attacker might feed in a constant stream of
//! characters that never includes a line ending, causing the server to exceed
//! its memory allocation limits.  To prevent this, the
//! [`MessageHeaders::set_line_limit`] function can be used to set a constraint
//! for header line length, both for parsing and generation.
//!
//! [`MessageHeaders`]: struct.MessageHeaders.html
//! [`MessageHeaders::new`]: struct.MessageHeaders.html#method.new
//! [`MessageHeaders::parse`]: struct.MessageHeaders.html#method.parse
//! [`MessageHeaders::add_header`]: struct.MessageHeaders.html#method.add_header
//! [`MessageHeaders::generate`]: struct.MessageHeaders.html#method.generate
//! [`MessageHeaders::set_line_limit`]:
//! struct.MessageHeaders.html#method.set_line_limit

#![warn(clippy::pedantic)]
#![warn(missing_docs)]
#![allow(clippy::non_ascii_literal)]

#[cfg(test)]
#[macro_use]
extern crate named_tuple;

mod error;

pub use error::Error;
use std::fmt::Write;

// These are the characters that are considered whitespace and
// should be stripped off by the Strip() function.
//
// Name of "WSP" chosen to match the symbol name from
// RFC 5322 (https://tools.ietf.org/html/rfc5322) which refers
// to this specific character set.
const WSP: &str = " \t";

// This is the required line terminator for internet message header lines.
const CRLF: &str = "\r\n";

fn find_crlf<T>(message: T) -> Option<usize>
where
    T: AsRef<[u8]>,
{
    message.as_ref().windows(2).enumerate().find_map(|(i, window)| {
        if window == b"\r\n" {
            Some(i)
        } else {
            None
        }
    })
}

fn fold_header(
    line: &str,
    line_length_limit: usize,
    skip: usize,
) -> Result<(&str, &str), Error> {
    // No need to fold if the line fits within the limit.
    if line.len() <= line_length_limit {
        Ok((line, ""))
    } else {
        // Otherwise look for where to split the line,
        // starting at the limit and searching back.
        line.char_indices()
            .rev()
            .skip_while(|(i, _)| *i > line_length_limit)
            .take_while(|(i, _)| *i >= skip)
            .find_map(|(i, c)| WSP.find(c).map(|_| i))
            .map_or_else(
                || {
                    // Error if we couldn't find a place to split.
                    Err(Error::HeaderLineCouldNotBeFolded(String::from(line)))
                },
                |i| {
                    // The first part leads up to the split point.
                    let part = &line[..i];

                    // Find the last whitespace character
                    // starting at the split point.  This keeps one
                    // whitespace character and drops the rest.
                    let j = i + line[i..]
                        .char_indices()
                        .take_while(|(_, c)| WSP.find(*c).is_some())
                        .last()
                        .map(|(i, _)| i)
                        .unwrap();
                    Ok((part, &line[j..]))
                },
            )
    }
}

fn separate_header_name_and_value(line: &str) -> Result<Header, Error> {
    match line.find(':') {
        None => Err(Error::HeaderLineMissingColon(line.to_string())),
        Some(name_value_delimiter) => {
            let name = &line[0..name_value_delimiter];
            validate_header_name(name)?;
            Ok(Header {
                name: name.into(),
                value: line[name_value_delimiter + 1..].to_string(),
            })
        },
    }
}

fn unfold_header(
    mut raw_message: &[u8],
    mut header: Header,
) -> Result<Option<(Header, usize)>, Error> {
    let mut consumed = 0;
    loop {
        // Find where the next line ends.
        let line_terminator = match find_crlf(raw_message) {
            None => return Ok(None),
            Some(i) => i,
        };

        // Calculate the next line's length, and convert it into a string.
        let line_length = line_terminator + 2;
        let line = std::str::from_utf8(&raw_message[..line_terminator])
            .map_err(|error| Error::HeaderLineInvalidText {
                line: raw_message[..line_terminator].to_vec(),
                source: error,
            })?;

        // If the next line begins with whitespace, unfold the line
        if line_terminator > 0  // line is not empty
            && WSP.find(         // and we can find whitespace
                line             // in the message
                    .chars()
                    .next()      // beginning at the line start
                    .unwrap()
            ).is_some()
        {
            // Append a single space to the header value.
            header.value.push(' ');

            // Concatenate the next line to the header value after trimming it.
            validate_header_value(&header, line)?;
            header.value += line.trim();

            // Move to the line following the next line.
            raw_message = &raw_message[line_length..];
            consumed += line_length;
        } else {
            return Ok(Some((header, consumed)));
        }
    }
}

fn validate_header_name(text: &str) -> Result<(), Error> {
    if text.chars().all(|c| c.is_ascii_graphic()) {
        Ok(())
    } else {
        Err(Error::HeaderNameContainsIllegalCharacter(text.to_string()))
    }
}

fn validate_header_value(
    header: &Header,
    value_segment: &str,
) -> Result<(), Error> {
    if value_segment
        .chars()
        .all(|c| c == '\t' || c == ' ' || (c as char).is_ascii_graphic())
    {
        Ok(())
    } else {
        Err(Error::HeaderValueContainsIllegalCharacter {
            name: header.name.clone(),
            value_segment: value_segment.to_string(),
        })
    }
}

/// This is a newtype wrapping a `String` in order to handle it
/// as an Internet message header name.
///
/// Names of headers in Internet messages are case insensitive.
/// This type is used to represent those names in such a way that
/// case insensitivity is considered for equality and order.
#[derive(Clone, Debug, Default, Eq)]
pub struct HeaderName(String);

impl From<&str> for HeaderName {
    fn from(name: &str) -> Self {
        Self(name.to_string())
    }
}

impl AsRef<str> for HeaderName {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl std::fmt::Display for HeaderName {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        f.write_str(self.0.as_ref())
    }
}

impl PartialEq for HeaderName {
    fn eq(
        &self,
        rhs: &Self,
    ) -> bool {
        self.0.eq_ignore_ascii_case(&rhs.0)
    }
}

impl PartialEq<&str> for HeaderName {
    fn eq(
        &self,
        rhs: &&str,
    ) -> bool {
        self.0.eq_ignore_ascii_case(*rhs)
    }
}

impl PartialEq<HeaderName> for &str {
    fn eq(
        &self,
        rhs: &HeaderName,
    ) -> bool {
        self.eq_ignore_ascii_case(&rhs.0)
    }
}

impl PartialOrd for HeaderName {
    fn partial_cmp(
        &self,
        other: &Self,
    ) -> Option<std::cmp::Ordering> {
        Some(
            match self.0.chars().zip(other.0.chars()).find_map(|(lhs, rhs)| {
                match lhs.to_ascii_lowercase().cmp(&rhs.to_ascii_lowercase()) {
                    std::cmp::Ordering::Equal => None,
                    ordering => Some(ordering),
                }
            }) {
                Some(ordering) => ordering,
                None => self.0.len().cmp(&other.0.len()),
            },
        )
    }
}

/// This represents one line of the headers portion of an Internet message.  It
/// consists of a case-insensitive name and a value, which may contain parts
/// separated by commas.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Header {
    /// This is the name of the header.  It is case-insensitive.
    pub name: HeaderName,

    /// This is the value of the header.  When parsed from text, or in text
    /// generated from it, this string may span (or fold across) multiple
    /// lines.  Also, in some contexts, it may contain multiple values,
    /// in which case the values are commonly separated by commas.
    pub value: String,
}

/// This enumerates the possible non-error states `MessageHeaders` can be in
/// after parsing a bit of input.
#[derive(Debug, Eq, PartialEq)]
pub enum ParseStatus {
    /// The message headers were fully parsed and the body (if any) begins
    /// after the last byte consumed.
    Complete,

    /// The message headers have not yet been fully parsed.
    ///
    /// The user is expected to call `parse` again with more input, starting
    /// with the unparsed portion of the previous input string, and adding more
    /// to it.
    Incomplete,
}

/// This holds the values returned by `MessageHeaders::parse`.
#[derive(Debug, Eq, PartialEq)]
pub struct ParseResults {
    /// This indicates the state of the parser.
    pub status: ParseStatus,

    /// This is the number of bytes of input consumed by the last
    /// `MessageHeaders::parse` call.
    pub consumed: usize,
}

/// This enumerates the ways in which a multi-value header may be represented
/// in the generated text.
#[derive(Debug, Copy, Clone)]
pub enum HeaderMultiMode {
    /// Put all values in the same header line (possibly folded) and separate
    /// the values with commas.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # extern crate rhymessage;
    /// use rhymessage::{MessageHeaders, HeaderMultiMode};
    ///
    /// # fn main() -> Result<(), rhymessage::Error> {
    /// let via: Vec<String> = [
    ///     "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3",
    ///     "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2",
    ///     "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1"
    /// ].iter().map(|s| String::from(*s)).collect();
    /// let mut headers = MessageHeaders::new();
    /// headers.set_header_multi_value("Via", via.clone(), HeaderMultiMode::OneLine);
    /// assert_eq!(
    ///     Ok(concat!(
    ///         "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
    ///             "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
    ///             "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
    ///         "\r\n",
    ///     ).as_bytes()),
    ///     headers.generate().as_deref()
    /// );
    /// # Ok(())
    /// # }
    /// ```
    OneLine,

    /// Put each value in a separate header line (each possibly folded)
    /// where all the header lines have the same header name.  Note that
    /// the higher-layer protocol may have restrictions on what kinds of
    /// headers may express multiple values in this way.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # extern crate rhymessage;
    /// use rhymessage::{MessageHeaders, HeaderMultiMode};
    ///
    /// # fn main() -> Result<(), rhymessage::Error> {
    /// let via: Vec<String> = [
    ///     "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3",
    ///     "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2",
    ///     "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1"
    /// ].iter().map(|s| String::from(*s)).collect();
    /// let mut headers = MessageHeaders::new();
    /// headers.set_header_multi_value("Via", via.clone(), HeaderMultiMode::MultiLine);
    /// assert_eq!(
    ///     concat!(
    ///         "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3\r\n",
    ///         "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n",
    ///         "Via: SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
    ///         "\r\n",
    ///     ).as_bytes(),
    ///     headers.generate()?
    /// );
    /// # Ok(())
    /// # }
    MultiLine,
}

/// This type is used to parse and generate the headers of Internet messages,
/// which consist of text lines, with an empty line separating the headers from
/// the body.  The format is specified in [IETF RFC
/// 5322](https://tools.ietf.org/html/rfc5322).
#[derive(Clone, Debug, Default)]
pub struct MessageHeaders {
    headers: Vec<Header>,
    line_length_limit: Option<usize>,
}

impl MessageHeaders {
    /// Append the given header.  This does not change or remove any previously
    /// added headers, even if they have the same name.  To replace a
    /// previously added header, use [`set_header`](#method.set_header)
    /// instead.
    pub fn add_header<H>(
        &mut self,
        header: H,
    ) where
        H: Into<Header>,
    {
        self.headers.push(header.into());
    }

    /// Append one or more headers with the given name and values derived from
    /// the given vector, depending on the given mode (see
    /// [`HeaderMultiMode`](enum.HeaderMultiMode.html) for more information).
    /// This does not change or remove any previously added headers, even if
    /// they have the same name.  To replace a previously added header, use
    /// [`set_header_multi_value`](#method.set_header_multi_value) instead.
    pub fn add_header_multi_value<N, V>(
        &mut self,
        name: N,
        values: V,
        mode: HeaderMultiMode,
    ) where
        N: AsRef<str>,
        V: Into<Vec<String>>,
    {
        let values = values.into();
        if values.is_empty() {
            return;
        }
        match mode {
            HeaderMultiMode::OneLine => {
                self.add_header(Header {
                    name: name.as_ref().into(),
                    value: values.join(","),
                });
            },
            HeaderMultiMode::MultiLine => {
                for value in values {
                    let name = name.as_ref();
                    self.add_header(Header {
                        name: name.into(),
                        value,
                    });
                }
            },
        }
    }

    /// Produce the text string form of the message, according to
    /// the rules of RFC 5322:
    ///
    /// * Each header is separated by a carriage-return line-feed sequence
    ///   (CRLF).
    /// * Headers may be folded onto multiple lines at whitespace characters
    ///   (which are strictly space ('\x20') and horizontal tab ('\x09').
    /// * The first line of a header consists of the header name followed by a
    ///   colon (':') followed by a space ('\x20') followed by part or all of
    ///   the header value.
    /// * After all headers, an empty line is placed to mark where the body
    ///   begins.
    ///
    /// # Errors
    ///
    /// `Error::HeaderLineCouldNotBeFolded` is returned if the generator
    /// is configured with a line limit constraint and one or more headers
    /// are too long and cannot be folded to fit within the constraint.
    ///
    /// While technically it shouldn't happen, logically we may return
    /// `Error::StringFormat` if one of the internal string formatting
    /// functions should fail (which they shouldn't unless something
    /// used internally doesn't implement `Display` properly.
    pub fn generate(&self) -> Result<Vec<u8>, Error> {
        let mut raw_string = String::new();
        for header in &self.headers {
            let line_buffer = format!("{}: {}", header.name.0, header.value);
            if let Some(line_length_limit) = self.line_length_limit {
                let mut rest = &line_buffer[..];
                let mut skip = header.name.0.len() + 2;
                while !rest.is_empty() {
                    let (part, rest_out) =
                        fold_header(rest, line_length_limit - 2, skip)?;
                    rest = rest_out;
                    skip = 1;
                    write!(&mut raw_string, "{}{}", part, CRLF)?;
                }
            } else {
                write!(&mut raw_string, "{}{}", line_buffer, CRLF)?;
            }
        }
        write!(&mut raw_string, "{}", CRLF)?;
        Ok(raw_string.into_bytes())
    }

    /// Borrow the list of headers.
    #[must_use]
    pub fn headers(&self) -> &Vec<Header> {
        &self.headers
    }

    /// Produce a list of the values for the headers with the given name.
    /// If no headers have the given name, an empty list is returned.
    #[must_use]
    pub fn header_multi_value<T>(
        &self,
        name: T,
    ) -> Vec<String>
    where
        T: AsRef<str>,
    {
        let name = name.as_ref();
        self.headers
            .iter()
            .filter_map(|header| {
                if header.name == name {
                    Some(header.value.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Take the value of the header with the given name, interpret it as a
    /// comma-separated list of values, and produce that list.  If no headers
    /// have the given name, an empty list is returned.
    #[must_use]
    pub fn header_tokens<T>(
        &self,
        name: T,
    ) -> Vec<String>
    where
        T: AsRef<str>,
    {
        self.header_multi_value(name)
            .iter()
            .flat_map(|value| {
                value
                    .split_terminator(',')
                    .map(str::trim)
                    .map(str::to_ascii_lowercase)
            })
            .collect()
    }

    /// Produce the value for the header with the given name, if any.
    /// If there are two or more headers with the same name, the produced
    /// string will be the joining of their values with commas.
    #[must_use]
    pub fn header_value<T>(
        &self,
        name: T,
    ) -> Option<String>
    where
        T: AsRef<str>,
    {
        let name = name.as_ref();
        self.headers.iter().fold(None, |composite, header| {
            if header.name == name {
                Some(composite.map_or_else(
                    || header.value.clone(),
                    |mut composite| {
                        composite += ",";
                        composite += &header.value;
                        composite
                    },
                ))
            } else {
                composite
            }
        })
    }

    /// This is a convenience function which essentially looks up the tokens
    /// in a header using [`header_tokens`](#method.header_tokens)
    /// and searches them to see if the given token is among them.
    pub fn has_header_token<T>(
        &self,
        name: T,
        token: T,
    ) -> bool
    where
        T: AsRef<str>,
    {
        let token = token.as_ref();
        let normalized_token = token.to_ascii_lowercase();
        self.header_tokens(name)
            .iter()
            .any(|token_in_header| *token_in_header == normalized_token)
    }

    /// Determine whether or not the given header is present.
    #[must_use]
    pub fn has_header<T>(
        &self,
        name: T,
    ) -> bool
    where
        T: AsRef<str>,
    {
        let name = name.as_ref();
        self.headers.iter().any(|header| header.name == name)
    }

    /// Create a new instance with no headers in it.
    #[must_use]
    pub fn new() -> Self {
        Self {
            headers: Vec::new(),
            line_length_limit: None,
        }
    }

    /// Feed more Internet message text into the parser, building the
    /// collection of headers internally, and detecting when the end of
    /// the headers and the beginning of the body is found.
    ///
    /// This function may be called multiple times to parse input
    /// incrementally.  Each call returns an indication of whether or
    /// not a message was parsed and how many input bytes were consumed.
    ///
    /// # Errors
    ///
    /// * `HeaderLineTooLong` &nash; the parser is configured with a line length
    ///   constraint and the input exceeds it on a line
    /// * `HeaderLineMissingColon` &ndash; there is no colon between the name
    ///   and value of a header line
    /// * `HeaderNameContainsIllegalCharacter` &ndash; the name of a header
    ///   contains an illegal character
    /// * `HeaderValueContainsIllegalCharacter` &ndash; the value of a header
    ///   contains an illegal character
    ///
    /// # Examples
    ///
    /// ```rust
    /// # extern crate rhymessage;
    /// use rhymessage::{MessageHeaders, ParseResults, ParseStatus};
    ///
    /// # fn main() -> Result<(), rhymessage::Error> {
    /// let mut headers = MessageHeaders::new();
    ///
    /// // `parse` does not consume first line because the next line
    /// // it hasn't seen yet might contain part of the "From" header.
    /// assert_eq!(
    ///     ParseResults{
    ///         status: ParseStatus::Incomplete,
    ///         consumed: 0,
    ///     },
    ///     headers.parse("From: joe@example.com\r\n")?
    /// );
    ///
    /// // So we re-send the "From" line along with the content that follows.
    /// // At this point, `parse` knows it has the full "From" header, and
    /// // consumes it, but not the "To" header, since the next line might
    /// // be a continuation of it.
    /// assert_eq!(
    ///     ParseResults{
    ///         status: ParseStatus::Incomplete,
    ///         consumed: 23,
    ///     },
    ///     headers.parse(concat!(
    ///         "From: joe@example.com\r\n",
    ///         "To: sal@example.com\r\n",
    ///     ))?
    /// );
    ///
    /// // So we re-send the "To" line along with the content that follows.
    /// // At this point, `parse` knows it has the full "To" header, and
    /// // consumes it, but not the "Subject" header, since the next line might
    /// // be a continuation of it.
    /// assert_eq!(
    ///     ParseResults{
    ///         status: ParseStatus::Incomplete,
    ///         consumed: 21,
    ///     },
    ///     headers.parse(concat!(
    ///         "To: sal@example.com\r\n",
    ///         "Subject: Hello,\r\n",
    ///     ))?
    /// );
    ///
    /// // So we re-send the "Subject" line along with the content that
    /// // follows.  At this point, `parse` still doesn't know it has the
    /// // full "Subject" header, so nothing is consumed.
    /// assert_eq!(
    ///     ParseResults{
    ///         status: ParseStatus::Incomplete,
    ///         consumed: 0,
    ///     },
    ///     headers.parse(concat!(
    ///         "Subject: Hello,\r\n",
    ///         " World!\r\n",
    ///     ))?
    /// );
    ///
    /// // So we re-send again with even more content.  At this point,
    /// // `parse` sees the empty line separating headers from body,
    /// // so it can finally consume the "Subject" header as well as
    /// // indicate that parsing the headers is complete.
    /// assert_eq!(
    ///     ParseResults{
    ///         status: ParseStatus::Complete,
    ///         consumed: 28,
    ///     },
    ///     headers.parse(concat!(
    ///         "Subject: Hello,\r\n",
    ///         " World!\r\n",
    ///         "\r\n",
    ///     ))?
    /// );
    /// # Ok(())
    /// # }
    pub fn parse<T>(
        &mut self,
        raw_message: T,
    ) -> Result<ParseResults, Error>
    where
        T: AsRef<[u8]>,
    {
        let mut offset = 0;
        let raw_message = raw_message.as_ref();
        while offset < raw_message.len() {
            // Find the end of the current line.
            let line_terminator = find_crlf(&raw_message[offset..]);
            if line_terminator.is_none() {
                if let Some(line_length_limit) = self.line_length_limit {
                    let unterminated_line_length = raw_message.len() - offset;
                    if unterminated_line_length + 2 > line_length_limit {
                        let end = std::cmp::min(
                            unterminated_line_length,
                            line_length_limit,
                        );
                        return Err(Error::HeaderLineTooLong(
                            raw_message[offset..offset + end].to_vec(),
                        ));
                    }
                }
                break;
            }

            // Bail if the line is longer than the limit (if set).
            let line_terminator = line_terminator.unwrap();
            if let Some(line_length_limit) = self.line_length_limit {
                if line_terminator + CRLF.len() > line_length_limit {
                    return Err(Error::HeaderLineTooLong(
                        raw_message[offset..offset + line_length_limit]
                            .to_vec(),
                    ));
                }
            }

            // Stop if empty line is found -- this is where
            // the headers end and the body (which we don't parse,
            // but leave up to the user to handle) begins.
            if line_terminator == 0 {
                offset += CRLF.len();
                return Ok(ParseResults {
                    status: ParseStatus::Complete,
                    consumed: offset,
                });
            }

            // Separate the header name from the header value.
            let header = separate_header_name_and_value(
                std::str::from_utf8(
                    &raw_message[offset..offset + line_terminator],
                )
                .map_err(|error| {
                    Error::HeaderLineInvalidText {
                        line: raw_message[offset..offset + line_terminator]
                            .to_vec(),
                        source: error,
                    }
                })?,
            )?;
            // Check the first value segment for validity.
            validate_header_value(&header, &header.value)?;

            // Look ahead in the raw message and perform line unfolding
            // if we see any lines that begin with whitespace.
            let next_offset = offset + line_terminator + CRLF.len();
            if let Some((mut header, consumed)) =
                unfold_header(&raw_message[next_offset..], header)?
            {
                // Remove any whitespace that might be at the beginning
                // or end of the header value, and then store the
                // header.
                header.value = header.value.trim().to_string();
                self.headers.push(header);
                offset = next_offset + consumed;
            } else {
                return Ok(ParseResults {
                    status: ParseStatus::Incomplete,
                    consumed: offset,
                });
            }
        }
        Ok(ParseResults {
            status: ParseStatus::Incomplete,
            consumed: offset,
        })
    }

    /// Remove all headers with the given name.
    pub fn remove_header<N>(
        &mut self,
        name: N,
    ) where
        N: AsRef<str>,
    {
        let name = name.as_ref();
        self.headers.retain(|header| header.name != name);
    }

    /// Add or replace a header with the given name and value.  If one or more
    /// previously added headers has the same name, the first one is changed to
    /// hold the given value, and the others are removed.  To add a new header
    /// without replacing or removing previously added ones with the same name,
    /// use [`add_header`](#method.add_header) instead.
    pub fn set_header<N, V>(
        &mut self,
        name: N,
        value: V,
    ) where
        N: AsRef<str>,
        V: Into<String>,
    {
        let name = name.as_ref();
        let mut value = value.into();

        // This code is inspired by `Vec::retain`.  We're essentially
        // doing the same thing, except we're modifying the
        // first matching header rather than discarding it.
        let len = self.headers.len();
        let mut matches = 0;
        for i in 0..len {
            if self.headers[i].name == name {
                if matches == 0 {
                    std::mem::swap(&mut self.headers[i].value, &mut value);
                }
                matches += 1;
            } else if matches > 1 {
                self.headers.swap(i - matches + 1, i);
            }
        }
        if matches > 1 {
            self.headers.truncate(len - matches + 1);
        } else if matches == 0 {
            self.add_header(Header {
                name: name.into(),
                value,
            });
        }
    }

    /// Add or replace one or more headers with the given name and values
    /// derived from the given vector, depending on the given mode (see
    /// [`HeaderMultiMode`](enum.HeaderMultiMode.html) for more information).
    /// If one or more previously added headers has the same name, the first
    /// one is changed to hold the given value(s), and the others are removed.
    /// To add new headers without replacing or removing previously added ones
    /// with the same name, use
    /// [`add_header_multi_value`](#method.add_header_multi_value) instead.
    pub fn set_header_multi_value<N, V>(
        &mut self,
        name: N,
        values: V,
        mode: HeaderMultiMode,
    ) where
        N: AsRef<str>,
        V: Into<Vec<String>>,
    {
        let values = values.into();
        if values.is_empty() {
            return;
        }
        match mode {
            HeaderMultiMode::OneLine => {
                self.set_header(name, values.join(","));
            },
            HeaderMultiMode::MultiLine => {
                let name = name.as_ref();
                for (i, value) in values.into_iter().enumerate() {
                    if i == 0 {
                        self.set_header(name, value);
                    } else {
                        self.add_header(Header {
                            name: name.into(),
                            value,
                        });
                    }
                }
            },
        }
    }

    /// This configures the parser and generator to constrain the lengths
    /// of header lines to no more than the given number, if any.  Setting
    /// `None` removes any constraint.
    pub fn set_line_limit(
        &mut self,
        limit: Option<usize>,
    ) {
        self.line_length_limit = limit;
    }
}

impl IntoIterator for MessageHeaders {
    type IntoIter = std::vec::IntoIter<Header>;
    type Item = Header;

    fn into_iter(self) -> Self::IntoIter {
        self.headers.into_iter()
    }
}

impl<'a> IntoIterator for &'a MessageHeaders {
    type IntoIter = std::slice::Iter<'a, Header>;
    type Item = &'a Header;

    fn into_iter(self) -> Self::IntoIter {
        (&self.headers).iter()
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn header_name_equivalency() {
        named_tuple!(
            struct TestVector {
                expected_result: bool,
                lhs: &'static str,
                rhs: &'static str,
            }
        );
        let test_vectors: &[TestVector] = &[
            (true, "hello", "hello").into(),
            (true, "Hello", "hello").into(),
            (false, "jello", "hello").into(),
            (false, "hello", "hell").into(),
        ];
        for test_vector in test_vectors.iter() {
            let lhs = HeaderName::from(*test_vector.lhs());
            let rhs = HeaderName::from(*test_vector.rhs());
            assert_eq!(*test_vector.expected_result(), (lhs == rhs));
        }
    }

    #[test]
    fn header_name_rank() {
        named_tuple!(
            struct TestVector {
                expected_result: bool,
                lhs: &'static str,
                rhs: &'static str,
            }
        );
        let test_vectors: &[TestVector] = &[
            (false, "hello", "hello").into(),
            (false, "Hello", "hello").into(),
            (false, "hello", "Hello").into(),
            (false, "jello", "hello").into(),
            (true, "hello", "jello").into(),
            (false, "hello", "hell").into(),
            (true, "hell", "hello").into(),
        ];
        for test_vector in test_vectors.iter() {
            let lhs = HeaderName::from(*test_vector.lhs());
            let rhs = HeaderName::from(*test_vector.rhs());
            assert_eq!(
                *test_vector.expected_result(),
                (lhs < rhs),
                "{} < {}",
                test_vector.lhs(),
                test_vector.rhs()
            );
        }
    }

    #[test]
    fn header_name_to_string() {
        let name = HeaderName::from("Content-Length");
        let name_as_str = name.as_ref();
        assert_eq!("Content-Length", name);
        assert_eq!("Content-Length", name_as_str);
        assert_eq!("Content-Length", name.to_string());
        assert_eq!("Content-Length", format!("{}", name));
    }

    #[test]
    fn http_client_request_message() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        let header_collection = headers.headers();
        named_tuple!(
            struct ExpectedHeader {
                name: &'static str,
                value: &'static str,
            }
        );
        let expected_headers: &[ExpectedHeader] = &[
            (
                "User-Agent",
                "curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3",
            )
                .into(),
            ("Host", "www.example.com").into(),
            ("Accept-Language", "en, mi").into(),
        ];
        assert_eq!(expected_headers.len(), header_collection.len());
        expected_headers.iter().zip(header_collection.iter()).for_each(
            |(expected, actual)| {
                assert_eq!(*expected.name(), actual.name);
                assert_eq!(*expected.value(), actual.value);
            },
        );
        assert!(headers.has_header("Host"));
        assert!(!headers.has_header("Foobar"));
        assert_eq!(Ok(raw_message.as_bytes()), headers.generate().as_deref());
    }

    #[test]
    fn http_server_response_message() {
        let mut headers = MessageHeaders::new();
        let raw_headers = concat!(
            "Date: Mon, 27 Jul 2009 12:28:53 GMT\r\n",
            "Server: Apache\r\n",
            "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\r\n",
            "ETag: \"34aa387-d-1568eb00\"\r\n",
            "Accept-Ranges: bytes\r\n",
            "Content-Length: 51\r\n",
            "Vary: Accept-Encoding\r\n",
            "Content-Type: text/plain\r\n",
            "\r\n",
        );
        let raw_message = String::from(raw_headers)
            + "Hello World! My payload includes a trailing CRLF.\r\n";
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_headers.len(),
            }),
            headers.parse(raw_message)
        );
        let header_collection = headers.headers();
        named_tuple!(
            struct ExpectedHeader {
                name: &'static str,
                value: &'static str,
            }
        );
        let expected_headers: &[ExpectedHeader] = &[
            ("Date", "Mon, 27 Jul 2009 12:28:53 GMT").into(),
            ("Server", "Apache").into(),
            ("Last-Modified", "Wed, 22 Jul 2009 19:15:56 GMT").into(),
            ("ETag", "\"34aa387-d-1568eb00\"").into(),
            ("Accept-Ranges", "bytes").into(),
            ("Content-Length", "51").into(),
            ("Vary", "Accept-Encoding").into(),
            ("Content-Type", "text/plain").into(),
        ];
        assert_eq!(expected_headers.len(), header_collection.len());
        expected_headers.iter().zip(header_collection.iter()).for_each(
            |(expected, actual)| {
                assert_eq!(*expected.name(), actual.name);
                assert_eq!(*expected.value(), actual.value);
            },
        );
        assert!(headers.has_header("Last-Modified"));
        assert!(!headers.has_header("Foobar"));
        assert_eq!(Ok(raw_headers.as_bytes()), headers.generate().as_deref());
    }

    #[test]
    fn header_line_almost_too_long() {
        let mut headers = MessageHeaders::new();
        headers.set_line_limit(Some(1000));
        let test_header_name = "X-Poggers";
        let test_header_name_with_delimiters =
            String::from(test_header_name) + ": ";
        let longest_possible_poggers =
            "X".repeat(998 - test_header_name_with_delimiters.len());
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
        )
        .to_string()
            + &test_header_name_with_delimiters
            + &longest_possible_poggers
            + "\r\n"
            + "Accept-Language: en, mi\r\n"
            + "\r\n";
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(
            Some(longest_possible_poggers),
            headers.header_value(test_header_name)
        );
    }

    #[test]
    fn header_line_too_long() {
        let mut headers = MessageHeaders::new();
        headers.set_line_limit(Some(1000));
        let test_header_name = "X-Poggers";
        let test_header_name_with_delimiters =
            String::from(test_header_name) + ": ";
        let too_long_poggers =
            "X".repeat(999 - test_header_name_with_delimiters.len());
        let too_long_header = test_header_name_with_delimiters.clone()
            + &too_long_poggers
            + "\r\n";
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
        )
        .to_string()
            + &test_header_name_with_delimiters
            + &too_long_poggers
            + "\r\n"
            + "Accept-Language: en, mi\r\n"
            + "\r\n";
        assert_eq!(
            Err(Error::HeaderLineTooLong(
                too_long_header[0..1000].as_bytes().to_vec()
            )),
            headers.parse(raw_message)
        );
    }

    #[test]
    fn header_line_too_long_and_not_terminated() {
        let mut headers = MessageHeaders::new();
        headers.set_line_limit(Some(1000));
        let test_header_name = "X-Poggers";
        let test_header_name_with_delimiters =
            String::from(test_header_name) + ": ";
        let value_is_too_long =
            "X".repeat(999 - test_header_name_with_delimiters.len());
        let raw_message = test_header_name_with_delimiters + &value_is_too_long;
        assert_eq!(
            Err(Error::HeaderLineTooLong(raw_message.clone().into_bytes())),
            headers.parse(raw_message)
        );
    }

    #[test]
    fn header_line_over1000_characters_allowed_by_default() {
        let mut headers = MessageHeaders::new();
        let test_header_name = "X-Poggers";
        let test_header_name_with_delimiters =
            String::from(test_header_name) + ": ";
        let long_poggers =
            "X".repeat(999 - test_header_name_with_delimiters.len());
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
        )
        .to_string()
            + &test_header_name_with_delimiters
            + &long_poggers
            + "\r\n"
            + "Accept-Language: en, mi\r\n"
            + "\r\n";
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(Some(long_poggers), headers.header_value(test_header_name));
    }

    #[test]
    fn empty_message() {
        let mut headers = MessageHeaders::new();
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Incomplete,
                consumed: 0,
            }),
            headers.parse("")
        );
    }

    #[test]
    fn single_line_truncated() {
        let mut headers = MessageHeaders::new();
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Incomplete,
                consumed: 0,
            }),
            headers.parse("User-Agent: curl")
        );
    }

    #[test]
    fn single_line_not_truncated() {
        let mut headers = MessageHeaders::new();
        let input = "User-Agent: curl\r\n\r\n";
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: input.len(),
            }),
            headers.parse(input)
        );
    }

    #[test]
    fn no_headers_at_all() {
        let mut headers = MessageHeaders::new();
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: 2,
            }),
            headers.parse("\r\n Something Else Not Part Of The Message")
        );
        assert!(headers.headers().is_empty());
    }

    #[test]
    fn get_value_of_present_header() {
        let mut headers = MessageHeaders::new();
        let raw_message_headers = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        let raw_message = String::from(raw_message_headers)
            + " Something Else Not Part Of The Message";
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message_headers.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(
            Some("www.example.com"),
            headers.header_value("Host").as_deref()
        );
    }

    #[test]
    fn set_header_add() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        headers.set_header("X", "PogChamp");
        assert_eq!(
            Ok(concat!(
                "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
                "Host: www.example.com\r\n",
                "Accept-Language: en, mi\r\n",
                "X: PogChamp\r\n",
                "\r\n",
            )
            .as_bytes()),
            headers.generate().as_deref()
        );
    }

    #[test]
    fn set_header_replace() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        headers.set_header("Host", "example.com");
        assert_eq!(
            Ok(concat!(
                "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
                "Host: example.com\r\n",
                "Accept-Language: en, mi\r\n",
                "\r\n",
            )
            .as_bytes()),
            headers.generate().as_deref()
        );
    }

    #[test]
    fn get_value_of_missing_header() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(None, headers.header_value("PePe"));
    }

    #[test]
    fn header_with_character_less_than_33_in_name() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Feels Bad Man: LUL\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Err(Error::HeaderNameContainsIllegalCharacter(String::from(
                "Feels Bad Man"
            ))),
            headers.parse(raw_message)
        );
    }

    #[test]
    fn header_with_character_greater_than_126_in_name() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "FeelsBadMan\x7f: LUL\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Err(Error::HeaderNameContainsIllegalCharacter(String::from(
                "FeelsBadMan\x7f"
            ))),
            headers.parse(raw_message)
        );
    }

    #[test]
    fn header_with_illegal_character_in_value_first_segment() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.çom\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Err(Error::HeaderValueContainsIllegalCharacter {
                name: HeaderName::from("Host"),
                value_segment: String::from(" www.example.çom")
            }),
            headers.parse(raw_message)
        );
    }

    #[test]
    fn header_with_illegal_character_in_value_second_segment() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en,\r\n",
            " mi, ça\r\n",
            "\r\n",
        );
        assert_eq!(
            Err(Error::HeaderValueContainsIllegalCharacter {
                name: HeaderName::from("Accept-Language"),
                value_segment: String::from(" mi, ça")
            }),
            headers.parse(raw_message)
        );
    }

    #[test]
    fn header_with_colon_in_name() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Feels:BadMan: LUL\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        let header_collection = headers.headers();
        named_tuple!(
            struct ExpectedHeader {
                name: &'static str,
                value: &'static str,
            }
        );
        let expected_headers: &[ExpectedHeader] = &[
            (
                "User-Agent",
                "curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3",
            )
                .into(),
            ("Host", "www.example.com").into(),
            ("Feels", "BadMan: LUL").into(),
            ("Accept-Language", "en, mi").into(),
        ];
        assert_eq!(expected_headers.len(), header_collection.len());
        expected_headers.iter().zip(header_collection.iter()).for_each(
            |(expected_header, actual_header)| {
                assert_eq!(*expected_header.name(), actual_header.name);
                assert_eq!(*expected_header.value(), actual_header.value);
            },
        );
    }

    #[test]
    fn header_value_unfolding_single_wsp() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "Subject: This\r\n",
            " is a test\r\n",
            "\r\n",
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(
            Some("This is a test"),
            headers.header_value("Subject").as_deref()
        );
    }

    #[test]
    fn header_value_unfolding_multiple_wsp() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "Subject: This\r\n",
            "    is a test\r\n",
            "\r\n",
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(
            Some("This is a test"),
            headers.header_value("Subject").as_deref()
        );
    }

    #[test]
    fn fold_line_that_would_exceed_limit() {
        let header_name = "X";
        named_tuple!(
            struct TestVector {
                header_value: &'static str,
                expected_lines: Option<&'static [&'static str]>,
            }
        );
        let test_vectors: &[TestVector] = &[
            // ...................... Some(&["..........", "..........",
            // "..........", "", ""][..])).into(),
            ("Hello!", Some(&["X: Hello!", "", ""][..])).into(),
            ("Hello!!", Some(&["X: Hello!!", "", ""][..])).into(),
            ("Hello!!!", None).into(),
            ("Hello, World!", Some(&["X: Hello,", " World!", "", ""][..]))
                .into(),
            (
                "This is even longer!",
                Some(&["X: This is", " even", " longer!", "", ""][..]),
            )
                .into(),
            (
                "This is even long er!",
                Some(&["X: This is", " even long", " er!", "", ""][..]),
            )
                .into(),
            ("This is evenlonger!", None).into(),
            ("sadfjkasdfjlkasdfjla", None).into(),
        ];
        for test_vector in test_vectors {
            let mut headers = MessageHeaders::new();
            headers.set_line_limit(Some(12));
            headers.set_header(header_name, *test_vector.header_value());
            let raw_headers = headers.generate();
            if let Some(expected_lines) = test_vector.expected_lines() {
                assert!(raw_headers.is_ok());
                let raw_headers = raw_headers.unwrap();
                let raw_headers = std::str::from_utf8(&raw_headers).unwrap();
                let actual_lines =
                    raw_headers.split("\r\n").collect::<Vec<&str>>();
                assert_eq!(*expected_lines, actual_lines);
            } else {
                assert!(raw_headers.is_err());
            }
        }
    }

    #[test]
    fn header_names_should_be_case_insensive() {
        named_tuple!(
            struct TestVector {
                header_name: &'static str,
                should_also_match: &'static [&'static str],
            }
        );
        let test_vectors: &[TestVector] = &[
            (
                "Content-Type",
                &[
                    "content-type",
                    "CONTENT-TYPE",
                    "Content-type",
                    "CoNtENt-TYpe",
                ][..],
            )
                .into(),
            ("ETag", &["etag", "ETAG", "Etag", "eTag", "etaG"][..]).into(),
        ];
        for test_vector in test_vectors {
            let mut headers = MessageHeaders::new();
            headers.set_header(test_vector.header_name(), "HeyGuys");
            for alternative in *test_vector.should_also_match() {
                assert!(headers.has_header(alternative));
            }
        }
    }

    #[test]
    fn get_header_multiple_values() {
        let raw_message = concat!(
            "Via: SIP/2.0/UDP server10.biloxi.com\r\n",
            "    ;branch=z9hG4bKnashds8;received=192.0.2.3\r\n",
            "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com\r\n",
            "    ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n",
            "Via: SIP/2.0/UDP pc33.atlanta.com\r\n",
            "    ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
            "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
            "\r\n",
        );
        let mut headers = MessageHeaders::new();
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(
            Some(concat!(
                "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
                "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
                "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1",
            )),
            headers.header_value("Via").as_deref()
        );
        assert_eq!(
            &[
                "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3",
                "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2",
                "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1",
            ][..],
            headers.header_multi_value("Via")
        );
        assert_eq!(
            Some("Bob <sip:bob@biloxi.com>;tag=a6c85cf"),
            headers.header_value("To").as_deref()
        );
        assert_eq!(
            &["Bob <sip:bob@biloxi.com>;tag=a6c85cf"][..],
            headers.header_multi_value("To")
        );
        assert!(headers.header_multi_value("PogChamp").is_empty());
    }

    #[test]
    fn set_header_multi_valueple_values() {
        let via: Vec<String> = [
            "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3",
            "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2",
            "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1"
        ].iter().map(|s| String::from(*s)).collect();
        let to: Vec<String> = ["Bob <sip:bob@biloxi.com>;tag=a6c85cf"]
            .iter()
            .map(|s| String::from(*s))
            .collect();
        let mut headers = MessageHeaders::new();
        headers.set_header_multi_value(
            "Via",
            via.clone(),
            HeaderMultiMode::OneLine,
        );
        headers.set_header_multi_value(
            "To",
            to.clone(),
            HeaderMultiMode::OneLine,
        );
        headers.set_header_multi_value(
            "FeelsBadMan",
            vec![] as Vec<String>,
            HeaderMultiMode::OneLine,
        );
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
                    "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
                    "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
        headers = MessageHeaders::new();
        headers.set_header_multi_value("Via", via, HeaderMultiMode::MultiLine);
        headers.set_header_multi_value("To", to, HeaderMultiMode::MultiLine);
        headers.set_header_multi_value(
            "FeelsBadMan",
            vec![] as Vec<String>,
            HeaderMultiMode::MultiLine,
        );
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3\r\n",
                "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n",
                "Via: SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
    }

    #[test]
    fn set_header_should_replace_all_previous_values() {
        let via: Vec<String> = [
            "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3",
            "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2",
            "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1"
        ].iter().map(|s| String::from(*s)).collect();
        let mut headers = MessageHeaders::new();
        headers.set_header_multi_value("Via", via, HeaderMultiMode::OneLine);
        headers.set_header("To", "Bob <sip:bob@biloxi.com>;tag=a6c85cf");
        headers
            .set_header("From", "Alice <sip:alice@atlanta.com>;tag=1928301774");
        headers.add_header(Header {
            name: "Via".into(),
            value: "Trickster".into(),
        });
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
                    "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
                    "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n",
                "Via: Trickster\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
        headers.set_header("Via", "Kappa");
        assert_eq!(
            Ok(concat!(
                "Via: Kappa\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n",
                "\r\n",
            )
            .as_bytes()),
            headers.generate().as_deref()
        );
    }

    #[test]
    fn add_header() {
        let via: Vec<String> = [
            "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3",
            "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2",
            "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1"
        ].iter().map(|s| String::from(*s)).collect();
        let mut headers = MessageHeaders::new();
        headers.set_header_multi_value("Via", via, HeaderMultiMode::OneLine);
        headers.set_header("To", "Bob <sip:bob@biloxi.com>;tag=a6c85cf");
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
                    "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
                    "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
        headers.add_header(Header {
            name: "From".into(),
            value: "Alice <sip:alice@atlanta.com>;tag=1928301774".into(),
        });
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
                    "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
                    "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
        let x_pepe: Vec<String> =
            ["<3", "SeemsGood"].iter().map(|s| String::from(*s)).collect();
        headers.add_header_multi_value(
            "X-PePe",
            x_pepe,
            HeaderMultiMode::OneLine,
        );
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
                    "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
                    "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n",
                "X-PePe: <3,SeemsGood\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
        let to: Vec<String> =
            ["Carol"].iter().map(|s| String::from(*s)).collect();
        headers.add_header_multi_value("To", to, HeaderMultiMode::OneLine);
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3,",
                    "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2,",
                    "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n",
                "X-PePe: <3,SeemsGood\r\n",
                "To: Carol\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
    }

    #[test]
    fn remove_header() {
        let via: Vec<String> = [
            "SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3",
            "SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2",
            "SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1"
        ].iter().map(|s| String::from(*s)).collect();
        let mut headers = MessageHeaders::new();
        headers.set_header_multi_value("Via", via, HeaderMultiMode::MultiLine);
        headers.set_header("To", "Bob <sip:bob@biloxi.com>;tag=a6c85cf");
        headers.add_header(Header {
            name: "From".into(),
            value: "Alice <sip:alice@atlanta.com>;tag=1928301774".into(),
        });
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3\r\n",
                "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n",
                "Via: SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
        headers.remove_header("From");
        assert_eq!(
            Ok(concat!(
                "Via: SIP/2.0/UDP server10.biloxi.com ;branch=z9hG4bKnashds8;received=192.0.2.3\r\n",
                "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n",
                "Via: SIP/2.0/UDP pc33.atlanta.com ;branch=z9hG4bK776asdhds ;received=192.0.2.1\r\n",
                "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n",
                "\r\n",
            ).as_bytes()),
            headers.generate().as_deref()
        );
        headers.remove_header("Via");
        assert_eq!(
            Ok(concat!("To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n", "\r\n",).as_bytes()),
            headers.generate().as_deref()
        );
    }

    #[test]
    fn header_tokens() {
        let raw_message = concat!(
            "Foo: bar, Spam,  heLLo\r\n",
            "Bar: Foo \r\n",
            "Spam:   \t  \r\n",
            "\r\n",
        );
        let mut headers = MessageHeaders::new();
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert_eq!(
            &["bar", "spam", "hello",][..],
            headers.header_tokens("Foo")
        );
        assert_eq!(&["foo",][..], headers.header_tokens("Bar"));
        assert_eq!(&[] as &[String], headers.header_tokens("Spam"));
    }

    #[test]
    fn has_header_token() {
        let raw_message = concat!(
            "Foo: bar, Spam,  heLLo\r\n",
            "Bar: Foo \r\n",
            "Spam:   \t  \r\n",
            "\r\n",
        );
        let mut headers = MessageHeaders::new();
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: raw_message.len(),
            }),
            headers.parse(raw_message)
        );
        assert!(headers.has_header_token("Foo", "bar"));
        assert!(headers.has_header_token("Foo", "Bar"));
        assert!(headers.has_header_token("Foo", "spam"));
        assert!(headers.has_header_token("Foo", "hello"));
        assert!(!headers.has_header_token("Foo", "xyz"));
        assert!(!headers.has_header_token("Foo", "secret_to_the_universe"));
        assert!(headers.has_header_token("Bar", "foo"));
        assert!(!headers.has_header_token("Bar", "spam"));
        assert!(!headers.has_header_token("Spam", "foo"));
        assert!(!headers.has_header_token("Spam", "spam"));
    }

    #[test]
    fn clone_headers() {
        let mut original_headers = MessageHeaders::new();
        original_headers.set_header("Foo", "Bar");
        original_headers.set_header("Hello", "World");
        let mut headers_copy = original_headers.clone();
        headers_copy.set_header("Hello", "PePe");
        assert_eq!(
            Some("Bar"),
            original_headers.header_value("Foo").as_deref()
        );
        assert_eq!(
            Some("World"),
            original_headers.header_value("Hello").as_deref()
        );
        assert_eq!(Some("Bar"), headers_copy.header_value("Foo").as_deref());
        assert_eq!(Some("PePe"), headers_copy.header_value("Hello").as_deref());
    }

    #[test]
    fn http_client_request_message_in_two_parts_divided_between_header_lines() {
        let mut headers = MessageHeaders::new();
        let raw_message_pieces = &[
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        ][..];
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Incomplete,
                consumed: raw_message_pieces[0].len(),
            }),
            headers.parse(
                String::from(raw_message_pieces[0]) + raw_message_pieces[1]
            )
        );
        assert_eq!(
            Ok(ParseResults {
                status: ParseStatus::Complete,
                consumed: (raw_message_pieces[1].len()
                    + raw_message_pieces[2].len()
                    + raw_message_pieces[3].len()),
            }),
            headers.parse(
                String::from(raw_message_pieces[1])
                    + raw_message_pieces[2]
                    + raw_message_pieces[3]
            )
        );
        let header_collection = headers.headers();
        named_tuple!(
            struct ExpectedHeader {
                name: &'static str,
                value: &'static str,
            }
        );
        let expected_headers: &[ExpectedHeader] = &[
            (
                "User-Agent",
                "curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3",
            )
                .into(),
            ("Host", "www.example.com").into(),
            ("Accept-Language", "en, mi").into(),
        ];
        assert_eq!(expected_headers.len(), header_collection.len());
        expected_headers.iter().zip(header_collection.iter()).for_each(
            |(expected_header, actual_header)| {
                assert_eq!(*expected_header.name(), actual_header.name);
                assert_eq!(*expected_header.value(), actual_header.value);
            },
        );
        assert!(headers.has_header("Host"));
        assert!(!headers.has_header("Foobar"));
        assert_eq!(
            Ok(raw_message_pieces
                .iter()
                .flat_map(|s| s.as_bytes())
                .copied()
                .collect()),
            headers.generate()
        );
    }

    #[test]
    fn headers_into_iter_move() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        headers.parse(raw_message).unwrap();
        assert_eq!(
            vec![
                Header {
                    name: "User-Agent".into(),
                    value:
                        "curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3"
                            .into(),
                },
                Header {
                    name: "Host".into(),
                    value: "www.example.com".into(),
                },
                Header {
                    name: "Accept-Language".into(),
                    value: "en, mi".into(),
                },
            ],
            headers.into_iter().collect::<Vec<Header>>()
        );
    }

    #[test]
    fn headers_into_iter_borrow() {
        let mut headers = MessageHeaders::new();
        let raw_message = concat!(
            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n",
            "Host: www.example.com\r\n",
            "Accept-Language: en, mi\r\n",
            "\r\n",
        );
        headers.parse(raw_message).unwrap();
        assert!([
            Header {
                name: "User-Agent".into(),
                value: "curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3"
                    .into(),
            },
            Header {
                name: "Host".into(),
                value: "www.example.com".into(),
            },
            Header {
                name: "Accept-Language".into(),
                value: "en, mi".into(),
            },
        ]
        .iter()
        .eq((&headers).into_iter()));
    }
}