File: URL.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (1946 lines) | stat: -rw-r--r-- 62,007 bytes parent folder | download | duplicates (2)
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
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include <cassert>
#include <new>
#include "tscore/ink_platform.h"
#include "tscore/ink_memory.h"
#include "tscore/TsBuffer.h"
#include "URL.h"
#include "MIME.h"
#include "HTTP.h"
#include "tscore/Diags.h"

const char *URL_SCHEME_FILE;
const char *URL_SCHEME_FTP;
const char *URL_SCHEME_GOPHER;
const char *URL_SCHEME_HTTP;
const char *URL_SCHEME_HTTPS;
const char *URL_SCHEME_WSS;
const char *URL_SCHEME_WS;
const char *URL_SCHEME_MAILTO;
const char *URL_SCHEME_NEWS;
const char *URL_SCHEME_NNTP;
const char *URL_SCHEME_PROSPERO;
const char *URL_SCHEME_TELNET;
const char *URL_SCHEME_TUNNEL;
const char *URL_SCHEME_WAIS;
const char *URL_SCHEME_PNM;
const char *URL_SCHEME_RTSP;
const char *URL_SCHEME_RTSPU;
const char *URL_SCHEME_MMS;
const char *URL_SCHEME_MMSU;
const char *URL_SCHEME_MMST;

int URL_WKSIDX_FILE;
int URL_WKSIDX_FTP;
int URL_WKSIDX_GOPHER;
int URL_WKSIDX_HTTP;
int URL_WKSIDX_HTTPS;
int URL_WKSIDX_WS;
int URL_WKSIDX_WSS;
int URL_WKSIDX_MAILTO;
int URL_WKSIDX_NEWS;
int URL_WKSIDX_NNTP;
int URL_WKSIDX_PROSPERO;
int URL_WKSIDX_TELNET;
int URL_WKSIDX_TUNNEL;
int URL_WKSIDX_WAIS;
int URL_WKSIDX_PNM;
int URL_WKSIDX_RTSP;
int URL_WKSIDX_RTSPU;
int URL_WKSIDX_MMS;
int URL_WKSIDX_MMSU;
int URL_WKSIDX_MMST;

int URL_LEN_FILE;
int URL_LEN_FTP;
int URL_LEN_GOPHER;
int URL_LEN_HTTP;
int URL_LEN_HTTPS;
int URL_LEN_WS;
int URL_LEN_WSS;
int URL_LEN_MAILTO;
int URL_LEN_NEWS;
int URL_LEN_NNTP;
int URL_LEN_PROSPERO;
int URL_LEN_TELNET;
int URL_LEN_TUNNEL;
int URL_LEN_WAIS;
int URL_LEN_PNM;
int URL_LEN_RTSP;
int URL_LEN_RTSPU;
int URL_LEN_MMS;
int URL_LEN_MMSU;
int URL_LEN_MMST;

// Whether we should implement url_CryptoHash_get() using url_CryptoHash_get_fast(). Note that
// url_CryptoHash_get_fast() does NOT produce the same result as url_CryptoHash_get_general().
static int url_hash_method = 0;

// test to see if a character is a valid character for a host in a URI according to
// RFC 3986 and RFC 1034
inline static int
is_host_char(char c)
{
  return (ParseRules::is_alnum(c) || (c == '-') || (c == '.') || (c == '[') || (c == ']') || (c == '_') || (c == ':') ||
          (c == '~') || (c == '%'));
}

// Checks if `addr` is a valid FQDN string
bool
validate_host_name(std::string_view addr)
{
  return std::all_of(addr.begin(), addr.end(), &is_host_char);
}

