File: http.h

package info (click to toggle)
pwlib 1.10.10-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,068 kB
  • ctags: 15,167
  • sloc: cpp: 112,149; ansic: 6,061; sh: 2,920; makefile: 1,062; yacc: 861; asm: 161
file content (1908 lines) | stat: -rw-r--r-- 66,014 bytes parent folder | download | duplicates (5)
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
/*
 * http.h
 *
 * HyperText Transport Protocol classes.
 *
 * Portable Windows Library
 *
 * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Portable Windows Library.
 *
 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
 *
 * Contributor(s): ______________________________________.
 *
 * $Log: http.h,v $
 * Revision 1.60  2005/11/30 12:47:37  csoutheren
 * Removed tabs, reformatted some code, and changed tags for Doxygen
 *
 * Revision 1.59  2005/01/03 12:48:41  csoutheren
 * Added new configure options and ability to disable/enable modules
 *
 * Revision 1.58  2004/10/23 11:34:59  ykiryanov
 * Added ifdef _WIN32_WCE for PocketPC 2003 SDK port
 *
 * Revision 1.57  2002/12/03 22:37:36  robertj
 * Removed get document that just returns a content length as the chunked
 *   transfer encoding makes this very dangerous.
 * Added GetTextDocument() to get a URL content into a PString.
 * Added a version pf PostData() that gets the reponse content into a PString.
 * Added ReadContentBody() that takes a PString, not just PBYTEArray.
 *
 * Revision 1.56  2002/11/06 22:47:23  robertj
 * Fixed header comment (copyright etc)
 *
 * Revision 1.55  2002/10/10 04:43:43  robertj
 * VxWorks port, thanks Martijn Roest
 *
 * Revision 1.54  2002/10/02 08:54:34  craigs
 * Added support for XMLRPC server
 *
 * Revision 1.53  2002/09/16 01:08:59  robertj
 * Added #define so can select if #pragma interface/implementation is used on
 *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
 *
 * Revision 1.52  2002/05/08 05:38:53  robertj
 * Added PHTTPTailFile resource to do a unix 'tail -f' of a file.
 *
 * Revision 1.51  2001/10/31 01:34:47  robertj
 * Added extra const for constant HTTP tag name strings.
 * Changes to support HTTP v1.1 chunked transfer encoding.
 *
 * Revision 1.50  2001/10/03 00:26:34  robertj
 * Upgraded client to HTTP/1.1 and for chunked mode entity bodies.
 *
 * Revision 1.49  2001/09/28 08:55:15  robertj
 * More changes to support restartable PHTTPClient
 *
 * Revision 1.48  2001/09/28 00:42:54  robertj
 * Added automatic setting of some outward MIME fields.
 * Added "user agent" string field for automatic inclusion.
 * Added function to read the contents of the HTTP request.
 *
 * Revision 1.47  2001/08/28 06:44:30  craigs
 * Added ability to override PHTTPServer creation
 *
 * Revision 1.46  2001/03/14 01:49:54  craigs
 * Added ability to handle multi-part form POST commands
 *
 * Revision 1.45  2001/02/22 05:26:47  robertj
 * Added "nicer" version of GetDocument in HTTP client class.
 *
 * Revision 1.44  2001/01/15 06:16:53  robertj
 * Set HTTP resource members to private to assure are not modified by
 *   dscendents in non-threadsafe manner.
 *
 * Revision 1.43  2000/09/04 03:57:58  robertj
 * Added ability to change the persistent connection parameters (timeout etc).
 *
 * Revision 1.42  2000/05/02 08:28:10  craigs
 * Removed "memory leaks" caused by brain-dead GNU linker
 *
 * Revision 1.41  1999/09/17 01:11:14  robertj
 * Fixed some documentation typos.
 *
 * Revision 1.40  1999/05/13 04:04:04  robertj
 * Fixed problem of initialised commandName in ConnectionInfo.
 *
 * Revision 1.39  1999/05/04 15:26:01  robertj
 * Improved HTTP/1.1 compatibility (pass through user commands).
 * Fixed problems with quicktime installer.
 *
 * Revision 1.38  1999/04/21 01:58:08  robertj
 * Fixed problem with reading data for request using second form of PHTTPRequestInfo constructor.
 *
 * Revision 1.37  1999/03/09 08:01:46  robertj
 * Changed comments for doc++ support (more to come).
 *
 * Revision 1.36  1999/02/16 08:07:10  robertj
 * MSVC 6.0 compatibility changes.
 *
 * Revision 1.35  1998/11/30 02:50:47  robertj
 * New directory structure
 *
 * Revision 1.34  1998/10/31 12:49:21  robertj
 * Added read/write mutex to the HTTP space variable to avoid thread crashes.
 *
 * Revision 1.33  1998/10/25 01:00:46  craigs
 * Added ability to specify per-directory authorisation for PHTTPDirectory
 *
 * Revision 1.32  1998/09/23 06:19:29  robertj
 * Added open source copyright license.
 *
 * Revision 1.31  1998/07/24 06:58:42  robertj
 * Changed PostData function so just has string for data instead of dictionary.
 *
 * Revision 1.30  1998/06/16 03:33:33  robertj
 * Changed TCP connection shutdown to be parameterised.
 * Propagated persistence and proxy flags in new connection info instances.
 *
 * Revision 1.29  1998/04/14 03:42:59  robertj
 * Fixed error code propagation in HTTP client.
 *
 * Revision 1.28  1998/02/03 06:29:38  robertj
 * Added local address and port to PHTTPRequest.
 *
 * Revision 1.27  1998/01/26 00:24:24  robertj
 * Added more information to PHTTPConnectionInfo.
 * Added function to allow HTTPClient to automatically connect if URL has hostname.
 *
 * Revision 1.26  1997/10/30 10:22:52  robertj
 * Added multiple user basic authorisation scheme.
 *
 * Revision 1.25  1997/10/03 13:30:15  craigs
 * Added ability to access client socket from within HTTP resources
 *
 * Revision 1.24  1997/03/28 04:40:22  robertj
 * Added tags for cookies.
 *
 * Revision 1.23  1997/01/12 04:15:19  robertj
 * Globalised MIME tag strings.
 *
 * Revision 1.22  1996/10/26 03:31:05  robertj
 * Changed OnError so can pass in full HTML page as parameter.
 *
 * Revision 1.21  1996/09/14 13:09:10  robertj
 * Major upgrade:
 *   rearranged sockets to help support IPX.
 *   added indirect channel class and moved all protocols to descend from it,
 *   separating the protocol from the low level byte transport.
 *
 * Revision 1.20  1996/08/22 13:20:55  robertj
 * Fixed bug in authorisation, missing virtual prevented polymorphism.
 *
 * Revision 1.19  1996/08/19 13:44:06  robertj
 * Fixed authorisation so if have no user/password on basic authentication, does not require it.
 *
 * Revision 1.18  1996/06/28 13:15:23  robertj
 * Modified HTTPAuthority so gets PHTTPReqest (mainly for URL) passed in.
 * Moved HTTP form resource to another compilation module.
 *
 * Revision 1.17  1996/06/07 13:52:20  robertj
 * Added PUT to HTTP proxy FTP. Necessitating redisign of entity body processing.
 *
 * Revision 1.16  1996/05/23 10:00:52  robertj
 * Added common function for GET and HEAD commands.
 * Fixed status codes to be the actual status code instead of sequential enum.
 * This fixed some problems with proxy pass through of status codes.
 *
 * Revision 1.14  1996/03/31 08:46:51  robertj
 * HTTP 1.1 upgrade.
 *
 * Revision 1.13  1996/03/17 05:41:57  robertj
 * Added hit count to PHTTPResource.
 *
 * Revision 1.12  1996/03/16 04:39:55  robertj
 * Added ParseReponse() for splitting reponse line into code and info.
 * Added client side support for HTTP socket.
 * Added hooks for proxy support in HTTP socket.
 *
 * Revision 1.11  1996/03/10 13:15:23  robertj
 * Redesign to make resources thread safe.
 *
 * Revision 1.10  1996/03/02 03:12:55  robertj
 * Added radio button and selection boxes to HTTP form resource.
 *
 * Revision 1.9  1996/02/25 11:14:21  robertj
 * Radio button support for forms.
 *
 * Revision 1.8  1996/02/25 02:57:48  robertj
 * Removed pass through HTTP resource.
 *
 * Revision 1.7  1996/02/19 13:25:43  robertj
 * Added overwrite option to AddResource().
 * Added get/set string to PHTTPString resource.
 * Moved nested classes from PHTTPForm.
 *
 * Revision 1.6  1996/02/13 13:09:16  robertj
 * Added extra parameters to callback function in PHTTPResources, required
 *   by descendants to make informed decisions on data being loaded.
 *
 * Revision 1.5  1996/02/08 12:04:19  robertj
 * Redesign of resource object callback virtuals.
 * Added HTML form resource type.
 *
 * Revision 1.4  1996/02/03 11:03:32  robertj
 * Added ismodified since and expires time checking.
 * Added PHTTPString that defaults to empty string.
 *
 * Revision 1.3  1996/01/28 14:15:38  robertj
 * Changed PCharArray in OnLoadData to PString for convenience in mangling data.
 * Beginning of pass through resource type.
 *
 * Revision 1.2  1996/01/26 02:24:26  robertj
 * Further implemetation.
 *
 * Revision 1.1  1996/01/23 13:04:20  robertj
 * Initial revision
 *
 */

