summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/e10s-content/lib/httpd.js
blob: e46ca96a0da0e299143683d74d8a008e4434a5ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* An implementation of an HTTP server both as a loadable script and as an XPCOM
* component. See the accompanying README file for user documentation on
* httpd.js.
*/

module.metadata = {
  "stability": "experimental"
};

const { components, CC, Cc, Ci, Cr, Cu } = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");


const PR_UINT32_MAX = Math.pow(2, 32) - 1;

/** True if debugging output is enabled, false otherwise. */
var DEBUG = false; // non-const *only* so tweakable in server tests

/** True if debugging output should be timestamped. */
var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests

var gGlobalObject = Cc["@mozilla.org/systemprincipal;1"].createInstance();

/**
* Asserts that the given condition holds. If it doesn't, the given message is
* dumped, a stack trace is printed, and an exception is thrown to attempt to
* stop execution (which unfortunately must rely upon the exception not being
* accidentally swallowed by the code that uses it).
*/
function NS_ASSERT(cond, msg)
{
  if (DEBUG && !cond)
  {
    dumpn("###!!!");
    dumpn("###!!! ASSERTION" + (msg ? ": " + msg : "!"));
    dumpn("###!!! Stack follows:");

    var stack = new Error().stack.split(/\n/);
    dumpn(stack.map(function(val) { return "###!!! " + val; }).join("\n"));

    throw Cr.NS_ERROR_ABORT;
  }
}

/** Constructs an HTTP error object. */
function HttpError(code, description)
{
  this.code = code;
  this.description = description;
}
HttpError.prototype =
{
  toString: function()
  {
    return this.code + " " + this.description;
  }
};

/**
* Errors thrown to trigger specific HTTP server responses.
*/
const HTTP_400 = new HttpError(400, "Bad Request");
const HTTP_401 = new HttpError(401, "Unauthorized");
const HTTP_402 = new HttpError(402, "Payment Required");
const HTTP_403 = new HttpError(403, "Forbidden");
const HTTP_404 = new HttpError(404, "Not Found");
const HTTP_405 = new HttpError(405, "Method Not Allowed");
const HTTP_406 = new HttpError(406, "Not Acceptable");
const HTTP_407 = new HttpError(407, "Proxy Authentication Required");
const HTTP_408 = new HttpError(408, "Request Timeout");
const HTTP_409 = new HttpError(409, "Conflict");
const HTTP_410 = new HttpError(410, "Gone");
const HTTP_411 = new HttpError(411, "Length Required");
const HTTP_412 = new HttpError(412, "Precondition Failed");
const HTTP_413 = new HttpError(413, "Request Entity Too Large");
const HTTP_414 = new HttpError(414, "Request-URI Too Long");
const HTTP_415 = new HttpError(415, "Unsupported Media Type");
const HTTP_417 = new HttpError(417, "Expectation Failed");

const HTTP_500 = new HttpError(500, "Internal Server Error");
const HTTP_501 = new HttpError(501, "Not Implemented");
const HTTP_502 = new HttpError(502, "Bad Gateway");
const HTTP_503 = new HttpError(503, "Service Unavailable");
const HTTP_504 = new HttpError(504, "Gateway Timeout");
const HTTP_505 = new HttpError(505, "HTTP Version Not Supported");

/** Creates a hash with fields corresponding to the values in arr. */
function array2obj(arr)
{
  var obj = {};
  for (var i = 0; i < arr.length; i++)
    obj[arr[i]] = arr[i];
  return obj;
}

/** Returns an array of the integers x through y, inclusive. */
function range(x, y)
{
  var arr = [];
  for (var i = x; i <= y; i++)
    arr.push(i);
  return arr;
}

/** An object (hash) whose fields are the numbers of all HTTP error codes. */
const HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505)));


/**
* The character used to distinguish hidden files from non-hidden files, a la
* the leading dot in Apache. Since that mechanism also hides files from
* easy display in LXR, ls output, etc. however, we choose instead to use a
* suffix character. If a requested file ends with it, we append another
* when getting the file on the server. If it doesn't, we just look up that
* file. Therefore, any file whose name ends with exactly one of the character
* is "hidden" and available for use by the server.
*/
const HIDDEN_CHAR = "^";

/**
* The file name suffix indicating the file containing overridden headers for
* a requested file.
*/
const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR;

/** Type used to denote SJS scripts for CGI-like functionality. */
const SJS_TYPE = "sjs";

/** Base for relative timestamps produced by dumpn(). */
var firstStamp = 0;

/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
function dumpn(str)
{
  if (DEBUG)
  {
    var prefix = "HTTPD-INFO | ";
    if (DEBUG_TIMESTAMP)
    {
      if (firstStamp === 0)
        firstStamp = Date.now();

      var elapsed = Date.now() - firstStamp; // milliseconds
      var min = Math.floor(elapsed / 60000);
      var sec = (elapsed % 60000) / 1000;

      if (sec < 10)
        prefix += min + ":0" + sec.toFixed(3) + " | ";
      else
        prefix += min + ":" + sec.toFixed(3) + " | ";
    }

    dump(prefix + str + "\n");
  }
}

/** Dumps the current JS stack if DEBUG. */
function dumpStack()
{
  // peel off the frames for dumpStack() and Error()
  var stack = new Error().stack.split(/\n/).slice(2);
  stack.forEach(dumpn);
}


/** The XPCOM thread manager. */
var gThreadManager = null;

/** The XPCOM prefs service. */
var gRootPrefBranch = null;
function getRootPrefBranch()
{
  if (!gRootPrefBranch)
  {
    gRootPrefBranch = Cc["@mozilla.org/preferences-service;1"]
                        .getService(Ci.nsIPrefBranch);
  }
  return gRootPrefBranch;
}

/**
* JavaScript constructors for commonly-used classes; precreating these is a
* speedup over doing the same from base principles. See the docs at
* http://developer.mozilla.org/en/docs/components.Constructor for details.
*/
const ServerSocket = CC("@mozilla.org/network/server-socket;1",
                        "nsIServerSocket",
                        "init");
const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1",
                                 "nsIScriptableInputStream",
                                 "init");
const Pipe = CC("@mozilla.org/pipe;1",
                "nsIPipe",
                "init");
const FileInputStream = CC("@mozilla.org/network/file-input-stream;1",
                           "nsIFileInputStream",
                           "init");
const ConverterInputStream = CC("@mozilla.org/intl/converter-input-stream;1",
                                "nsIConverterInputStream",
                                "init");
const WritablePropertyBag = CC("@mozilla.org/hash-property-bag;1",
                               "nsIWritablePropertyBag2");
const SupportsString = CC("@mozilla.org/supports-string;1",
                          "nsISupportsString");

/* These two are non-const only so a test can overwrite them. */
var BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
                           "nsIBinaryInputStream",
                           "setInputStream");
var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
                            "nsIBinaryOutputStream",
                            "setOutputStream");

/**
* Returns the RFC 822/1123 representation of a date.
*
* @param date : Number
* the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT
* @returns string
* the representation of the given date
*/
function toDateString(date)
{
  //
  // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
  // date1 = 2DIGIT SP month SP 4DIGIT
  // ; day month year (e.g., 02 Jun 1982)
  // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
  // ; 00:00:00 - 23:59:59
  // wkday = "Mon" | "Tue" | "Wed"
  // | "Thu" | "Fri" | "Sat" | "Sun"
  // month = "Jan" | "Feb" | "Mar" | "Apr"
  // | "May" | "Jun" | "Jul" | "Aug"
  // | "Sep" | "Oct" | "Nov" | "Dec"
  //

  const wkdayStrings = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  const monthStrings = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

  /**
* Processes a date and returns the encoded UTC time as a string according to
* the format specified in RFC 2616.
*
* @param date : Date
* the date to process
* @returns string
* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
*/
  function toTime(date)
  {
    var hrs = date.getUTCHours();
    var rv = (hrs < 10) ? "0" + hrs : hrs;

    var mins = date.getUTCMinutes();
    rv += ":";
    rv += (mins < 10) ? "0" + mins : mins;

    var secs = date.getUTCSeconds();
    rv += ":";
    rv += (secs < 10) ? "0" + secs : secs;

    return rv;
  }

  /**
* Processes a date and returns the encoded UTC date as a string according to
* the date1 format specified in RFC 2616.
*
* @param date : Date
* the date to process
* @returns string
* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
*/
  function toDate1(date)
  {
    var day = date.getUTCDate();
    var month = date.getUTCMonth();
    var year = date.getUTCFullYear();

    var rv = (day < 10) ? "0" + day : day;
    rv += " " + monthStrings[month];
    rv += " " + year;

    return rv;
  }

  date = new Date(date);

  const fmtString = "%wkday%, %date1% %time% GMT";
  var rv = fmtString.replace("%wkday%", wkdayStrings[date.getUTCDay()]);
  rv = rv.replace("%time%", toTime(date));
  return rv.replace("%date1%", toDate1(date));
}

/**
* Prints out a human-readable representation of the object o and its fields,
* omitting those whose names begin with "_" if showMembers != true (to ignore
* "private" properties exposed via getters/setters).
*/
function printObj(o, showMembers)
{
  var s = "******************************\n";
  s += "o = {\n";
  for (var i in o)
  {
    if (typeof(i) != "string" ||
        (showMembers || (i.length > 0 && i[0] != "_")))
      s+= " " + i + ": " + o[i] + ",\n";
  }
  s += " };\n";
  s += "******************************";
  dumpn(s);
}

/**
* Instantiates a new HTTP server.
*/
function nsHttpServer()
{
  if (!gThreadManager)
    gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();

  /** The port on which this server listens. */
  this._port = undefined;

  /** The socket associated with this. */
  this._socket = null;

  /** The handler used to process requests to this server. */
  this._handler = new ServerHandler(this);

  /** Naming information for this server. */
  this._identity = new ServerIdentity();

  /**
* Indicates when the server is to be shut down at the end of the request.
*/
  this._doQuit = false;

  /**
* True if the socket in this is closed (and closure notifications have been
* sent and processed if the socket was ever opened), false otherwise.
*/
  this._socketClosed = true;

  /**
* Used for tracking existing connections and ensuring that all connections
* are properly cleaned up before server shutdown; increases by 1 for every
* new incoming connection.
*/
  this._connectionGen = 0;

  /**
* Hash of all open connections, indexed by connection number at time of
* creation.
*/
  this._connections = {};
}
nsHttpServer.prototype =
{
  classID: components.ID("{54ef6f81-30af-4b1d-ac55-8ba811293e41}"),

  // NSISERVERSOCKETLISTENER

  /**
* Processes an incoming request coming in on the given socket and contained
* in the given transport.
*
* @param socket : nsIServerSocket
* the socket through which the request was served
* @param trans : nsISocketTransport
* the transport for the request/response
* @see nsIServerSocketListener.onSocketAccepted
*/
  onSocketAccepted: function(socket, trans)
  {
    dumpn("*** onSocketAccepted(socket=" + socket + ", trans=" + trans + ")");

    dumpn(">>> new connection on " + trans.host + ":" + trans.port);

    const SEGMENT_SIZE = 8192;
    const SEGMENT_COUNT = 1024;
    try
    {
      var input = trans.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
                       .QueryInterface(Ci.nsIAsyncInputStream);
      var output = trans.openOutputStream(0, 0, 0);
    }
    catch (e)
    {
      dumpn("*** error opening transport streams: " + e);
      trans.close(Cr.NS_BINDING_ABORTED);
      return;
    }

    var connectionNumber = ++this._connectionGen;

    try
    {
      var conn = new Connection(input, output, this, socket.port, trans.port,
                                connectionNumber);
      var reader = new RequestReader(conn);

      // XXX add request timeout functionality here!

      // Note: must use main thread here, or we might get a GC that will cause
      // threadsafety assertions. We really need to fix XPConnect so that
      // you can actually do things in multi-threaded JS. :-(
      input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
    }
    catch (e)
    {
      // Assume this connection can't be salvaged and bail on it completely;
      // don't attempt to close it so that we can assert that any connection
      // being closed is in this._connections.
      dumpn("*** error in initial request-processing stages: " + e);
      trans.close(Cr.NS_BINDING_ABORTED);
      return;
    }

    this._connections[connectionNumber] = conn;
    dumpn("*** starting connection " + connectionNumber);
  },

  /**
* Called when the socket associated with this is closed.
*
* @param socket : nsIServerSocket
* the socket being closed
* @param status : nsresult
* the reason the socket stopped listening (NS_BINDING_ABORTED if the server
* was stopped using nsIHttpServer.stop)
* @see nsIServerSocketListener.onStopListening
*/
  onStopListening: function(socket, status)
  {
    dumpn(">>> shutting down server on port " + socket.port);
    this._socketClosed = true;
    if (!this._hasOpenConnections())
    {
      dumpn("*** no open connections, notifying async from onStopListening");

      // Notify asynchronously so that any pending teardown in stop() has a
      // chance to run first.
      var self = this;
      var stopEvent =
        {
          run: function()
          {
            dumpn("*** _notifyStopped async callback");
            self._notifyStopped();
          }
        };
      gThreadManager.currentThread
                    .dispatch(stopEvent, Ci.nsIThread.DISPATCH_NORMAL);
    }
  },

  // NSIHTTPSERVER

  //
  // see nsIHttpServer.start
  //
  start: function(port)
  {
    this._start(port, "localhost")
  },

  _start: function(port, host)
  {
    if (this._socket)
      throw Cr.NS_ERROR_ALREADY_INITIALIZED;

    this._port = port;
    this._doQuit = this._socketClosed = false;

    this._host = host;

    // The listen queue needs to be long enough to handle
    // network.http.max-persistent-connections-per-server concurrent connections,
    // plus a safety margin in case some other process is talking to
    // the server as well.
    var prefs = getRootPrefBranch();
    var maxConnections;
    try {
      // Bug 776860: The original pref was removed in favor of this new one:
      maxConnections = prefs.getIntPref("network.http.max-persistent-connections-per-server") + 5;
    }
    catch(e) {
      maxConnections = prefs.getIntPref("network.http.max-connections-per-server") + 5;
    }

    try
    {
      var loopback = true;
      if (this._host != "127.0.0.1" && this._host != "localhost") {
        var loopback = false;
      }

      var socket = new ServerSocket(this._port,
                                    loopback, // true = localhost, false = everybody
                                    maxConnections);
      dumpn(">>> listening on port " + socket.port + ", " + maxConnections +
            " pending connections");
      socket.asyncListen(this);
      this._identity._initialize(socket.port, host, true);
      this._socket = socket;
    }
    catch (e)
    {
      dumpn("!!! could not start server on port " + port + ": " + e);
      throw Cr.NS_ERROR_NOT_AVAILABLE;
    }
  },

  //
  // see nsIHttpServer.stop
  //
  stop: function(callback)
  {
    if (!callback)
      throw Cr.NS_ERROR_NULL_POINTER;
    if (!this._socket)
      throw Cr.NS_ERROR_UNEXPECTED;

    this._stopCallback = typeof callback === "function"
                       ? callback
                       : function() { callback.onStopped(); };

    dumpn(">>> stopping listening on port " + this._socket.port);
    this._socket.close();
    this._socket = null;

    // We can't have this identity any more, and the port on which we're running
    // this server now could be meaningless the next time around.
    this._identity._teardown();

    this._doQuit = false;

    // socket-close notification and pending request completion happen async
  },

  //
  // see nsIHttpServer.registerFile
  //
  registerFile: function(path, file)
  {
    if (file && (!file.exists() || file.isDirectory()))
      throw Cr.NS_ERROR_INVALID_ARG;

    this._handler.registerFile(path, file);
  },

  //
  // see nsIHttpServer.registerDirectory
  //
  registerDirectory: function(path, directory)
  {
    // XXX true path validation!
    if (path.charAt(0) != "/" ||
        path.charAt(path.length - 1) != "/" ||
        (directory &&
         (!directory.exists() || !directory.isDirectory())))
      throw Cr.NS_ERROR_INVALID_ARG;

    // XXX determine behavior of nonexistent /foo/bar when a /foo/bar/ mapping
    // exists!

    this._handler.registerDirectory(path, directory);
  },

  //
  // see nsIHttpServer.registerPathHandler
  //
  registerPathHandler: function(path, handler)
  {
    this._handler.registerPathHandler(path, handler);
  },

  //
  // see nsIHttpServer.registerPrefixHandler
  //
  registerPrefixHandler: function(prefix, handler)
  {
    this._handler.registerPrefixHandler(prefix, handler);
  },

  //
  // see nsIHttpServer.registerErrorHandler
  //
  registerErrorHandler: function(code, handler)
  {
    this._handler.registerErrorHandler(code, handler);
  },

  //
  // see nsIHttpServer.setIndexHandler
  //
  setIndexHandler: function(handler)
  {
    this._handler.setIndexHandler(handler);
  },

  //
  // see nsIHttpServer.registerContentType
  //
  registerContentType: function(ext, type)
  {
    this._handler.registerContentType(ext, type);
  },

  //
  // see nsIHttpServer.serverIdentity
  //
  get identity()
  {
    return this._identity;
  },

  //
  // see nsIHttpServer.getState
  //
  getState: function(path, k)
  {
    return this._handler._getState(path, k);
  },

  //
  // see nsIHttpServer.setState
  //
  setState: function(path, k, v)
  {
    return this._handler._setState(path, k, v);
  },

  //
  // see nsIHttpServer.getSharedState
  //
  getSharedState: function(k)
  {
    return this._handler._getSharedState(k);
  },

  //
  // see nsIHttpServer.setSharedState
  //
  setSharedState: function(k, v)
  {
    return this._handler._setSharedState(k, v);
  },

  //
  // see nsIHttpServer.getObjectState
  //
  getObjectState: function(k)
  {
    return this._handler._getObjectState(k);
  },

  //
  // see nsIHttpServer.setObjectState
  //
  setObjectState: function(k, v)
  {
    return this._handler._setObjectState(k, v);
  },


  // NSISUPPORTS

  //
  // see nsISupports.QueryInterface
  //
  QueryInterface: function(iid)
  {
    if (iid.equals(Ci.nsIServerSocketListener) || iid.equals(Ci.nsISupports))
      return this;

    throw Cr.NS_ERROR_NO_INTERFACE;
  },


  // NON-XPCOM PUBLIC API

  /**
* Returns true iff this server is not running (and is not in the process of
* serving any requests still to be processed when the server was last
* stopped after being run).
*/
  isStopped: function()
  {
    return this._socketClosed && !this._hasOpenConnections();
  },

  // PRIVATE IMPLEMENTATION

  /** True if this server has any open connections to it, false otherwise. */
  _hasOpenConnections: function()
  {
    //
    // If we have any open connections, they're tracked as numeric properties on
    // |this._connections|. The non-standard __count__ property could be used
    // to check whether there are any properties, but standard-wise, even
    // looking forward to ES5, there's no less ugly yet still O(1) way to do
    // this.
    //
    for (var n in this._connections)
      return true;
    return false;
  },

  /** Calls the server-stopped callback provided when stop() was called. */
  _notifyStopped: function()
  {
    NS_ASSERT(this._stopCallback !== null, "double-notifying?");
    NS_ASSERT(!this._hasOpenConnections(), "should be done serving by now");

    //
    // NB: We have to grab this now, null out the member, *then* call the
    // callback here, or otherwise the callback could (indirectly) futz with
    // this._stopCallback by starting and immediately stopping this, at
    // which point we'd be nulling out a field we no longer have a right to
    // modify.
    //
    var callback = this._stopCallback;
    this._stopCallback = null;
    try
    {
      callback();
    }
    catch (e)
    {
      // not throwing because this is specified as being usually (but not
      // always) asynchronous
      dump("!!! error running onStopped callback: " + e + "\n");
    }
  },

  /**
* Notifies this server that the given connection has been closed.
*
* @param connection : Connection
* the connection that was closed
*/
  _connectionClosed: function(connection)
  {
    NS_ASSERT(connection.number in this._connections,
              "closing a connection " + this + " that we never added to the " +
              "set of open connections?");
    NS_ASSERT(this._connections[connection.number] === connection,
              "connection number mismatch? " +
              this._connections[connection.number]);
    delete this._connections[connection.number];

    // Fire a pending server-stopped notification if it's our responsibility.
    if (!this._hasOpenConnections() && this._socketClosed)
      this._notifyStopped();
  },

  /**
* Requests that the server be shut down when possible.
*/
  _requestQuit: function()
  {
    dumpn(">>> requesting a quit");
    dumpStack();
    this._doQuit = true;
  }
};


