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
|
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* util.c: string utility things
*
* 3/21/93 Rob McCool
* 1995-96 Many changes by the Apache Group
*
*/
/* Debugging aid:
* #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
* #define DEBUG_CFG_LINES to trace every line read from the config files
*/
#include "httpd.h"
#include "http_conf_globals.h" /* for user_id & group_id */
#include "http_log.h"
#if defined(SUNOS4)
/* stdio.h has been read in ap_config.h already. Add missing prototypes here: */
extern int fgetc(FILE *);
extern char *fgets(char *s, int, FILE*);
extern int fclose(FILE *);
#endif
/* A bunch of functions in util.c scan strings looking for certain characters.
* To make that more efficient we encode a lookup table. The test_char_table
* is generated automatically by gen_test_char.c.
*/
#include "test_char.h"
/* we assume the folks using this ensure 0 <= c < 256... which means
* you need a cast to (unsigned char) first, you can't just plug a
* char in here and get it to work, because if char is signed then it
* will first be sign extended.
*/
#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
void ap_util_init(void)
{
/* nothing to do... previously there was run-time initialization of
* test_char_table here
*/
}
API_VAR_EXPORT const char ap_month_snames[12][4] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
API_VAR_EXPORT const char ap_day_snames[7][4] =
{
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
API_EXPORT(char *) ap_get_time(void)
{
time_t t;
char *time_string;
t = time(NULL);
time_string = ctime(&t);
time_string[strlen(time_string) - 1] = '\0';
return (time_string);
}
/*
* Examine a field value (such as a media-/content-type) string and return
* it sans any parameters; e.g., strip off any ';charset=foo' and the like.
*/
API_EXPORT(char *) ap_field_noparam(pool *p, const char *intype)
{
const char *semi;
if (intype == NULL) return NULL;
semi = strchr(intype, ';');
if (semi == NULL) {
return ap_pstrdup(p, intype);
}
else {
while ((semi > intype) && ap_isspace(semi[-1])) {
semi--;
}
return ap_pstrndup(p, intype, semi - intype);
}
}
API_EXPORT(char *) ap_ht_time(pool *p, time_t t, const char *fmt, int gmt)
{
char ts[MAX_STRING_LEN];
char tf[MAX_STRING_LEN];
struct tm *tms;
tms = (gmt ? gmtime(&t) : localtime(&t));
if(gmt) {
/* Convert %Z to "GMT" and %z to "+0000";
* on hosts that do not have a time zone string in struct tm,
* strftime must assume its argument is local time.
*/
const char *f;
char *strp;
for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
; f++, strp++) {
if (*f != '%') continue;
switch (f[1]) {
case '%':
*++strp = *++f;
break;
case 'Z':
*strp++ = 'G';
*strp++ = 'M';
*strp = 'T';
f++;
break;
case 'z': /* common extension */
*strp++ = '+';
*strp++ = '0';
*strp++ = '0';
*strp++ = '0';
*strp = '0';
f++;
break;
}
}
*strp = '\0';
fmt = tf;
}
/* check return code? */
strftime(ts, MAX_STRING_LEN, fmt, tms);
ts[MAX_STRING_LEN - 1] = '\0';
return ap_pstrdup(p, ts);
}
API_EXPORT(char *) ap_gm_timestr_822(pool *p, time_t sec)
{
struct tm *tms;
tms = gmtime(&sec);
/* RFC date format; as strftime '%a, %d %b %Y %T GMT' */
return ap_psprintf(p,
"%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[tms->tm_wday],
tms->tm_mday, ap_month_snames[tms->tm_mon], tms->tm_year + 1900,
tms->tm_hour, tms->tm_min, tms->tm_sec);
}
/* What a pain in the ass. */
#if defined(HAVE_GMTOFF)
API_EXPORT(struct tm *) ap_get_gmtoff(int *tz)
{
time_t tt = time(NULL);
struct tm *t;
t = localtime(&tt);
*tz = (int) (t->tm_gmtoff / 60);
return t;
}
#else
API_EXPORT(struct tm *) ap_get_gmtoff(int *tz)
{
time_t tt = time(NULL);
struct tm gmt;
struct tm *t;
int days, hours, minutes;
/* Assume we are never more than 24 hours away. */
gmt = *gmtime(&tt); /* remember gmtime/localtime return ptr to static */
t = localtime(&tt); /* buffer... so be careful */
days = t->tm_yday - gmt.tm_yday;
hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24)
+ t->tm_hour - gmt.tm_hour);
minutes = hours * 60 + t->tm_min - gmt.tm_min;
*tz = minutes;
return t;
}
#endif
/* Roy owes Rob beer. */
/* Rob owes Roy dinner. */
/* These legacy comments would make a lot more sense if Roy hadn't
* replaced the old later_than() routine with util_date.c.
*
* Well, okay, they still wouldn't make any sense.
*/
/* Match = 0, NoMatch = 1, Abort = -1
* Based loosely on sections of wildmat.c by Rich Salz
* Hmmm... shouldn't this really go component by component?
*/
API_EXPORT(int) ap_strcmp_match(const char *str, const char *exp)
{
int x, y;
for (x = 0, y = 0; exp[y]; ++y, ++x) {
if ((!str[x]) && (exp[y] != '*'))
return -1;
if (exp[y] == '*') {
while (exp[++y] == '*');
if (!exp[y])
return 0;
while (str[x]) {
int ret;
if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
return ret;
}
return -1;
}
else if ((exp[y] != '?') && (str[x] != exp[y]))
return 1;
}
return (str[x] != '\0');
}
API_EXPORT(int) ap_strcasecmp_match(const char *str, const char *exp)
{
int x, y;
for (x = 0, y = 0; exp[y]; ++y, ++x) {
if ((!str[x]) && (exp[y] != '*'))
return -1;
if (exp[y] == '*') {
while (exp[++y] == '*');
if (!exp[y])
return 0;
while (str[x]) {
int ret;
if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
return ret;
}
return -1;
}
else if ((exp[y] != '?') && (ap_tolower(str[x]) != ap_tolower(exp[y])))
return 1;
}
return (str[x] != '\0');
}
API_EXPORT(int) ap_is_matchexp(const char *str)
{
register int x;
for (x = 0; str[x]; x++)
if ((str[x] == '*') || (str[x] == '?'))
return 1;
return 0;
}
/*
* Similar to standard strstr() but we ignore case in this version.
* Based on the strstr() implementation further below.
*/
API_EXPORT(char *) ap_strcasestr(const char *s1, const char *s2)
{
char *p1, *p2;
if (*s2 == '\0') {
/* an empty s2 */
return((char *)s1);
}
while(1) {
for ( ; (*s1 != '\0') && (ap_tolower(*s1) != ap_tolower(*s2)); s1++);
if (*s1 == '\0') return(NULL);
/* found first character of s2, see if the rest matches */
p1 = (char *)s1;
p2 = (char *)s2;
while (ap_tolower(*++p1) == ap_tolower(*++p2)) {
if (*p1 == '\0') {
/* both strings ended together */
return((char *)s1);
}
}
if (*p2 == '\0') {
/* second string ended, a match */
break;
}
/* didn't find a match here, try starting at next character in s1 */
s1++;
}
return((char *)s1);
}
/*
* Returns an offsetted pointer in bigstring immediately after
* prefix. Returns bigstring if bigstring doesn't start with
* prefix or if prefix is longer than bigstring while still matching.
* NOTE: pointer returned is relative to bigstring, so we
* can use standard pointer comparisons in the calling function
* (eg: test if ap_stripprefix(a,b) == a)
*/
API_EXPORT(char *) ap_stripprefix(const char *bigstring, const char *prefix)
{
char *p1;
if (*prefix == '\0') {
return( (char *)bigstring);
}
p1 = (char *)bigstring;
while(*p1 && *prefix) {
if (*p1++ != *prefix++)
return( (char *)bigstring);
}
if (*prefix == '\0')
return(p1);
else /* hit the end of bigstring! */
return( (char *)bigstring);
}
/*
* Apache stub function for the regex libraries regexec() to make sure the
* whole regex(3) API is available through the Apache (exported) namespace.
* This is especially important for the DSO situations of modules.
* DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
*/
API_EXPORT(int) ap_regexec(const regex_t *preg, const char *string,
size_t nmatch, regmatch_t pmatch[], int eflags)
{
return regexec(preg, string, nmatch, pmatch, eflags);
}
API_EXPORT(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
{
return regerror(errcode, preg, errbuf, errbuf_size);
}
/* This function substitutes for $0-$9, filling in regular expression
* submatches. Pass it the same nmatch and pmatch arguments that you
* passed ap_regexec(). pmatch should not be greater than the maximum number
* of subexpressions - i.e. one more than the re_nsub member of regex_t.
*
* input should be the string with the $-expressions, source should be the
* string that was matched against.
*
* It returns the substituted string, or NULL on error.
*
* Parts of this code are based on Henry Spencer's regsub(), from his
* AT&T V8 regexp package.
*/
API_EXPORT(char *) ap_pregsub(pool *p, const char *input, const char *source,
size_t nmatch, regmatch_t pmatch[])
{
const char *src = input;
char *dest, *dst;
char c;
size_t no;
int len;
if (!source)
return NULL;
if (!nmatch)
return ap_pstrdup(p, src);
/* First pass, find the size */
len = 0;
while ((c = *src++) != '\0') {
if (c == '&')
no = 0;
else if (c == '$' && ap_isdigit(*src))
no = *src++ - '0';
else
no = 10;
if (no > 9) { /* Ordinary character. */
if (c == '\\' && (*src == '$' || *src == '&'))
c = *src++;
len++;
}
else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
len += pmatch[no].rm_eo - pmatch[no].rm_so;
}
}
dest = dst = ap_pcalloc(p, len + 1);
/* Now actually fill in the string */
src = input;
while ((c = *src++) != '\0') {
if (c == '&')
no = 0;
else if (c == '$' && ap_isdigit(*src))
no = *src++ - '0';
else
no = 10;
if (no > 9) { /* Ordinary character. */
if (c == '\\' && (*src == '$' || *src == '&'))
c = *src++;
*dst++ = c;
}
else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
len = pmatch[no].rm_eo - pmatch[no].rm_so;
memcpy(dst, source + pmatch[no].rm_so, len);
dst += len;
}
}
*dst = '\0';
return dest;
}
/*
* Parse .. so we don't compromise security
*/
API_EXPORT(void) ap_getparents(char *name)
{
int l, w;
/* Four passes, as per RFC 1808 */
/* a) remove ./ path segments */
for (l = 0, w = 0; name[l] != '\0';) {
if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
l += 2;
else
name[w++] = name[l++];
}
/* b) remove trailing . path, segment */
if (w == 1 && name[0] == '.')
w--;
else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
w--;
name[w] = '\0';
/* c) remove all xx/../ segments. (including leading ../ and /../) */
l = 0;
while (name[l] != '\0') {
if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
(l == 0 || name[l - 1] == '/')) {
register int m = l + 3, n;
l = l - 2;
if (l >= 0) {
while (l >= 0 && name[l] != '/')
l--;
l++;
}
else
l = 0;
n = l;
while ((name[n] = name[m]))
(++n, ++m);
}
else
++l;
}
/* d) remove trailing xx/.. segment. */
if (l == 2 && name[0] == '.' && name[1] == '.')
name[0] = '\0';
else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
l = l - 4;
if (l >= 0) {
while (l >= 0 && name[l] != '/')
l--;
l++;
}
else
l = 0;
name[l] = '\0';
}
}
API_EXPORT(void) ap_no2slash(char *name)
{
char *d, *s;
s = d = name;
#ifdef HAVE_UNC_PATHS
/* Check for UNC names. Leave leading two slashes. */
if (s[0] == '/' && s[1] == '/')
*d++ = *s++;
#endif
while (*s) {
if ((*d++ = *s) == '/') {
do {
++s;
} while (*s == '/');
}
else {
++s;
}
}
*d = '\0';
}
/*
* copy at most n leading directories of s into d
* d should be at least as large as s plus 1 extra byte
* assumes n > 0
* the return value is the ever useful pointer to the trailing \0 of d
*
* examples:
* /a/b, 1 ==> /
* /a/b, 2 ==> /a/
* /a/b, 3 ==> /a/b/
* /a/b, 4 ==> /a/b/
*
* MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
* so that if n == 0, "/" is returned in d with n == 1
* and s == "e:/test.html", "e:/" is returned in d
* *** See also directory_walk in src/main/http_request.c
*/
API_EXPORT(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
{
#if defined(HAVE_DRIVE_LETTERS) || defined(NETWARE)
if (!n) {
*d = '/';
*++d = '\0';
return (d);
}
#endif /* def HAVE_DRIVE_LETTERS || NETWARE */
for (;;) {
*d = *s;
if (*d == '\0') {
*d = '/';
break;
}
if (*d == '/' && (--n) == 0)
break;
++d;
++s;
}
*++d = 0;
return (d);
}
/*
* return the parent directory name including trailing / of the file s
*/
API_EXPORT(char *) ap_make_dirstr_parent(pool *p, const char *s)
{
char *last_slash = strrchr(s, '/');
char *d;
int l;
if (last_slash == NULL) {
/* XXX: well this is really broken if this happens */
return (ap_pstrdup(p, "/"));
}
l = (last_slash - s) + 1;
d = ap_palloc(p, l + 1);
memcpy(d, s, l);
d[l] = 0;
return (d);
}
/*
* This function is deprecated. Use one of the preceeding two functions
* which are faster.
*/
API_EXPORT(char *) ap_make_dirstr(pool *p, const char *s, int n)
{
register int x, f;
char *res;
for (x = 0, f = 0; s[x]; x++) {
if (s[x] == '/')
if ((++f) == n) {
res = ap_palloc(p, x + 2);
memcpy(res, s, x);
res[x] = '/';
res[x + 1] = '\0';
return res;
}
}
if (s[strlen(s) - 1] == '/')
return ap_pstrdup(p, s);
else
return ap_pstrcat(p, s, "/", NULL);
}
API_EXPORT(int) ap_count_dirs(const char *path)
{
register int x, n;
for (x = 0, n = 0; path[x]; x++)
if (path[x] == '/')
n++;
return n;
}
API_EXPORT(void) ap_chdir_file(const char *file)
{
const char *x;
char buf[HUGE_STRING_LEN];
x = strrchr(file, '/');
if (x == NULL) {
chdir(file);
}
else if (x - file < sizeof(buf) - 1) {
memcpy(buf, file, x - file);
buf[x - file] = '\0';
chdir(buf);
}
/* XXX: well, this is a silly function, no method of reporting an
* error... ah well. */
}
API_EXPORT(char *) ap_getword_nc(pool *atrans, char **line, char stop)
{
return ap_getword(atrans, (const char **) line, stop);
}
API_EXPORT(char *) ap_getword(pool *atrans, const char **line, char stop)
{
char *pos = strchr(*line, stop);
char *res;
if (!pos) {
res = ap_pstrdup(atrans, *line);
*line += strlen(*line);
return res;
}
res = ap_pstrndup(atrans, *line, pos - *line);
while (*pos == stop) {
++pos;
}
*line = pos;
return res;
}
API_EXPORT(char *) ap_getword_white_nc(pool *atrans, char **line)
{
return ap_getword_white(atrans, (const char **) line);
}
API_EXPORT(char *) ap_getword_white(pool *atrans, const char **line)
{
int pos = -1, x;
char *res;
for (x = 0; (*line)[x]; x++) {
if (ap_isspace((*line)[x])) {
pos = x;
break;
}
}
if (pos == -1) {
res = ap_pstrdup(atrans, *line);
*line += strlen(*line);
return res;
}
res = ap_palloc(atrans, pos + 1);
ap_cpystrn(res, *line, pos + 1);
while (ap_isspace((*line)[pos]))
++pos;
*line += pos;
return res;
}
API_EXPORT(char *) ap_getword_nulls_nc(pool *atrans, char **line, char stop)
{
return ap_getword_nulls(atrans, (const char **) line, stop);
}
API_EXPORT(char *) ap_getword_nulls(pool *atrans, const char **line, char stop)
{
char *pos = strchr(*line, stop);
char *res;
if (!pos) {
res = ap_pstrdup(atrans, *line);
*line += strlen(*line);
return res;
}
res = ap_pstrndup(atrans, *line, pos - *line);
++pos;
*line = pos;
return res;
}
/* Get a word, (new) config-file style --- quoted strings and backslashes
* all honored
*/
static char *substring_conf(pool *p, const char *start, int len, char quote)
{
char *result = ap_palloc(p, len + 2);
char *resp = result;
int i;
for (i = 0; i < len; ++i) {
if (start[i] == '\\' && (start[i + 1] == '\\'
|| (quote && start[i + 1] == quote)))
*resp++ = start[++i];
else
*resp++ = start[i];
}
*resp++ = '\0';
return result;
}
API_EXPORT(char *) ap_getword_conf_nc(pool *p, char **line)
{
return ap_getword_conf(p, (const char **) line);
}
API_EXPORT(char *) ap_getword_conf(pool *p, const char **line)
{
const char *str = *line, *strend;
char *res;
char quote;
while (*str && ap_isspace(*str))
++str;
if (!*str) {
*line = str;
return "";
}
if ((quote = *str) == '"' || quote == '\'') {
strend = str + 1;
while (*strend && *strend != quote) {
if (*strend == '\\' && strend[1] && strend[1] == quote)
strend += 2;
else
++strend;
}
res = substring_conf(p, str + 1, strend - str - 1, quote);
if (*strend == quote)
++strend;
}
else {
if (*str == '#')
ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, NULL,
"Apache does not support line-end comments. Consider using quotes around argument: \"%s\"", str);
strend = str;
while (*strend && !ap_isspace(*strend))
++strend;
res = substring_conf(p, str, strend - str, 0);
}
while (*strend && ap_isspace(*strend))
++strend;
*line = strend;
return res;
}
API_EXPORT(int) ap_cfg_closefile(configfile_t *cfp)
{
#ifdef DEBUG
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,
"Done with config file %s", cfp->name);
#endif
return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
}
/* Common structure that holds the file and pool for ap_pcfg_openfile */
typedef struct {
struct pool *pool;
FILE *file;
} poolfile_t;
static int cfg_close(void *param)
{
poolfile_t *cfp = (poolfile_t *) param;
return (ap_pfclose(cfp->pool, cfp->file));
}
static int cfg_getch(void *param)
{
poolfile_t *cfp = (poolfile_t *) param;
return (fgetc(cfp->file));
}
static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
{
poolfile_t *cfp = (poolfile_t *) param;
return (fgets(buf, bufsiz, cfp->file));
}
/* Open a configfile_t as FILE, return open configfile_t struct pointer */
API_EXPORT(configfile_t *) ap_pcfg_openfile(pool *p, const char *name)
{
configfile_t *new_cfg;
poolfile_t *new_pfile;
FILE *file;
struct stat stbuf;
int saved_errno;
if (name == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
"Internal error: pcfg_openfile() called with NULL filename");
return NULL;
}
if (!ap_os_is_filename_valid(name)) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
"Access to config file %s denied: not a valid filename",
name);
errno = EACCES;
return NULL;
}
#ifdef FOPEN_REQUIRES_T
file = ap_pfopen(p, name, "rt");
#else
file = ap_pfopen(p, name, "r");
#endif
#ifdef DEBUG
saved_errno = errno;
ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL,
"Opening config file %s (%s)",
name, (file == NULL) ? strerror(errno) : "successful");
errno = saved_errno;
#endif
if (file == NULL)
return NULL;
if (fstat(fileno(file), &stbuf) == 0 &&
!S_ISREG(stbuf.st_mode) &&
#if defined(WIN32) || defined(OS2)
!(strcasecmp(name, "nul") == 0 ||
(strlen(name) >= 4 &&
strcasecmp(name + strlen(name) - 4, "/nul") == 0))) {
#else
strcmp(name, "/dev/null") != 0) {
#endif /* WIN32 || OS2 */
saved_errno = errno;
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
"Access to file %s denied by server: not a regular file",
name);
ap_pfclose(p, file);
errno = saved_errno;
return NULL;
}
new_cfg = ap_palloc(p, sizeof(*new_cfg));
new_pfile = ap_palloc(p, sizeof(*new_pfile));
new_pfile->file = file;
new_pfile->pool = p;
new_cfg->param = new_pfile;
new_cfg->name = ap_pstrdup(p, name);
new_cfg->getch = (int (*)(void *)) cfg_getch;
new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
new_cfg->close = (int (*)(void *)) cfg_close;
new_cfg->line_number = 0;
return new_cfg;
}
/* Allocate a configfile_t handle with user defined functions and params */
API_EXPORT(configfile_t *) ap_pcfg_open_custom(pool *p, const char *descr,
void *param,
int(*getch)(void *param),
void *(*getstr) (void *buf, size_t bufsiz, void *param),
int(*close_func)(void *param))
{
configfile_t *new_cfg = ap_palloc(p, sizeof(*new_cfg));
#ifdef DEBUG
ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL, "Opening config handler %s", descr);
#endif
new_cfg->param = param;
new_cfg->name = descr;
new_cfg->getch = getch;
new_cfg->getstr = getstr;
new_cfg->close = close_func;
new_cfg->line_number = 0;
return new_cfg;
}
/* Read one character from a configfile_t */
API_EXPORT(int) ap_cfg_getc(configfile_t *cfp)
{
register int ch = cfp->getch(cfp->param);
if (ch == LF)
++cfp->line_number;
return ch;
}
/* Read one line from open configfile_t, strip LF, increase line number */
/* If custom handler does not define a getstr() function, read char by char */
API_EXPORT(int) ap_cfg_getline(char *buf, size_t bufsize, configfile_t *cfp)
{
/* If a "get string" function is defined, use it */
if (cfp->getstr != NULL) {
char *src, *dst;
char *cp;
char *cbuf = buf;
size_t cbufsize = bufsize;
while (1) {
++cfp->line_number;
if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
return 1;
/*
* check for line continuation,
* i.e. match [^\\]\\[\r]\n only
*/
cp = cbuf;
while (cp < cbuf+cbufsize && *cp != '\0')
cp++;
if (cp > cbuf && cp[-1] == LF) {
cp--;
if (cp > cbuf && cp[-1] == CR)
cp--;
if (cp > cbuf && cp[-1] == '\\') {
cp--;
if (!(cp > cbuf && cp[-1] == '\\')) {
/*
* line continuation requested -
* then remove backslash and continue
*/
cbufsize -= (cp-cbuf);
cbuf = cp;
continue;
}
else {
/*
* no real continuation because escaped -
* then just remove escape character
*/
for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
cp[0] = cp[1];
}
}
}
break;
}
/*
* Leading and trailing white space is eliminated completely
*/
src = buf;
while (ap_isspace(*src))
++src;
/* blast trailing whitespace */
dst = &src[strlen(src)];
while (--dst >= src && ap_isspace(*dst))
*dst = '\0';
/* Zap leading whitespace by shifting */
if (src != buf)
for (dst = buf; (*dst++ = *src++) != '\0'; )
;
#ifdef DEBUG_CFG_LINES
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL, "Read config: %s", buf);
#endif
return 0;
} else {
/* No "get string" function defined; read character by character */
register int c;
register size_t i = 0;
buf[0] = '\0';
/* skip leading whitespace */
do {
c = cfp->getch(cfp->param);
} while (c == '\t' || c == ' ');
if (c == EOF)
return 1;
if(bufsize < 2) {
/* too small, assume caller is crazy */
return 1;
}
while (1) {
if ((c == '\t') || (c == ' ')) {
buf[i++] = ' ';
while ((c == '\t') || (c == ' '))
c = cfp->getch(cfp->param);
}
if (c == CR) {
/* silently ignore CR (_assume_ that a LF follows) */
c = cfp->getch(cfp->param);
}
if (c == LF) {
/* increase line number and return on LF */
++cfp->line_number;
}
if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
/*
* check for line continuation
*/
if (i > 0 && buf[i-1] == '\\') {
i--;
if (!(i > 0 && buf[i-1] == '\\')) {
/* line is continued */
c = cfp->getch(cfp->param);
continue;
}
/* else nothing needs be done because
* then the backslash is escaped and
* we just strip to a single one
*/
}
/* blast trailing whitespace */
while (i > 0 && ap_isspace(buf[i - 1]))
--i;
buf[i] = '\0';
#ifdef DEBUG_CFG_LINES
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL, "Read config: %s", buf);
#endif
return 0;
}
buf[i] = c;
++i;
c = cfp->getch(cfp->param);
}
}
}
/* Size an HTTP header field list item, as separated by a comma.
* The return value is a pointer to the beginning of the non-empty list item
* within the original string (or NULL if there is none) and the address
* of field is shifted to the next non-comma, non-whitespace character.
* len is the length of the item excluding any beginning whitespace.
*/
API_EXPORT(const char *) ap_size_list_item(const char **field, int *len)
{
const unsigned char *ptr = (const unsigned char *)*field;
const unsigned char *token;
int in_qpair, in_qstr, in_com;
/* Find first non-comma, non-whitespace byte */
while (*ptr == ',' || ap_isspace(*ptr))
++ptr;
token = ptr;
/* Find the end of this item, skipping over dead bits */
for (in_qpair = in_qstr = in_com = 0;
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
++ptr) {
if (in_qpair) {
in_qpair = 0;
}
else {
switch (*ptr) {
case '\\': in_qpair = 1; /* quoted-pair */
break;
case '"' : if (!in_com) /* quoted string delim */
in_qstr = !in_qstr;
break;
case '(' : if (!in_qstr) /* comment (may nest) */
++in_com;
break;
case ')' : if (in_com) /* end comment */
--in_com;
break;
default : break;
}
}
}
if ((*len = (ptr - token)) == 0) {
*field = (const char *)ptr;
return NULL;
}
/* Advance field pointer to the next non-comma, non-white byte */
while (*ptr == ',' || ap_isspace(*ptr))
++ptr;
*field = (const char *)ptr;
return (const char *)token;
}
/* Retrieve an HTTP header field list item, as separated by a comma,
* while stripping insignificant whitespace and lowercasing anything not in
* a quoted string or comment. The return value is a new string containing
* the converted list item (or NULL if none) and the address pointed to by
* field is shifted to the next non-comma, non-whitespace.
*/
API_EXPORT(char *) ap_get_list_item(pool *p, const char **field)
{
const char *tok_start;
const unsigned char *ptr;
unsigned char *pos;
char *token;
int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
/* Find the beginning and maximum length of the list item so that
* we can allocate a buffer for the new string and reset the field.
*/
if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
return NULL;
}
token = ap_palloc(p, tok_len + 1);
/* Scan the token again, but this time copy only the good bytes.
* We skip extra whitespace and any whitespace around a '=', '/',
* or ';' and lowercase normal characters not within a comment,
* quoted-string or quoted-pair.
*/
for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
++ptr) {
if (in_qpair) {
in_qpair = 0;
*pos++ = *ptr;
}
else {
switch (*ptr) {
case '\\': in_qpair = 1;
if (addspace == 1)
*pos++ = ' ';
*pos++ = *ptr;
addspace = 0;
break;
case '"' : if (!in_com)
in_qstr = !in_qstr;
if (addspace == 1)
*pos++ = ' ';
*pos++ = *ptr;
addspace = 0;
break;
case '(' : if (!in_qstr)
++in_com;
if (addspace == 1)
*pos++ = ' ';
*pos++ = *ptr;
addspace = 0;
break;
case ')' : if (in_com)
--in_com;
*pos++ = *ptr;
addspace = 0;
break;
case ' ' :
case '\t': if (addspace)
break;
if (in_com || in_qstr)
*pos++ = *ptr;
else
addspace = 1;
break;
case '=' :
case '/' :
case ';' : if (!(in_com || in_qstr))
addspace = -1;
*pos++ = *ptr;
break;
default : if (addspace == 1)
*pos++ = ' ';
*pos++ = (in_com || in_qstr) ? *ptr
: ap_tolower(*ptr);
addspace = 0;
break;
}
}
}
*pos = '\0';
return token;
}
/* Find an item in canonical form (lowercase, no extra spaces) within
* an HTTP field value list. Returns 1 if found, 0 if not found.
* This would be much more efficient if we stored header fields as
* an array of list items as they are received instead of a plain string.
*/
API_EXPORT(int) ap_find_list_item(pool *p, const char *line, const char *tok)
{
const unsigned char *pos;
const unsigned char *ptr = (const unsigned char *)line;
int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
if (!line || !tok)
return 0;
do { /* loop for each item in line's list */
/* Find first non-comma, non-whitespace byte */
while (*ptr == ',' || ap_isspace(*ptr))
++ptr;
if (*ptr)
good = 1; /* until proven otherwise for this item */
else
break; /* no items left and nothing good found */
/* We skip extra whitespace and any whitespace around a '=', '/',
* or ';' and lowercase normal characters not within a comment,
* quoted-string or quoted-pair.
*/
for (pos = (const unsigned char *)tok;
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
++ptr) {
if (in_qpair) {
in_qpair = 0;
if (good)
good = (*pos++ == *ptr);
}
else {
switch (*ptr) {
case '\\': in_qpair = 1;
if (addspace == 1)
good = good && (*pos++ == ' ');
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case '"' : if (!in_com)
in_qstr = !in_qstr;
if (addspace == 1)
good = good && (*pos++ == ' ');
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case '(' : if (!in_qstr)
++in_com;
if (addspace == 1)
good = good && (*pos++ == ' ');
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case ')' : if (in_com)
--in_com;
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case ' ' :
case '\t': if (addspace || !good)
break;
if (in_com || in_qstr)
good = (*pos++ == *ptr);
else
addspace = 1;
break;
case '=' :
case '/' :
case ';' : if (!(in_com || in_qstr))
addspace = -1;
good = good && (*pos++ == *ptr);
break;
default : if (!good)
break;
if (addspace == 1)
good = (*pos++ == ' ');
if (in_com || in_qstr)
good = good && (*pos++ == *ptr);
else
good = good && (*pos++ == ap_tolower(*ptr));
addspace = 0;
break;
}
}
}
if (good && *pos)
good = 0; /* not good if only a prefix was matched */
} while (*ptr && !good);
return good;
}
/* Retrieve a token, spacing over it and returning a pointer to
* the first non-white byte afterwards. Note that these tokens
* are delimited by semis and commas; and can also be delimited
* by whitespace at the caller's option.
*/
API_EXPORT(char *) ap_get_token(pool *p, const char **accept_line, int accept_white)
{
const char *ptr = *accept_line;
const char *tok_start;
char *token;
int tok_len;
/* Find first non-white byte */
while (*ptr && ap_isspace(*ptr))
++ptr;
tok_start = ptr;
/* find token end, skipping over quoted strings.
* (comments are already gone).
*/
while (*ptr && (accept_white || !ap_isspace(*ptr))
&& *ptr != ';' && *ptr != ',') {
if (*ptr++ == '"')
while (*ptr)
if (*ptr++ == '"')
break;
}
tok_len = ptr - tok_start;
token = ap_pstrndup(p, tok_start, tok_len);
/* Advance accept_line pointer to the next non-white byte */
while (*ptr && ap_isspace(*ptr))
++ptr;
*accept_line = ptr;
return token;
}
/* find http tokens, see the definition of token from RFC2068 */
API_EXPORT(int) ap_find_token(pool *p, const char *line, const char *tok)
{
const unsigned char *start_token;
const unsigned char *s;
if (!line)
return 0;
s = (const unsigned char *)line;
for (;;) {
/* find start of token, skip all stop characters, note NUL
* isn't a token stop, so we don't need to test for it
*/
while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
++s;
}
if (!*s) {
return 0;
}
start_token = s;
/* find end of the token */
while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
++s;
}
if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
return 1;
}
if (!*s) {
return 0;
}
}
}
API_EXPORT(int) ap_find_last_token(pool *p, const char *line, const char *tok)
{
int llen, tlen, lidx;
if (!line)
return 0;
llen = strlen(line);
tlen = strlen(tok);
lidx = llen - tlen;
if ((lidx < 0) ||
((lidx > 0) && !(ap_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
return 0;
return (strncasecmp(&line[lidx], tok, tlen) == 0);
}
/* c2x takes an unsigned, and expects the caller has guaranteed that
* 0 <= what < 256... which usually means that you have to cast to
* unsigned char first, because (unsigned)(char)(x) first goes through
* signed extension to an int before the unsigned cast.
*
* The reason for this assumption is to assist gcc code generation --
* the unsigned char -> unsigned extension is already done earlier in
* both uses of this code, so there's no need to waste time doing it
* again.
*/
static const char c2x_table[] = "0123456789abcdef";
static ap_inline unsigned char *c2x(unsigned what, unsigned char *where)
{
#ifdef CHARSET_EBCDIC
what = os_toascii[what];
#endif /*CHARSET_EBCDIC*/
*where++ = '%';
*where++ = c2x_table[what >> 4];
*where++ = c2x_table[what & 0xf];
return where;
}
/* escape a string for logging */
API_EXPORT(char *) ap_escape_logitem(pool *p, const char *str)
{
char *ret;
unsigned char *d;
const unsigned char *s;
if (str == NULL)
return NULL;
ret = ap_palloc(p, 4 * strlen(str) + 1); /* Be safe */
d = (unsigned char *)ret;
s = (const unsigned char *)str;
for (; *s; ++s) {
if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
*d++ = '\\';
switch(*s) {
case '\b':
*d++ = 'b';
break;
case '\n':
*d++ = 'n';
break;
case '\r':
*d++ = 'r';
break;
case '\t':
*d++ = 't';
break;
case '\v':
*d++ = 'v';
break;
case '\\':
case '"':
*d++ = *s;
break;
default:
c2x(*s, d);
*d = 'x';
d += 3;
}
}
else
*d++ = *s;
}
*d = '\0';
return ret;
}
API_EXPORT(size_t) ap_escape_errorlog_item(char *dest, const char *source,
size_t buflen)
{
unsigned char *d, *ep;
const unsigned char *s;
if (!source || !buflen) { /* be safe */
return 0;
}
d = (unsigned char *)dest;
s = (const unsigned char *)source;
ep = d + buflen - 1;
for (; d < ep && *s; ++s) {
if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
*d++ = '\\';
if (d >= ep) {
--d;
break;
}
switch(*s) {
case '\b':
*d++ = 'b';
break;
case '\n':
*d++ = 'n';
break;
case '\r':
*d++ = 'r';
break;
case '\t':
*d++ = 't';
break;
case '\v':
*d++ = 'v';
break;
case '\\':
*d++ = *s;
break;
case '"': /* no need for this in error log */
d[-1] = *s;
break;
default:
if (d >= ep - 2) {
ep = --d; /* break the for loop as well */
break;
}
c2x(*s, d);
*d = 'x';
d += 3;
}
}
else {
*d++ = *s;
}
}
*d = '\0';
return (d - (unsigned char *)dest);
}
API_EXPORT(char *) ap_escape_shell_cmd(pool *p, const char *str)
{
char *cmd;
unsigned char *d;
const unsigned char *s;
cmd = ap_palloc(p, 2 * strlen(str) + 1); /* Be safe */
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
for (; *s; ++s) {
#if defined(WIN32) || defined(OS2)
/*
* Newlines to Win32/OS2 CreateProcess() are ill advised.
* Convert them to spaces since they are effectively white
* space to most applications
*/
if (*s == '\r' || *s == '\n') {
*d++ = ' ';
continue;
}
#endif
if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
*d++ = '\\';
}
*d++ = *s;
}
*d = '\0';
return cmd;
}
static char x2c(const char *what)
{
register char digit;
#ifndef CHARSET_EBCDIC
digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
#else /*CHARSET_EBCDIC*/
char xstr[5];
xstr[0]='0';
xstr[1]='x';
xstr[2]=what[0];
xstr[3]=what[1];
xstr[4]='\0';
digit = os_toebcdic[0xFF & ap_strtol(xstr, NULL, 16)];
#endif /*CHARSET_EBCDIC*/
return (digit);
}
/*
* Unescapes a URL.
* Returns 0 on success, non-zero on error
* Failure is due to
* bad % escape returns BAD_REQUEST
*
* decoding %00 -> \0 (the null character)
* decoding %2f -> / (a special character)
* returns NOT_FOUND
*/
API_EXPORT(int) ap_unescape_url(char *url)
{
register int x, y, badesc, badpath;
badesc = 0;
badpath = 0;
for (x = 0, y = 0; url[y]; ++x, ++y) {
if (url[y] != '%')
url[x] = url[y];
else {
if (!ap_isxdigit(url[y + 1]) || !ap_isxdigit(url[y + 2])) {
badesc = 1;
url[x] = '%';
}
else {
url[x] = x2c(&url[y + 1]);
y += 2;
if (url[x] == '/' || url[x] == '\0')
badpath = 1;
}
}
}
url[x] = '\0';
if (badesc)
return BAD_REQUEST;
else if (badpath)
return NOT_FOUND;
else
return OK;
}
API_EXPORT(char *) ap_construct_server(pool *p, const char *hostname,
unsigned port, const request_rec *r)
{
if (ap_is_default_port(port, r))
return ap_pstrdup(p, hostname);
else {
return ap_psprintf(p, "%s:%u", hostname, port);
}
}
/*
* escape_path_segment() escapes a path segment, as defined in RFC 1808. This
* routine is (should be) OS independent.
*
* os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
* cases if a ':' occurs before the first '/' in the URL, the URL should be
* prefixed with "./" (or the ':' escaped). In the case of Unix, this means
* leaving '/' alone, but otherwise doing what escape_path_segment() does. For
* efficiency reasons, we don't use escape_path_segment(), which is provided for
* reference. Again, RFC 1808 is where this stuff is defined.
*
* If partial is set, os_escape_path() assumes that the path will be appended to
* something with a '/' in it (and thus does not prefix "./").
*/
API_EXPORT(char *) ap_escape_path_segment(pool *p, const char *segment)
{
char *copy = ap_palloc(p, 3 * strlen(segment) + 1);
const unsigned char *s = (const unsigned char *)segment;
unsigned char *d = (unsigned char *)copy;
unsigned c;
while ((c = *s)) {
if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
d = c2x(c, d);
}
else {
*d++ = c;
}
++s;
}
*d = '\0';
return copy;
}
API_EXPORT(char *) ap_os_escape_path(pool *p, const char *path, int partial)
{
char *copy = ap_palloc(p, 3 * strlen(path) + 3);
const unsigned char *s = (const unsigned char *)path;
unsigned char *d = (unsigned char *)copy;
unsigned c;
if (!partial) {
char *colon = strchr(path, ':');
char *slash = strchr(path, '/');
if (colon && (!slash || colon < slash)) {
*d++ = '.';
*d++ = '/';
}
}
while ((c = *s)) {
if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
d = c2x(c, d);
}
else {
*d++ = c;
}
++s;
}
*d = '\0';
return copy;
}
/* ap_escape_uri is now a macro for os_escape_path */
API_EXPORT(char *) ap_escape_html(pool *p, const char *s)
{
int i, j;
char *x;
/* first, count the number of extra characters */
for (i = 0, j = 0; s[i] != '\0'; i++)
if (s[i] == '<' || s[i] == '>')
j += 3;
else if (s[i] == '&')
j += 4;
if (j == 0)
return ap_pstrndup(p, s, i);
x = ap_palloc(p, i + j + 1);
for (i = 0, j = 0; s[i] != '\0'; i++, j++)
if (s[i] == '<') {
memcpy(&x[j], "<", 4);
j += 3;
}
else if (s[i] == '>') {
memcpy(&x[j], ">", 4);
j += 3;
}
else if (s[i] == '&') {
memcpy(&x[j], "&", 5);
j += 4;
}
else
x[j] = s[i];
x[j] = '\0';
return x;
}
API_EXPORT(int) ap_is_directory(const char *path)
{
struct stat finfo;
if (stat(path, &finfo) == -1)
return 0; /* in error condition, just return no */
return (S_ISDIR(finfo.st_mode));
}
/*
* see ap_is_directory() except this one is symlink aware, so it
* checks for a "real" directory
*/
API_EXPORT(int) ap_is_rdirectory(const char *path)
{
struct stat finfo;
if (lstat(path, &finfo) == -1)
return 0; /* in error condition, just return no */
return ((!(S_ISLNK(finfo.st_mode))) && (S_ISDIR(finfo.st_mode)));
}
API_EXPORT(char *) ap_make_full_path(pool *a, const char *src1,
const char *src2)
{
register int x;
x = strlen(src1);
if (x == 0)
return ap_pstrcat(a, "/", src2, NULL);
if (src1[x - 1] != '/')
return ap_pstrcat(a, src1, "/", src2, NULL);
else
return ap_pstrcat(a, src1, src2, NULL);
}
/*
* Check for an absoluteURI syntax (see section 3.2 in RFC2068).
*/
API_EXPORT(int) ap_is_url(const char *u)
{
register int x;
for (x = 0; u[x] != ':'; x++) {
if ((!u[x]) ||
((!ap_isalpha(u[x])) && (!ap_isdigit(u[x])) &&
(u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
return 0;
}
}
return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
}
API_EXPORT(int) ap_can_exec(const struct stat *finfo)
{
#ifdef MULTIPLE_GROUPS
int cnt;
#endif
#if defined(OS2) || defined(WIN32) || defined(NETWARE)
/* OS/2 dosen't have Users and Groups */
return 1;
#else
if (ap_user_id == finfo->st_uid)
if (finfo->st_mode & S_IXUSR)
return 1;
if (ap_group_id == finfo->st_gid)
if (finfo->st_mode & S_IXGRP)
return 1;
#ifdef MULTIPLE_GROUPS
for (cnt = 0; cnt < NGROUPS_MAX; cnt++) {
if (group_id_list[cnt] == finfo->st_gid)
if (finfo->st_mode & S_IXGRP)
return 1;
}
#endif
return ((finfo->st_mode & S_IXOTH) != 0);
#endif
}
#ifdef NEED_STRDUP
char *strdup(const char *str)
{
char *sdup;
if (!(sdup = (char *) malloc(strlen(str) + 1))) {
fprintf(stderr, "Ouch! Out of memory in our strdup()!\n");
return NULL;
}
sdup = strlcpy(sdup, str, strlen(str) + 1);
return sdup;
}
#endif
/* The following two routines were donated for SVR4 by Andreas Vogel */
#ifdef NEED_STRCASECMP
int strcasecmp(const char *a, const char *b)
{
const char *p = a;
const char *q = b;
for (p = a, q = b; *p && *q; p++, q++) {
int diff = ap_tolower(*p) - ap_tolower(*q);
if (diff)
return diff;
}
if (*p)
return 1; /* p was longer than q */
if (*q)
return -1; /* p was shorter than q */
return 0; /* Exact match */
}
#endif
#ifdef NEED_STRNCASECMP
int strncasecmp(const char *a, const char *b, int n)
{
const char *p = a;
const char *q = b;
for (p = a, q = b; /*NOTHING */ ; p++, q++) {
int diff;
if (p == a + n)
return 0; /* Match up to n characters */
if (!(*p && *q))
return *p - *q;
diff = ap_tolower(*p) - ap_tolower(*q);
if (diff)
return diff;
}
/*NOTREACHED */
}
#endif
/* The following routine was donated for UTS21 by dwd@bell-labs.com */
#ifdef NEED_STRSTR
char *strstr(char *s1, char *s2)
{
char *p1, *p2;
if (*s2 == '\0') {
/* an empty s2 */
return(s1);
}
while((s1 = strchr(s1, *s2)) != NULL) {
/* found first character of s2, see if the rest matches */
p1 = s1;
p2 = s2;
while (*++p1 == *++p2) {
if (*p1 == '\0') {
/* both strings ended together */
return(s1);
}
}
if (*p2 == '\0') {
/* second string ended, a match */
break;
}
/* didn't find a match here, try starting at next character in s1 */
s1++;
}
return(s1);
}
#endif
#ifdef NEED_INITGROUPS
int initgroups(const char *name, gid_t basegid)
{
#if defined(QNX) || defined(MPE) || defined(BEOS) || defined(TPF) || defined(__TANDEM) || defined(NETWARE) || defined(BONE)
/* QNX, MPE and BeOS do not appear to support supplementary groups. */
return 0;
#else /* ndef QNX */
gid_t groups[NGROUPS_MAX];
struct group *g;
int index = 0;
setgrent();
groups[index++] = basegid;
while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
if (g->gr_gid != basegid) {
char **names;
for (names = g->gr_mem; *names != NULL; ++names)
if (!strcmp(*names, name))
groups[index++] = g->gr_gid;
}
endgrent();
return setgroups(index, groups);
#endif /* def QNX */
}
#endif /* def NEED_INITGROUPS */
#ifdef NEED_WAITPID
/* From ikluft@amdahl.com
* this is not ideal but it works for SVR3 variants
* Modified by dwd@bell-labs.com to call wait3 instead of wait because
* apache started to use the WNOHANG option.
*/
int waitpid(pid_t pid, int *statusp, int options)
{
int tmp_pid;
if (kill(pid, 0) == -1) {
errno = ECHILD;
return -1;
}
while (((tmp_pid = wait3(statusp, options, 0)) != pid) &&
(tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
;
return tmp_pid;
}
#endif
API_EXPORT(int) ap_ind(const char *s, char c)
{
register int x;
for (x = 0; s[x]; x++)
if (s[x] == c)
return x;
return -1;
}
API_EXPORT(int) ap_rind(const char *s, char c)
{
register int x;
for (x = strlen(s) - 1; x != -1; x--)
if (s[x] == c)
return x;
return -1;
}
API_EXPORT(void) ap_str_tolower(char *str)
{
while (*str) {
*str = ap_tolower(*str);
++str;
}
}
API_EXPORT(uid_t) ap_uname2id(const char *name)
{
#if defined(WIN32) || defined(NETWARE)
return (1);
#else
struct passwd *ent;
if (name[0] == '#')
return (atoi(&name[1]));
if (!(ent = getpwnam(name))) {
fprintf(stderr, "%s: bad user name %s\n", ap_server_argv0, name);
exit(1);
}
return (ent->pw_uid);
#endif
}
API_EXPORT(gid_t) ap_gname2id(const char *name)
{
#if defined(WIN32) || defined(NETWARE)
return (1);
#else
struct group *ent;
if (name[0] == '#')
return (atoi(&name[1]));
if (!(ent = getgrnam(name))) {
fprintf(stderr, "%s: bad group name %s\n", ap_server_argv0, name);
exit(1);
}
return (ent->gr_gid);
#endif
}
/*
* Parses a host of the form <address>[:port]
* :port is permitted if 'port' is not NULL
*/
API_EXPORT(unsigned long) ap_get_virthost_addr(char *w, unsigned short *ports)
{
struct hostent *hep;
unsigned long my_addr;
char *p;
p = strchr(w, ':');
if (ports != NULL) {
*ports = 0;
if (p != NULL && strcmp(p + 1, "*") != 0)
*ports = atoi(p + 1);
}
if (p != NULL)
*p = '\0';
if (strcmp(w, "*") == 0) {
if (p != NULL)
*p = ':';
return htonl(INADDR_ANY);
}
my_addr = ap_inet_addr((char *)w);
if (my_addr != INADDR_NONE) {
if (p != NULL)
*p = ':';
return my_addr;
}
hep = gethostbyname(w);
if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
fprintf(stderr, "Cannot resolve host name %s --- exiting!\n", w);
exit(1);
}
if (hep->h_addr_list[1]) {
fprintf(stderr, "Host %s has multiple addresses ---\n", w);
fprintf(stderr, "you must choose one explicitly for use as\n");
fprintf(stderr, "a virtual host. Exiting!!!\n");
exit(1);
}
if (p != NULL)
*p = ':';
return ((struct in_addr *) (hep->h_addr))->s_addr;
}
static char *find_fqdn(pool *a, struct hostent *p)
{
int x;
if (!strchr(p->h_name, '.')) {
if (p->h_aliases) {
for (x = 0; p->h_aliases[x]; ++x) {
if (p->h_aliases[x] && strchr(p->h_aliases[x], '.') &&
(!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
return ap_pstrdup(a, p->h_aliases[x]);
}
}
return NULL;
}
return ap_pstrdup(a, (void *) p->h_name);
}
API_EXPORT(char *) ap_get_local_host(pool *a)
{
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif
char str[MAXHOSTNAMELEN];
char *server_hostname = NULL;
struct hostent *p;
#ifdef BEOS /* BeOS returns zero as an error for gethostname */
if (gethostname(str, sizeof(str) - 1) == 0) {
#else
if (gethostname(str, sizeof(str) - 1) != 0) {
#endif /* BeOS */
ap_log_error(APLOG_MARK, APLOG_WARNING, NULL,
"%s: gethostname() failed to determine ServerName\n",
ap_server_argv0);
}
else
{
str[sizeof(str) - 1] = '\0';
if ((!(p = gethostbyname(str)))
|| (!(server_hostname = find_fqdn(a, p)))) {
if (p == NULL || p->h_addr_list == NULL)
server_hostname=NULL;
else {
/* Recovery - return the default servername by IP: */
if (p->h_addr_list[0]) {
ap_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
server_hostname = ap_pstrdup(a, str);
/* We will drop through to report the IP-named server */
}
}
}
else
/* Since we found a fqdn, return it with no logged message. */
return server_hostname;
}
/* If we don't have an fqdn or IP, fall back to the loopback addr */
if (!server_hostname)
server_hostname = ap_pstrdup(a, "127.0.0.1");
ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, NULL,
"%s: Could not determine the server's fully qualified "
"domain name, using %s for ServerName",
ap_server_argv0, server_hostname);
return server_hostname;
}
/* simple 'pool' alloc()ing glue to ap_base64.c
*/
API_EXPORT(char *) ap_pbase64decode(pool *p, const char *bufcoded)
{
char *decoded;
int l;
decoded = (char *) ap_palloc(p, 1 + ap_base64decode_len(bufcoded));
l = ap_base64decode(decoded, bufcoded);
decoded[l] = '\0'; /* make binary sequence into string */
return decoded;
}
API_EXPORT(char *) ap_pbase64encode(pool *p, char *string)
{
char *encoded;
int l = strlen(string);
encoded = (char *) ap_palloc(p, 1 + ap_base64encode_len(l));
l = ap_base64encode(encoded, string, l);
encoded[l] = '\0'; /* make binary sequence into string */
return encoded;
}
/* deprecated names for the above two functions, here for compatibility
*/
API_EXPORT(char *) ap_uudecode(pool *p, const char *bufcoded)
{
return ap_pbase64decode(p, bufcoded);
}
API_EXPORT(char *) ap_uuencode(pool *p, char *string)
{
return ap_pbase64encode(p, string);
}
#if defined(OS2) || defined(WIN32)
/* quotes in the string are doubled up.
* Used to escape quotes in args passed to OS/2's cmd.exe
* and Win32's command.com
*/
API_EXPORT(char *) ap_double_quotes(pool *p, const char *str)
{
int num_quotes = 0;
int len = 0;
char *quote_doubled_str, *dest;
while (str[len]) {
if (str[len++] == '\"') {
num_quotes++;
}
}
quote_doubled_str = ap_palloc(p, len + num_quotes + 1);
dest = quote_doubled_str;
while (*str) {
if (*str == '\"')
*(dest++) = '\"';
*(dest++) = *(str++);
}
*dest = 0;
return quote_doubled_str;
}
/*
* If ap_caret_escape_args resembles ap_escape_shell_cmd, it aught to.
* Taken verbatim so we can trust the integrety of this function.
*/
API_EXPORT(char *) ap_caret_escape_args(pool *p, const char *str)
{
char *cmd;
unsigned char *d;
const unsigned char *s;
cmd = ap_palloc(p, 2 * strlen(str) + 1); /* Be safe */
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
for (; *s; ++s) {
/*
* Newlines to Win32/OS2 CreateProcess() are ill advised.
* Convert them to spaces since they are effectively white
* space to most applications
*/
if (*s == '\r' || *s == '\n') {
*d++ = ' ';
continue;
}
if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
*d++ = '^';
}
*d++ = *s;
}
*d = '\0';
return cmd;
}
#endif
#ifdef NEED_STRERROR
char *
strerror(int err)
{
char *p;
extern char *const sys_errlist[];
p = sys_errlist[err];
return (p);
}
#endif
#if defined(NEED_DIFFTIME)
double difftime(time_t time1, time_t time0)
{
return (time1 - time0);
}
#endif
/* we want to downcase the type/subtype for comparison purposes
* but nothing else because ;parameter=foo values are case sensitive.
* XXX: in truth we want to downcase parameter names... but really,
* apache has never handled parameters and such correctly. You
* also need to compress spaces and such to be able to compare
* properly. -djg
*/
API_EXPORT(void) ap_content_type_tolower(char *str)
{
char *semi;
semi = strchr(str, ';');
if (semi) {
*semi = '\0';
}
while (*str) {
*str = ap_tolower(*str);
++str;
}
if (semi) {
*semi = ';';
}
}
/*
* Given a string, replace any bare " with \" .
*/
API_EXPORT(char *) ap_escape_quotes (pool *p, const char *instring)
{
int newlen = 0;
const char *inchr = instring;
char *outchr, *outstring;
/*
* Look through the input string, jogging the length of the output
* string up by an extra byte each time we find an unescaped ".
*/
while (*inchr != '\0') {
newlen++;
if (*inchr == '"') {
newlen++;
}
/*
* If we find a slosh, and it's not the last byte in the string,
* it's escaping something - advance past both bytes.
*/
if ((*inchr == '\\') && (inchr[1] != '\0')) {
inchr++;
newlen++;
}
inchr++;
}
outstring = ap_palloc(p, newlen + 1);
inchr = instring;
outchr = outstring;
/*
* Now copy the input string to the output string, inserting a slosh
* in front of every " that doesn't already have one.
*/
while (*inchr != '\0') {
if ((*inchr == '\\') && (inchr[1] != '\0')) {
*outchr++ = *inchr++;
*outchr++ = *inchr++;
}
if (*inchr == '"') {
*outchr++ = '\\';
}
if (*inchr != '\0') {
*outchr++ = *inchr++;
}
}
*outchr = '\0';
return outstring;
}
/* dest = src with whitespace removed
* length of dest assumed >= length of src
*/
API_EXPORT(void) ap_remove_spaces(char *dest, char *src)
{
while (*src) {
if (!ap_isspace(*src))
*dest++ = *src;
src++;
}
*dest = 0;
}
|