#ifndef _PHTTP
#define _PHTTP

#ifdef P_USE_PRAGMA
#pragma interface
#endif

#include <ptclib/inetprot.h>
#include <ptclib/mime.h>
#include <ptclib/url.h>
#include <ptlib/ipsock.h>


#ifdef P_HTTPSVC

#include <ptclib/html.h>

//////////////////////////////////////////////////////////////////////////////
// PHTTPSpace

class PHTTPResource;

/** This class describes a name space that a Universal Resource Locator operates
   in. Each section of the hierarchy field of the URL points to a leg in the
   tree specified by this class.
 */
class PHTTPSpace : public PContainer
{
  PCONTAINERINFO(PHTTPSpace, PContainer)
  public:
    /// Constructor for HTTP URL Name Space
    PHTTPSpace();


  // New functions for class.
    enum AddOptions {
      /// Generate error if resource already exists
      ErrorOnExist,
      /// Overwrite the existing resource at URL location
      Overwrite
    };


    /** Add a new resource to the URL space. If there is already a resource at
       the location in the tree, or that location in the tree is already in
       the path to another resource then the function will fail.

       The <CODE>overwrite</CODE> flag can be used to replace an existing
       resource. The function will still fail if the resource is on a partial
       path to another resource but not if it is a leaf node.

       @return
       TRUE if resource added, FALSE if failed.
     */
    BOOL AddResource(
      PHTTPResource * resource, ///< Resource to add to the name space.
      AddOptions overwrite = ErrorOnExist
        ///< Flag to overwrite an existing resource if it already exists.
    );

    /** Delete an existing resource to the URL space. If there is not a resource
       at the location in the tree, or that location in the tree is in the
       path to another resource then the function will fail.

       @return
       TRUE if resource deleted, FALSE if failed.
     */
    BOOL DelResource(
      const PURL & url          ///< URL to search for in the name space.
    );

    /** Locate the resource specified by the URL in the URL name space.

       @return
       The resource found or NULL if no resource at that position in hiearchy.
     */
    PHTTPResource * FindResource(
      const PURL & url   ///< URL to search for in the name space.
    );

    /** This function attempts to acquire the mutex for reading.
     */
    void StartRead() const
      { mutex->StartRead(); }

    /** This function attempts to release the mutex for reading.
     */
    void EndRead() const
      { mutex->EndRead(); }

    /** This function attempts to acquire the mutex for writing.
     */
    void StartWrite() const
      { mutex->StartWrite(); }

    /** This function attempts to release the mutex for writing.
     */
    void EndWrite() const
      { mutex->EndWrite(); }


  protected:
    PReadWriteMutex * mutex;

    class Node;
    PSORTED_LIST(ChildList, Node);
    class Node : public PString
    {
      PCLASSINFO(Node, PString)
      public:
        Node(const PString & name, Node * parentNode);
        ~Node();

        Node          * parent;
        ChildList       children;
        PHTTPResource * resource;
    } * root;

  private:
    BOOL SetSize(PINDEX) { return FALSE; }
};

#endif // P_HTTPSVC

#ifdef _WIN32_WCE
#undef TRACE
#endif

//////////////////////////////////////////////////////////////////////////////
// PHTTP

/** A common base class for TCP/IP socket for the HyperText Transfer Protocol
version 1.0 client and server.
 */
class PHTTP : public PInternetProtocol
{
  PCLASSINFO(PHTTP, PInternetProtocol)

  public:
  // New functions for class.
    enum Commands {
      // HTTP/1.0 commands
      GET, HEAD, POST,
      // HTTP/1.1 commands
      PUT, DELETE, TRACE, OPTIONS,
      // HTTPS command
      CONNECT,
      NumCommands
    };

    enum StatusCode {
      Continue = 100,              ///< 100 - Continue
      SwitchingProtocols,          ///< 101 - upgrade allowed
      RequestOK = 200,             ///< 200 - request has succeeded
      Created,                     ///< 201 - new resource created: entity body contains URL
      Accepted,                    ///< 202 - request accepted, but not yet completed
      NonAuthoritativeInformation, ///< 203 - not definitive entity header
      NoContent,                   ///< 204 - no new information
      ResetContent,                ///< 205 - contents have been reset
      PartialContent,              ///< 206 - partial GET succeeded
      MultipleChoices = 300,       ///< 300 - requested resource available elsewehere 
      MovedPermanently,            ///< 301 - resource moved permanently: location field has new URL
      MovedTemporarily,            ///< 302 - resource moved temporarily: location field has new URL
      SeeOther,                    ///< 303 - see other URL
      NotModified,                 ///< 304 - document has not been modified
      UseProxy,                    ///< 305 - proxy redirect
      BadRequest = 400,            ///< 400 - request malformed or not understood
      UnAuthorised,                ///< 401 - request requires authentication
      PaymentRequired,             ///< 402 - reserved 
      Forbidden,                   ///< 403 - request is refused due to unsufficient authorisation
      NotFound,                    ///< 404 - resource cannot be found
      MethodNotAllowed,            ///< 405 - not allowed on this resource
      NoneAcceptable,              ///< 406 - encoding not acceptable
      ProxyAuthenticationRequired, ///< 407 - must authenticate with proxy first
      RequestTimeout,              ///< 408 - server timeout on request
      Conflict,                    ///< 409 - resource conflict on action
      Gone,                        ///< 410 - resource gone away
      LengthRequired,              ///< 411 - no Content-Length
      UnlessTrue,                  ///< 412 - no Range header for TRUE Unless
      InternalServerError = 500,   ///< 500 - server has encountered an unexpected error
      NotImplemented,              ///< 501 - server does not implement request
      BadGateway,                  ///< 502 - error whilst acting as gateway
      ServiceUnavailable,          ///< 503 - server temporarily unable to service request
      GatewayTimeout               ///< 504 - timeout whilst talking to gateway
    };