//
// RFC 2396 section 3.2.2:
//
// host = hostname | IPv4address
// hostname = *( domainlabel "." ) toplabel [ "." ]
// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
// IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
//

const HOST_REGEX =
  new RegExp("^(?:" +
               // *( domainlabel "." )
               "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)*" +
               // toplabel
               "[a-z](?:[a-z0-9-]*[a-z0-9])?" +
             "|" +
               // IPv4 address
               "\\d+\\.\\d+\\.\\d+\\.\\d+" +
             ")$",
             "i");


/**
* Represents the identity of a server. An identity consists of a set of
* (scheme, host, port) tuples denoted as locations (allowing a single server to
* serve multiple sites or to be used behind both HTTP and HTTPS proxies for any
* host/port). Any incoming request must be to one of these locations, or it
* will be rejected with an HTTP 400 error. One location, denoted as the
* primary location, is the location assigned in contexts where a location
* cannot otherwise be endogenously derived, such as for HTTP/1.0 requests.
*
* A single identity may contain at most one location per unique host/port pair;
* other than that, no restrictions are placed upon what locations may
* constitute an identity.
*/
function ServerIdentity()
{
  /** The scheme of the primary location. */
  this._primaryScheme = "http";

  /** The hostname of the primary location. */
  this._primaryHost = "127.0.0.1"

  /** The port number of the primary location. */
  this._primaryPort = -1;

  /**
* The current port number for the corresponding server, stored so that a new
* primary location can always be set if the current one is removed.
*/
  this._defaultPort = -1;

  /**
* Maps hosts to maps of ports to schemes, e.g. the following would represent
* https://example.com:789/ and http://example.org/:
*
* {
* "xexample.com": { 789: "https" },
* "xexample.org": { 80: "http" }
* }
*
* Note the "x" prefix on hostnames, which prevents collisions with special
* JS names like "prototype".
*/
  this._locations = { "xlocalhost": {} };
}
ServerIdentity.prototype =
{
  // NSIHTTPSERVERIDENTITY

  //
  // see nsIHttpServerIdentity.primaryScheme
  //
  get primaryScheme()
  {
    if (this._primaryPort === -1)
      throw Cr.NS_ERROR_NOT_INITIALIZED;
    return this._primaryScheme;
  },

  //
  // see nsIHttpServerIdentity.primaryHost
  //
  get primaryHost()
  {
    if (this._primaryPort === -1)
      throw Cr.NS_ERROR_NOT_INITIALIZED;
    return this._primaryHost;
  },

  //
  // see nsIHttpServerIdentity.primaryPort
  //
  get primaryPort()
  {
    if (this._primaryPort === -1)
      throw Cr.NS_ERROR_NOT_INITIALIZED;
    return this._primaryPort;
  },

  //
  // see nsIHttpServerIdentity.add
  //
  add: function(scheme, host, port)
  {
    this._validate(scheme, host, port);

    var entry = this._locations["x" + host];
    if (!entry)
      this._locations["x" + host] = entry = {};

    entry[port] = scheme;
  },

  //
  // see nsIHttpServerIdentity.remove
  //
  remove: function(scheme, host, port)
  {
    this._validate(scheme, host, port);

    var entry = this._locations["x" + host];
    if (!entry)
      return false;

    var present = port in entry;
    delete entry[port];

    if (this._primaryScheme == scheme &&
        this._primaryHost == host &&
        this._primaryPort == port &&
        this._defaultPort !== -1)
    {
      // Always keep at least one identity in existence at any time, unless
      // we're in the process of shutting down (the last condition above).
      this._primaryPort = -1;
      this._initialize(this._defaultPort, host, false);
    }

    return present;
  },

  //
  // see nsIHttpServerIdentity.has
  //
  has: function(scheme, host, port)
  {
    this._validate(scheme, host, port);

    return "x" + host in this._locations &&
           scheme === this._locations["x" + host][port];
  },

  //
  // see nsIHttpServerIdentity.has
  //
  getScheme: function(host, port)
  {
    this._validate("http", host, port);

    var entry = this._locations["x" + host];
    if (!entry)
      return "";

    return entry[port] || "";
  },

  //
  // see nsIHttpServerIdentity.setPrimary
  //
  setPrimary: function(scheme, host, port)
  {
    this._validate(scheme, host, port);

    this.add(scheme, host, port);

    this._primaryScheme = scheme;
    this._primaryHost = host;
    this._primaryPort = port;
  },


  // NSISUPPORTS

  //
  // see nsISupports.QueryInterface
  //
  QueryInterface: function(iid)
  {
    if (iid.equals(Ci.nsIHttpServerIdentity) || iid.equals(Ci.nsISupports))
      return this;

    throw Cr.NS_ERROR_NO_INTERFACE;
  },


  // PRIVATE IMPLEMENTATION

  /**
* Initializes the primary name for the corresponding server, based on the
* provided port number.
*/
  _initialize: function(port, host, addSecondaryDefault)
  {
    this._host = host;
    if (this._primaryPort !== -1)
      this.add("http", host, port);
    else
      this.setPrimary("http", "localhost", port);
    this._defaultPort = port;

    // Only add this if we're being called at server startup
    if (addSecondaryDefault && host != "127.0.0.1")
      this.add("http", "127.0.0.1", port);
  },

  /**
* Called at server shutdown time, unsets the primary location only if it was
* the default-assigned location and removes the default location from the
* set of locations used.
*/
  _teardown: function()
  {
    if (this._host != "127.0.0.1") {
      // Not the default primary location, nothing special to do here
      this.remove("http", "127.0.0.1", this._defaultPort);
    }

    // This is a *very* tricky bit of reasoning here; make absolutely sure the
    // tests for this code pass before you commit changes to it.
    if (this._primaryScheme == "http" &&
        this._primaryHost == this._host &&
        this._primaryPort == this._defaultPort)
    {
      // Make sure we don't trigger the readding logic in .remove(), then remove
      // the default location.
      var port = this._defaultPort;
      this._defaultPort = -1;
      this.remove("http", this._host, port);

      // Ensure a server start triggers the setPrimary() path in ._initialize()
      this._primaryPort = -1;
    }
    else
    {
      // No reason not to remove directly as it's not our primary location
      this.remove("http", this._host, this._defaultPort);
    }
  },

  /**
* Ensures scheme, host, and port are all valid with respect to RFC 2396.
*
* @throws NS_ERROR_ILLEGAL_VALUE
* if any argument doesn't match the corresponding production
*/
  _validate: function(scheme, host, port)
  {
    if (scheme !== "http" && scheme !== "https")
    {
      dumpn("*** server only supports http/https schemes: '" + scheme + "'");
      dumpStack();
      throw Cr.NS_ERROR_ILLEGAL_VALUE;
    }
    if (!HOST_REGEX.test(host))
    {
      dumpn("*** unexpected host: '" + host + "'");
      throw Cr.NS_ERROR_ILLEGAL_VALUE;
    }
    if (port < 0 || port > 65535)
    {
      dumpn("*** unexpected port: '" + port + "'");
      throw Cr.NS_ERROR_ILLEGAL_VALUE;
    }
  }
};


/**
* Represents a connection to the server (and possibly in the future the thread
* on which the connection is processed).
*
* @param input : nsIInputStream
* stream from which incoming data on the connection is read
* @param output : nsIOutputStream
* stream to write data out the connection
* @param server : nsHttpServer
* the server handling the connection
* @param port : int
* the port on which the server is running
* @param outgoingPort : int
* the outgoing port used by this connection
* @param number : uint
* a serial number used to uniquely identify this connection
*/
function Connection(input, output, server, port, outgoingPort, number)
{
  dumpn("*** opening new connection " + number + " on port " + outgoingPort);

  /** Stream of incoming data. */
  this.input = input;

  /** Stream for outgoing data. */
  this.output = output;

  /** The server associated with this request. */
  this.server = server;

  /** The port on which the server is running. */
  this.port = port;

  /** The outgoing poort used by this connection. */
  this._outgoingPort = outgoingPort;

  /** The serial number of this connection. */
  this.number = number;

  /**
* The request for which a response is being generated, null if the
* incoming request has not been fully received or if it had errors.
*/
  this.request = null;

  /** State variables for debugging. */
  this._closed = this._processed = false;
}
Connection.prototype =
{
  /** Closes this connection's input/output streams. */
  close: function()
  {
    dumpn("*** closing connection " + this.number +
          " on port " + this._outgoingPort);

    this.input.close();
    this.output.close();
    this._closed = true;

    var server = this.server;
    server._connectionClosed(this);

    // If an error triggered a server shutdown, act on it now
    if (server._doQuit)
      server.stop(function() { /* not like we can do anything better */ });
  },

  /**
* Initiates processing of this connection, using the data in the given
* request.
*
* @param request : Request
* the request which should be processed
*/
  process: function(request)
  {
    NS_ASSERT(!this._closed && !this._processed);

    this._processed = true;

    this.request = request;
    this.server._handler.handleResponse(this);
  },

  /**
* Initiates processing of this connection, generating a response with the
* given HTTP error code.
*
* @param code : uint
* an HTTP code, so in the range [0, 1000)
* @param request : Request
* incomplete data about the incoming request (since there were errors
* during its processing
*/
  processError: function(code, request)
  {
    NS_ASSERT(!this._closed && !this._processed);

    this._processed = true;
    this.request = request;
    this.server._handler.handleError(code, this);
  },

  /** Converts this to a string for debugging purposes. */
  toString: function()
  {
    return "<Connection(" + this.number +
           (this.request ? ", " + this.request.path : "") +"): " +
           (this._closed ? "closed" : "open") + ">";
  }
};



/** Returns an array of count bytes from the given input stream. */
function readBytes(inputStream, count)
{
  return new BinaryInputStream(inputStream).readByteArray(count);
}



/** Request reader processing states; see RequestReader for details. */
const READER_IN_REQUEST_LINE = 0;
const READER_IN_HEADERS = 1;
const READER_IN_BODY = 2;
const READER_FINISHED = 3;