/**
   Checks if the (un-well-known) scheme is valid

   RFC 3986 Section 3.1
   These are the valid characters in a scheme:
     scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
   return an error if there is another character in the scheme
*/
bool
validate_scheme(std::string_view scheme)
{
  if (scheme.empty()) {
    return false;
  }

  if (!ParseRules::is_alpha(scheme[0])) {
    return false;
  }

  for (size_t i = 0; i < scheme.size(); ++i) {
    const char &c = scheme[i];

    if (!(ParseRules::is_alnum(c) != 0 || c == '+' || c == '-' || c == '.')) {
      return false;
    }
  }

  return true;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
url_init()
{
  static int init = 1;

  if (init) {
    init = 0;

    hdrtoken_init();

    URL_SCHEME_FILE     = hdrtoken_string_to_wks("file");
    URL_SCHEME_FTP      = hdrtoken_string_to_wks("ftp");
    URL_SCHEME_GOPHER   = hdrtoken_string_to_wks("gopher");
    URL_SCHEME_HTTP     = hdrtoken_string_to_wks("http");
    URL_SCHEME_HTTPS    = hdrtoken_string_to_wks("https");
    URL_SCHEME_WSS      = hdrtoken_string_to_wks("wss");
    URL_SCHEME_WS       = hdrtoken_string_to_wks("ws");
    URL_SCHEME_MAILTO   = hdrtoken_string_to_wks("mailto");
    URL_SCHEME_NEWS     = hdrtoken_string_to_wks("news");
    URL_SCHEME_NNTP     = hdrtoken_string_to_wks("nntp");
    URL_SCHEME_PROSPERO = hdrtoken_string_to_wks("prospero");
    URL_SCHEME_TELNET   = hdrtoken_string_to_wks("telnet");
    URL_SCHEME_TUNNEL   = hdrtoken_string_to_wks("tunnel");
    URL_SCHEME_WAIS     = hdrtoken_string_to_wks("wais");
    URL_SCHEME_PNM      = hdrtoken_string_to_wks("pnm");
    URL_SCHEME_RTSP     = hdrtoken_string_to_wks("rtsp");
    URL_SCHEME_RTSPU    = hdrtoken_string_to_wks("rtspu");
    URL_SCHEME_MMS      = hdrtoken_string_to_wks("mms");
    URL_SCHEME_MMSU     = hdrtoken_string_to_wks("mmsu");
    URL_SCHEME_MMST     = hdrtoken_string_to_wks("mmst");

    ink_assert(URL_SCHEME_FILE && URL_SCHEME_FTP && URL_SCHEME_GOPHER && URL_SCHEME_HTTP && URL_SCHEME_HTTPS && URL_SCHEME_WS &&
               URL_SCHEME_WSS && URL_SCHEME_MAILTO && URL_SCHEME_NEWS && URL_SCHEME_NNTP && URL_SCHEME_PROSPERO &&
               URL_SCHEME_TELNET && URL_SCHEME_TUNNEL && URL_SCHEME_WAIS && URL_SCHEME_PNM && URL_SCHEME_RTSP && URL_SCHEME_RTSPU &&
               URL_SCHEME_MMS && URL_SCHEME_MMSU && URL_SCHEME_MMST);

    URL_WKSIDX_FILE     = hdrtoken_wks_to_index(URL_SCHEME_FILE);
    URL_WKSIDX_FTP      = hdrtoken_wks_to_index(URL_SCHEME_FTP);
    URL_WKSIDX_GOPHER   = hdrtoken_wks_to_index(URL_SCHEME_GOPHER);
    URL_WKSIDX_HTTP     = hdrtoken_wks_to_index(URL_SCHEME_HTTP);
    URL_WKSIDX_HTTPS    = hdrtoken_wks_to_index(URL_SCHEME_HTTPS);
    URL_WKSIDX_WS       = hdrtoken_wks_to_index(URL_SCHEME_WS);
    URL_WKSIDX_WSS      = hdrtoken_wks_to_index(URL_SCHEME_WSS);
    URL_WKSIDX_MAILTO   = hdrtoken_wks_to_index(URL_SCHEME_MAILTO);
    URL_WKSIDX_NEWS     = hdrtoken_wks_to_index(URL_SCHEME_NEWS);
    URL_WKSIDX_NNTP     = hdrtoken_wks_to_index(URL_SCHEME_NNTP);
    URL_WKSIDX_PROSPERO = hdrtoken_wks_to_index(URL_SCHEME_PROSPERO);
    URL_WKSIDX_TELNET   = hdrtoken_wks_to_index(URL_SCHEME_TELNET);
    URL_WKSIDX_TUNNEL   = hdrtoken_wks_to_index(URL_SCHEME_TUNNEL);
    URL_WKSIDX_WAIS     = hdrtoken_wks_to_index(URL_SCHEME_WAIS);
    URL_WKSIDX_PNM      = hdrtoken_wks_to_index(URL_SCHEME_PNM);
    URL_WKSIDX_RTSP     = hdrtoken_wks_to_index(URL_SCHEME_RTSP);
    URL_WKSIDX_RTSPU    = hdrtoken_wks_to_index(URL_SCHEME_RTSPU);
    URL_WKSIDX_MMS      = hdrtoken_wks_to_index(URL_SCHEME_MMS);
    URL_WKSIDX_MMSU     = hdrtoken_wks_to_index(URL_SCHEME_MMSU);
    URL_WKSIDX_MMST     = hdrtoken_wks_to_index(URL_SCHEME_MMST);

    URL_LEN_FILE     = hdrtoken_wks_to_length(URL_SCHEME_FILE);
    URL_LEN_FTP      = hdrtoken_wks_to_length(URL_SCHEME_FTP);
    URL_LEN_GOPHER   = hdrtoken_wks_to_length(URL_SCHEME_GOPHER);
    URL_LEN_HTTP     = hdrtoken_wks_to_length(URL_SCHEME_HTTP);
    URL_LEN_HTTPS    = hdrtoken_wks_to_length(URL_SCHEME_HTTPS);
    URL_LEN_WS       = hdrtoken_wks_to_length(URL_SCHEME_WS);
    URL_LEN_WSS      = hdrtoken_wks_to_length(URL_SCHEME_WSS);
    URL_LEN_MAILTO   = hdrtoken_wks_to_length(URL_SCHEME_MAILTO);
    URL_LEN_NEWS     = hdrtoken_wks_to_length(URL_SCHEME_NEWS);
    URL_LEN_NNTP     = hdrtoken_wks_to_length(URL_SCHEME_NNTP);
    URL_LEN_PROSPERO = hdrtoken_wks_to_length(URL_SCHEME_PROSPERO);
    URL_LEN_TELNET   = hdrtoken_wks_to_length(URL_SCHEME_TELNET);
    URL_LEN_TUNNEL   = hdrtoken_wks_to_length(URL_SCHEME_TUNNEL);
    URL_LEN_WAIS     = hdrtoken_wks_to_length(URL_SCHEME_WAIS);
    URL_LEN_PNM      = hdrtoken_wks_to_length(URL_SCHEME_PNM);
    URL_LEN_RTSP     = hdrtoken_wks_to_length(URL_SCHEME_RTSP);
    URL_LEN_RTSPU    = hdrtoken_wks_to_length(URL_SCHEME_RTSPU);
    URL_LEN_MMS      = hdrtoken_wks_to_length(URL_SCHEME_MMS);
    URL_LEN_MMSU     = hdrtoken_wks_to_length(URL_SCHEME_MMSU);
    URL_LEN_MMST     = hdrtoken_wks_to_length(URL_SCHEME_MMST);
  }
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *             U R L    C R E A T I O N    A N D    C O P Y            *
 *                                                                     *
 ***********************************************************************/

URLImpl *
url_create(HdrHeap *heap)
{
  URLImpl *url;

  url = (URLImpl *)heap->allocate_obj(sizeof(URLImpl), HDR_HEAP_OBJ_URL);
  obj_clear_data((HdrHeapObjImpl *)url);
  url->m_url_type       = URL_TYPE_NONE;
  url->m_scheme_wks_idx = -1;
  url_clear_string_ref(url);
  return url;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
url_clear(URLImpl *url_impl)
{
  obj_clear_data((HdrHeapObjImpl *)url_impl);
  url_impl->m_url_type       = URL_TYPE_NONE;
  url_impl->m_scheme_wks_idx = -1;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

URLImpl *
url_copy(URLImpl *s_url, HdrHeap *s_heap, HdrHeap *d_heap, bool inherit_strs)
{
  URLImpl *d_url = url_create(d_heap);
  url_copy_onto(s_url, s_heap, d_url, d_heap, inherit_strs);
  return d_url;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
url_copy_onto(URLImpl *s_url, HdrHeap *s_heap, URLImpl *d_url, HdrHeap *d_heap, bool inherit_strs)
{
  if (s_url != d_url) {
    obj_copy_data((HdrHeapObjImpl *)s_url, (HdrHeapObjImpl *)d_url);
    if (inherit_strs && (s_heap != d_heap)) {
      d_heap->inherit_string_heaps(s_heap);
    }
  }
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
url_nuke_proxy_stuff(URLImpl *d_url)
{
  d_url->m_len_scheme   = 0;
  d_url->m_len_user     = 0;
  d_url->m_len_password = 0;
  d_url->m_len_host     = 0;
  d_url->m_len_port     = 0;

  d_url->m_ptr_scheme   = nullptr;
  d_url->m_ptr_user     = nullptr;
  d_url->m_ptr_password = nullptr;
  d_url->m_ptr_host     = nullptr;
  d_url->m_ptr_port     = nullptr;

  d_url->m_scheme_wks_idx = -1;
  d_url->m_port           = 0;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *                        M A R S H A L I N G                          *
 *                                                                     *
 ***********************************************************************/
int
URLImpl::marshal(MarshalXlate *str_xlate, int num_xlate)
{
  HDR_MARSHAL_STR(m_ptr_scheme, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_user, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_password, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_host, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_port, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_path, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_params, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_query, str_xlate, num_xlate);
  HDR_MARSHAL_STR(m_ptr_fragment, str_xlate, num_xlate);
  //    HDR_MARSHAL_STR(m_ptr_printed_string, str_xlate, num_xlate);
  return 0;
}

void
URLImpl::unmarshal(intptr_t offset)
{
  HDR_UNMARSHAL_STR(m_ptr_scheme, offset);
  HDR_UNMARSHAL_STR(m_ptr_user, offset);
  HDR_UNMARSHAL_STR(m_ptr_password, offset);
  HDR_UNMARSHAL_STR(m_ptr_host, offset);
  HDR_UNMARSHAL_STR(m_ptr_port, offset);
  HDR_UNMARSHAL_STR(m_ptr_path, offset);
  HDR_UNMARSHAL_STR(m_ptr_params, offset);
  HDR_UNMARSHAL_STR(m_ptr_query, offset);
  HDR_UNMARSHAL_STR(m_ptr_fragment, offset);
  //    HDR_UNMARSHAL_STR(m_ptr_printed_string, offset);
}

void
URLImpl::rehome_strings(HdrHeap *new_heap)
{
  m_ptr_scheme         = new_heap->localize({m_ptr_scheme, m_len_scheme}).data();
  m_ptr_user           = new_heap->localize({m_ptr_user, m_len_user}).data();
  m_ptr_password       = new_heap->localize({m_ptr_password, m_len_password}).data();
  m_ptr_host           = new_heap->localize({m_ptr_host, m_len_host}).data();
  m_ptr_port           = new_heap->localize({m_ptr_port, m_len_port}).data();
  m_ptr_path           = new_heap->localize({m_ptr_path, m_len_path}).data();
  m_ptr_params         = new_heap->localize({m_ptr_params, m_len_params}).data();
  m_ptr_query          = new_heap->localize({m_ptr_query, m_len_query}).data();
  m_ptr_fragment       = new_heap->localize({m_ptr_fragment, m_len_fragment}).data();
  m_ptr_printed_string = new_heap->localize({m_ptr_printed_string, m_len_printed_string}).data();
}

void
URLImpl::move_strings(HdrStrHeap *new_heap)
{
  HDR_MOVE_STR(m_ptr_scheme, m_len_scheme);
  HDR_MOVE_STR(m_ptr_user, m_len_user);
  HDR_MOVE_STR(m_ptr_password, m_len_password);
  HDR_MOVE_STR(m_ptr_host, m_len_host);
  HDR_MOVE_STR(m_ptr_port, m_len_port);
  HDR_MOVE_STR(m_ptr_path, m_len_path);
  HDR_MOVE_STR(m_ptr_params, m_len_params);
  HDR_MOVE_STR(m_ptr_query, m_len_query);
  HDR_MOVE_STR(m_ptr_fragment, m_len_fragment);
  HDR_MOVE_STR(m_ptr_printed_string, m_len_printed_string);
}

size_t
URLImpl::strings_length()
{
  size_t ret = 0;

  ret += m_len_scheme;
  ret += m_len_user;
  ret += m_len_password;
  ret += m_len_host;
  ret += m_len_port;
  ret += m_len_path;
  ret += m_len_params;
  ret += m_len_query;
  ret += m_len_fragment;
  ret += m_len_printed_string;
  return ret;
}

void
URLImpl::check_strings(HeapCheck *heaps, int num_heaps)
{
  CHECK_STR(m_ptr_scheme, m_len_scheme, heaps, num_heaps);
  CHECK_STR(m_ptr_user, m_len_user, heaps, num_heaps);
  CHECK_STR(m_ptr_password, m_len_password, heaps, num_heaps);
  CHECK_STR(m_ptr_host, m_len_host, heaps, num_heaps);
  CHECK_STR(m_ptr_port, m_len_port, heaps, num_heaps);
  CHECK_STR(m_ptr_path, m_len_path, heaps, num_heaps);
  CHECK_STR(m_ptr_params, m_len_params, heaps, num_heaps);
  CHECK_STR(m_ptr_query, m_len_query, heaps, num_heaps);
  CHECK_STR(m_ptr_fragment, m_len_fragment, heaps, num_heaps);
  //    CHECK_STR(m_ptr_printed_string, m_len_printed_string, heaps, num_heaps);
}

/***********************************************************************
 *                                                                     *
 *                               S E T                                 *
 *                                                                     *
 ***********************************************************************/

const char *
URLImpl::set_scheme(HdrHeap *heap, const char *scheme_str, int scheme_wks_idx, int length, bool copy_string)
{
  const char *scheme_wks;
  url_called_set(this);
  if (length == 0) {
    scheme_str = nullptr;
  }

  mime_str_u16_set(heap, scheme_str, length, &(this->m_ptr_scheme), &(this->m_len_scheme), copy_string);

  this->m_scheme_wks_idx = scheme_wks_idx;
  if (scheme_wks_idx >= 0) {
    scheme_wks = hdrtoken_index_to_wks(scheme_wks_idx);
  } else {
    scheme_wks = nullptr;
  }

  if (scheme_wks == URL_SCHEME_HTTP || scheme_wks == URL_SCHEME_WS) {
    this->m_url_type = URL_TYPE_HTTP;
  } else if (scheme_wks == URL_SCHEME_HTTPS || scheme_wks == URL_SCHEME_WSS) {
    this->m_url_type = URL_TYPE_HTTPS;
  } else {
    this->m_url_type = URL_TYPE_HTTP;
  }

  return scheme_wks; // tokenized string or NULL if not well known
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_user(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  if (length == 0) {
    value = nullptr;
  }
  mime_str_u16_set(heap, value, length, &(this->m_ptr_user), &(this->m_len_user), copy_string);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_password(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  if (length == 0) {
    value = nullptr;
  }
  mime_str_u16_set(heap, value, length, &(this->m_ptr_password), &(this->m_len_password), copy_string);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_host(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  if (length == 0) {
    value = nullptr;
  }
  mime_str_u16_set(heap, value, length, &(this->m_ptr_host), &(this->m_len_host), copy_string);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_port(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  if (length == 0) {
    value = nullptr;
  }
  mime_str_u16_set(heap, value, length, &(this->m_ptr_port), &(this->m_len_port), copy_string);

  this->m_port = 0;
  for (int i = 0; i < length; i++) {
    if (!ParseRules::is_digit(value[i])) {
      break;
    }
    this->m_port = this->m_port * 10 + (value[i] - '0');
  }
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_port(HdrHeap *heap, unsigned int port)
{
  url_called_set(this);
  if (port > 0) {
    char value[6];
    int length;

    length = ink_fast_itoa(port, value, sizeof(value));
    mime_str_u16_set(heap, value, length, &(this->m_ptr_port), &(this->m_len_port), true);
  } else {
    mime_str_u16_set(heap, nullptr, 0, &(this->m_ptr_port), &(this->m_len_port), true);
  }
  this->m_port = port;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_path(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  if (length == 0) {
    value = nullptr;
  }
  mime_str_u16_set(heap, value, length, &(this->m_ptr_path), &(this->m_len_path), copy_string);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

// empties params/query/fragment component
// url_{params|query|fragment}_set()

void
URLImpl::set_params(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  mime_str_u16_set(heap, value, length, &(this->m_ptr_params), &(this->m_len_params), copy_string);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_query(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  mime_str_u16_set(heap, value, length, &(this->m_ptr_query), &(this->m_len_query), copy_string);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_fragment(HdrHeap *heap, const char *value, int length, bool copy_string)
{
  url_called_set(this);
  mime_str_u16_set(heap, value, length, &(this->m_ptr_fragment), &(this->m_len_fragment), copy_string);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_type(int type)
{
  url_called_set(this);
  this->m_url_type = type;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
URLImpl::set_type_code(unsigned int typecode)
{
  url_called_set(this);
  this->m_type_code = typecode;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *                               G E T                                 *
 *                                                                     *
 ***********************************************************************/

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
url_called_set(URLImpl *url)
{
  url->m_clean = !url->m_ptr_printed_string;
}

void
url_clear_string_ref(URLImpl *url)
{
  if (url->m_ptr_printed_string) {
    url->m_len_printed_string = 0;
    url->m_ptr_printed_string = nullptr;
    url->m_clean              = true;
  }
  return;
}

char *
url_string_get_ref(HdrHeap *heap, URLImpl *url, int *length, unsigned normalization_flags)
{
  if (!url) {
    return nullptr;
  }

  if (url->m_ptr_printed_string && url->m_clean && (normalization_flags == url->m_normalization_flags)) {
    if (length) {
      *length = url->m_len_printed_string;
    }
    return const_cast<char *>(url->m_ptr_printed_string);
  } else { // either not clean or never printed
    int len = url_length_get(url, normalization_flags);
    char *buf;
    int index  = 0;
    int offset = 0;

    /* stuff alloc'd here gets gc'd on HdrHeap::destroy() */
    buf = heap->allocate_str(len + 1);
    url_print(url, buf, len, &index, &offset, normalization_flags);
    buf[len] = '\0';

    if (length) {
      *length = len;
    }
    url->m_clean               = true; // reset since we have url_print()'ed again
    url->m_len_printed_string  = len;
    url->m_ptr_printed_string  = buf;
    url->m_normalization_flags = normalization_flags;
    return buf;
  }
}

char *
url_string_get(URLImpl *url, Arena *arena, int *length, HdrHeap *heap)
{
  int len = url_length_get(url);
  char *buf;
  char *buf2;
  int index  = 0;
  int offset = 0;

  buf = arena ? arena->str_alloc(len) : static_cast<char *>(ats_malloc(len + 1));

  url_print(url, buf, len, &index, &offset);
  buf[len] = '\0';

  /* see string_get_ref() */
  if (heap) {
    buf2 = heap->allocate_str(len + 1);
    memcpy(buf2, buf, len);
    buf2[len]                 = '\0';
    url->m_clean              = true; // reset since we have url_print()'ed again
    url->m_len_printed_string = len;
    url->m_ptr_printed_string = buf2;
  }

  if (length) {
    *length = len;
  }
  return buf;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

char *
url_string_get_buf(URLImpl *url, char *dstbuf, int dstbuf_size, int *length)
{
  int len    = url_length_get(url);
  int index  = 0;
  int offset = 0;
  char *buf  = nullptr;

  if (dstbuf && dstbuf_size > 0) {
    buf = dstbuf;
    if (len >= dstbuf_size) {
      len = dstbuf_size - 1;
    }
    url_print(url, dstbuf, len, &index, &offset);
    buf[len] = 0;

    if (length) {
      *length = len;
    }
  }
  return buf;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_scheme(int *length)
{
  if (this->m_scheme_wks_idx >= 0) {
    *length = hdrtoken_index_to_length(this->m_scheme_wks_idx);
    return hdrtoken_index_to_wks(this->m_scheme_wks_idx);
  } else {
    *length = this->m_len_scheme;
    return this->m_ptr_scheme;
  }
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_user(int *length)
{
  *length = this->m_len_user;
  return this->m_ptr_user;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_password(int *length)
{
  *length = this->m_len_password;
  return this->m_ptr_password;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_host(int *length)
{
  *length = this->m_len_host;
  return this->m_ptr_host;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

int
URLImpl::get_port()
{
  return this->m_port;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_path(int *length)
{
  *length = this->m_len_path;
  return this->m_ptr_path;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_params(int *length)
{
  *length = this->m_len_params;
  return this->m_ptr_params;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_query(int *length)
{
  *length = this->m_len_query;
  return this->m_ptr_query;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

const char *
URLImpl::get_fragment(int *length)
{
  *length = this->m_len_fragment;
  return this->m_ptr_fragment;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

int
URLImpl::get_type()
{
  return this->m_url_type;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

int
URLImpl::get_type_code()
{
  return this->m_type_code;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *               U R L    S T R I N G    F U N C T I O N S             *
 *                                                                     *
 ***********************************************************************/

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

int
url_length_get(URLImpl *url, unsigned normalization_flags)
{
  int length = 0;

  if (url->m_ptr_scheme) {
    length += url->m_len_scheme + 3; // +3 for "://"

  } else if (normalization_flags & URLNormalize::IMPLIED_SCHEME) {
    if (URL_TYPE_HTTP == url->m_url_type) {
      length += URL_LEN_HTTP + 3;

    } else if (URL_TYPE_HTTPS == url->m_url_type) {
      length += URL_LEN_HTTPS + 3;
    }
  }

  if (url->m_ptr_user) {
    length += url->m_len_user + 1; // +1 for "@"
    if (url->m_ptr_password) {
      length += url->m_len_password + 1; // +1 for ":"
    }
  }

  if (url->m_ptr_host) {
    length += url->m_len_host;
    if (url->m_ptr_port && url->m_port) {
      length += url->m_len_port + 1; // +1 for ":"
    }
  }

  if (url->m_ptr_path) {
    length += url->m_len_path;
  }

  if (!url->m_path_is_empty) {
    // m_ptr_path does not contain the initial "/" and thus m_len_path does not
    // count it. We account for it here.
    length += 1; // +1 for "/"
  }

  if (url->m_ptr_params && url->m_len_params > 0) {
    length += url->m_len_params + 1; // +1 for ";"
  }

  if (url->m_ptr_query && url->m_len_query > 0) {
    length += url->m_len_query + 1; // +1 for "?"
  }

  if (url->m_ptr_fragment && url->m_len_fragment > 0) {
    length += url->m_len_fragment + 1; // +1 for "#"
  }

  return length;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

char *
url_to_string(URLImpl *url, Arena *arena, int *length)
{
  int len;
  int idx;
  char *str;

  len = url_length_get(url) + 1;

  if (length) {
    *length = len;
  }

  if (arena) {
    str = arena->str_alloc(len);
  } else {
    str = static_cast<char *>(ats_malloc(len + 1));
  }

  idx = 0;

  if (url->m_ptr_scheme) {
    memcpy(&str[idx], url->m_ptr_scheme, url->m_len_scheme);
    idx += url->m_len_scheme;
    if ((url->m_scheme_wks_idx >= 0) && (hdrtoken_index_to_wks(url->m_scheme_wks_idx) == URL_SCHEME_FILE)) {
      str[idx++] = ':';
    } else {
      str[idx++] = ':';
      str[idx++] = '/';
      str[idx++] = '/';
    }
  }

  if (url->m_ptr_user) {
    memcpy(&str[idx], url->m_ptr_user, url->m_len_user);
    idx += url->m_len_user;
    if (url->m_ptr_password) {
      str[idx++] = ':';
      memcpy(&str[idx], url->m_ptr_password, url->m_len_password);
      idx += url->m_len_password;
    }
    str[idx++] = '@';
  }

  if (url->m_ptr_host) {
    memcpy(&str[idx], url->m_ptr_host, url->m_len_host);
    idx += url->m_len_host;
    if (url->m_ptr_port != nullptr) {
      str[idx++] = ':';
      memcpy(&str[idx], url->m_ptr_port, url->m_len_port);
      idx += url->m_len_port;
    }
  }

  memcpy(&str[idx], url->m_ptr_path, url->m_len_path);
  idx += url->m_len_path;

  if (url->m_ptr_params && url->m_len_params > 0) {
    str[idx++] = ';';
    memcpy(&str[idx], url->m_ptr_params, url->m_len_params);
    idx += url->m_len_params;
  }

  if (url->m_ptr_query && url->m_len_query > 0) {
    str[idx++] = '?';
    memcpy(&str[idx], url->m_ptr_query, url->m_len_query);
    idx += url->m_len_query;
  }

  if (url->m_ptr_fragment && url->m_len_fragment > 0) {
    str[idx++] = '#';
    memcpy(&str[idx], url->m_ptr_fragment, url->m_len_fragment);
    idx += url->m_len_fragment;
  }

  str[idx++] = '\0';

  ink_release_assert(idx == len);

  return str;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *                     E S C A P E - H A N D L I N G                   *
 *                                                                     *
 ***********************************************************************/

void
unescape_str(char *&buf, char *buf_e, const char *&str, const char *str_e, int &state)
{
  int copy_len;
  char *first_pct;
  int buf_len = static_cast<int>(buf_e - buf);
  int str_len = static_cast<int>(str_e - str);
  int min_len = (str_len < buf_len ? str_len : buf_len);

  first_pct = ink_memcpy_until_char(buf, const_cast<char *>(str), min_len, '%');
  copy_len  = static_cast<int>(first_pct - str);
  str += copy_len;
  buf += copy_len;
  if (copy_len == min_len) {
    return;
  }

  while (str < str_e && (buf != buf_e)) {
    switch (state) {
    case 0:
      if (str[0] == '%') {
        str += 1;
        state = 1;
      } else {
        *buf++ = str[0];
        str += 1;
      }
      break;
    case 1:
      if (ParseRules::is_hex(str[0])) {
        str += 1;
        state = 2;
      } else {
        *buf++ = str[-1];
        state  = 0;
      }
      break;
    case 2:
      if (ParseRules::is_hex(str[0])) {
        int tmp;

        if (ParseRules::is_alpha(str[-1])) {
          tmp = (ParseRules::ink_toupper(str[-1]) - 'A' + 10) * 16;
        } else {
          tmp = (str[-1] - '0') * 16;
        }
        if (ParseRules::is_alpha(str[0])) {
          tmp += (ParseRules::ink_toupper(str[0]) - 'A' + 10);
        } else {
          tmp += str[0] - '0';
        }

        *buf++ = tmp;
        str += 1;
        state = 0;
      } else {
        *buf++ = str[-2];
        state  = 3;
      }
      break;
    case 3:
      *buf++ = str[-1];
      state  = 0;
      break;
    }
  }
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
unescape_str_tolower(char *&buf, char *end, const char *&str, const char *str_e, int &state)
{
  while (str < str_e && (buf != end)) {
    switch (state) {
    case 0:
      if (str[0] == '%') {
        str += 1;
        state = 1;
      } else {
        *buf++ = ParseRules::ink_tolower(str[0]);
        str += 1;
      }
      break;
    case 1:
      if (ParseRules::is_hex(str[0])) {
        str += 1;
        state = 2;
      } else {
        *buf++ = ParseRules::ink_tolower(str[-1]);
        state  = 0;
      }
      break;
    case 2:
      if (ParseRules::is_hex(str[0])) {
        int tmp;

        if (ParseRules::is_alpha(str[-1])) {
          tmp = (ParseRules::ink_toupper(str[-1]) - 'A' + 10) * 16;
        } else {
          tmp = (str[-1] - '0') * 16;
        }
        if (ParseRules::is_alpha(str[0])) {
          tmp += (ParseRules::ink_toupper(str[0]) - 'A' + 10);
        } else {
          tmp += str[0] - '0';
        }

        *buf++ = tmp;
        str += 1;
        state = 0;
      } else {
        *buf++ = ParseRules::ink_tolower(str[-2]);
        state  = 3;
      }
      break;
    case 3:
      *buf++ = ParseRules::ink_tolower(str[-1]);
      state  = 0;
      break;
    }
  }
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

char *
url_unescapify(Arena *arena, const char *str, int length)
{
  char *buffer;
  char *t, *e;
  int s;

  if (length == -1) {
    length = static_cast<int>(strlen(str));
  }

  buffer = arena->str_alloc(length);
  t      = buffer;
  e      = buffer + length;
  s      = 0;

  unescape_str(t, e, str, str + length, s);
  *t = '\0';

  return buffer;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *                            P A R S I N G                            *
 *                                                                     *
 ***********************************************************************/

#define GETNEXT(label) \
  {                    \
    cur += 1;          \
    if (cur >= end) {  \
      goto label;      \
    }                  \
  }

ParseResult
url_parse_scheme(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings_p)
{
  const char *cur = *start;
  const char *scheme_wks;
  const char *scheme_start = nullptr;
  const char *scheme_end   = nullptr;
  int scheme_wks_idx;

  // Skip over spaces
  while (' ' == *cur && ++cur < end) {
  }

  if (cur < end) {
    scheme_start = scheme_end = cur;

    // If the URL is more complex then a path, parse to see if there is a scheme
    if ('/' != *cur) {
      // Search for a : it could be part of a scheme or a username:password
      while (':' != *cur && ++cur < end) {
      }

      // If there is a :// then there is a scheme
      if (cur + 2 < end && cur[1] == '/' && cur[2] == '/') { // found "://"
        scheme_end     = cur;
        scheme_wks_idx = hdrtoken_tokenize(scheme_start, scheme_end - scheme_start, &scheme_wks);

        if (!(scheme_wks_idx > 0 && hdrtoken_wks_to_token_type(scheme_wks) == HDRTOKEN_TYPE_SCHEME)) {
          // Unknown scheme, validate the scheme
          if (!validate_scheme({scheme_start, static_cast<size_t>(scheme_end - scheme_start)})) {
            return PARSE_RESULT_ERROR;
          }
        }
        url->set_scheme(heap, scheme_start, scheme_wks_idx, scheme_end - scheme_start, copy_strings_p);
      }
    }
    *start = scheme_end;
    return PARSE_RESULT_CONT;
  }
  return PARSE_RESULT_ERROR; // no non-whitespace found
}

// This implementation namespace is necessary because this function is tested by a Catch unit test
// in another source file.
//
namespace UrlImpl
{
/**
 *  This method will return TRUE if the uri is strictly compliant with
 *  RFC 3986 and it will return FALSE if not.
 */
bool
url_is_strictly_compliant(const char *start, const char *end)
{
  for (const char *i = start; i < end; ++i) {
    if (!ParseRules::is_uri(*i)) {
      Debug("http", "Non-RFC compliant character [0x%.2X] found in URL", (unsigned char)*i);
      return false;
    }
  }
  return true;
}

/**
 *  This method will return TRUE if the uri is mostly compliant with
 *  RFC 3986 and it will return FALSE if not. Specifically denying white
 *  space an unprintable characters
 */
bool
url_is_mostly_compliant(const char *start, const char *end)
{
  for (const char *i = start; i < end; ++i) {
    if (isspace(*i)) {
      Debug("http", "Whitespace character [0x%.2X] found in URL", (unsigned char)*i);
      return false;
    }
    if (!isprint(*i)) {
      Debug("http", "Non-printable character [0x%.2X] found in URL", (unsigned char)*i);
      return false;
    }
  }
  return true;
}

} // namespace UrlImpl
using namespace UrlImpl;

ParseResult
url_parse(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings_p, int strict_uri_parsing,
          bool verify_host_characters)
{
  if (strict_uri_parsing == 1 && !url_is_strictly_compliant(*start, end)) {
    return PARSE_RESULT_ERROR;
  }
  if (strict_uri_parsing == 2 && !url_is_mostly_compliant(*start, end)) {
    return PARSE_RESULT_ERROR;
  }

  ParseResult zret = url_parse_scheme(heap, url, start, end, copy_strings_p);
  return PARSE_RESULT_CONT == zret ? url_parse_http(heap, url, start, end, copy_strings_p, verify_host_characters) : zret;
}

ParseResult
url_parse_regex(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings_p)
{
  ParseResult zret = url_parse_scheme(heap, url, start, end, copy_strings_p);
  return PARSE_RESULT_CONT == zret ? url_parse_http_regex(heap, url, start, end, copy_strings_p) : zret;
}

/**
  Parse internet URL.

  After this function completes, start will point to the first character after the
  host or @a end if there are not characters after it.

  @verbatim
  [://][user[:password]@]host[:port]

  some.place/
  some.place:80/
  foo@some.place:80/
  foo:bar@some.place:80/
  foo:bar@some.place/
  foo:42@some.place/
  @endverbatim

*/

ParseResult
url_parse_internet(HdrHeap *heap, URLImpl *url, const char **start, char const *end, bool copy_strings_p,
                   bool verify_host_characters)
{
  const char *cur = *start;
  const char *base;              // Base for host/port field.
  const char *bracket = nullptr; // marker for open bracket, if any.
  ts::ConstBuffer user, passw, host, port;
  static size_t const MAX_COLON = 8; // max # of valid colons.
  size_t n_colon                = 0;
  const char *last_colon        = nullptr; // pointer to last colon seen.

  // Do a quick check for "://"
  if (end - cur > 3 && (((':' ^ *cur) | ('/' ^ cur[1]) | ('/' ^ cur[2])) == 0)) {
    cur += 3;
  } else if (':' == *cur && (++cur >= end || ('/' == *cur && (++cur >= end || ('/' == *cur && ++cur >= end))))) {
    return PARSE_RESULT_ERROR;
  }

  base = cur;
  // skipped leading stuff, start real parsing.
  while (cur < end) {
    // Note: Each case is responsible for incrementing @a cur if
    // appropriate!
    switch (*cur) {
    case ']': // address close
      if (nullptr == bracket || n_colon >= MAX_COLON) {
        return PARSE_RESULT_ERROR;
      }
      ++cur;
      /* We keep the brackets because there are too many other places
         that depend on them and it's too painful to keep track if
         they should be used. I thought about being clever with
         stripping brackets from non-IPv6 content but that gets ugly
         as well. Just not worth it.
       */
      host.set(bracket, cur);
      // Spec requires This constitute the entire host so the next
      // character must be missing (EOS), slash, or colon.
      if (cur >= end || '/' == *cur) { // done which is OK
        last_colon = nullptr;
        break;
      } else if (':' != *cur) { // otherwise it must be a colon
        return PARSE_RESULT_ERROR;
      }
      /* We want to prevent more than 1 colon following so we set @a
         n_colon appropriately.
      */
      n_colon = MAX_COLON - 1;
    // FALL THROUGH
    case ':': // track colons, fail if too many.
      if (++n_colon > MAX_COLON) {
        return PARSE_RESULT_ERROR;
      }
      last_colon = cur;
      ++cur;
      break;
    case '@': // user/password marker.
      if (user || n_colon > 1) {
        return PARSE_RESULT_ERROR; // we already got one, or too many colons.
      }
      if (n_colon) {
        user.set(base, last_colon);
        passw.set(last_colon + 1, cur);
        n_colon    = 0;
        last_colon = nullptr;
      } else {
        user.set(base, cur);
      }
      ++cur;
      base = cur;
      break;
    case '[':                       // address open
      if (bracket || base != cur) { // must be first char in field
        return PARSE_RESULT_ERROR;
      }
      bracket = cur; // location and flag.
      ++cur;
      break;
    // RFC 3986, section 3.2:
    // The authority component is ...  terminated by the next slash ("/"),
    // question mark ("?"), or number sign ("#") character, or by the end of
    // the URI.
    case '/':
    case '?':
    case '#':
      end = cur; // We're done parsing authority, cause loop exit.
      break;
    default:
      ++cur;
      break;
    };
  }
  // Time to pick up the pieces. At this pointer cur._ptr is the first
  // character past the parse area.

  if (user) {
    url->set_user(heap, user._ptr, user._size, copy_strings_p);
    if (passw) {
      url->set_password(heap, passw._ptr, passw._size, copy_strings_p);
    }
  }

  // @a host not set means no brackets to mark explicit host.
  if (!host) {
    if (1 == n_colon || MAX_COLON == n_colon) { // presume port.
      host.set(base, last_colon);
    } else { // it's all host.
      host.set(base, cur);
      last_colon = nullptr; // prevent port setting.
    }
  }
  if (host._size) {
    if (!verify_host_characters || validate_host_name(std::string_view(host._ptr, host._size))) {
      url->set_host(heap, host._ptr, host._size, copy_strings_p);
    } else {
      return PARSE_RESULT_ERROR;
    }
  }

  if (last_colon) {
    ink_assert(n_colon);
    port.set(last_colon + 1, cur);
    if (!port._size) {
      return PARSE_RESULT_ERROR; // colon w/o port value.
    }
    url->set_port(heap, port._ptr, port._size, copy_strings_p);
  }
  *start = cur;
  return PARSE_RESULT_DONE;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

// empties params/query/fragment component

ParseResult
url_parse_http(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings, bool verify_host_characters)
{
  ParseResult err;
  const char *cur;
  const char *path_start     = nullptr;
  const char *path_end       = nullptr;
  const char *params_start   = nullptr;
  const char *params_end     = nullptr;
  const char *query_start    = nullptr;
  const char *query_end      = nullptr;
  const char *fragment_start = nullptr;
  const char *fragment_end   = nullptr;
  char mask;

  err = url_parse_internet(heap, url, start, end, copy_strings, verify_host_characters);
  if (err < 0) {
    return err;
  }

  cur                     = *start;
  bool nothing_after_host = false;
  if (*start == end) {
    nothing_after_host = true;
    goto done;
  }

  if (*cur == '/') {
    path_start = cur;
  }
  mask = ';' & '?' & '#';
parse_path2:
  if ((*cur & mask) == mask) {
    if (*cur == ';') {
      path_end = cur;
      goto parse_params1;
    }
    if (*cur == '?') {
      path_end = cur;
      goto parse_query1;
    }
    if (*cur == '#') {
      path_end = cur;
      goto parse_fragment1;
    }
  } else {
    ink_assert((*cur != ';') && (*cur != '?') && (*cur != '#'));
  }
  GETNEXT(done);
  goto parse_path2;

parse_params1:
  params_start = cur + 1;
  GETNEXT(done);
parse_params2:
  if (*cur == '?') {
    params_end = cur;
    goto parse_query1;
  }
  if (*cur == '#') {
    params_end = cur;
    goto parse_fragment1;
  }
  GETNEXT(done);
  goto parse_params2;

parse_query1:
  query_start = cur + 1;
  GETNEXT(done);
parse_query2:
  if (*cur == '#') {
    query_end = cur;
    goto parse_fragment1;
  }
  GETNEXT(done);
  goto parse_query2;

parse_fragment1:
  fragment_start = cur + 1;
  GETNEXT(done);
  fragment_end = end;

done:
  if (path_start) {
    // There was an explicit path set with '/'.
    if (!path_end) {
      path_end = cur;
    }
    if (path_start == path_end) {
      url->m_path_is_empty = true;
    } else {
      url->m_path_is_empty = false;
      // Per RFC 3986 section 3, the query string does not contain the initial
      // '?' nor does the fragment contain the initial '#'. The path however
      // does contain the initial '/' and a path can be empty, containing no
      // characters at all, not even the initial '/'. Our path_get interface,
      // however, has long not behaved accordingly, returning only the
      // characters after the first '/'. This does not allow users to tell
      // whether the path was absolutely empty. Further, callers have to
      // account for the missing first '/' character themselves, either in URL
      // length calculations or when piecing together their own URL. There are
      // various examples of this in core and in the plugins shipped with Traffic
      // Server.
      //
      // Correcting this behavior by having path_get return the entire path,
      // (inclusive of any first '/') and an empty string if there were no
      // characters specified in the path would break existing functionality,
      // including various plugins that expect this behavior. Rather than
      // correcting this behavior, therefore, we maintain the current
      // functionality but add state to determine whether the path was
      // absolutely empty so we can reconstruct such URLs.
      //
      // Remove all preceding slashes
      while (path_start < path_end && *path_start == '/') {
        ++path_start;
      }
    }

    url->set_path(heap, path_start, path_end - path_start, copy_strings);
  } else if (!nothing_after_host) {
    // There was no path set via '/': it is absolutely empty. However, if there
    // is no path, query, or fragment after the host, we by convention add a
    // slash after the authority.  Users of URL expect this behavior. Thus the
    // nothing_after_host check.
    url->m_path_is_empty = true;
  }
  if (params_start) {
    if (!params_end) {
      params_end = cur;
    }
    url->set_params(heap, params_start, params_end - params_start, copy_strings);
  }
  if (query_start) {
    // There was a query string marked by '?'.
    if (!query_end) {
      query_end = cur;
    }
    url->set_query(heap, query_start, query_end - query_start, copy_strings);
  }
  if (fragment_start) {
    // There was a fragment string marked by '#'.
    if (!fragment_end) {
      fragment_end = cur;
    }
    url->set_fragment(heap, fragment_start, fragment_end - fragment_start, copy_strings);
  }

  *start = cur;
  return PARSE_RESULT_DONE;
}

ParseResult
url_parse_http_regex(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings)
{
  const char *cur = *start;
  const char *host_end;

  // Do a quick check for "://" - our only format check.
  if (end - cur > 3 && (((':' ^ *cur) | ('/' ^ cur[1]) | ('/' ^ cur[2])) == 0)) {
    cur += 3;
  } else if (':' == *cur && (++cur >= end || ('/' == *cur && (++cur >= end || ('/' == *cur && ++cur >= end))))) {
    return PARSE_RESULT_ERROR;
  }

  // Grab everything until EOS or slash.
  const char *base = cur;
  cur              = static_cast<const char *>(memchr(cur, '/', end - cur));
  if (cur) {
    host_end = cur;
    // Remove all preceding slashes
    while (cur < end && *cur == '/') {
      cur++;
    }
  } else {
    host_end = cur = end;
  }

  // Did we find something for the host?
  if (base != host_end) {
    const char *port = nullptr;
    int port_len     = 0;

    // Check for port. Search from the end stopping on the first non-digit
    // or more than 5 digits and a delimiter.
    port                   = host_end - 1;
    const char *port_limit = host_end - 6;
    if (port_limit < base) {
      port_limit = base; // don't go past start.
    }

    while (port >= port_limit && isdigit(*port)) {
      --port;
    }

    // A port if we're still in the host area and we found a ':' as
    // the immediately preceeding character.
    if (port >= base && ':' == *port) {
      port_len = host_end - port - 1; // must compute this first.
      host_end = port;                // then point at colon.
      ++port;                         // drop colon from port.
      url->set_port(heap, port, port_len, copy_strings);
    }

    // Now we can set the host.
    url->set_host(heap, base, host_end - base, copy_strings);
  }

  // path is anything that's left.
  if (cur < end) {
    url->set_path(heap, cur, end - cur, copy_strings);
    cur = end;
  }
  *start = cur;
  return PARSE_RESULT_DONE;
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *                           P R I N T I N G                           *
 *                                                                     *
 ***********************************************************************/

int
url_print(URLImpl *url, char *buf_start, int buf_length, int *buf_index_inout, int *buf_chars_to_skip_inout,
          unsigned normalization_flags)
{
#define TRY(x) \
  if (!x)      \
  return 0

  bool scheme_added = false;
  if (url->m_ptr_scheme) {
    TRY(((normalization_flags & URLNormalize::LC_SCHEME_HOST) ? mime_mem_print_lc : mime_mem_print)(
      url->m_ptr_scheme, url->m_len_scheme, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    scheme_added = true;

  } else if (normalization_flags & URLNormalize::IMPLIED_SCHEME) {
    if (URL_TYPE_HTTP == url->m_url_type) {
      TRY(mime_mem_print(URL_SCHEME_HTTP, URL_LEN_HTTP, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
      scheme_added = true;

    } else if (URL_TYPE_HTTPS == url->m_url_type) {
      TRY(mime_mem_print(URL_SCHEME_HTTPS, URL_LEN_HTTPS, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
      scheme_added = true;
    }
  }
  if (scheme_added) {
    TRY(mime_mem_print("://", 3, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
  }

  if (url->m_ptr_user) {
    TRY(mime_mem_print(url->m_ptr_user, url->m_len_user, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    if (url->m_ptr_password) {
      TRY(mime_mem_print(":", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
      TRY(
        mime_mem_print(url->m_ptr_password, url->m_len_password, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    }
    TRY(mime_mem_print("@", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
  }

  if (url->m_ptr_host) {
    // Force brackets for IPv6. Note colon must occur in first 5 characters.
    // But it can be less (e.g. "::1").
    int n          = url->m_len_host;
    bool bracket_p = '[' != *url->m_ptr_host && (nullptr != memchr(url->m_ptr_host, ':', n > 5 ? 5 : n));
    if (bracket_p) {
      TRY(mime_mem_print("[", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    }
    TRY(((normalization_flags & URLNormalize::LC_SCHEME_HOST) ? mime_mem_print_lc : mime_mem_print)(
      url->m_ptr_host, url->m_len_host, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    if (bracket_p) {
      TRY(mime_mem_print("]", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    }
    if (url->m_ptr_port && url->m_port) {
      TRY(mime_mem_print(":", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
      TRY(mime_mem_print(url->m_ptr_port, url->m_len_port, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    }
  }

  if (!url->m_path_is_empty) {
    TRY(mime_mem_print("/", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
  }
  if (url->m_ptr_path) {
    TRY(mime_mem_print(url->m_ptr_path, url->m_len_path, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
  }

  if (url->m_ptr_params && url->m_len_params > 0) {
    TRY(mime_mem_print(";", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    TRY(mime_mem_print(url->m_ptr_params, url->m_len_params, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
  }

  if (url->m_ptr_query && url->m_len_query > 0) {
    TRY(mime_mem_print("?", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    TRY(mime_mem_print(url->m_ptr_query, url->m_len_query, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
  }

  if (url->m_ptr_fragment && url->m_len_fragment > 0) {
    TRY(mime_mem_print("#", 1, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
    TRY(mime_mem_print(url->m_ptr_fragment, url->m_len_fragment, buf_start, buf_length, buf_index_inout, buf_chars_to_skip_inout));
  }

  return 1;

#undef TRY
}

void
url_describe(HdrHeapObjImpl *raw, bool /* recurse ATS_UNUSED */)
{
  URLImpl *obj = (URLImpl *)raw;

  Debug("http", "[URLTYPE: %d, SWKSIDX: %d,", obj->m_url_type, obj->m_scheme_wks_idx);
  Debug("http", "\tSCHEME: \"%.*s\", SCHEME_LEN: %d,", obj->m_len_scheme, (obj->m_ptr_scheme ? obj->m_ptr_scheme : "NULL"),
        obj->m_len_scheme);
  Debug("http", "\tUSER: \"%.*s\", USER_LEN: %d,", obj->m_len_user, (obj->m_ptr_user ? obj->m_ptr_user : "NULL"), obj->m_len_user);
  Debug("http", "\tPASSWORD: \"%.*s\", PASSWORD_LEN: %d,", obj->m_len_password,
        (obj->m_ptr_password ? obj->m_ptr_password : "NULL"), obj->m_len_password);
  Debug("http", "\tHOST: \"%.*s\", HOST_LEN: %d,", obj->m_len_host, (obj->m_ptr_host ? obj->m_ptr_host : "NULL"), obj->m_len_host);
  Debug("http", "\tPORT: \"%.*s\", PORT_LEN: %d, PORT_NUM: %d", obj->m_len_port, (obj->m_ptr_port ? obj->m_ptr_port : "NULL"),
        obj->m_len_port, obj->m_port);
  Debug("http", "\tPATH: \"%.*s\", PATH_LEN: %d,", obj->m_len_path, (obj->m_ptr_path ? obj->m_ptr_path : "NULL"), obj->m_len_path);
  Debug("http", "\tPARAMS: \"%.*s\", PARAMS_LEN: %d,", obj->m_len_params, (obj->m_ptr_params ? obj->m_ptr_params : "NULL"),
        obj->m_len_params);
  Debug("http", "\tQUERY: \"%.*s\", QUERY_LEN: %d,", obj->m_len_query, (obj->m_ptr_query ? obj->m_ptr_query : "NULL"),
        obj->m_len_query);
  Debug("http", "\tFRAGMENT: \"%.*s\", FRAGMENT_LEN: %d]", obj->m_len_fragment,
        (obj->m_ptr_fragment ? obj->m_ptr_fragment : "NULL"), obj->m_len_fragment);
}

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

/***********************************************************************
 *                                                                     *
 *                        U R L    D I G E S T S                       *
 *                                                                     *
 ***********************************************************************/

static inline void
memcpy_tolower(char *d, const char *s, int n)
{
  while (n--) {
    *d = ParseRules::ink_tolower(*s);
    s++;
    d++;
  }
}

#define BUFSIZE 512

// fast path for CryptoHash, HTTP, no user/password/params/query,
// no buffer overflow, no unescaping needed

static inline void
url_CryptoHash_get_fast(const URLImpl *url, CryptoContext &ctx, CryptoHash *hash, cache_generation_t generation)
{
  char buffer[BUFSIZE];
  char *p;

  p = buffer;
  memcpy_tolower(p, url->m_ptr_scheme, url->m_len_scheme);
  p += url->m_len_scheme;
  *p++ = ':';
  *p++ = '/';
  *p++ = '/';
  // no user
  *p++ = ':';
  // no password
  *p++ = '@';
  memcpy_tolower(p, url->m_ptr_host, url->m_len_host);
  p += url->m_len_host;
  *p++ = '/';
  memcpy(p, url->m_ptr_path, url->m_len_path);
  p += url->m_len_path;
  *p++ = ';';
  // no params
  *p++ = '?';
  // no query

  ink_assert(sizeof(url->m_port) == 2);
  uint16_t port = static_cast<uint16_t>(url_canonicalize_port(url->m_url_type, url->m_port));
  *p++          = (reinterpret_cast<char *>(&port))[0];
  *p++          = (reinterpret_cast<char *>(&port))[1];

  ctx.update(buffer, p - buffer);
  if (generation != -1) {
    ctx.update(&generation, sizeof(generation));
  }

  ctx.finalize(*hash);
}

static inline void
url_CryptoHash_get_general(const URLImpl *url, CryptoContext &ctx, CryptoHash &hash, cache_generation_t generation)
{
  char buffer[BUFSIZE];
  char *p, *e;
  const char *strs[13], *ends[13];
  const char *t;
  in_port_t port;
  int i, s;

  strs[0] = url->m_ptr_scheme;
  strs[1] = "://";
  strs[2] = url->m_ptr_user;
  strs[3] = ":";
  strs[4] = url->m_ptr_password;
  strs[5] = "@";
  strs[6] = url->m_ptr_host;
  strs[7] = "/";
  strs[8] = url->m_ptr_path;

  ends[0] = strs[0] + url->m_len_scheme;
  ends[1] = strs[1] + 3;
  ends[2] = strs[2] + url->m_len_user;
  ends[3] = strs[3] + 1;
  ends[4] = strs[4] + url->m_len_password;
  ends[5] = strs[5] + 1;
  ends[6] = strs[6] + url->m_len_host;
  ends[7] = strs[7] + 1;
  ends[8] = strs[8] + url->m_len_path;

  strs[9]  = ";";
  strs[10] = url->m_ptr_params;
  strs[11] = "?";
  strs[12] = url->m_ptr_query;
  ends[9]  = strs[9] + 1;
  ends[10] = strs[10] + url->m_len_params;
  ends[11] = strs[11] + 1;
  ends[12] = strs[12] + url->m_len_query;

  p = buffer;
  e = buffer + BUFSIZE;

  for (i = 0; i < 13; i++) {
    if (strs[i]) {
      t = strs[i];
      s = 0;

      while (t < ends[i]) {
        if ((i == 0) || (i == 6)) { // scheme and host
          unescape_str_tolower(p, e, t, ends[i], s);
        } else if (i == 8 || i == 10 || i == 12) { // path, params, query
          // Don't unescape the parts of the URI that are processed by the
          // origin since it may behave differently based upon whether these are
          // escaped or not. Therefore differently encoded strings should be
          // cached separately via differentiated hashes.
          int path_len = ends[i] - t;
          int min_len  = std::min(path_len, static_cast<int>(e - p));
          memcpy(p, t, min_len);
          p += min_len;
          t += min_len;
        } else {
          unescape_str(p, e, t, ends[i], s);
        }

        if (p == e) {
          ctx.update(buffer, BUFSIZE);
          p = buffer;
        }
      }
    }
  }

  if (p != buffer) {
    ctx.update(buffer, p - buffer);
  }
  int buffer_len = static_cast<int>(p - buffer);
  port           = url_canonicalize_port(url->m_url_type, url->m_port);

  ctx.update(&port, sizeof(port));
  if (generation != -1) {
    ctx.update(&generation, sizeof(generation));
    Debug("url_cachekey", "Final url string for cache hash key %.*s%d%d", buffer_len, buffer, port, static_cast<int>(generation));
  } else {
    Debug("url_cachekey", "Final url string for cache hash key %.*s%d", buffer_len, buffer, port);
  }
  ctx.finalize(hash);
}

void
url_CryptoHash_get(const URLImpl *url, CryptoHash *hash, cache_generation_t generation)
{
  URLHashContext ctx;
  if ((url_hash_method != 0) && (url->m_url_type == URL_TYPE_HTTP) &&
      ((url->m_len_user + url->m_len_password + url->m_len_params + url->m_len_query) == 0) &&
      (3 + 1 + 1 + 1 + 1 + 1 + 2 + url->m_len_scheme + url->m_len_host + url->m_len_path < BUFSIZE) &&
      (memchr(url->m_ptr_host, '%', url->m_len_host) == nullptr) && (memchr(url->m_ptr_path, '%', url->m_len_path) == nullptr)) {
    url_CryptoHash_get_fast(url, ctx, hash, generation);
#ifdef DEBUG
    CryptoHash hash_general;
    url_CryptoHash_get_general(url, ctx, hash_general, generation);
    ink_assert(*hash == hash_general);
#endif
  } else {
    url_CryptoHash_get_general(url, ctx, *hash, generation);
  }
}

#undef BUFSIZE

/*-------------------------------------------------------------------------
  -------------------------------------------------------------------------*/

void
url_host_CryptoHash_get(URLImpl *url, CryptoHash *hash)
{
  CryptoContext ctx;

  if (url->m_ptr_scheme) {
    ctx.update(url->m_ptr_scheme, url->m_len_scheme);
  }

  ctx.update("://", 3);

  if (url->m_ptr_host) {
    ctx.update(url->m_ptr_host, url->m_len_host);
  }

  ctx.update(":", 1);

  // [amc] Why is this <int> and not <in_port_t>?
  // Especially since it's in_port_t for url_CryptoHash_get.
  int port = url_canonicalize_port(url->m_url_type, url->m_port);
  ctx.update(&port, sizeof(port));
  ctx.finalize(*hash);
}