    // Common MIME header tags
    static const char * const AllowTag;
    static const char * const AuthorizationTag;
    static const char * const ContentEncodingTag;
    static const char * const ContentLengthTag;
    static const char * const ContentTypeTag;
    static const char * const DateTag;
    static const char * const ExpiresTag;
    static const char * const FromTag;
    static const char * const IfModifiedSinceTag;
    static const char * const LastModifiedTag;
    static const char * const LocationTag;
    static const char * const PragmaTag;
    static const char * const PragmaNoCacheTag;
    static const char * const RefererTag;
    static const char * const ServerTag;
    static const char * const UserAgentTag;
    static const char * const WWWAuthenticateTag;
    static const char * const MIMEVersionTag;
    static const char * const ConnectionTag;
    static const char * const KeepAliveTag;
    static const char * const TransferEncodingTag;
    static const char * const ChunkedTag;
    static const char * const ProxyConnectionTag;
    static const char * const ProxyAuthorizationTag;
    static const char * const ProxyAuthenticateTag;
    static const char * const ForwardedTag;
    static const char * const SetCookieTag;
    static const char * const CookieTag;

  protected:
    /** Create a TCP/IP HTTP protocol channel.
     */
    PHTTP();

    /** Parse a response line string into a response code and any extra info
       on the line. Results are placed into the member variables
       <CODE>lastResponseCode</CODE> and <CODE>lastResponseInfo</CODE>.

       The default bahaviour looks for a space or a '-' and splits the code
       and info either side of that character, then returns FALSE.

       @return
       Position of continuation character in response, 0 if no continuation
       lines are possible.
     */
    virtual PINDEX ParseResponse(
      const PString & line    ///< Input response line to be parsed
    );
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPClient

/** A TCP/IP socket for the HyperText Transfer Protocol version 1.0.

   When acting as a client, the procedure is to make the connection to a
   remote server, then to retrieve a document using the following procedure:
      <PRE><CODE>
      PHTTPSocket web("webserver");
      if (web.IsOpen()) {
        PINDEX len;
        if (web.GetDocument("http://www.someone.com/somewhere/url", len)) {
          PString html = web.ReadString(len);
          if (!html.IsEmpty())
            ProcessHTML(html);
        }
        else
           PError << "Could not get page." << endl;
      }
      else
         PError << "HTTP conection failed." << endl;
      </PRE></CODE>
 */
class PHTTPClient : public PHTTP
{
  PCLASSINFO(PHTTPClient, PHTTP)

  public:
    /// Create a new HTTP client channel.
    PHTTPClient();
    PHTTPClient(
      const PString & userAgentName
    );


  // New functions for class.
    /** Send a command and wait for the response header (including MIME fields).
       Note that a body may still be on its way even if lasResponseCode is not
       200!

       @return
       TRUE if all of header returned and ready to receive body.
     */
    int ExecuteCommand(
      Commands cmd,
      const PURL & url,
      PMIMEInfo & outMIME,
      const PString & dataBody,
      PMIMEInfo & replyMime,
      BOOL persist = TRUE
    );
    int ExecuteCommand(
      const PString & cmdName,
      const PURL & url,
      PMIMEInfo & outMIME,
      const PString & dataBody,
      PMIMEInfo & replyMime,
      BOOL persist = TRUE
    );

    /// Write a HTTP command to server
    BOOL WriteCommand(
      Commands cmd,
      const PString & url,
      PMIMEInfo & outMIME,
      const PString & dataBody
    );
    BOOL WriteCommand(
      const PString & cmdName,
      const PString & url,
      PMIMEInfo & outMIME,
      const PString & dataBody
    );

    /// Read a response from the server
    BOOL ReadResponse(
      PMIMEInfo & replyMIME
    );

    /// Read the body of the HTTP command
    BOOL ReadContentBody(
      PMIMEInfo & replyMIME,
      PBYTEArray & body
    );
    BOOL ReadContentBody(
      PMIMEInfo & replyMIME,
      PString & body
    );


    /** Get the document specified by the URL.

       @return
       TRUE if document is being transferred.
     */
    BOOL GetTextDocument(
      const PURL & url,         ///< Universal Resource Locator for document.
      PString & document,       ///< Body read
      BOOL persist = TRUE       ///< if TRUE, enable HTTP persistence
    );

    /** Get the document specified by the URL.

       @return
       TRUE if document is being transferred.
     */
    BOOL GetDocument(
      const PURL & url,         ///< Universal Resource Locator for document.
      PMIMEInfo & outMIME,      ///< MIME info in request
      PMIMEInfo & replyMIME,    ///< MIME info in response
      BOOL persist = TRUE       ///< if TRUE, enable HTTP persistence
    );

    /** Get the header for the document specified by the URL.

       @return
       TRUE if document header is being transferred.
     */
    BOOL GetHeader(
      const PURL & url,         ///< Universal Resource Locator for document.
      PMIMEInfo & outMIME,      ///< MIME info in request
      PMIMEInfo & replyMIME,    ///< MIME info in response
      BOOL persist = TRUE       ///< if TRUE, enable HTTP persistence
    );


    /** Post the data specified to the URL.

       @return
       TRUE if document is being transferred.
     */
    BOOL PostData(
      const PURL & url,       ///< Universal Resource Locator for document.
      PMIMEInfo & outMIME,    ///< MIME info in request
      const PString & data,   ///< Information posted to the HTTP server.
      PMIMEInfo & replyMIME,  ///< MIME info in response
      BOOL persist = TRUE     ///< if TRUE, enable HTTP persistence
    );

    /** Post the data specified to the URL.

       @return
       TRUE if document is being transferred.
     */
    BOOL PostData(
      const PURL & url,       ///< Universal Resource Locator for document.
      PMIMEInfo & outMIME,    ///< MIME info in request
      const PString & data,   ///< Information posted to the HTTP server.
      PMIMEInfo & replyMIME,  ///< MIME info in response
      PString & replyBody,    ///< Body of response
      BOOL persist = TRUE     ///< if TRUE, enable HTTP persistence
    );