/**
* Reads incoming request data asynchronously, does any necessary preprocessing,
* and forwards it to the request handler. Processing occurs in three states:
*
* READER_IN_REQUEST_LINE Reading the request's status line
* READER_IN_HEADERS Reading headers in the request
* READER_IN_BODY Reading the body of the request
* READER_FINISHED Entire request has been read and processed
*
* During the first two stages, initial metadata about the request is gathered
* into a Request object. Once the status line and headers have been processed,
* we start processing the body of the request into the Request. Finally, when
* the entire body has been read, we create a Response and hand it off to the
* ServerHandler to be given to the appropriate request handler.
*
* @param connection : Connection
* the connection for the request being read
*/
function RequestReader(connection)
{
  /** Connection metadata for this request. */
  this._connection = connection;

  /**
* A container providing line-by-line access to the raw bytes that make up the
* data which has been read from the connection but has not yet been acted
* upon (by passing it to the request handler or by extracting request
* metadata from it).
*/
  this._data = new LineData();

  /**
* The amount of data remaining to be read from the body of this request.
* After all headers in the request have been read this is the value in the
* Content-Length header, but as the body is read its value decreases to zero.
*/
  this._contentLength = 0;

  /** The current state of parsing the incoming request. */
  this._state = READER_IN_REQUEST_LINE;

  /** Metadata constructed from the incoming request for the request handler. */
  this._metadata = new Request(connection.port);

  /**
* Used to preserve state if we run out of line data midway through a
* multi-line header. _lastHeaderName stores the name of the header, while
* _lastHeaderValue stores the value we've seen so far for the header.
*
* These fields are always either both undefined or both strings.
*/
  this._lastHeaderName = this._lastHeaderValue = undefined;
}
RequestReader.prototype =
{
  // NSIINPUTSTREAMCALLBACK

  /**
* Called when more data from the incoming request is available. This method
* then reads the available data from input and deals with that data as
* necessary, depending upon the syntax of already-downloaded data.
*
* @param input : nsIAsyncInputStream
* the stream of incoming data from the connection
*/
  onInputStreamReady: function(input)
  {
    dumpn("*** onInputStreamReady(input=" + input + ") on thread " +
          gThreadManager.currentThread + " (main is " +
          gThreadManager.mainThread + ")");
    dumpn("*** this._state == " + this._state);

    // Handle cases where we get more data after a request error has been
    // discovered but *before* we can close the connection.
    var data = this._data;
    if (!data)
      return;

    try
    {
      data.appendBytes(readBytes(input, input.available()));
    }
    catch (e)
    {
      if (streamClosed(e))
      {
        dumpn("*** WARNING: unexpected error when reading from socket; will " +
              "be treated as if the input stream had been closed");
        dumpn("*** WARNING: actual error was: " + e);
      }

      // We've lost a race -- input has been closed, but we're still expecting
      // to read more data. available() will throw in this case, and since
      // we're dead in the water now, destroy the connection.
      dumpn("*** onInputStreamReady called on a closed input, destroying " +
            "connection");
      this._connection.close();
      return;
    }

    switch (this._state)
    {
      default:
        NS_ASSERT(false, "invalid state: " + this._state);
        break;

      case READER_IN_REQUEST_LINE:
        if (!this._processRequestLine())
          break;
        /* fall through */

      case READER_IN_HEADERS:
        if (!this._processHeaders())
          break;
        /* fall through */

      case READER_IN_BODY:
        this._processBody();
    }

    if (this._state != READER_FINISHED)
      input.asyncWait(this, 0, 0, gThreadManager.currentThread);
  },

  //
  // see nsISupports.QueryInterface
  //
  QueryInterface: function(aIID)
  {
    if (aIID.equals(Ci.nsIInputStreamCallback) ||
        aIID.equals(Ci.nsISupports))
      return this;

    throw Cr.NS_ERROR_NO_INTERFACE;
  },


  // PRIVATE API

  /**
* Processes unprocessed, downloaded data as a request line.
*
* @returns boolean
* true iff the request line has been fully processed
*/
  _processRequestLine: function()
  {
    NS_ASSERT(this._state == READER_IN_REQUEST_LINE);

    // Servers SHOULD ignore any empty line(s) received where a Request-Line
    // is expected (section 4.1).
    var data = this._data;
    var line = {};
    var readSuccess;
    while ((readSuccess = data.readLine(line)) && line.value == "")
      dumpn("*** ignoring beginning blank line...");

    // if we don't have a full line, wait until we do
    if (!readSuccess)
      return false;

    // we have the first non-blank line
    try
    {
      this._parseRequestLine(line.value);
      this._state = READER_IN_HEADERS;
      return true;
    }
    catch (e)
    {
      this._handleError(e);
      return false;
    }
  },

  /**
* Processes stored data, assuming it is either at the beginning or in
* the middle of processing request headers.
*
* @returns boolean
* true iff header data in the request has been fully processed
*/
  _processHeaders: function()
  {
    NS_ASSERT(this._state == READER_IN_HEADERS);

    // XXX things to fix here:
    //
    // - need to support RFC 2047-encoded non-US-ASCII characters

    try
    {
      var done = this._parseHeaders();
      if (done)
      {
        var request = this._metadata;

        // XXX this is wrong for requests with transfer-encodings applied to
        // them, particularly chunked (which by its nature can have no
        // meaningful Content-Length header)!
        this._contentLength = request.hasHeader("Content-Length")
                            ? parseInt(request.getHeader("Content-Length"), 10)
                            : 0;
        dumpn("_processHeaders, Content-length=" + this._contentLength);

        this._state = READER_IN_BODY;
      }
      return done;
    }
    catch (e)
    {
      this._handleError(e);
      return false;
    }
  },

  /**
* Processes stored data, assuming it is either at the beginning or in
* the middle of processing the request body.
*
* @returns boolean
* true iff the request body has been fully processed
*/
  _processBody: function()
  {
    NS_ASSERT(this._state == READER_IN_BODY);

    // XXX handle chunked transfer-coding request bodies!

    try
    {
      if (this._contentLength > 0)
      {
        var data = this._data.purge();
        var count = Math.min(data.length, this._contentLength);
        dumpn("*** loading data=" + data + " len=" + data.length +
              " excess=" + (data.length - count));

        var bos = new BinaryOutputStream(this._metadata._bodyOutputStream);
        bos.writeByteArray(data, count);
        this._contentLength -= count;
      }

      dumpn("*** remaining body data len=" + this._contentLength);
      if (this._contentLength == 0)
      {
        this._validateRequest();
        this._state = READER_FINISHED;
        this._handleResponse();
        return true;
      }

      return false;
    }
    catch (e)
    {
      this._handleError(e);
      return false;
    }
  },

  /**
* Does various post-header checks on the data in this request.
*
* @throws : HttpError
* if the request was malformed in some way
*/
  _validateRequest: function()
  {
    NS_ASSERT(this._state == READER_IN_BODY);

    dumpn("*** _validateRequest");

    var metadata = this._metadata;
    var headers = metadata._headers;

    // 19.6.1.1 -- servers MUST report 400 to HTTP/1.1 requests w/o Host header
    var identity = this._connection.server.identity;
    if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
    {
      if (!headers.hasHeader("Host"))
      {
        dumpn("*** malformed HTTP/1.1 or greater request with no Host header!");
        throw HTTP_400;
      }

      // If the Request-URI wasn't absolute, then we need to determine our host.
      // We have to determine what scheme was used to access us based on the
      // server identity data at this point, because the request just doesn't
      // contain enough data on its own to do this, sadly.
      if (!metadata._host)
      {
        var host, port;
        var hostPort = headers.getHeader("Host");
        var colon = hostPort.indexOf(":");
        if (colon < 0)
        {
          host = hostPort;
          port = "";
        }
        else
        {
          host = hostPort.substring(0, colon);
          port = hostPort.substring(colon + 1);
        }

        // NB: We allow an empty port here because, oddly, a colon may be
        // present even without a port number, e.g. "example.com:"; in this
        // case the default port applies.
        if (!HOST_REGEX.test(host) || !/^\d*$/.test(port))
        {
          dumpn("*** malformed hostname (" + hostPort + ") in Host " +
                "header, 400 time");
          throw HTTP_400;
        }

        // If we're not given a port, we're stuck, because we don't know what
        // scheme to use to look up the correct port here, in general. Since
        // the HTTPS case requires a tunnel/proxy and thus requires that the
        // requested URI be absolute (and thus contain the necessary
        // information), let's assume HTTP will prevail and use that.
        port = +port || 80;

        var scheme = identity.getScheme(host, port);
        if (!scheme)
        {
          dumpn("*** unrecognized hostname (" + hostPort + ") in Host " +
                "header, 400 time");
          throw HTTP_400;
        }

        metadata._scheme = scheme;
        metadata._host = host;
        metadata._port = port;
      }
    }
    else
    {
      NS_ASSERT(metadata._host === undefined,
                "HTTP/1.0 doesn't allow absolute paths in the request line!");

      metadata._scheme = identity.primaryScheme;
      metadata._host = identity.primaryHost;
      metadata._port = identity.primaryPort;
    }

    NS_ASSERT(identity.has(metadata._scheme, metadata._host, metadata._port),
              "must have a location we recognize by now!");
  },

  /**
* Handles responses in case of error, either in the server or in the request.
*
* @param e
* the specific error encountered, which is an HttpError in the case where
* the request is in some way invalid or cannot be fulfilled; if this isn't
* an HttpError we're going to be paranoid and shut down, because that
* shouldn't happen, ever
*/
  _handleError: function(e)
  {
    // Don't fall back into normal processing!
    this._state = READER_FINISHED;

    var server = this._connection.server;
    if (e instanceof HttpError)
    {
      var code = e.code;
    }
    else
    {
      dumpn("!!! UNEXPECTED ERROR: " + e +
            (e.lineNumber ? ", line " + e.lineNumber : ""));

      // no idea what happened -- be paranoid and shut down
      code = 500;
      server._requestQuit();
    }

    // make attempted reuse of data an error
    this._data = null;

    this._connection.processError(code, this._metadata);
  },

  /**
* Now that we've read the request line and headers, we can actually hand off
* the request to be handled.
*
* This method is called once per request, after the request line and all
* headers and the body, if any, have been received.
*/
  _handleResponse: function()
  {
    NS_ASSERT(this._state == READER_FINISHED);

    // We don't need the line-based data any more, so make attempted reuse an
    // error.
    this._data = null;

    this._connection.process(this._metadata);
  },


  // PARSING

  /**
* Parses the request line for the HTTP request associated with this.
*
* @param line : string
* the request line
*/
  _parseRequestLine: function(line)
  {
    NS_ASSERT(this._state == READER_IN_REQUEST_LINE);

    dumpn("*** _parseRequestLine('" + line + "')");

    var metadata = this._metadata;

    // clients and servers SHOULD accept any amount of SP or HT characters
    // between fields, even though only a single SP is required (section 19.3)
    var request = line.split(/[ \t]+/);
    if (!request || request.length != 3)
      throw HTTP_400;

    metadata._method = request[0];

    // get the HTTP version
    var ver = request[2];
    var match = ver.match(/^HTTP\/(\d+\.\d+)$/);
    if (!match)
      throw HTTP_400;

    // determine HTTP version
    try
    {
      metadata._httpVersion = new nsHttpVersion(match[1]);
      if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_0))
        throw "unsupported HTTP version";
    }
    catch (e)
    {
      // we support HTTP/1.0 and HTTP/1.1 only
      throw HTTP_501;
    }


    var fullPath = request[1];
    var serverIdentity = this._connection.server.identity;

    var scheme, host, port;

    if (fullPath.charAt(0) != "/")
    {
      // No absolute paths in the request line in HTTP prior to 1.1
      if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
        throw HTTP_400;

      try
      {
        var uri = Cc["@mozilla.org/network/io-service;1"]
                    .getService(Ci.nsIIOService)
                    .newURI(fullPath, null, null);
        fullPath = uri.path;
        scheme = uri.scheme;
        host = metadata._host = uri.asciiHost;
        port = uri.port;
        if (port === -1)
        {
          if (scheme === "http")
            port = 80;
          else if (scheme === "https")
            port = 443;
          else
            throw HTTP_400;
        }
      }
      catch (e)
      {
        // If the host is not a valid host on the server, the response MUST be a
        // 400 (Bad Request) error message (section 5.2). Alternately, the URI
        // is malformed.
        throw HTTP_400;
      }

      if (!serverIdentity.has(scheme, host, port) || fullPath.charAt(0) != "/")
        throw HTTP_400;
    }

    var splitter = fullPath.indexOf("?");
    if (splitter < 0)
    {
      // _queryString already set in ctor
      metadata._path = fullPath;
    }
    else
    {
      metadata._path = fullPath.substring(0, splitter);
      metadata._queryString = fullPath.substring(splitter + 1);
    }

    metadata._scheme = scheme;
    metadata._host = host;
    metadata._port = port;
  },

  /**
* Parses all available HTTP headers in this until the header-ending CRLFCRLF,
* adding them to the store of headers in the request.
*
* @throws
* HTTP_400 if the headers are malformed
* @returns boolean
* true if all headers have now been processed, false otherwise
*/
  _parseHeaders: function()
  {
    NS_ASSERT(this._state == READER_IN_HEADERS);

    dumpn("*** _parseHeaders");

    var data = this._data;

    var headers = this._metadata._headers;
    var lastName = this._lastHeaderName;
    var lastVal = this._lastHeaderValue;

    var line = {};
    while (true)
    {
      NS_ASSERT(!((lastVal === undefined) ^ (lastName === undefined)),
                lastName === undefined ?
                  "lastVal without lastName? lastVal: '" + lastVal + "'" :
                  "lastName without lastVal? lastName: '" + lastName + "'");

      if (!data.readLine(line))
      {
        // save any data we have from the header we might still be processing
        this._lastHeaderName = lastName;
        this._lastHeaderValue = lastVal;
        return false;
      }

      var lineText = line.value;
      var firstChar = lineText.charAt(0);

      // blank line means end of headers
      if (lineText == "")
      {
        // we're finished with the previous header
        if (lastName)
        {
          try
          {
            headers.setHeader(lastName, lastVal, true);
          }
          catch (e)
          {
            dumpn("*** e == " + e);
            throw HTTP_400;
          }
        }
        else
        {
          // no headers in request -- valid for HTTP/1.0 requests
        }

        // either way, we're done processing headers
        this._state = READER_IN_BODY;
        return true;
      }
      else if (firstChar == " " || firstChar == "\t")
      {
        // multi-line header if we've already seen a header line
        if (!lastName)
        {
          // we don't have a header to continue!
          throw HTTP_400;
        }

        // append this line's text to the value; starts with SP/HT, so no need
        // for separating whitespace
        lastVal += lineText;
      }
      else
      {
        // we have a new header, so set the old one (if one existed)
        if (lastName)
        {
          try
          {
            headers.setHeader(lastName, lastVal, true);
          }
          catch (e)
          {
            dumpn("*** e == " + e);
            throw HTTP_400;
          }
        }

        var colon = lineText.indexOf(":"); // first colon must be splitter
        if (colon < 1)
        {
          // no colon or missing header field-name
          throw HTTP_400;
        }

        // set header name, value (to be set in the next loop, usually)
        lastName = lineText.substring(0, colon);
        lastVal = lineText.substring(colon + 1);
      } // empty, continuation, start of header
    } // while (true)
  }
};


/** The character codes for CR and LF. */
const CR = 0x0D, LF = 0x0A;

/**
* Calculates the number of characters before the first CRLF pair in array, or
* -1 if the array contains no CRLF pair.
*
* @param array : Array
* an array of numbers in the range [0, 256), each representing a single
* character; the first CRLF is the lowest index i where
* |array[i] == "\r".charCodeAt(0)| and |array[i+1] == "\n".charCodeAt(0)|,
* if such an |i| exists, and -1 otherwise
* @returns int
* the index of the first CRLF if any were present, -1 otherwise
*/
function findCRLF(array)
{
  for (var i = array.indexOf(CR); i >= 0; i = array.indexOf(CR, i + 1))
  {
    if (array[i + 1] == LF)
      return i;
  }
  return -1;
}


/**
* A container which provides line-by-line access to the arrays of bytes with
* which it is seeded.
*/
function LineData()
{
  /** An array of queued bytes from which to get line-based characters. */
  this._data = [];
}
LineData.prototype =
{
  /**
* Appends the bytes in the given array to the internal data cache maintained
* by this.
*/
  appendBytes: function(bytes)
  {
    Array.prototype.push.apply(this._data, bytes);
  },

  /**
* Removes and returns a line of data, delimited by CRLF, from this.
*
* @param out
* an object whose "value" property will be set to the first line of text
* present in this, sans CRLF, if this contains a full CRLF-delimited line
* of text; if this doesn't contain enough data, the value of the property
* is undefined
* @returns boolean
* true if a full line of data could be read from the data in this, false
* otherwise
*/
  readLine: function(out)
  {
    var data = this._data;
    var length = findCRLF(data);
    if (length < 0)
      return false;

    //
    // We have the index of the CR, so remove all the characters, including
    // CRLF, from the array with splice, and convert the removed array into the
    // corresponding string, from which we then strip the trailing CRLF.
    //
    // Getting the line in this matter acknowledges that substring is an O(1)
    // operation in SpiderMonkey because strings are immutable, whereas two
    // splices, both from the beginning of the data, are less likely to be as
    // cheap as a single splice plus two extra character conversions.
    //
    var line = String.fromCharCode.apply(null, data.splice(0, length + 2));
    out.value = line.substring(0, length);

    return true;
  },

  /**
* Removes the bytes currently within this and returns them in an array.
*
* @returns Array
* the bytes within this when this method is called
*/
  purge: function()
  {
    var data = this._data;
    this._data = [];
    return data;
  }
};



/**
* Creates a request-handling function for an nsIHttpRequestHandler object.
*/
function createHandlerFunc(handler)
{
  return function(metadata, response) { handler.handle(metadata, response); };
}


/**
* The default handler for directories; writes an HTML response containing a
* slightly-formatted directory listing.
*/
function defaultIndexHandler(metadata, response)
{
  response.setHeader("Content-Type", "text/html", false);

  var path = htmlEscape(decodeURI(metadata.path));

  //
  // Just do a very basic bit of directory listings -- no need for too much
  // fanciness, especially since we don't have a style sheet in which we can
  // stick rules (don't want to pollute the default path-space).
  //

  var body = '<html>\
<head>\
<title>' + path + '</title>\
</head>\
<body>\
<h1>' + path + '</h1>\
<ol style="list-style-type: none">';

  var directory = metadata.getProperty("directory").QueryInterface(Ci.nsILocalFile);
  NS_ASSERT(directory && directory.isDirectory());

  var fileList = [];
  var files = directory.directoryEntries;
  while (files.hasMoreElements())
  {
    var f = files.getNext().QueryInterface(Ci.nsIFile);
    var name = f.leafName;
    if (!f.isHidden() &&
        (name.charAt(name.length - 1) != HIDDEN_CHAR ||
         name.charAt(name.length - 2) == HIDDEN_CHAR))
      fileList.push(f);
  }

  fileList.sort(fileSort);

  for (var i = 0; i < fileList.length; i++)
  {
    var file = fileList[i];
    try
    {
      var name = file.leafName;
      if (name.charAt(name.length - 1) == HIDDEN_CHAR)
        name = name.substring(0, name.length - 1);
      var sep = file.isDirectory() ? "/" : "";

      // Note: using " to delimit the attribute here because encodeURIComponent
      // passes through '.
      var item = '<li><a href="' + encodeURIComponent(name) + sep + '">' +
                   htmlEscape(name) + sep +
                 '</a></li>';

      body += item;
    }
    catch (e) { /* some file system error, ignore the file */ }
  }

  body += ' </ol>\
</body>\
</html>';

  response.bodyOutputStream.write(body, body.length);
}