  protected:
    BOOL AssureConnect(const PURL & url, PMIMEInfo & outMIME);
    BOOL InternalReadContentBody(
      PMIMEInfo & replyMIME,
      PAbstractArray & body
    );

    PString userAgentName;
};

#ifdef P_HTTPSVC

//////////////////////////////////////////////////////////////////////////////
// PMultipartFormInfo

/** This object describes the information associated with a multi-part
    form entry
  */

class PMultipartFormInfo : public PObject
{
  PCLASSINFO(PMultipartFormInfo, PObject);
  public:
    PMIMEInfo mime;
    PString body;
};

PARRAY(PMultipartFormInfoArray, PMultipartFormInfo);

//////////////////////////////////////////////////////////////////////////////
// PHTTPConnectionInfo

class PHTTPServer;

/** This object describes the connectiono associated with a HyperText Transport
   Protocol request. This information is required by handler functions on
   #PHTTPResource# descendant classes to manage the connection correctly.
*/
class PHTTPConnectionInfo : public PObject
{
  PCLASSINFO(PHTTPConnectionInfo, PObject)
  public:
    PHTTPConnectionInfo();

    PHTTP::Commands GetCommandCode() const { return commandCode; }
    const PString & GetCommandName() const { return commandName; }

    const PURL & GetURL() const       { return url; }

    const PMIMEInfo & GetMIME() const { return mimeInfo; }
    void SetMIME(const PString & tag, const PString & value);

    BOOL IsCompatible(int major, int minor) const;

    BOOL IsPersistant() const         { return isPersistant; }
    BOOL WasPersistant() const        { return wasPersistant; }
    BOOL IsProxyConnection() const    { return isProxyConnection; }
    int  GetMajorVersion() const      { return majorVersion; }
    int  GetMinorVersion() const      { return minorVersion; }

    long GetEntityBodyLength() const  { return entityBodyLength; }

    /**Get the maximum time a persistent connection may persist.
      */
    PTimeInterval GetPersistenceTimeout() const { return persistenceTimeout; }

    /**Set the maximum time a persistent connection may persist.
      */
    void SetPersistenceTimeout(const PTimeInterval & t) { persistenceTimeout = t; }

    /**Get the maximum number of transations (GET/POST etc) for persistent connection.
       If this is zero then there is no maximum.
      */
    unsigned GetPersistenceMaximumTransations() const { return persistenceMaximum; }

    /**Set the maximum number of transations (GET/POST etc) for persistent connection.
       If this is zero then there is no maximum.
      */
    void SetPersistenceMaximumTransations(unsigned m) { persistenceMaximum = m; }

    const PMultipartFormInfoArray & GetMultipartFormInfo() const
      { return multipartFormInfoArray; }

    void ResetMultipartFormInfo()
      { multipartFormInfoArray.RemoveAll(); }

    PString GetEntityBody() const   { return entityBody; }

  protected:
    BOOL Initialise(PHTTPServer & server, PString & args);
    void DecodeMultipartFormInfo(const PString & type, const PString & entityBody);

    PHTTP::Commands commandCode;
    PString         commandName;
    PURL            url;
    PMIMEInfo       mimeInfo;
    BOOL            isPersistant;
    BOOL            wasPersistant;
    BOOL            isProxyConnection;
    int             majorVersion;
    int             minorVersion;
    PString         entityBody;        // original entity body (POST only)
    long            entityBodyLength;
    PTimeInterval   persistenceTimeout;
    unsigned        persistenceMaximum;
    PMultipartFormInfoArray multipartFormInfoArray;

  friend class PHTTPServer;
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPServer

/** A TCP/IP socket for the HyperText Transfer Protocol version 1.0.

    When acting as a server, a descendant class would be created to override
    at least the #HandleOpenMailbox()#, #HandleSendMessage()# and
    #HandleDeleteMessage()# functions. Other functions may be overridden
    for further enhancement to the sockets capabilities, but these will give a
    basic POP3 server functionality.

    The server socket thread would continuously call the
    #ProcessMessage()# function until it returns FALSE. This will then
    call the appropriate virtual function on parsing the POP3 protocol.
 */
class PHTTPServer : public PHTTP
{
  PCLASSINFO(PHTTPServer, PHTTP)

  public:
    /** Create a TCP/IP HTTP protocol socket channel. The form with the single
       <CODE>port</CODE> parameter creates an unopened socket, the form with
       the <CODE>address</CODE> parameter makes a connection to a remote
       system, opening the socket. The form with the <CODE>socket</CODE>
       parameter opens the socket to an incoming call from a "listening"
       socket.
     */
    PHTTPServer();
    PHTTPServer(
     const PHTTPSpace & urlSpace  ///< Name space to use for URLs received.
    );


  // New functions for class.
    /** Get the name of the server.

       @return
       String name of the server.
     */
    virtual PString GetServerName() const;

    /** Get the name space being used by the HTTP server socket.

       @return
       URL name space tree.
     */
    PHTTPSpace & GetURLSpace() { return urlSpace; }

    /// Use a new URL name space for this HTTP socket.
    void SetURLSpace(
      const PHTTPSpace & space   ///< New URL name space to use.
    );


    /** Process commands, dispatching to the appropriate virtual function. This
       is used when the socket is acting as a server.

       @return
       TRUE if the request specified persistant mode and the request version
       allows it, FALSE if the socket closed, timed out, the protocol does not
       allow persistant mode, or the client did not request it
       timed out
     */
    virtual BOOL ProcessCommand();

    /** Handle a GET command from a client.

       The default implementation looks up the URL in the name space declared by
       the #PHTTPSpace# class tree and despatches to the
       #PHTTPResource# object contained therein.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
     */
    virtual BOOL OnGET(
      const PURL & url,                    ///< Universal Resource Locator for document.
      const PMIMEInfo & info,              ///< Extra MIME information in command.
      const PHTTPConnectionInfo & conInfo  ///< HTTP connection information
    );



    /** Handle a HEAD command from a client.

       The default implemetation looks up the URL in the name space declared by
       the #PHTTPSpace# class tree and despatches to the
       #PHTTPResource# object contained therein.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
     */
    virtual BOOL OnHEAD(
      const PURL & url,                   ///< Universal Resource Locator for document.
      const PMIMEInfo & info,             ///< Extra MIME information in command.
      const PHTTPConnectionInfo & conInfo ///< HTTP connection information
    );

    /** Handle a POST command from a client.

       The default implementation looks up the URL in the name space declared by
       the #PHTTPSpace# class tree and despatches to the
       #PHTTPResource# object contained therein.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
     */
    virtual BOOL OnPOST(
      const PURL & url,                   ///< Universal Resource Locator for document.
      const PMIMEInfo & info,             ///< Extra MIME information in command.
      const PStringToString & data,       ///< Variables provided in the POST data.
      const PHTTPConnectionInfo & conInfo ///< HTTP connection information
    );

    /** Handle a proxy command request from a client. This will only get called
       if the request was not for this particular server. If it was a proxy
       request for this server (host and port number) then the appropriate
       #OnGET()#, #OnHEAD()# or #OnPOST()# command is called.

       The default implementation returns OnError(BadGateway).

       @return
       TRUE if the connection may persist, FALSE if the connection must close
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
     */
    virtual BOOL OnProxy(
      const PHTTPConnectionInfo & conInfo   ///<  HTTP connection information
    );


    /** Read the entity body associated with a HTTP request, and close the
       socket if not a persistant connection.

       @return
       The entity body of the command
     */
    virtual PString ReadEntityBody();

    /** Handle an unknown command.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
     */
    virtual BOOL OnUnknown(
      const PCaselessString & command,         ///< Complete command line received.
      const PHTTPConnectionInfo & connectInfo  ///< HTTP connection information
    );

    /** Write a command reply back to the client, and construct some of the
       outgoing MIME fields. The MIME fields are not sent.

       The <CODE>bodySize</CODE> parameter determines the size of the 
       entity body associated with the response. If <CODE>bodySize</CODE> is
       >= 0, then a ContentLength field will be added to the outgoing MIME
       headers if one does not already exist.

       If <CODE>bodySize</CODE> is < 0, then it is assumed that the size of
       the entity body is unknown, or has already been added, and no
       ContentLength field will be constructed. 

       If the version of the request is less than 1.0, then this function does
       nothing.

       @return
       TRUE if requires v1.1 chunked transfer encoding.
     */
    BOOL StartResponse(
      StatusCode code,      ///< Status code for the response.
      PMIMEInfo & headers,  ///< MIME variables included in response.
      long bodySize         ///< Size of the rest of the response.
    );

    /** Write an error response for the specified code.

       Depending on the <CODE>code</CODE> parameter this function will also
       send a HTML version of the status code for display on the remote client
       viewer.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
     */
    virtual BOOL OnError(
      StatusCode code,                         ///< Status code for the error response.
      const PCaselessString & extra,           ///< Extra information included in the response.
      const PHTTPConnectionInfo & connectInfo  ///< HTTP connection information
    );

    /** Set the default mime info
     */
    void SetDefaultMIMEInfo(
      PMIMEInfo & info,      ///< Extra MIME information in command.
      const PHTTPConnectionInfo & connectInfo
    );

    /**Get the connection info for this connection.
      */
    PHTTPConnectionInfo & GetConnectionInfo() { return connectInfo; }

  protected:
    void Construct();

    PHTTPSpace          urlSpace;
    PHTTPConnectionInfo connectInfo;
    unsigned            transactionCount;
    PTimeInterval       nextTimeout;
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPRequest

/** This object describes a HyperText Transport Protocol request. An individual
   request is passed to handler functions on #PHTTPResource# descendant
   classes.
 */
class PHTTPRequest : public PObject
{
  PCLASSINFO(PHTTPRequest, PObject)

  public:
    PHTTPRequest(
      const PURL & url,             ///< Universal Resource Locator for document.
      const PMIMEInfo & inMIME,     ///< Extra MIME information in command.
      const PMultipartFormInfoArray & multipartFormInfo, ///< multipart form information (if any)
      PHTTPServer & server          ///< Server channel that request initiated on
    );

    PHTTPServer & server;           ///< Server channel that request initiated on
    const PURL & url;               ///< Universal Resource Locator for document.
    const PMIMEInfo & inMIME;       ///< Extra MIME information in command.
    const PMultipartFormInfoArray & multipartFormInfo; ///< multipart form information, if any
    PHTTP::StatusCode code;         ///< Status code for OnError() reply.
    PMIMEInfo outMIME;              ///< MIME information used in reply.
    PString entityBody;             ///< original entity body (POST only)
    PINDEX contentSize;             ///< Size of the body of the resource data.
    PIPSocket::Address origin;      ///< IP address of origin host for request
    PIPSocket::Address localAddr;   ///< IP address of local interface for request
    WORD               localPort;   ///< Port number of local server for request
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPAuthority

/** This abstract class describes the authorisation mechanism for a Universal
   Resource Locator.
 */
class PHTTPAuthority : public PObject
{
  PCLASSINFO(PHTTPAuthority, PObject)

  public:
  // New functions for class.
    /** Get the realm or name space for the user authorisation name and
       password as required by the basic authorisation system of HTTP/1.0.

       @return
       String for the authorisation realm name.
     */
    virtual PString GetRealm(
      const PHTTPRequest & request   ///< Request information.
    ) const = 0;

    /** Validate the user and password provided by the remote HTTP client for
       the realm specified by the class instance.

       @return
       TRUE if the user and password are authorised in the realm.
     */
    virtual BOOL Validate(
      const PHTTPRequest & request,  ///< Request information.
      const PString & authInfo       ///< Authority information string.
    ) const = 0;

    /** Determine if the authorisation is to be applied. This could be used to
       distinguish between net requiring authorisation and requiring autorisation
       but having no password.

       The default behaviour is to return TRUE.

       @return
       TRUE if the authorisation in the realm is to be applied.
     */
    virtual BOOL IsActive() const;

  protected:
    static void DecodeBasicAuthority(
      const PString & authInfo,   ///< Authority information string.
      PString & username,         ///< User name decoded from authInfo
      PString & password          ///< Password decoded from authInfo
    );
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPSimpleAuth

/** This class describes the simplest authorisation mechanism for a Universal
   Resource Locator, a fixed realm, username and password.
 */
class PHTTPSimpleAuth : public PHTTPAuthority
{
  PCLASSINFO(PHTTPSimpleAuth, PHTTPAuthority)

  public:
    PHTTPSimpleAuth(
      const PString & realm,      ///< Name space for the username and password.
      const PString & username,   ///< Username that this object wiull authorise.
      const PString & password    ///< Password for the above username.
    );
    // Construct the simple authorisation structure.


  // Overrides from class PObject.
    /** Create a copy of the class on the heap. This is used by the
       #PHTTPResource# classes for maintaining authorisation to
       resources.

       @return
       pointer to new copy of the class instance.
     */
    virtual PObject * Clone() const;


  // Overrides from class PHTTPAuthority.
    /** Get the realm or name space for the user authorisation name and
       password as required by the basic authorisation system of HTTP/1.0.

       @return
       String for the authorisation realm name.
     */
    virtual PString GetRealm(
      const PHTTPRequest & request   ///< Request information.
    ) const;

    /** Validate the user and password provided by the remote HTTP client for
       the realm specified by the class instance.

       @return
       TRUE if the user and password are authorised in the realm.
     */
    virtual BOOL Validate(
      const PHTTPRequest & request,  ///< Request information.
      const PString & authInfo       ///< Authority information string.
    ) const;

    /** Determine if the authorisation is to be applied. This could be used to
       distinguish between net requiring authorisation and requiring autorisation
       but having no password.

       The default behaviour is to return TRUE.

       @return
       TRUE if the authorisation in the realm is to be applied.
     */
    virtual BOOL IsActive() const;

    /** Get the user name allocated to this simple authorisation.

       @return
       String for the authorisation user name.
     */
    const PString & GetUserName() const { return username; }

    /** Get the password allocated to this simple authorisation.

       @return
       String for the authorisation password.
     */
    const PString & GetPassword() const { return password; }