/**
* Sorts a and b (nsIFile objects) into an aesthetically pleasing order.
*/
function fileSort(a, b)
{
  var dira = a.isDirectory(), dirb = b.isDirectory();

  if (dira && !dirb)
    return -1;
  if (dirb && !dira)
    return 1;

  var namea = a.leafName.toLowerCase(), nameb = b.leafName.toLowerCase();
  return nameb > namea ? -1 : 1;
}


/**
* Converts an externally-provided path into an internal path for use in
* determining file mappings.
*
* @param path
* the path to convert
* @param encoded
* true if the given path should be passed through decodeURI prior to
* conversion
* @throws URIError
* if path is incorrectly encoded
*/
function toInternalPath(path, encoded)
{
  if (encoded)
    path = decodeURI(path);

  var comps = path.split("/");
  for (var i = 0, sz = comps.length; i < sz; i++)
  {
    var comp = comps[i];
    if (comp.charAt(comp.length - 1) == HIDDEN_CHAR)
      comps[i] = comp + HIDDEN_CHAR;
  }
  return comps.join("/");
}


/**
* Adds custom-specified headers for the given file to the given response, if
* any such headers are specified.
*
* @param file
* the file on the disk which is to be written
* @param metadata
* metadata about the incoming request
* @param response
* the Response to which any specified headers/data should be written
* @throws HTTP_500
* if an error occurred while processing custom-specified headers
*/
function maybeAddHeaders(file, metadata, response)
{
  var name = file.leafName;
  if (name.charAt(name.length - 1) == HIDDEN_CHAR)
    name = name.substring(0, name.length - 1);

  var headerFile = file.parent;
  headerFile.append(name + HEADERS_SUFFIX);

  if (!headerFile.exists())
    return;

  const PR_RDONLY = 0x01;
  var fis = new FileInputStream(headerFile, PR_RDONLY, 0o444,
                                Ci.nsIFileInputStream.CLOSE_ON_EOF);

  try
  {
    var lis = new ConverterInputStream(fis, "UTF-8", 1024, 0x0);
    lis.QueryInterface(Ci.nsIUnicharLineInputStream);

    var line = {value: ""};
    var more = lis.readLine(line);

    if (!more && line.value == "")
      return;


    // request line

    var status = line.value;
    if (status.indexOf("HTTP ") == 0)
    {
      status = status.substring(5);
      var space = status.indexOf(" ");
      var code, description;
      if (space < 0)
      {
        code = status;
        description = "";
      }
      else
      {
        code = status.substring(0, space);
        description = status.substring(space + 1, status.length);
      }

      response.setStatusLine(metadata.httpVersion, parseInt(code, 10), description);

      line.value = "";
      more = lis.readLine(line);
    }

    // headers
    while (more || line.value != "")
    {
      var header = line.value;
      var colon = header.indexOf(":");

      response.setHeader(header.substring(0, colon),
                         header.substring(colon + 1, header.length),
                         false); // allow overriding server-set headers

      line.value = "";
      more = lis.readLine(line);
    }
  }
  catch (e)
  {
    dumpn("WARNING: error in headers for " + metadata.path + ": " + e);
    throw HTTP_500;
  }
  finally
  {
    fis.close();
  }
}