  protected:
    PString realm;
    PString username;
    PString password;
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPMultiSimpAuth

/** This class describes the simple authorisation mechanism for a Universal
   Resource Locator, a fixed realm, multiple usernames and passwords.
 */
class PHTTPMultiSimpAuth : public PHTTPAuthority
{
  PCLASSINFO(PHTTPMultiSimpAuth, PHTTPAuthority)

  public:
    PHTTPMultiSimpAuth(
      const PString & realm      ///< Name space for the username and password.
    );
    PHTTPMultiSimpAuth(
      const PString & realm,           ///< Name space for the usernames.
      const PStringToString & userList ///< List of usernames and passwords.
    );
    // Construct the simple authorisation structure.


  // Overrides from class PObject.
    /** Create a copy of the class on the heap. This is used by the
       #PHTTPResource# classes for maintaining authorisation to
       resources.

       @return
       pointer to new copy of the class instance.
     */
    virtual PObject * Clone() const;


  // Overrides from class PHTTPAuthority.
    /** Get the realm or name space for the user authorisation name and
       password as required by the basic authorisation system of HTTP/1.0.

       @return
       String for the authorisation realm name.
     */
    virtual PString GetRealm(
      const PHTTPRequest & request   ///< Request information.
    ) const;

    /** Validate the user and password provided by the remote HTTP client for
       the realm specified by the class instance.

       @return
       TRUE if the user and password are authorised in the realm.
     */
    virtual BOOL Validate(
      const PHTTPRequest & request,  ///< Request information.
      const PString & authInfo       ///< Authority information string.
    ) const;

    /** Determine if the authirisation is to be applied. This could be used to
       distinguish between net requiring authorisation and requiring autorisation
       but having no password.

       The default behaviour is to return TRUE.

       @return
       TRUE if the authorisation in the realm is to be applied.
     */
    virtual BOOL IsActive() const;

    /** Get the user name allocated to this simple authorisation.

       @return
       String for the authorisation user name.
     */
    void AddUser(
      const PString & username,   ///< Username that this object wiull authorise.
      const PString & password    ///< Password for the above username.
    );


  protected:
    PString realm;
    PStringToString users;
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPResource

/** This object describes a HyperText Transport Protocol resource. A tree of
   these resources are available to the #PHTTPSocket# class.
 */
class PHTTPResource : public PObject
{
  PCLASSINFO(PHTTPResource, PObject)

  protected:
    PHTTPResource(
      const PURL & url               ///< Name of the resource in URL space.
    );
    PHTTPResource(
      const PURL & url,              ///< Name of the resource in URL space.
      const PHTTPAuthority & auth    ///< Authorisation for the resource.
    );
    PHTTPResource(
      const PURL & url,              ///< Name of the resource in URL space.
      const PString & contentType    ///< MIME content type for the resource.
    );
    PHTTPResource(
      const PURL & url,              ///< Name of the resource in URL space.
      const PString & contentType,   ///< MIME content type for the resource.
      const PHTTPAuthority & auth    ///< Authorisation for the resource.
    );
    // Create a new HTTP Resource.


  public:
    virtual ~PHTTPResource();
    // Destroy the HTTP Resource.


  // New functions for class.
    /** Get the URL for this resource.

       @return
       The URL for this resource.
     */
    const PURL & GetURL() const { return baseURL; }

    /** Get the current content type for the resource.

       @return
       string for the current MIME content type.
     */
    const PString & GetContentType() const { return contentType; }

    /** Get the current authority for the resource.

       @return
       Pointer to authority or NULL if unrestricted.
     */

    PHTTPAuthority * GetAuthority() const { return authority; }

    /** Set the current authority for the resource.
     */
    void SetAuthority(
      const PHTTPAuthority & auth      ///< authority to set
    );

    /** Set the current authority for the resource to unrestricted.
     */
    void ClearAuthority();

    /** Get the current hit count for the resource. This is the total number of
       times the resource was asked for by a remote client.

       @return
       Hit count for the resource.
     */
    DWORD GetHitCount() const { return hitCount; }

    void ClearHitCount() { hitCount = 0; }
    // Clear the hit count for the resource.


    /** Handle the GET command passed from the HTTP socket.

       The default action is to check the authorisation for the resource and
       call the virtuals #LoadHeaders()# and #OnGETData()# to get
       a resource to be sent to the socket.

       @return
       TRUE if the connection may persist, FALSE if the connection must close.
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
     */
    virtual BOOL OnGET(
      PHTTPServer & server,       ///< HTTP server that received the request
      const PURL & url,           ///< Universal Resource Locator for document.
      const PMIMEInfo & info,     ///< Extra MIME information in command.
      const PHTTPConnectionInfo & conInfo   ///< HTTP connection information
    );

    /**Send the data associated with a GET command.

       The default action calls #SendData()#.

       @return
       TRUE if the connection may persist, FALSE if the connection must close.
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
    */
    virtual BOOL OnGETData(
      PHTTPServer & server,                       ///< HTTP server that received the request
      const PURL & url,                           ///< Universal Resource Locator for document
      const PHTTPConnectionInfo & connectInfo,    ///< HTTP connection information
      PHTTPRequest & request                      ///< request state information
    );

    /** Handle the HEAD command passed from the HTTP socket.

       The default action is to check the authorisation for the resource and
       call the virtual #LoadHeaders()# to get the header information to
       be sent to the socket.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
     */
    virtual BOOL OnHEAD(
      PHTTPServer & server,       ///< HTTP server that received the request
      const PURL & url,           ///< Universal Resource Locator for document.
      const PMIMEInfo & info,     ///< Extra MIME information in command.
      const PHTTPConnectionInfo & conInfo  ///< HTTP connection information
    );

    /** Handle the POST command passed from the HTTP socket.

       The default action is to check the authorisation for the resource and
       call the virtual #Post()# function to handle the data being
       received.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
     */
    virtual BOOL OnPOST(
      PHTTPServer & server,         ///< HTTP server that received the request
      const PURL & url,             ///< Universal Resource Locator for document.
      const PMIMEInfo & info,       ///< Extra MIME information in command.
      const PStringToString & data, ///< Variables in the POST data.
      const PHTTPConnectionInfo & conInfo  ///< HTTP connection information
    );

    /**Send the data associated with a POST command.

       The default action calls #Post()#.

       @return
       TRUE if the connection may persist, FALSE if the connection must close.
       If there is no ContentLength field in the response, this value must
       be FALSE for correct operation.
    */
    virtual BOOL OnPOSTData(
      PHTTPRequest & request,        ///< request information
      const PStringToString & data   ///< Variables in the POST data.
    );

    /** Check to see if the resource has been modified since the date
       specified.

       @return
       TRUE if has been modified since.
     */
    virtual BOOL IsModifiedSince(
      const PTime & when    ///< Time to see if modified later than
    );

    /** Get a block of data (eg HTML) that the resource contains.

       @return
       Status of load operation.
     */
    virtual BOOL GetExpirationDate(
      PTime & when          ///< Time that the resource expires
    );

    /** Create a new request block for this type of resource.

       The default behaviour is to create a new PHTTPRequest instance.

       @return
       Pointer to instance of PHTTPRequest descendant class.
     */
    virtual PHTTPRequest * CreateRequest(
      const PURL & url,                   ///< Universal Resource Locator for document.
      const PMIMEInfo & inMIME,           ///< Extra MIME information in command.
      const PMultipartFormInfoArray & multipartFormInfo,  ///< additional information for multi-part posts
      PHTTPServer & socket                                ///< socket used for request
    );

    /** Get the headers for block of data (eg HTML) that the resource contains.
       This will fill in all the fields of the <CODE>outMIME</CODE> parameter
       required by the resource and return the status for the load.

       @return
       TRUE if all OK, FALSE if an error occurred.
     */
    virtual BOOL LoadHeaders(
      PHTTPRequest & request    ///<  Information on this request.
    ) = 0;

    /**Send the data associated with a command.

       The default action is to call the virtual #LoadData()# to get a
       resource to be sent to the socket.
    */
    virtual void SendData(
      PHTTPRequest & request    ///< information for this request
    );

    /** Get a block of data that the resource contains.

       The default behaviour is to call the #LoadText()# function and
       if successful, call the #OnLoadedText()# function.

       @return
       TRUE if there is still more to load.
     */
    virtual BOOL LoadData(
      PHTTPRequest & request,    ///<  Information on this request.
      PCharArray & data          ///<  Data used in reply.
    );

    /** Get a block of text data (eg HTML) that the resource contains.

       The default behaviour is to assert, one of #LoadText()# or
       #LoadData()# functions must be overridden for correct operation.

       @return
       String for loaded text.
     */
    virtual PString LoadText(
      PHTTPRequest & request    ///< Information on this request.
    );

    /** This is called after the text has been loaded and may be used to
       customise or otherwise mangle a loaded piece of text. Typically this is
       used with HTML responses.

       The default action for this function is to do nothing.
     */
    virtual void OnLoadedText(
      PHTTPRequest & request,    ///<  Information on this request.
      PString & text             ///<  Data used in reply.
    );

    /** Get a block of data (eg HTML) that the resource contains.

       The default action for this function is to do nothing and return
       success.

       @return
       TRUE if the connection may persist, FALSE if the connection must close
     */
    virtual BOOL Post(
      PHTTPRequest & request,       ///<  Information on this request.
      const PStringToString & data, ///<  Variables in the POST data.
      PHTML & replyMessage          ///<  Reply message for post.
    );


  protected:
    /** See if the resource is authorised given the mime info
     */
    virtual BOOL CheckAuthority(
      PHTTPServer & server,               ///<  Server to send response to.
      const PHTTPRequest & request,       ///<  Information on this request.
      const PHTTPConnectionInfo & conInfo ///<  Information on the connection
    );
    static BOOL CheckAuthority(
                   PHTTPAuthority & authority,
                      PHTTPServer & server,
               const PHTTPRequest & request,
        const PHTTPConnectionInfo & connectInfo
    );


    /** common code for GET and HEAD commands */
    virtual BOOL OnGETOrHEAD(
      PHTTPServer & server,       ///<  HTTP server that received the request
      const PURL & url,           ///<  Universal Resource Locator for document.
      const PMIMEInfo & info,     ///<  Extra MIME information in command.
      const PHTTPConnectionInfo & conInfo,
      BOOL  IsGet
    );

    /// Base URL for the resource, may accept URLS with a longer hierarchy
    PURL             baseURL;
    /// MIME content type for the resource
    PString          contentType;
    /// Authorisation method for the resource
    PHTTPAuthority * authority;
    /// COunt of number of times resource was accessed.
    volatile DWORD   hitCount;
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPString

/** This object describes a HyperText Transport Protocol resource which is a
   string kept in memory. For instance a pre-calculated HTML string could be
   set in this type of resource.
 */
class PHTTPString : public PHTTPResource
{
  PCLASSINFO(PHTTPString, PHTTPResource)