/**
* An object which handles requests for a server, executing default and
* overridden behaviors as instructed by the code which uses and manipulates it.
* Default behavior includes the paths / and /trace (diagnostics), with some
* support for HTTP error pages for various codes and fallback to HTTP 500 if
* those codes fail for any reason.
*
* @param server : nsHttpServer
* the server in which this handler is being used
*/
function ServerHandler(server)
{
  // FIELDS

  /**
* The nsHttpServer instance associated with this handler.
*/
  this._server = server;

  /**
* A FileMap object containing the set of path->nsILocalFile mappings for
* all directory mappings set in the server (e.g., "/" for /var/www/html/,
* "/foo/bar/" for /local/path/, and "/foo/bar/baz/" for /local/path2).
*
* Note carefully: the leading and trailing "/" in each path (not file) are
* removed before insertion to simplify the code which uses this. You have
* been warned!
*/
  this._pathDirectoryMap = new FileMap();

  /**
* Custom request handlers for the server in which this resides. Path-handler
* pairs are stored as property-value pairs in this property.
*
* @see ServerHandler.prototype._defaultPaths
*/
  this._overridePaths = {};

  /**
* Custom request handlers for the server in which this resides. Prefix-handler
* pairs are stored as property-value pairs in this property.
*/
  this._overridePrefixes = {};

  /**
* Custom request handlers for the error handlers in the server in which this
* resides. Path-handler pairs are stored as property-value pairs in this
* property.
*
* @see ServerHandler.prototype._defaultErrors
*/
  this._overrideErrors = {};

  /**
* Maps file extensions to their MIME types in the server, overriding any
* mapping that might or might not exist in the MIME service.
*/
  this._mimeMappings = {};

  /**
* The default handler for requests for directories, used to serve directories
* when no index file is present.
*/
  this._indexHandler = defaultIndexHandler;

  /** Per-path state storage for the server. */
  this._state = {};

  /** Entire-server state storage. */
  this._sharedState = {};

  /** Entire-server state storage for nsISupports values. */
  this._objectState = {};
}
ServerHandler.prototype =
{
  // PUBLIC API

  /**
* Handles a request to this server, responding to the request appropriately
* and initiating server shutdown if necessary.
*
* This method never throws an exception.
*
* @param connection : Connection
* the connection for this request
*/
  handleResponse: function(connection)
  {
    var request = connection.request;
    var response = new Response(connection);

    var path = request.path;
    dumpn("*** path == " + path);

    try
    {
      try
      {
        if (path in this._overridePaths)
        {
          // explicit paths first, then files based on existing directory mappings,
          // then (if the file doesn't exist) built-in server default paths
          dumpn("calling override for " + path);
          this._overridePaths[path](request, response);
        }
        else
        {
          let longestPrefix = "";
          for (let prefix in this._overridePrefixes)
          {
            if (prefix.length > longestPrefix.length && path.startsWith(prefix))
            {
              longestPrefix = prefix;
            }
          }
          if (longestPrefix.length > 0)
          {
            dumpn("calling prefix override for " + longestPrefix);
            this._overridePrefixes[longestPrefix](request, response);
          }
          else
          {
            this._handleDefault(request, response);
          }
        }
      }
      catch (e)
      {
        if (response.partiallySent())
        {
          response.abort(e);
          return;
        }

        if (!(e instanceof HttpError))
        {
          dumpn("*** unexpected error: e == " + e);
          throw HTTP_500;
        }
        if (e.code !== 404)
          throw e;

        dumpn("*** default: " + (path in this._defaultPaths));

        response = new Response(connection);
        if (path in this._defaultPaths)
          this._defaultPaths[path](request, response);
        else
          throw HTTP_404;
      }
    }
    catch (e)
    {
      if (response.partiallySent())
      {
        response.abort(e);
        return;
      }

      var errorCode = "internal";

      try
      {
        if (!(e instanceof HttpError))
          throw e;

        errorCode = e.code;
        dumpn("*** errorCode == " + errorCode);

        response = new Response(connection);
        if (e.customErrorHandling)
          e.customErrorHandling(response);
        this._handleError(errorCode, request, response);
        return;
      }
      catch (e2)
      {
        dumpn("*** error handling " + errorCode + " error: " +
              "e2 == " + e2 + ", shutting down server");

        connection.server._requestQuit();
        response.abort(e2);
        return;
      }
    }

    response.complete();
  },

  //
  // see nsIHttpServer.registerFile
  //
  registerFile: function(path, file)
  {
    if (!file)
    {
      dumpn("*** unregistering '" + path + "' mapping");
      delete this._overridePaths[path];
      return;
    }

    dumpn("*** registering '" + path + "' as mapping to " + file.path);
    file = file.clone();

    var self = this;
    this._overridePaths[path] =
      function(request, response)
      {
        if (!file.exists())
          throw HTTP_404;

        response.setStatusLine(request.httpVersion, 200, "OK");
        self._writeFileResponse(request, file, response, 0, file.fileSize);
      };
  },

  //
  // see nsIHttpServer.registerPathHandler
  //
  registerPathHandler: function(path, handler)
  {
    // XXX true path validation!
    if (path.charAt(0) != "/")
      throw Cr.NS_ERROR_INVALID_ARG;

    this._handlerToField(handler, this._overridePaths, path);
  },

  //
  // see nsIHttpServer.registerPrefixHandler
  //
  registerPrefixHandler: function(prefix, handler)
  {
    // XXX true prefix validation!
    if (!(prefix.startsWith("/") && prefix.endsWith("/")))
      throw Cr.NS_ERROR_INVALID_ARG;

    this._handlerToField(handler, this._overridePrefixes, prefix);
  },

  //
  // see nsIHttpServer.registerDirectory
  //
  registerDirectory: function(path, directory)
  {
    // strip off leading and trailing '/' so that we can use lastIndexOf when
    // determining exactly how a path maps onto a mapped directory --
    // conditional is required here to deal with "/".substring(1, 0) being
    // converted to "/".substring(0, 1) per the JS specification
    var key = path.length == 1 ? "" : path.substring(1, path.length - 1);

    // the path-to-directory mapping code requires that the first character not
    // be "/", or it will go into an infinite loop
    if (key.charAt(0) == "/")
      throw Cr.NS_ERROR_INVALID_ARG;

    key = toInternalPath(key, false);

    if (directory)
    {
      dumpn("*** mapping '" + path + "' to the location " + directory.path);
      this._pathDirectoryMap.put(key, directory);
    }
    else
    {
      dumpn("*** removing mapping for '" + path + "'");
      this._pathDirectoryMap.put(key, null);
    }
  },

  //
  // see nsIHttpServer.registerErrorHandler
  //
  registerErrorHandler: function(err, handler)
  {
    if (!(err in HTTP_ERROR_CODES))
      dumpn("*** WARNING: registering non-HTTP/1.1 error code " +
            "(" + err + ") handler -- was this intentional?");

    this._handlerToField(handler, this._overrideErrors, err);
  },

  //
  // see nsIHttpServer.setIndexHandler
  //
  setIndexHandler: function(handler)
  {
    if (!handler)
      handler = defaultIndexHandler;
    else if (typeof(handler) != "function")
      handler = createHandlerFunc(handler);

    this._indexHandler = handler;
  },

  //
  // see nsIHttpServer.registerContentType
  //
  registerContentType: function(ext, type)
  {
    if (!type)
      delete this._mimeMappings[ext];
    else
      this._mimeMappings[ext] = headerUtils.normalizeFieldValue(type);
  },

  // PRIVATE API

  /**
* Sets or remove (if handler is null) a handler in an object with a key.
*
* @param handler
* a handler, either function or an nsIHttpRequestHandler
* @param dict
* The object to attach the handler to.
* @param key
* The field name of the handler.
*/
  _handlerToField: function(handler, dict, key)
  {
    // for convenience, handler can be a function if this is run from xpcshell
    if (typeof(handler) == "function")
      dict[key] = handler;
    else if (handler)
      dict[key] = createHandlerFunc(handler);
    else
      delete dict[key];
  },

  /**
* Handles a request which maps to a file in the local filesystem (if a base
* path has already been set; otherwise the 404 error is thrown).
*
* @param metadata : Request
* metadata for the incoming request
* @param response : Response
* an uninitialized Response to the given request, to be initialized by a
* request handler
* @throws HTTP_###
* if an HTTP error occurred (usually HTTP_404); note that in this case the
* calling code must handle post-processing of the response
*/
  _handleDefault: function(metadata, response)
  {
    dumpn("*** _handleDefault()");

    response.setStatusLine(metadata.httpVersion, 200, "OK");

    var path = metadata.path;
    NS_ASSERT(path.charAt(0) == "/", "invalid path: <" + path + ">");

    // determine the actual on-disk file; this requires finding the deepest
    // path-to-directory mapping in the requested URL
    var file = this._getFileForPath(path);

    // the "file" might be a directory, in which case we either serve the
    // contained index.html or make the index handler write the response
    if (file.exists() && file.isDirectory())
    {
      file.append("index.html"); // make configurable?
      if (!file.exists() || file.isDirectory())
      {
        metadata._ensurePropertyBag();
        metadata._bag.setPropertyAsInterface("directory", file.parent);
        this._indexHandler(metadata, response);
        return;
      }
    }

    // alternately, the file might not exist
    if (!file.exists())
      throw HTTP_404;

    var start, end;
    if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1) &&
        metadata.hasHeader("Range") &&
        this._getTypeFromFile(file) !== SJS_TYPE)
    {
      var rangeMatch = metadata.getHeader("Range").match(/^bytes=(\d+)?-(\d+)?$/);
      if (!rangeMatch)
        throw HTTP_400;

      if (rangeMatch[1] !== undefined)
        start = parseInt(rangeMatch[1], 10);

      if (rangeMatch[2] !== undefined)
        end = parseInt(rangeMatch[2], 10);

      if (start === undefined && end === undefined)
        throw HTTP_400;

      // No start given, so the end is really the count of bytes from the
      // end of the file.
      if (start === undefined)
      {
        start = Math.max(0, file.fileSize - end);
        end = file.fileSize - 1;
      }

      // start and end are inclusive
      if (end === undefined || end >= file.fileSize)
        end = file.fileSize - 1;

      if (start !== undefined && start >= file.fileSize) {
        var HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable");
        HTTP_416.customErrorHandling = function(errorResponse)
        {
          maybeAddHeaders(file, metadata, errorResponse);
        };
        throw HTTP_416;
      }

      if (end < start)
      {
        response.setStatusLine(metadata.httpVersion, 200, "OK");
        start = 0;
        end = file.fileSize - 1;
      }
      else
      {
        response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
        var contentRange = "bytes " + start + "-" + end + "/" + file.fileSize;
        response.setHeader("Content-Range", contentRange);
      }
    }
    else
    {
      start = 0;
      end = file.fileSize - 1;
    }

    // finally...
    dumpn("*** handling '" + path + "' as mapping to " + file.path + " from " +
          start + " to " + end + " inclusive");
    this._writeFileResponse(metadata, file, response, start, end - start + 1);
  },

  /**
* Writes an HTTP response for the given file, including setting headers for
* file metadata.
*
* @param metadata : Request
* the Request for which a response is being generated
* @param file : nsILocalFile
* the file which is to be sent in the response
* @param response : Response
* the response to which the file should be written
* @param offset: uint
* the byte offset to skip to when writing
* @param count: uint
* the number of bytes to write
*/
  _writeFileResponse: function(metadata, file, response, offset, count)
  {
    const PR_RDONLY = 0x01;

    var type = this._getTypeFromFile(file);
    if (type === SJS_TYPE)
    {
      var fis = new FileInputStream(file, PR_RDONLY, 0o444,
                                    Ci.nsIFileInputStream.CLOSE_ON_EOF);

      try
      {
        var sis = new ScriptableInputStream(fis);
        var s = Cu.Sandbox(gGlobalObject);
        s.importFunction(dump, "dump");

        // Define a basic key-value state-preservation API across requests, with
        // keys initially corresponding to the empty string.
        var self = this;
        var path = metadata.path;
        s.importFunction(function getState(k)
        {
          return self._getState(path, k);
        });
        s.importFunction(function setState(k, v)
        {
          self._setState(path, k, v);
        });
        s.importFunction(function getSharedState(k)
        {
          return self._getSharedState(k);
        });
        s.importFunction(function setSharedState(k, v)
        {
          self._setSharedState(k, v);
        });
        s.importFunction(function getObjectState(k, callback)
        {
          callback(self._getObjectState(k));
        });
        s.importFunction(function setObjectState(k, v)
        {
          self._setObjectState(k, v);
        });
        s.importFunction(function registerPathHandler(p, h)
        {
          self.registerPathHandler(p, h);
        });

        // Make it possible for sjs files to access their location
        this._setState(path, "__LOCATION__", file.path);

        try
        {
          // Alas, the line number in errors dumped to console when calling the
          // request handler is simply an offset from where we load the SJS file.
          // Work around this in a reasonably non-fragile way by dynamically
          // getting the line number where we evaluate the SJS file. Don't
          // separate these two lines!
          var line = new Error().lineNumber;
          Cu.evalInSandbox(sis.read(file.fileSize), s);
        }
        catch (e)
        {
          dumpn("*** syntax error in SJS at " + file.path + ": " + e);
          throw HTTP_500;
        }

        try
        {
          s.handleRequest(metadata, response);
        }
        catch (e)
        {
          dump("*** error running SJS at " + file.path + ": " +
               e + " on line " +
               (e instanceof Error
               ? e.lineNumber + " in httpd.js"
               : (e.lineNumber - line)) + "\n");
          throw HTTP_500;
        }
      }
      finally
      {
        fis.close();
      }
    }
    else
    {
      try
      {
        response.setHeader("Last-Modified",
                           toDateString(file.lastModifiedTime),
                           false);
      }
      catch (e) { /* lastModifiedTime threw, ignore */ }

      response.setHeader("Content-Type", type, false);
      maybeAddHeaders(file, metadata, response);
      response.setHeader("Content-Length", "" + count, false);

      var fis = new FileInputStream(file, PR_RDONLY, 0o444,
                                    Ci.nsIFileInputStream.CLOSE_ON_EOF);

      offset = offset || 0;
      count = count || file.fileSize;
      NS_ASSERT(offset === 0 || offset < file.fileSize, "bad offset");
      NS_ASSERT(count >= 0, "bad count");
      NS_ASSERT(offset + count <= file.fileSize, "bad total data size");

      try
      {
        if (offset !== 0)
        {
          // Seek (or read, if seeking isn't supported) to the correct offset so
          // the data sent to the client matches the requested range.
          if (fis instanceof Ci.nsISeekableStream)
            fis.seek(Ci.nsISeekableStream.NS_SEEK_SET, offset);
          else
            new ScriptableInputStream(fis).read(offset);
        }
      }
      catch (e)
      {
        fis.close();
        throw e;
      }

      let writeMore = function writeMore()
      {
        gThreadManager.currentThread
                      .dispatch(writeData, Ci.nsIThread.DISPATCH_NORMAL);
      }

      var input = new BinaryInputStream(fis);
      var output = new BinaryOutputStream(response.bodyOutputStream);
      var writeData =
        {
          run: function()
          {
            var chunkSize = Math.min(65536, count);
            count -= chunkSize;
            NS_ASSERT(count >= 0, "underflow");

            try
            {
              var data = input.readByteArray(chunkSize);
              NS_ASSERT(data.length === chunkSize,
                        "incorrect data returned? got " + data.length +
                        ", expected " + chunkSize);
              output.writeByteArray(data, data.length);
              if (count === 0)
              {
                fis.close();
                response.finish();
              }
              else
              {
                writeMore();
              }
            }
            catch (e)
            {
              try
              {
                fis.close();
              }
              finally
              {
                response.finish();
              }
              throw e;
            }
          }
        };

      writeMore();

      // Now that we know copying will start, flag the response as async.
      response.processAsync();
    }
  },

  /**
* Get the value corresponding to a given key for the given path for SJS state
* preservation across requests.
*
* @param path : string
* the path from which the given state is to be retrieved
* @param k : string
* the key whose corresponding value is to be returned
* @returns string
* the corresponding value, which is initially the empty string
*/
  _getState: function(path, k)
  {
    var state = this._state;
    if (path in state && k in state[path])
      return state[path][k];
    return "";
  },

  /**
* Set the value corresponding to a given key for the given path for SJS state
* preservation across requests.
*
* @param path : string
* the path from which the given state is to be retrieved
* @param k : string
* the key whose corresponding value is to be set
* @param v : string
* the value to be set
*/
  _setState: function(path, k, v)
  {
    if (typeof v !== "string")
      throw new Error("non-string value passed");
    var state = this._state;
    if (!(path in state))
      state[path] = {};
    state[path][k] = v;
  },

  /**
* Get the value corresponding to a given key for SJS state preservation
* across requests.
*
* @param k : string
* the key whose corresponding value is to be returned
* @returns string
* the corresponding value, which is initially the empty string
*/
  _getSharedState: function(k)
  {
    var state = this._sharedState;
    if (k in state)
      return state[k];
    return "";
  },

  /**
* Set the value corresponding to a given key for SJS state preservation
* across requests.
*
* @param k : string
* the key whose corresponding value is to be set
* @param v : string
* the value to be set
*/
  _setSharedState: function(k, v)
  {
    if (typeof v !== "string")
      throw new Error("non-string value passed");
    this._sharedState[k] = v;
  },

  /**
* Returns the object associated with the given key in the server for SJS
* state preservation across requests.
*
* @param k : string
* the key whose corresponding object is to be returned
* @returns nsISupports
* the corresponding object, or null if none was present
*/
  _getObjectState: function(k)
  {
    if (typeof k !== "string")
      throw new Error("non-string key passed");
    return this._objectState[k] || null;
  },

  /**
* Sets the object associated with the given key in the server for SJS
* state preservation across requests.
*
* @param k : string
* the key whose corresponding object is to be set
* @param v : nsISupports
* the object to be associated with the given key; may be null
*/
  _setObjectState: function(k, v)
  {
    if (typeof k !== "string")
      throw new Error("non-string key passed");
    if (typeof v !== "object")
      throw new Error("non-object value passed");
    if (v && !("QueryInterface" in v))
    {
      throw new Error("must pass an nsISupports; use wrappedJSObject to ease " +
                      "pain when using the server from JS");
    }

    this._objectState[k] = v;
  },

  /**
* Gets a content-type for the given file, first by checking for any custom
* MIME-types registered with this handler for the file's extension, second by
* asking the global MIME service for a content-type, and finally by failing
* over to application/octet-stream.
*
* @param file : nsIFile
* the nsIFile for which to get a file type
* @returns string
* the best content-type which can be determined for the file
*/
  _getTypeFromFile: function(file)
  {
    try
    {
      var name = file.leafName;
      var dot = name.lastIndexOf(".");
      if (dot > 0)
      {
        var ext = name.slice(dot + 1);
        if (ext in this._mimeMappings)
          return this._mimeMappings[ext];
      }
      return Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
               .getService(Ci.nsIMIMEService)
               .getTypeFromFile(file);
    }
    catch (e)
    {
      return "application/octet-stream";
    }
  },

  /**
* Returns the nsILocalFile which corresponds to the path, as determined using
* all registered path->directory mappings and any paths which are explicitly
* overridden.
*
* @param path : string
* the server path for which a file should be retrieved, e.g. "/foo/bar"
* @throws HttpError
* when the correct action is the corresponding HTTP error (i.e., because no
* mapping was found for a directory in path, the referenced file doesn't
* exist, etc.)
* @returns nsILocalFile
* the file to be sent as the response to a request for the path
*/
  _getFileForPath: function(path)
  {
    // decode and add underscores as necessary
    try
    {
      path = toInternalPath(path, true);
    }
    catch (e)
    {
      throw HTTP_400; // malformed path
    }

    // next, get the directory which contains this path
    var pathMap = this._pathDirectoryMap;

    // An example progression of tmp for a path "/foo/bar/baz/" might be:
    // "foo/bar/baz/", "foo/bar/baz", "foo/bar", "foo", ""
    var tmp = path.substring(1);
    while (true)
    {
      // do we have a match for current head of the path?
      var file = pathMap.get(tmp);
      if (file)
      {
        // XXX hack; basically disable showing mapping for /foo/bar/ when the
        // requested path was /foo/bar, because relative links on the page
        // will all be incorrect -- we really need the ability to easily
        // redirect here instead
        if (tmp == path.substring(1) &&
            tmp.length != 0 &&
            tmp.charAt(tmp.length - 1) != "/")
          file = null;
        else
          break;
      }

      // if we've finished trying all prefixes, exit
      if (tmp == "")
        break;

      tmp = tmp.substring(0, tmp.lastIndexOf("/"));
    }

    // no mapping applies, so 404
    if (!file)
      throw HTTP_404;


    // last, get the file for the path within the determined directory
    var parentFolder = file.parent;
    var dirIsRoot = (parentFolder == null);

    // Strategy here is to append components individually, making sure we
    // never move above the given directory; this allows paths such as
    // "<file>/foo/../bar" but prevents paths such as "<file>/../base-sibling";
    // this component-wise approach also means the code works even on platforms
    // which don't use "/" as the directory separator, such as Windows
    var leafPath = path.substring(tmp.length + 1);
    var comps = leafPath.split("/");
    for (var i = 0, sz = comps.length; i < sz; i++)
    {
      var comp = comps[i];

      if (comp == "..")
        file = file.parent;
      else if (comp == "." || comp == "")
        continue;
      else
        file.append(comp);

      if (!dirIsRoot && file.equals(parentFolder))
        throw HTTP_403;
    }

    return file;
  },

  /**
* Writes the error page for the given HTTP error code over the given
* connection.
*
* @param errorCode : uint
* the HTTP error code to be used
* @param connection : Connection
* the connection on which the error occurred
*/
  handleError: function(errorCode, connection)
  {
    var response = new Response(connection);

    dumpn("*** error in request: " + errorCode);

    this._handleError(errorCode, new Request(connection.port), response);
  },

  /**
* Handles a request which generates the given error code, using the
* user-defined error handler if one has been set, gracefully falling back to
* the x00 status code if the code has no handler, and failing to status code
* 500 if all else fails.
*
* @param errorCode : uint
* the HTTP error which is to be returned
* @param metadata : Request
* metadata for the request, which will often be incomplete since this is an
* error
* @param response : Response
* an uninitialized Response should be initialized when this method
* completes with information which represents the desired error code in the
* ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
* fallback for 505, per HTTP specs)
*/
  _handleError: function(errorCode, metadata, response)
  {
    if (!metadata)
      throw Cr.NS_ERROR_NULL_POINTER;

    var errorX00 = errorCode - (errorCode % 100);

    try
    {
      if (!(errorCode in HTTP_ERROR_CODES))
        dumpn("*** WARNING: requested invalid error: " + errorCode);

      // RFC 2616 says that we should try to handle an error by its class if we
      // can't otherwise handle it -- if that fails, we revert to handling it as
      // a 500 internal server error, and if that fails we throw and shut down
      // the server

      // actually handle the error
      try
      {
        if (errorCode in this._overrideErrors)
          this._overrideErrors[errorCode](metadata, response);
        else
          this._defaultErrors[errorCode](metadata, response);
      }
      catch (e)
      {
        if (response.partiallySent())
        {
          response.abort(e);
          return;
        }

        // don't retry the handler that threw
        if (errorX00 == errorCode)
          throw HTTP_500;

        dumpn("*** error in handling for error code " + errorCode + ", " +
              "falling back to " + errorX00 + "...");
        response = new Response(response._connection);
        if (errorX00 in this._overrideErrors)
          this._overrideErrors[errorX00](metadata, response);
        else if (errorX00 in this._defaultErrors)
          this._defaultErrors[errorX00](metadata, response);
        else
          throw HTTP_500;
      }
    }
    catch (e)
    {
      if (response.partiallySent())
      {
        response.abort();
        return;
      }

      // we've tried everything possible for a meaningful error -- now try 500
      dumpn("*** error in handling for error code " + errorX00 + ", falling " +
            "back to 500...");

      try
      {
        response = new Response(response._connection);
        if (500 in this._overrideErrors)
          this._overrideErrors[500](metadata, response);
        else
          this._defaultErrors[500](metadata, response);
      }
      catch (e2)
      {
        dumpn("*** multiple errors in default error handlers!");
        dumpn("*** e == " + e + ", e2 == " + e2);
        response.abort(e2);
        return;
      }
    }

    response.complete();
  },

  // FIELDS

  /**
* This object contains the default handlers for the various HTTP error codes.
*/
  _defaultErrors:
  {
    400: function(metadata, response)
    {
      // none of the data in metadata is reliable, so hard-code everything here
      response.setStatusLine("1.1", 400, "Bad Request");
      response.setHeader("Content-Type", "text/plain", false);

      var body = "Bad request\n";
      response.bodyOutputStream.write(body, body.length);
    },
    403: function(metadata, response)
    {
      response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
      response.setHeader("Content-Type", "text/html", false);

      var body = "<html>\
<head><title>403 Forbidden</title></head>\
<body>\
<h1>403 Forbidden</h1>\
</body>\
</html>";
      response.bodyOutputStream.write(body, body.length);
    },
    404: function(metadata, response)
    {
      response.setStatusLine(metadata.httpVersion, 404, "Not Found");
      response.setHeader("Content-Type", "text/html", false);

      var body = "<html>\
<head><title>404 Not Found</title></head>\
<body>\
<h1>404 Not Found</h1>\
<p>\
<span style='font-family: monospace;'>" +
                          htmlEscape(metadata.path) +
                       "</span> was not found.\
</p>\
</body>\
</html>";
      response.bodyOutputStream.write(body, body.length);
    },
    416: function(metadata, response)
    {
      response.setStatusLine(metadata.httpVersion,
                            416,
                            "Requested Range Not Satisfiable");
      response.setHeader("Content-Type", "text/html", false);

      var body = "<html>\
<head>\
<title>416 Requested Range Not Satisfiable</title></head>\
<body>\
<h1>416 Requested Range Not Satisfiable</h1>\
<p>The byte range was not valid for the\
requested resource.\
</p>\
</body>\
</html>";
      response.bodyOutputStream.write(body, body.length);
    },
    500: function(metadata, response)
    {
      response.setStatusLine(metadata.httpVersion,
                             500,
                             "Internal Server Error");
      response.setHeader("Content-Type", "text/html", false);

      var body = "<html>\
<head><title>500 Internal Server Error</title></head>\
<body>\
<h1>500 Internal Server Error</h1>\
<p>Something's broken in this server and\
needs to be fixed.</p>\
</body>\
</html>";
      response.bodyOutputStream.write(body, body.length);
    },
    501: function(metadata, response)
    {
      response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
      response.setHeader("Content-Type", "text/html", false);

      var body = "<html>\
<head><title>501 Not Implemented</title></head>\
<body>\
<h1>501 Not Implemented</h1>\
<p>This server is not (yet) Apache.</p>\
</body>\
</html>";
      response.bodyOutputStream.write(body, body.length);
    },
    505: function(metadata, response)
    {
      response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
      response.setHeader("Content-Type", "text/html", false);

      var body = "<html>\
<head><title>505 HTTP Version Not Supported</title></head>\
<body>\
<h1>505 HTTP Version Not Supported</h1>\
<p>This server only supports HTTP/1.0 and HTTP/1.1\
connections.</p>\
</body>\
</html>";
      response.bodyOutputStream.write(body, body.length);
    }
  },

  /**
* Contains handlers for the default set of URIs contained in this server.
*/
  _defaultPaths:
  {
    "/": function(metadata, response)
    {
      response.setStatusLine(metadata.httpVersion, 200, "OK");
      response.setHeader("Content-Type", "text/html", false);

      var body = "<html>\
<head><title>httpd.js</title></head>\
<body>\
<h1>httpd.js</h1>\
<p>If you're seeing this page, httpd.js is up and\
serving requests! Now set a base path and serve some\
files!</p>\
</body>\
</html>";

      response.bodyOutputStream.write(body, body.length);
    },

    "/trace": function(metadata, response)
    {
      response.setStatusLine(metadata.httpVersion, 200, "OK");
      response.setHeader("Content-Type", "text/plain", false);

      var body = "Request-URI: " +
                 metadata.scheme + "://" + metadata.host + ":" + metadata.port +
                 metadata.path + "\n\n";
      body += "Request (semantically equivalent, slightly reformatted):\n\n";
      body += metadata.method + " " + metadata.path;

      if (metadata.queryString)
        body += "?" + metadata.queryString;

      body += " HTTP/" + metadata.httpVersion + "\r\n";

      var headEnum = metadata.headers;
      while (headEnum.hasMoreElements())
      {
        var fieldName = headEnum.getNext()
                                .QueryInterface(Ci.nsISupportsString)
                                .data;
        body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
      }

      response.bodyOutputStream.write(body, body.length);
    }
  }
};