  public:
    /** Contruct a new simple string resource for the HTTP space. If no MIME
       content type is specified then a default type is "text/html".
     */
    PHTTPString(
      const PURL & url             // Name of the resource in URL space.
    );
    PHTTPString(
      const PURL & url,            // Name of the resource in URL space.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );
    PHTTPString(
      const PURL & url,            // Name of the resource in URL space.
      const PString & str          // String to return in this resource.
    );
    PHTTPString(
      const PURL & url,            // Name of the resource in URL space.
      const PString & str,         // String to return in this resource.
      const PString & contentType  // MIME content type for the file.
    );
    PHTTPString(
      const PURL & url,            // Name of the resource in URL space.
      const PString & str,         // String to return in this resource.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );
    PHTTPString(
      const PURL & url,            // Name of the resource in URL space.
      const PString & str,         // String to return in this resource.
      const PString & contentType, // MIME content type for the file.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );


  // Overrides from class PHTTPResource
    /** Get the headers for block of data (eg HTML) that the resource contains.
       This will fill in all the fields of the <CODE>outMIME</CODE> parameter
       required by the resource and return the status for the load.

       @return
       TRUE if all OK, FALSE if an error occurred.
     */
    virtual BOOL LoadHeaders(
      PHTTPRequest & request    // Information on this request.
    );

    /** Get a block of text data (eg HTML) that the resource contains.

       The default behaviour is to assert, one of #LoadText()# or
       #LoadData()# functions must be overridden for correct operation.

       @return
       String for loaded text.
     */
    virtual PString LoadText(
      PHTTPRequest & request    // Information on this request.
    );

  // New functions for class.
    /** Get the string for this resource.

       @return
       String for resource.
     */
    const PString & GetString() { return string; }

    /** Set the string to be returned by this resource.
     */
    void SetString(
      const PString & str   // New string for the resource.
    ) { string = str; }


  protected:
    PString string;
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPFile

/** This object describes a HyperText Transport Protocol resource which is a
   single file. The file can be anywhere in the file system and is mapped to
   the specified URL location in the HTTP name space defined by the
   #PHTTPSpace# class.
 */
class PHTTPFile : public PHTTPResource
{
  PCLASSINFO(PHTTPFile, PHTTPResource)

  public:
    /** Contruct a new simple file resource for the HTTP space. If no MIME
       content type is specified then a default type is used depending on the
       file type. For example, "text/html" is used of the file type is
       ".html" or ".htm". The default for an unknown type is
       "application/octet-stream".
     */
    PHTTPFile(
      const PString & filename     // file in file system and URL name.
    );
    PHTTPFile(
      const PString & filename,    // file in file system and URL name.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );
    PHTTPFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file       // Location of file in file system.
    );
    PHTTPFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file,      // Location of file in file system.
      const PString & contentType  // MIME content type for the file.
    );
    PHTTPFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file,      // Location of file in file system.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );
    PHTTPFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file,      // Location of file in file system.
      const PString & contentType, // MIME content type for the file.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );


  // Overrides from class PHTTPResource
    /** Create a new request block for this type of resource.

       @return
       Pointer to instance of PHTTPRequest descendant class.
     */
    virtual PHTTPRequest * CreateRequest(
      const PURL & url,                  // Universal Resource Locator for document.
      const PMIMEInfo & inMIME,          // Extra MIME information in command.
      const PMultipartFormInfoArray & multipartFormInfo,
      PHTTPServer & socket
    );

    /** Get the headers for block of data (eg HTML) that the resource contains.
       This will fill in all the fields of the <CODE>outMIME</CODE> parameter
       required by the resource and return the status for the load.

       @return
       TRUE if all OK, FALSE if an error occurred.
     */
    virtual BOOL LoadHeaders(
      PHTTPRequest & request    // Information on this request.
    );

    /** Get a block of data that the resource contains.

       @return
       TRUE if more to load.
     */
    virtual BOOL LoadData(
      PHTTPRequest & request,    // Information on this request.
      PCharArray & data          // Data used in reply.
    );

    /** Get a block of text data (eg HTML) that the resource contains.

       The default behaviour is to assert, one of #LoadText()# or
       #LoadData()# functions must be overridden for correct operation.

       @return
       String for loaded text.
     */
    virtual PString LoadText(
      PHTTPRequest & request    // Information on this request.
    );


  protected:
    PHTTPFile(
      const PURL & url,       // Name of the resource in URL space.
      int dummy
    );
    // Constructor used by PHTTPDirectory


    PFilePath filePath;
};


class PHTTPFileRequest : public PHTTPRequest
{
  PCLASSINFO(PHTTPFileRequest, PHTTPRequest)
  public:
    PHTTPFileRequest(
      const PURL & url,             // Universal Resource Locator for document.
      const PMIMEInfo & inMIME,     // Extra MIME information in command.
      const PMultipartFormInfoArray & multipartFormInfo,
      PHTTPServer & server
    );

    PFile file;
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPTailFile

/** This object describes a HyperText Transport Protocol resource which is a
   single file. The file can be anywhere in the file system and is mapped to
   the specified URL location in the HTTP name space defined by the
   #PHTTPSpace# class.

   The difference between this and PHTTPFile is that it continually outputs
   the contents of the file, as per the unix "tail -f" command.
 */
class PHTTPTailFile : public PHTTPFile
{
  PCLASSINFO(PHTTPTailFile, PHTTPFile)

  public:
    /** Contruct a new simple file resource for the HTTP space. If no MIME
       content type is specified then a default type is used depending on the
       file type. For example, "text/html" is used of the file type is
       ".html" or ".htm". The default for an unknown type is
       "application/octet-stream".
     */
    PHTTPTailFile(
      const PString & filename     // file in file system and URL name.
    );
    PHTTPTailFile(
      const PString & filename,    // file in file system and URL name.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );
    PHTTPTailFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file       // Location of file in file system.
    );
    PHTTPTailFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file,      // Location of file in file system.
      const PString & contentType  // MIME content type for the file.
    );
    PHTTPTailFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file,      // Location of file in file system.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );
    PHTTPTailFile(
      const PURL & url,            // Name of the resource in URL space.
      const PFilePath & file,      // Location of file in file system.
      const PString & contentType, // MIME content type for the file.
      const PHTTPAuthority & auth  // Authorisation for the resource.
    );


  // Overrides from class PHTTPResource
    /** Get the headers for block of data (eg HTML) that the resource contains.
       This will fill in all the fields of the <CODE>outMIME</CODE> parameter
       required by the resource and return the status for the load.

       @return
       TRUE if all OK, FALSE if an error occurred.
     */
    virtual BOOL LoadHeaders(
      PHTTPRequest & request    // Information on this request.
    );

    /** Get a block of data that the resource contains.

       @return
       TRUE if more to load.
     */
    virtual BOOL LoadData(
      PHTTPRequest & request,    // Information on this request.
      PCharArray & data          // Data used in reply.
    );
};


//////////////////////////////////////////////////////////////////////////////
// PHTTPDirectory

/** This object describes a HyperText Transport Protocol resource which is a
   set of files in a directory. The directory can be anywhere in the file
   system and is mapped to the specified URL location in the HTTP name space
   defined by the #PHTTPSpace# class.

   All subdirectories and files are available as URL names in the HTTP name
   space. This effectively grafts a file system directory tree onto the URL
   name space tree.

   See the #PMIMEInfo# class for more information on the mappings between
   file types and MIME types.
 */
class PHTTPDirectory : public PHTTPFile
{
  PCLASSINFO(PHTTPDirectory, PHTTPFile)

  public:
    PHTTPDirectory(
      const PURL & url,            /// Name of the resource in URL space.
      const PDirectory & dir       /// Location of file in file system.
    );
    PHTTPDirectory(
      const PURL & url,            /// Name of the resource in URL space.
      const PDirectory & dir,      /// Location of file in file system.
      const PHTTPAuthority & auth  /// Authorisation for the resource.
    );
    // Construct a new directory resource for HTTP.


  // Overrides from class PHTTPResource
    /** Create a new request block for this type of resource.

       @return
       Pointer to instance of PHTTPRequest descendant class.
     */
    virtual PHTTPRequest * CreateRequest(
      const PURL & url,                  // Universal Resource Locator for document.
      const PMIMEInfo & inMIME,          // Extra MIME information in command.
      const PMultipartFormInfoArray & multipartFormInfo,
      PHTTPServer & socket
    );

    /** Get the headers for block of data (eg HTML) that the resource contains.
       This will fill in all the fields of the <CODE>outMIME</CODE> parameter
       required by the resource and return the status for the load.

       @return
       TRUE if all OK, FALSE if an error occurred.
     */
    virtual BOOL LoadHeaders(
      PHTTPRequest & request    /// Information on this request.
    );

    /** Get a block of text data (eg HTML) that the resource contains.

       The default behaviour is to assert, one of #LoadText()# or
       #LoadData()# functions must be overridden for correct operation.

       @return
       String for loaded text.
     */
    virtual PString LoadText(
      PHTTPRequest & request    /// Information on this request.
    );

    /** Enable or disable access control using .access files. A directory tree containing
       a _access file will require authorisation to allow access. This file has 
       contains one or more lines, each containing a username and password seperated 
       by a ":" character.

       The parameter sets the realm used for authorisation requests. An empty realm disables
       auhtorisation.
     */
    void EnableAuthorisation(const PString & realm);

    /** Enable or disable directory listings when a default directory file does not exist
     */
    void AllowDirectories(BOOL enable = TRUE);

  protected:
    BOOL CheckAuthority(
      PHTTPServer & server,               // Server to send response to.
      const PHTTPRequest & request,       // Information on this request.
      const PHTTPConnectionInfo & conInfo // Information on the connection
    );

    BOOL FindAuthorisations(const PDirectory & dir, PString & realm, PStringToString & authorisations);

    PDirectory basePath;
    PString authorisationRealm;
    BOOL allowDirectoryListing;
};


class PHTTPDirRequest : public PHTTPFileRequest
{
  PCLASSINFO(PHTTPDirRequest, PHTTPFileRequest)
  public:
    PHTTPDirRequest(
      const PURL & url,             // Universal Resource Locator for document.
      const PMIMEInfo & inMIME,     // Extra MIME information in command.
      const PMultipartFormInfoArray & multipartFormInfo, 
      PHTTPServer & server
    );

    PString fakeIndex;
    PFilePath realPath;
};

#endif // P_HTTPSVC

#endif


// End Of File ///////////////////////////////////////////////////////////////