/**
* Maps absolute paths to files on the local file system (as nsILocalFiles).
*/
function FileMap()
{
  /** Hash which will map paths to nsILocalFiles. */
  this._map = {};
}
FileMap.prototype =
{
  // PUBLIC API

  /**
* Maps key to a clone of the nsILocalFile value if value is non-null;
* otherwise, removes any extant mapping for key.
*
* @param key : string
* string to which a clone of value is mapped
* @param value : nsILocalFile
* the file to map to key, or null to remove a mapping
*/
  put: function(key, value)
  {
    if (value)
      this._map[key] = value.clone();
    else
      delete this._map[key];
  },

  /**
* Returns a clone of the nsILocalFile mapped to key, or null if no such
* mapping exists.
*
* @param key : string
* key to which the returned file maps
* @returns nsILocalFile
* a clone of the mapped file, or null if no mapping exists
*/
  get: function(key)
  {
    var val = this._map[key];
    return val ? val.clone() : null;
  }
};


// Response CONSTANTS

// token = *<any CHAR except CTLs or separators>
// CHAR = <any US-ASCII character (0-127)>
// CTL = <any US-ASCII control character (0-31) and DEL (127)>
// separators = "(" | ")" | "<" | ">" | "@"
// | "," | ";" | ":" | "\" | <">
// | "/" | "[" | "]" | "?" | "="
// | "{" | "}" | SP | HT
const IS_TOKEN_ARRAY =
  [0, 0, 0, 0, 0, 0, 0, 0, // 0
   0, 0, 0, 0, 0, 0, 0, 0, // 8
   0, 0, 0, 0, 0, 0, 0, 0, // 16
   0, 0, 0, 0, 0, 0, 0, 0, // 24

   0, 1, 0, 1, 1, 1, 1, 1, // 32
   0, 0, 1, 1, 0, 1, 1, 0, // 40
   1, 1, 1, 1, 1, 1, 1, 1, // 48
   1, 1, 0, 0, 0, 0, 0, 0, // 56

   0, 1, 1, 1, 1, 1, 1, 1, // 64
   1, 1, 1, 1, 1, 1, 1, 1, // 72
   1, 1, 1, 1, 1, 1, 1, 1, // 80
   1, 1, 1, 0, 0, 0, 1, 1, // 88

   1, 1, 1, 1, 1, 1, 1, 1, // 96
   1, 1, 1, 1, 1, 1, 1, 1, // 104
   1, 1, 1, 1, 1, 1, 1, 1, // 112
   1, 1, 1, 0, 1, 0, 1]; // 120


/**
* Determines whether the given character code is a CTL.
*
* @param code : uint
* the character code
* @returns boolean
* true if code is a CTL, false otherwise
*/
function isCTL(code)
{
  return (code >= 0 && code <= 31) || (code == 127);
}

/**
* Represents a response to an HTTP request, encapsulating all details of that
* response. This includes all headers, the HTTP version, status code and
* explanation, and the entity itself.
*
* @param connection : Connection
* the connection over which this response is to be written
*/
function Response(connection)
{
  /** The connection over which this response will be written. */
  this._connection = connection;

  /**
* The HTTP version of this response; defaults to 1.1 if not set by the
* handler.
*/
  this._httpVersion = nsHttpVersion.HTTP_1_1;

  /**
* The HTTP code of this response; defaults to 200.
*/
  this._httpCode = 200;

  /**
* The description of the HTTP code in this response; defaults to "OK".
*/
  this._httpDescription = "OK";

  /**
* An nsIHttpHeaders object in which the headers in this response should be
* stored. This property is null after the status line and headers have been
* written to the network, and it may be modified up until it is cleared,
* except if this._finished is set first (in which case headers are written
* asynchronously in response to a finish() call not preceded by
* flushHeaders()).
*/
  this._headers = new nsHttpHeaders();

  /**
* Set to true when this response is ended (completely constructed if possible
* and the connection closed); further actions on this will then fail.
*/
  this._ended = false;

  /**
* A stream used to hold data written to the body of this response.
*/
  this._bodyOutputStream = null;

  /**
* A stream containing all data that has been written to the body of this
* response so far. (Async handlers make the data contained in this
* unreliable as a way of determining content length in general, but auxiliary
* saved information can sometimes be used to guarantee reliability.)
*/
  this._bodyInputStream = null;

  /**
* A stream copier which copies data to the network. It is initially null
* until replaced with a copier for response headers; when headers have been
* fully sent it is replaced with a copier for the response body, remaining
* so for the duration of response processing.
*/
  this._asyncCopier = null;

  /**
* True if this response has been designated as being processed
* asynchronously rather than for the duration of a single call to
* nsIHttpRequestHandler.handle.
*/
  this._processAsync = false;

  /**
* True iff finish() has been called on this, signaling that no more changes
* to this may be made.
*/
  this._finished = false;

  /**
* True iff powerSeized() has been called on this, signaling that this
* response is to be handled manually by the response handler (which may then
* send arbitrary data in response, even non-HTTP responses).
*/
  this._powerSeized = false;
}
Response.prototype =
{
  // PUBLIC CONSTRUCTION API

  //
  // see nsIHttpResponse.bodyOutputStream
  //
  get bodyOutputStream()
  {
    if (this._finished)
      throw Cr.NS_ERROR_NOT_AVAILABLE;

    if (!this._bodyOutputStream)
    {
      var pipe = new Pipe(true, false, Response.SEGMENT_SIZE, PR_UINT32_MAX,
                          null);
      this._bodyOutputStream = pipe.outputStream;
      this._bodyInputStream = pipe.inputStream;
      if (this._processAsync || this._powerSeized)
        this._startAsyncProcessor();
    }

    return this._bodyOutputStream;
  },

  //
  // see nsIHttpResponse.write
  //
  write: function(data)
  {
    if (this._finished)
      throw Cr.NS_ERROR_NOT_AVAILABLE;

    var dataAsString = String(data);
    this.bodyOutputStream.write(dataAsString, dataAsString.length);
  },

  //
  // see nsIHttpResponse.setStatusLine
  //
  setStatusLine: function(httpVersion, code, description)
  {
    if (!this._headers || this._finished || this._powerSeized)
      throw Cr.NS_ERROR_NOT_AVAILABLE;
    this._ensureAlive();

    if (!(code >= 0 && code < 1000))
      throw Cr.NS_ERROR_INVALID_ARG;

    try
    {
      var httpVer;
      // avoid version construction for the most common cases
      if (!httpVersion || httpVersion == "1.1")
        httpVer = nsHttpVersion.HTTP_1_1;
      else if (httpVersion == "1.0")
        httpVer = nsHttpVersion.HTTP_1_0;
      else
        httpVer = new nsHttpVersion(httpVersion);
    }
    catch (e)
    {
      throw Cr.NS_ERROR_INVALID_ARG;
    }

    // Reason-Phrase = *<TEXT, excluding CR, LF>
    // TEXT = <any OCTET except CTLs, but including LWS>
    //
    // XXX this ends up disallowing octets which aren't Unicode, I think -- not
    // much to do if description is IDL'd as string
    if (!description)
      description = "";
    for (var i = 0; i < description.length; i++)
      if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t")
        throw Cr.NS_ERROR_INVALID_ARG;

    // set the values only after validation to preserve atomicity
    this._httpDescription = description;
    this._httpCode = code;
    this._httpVersion = httpVer;
  },

  //
  // see nsIHttpResponse.setHeader
  //
  setHeader: function(name, value, merge)
  {
    if (!this._headers || this._finished || this._powerSeized)
      throw Cr.NS_ERROR_NOT_AVAILABLE;
    this._ensureAlive();

    this._headers.setHeader(name, value, merge);
  },

  //
  // see nsIHttpResponse.processAsync
  //
  processAsync: function()
  {
    if (this._finished)
      throw Cr.NS_ERROR_UNEXPECTED;
    if (this._powerSeized)
      throw Cr.NS_ERROR_NOT_AVAILABLE;
    if (this._processAsync)
      return;
    this._ensureAlive();

    dumpn("*** processing connection " + this._connection.number + " async");
    this._processAsync = true;

    /*
* Either the bodyOutputStream getter or this method is responsible for
* starting the asynchronous processor and catching writes of data to the
* response body of async responses as they happen, for the purpose of
* forwarding those writes to the actual connection's output stream.
* If bodyOutputStream is accessed first, calling this method will create
* the processor (when it first is clear that body data is to be written
* immediately, not buffered). If this method is called first, accessing
* bodyOutputStream will create the processor. If only this method is
* called, we'll write nothing, neither headers nor the nonexistent body,
* until finish() is called. Since that delay is easily avoided by simply
* getting bodyOutputStream or calling write(""), we don't worry about it.
*/
    if (this._bodyOutputStream && !this._asyncCopier)
      this._startAsyncProcessor();
  },

  //
  // see nsIHttpResponse.seizePower
  //
  seizePower: function()
  {
    if (this._processAsync)
      throw Cr.NS_ERROR_NOT_AVAILABLE;
    if (this._finished)
      throw Cr.NS_ERROR_UNEXPECTED;
    if (this._powerSeized)
      return;
    this._ensureAlive();

    dumpn("*** forcefully seizing power over connection " +
          this._connection.number + "...");

    // Purge any already-written data without sending it. We could as easily
    // swap out the streams entirely, but that makes it possible to acquire and
    // unknowingly use a stale reference, so we require there only be one of
    // each stream ever for any response to avoid this complication.
    if (this._asyncCopier)
      this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
    this._asyncCopier = null;
    if (this._bodyOutputStream)
    {
      var input = new BinaryInputStream(this._bodyInputStream);
      var avail;
      while ((avail = input.available()) > 0)
        input.readByteArray(avail);
    }

    this._powerSeized = true;
    if (this._bodyOutputStream)
      this._startAsyncProcessor();
  },

  //
  // see nsIHttpResponse.finish
  //
  finish: function()
  {
    if (!this._processAsync && !this._powerSeized)
      throw Cr.NS_ERROR_UNEXPECTED;
    if (this._finished)
      return;

    dumpn("*** finishing connection " + this._connection.number);
    this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
    if (this._bodyOutputStream)
      this._bodyOutputStream.close();
    this._finished = true;
  },


  // NSISUPPORTS

  //
  // see nsISupports.QueryInterface
  //
  QueryInterface: function(iid)
  {
    if (iid.equals(Ci.nsIHttpResponse) || iid.equals(Ci.nsISupports))
      return this;

    throw Cr.NS_ERROR_NO_INTERFACE;
  },


  // POST-CONSTRUCTION API (not exposed externally)

  /**
* The HTTP version number of this, as a string (e.g. "1.1").
*/
  get httpVersion()
  {
    this._ensureAlive();
    return this._httpVersion.toString();
  },

  /**
* The HTTP status code of this response, as a string of three characters per
* RFC 2616.
*/
  get httpCode()
  {
    this._ensureAlive();

    var codeString = (this._httpCode < 10 ? "0" : "") +
                     (this._httpCode < 100 ? "0" : "") +
                     this._httpCode;
    return codeString;
  },

  /**
* The description of the HTTP status code of this response, or "" if none is
* set.
*/
  get httpDescription()
  {
    this._ensureAlive();

    return this._httpDescription;
  },

  /**
* The headers in this response, as an nsHttpHeaders object.
*/
  get headers()
  {
    this._ensureAlive();

    return this._headers;
  },

  //
  // see nsHttpHeaders.getHeader
  //
  getHeader: function(name)
  {
    this._ensureAlive();

    return this._headers.getHeader(name);
  },

  /**
* Determines whether this response may be abandoned in favor of a newly
* constructed response. A response may be abandoned only if it is not being
* sent asynchronously and if raw control over it has not been taken from the
* server.
*
* @returns boolean
* true iff no data has been written to the network
*/
  partiallySent: function()
  {
    dumpn("*** partiallySent()");
    return this._processAsync || this._powerSeized;
  },

  /**
* If necessary, kicks off the remaining request processing needed to be done
* after a request handler performs its initial work upon this response.
*/
  complete: function()
  {
    dumpn("*** complete()");
    if (this._processAsync || this._powerSeized)
    {
      NS_ASSERT(this._processAsync ^ this._powerSeized,
                "can't both send async and relinquish power");
      return;
    }

    NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");

    this._startAsyncProcessor();

    // Now make sure we finish processing this request!
    if (this._bodyOutputStream)
      this._bodyOutputStream.close();
  },

  /**
* Abruptly ends processing of this response, usually due to an error in an
* incoming request but potentially due to a bad error handler. Since we
* cannot handle the error in the usual way (giving an HTTP error page in
* response) because data may already have been sent (or because the response
* might be expected to have been generated asynchronously or completely from
* scratch by the handler), we stop processing this response and abruptly
* close the connection.
*
* @param e : Error
* the exception which precipitated this abort, or null if no such exception
* was generated
*/
  abort: function(e)
  {
    dumpn("*** abort(<" + e + ">)");

    // This response will be ended by the processor if one was created.
    var copier = this._asyncCopier;
    if (copier)
    {
      // We dispatch asynchronously here so that any pending writes of data to
      // the connection will be deterministically written. This makes it easier
      // to specify exact behavior, and it makes observable behavior more
      // predictable for clients. Note that the correctness of this depends on
      // callbacks in response to _waitToReadData in WriteThroughCopier
      // happening asynchronously with respect to the actual writing of data to
      // bodyOutputStream, as they currently do; if they happened synchronously,
      // an event which ran before this one could write more data to the
      // response body before we get around to canceling the copier. We have
      // tests for this in test_seizepower.js, however, and I can't think of a
      // way to handle both cases without removing bodyOutputStream access and
      // moving its effective write(data, length) method onto Response, which
      // would be slower and require more code than this anyway.
      gThreadManager.currentThread.dispatch({
        run: function()
        {
          dumpn("*** canceling copy asynchronously...");
          copier.cancel(Cr.NS_ERROR_UNEXPECTED);
        }
      }, Ci.nsIThread.DISPATCH_NORMAL);
    }
    else
    {
      this.end();
    }
  },

  /**
* Closes this response's network connection, marks the response as finished,
* and notifies the server handler that the request is done being processed.
*/
  end: function()
  {
    NS_ASSERT(!this._ended, "ending this response twice?!?!");

    this._connection.close();
    if (this._bodyOutputStream)
      this._bodyOutputStream.close();

    this._finished = true;
    this._ended = true;
  },

  // PRIVATE IMPLEMENTATION

  /**
* Sends the status line and headers of this response if they haven't been
* sent and initiates the process of copying data written to this response's
* body to the network.
*/
  _startAsyncProcessor: function()
  {
    dumpn("*** _startAsyncProcessor()");

    // Handle cases where we're being called a second time. The former case
    // happens when this is triggered both by complete() and by processAsync(),
    // while the latter happens when processAsync() in conjunction with sent
    // data causes abort() to be called.
    if (this._asyncCopier || this._ended)
    {
      dumpn("*** ignoring second call to _startAsyncProcessor");
      return;
    }

    // Send headers if they haven't been sent already and should be sent, then
    // asynchronously continue to send the body.
    if (this._headers && !this._powerSeized)
    {
      this._sendHeaders();
      return;
    }

    this._headers = null;
    this._sendBody();
  },

  /**
* Signals that all modifications to the response status line and headers are
* complete and then sends that data over the network to the client. Once
* this method completes, a different response to the request that resulted
* in this response cannot be sent -- the only possible action in case of
* error is to abort the response and close the connection.
*/
  _sendHeaders: function()
  {
    dumpn("*** _sendHeaders()");

    NS_ASSERT(this._headers);
    NS_ASSERT(!this._powerSeized);

    // request-line
    var statusLine = "HTTP/" + this.httpVersion + " " +
                     this.httpCode + " " +
                     this.httpDescription + "\r\n";

    // header post-processing

    var headers = this._headers;
    headers.setHeader("Connection", "close", false);
    headers.setHeader("Server", "httpd.js", false);
    if (!headers.hasHeader("Date"))
      headers.setHeader("Date", toDateString(Date.now()), false);

    // Any response not being processed asynchronously must have an associated
    // Content-Length header for reasons of backwards compatibility with the
    // initial server, which fully buffered every response before sending it.
    // Beyond that, however, it's good to do this anyway because otherwise it's
    // impossible to test behaviors that depend on the presence or absence of a
    // Content-Length header.
    if (!this._processAsync)
    {
      dumpn("*** non-async response, set Content-Length");

      var bodyStream = this._bodyInputStream;
      var avail = bodyStream ? bodyStream.available() : 0;

      // XXX assumes stream will always report the full amount of data available
      headers.setHeader("Content-Length", "" + avail, false);
    }


    // construct and send response
    dumpn("*** header post-processing completed, sending response head...");

    // request-line
    var preambleData = [statusLine];

    // headers
    var headEnum = headers.enumerator;
    while (headEnum.hasMoreElements())
    {
      var fieldName = headEnum.getNext()
                              .QueryInterface(Ci.nsISupportsString)
                              .data;
      var values = headers.getHeaderValues(fieldName);
      for (var i = 0, sz = values.length; i < sz; i++)
        preambleData.push(fieldName + ": " + values[i] + "\r\n");
    }

    // end request-line/headers
    preambleData.push("\r\n");

    var preamble = preambleData.join("");

    var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
    responseHeadPipe.outputStream.write(preamble, preamble.length);

    var response = this;
    var copyObserver =
      {
        onStartRequest: function(request, cx)
        {
          dumpn("*** preamble copying started");
        },

        onStopRequest: function(request, cx, statusCode)
        {
          dumpn("*** preamble copying complete " +
                "[status=0x" + statusCode.toString(16) + "]");

          if (!components.isSuccessCode(statusCode))
          {
            dumpn("!!! header copying problems: non-success statusCode, " +
                  "ending response");

            response.end();
          }
          else
          {
            response._sendBody();
          }
        },

        QueryInterface: function(aIID)
        {
          if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
            return this;

          throw Cr.NS_ERROR_NO_INTERFACE;
        }
      };

    var headerCopier = this._asyncCopier =
      new WriteThroughCopier(responseHeadPipe.inputStream,
                             this._connection.output,
                             copyObserver, null);

    responseHeadPipe.outputStream.close();

    // Forbid setting any more headers or modifying the request line.
    this._headers = null;
  },

  /**
* Asynchronously writes the body of the response (or the entire response, if
* seizePower() has been called) to the network.
*/
  _sendBody: function()
  {
    dumpn("*** _sendBody");

    NS_ASSERT(!this._headers, "still have headers around but sending body?");

    // If no body data was written, we're done
    if (!this._bodyInputStream)
    {
      dumpn("*** empty body, response finished");
      this.end();
      return;
    }

    var response = this;
    var copyObserver =
      {
        onStartRequest: function(request, context)
        {
          dumpn("*** onStartRequest");
        },

        onStopRequest: function(request, cx, statusCode)
        {
          dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");

          if (statusCode === Cr.NS_BINDING_ABORTED)
          {
            dumpn("*** terminating copy observer without ending the response");
          }
          else
          {
            if (!components.isSuccessCode(statusCode))
              dumpn("*** WARNING: non-success statusCode in onStopRequest");

            response.end();
          }
        },

        QueryInterface: function(aIID)
        {
          if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
            return this;

          throw Cr.NS_ERROR_NO_INTERFACE;
        }
      };

    dumpn("*** starting async copier of body data...");
    this._asyncCopier =
      new WriteThroughCopier(this._bodyInputStream, this._connection.output,
                            copyObserver, null);
  },

  /** Ensures that this hasn't been ended. */
  _ensureAlive: function()
  {
    NS_ASSERT(!this._ended, "not handling response lifetime correctly");
  }
};

/**
* Size of the segments in the buffer used in storing response data and writing
* it to the socket.
*/
Response.SEGMENT_SIZE = 8192;

/** Serves double duty in WriteThroughCopier implementation. */
function notImplemented()
{
  throw Cr.NS_ERROR_NOT_IMPLEMENTED;
}

/** Returns true iff the given exception represents stream closure. */
function streamClosed(e)
{
  return e === Cr.NS_BASE_STREAM_CLOSED ||
         (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED);
}

/** Returns true iff the given exception represents a blocked stream. */
function wouldBlock(e)
{
  return e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
         (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK);
}

/**
* Copies data from source to sink as it becomes available, when that data can
* be written to sink without blocking.
*
* @param source : nsIAsyncInputStream
* the stream from which data is to be read
* @param sink : nsIAsyncOutputStream
* the stream to which data is to be copied
* @param observer : nsIRequestObserver
* an observer which will be notified when the copy starts and finishes
* @param context : nsISupports
* context passed to observer when notified of start/stop
* @throws NS_ERROR_NULL_POINTER
* if source, sink, or observer are null
*/
function WriteThroughCopier(source, sink, observer, context)
{
  if (!source || !sink || !observer)
    throw Cr.NS_ERROR_NULL_POINTER;

  /** Stream from which data is being read. */
  this._source = source;

  /** Stream to which data is being written. */
  this._sink = sink;

  /** Observer watching this copy. */
  this._observer = observer;

  /** Context for the observer watching this. */
  this._context = context;

  /**
* True iff this is currently being canceled (cancel has been called, the
* callback may not yet have been made).
*/
  this._canceled = false;

  /**
* False until all data has been read from input and written to output, at
* which point this copy is completed and cancel() is asynchronously called.
*/
  this._completed = false;

  /** Required by nsIRequest, meaningless. */
  this.loadFlags = 0;
  /** Required by nsIRequest, meaningless. */
  this.loadGroup = null;
  /** Required by nsIRequest, meaningless. */
  this.name = "response-body-copy";

  /** Status of this request. */
  this.status = Cr.NS_OK;

  /** Arrays of byte strings waiting to be written to output. */
  this._pendingData = [];

  // start copying
  try
  {
    observer.onStartRequest(this, context);
    this._waitToReadData();
    this._waitForSinkClosure();
  }
  catch (e)
  {
    dumpn("!!! error starting copy: " + e +
          ("lineNumber" in e ? ", line " + e.lineNumber : ""));
    dumpn(e.stack);
    this.cancel(Cr.NS_ERROR_UNEXPECTED);
  }
}
WriteThroughCopier.prototype =
{
  /* nsISupports implementation */

  QueryInterface: function(iid)
  {
    if (iid.equals(Ci.nsIInputStreamCallback) ||
        iid.equals(Ci.nsIOutputStreamCallback) ||
        iid.equals(Ci.nsIRequest) ||
        iid.equals(Ci.nsISupports))
    {
      return this;
    }

    throw Cr.NS_ERROR_NO_INTERFACE;
  },


  // NSIINPUTSTREAMCALLBACK

  /**
* Receives a more-data-in-input notification and writes the corresponding
* data to the output.
*
* @param input : nsIAsyncInputStream
* the input stream on whose data we have been waiting
*/
  onInputStreamReady: function(input)
  {
    if (this._source === null)
      return;

    dumpn("*** onInputStreamReady");

    //
    // Ordinarily we'll read a non-zero amount of data from input, queue it up
    // to be written and then wait for further callbacks. The complications in
    // this method are the cases where we deviate from that behavior when errors
    // occur or when copying is drawing to a finish.
    //
    // The edge cases when reading data are:
    //
    // Zero data is read
    // If zero data was read, we're at the end of available data, so we can
    // should stop reading and move on to writing out what we have (or, if
    // we've already done that, onto notifying of completion).
    // A stream-closed exception is thrown
    // This is effectively a less kind version of zero data being read; the
    // only difference is that we notify of completion with that result
    // rather than with NS_OK.
    // Some other exception is thrown
    // This is the least kind result. We don't know what happened, so we
    // act as though the stream closed except that we notify of completion
    // with the result NS_ERROR_UNEXPECTED.
    //

    var bytesWanted = 0, bytesConsumed = -1;
    try
    {
      input = new BinaryInputStream(input);

      bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
      dumpn("*** input wanted: " + bytesWanted);

      if (bytesWanted > 0)
      {
        var data = input.readByteArray(bytesWanted);
        bytesConsumed = data.length;
        this._pendingData.push(String.fromCharCode.apply(String, data));
      }

      dumpn("*** " + bytesConsumed + " bytes read");

      // Handle the zero-data edge case in the same place as all other edge
      // cases are handled.
      if (bytesWanted === 0)
        throw Cr.NS_BASE_STREAM_CLOSED;
    }
    catch (e)
    {
      if (streamClosed(e))
      {
        dumpn("*** input stream closed");
        e = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
      }
      else
      {
        dumpn("!!! unexpected error reading from input, canceling: " + e);
        e = Cr.NS_ERROR_UNEXPECTED;
      }

      this._doneReadingSource(e);
      return;
    }

    var pendingData = this._pendingData;

    NS_ASSERT(bytesConsumed > 0);
    NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
    NS_ASSERT(pendingData[pendingData.length - 1].length > 0,
              "buffered zero bytes of data?");

    NS_ASSERT(this._source !== null);

    // Reading has gone great, and we've gotten data to write now. What if we
    // don't have a place to write that data, because output went away just
    // before this read? Drop everything on the floor, including new data, and
    // cancel at this point.
    if (this._sink === null)
    {
      pendingData.length = 0;
      this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
      return;
    }

    // Okay, we've read the data, and we know we have a place to write it. We
    // need to queue up the data to be written, but *only* if none is queued
    // already -- if data's already queued, the code that actually writes the
    // data will make sure to wait on unconsumed pending data.
    try
    {
      if (pendingData.length === 1)
        this._waitToWriteData();
    }
    catch (e)
    {
      dumpn("!!! error waiting to write data just read, swallowing and " +
            "writing only what we already have: " + e);
      this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
      return;
    }

    // Whee! We successfully read some data, and it's successfully queued up to
    // be written. All that remains now is to wait for more data to read.
    try
    {
      this._waitToReadData();
    }
    catch (e)
    {
      dumpn("!!! error waiting to read more data: " + e);
      this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
    }
  },


  // NSIOUTPUTSTREAMCALLBACK

  /**
* Callback when data may be written to the output stream without blocking, or
* when the output stream has been closed.
*
* @param output : nsIAsyncOutputStream
* the output stream on whose writability we've been waiting, also known as
* this._sink
*/
  onOutputStreamReady: function(output)
  {
    if (this._sink === null)
      return;

    dumpn("*** onOutputStreamReady");

    var pendingData = this._pendingData;
    if (pendingData.length === 0)
    {
      // There's no pending data to write. The only way this can happen is if
      // we're waiting on the output stream's closure, so we can respond to a
      // copying failure as quickly as possible (rather than waiting for data to
      // be available to read and then fail to be copied). Therefore, we must
      // be done now -- don't bother to attempt to write anything and wrap
      // things up.
      dumpn("!!! output stream closed prematurely, ending copy");

      this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
      return;
    }


    NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");

    //
    // Write out the first pending quantum of data. The possible errors here
    // are:
    //
    // The write might fail because we can't write that much data
    // Okay, we've written what we can now, so re-queue what's left and
    // finish writing it out later.
    // The write failed because the stream was closed
    // Discard pending data that we can no longer write, stop reading, and
    // signal that copying finished.
    // Some other error occurred.
    // Same as if the stream were closed, but notify with the status
    // NS_ERROR_UNEXPECTED so the observer knows something was wonky.
    //

    try
    {
      var quantum = pendingData[0];

      // XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
      // undefined behavior! We're only using this because writeByteArray
      // is unusably broken for asynchronous output streams; see bug 532834
      // for details.
      var bytesWritten = output.write(quantum, quantum.length);
      if (bytesWritten === quantum.length)
        pendingData.shift();
      else
        pendingData[0] = quantum.substring(bytesWritten);

      dumpn("*** wrote " + bytesWritten + " bytes of data");
    }
    catch (e)
    {
      if (wouldBlock(e))
      {
        NS_ASSERT(pendingData.length > 0,
                  "stream-blocking exception with no data to write?");
        NS_ASSERT(pendingData[0].length > 0,
                  "stream-blocking exception with empty quantum?");
        this._waitToWriteData();
        return;
      }

      if (streamClosed(e))
        dumpn("!!! output stream prematurely closed, signaling error...");
      else
        dumpn("!!! unknown error: " + e + ", quantum=" + quantum);

      this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
      return;
    }

    // The day is ours! Quantum written, now let's see if we have more data
    // still to write.
    try
    {
      if (pendingData.length > 0)
      {
        this._waitToWriteData();
        return;
      }
    }
    catch (e)
    {
      dumpn("!!! unexpected error waiting to write pending data: " + e);
      this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
      return;
    }

    // Okay, we have no more pending data to write -- but might we get more in
    // the future?
    if (this._source !== null)
    {
      /*
* If we might, then wait for the output stream to be closed. (We wait
* only for closure because we have no data to write -- and if we waited
* for a specific amount of data, we would get repeatedly notified for no
* reason if over time the output stream permitted more and more data to
* be written to it without blocking.)
*/
       this._waitForSinkClosure();
    }
    else
    {
      /*
* On the other hand, if we can't have more data because the input
* stream's gone away, then it's time to notify of copy completion.
* Victory!
*/
      this._sink = null;
      this._cancelOrDispatchCancelCallback(Cr.NS_OK);
    }
  },


  // NSIREQUEST

  /** Returns true if the cancel observer hasn't been notified yet. */
  isPending: function()
  {
    return !this._completed;
  },

  /** Not implemented, don't use! */
  suspend: notImplemented,
  /** Not implemented, don't use! */
  resume: notImplemented,

  /**
* Cancels data reading from input, asynchronously writes out any pending
* data, and causes the observer to be notified with the given error code when
* all writing has finished.
*
* @param status : nsresult
* the status to pass to the observer when data copying has been canceled
*/
  cancel: function(status)
  {
    dumpn("*** cancel(" + status.toString(16) + ")");

    if (this._canceled)
    {
      dumpn("*** suppressing a late cancel");
      return;
    }

    this._canceled = true;
    this.status = status;

    // We could be in the middle of absolutely anything at this point. Both
    // input and output might still be around, we might have pending data to
    // write, and in general we know nothing about the state of the world. We
    // therefore must assume everything's in progress and take everything to its
    // final steady state (or so far as it can go before we need to finish
    // writing out remaining data).

    this._doneReadingSource(status);
  },


  // PRIVATE IMPLEMENTATION

  /**
* Stop reading input if we haven't already done so, passing e as the status
* when closing the stream, and kick off a copy-completion notice if no more
* data remains to be written.
*
* @param e : nsresult
* the status to be used when closing the input stream
*/
  _doneReadingSource: function(e)
  {
    dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");

    this._finishSource(e);
    if (this._pendingData.length === 0)
      this._sink = null;
    else
      NS_ASSERT(this._sink !== null, "null output?");

    // If we've written out all data read up to this point, then it's time to
    // signal completion.
    if (this._sink === null)
    {
      NS_ASSERT(this._pendingData.length === 0, "pending data still?");
      this._cancelOrDispatchCancelCallback(e);
    }
  },

  /**
* Stop writing output if we haven't already done so, discard any data that
* remained to be sent, close off input if it wasn't already closed, and kick
* off a copy-completion notice.
*
* @param e : nsresult
* the status to be used when closing input if it wasn't already closed
*/
  _doneWritingToSink: function(e)
  {
    dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");

    this._pendingData.length = 0;
    this._sink = null;
    this._doneReadingSource(e);
  },

  /**
* Completes processing of this copy: either by canceling the copy if it
* hasn't already been canceled using the provided status, or by dispatching
* the cancel callback event (with the originally provided status, of course)
* if it already has been canceled.
*
* @param status : nsresult
* the status code to use to cancel this, if this hasn't already been
* canceled
*/
  _cancelOrDispatchCancelCallback: function(status)
  {
    dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");

    NS_ASSERT(this._source === null, "should have finished input");
    NS_ASSERT(this._sink === null, "should have finished output");
    NS_ASSERT(this._pendingData.length === 0, "should have no pending data");

    if (!this._canceled)
    {
      this.cancel(status);
      return;
    }

    var self = this;
    var event =
      {
        run: function()
        {
          dumpn("*** onStopRequest async callback");

          self._completed = true;
          try
          {
            self._observer.onStopRequest(self, self._context, self.status);
          }
          catch (e)
          {
            NS_ASSERT(false,
                      "how are we throwing an exception here? we control " +
                      "all the callers! " + e);
          }
        }
      };

    gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
  },

  /**
* Kicks off another wait for more data to be available from the input stream.
*/
  _waitToReadData: function()
  {
    dumpn("*** _waitToReadData");
    this._source.asyncWait(this, 0, Response.SEGMENT_SIZE,
                           gThreadManager.mainThread);
  },

  /**
* Kicks off another wait until data can be written to the output stream.
*/
  _waitToWriteData: function()
  {
    dumpn("*** _waitToWriteData");

    var pendingData = this._pendingData;
    NS_ASSERT(pendingData.length > 0, "no pending data to write?");
    NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");

    this._sink.asyncWait(this, 0, pendingData[0].length,
                         gThreadManager.mainThread);
  },

  /**
* Kicks off a wait for the sink to which data is being copied to be closed.
* We wait for stream closure when we don't have any data to be copied, rather
* than waiting to write a specific amount of data. We can't wait to write
* data because the sink might be infinitely writable, and if no data appears
* in the source for a long time we might have to spin quite a bit waiting to
* write, waiting to write again, &c. Waiting on stream closure instead means
* we'll get just one notification if the sink dies. Note that when data
* starts arriving from the sink we'll resume waiting for data to be written,
* dropping this closure-only callback entirely.
*/
  _waitForSinkClosure: function()
  {
    dumpn("*** _waitForSinkClosure");

    this._sink.asyncWait(this, Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY, 0,
                         gThreadManager.mainThread);
  },

  /**
* Closes input with the given status, if it hasn't already been closed;
* otherwise a no-op.
*
* @param status : nsresult
* status code use to close the source stream if necessary
*/
  _finishSource: function(status)
  {
    dumpn("*** _finishSource(" + status.toString(16) + ")");

    if (this._source !== null)
    {
      this._source.closeWithStatus(status);
      this._source = null;
    }
  }
};


/**
* A container for utility functions used with HTTP headers.
*/
const headerUtils =
{
  /**
* Normalizes fieldName (by converting it to lowercase) and ensures it is a
* valid header field name (although not necessarily one specified in RFC
* 2616).
*
* @throws NS_ERROR_INVALID_ARG
* if fieldName does not match the field-name production in RFC 2616
* @returns string
* fieldName converted to lowercase if it is a valid header, for characters
* where case conversion is possible
*/
  normalizeFieldName: function(fieldName)
  {
    if (fieldName == "")
      throw Cr.NS_ERROR_INVALID_ARG;

    for (var i = 0, sz = fieldName.length; i < sz; i++)
    {
      if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)])
      {
        dumpn(fieldName + " is not a valid header field name!");
        throw Cr.NS_ERROR_INVALID_ARG;
      }
    }

    return fieldName.toLowerCase();
  },

  /**
* Ensures that fieldValue is a valid header field value (although not
* necessarily as specified in RFC 2616 if the corresponding field name is
* part of the HTTP protocol), normalizes the value if it is, and
* returns the normalized value.
*
* @param fieldValue : string
* a value to be normalized as an HTTP header field value
* @throws NS_ERROR_INVALID_ARG
* if fieldValue does not match the field-value production in RFC 2616
* @returns string
* fieldValue as a normalized HTTP header field value
*/
  normalizeFieldValue: function(fieldValue)
  {
    // field-value = *( field-content | LWS )
    // field-content = <the OCTETs making up the field-value
    // and consisting of either *TEXT or combinations
    // of token, separators, and quoted-string>
    // TEXT = <any OCTET except CTLs,
    // but including LWS>
    // LWS = [CRLF] 1*( SP | HT )
    //
    // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
    // qdtext = <any TEXT except <">>
    // quoted-pair = "\" CHAR
    // CHAR = <any US-ASCII character (octets 0 - 127)>

    // Any LWS that occurs between field-content MAY be replaced with a single
    // SP before interpreting the field value or forwarding the message
    // downstream (section 4.2); we replace 1*LWS with a single SP
    var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");

    // remove leading/trailing LWS (which has been converted to SP)
    val = val.replace(/^ +/, "").replace(/ +$/, "");

    // that should have taken care of all CTLs, so val should contain no CTLs
    for (var i = 0, len = val.length; i < len; i++)
      if (isCTL(val.charCodeAt(i)))
        throw Cr.NS_ERROR_INVALID_ARG;

    // XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
    // normalize, however, so this can be construed as a tightening of the
    // spec and not entirely as a bug
    return val;
  }
};



/**
* Converts the given string into a string which is safe for use in an HTML
* context.
*
* @param str : string
* the string to make HTML-safe
* @returns string
* an HTML-safe version of str
*/
function htmlEscape(str)
{
  // this is naive, but it'll work
  var s = "";
  for (var i = 0; i < str.length; i++)
    s += "&#" + str.charCodeAt(i) + ";";
  return s;
}


/**
* Constructs an object representing an HTTP version (see section 3.1).
*
* @param versionString
* a string of the form "#.#", where # is an non-negative decimal integer with
* or without leading zeros
* @throws
* if versionString does not specify a valid HTTP version number
*/
function nsHttpVersion(versionString)
{
  var matches = /^(\d+)\.(\d+)$/.exec(versionString);
  if (!matches)
    throw "Not a valid HTTP version!";

  /** The major version number of this, as a number. */
  this.major = parseInt(matches[1], 10);

  /** The minor version number of this, as a number. */
  this.minor = parseInt(matches[2], 10);

  if (isNaN(this.major) || isNaN(this.minor) ||
      this.major < 0 || this.minor < 0)
    throw "Not a valid HTTP version!";
}
nsHttpVersion.prototype =
{
  /**
* Returns the standard string representation of the HTTP version represented
* by this (e.g., "1.1").
*/
  toString: function ()
  {
    return this.major + "." + this.minor;
  },

  /**
* Returns true if this represents the same HTTP version as otherVersion,
* false otherwise.
*
* @param otherVersion : nsHttpVersion
* the version to compare against this
*/
  equals: function (otherVersion)
  {
    return this.major == otherVersion.major &&
           this.minor == otherVersion.minor;
  },

  /** True if this >= otherVersion, false otherwise. */
  atLeast: function(otherVersion)
  {
    return this.major > otherVersion.major ||
           (this.major == otherVersion.major &&
            this.minor >= otherVersion.minor);
  }
};

nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");


/**
* An object which stores HTTP headers for a request or response.
*
* Note that since headers are case-insensitive, this object converts headers to
* lowercase before storing them. This allows the getHeader and hasHeader
* methods to work correctly for any case of a header, but it means that the
* values returned by .enumerator may not be equal case-sensitively to the
* values passed to setHeader when adding headers to this.
*/
function nsHttpHeaders()
{
  /**
* A hash of headers, with header field names as the keys and header field
* values as the values. Header field names are case-insensitive, but upon
* insertion here they are converted to lowercase. Header field values are
* normalized upon insertion to contain no leading or trailing whitespace.
*
* Note also that per RFC 2616, section 4.2, two headers with the same name in
* a message may be treated as one header with the same field name and a field
* value consisting of the separate field values joined together with a "," in
* their original order. This hash stores multiple headers with the same name
* in this manner.
*/
  this._headers = {};
}
nsHttpHeaders.prototype =
{
  /**
* Sets the header represented by name and value in this.
*
* @param name : string
* the header name
* @param value : string
* the header value
* @throws NS_ERROR_INVALID_ARG
* if name or value is not a valid header component
*/
  setHeader: function(fieldName, fieldValue, merge)
  {
    var name = headerUtils.normalizeFieldName(fieldName);
    var value = headerUtils.normalizeFieldValue(fieldValue);

    // The following three headers are stored as arrays because their real-world
    // syntax prevents joining individual headers into a single header using
    // ",". See also <http://hg.mozilla.org/mozilla-central/diff/9b2a99adc05e/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#l77>
    if (merge && name in this._headers)
    {
      if (name === "www-authenticate" ||
          name === "proxy-authenticate" ||
          name === "set-cookie")
      {
        this._headers[name].push(value);
      }
      else
      {
        this._headers[name][0] += "," + value;
        NS_ASSERT(this._headers[name].length === 1,
            "how'd a non-special header have multiple values?")
      }
    }
    else
    {
      this._headers[name] = [value];
    }
  },

  /**
* Returns the value for the header specified by this.
*
* @throws NS_ERROR_INVALID_ARG
* if fieldName does not constitute a valid header field name
* @throws NS_ERROR_NOT_AVAILABLE
* if the given header does not exist in this
* @returns string
* the field value for the given header, possibly with non-semantic changes
* (i.e., leading/trailing whitespace stripped, whitespace runs replaced
* with spaces, etc.) at the option of the implementation; multiple
* instances of the header will be combined with a comma, except for
* the three headers noted in the description of getHeaderValues
*/
  getHeader: function(fieldName)
  {
    return this.getHeaderValues(fieldName).join("\n");
  },

  /**
* Returns the value for the header specified by fieldName as an array.
*
* @throws NS_ERROR_INVALID_ARG
* if fieldName does not constitute a valid header field name
* @throws NS_ERROR_NOT_AVAILABLE
* if the given header does not exist in this
* @returns [string]
* an array of all the header values in this for the given
* header name. Header values will generally be collapsed
* into a single header by joining all header values together
* with commas, but certain headers (Proxy-Authenticate,
* WWW-Authenticate, and Set-Cookie) violate the HTTP spec
* and cannot be collapsed in this manner. For these headers
* only, the returned array may contain multiple elements if
* that header has been added more than once.
*/
  getHeaderValues: function(fieldName)
  {
    var name = headerUtils.normalizeFieldName(fieldName);

    if (name in this._headers)
      return this._headers[name];
    else
      throw Cr.NS_ERROR_NOT_AVAILABLE;
  },

  /**
* Returns true if a header with the given field name exists in this, false
* otherwise.
*
* @param fieldName : string
* the field name whose existence is to be determined in this
* @throws NS_ERROR_INVALID_ARG
* if fieldName does not constitute a valid header field name
* @returns boolean
* true if the header's present, false otherwise
*/
  hasHeader: function(fieldName)
  {
    var name = headerUtils.normalizeFieldName(fieldName);
    return (name in this._headers);
  },

  /**
* Returns a new enumerator over the field names of the headers in this, as
* nsISupportsStrings. The names returned will be in lowercase, regardless of
* how they were input using setHeader (header names are case-insensitive per
* RFC 2616).
*/
  get enumerator()
  {
    var headers = [];
    for (var i in this._headers)
    {
      var supports = new SupportsString();
      supports.data = i;
      headers.push(supports);
    }

    return new nsSimpleEnumerator(headers);
  }
};


/**
* Constructs an nsISimpleEnumerator for the given array of items.
*
* @param items : Array
* the items, which must all implement nsISupports
*/
function nsSimpleEnumerator(items)
{
  this._items = items;
  this._nextIndex = 0;
}
nsSimpleEnumerator.prototype =
{
  hasMoreElements: function()
  {
    return this._nextIndex < this._items.length;
  },
  getNext: function()
  {
    if (!this.hasMoreElements())
      throw Cr.NS_ERROR_NOT_AVAILABLE;

    return this._items[this._nextIndex++];
  },
  QueryInterface: function(aIID)
  {
    if (Ci.nsISimpleEnumerator.equals(aIID) ||
        Ci.nsISupports.equals(aIID))
      return this;

    throw Cr.NS_ERROR_NO_INTERFACE;
  }
};


/**
* A representation of the data in an HTTP request.
*
* @param port : uint
* the port on which the server receiving this request runs
*/
function Request(port)
{
  /** Method of this request, e.g. GET or POST. */
  this._method = "";

  /** Path of the requested resource; empty paths are converted to '/'. */
  this._path = "";

  /** Query string, if any, associated with this request (not including '?'). */
  this._queryString = "";

  /** Scheme of requested resource, usually http, always lowercase. */
  this._scheme = "http";

  /** Hostname on which the requested resource resides. */
  this._host = undefined;

  /** Port number over which the request was received. */
  this._port = port;

  var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);

  /** Stream from which data in this request's body may be read. */
  this._bodyInputStream = bodyPipe.inputStream;

  /** Stream to which data in this request's body is written. */
  this._bodyOutputStream = bodyPipe.outputStream;

  /**
* The headers in this request.
*/
  this._headers = new nsHttpHeaders();

  /**
* For the addition of ad-hoc properties and new functionality without having
* to change nsIHttpRequest every time; currently lazily created, as its only
* use is in directory listings.
*/
  this._bag = null;
}
Request.prototype =
{
  // SERVER METADATA

  //
  // see nsIHttpRequest.scheme
  //
  get scheme()
  {
    return this._scheme;
  },

  //
  // see nsIHttpRequest.host
  //
  get host()
  {
    return this._host;
  },

  //
  // see nsIHttpRequest.port
  //
  get port()
  {
    return this._port;
  },

  // REQUEST LINE

  //
  // see nsIHttpRequest.method
  //
  get method()
  {
    return this._method;
  },

  //
  // see nsIHttpRequest.httpVersion
  //
  get httpVersion()
  {
    return this._httpVersion.toString();
  },

  //
  // see nsIHttpRequest.path
  //
  get path()
  {
    return this._path;
  },

  //
  // see nsIHttpRequest.queryString
  //
  get queryString()
  {
    return this._queryString;
  },

  // HEADERS

  //
  // see nsIHttpRequest.getHeader
  //
  getHeader: function(name)
  {
    return this._headers.getHeader(name);
  },

  //
  // see nsIHttpRequest.hasHeader
  //
  hasHeader: function(name)
  {
    return this._headers.hasHeader(name);
  },

  //
  // see nsIHttpRequest.headers
  //
  get headers()
  {
    return this._headers.enumerator;
  },

  //
  // see nsIPropertyBag.enumerator
  //
  get enumerator()
  {
    this._ensurePropertyBag();
    return this._bag.enumerator;
  },

  //
  // see nsIHttpRequest.headers
  //
  get bodyInputStream()
  {
    return this._bodyInputStream;
  },

  //
  // see nsIPropertyBag.getProperty
  //
  getProperty: function(name)
  {
    this._ensurePropertyBag();
    return this._bag.getProperty(name);
  },


  // NSISUPPORTS

  //
  // see nsISupports.QueryInterface
  //
  QueryInterface: function(iid)
  {
    if (iid.equals(Ci.nsIHttpRequest) || iid.equals(Ci.nsISupports))
      return this;

    throw Cr.NS_ERROR_NO_INTERFACE;
  },


  // PRIVATE IMPLEMENTATION

  /** Ensures a property bag has been created for ad-hoc behaviors. */
  _ensurePropertyBag: function()
  {
    if (!this._bag)
      this._bag = new WritablePropertyBag();
  }
};


// XPCOM trappings
if ("XPCOMUtils" in this && // Firefox 3.6 doesn't load XPCOMUtils in this scope for some reason...
    "generateNSGetFactory" in XPCOMUtils) {
  var NSGetFactory = XPCOMUtils.generateNSGetFactory([nsHttpServer]);
}



/**
* Creates a new HTTP server listening for loopback traffic on the given port,
* starts it, and runs the server until the server processes a shutdown request,
* spinning an event loop so that events posted by the server's socket are
* processed.
*
* This method is primarily intended for use in running this script from within
* xpcshell and running a functional HTTP server without having to deal with
* non-essential details.
*
* Note that running multiple servers using variants of this method probably
* doesn't work, simply due to how the internal event loop is spun and stopped.
*
* @note
* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
* you should use this server as a component in Mozilla 1.8.
* @param port
* the port on which the server will run, or -1 if there exists no preference
* for a specific port; note that attempting to use some values for this
* parameter (particularly those below 1024) may cause this method to throw or
* may result in the server being prematurely shut down
* @param basePath
* a local directory from which requests will be served (i.e., if this is
* "/home/jwalden/" then a request to /index.html will load
* /home/jwalden/index.html); if this is omitted, only the default URLs in
* this server implementation will be functional
*/
function server(port, basePath)
{
  if (basePath)
  {
    var lp = Cc["@mozilla.org/file/local;1"]
               .createInstance(Ci.nsILocalFile);
    lp.initWithPath(basePath);
  }

  // if you're running this, you probably want to see debugging info
  DEBUG = true;

  var srv = new nsHttpServer();
  if (lp)
    srv.registerDirectory("/", lp);
  srv.registerContentType("sjs", SJS_TYPE);
  srv.start(port);

  var thread = gThreadManager.currentThread;
  while (!srv.isStopped())
    thread.processNextEvent(true);

  // get rid of any pending requests
  while (thread.hasPendingEvents())
    thread.processNextEvent(true);

  DEBUG = false;
}

function startServerAsync(port, basePath)
{
  if (basePath)
  {
    var lp = Cc["@mozilla.org/file/local;1"]
               .createInstance(Ci.nsILocalFile);
    lp.initWithPath(basePath);
  }

  var srv = new nsHttpServer();
  if (lp)
    srv.registerDirectory("/", lp);
  srv.registerContentType("sjs", "sjs");
  srv.start(port);
  return srv;
}

exports.nsHttpServer = nsHttpServer;
exports.ScriptableInputStream = ScriptableInputStream;
exports.server = server;
exports.startServerAsync = startServerAsync;