upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / server / util.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * util.c: string utility things
19  * 
20  * 3/21/93 Rob McCool
21  * 1995-96 Many changes by the Apache Software Foundation
22  * 
23  */
24
25 /* Debugging aid:
26  * #define DEBUG            to trace all cfg_open*()/cfg_closefile() calls
27  * #define DEBUG_CFG_LINES  to trace every line read from the config files
28  */
29
30 #include "apr.h"
31 #include "apr_strings.h"
32 #include "apr_lib.h"
33
34 #define APR_WANT_STDIO
35 #define APR_WANT_STRFUNC
36 #include "apr_want.h"
37
38 #if APR_HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #if APR_HAVE_NETDB_H
42 #include <netdb.h>              /* for gethostbyname() */
43 #endif
44
45 #define CORE_PRIVATE
46
47 #include "ap_config.h"
48 #include "apr_base64.h"
49 #include "httpd.h"
50 #include "http_main.h"
51 #include "http_log.h"
52 #include "http_protocol.h"
53 #include "http_config.h"
54 #include "util_ebcdic.h"
55
56 #ifdef HAVE_PWD_H
57 #include <pwd.h>
58 #endif
59 #ifdef HAVE_GRP_H
60 #include <grp.h>
61 #endif
62
63 /* A bunch of functions in util.c scan strings looking for certain characters.
64  * To make that more efficient we encode a lookup table.  The test_char_table
65  * is generated automatically by gen_test_char.c.
66  */
67 #include "test_char.h"
68
69 /* we assume the folks using this ensure 0 <= c < 256... which means
70  * you need a cast to (unsigned char) first, you can't just plug a
71  * char in here and get it to work, because if char is signed then it
72  * will first be sign extended.
73  */
74 #define TEST_CHAR(c, f)        (test_char_table[(unsigned)(c)] & (f))
75
76 /* Win32/NetWare/OS2 need to check for both forward and back slashes
77  * in ap_getparents() and ap_escape_url.
78  */
79 #ifdef CASE_BLIND_FILESYSTEM
80 #define IS_SLASH(s) ((s == '/') || (s == '\\'))
81 #else
82 #define IS_SLASH(s) (s == '/')
83 #endif
84
85
86 /*
87  * Examine a field value (such as a media-/content-type) string and return
88  * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
89  */
90 AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
91 {
92     const char *semi;
93
94     if (intype == NULL) return NULL;
95
96     semi = ap_strchr_c(intype, ';');
97     if (semi == NULL) {
98         return apr_pstrdup(p, intype);
99     } 
100     else {
101         while ((semi > intype) && apr_isspace(semi[-1])) {
102             semi--;
103         }
104         return apr_pstrndup(p, intype, semi - intype);
105     }
106 }
107
108 AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
109                               int gmt)
110 {
111     apr_size_t retcode;
112     char ts[MAX_STRING_LEN];
113     char tf[MAX_STRING_LEN];
114     apr_time_exp_t xt;
115
116     if (gmt) {
117         const char *f;
118         char *strp;
119
120         apr_time_exp_gmt(&xt, t);
121         /* Convert %Z to "GMT" and %z to "+0000";
122          * on hosts that do not have a time zone string in struct tm,
123          * strftime must assume its argument is local time.
124          */
125         for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
126             ; f++, strp++) {
127             if (*f != '%') continue;
128             switch (f[1]) {
129             case '%':
130                 *++strp = *++f;
131                 break;
132             case 'Z':
133                 *strp++ = 'G';
134                 *strp++ = 'M';
135                 *strp = 'T';
136                 f++;
137                 break;
138             case 'z': /* common extension */
139                 *strp++ = '+';
140                 *strp++ = '0';
141                 *strp++ = '0';
142                 *strp++ = '0';
143                 *strp = '0';
144                 f++;
145                 break;
146             }
147         }
148         *strp = '\0';
149         fmt = tf;
150     }
151     else {
152         apr_time_exp_lt(&xt, t);
153     }
154
155     /* check return code? */
156     apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
157     ts[MAX_STRING_LEN - 1] = '\0';
158     return apr_pstrdup(p, ts);
159 }
160
161 /* Roy owes Rob beer. */
162 /* Rob owes Roy dinner. */
163
164 /* These legacy comments would make a lot more sense if Roy hadn't
165  * replaced the old later_than() routine with util_date.c.
166  *
167  * Well, okay, they still wouldn't make any sense.
168  */
169
170 /* Match = 0, NoMatch = 1, Abort = -1
171  * Based loosely on sections of wildmat.c by Rich Salz
172  * Hmmm... shouldn't this really go component by component?
173  */
174 AP_DECLARE(int) ap_strcmp_match(const char *str, const char *expected)
175 {
176     int x, y;
177
178     for (x = 0, y = 0; expected[y]; ++y, ++x) {
179         if ((!str[x]) && (expected[y] != '*'))
180             return -1;
181         if (expected[y] == '*') {
182             while (expected[++y] == '*');
183             if (!expected[y])
184                 return 0;
185             while (str[x]) {
186                 int ret;
187                 if ((ret = ap_strcmp_match(&str[x++], &expected[y])) != 1)
188                     return ret;
189             }
190             return -1;
191         }
192         else if ((expected[y] != '?') && (str[x] != expected[y]))
193             return 1;
194     }
195     return (str[x] != '\0');
196 }
197
198 AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *expected)
199 {
200     int x, y;
201
202     for (x = 0, y = 0; expected[y]; ++y, ++x) {
203         if (!str[x] && expected[y] != '*')
204             return -1;
205         if (expected[y] == '*') {
206             while (expected[++y] == '*');
207             if (!expected[y])
208                 return 0;
209             while (str[x]) {
210                 int ret;
211                 if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1)
212                     return ret;
213             }
214             return -1;
215         }
216         else if (expected[y] != '?'
217                  && apr_tolower(str[x]) != apr_tolower(expected[y]))
218             return 1;
219     }
220     return (str[x] != '\0');
221 }
222
223 /* We actually compare the canonical root to this root, (but we don't
224  * waste time checking the case), since every use of this function in 
225  * httpd-2.0 tests if the path is 'proper', meaning we've already passed
226  * it through apr_filepath_merge, or we haven't.
227  */
228 AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir) 
229 {
230     const char *newpath;
231     const char *ourdir = dir;
232     if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
233             || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
234         return 0;
235     }
236     return 1;
237 }
238
239 AP_DECLARE(int) ap_is_matchexp(const char *str)
240 {
241     register int x;
242
243     for (x = 0; str[x]; x++)
244         if ((str[x] == '*') || (str[x] == '?'))
245             return 1;
246     return 0;
247 }
248
249 /*
250  * Here's a pool-based interface to POSIX regex's regcomp().
251  * Note that we return regex_t instead of being passed one.
252  * The reason is that if you use an already-used regex_t structure,
253  * the memory that you've already allocated gets forgotten, and
254  * regfree() doesn't clear it. So we don't allow it.
255  */
256
257 static apr_status_t regex_cleanup(void *preg)
258 {
259     regfree((regex_t *) preg);
260     return APR_SUCCESS;
261 }
262
263 AP_DECLARE(regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
264                                    int cflags)
265 {
266     regex_t *preg = apr_palloc(p, sizeof(regex_t));
267
268     if (regcomp(preg, pattern, cflags)) {
269         return NULL;
270     }
271
272     apr_pool_cleanup_register(p, (void *) preg, regex_cleanup,
273                               apr_pool_cleanup_null);
274
275     return preg;
276 }
277
278 AP_DECLARE(void) ap_pregfree(apr_pool_t *p, regex_t * reg)
279 {
280     regfree(reg);
281     apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
282 }
283
284 /*
285  * Similar to standard strstr() but we ignore case in this version.
286  * Based on the strstr() implementation further below.
287  */
288 AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
289 {
290     char *p1, *p2;
291     if (*s2 == '\0') {
292         /* an empty s2 */
293         return((char *)s1);
294     }
295     while(1) {
296         for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
297         if (*s1 == '\0') {
298             return(NULL);
299         }
300         /* found first character of s2, see if the rest matches */
301         p1 = (char *)s1;
302         p2 = (char *)s2;
303         for (++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
304             if (*p1 == '\0') {
305                 /* both strings ended together */
306                 return((char *)s1);
307             }
308         }
309         if (*p2 == '\0') {
310             /* second string ended, a match */
311             break;
312         }
313         /* didn't find a match here, try starting at next character in s1 */
314         s1++;
315     }
316     return((char *)s1);
317 }
318
319 /*
320  * Returns an offsetted pointer in bigstring immediately after
321  * prefix. Returns bigstring if bigstring doesn't start with
322  * prefix or if prefix is longer than bigstring while still matching.
323  * NOTE: pointer returned is relative to bigstring, so we
324  * can use standard pointer comparisons in the calling function
325  * (eg: test if ap_stripprefix(a,b) == a)
326  */
327 AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
328                                         const char *prefix)
329 {
330     const char *p1;
331
332     if (*prefix == '\0')
333         return bigstring;
334
335     p1 = bigstring;
336     while (*p1 && *prefix) {
337         if (*p1++ != *prefix++)
338             return bigstring;
339     }
340     if (*prefix == '\0')
341         return p1;
342
343     /* hit the end of bigstring! */
344     return bigstring;
345 }
346
347 /* 
348  * Apache stub function for the regex libraries regexec() to make sure the
349  * whole regex(3) API is available through the Apache (exported) namespace.
350  * This is especially important for the DSO situations of modules.
351  * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
352  */
353 AP_DECLARE(int) ap_regexec(regex_t *preg, const char *string,
354                            size_t nmatch, regmatch_t pmatch[], int eflags)
355 {
356     return regexec(preg, string, nmatch, pmatch, eflags);
357 }
358
359 AP_DECLARE(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf,
360                                size_t errbuf_size)
361 {
362     return regerror(errcode, preg, errbuf, errbuf_size);
363 }
364
365
366 /* This function substitutes for $0-$9, filling in regular expression
367  * submatches. Pass it the same nmatch and pmatch arguments that you
368  * passed ap_regexec(). pmatch should not be greater than the maximum number
369  * of subexpressions - i.e. one more than the re_nsub member of regex_t.
370  *
371  * input should be the string with the $-expressions, source should be the
372  * string that was matched against.
373  *
374  * It returns the substituted string, or NULL on error.
375  *
376  * Parts of this code are based on Henry Spencer's regsub(), from his
377  * AT&T V8 regexp package.
378  */
379
380 AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input,
381                               const char *source, size_t nmatch,
382                               regmatch_t pmatch[])
383 {
384     const char *src = input;
385     char *dest, *dst;
386     char c;
387     size_t no;
388     int len;
389
390     if (!source)
391         return NULL;
392     if (!nmatch)
393         return apr_pstrdup(p, src);
394
395     /* First pass, find the size */
396
397     len = 0;
398
399     while ((c = *src++) != '\0') {
400         if (c == '&')
401             no = 0;
402         else if (c == '$' && apr_isdigit(*src))
403             no = *src++ - '0';
404         else
405             no = 10;
406
407         if (no > 9) {                /* Ordinary character. */
408             if (c == '\\' && (*src == '$' || *src == '&'))
409                 c = *src++;
410             len++;
411         }
412         else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
413             len += pmatch[no].rm_eo - pmatch[no].rm_so;
414         }
415
416     }
417
418     dest = dst = apr_pcalloc(p, len + 1);
419
420     /* Now actually fill in the string */
421
422     src = input;
423
424     while ((c = *src++) != '\0') {
425         if (c == '&')
426             no = 0;
427         else if (c == '$' && apr_isdigit(*src))
428             no = *src++ - '0';
429         else
430             no = 10;
431
432         if (no > 9) {                /* Ordinary character. */
433             if (c == '\\' && (*src == '$' || *src == '&'))
434                 c = *src++;
435             *dst++ = c;
436         }
437         else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
438             len = pmatch[no].rm_eo - pmatch[no].rm_so;
439             memcpy(dst, source + pmatch[no].rm_so, len);
440             dst += len;
441         }
442
443     }
444     *dst = '\0';
445
446     return dest;
447 }
448
449 /*
450  * Parse .. so we don't compromise security
451  */
452 AP_DECLARE(void) ap_getparents(char *name)
453 {
454     char *next;
455     int l, w, first_dot;
456
457     /* Four paseses, as per RFC 1808 */
458     /* a) remove ./ path segments */
459     for (next = name; *next && (*next != '.'); next++) {
460     }
461     
462     l = w = first_dot = next - name;
463     while (name[l] != '\0') {
464         if (name[l] == '.' && IS_SLASH(name[l + 1])
465             && (l == 0 || IS_SLASH(name[l - 1])))
466             l += 2;
467         else
468             name[w++] = name[l++];
469     }
470
471     /* b) remove trailing . path, segment */
472     if (w == 1 && name[0] == '.')
473         w--;
474     else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
475         w--;
476     name[w] = '\0';
477
478     /* c) remove all xx/../ segments. (including leading ../ and /../) */
479     l = first_dot;
480
481     while (name[l] != '\0') {
482         if (name[l] == '.' && name[l + 1] == '.' && IS_SLASH(name[l + 2])
483             && (l == 0 || IS_SLASH(name[l - 1]))) {
484             register int m = l + 3, n;
485
486             l = l - 2;
487             if (l >= 0) {
488                 while (l >= 0 && !IS_SLASH(name[l]))
489                     l--;
490                 l++;
491             }
492             else
493                 l = 0;
494             n = l;
495             while ((name[n] = name[m]))
496                 (++n, ++m);
497         }
498         else
499             ++l;
500     }
501
502     /* d) remove trailing xx/.. segment. */
503     if (l == 2 && name[0] == '.' && name[1] == '.')
504         name[0] = '\0';
505     else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
506              && IS_SLASH(name[l - 3])) {
507         l = l - 4;
508         if (l >= 0) {
509             while (l >= 0 && !IS_SLASH(name[l]))
510                 l--;
511             l++;
512         }
513         else
514             l = 0;
515         name[l] = '\0';
516     }
517 }
518
519 AP_DECLARE(void) ap_no2slash(char *name)
520 {
521     char *d, *s;
522
523     s = d = name;
524
525 #ifdef HAVE_UNC_PATHS
526     /* Check for UNC names.  Leave leading two slashes. */
527     if (s[0] == '/' && s[1] == '/')
528         *d++ = *s++;
529 #endif
530
531     while (*s) {
532         if ((*d++ = *s) == '/') {
533             do {
534                 ++s;
535             } while (*s == '/');
536         }
537         else {
538             ++s;
539         }
540     }
541     *d = '\0';
542 }
543
544
545 /*
546  * copy at most n leading directories of s into d
547  * d should be at least as large as s plus 1 extra byte
548  * assumes n > 0
549  * the return value is the ever useful pointer to the trailing \0 of d
550  *
551  * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments, 
552  * so that if n == 0, "/" is returned in d with n == 1 
553  * and s == "e:/test.html", "e:/" is returned in d
554  * *** See also directory_walk in modules/http/http_request.c
555
556  * examples:
557  *    /a/b, 0  ==> /  (true for all platforms)
558  *    /a/b, 1  ==> /
559  *    /a/b, 2  ==> /a/
560  *    /a/b, 3  ==> /a/b/
561  *    /a/b, 4  ==> /a/b/
562  *
563  *    c:/a/b 0 ==> /
564  *    c:/a/b 1 ==> c:/
565  *    c:/a/b 2 ==> c:/a/
566  *    c:/a/b 3 ==> c:/a/b
567  *    c:/a/b 4 ==> c:/a/b
568  */
569 AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
570 {
571     if (n < 1) {
572         *d = '/';
573         *++d = '\0';
574         return (d);
575     }
576
577     for (;;) {
578         if (*s == '\0' || (*s == '/' && (--n) == 0)) {
579             *d = '/';
580             break;
581         }
582         *d++ = *s++;
583     }
584     *++d = 0;
585     return (d);
586 }
587
588
589 /*
590  * return the parent directory name including trailing / of the file s
591  */
592 AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
593 {
594     const char *last_slash = ap_strrchr_c(s, '/');
595     char *d;
596     int l;
597
598     if (last_slash == NULL) {
599         return apr_pstrdup(p, "");
600     }
601     l = (last_slash - s) + 1;
602     d = apr_palloc(p, l + 1);
603     memcpy(d, s, l);
604     d[l] = 0;
605     return (d);
606 }
607
608
609 AP_DECLARE(int) ap_count_dirs(const char *path)
610 {
611     register int x, n;
612
613     for (x = 0, n = 0; path[x]; x++)
614         if (path[x] == '/')
615             n++;
616     return n;
617 }
618
619 AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
620 {
621     return ap_getword(atrans, (const char **) line, stop);
622 }
623
624 AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
625 {
626     const char *pos = *line;
627     int len;
628     char *res;
629
630     while ((*pos != stop) && *pos) {
631         ++pos;
632     }
633
634     len = pos - *line;
635     res = (char *)apr_palloc(atrans, len + 1);
636     memcpy(res, *line, len);
637     res[len] = 0;
638
639     if (stop) {
640         while (*pos == stop) {
641             ++pos;
642         }
643     }
644     *line = pos;
645
646     return res;
647 }
648
649 AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
650 {
651     return ap_getword_white(atrans, (const char **) line);
652 }
653
654 AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
655 {
656     const char *pos = *line;
657     int len;
658     char *res;
659
660     while (!apr_isspace(*pos) && *pos) {
661         ++pos;
662     }
663
664     len = pos - *line;
665     res = (char *)apr_palloc(atrans, len + 1);
666     memcpy(res, *line, len);
667     res[len] = 0;
668
669     while (apr_isspace(*pos)) {
670         ++pos;
671     }
672
673     *line = pos;
674
675     return res;
676 }
677
678 AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line,
679                                        char stop)
680 {
681     return ap_getword_nulls(atrans, (const char **) line, stop);
682 }
683
684 AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line,
685                                     char stop)
686 {
687     const char *pos = ap_strchr_c(*line, stop);
688     char *res;
689
690     if (!pos) {
691         res = apr_pstrdup(atrans, *line);
692         *line += strlen(*line);
693         return res;
694     }
695
696     res = apr_pstrndup(atrans, *line, pos - *line);
697
698     ++pos;
699
700     *line = pos;
701
702     return res;
703 }
704
705 /* Get a word, (new) config-file style --- quoted strings and backslashes
706  * all honored
707  */
708
709 static char *substring_conf(apr_pool_t *p, const char *start, int len,
710                             char quote)
711 {
712     char *result = apr_palloc(p, len + 2);
713     char *resp = result;
714     int i;
715
716     for (i = 0; i < len; ++i) {
717         if (start[i] == '\\' && (start[i + 1] == '\\'
718                                  || (quote && start[i + 1] == quote)))
719             *resp++ = start[++i];
720         else
721             *resp++ = start[i];
722     }
723
724     *resp++ = '\0';
725 #if RESOLVE_ENV_PER_TOKEN
726     return (char *)ap_resolve_env(p,result);
727 #else
728     return result;
729 #endif
730 }
731
732 AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
733 {
734     return ap_getword_conf(p, (const char **) line);
735 }
736
737 AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
738 {
739     const char *str = *line, *strend;
740     char *res;
741     char quote;
742
743     while (*str && apr_isspace(*str))
744         ++str;
745
746     if (!*str) {
747         *line = str;
748         return "";
749     }
750
751     if ((quote = *str) == '"' || quote == '\'') {
752         strend = str + 1;
753         while (*strend && *strend != quote) {
754             if (*strend == '\\' && strend[1] && strend[1] == quote)
755                 strend += 2;
756             else
757                 ++strend;
758         }
759         res = substring_conf(p, str + 1, strend - str - 1, quote);
760
761         if (*strend == quote)
762             ++strend;
763     }
764     else {
765         strend = str;
766         while (*strend && !apr_isspace(*strend))
767             ++strend;
768
769         res = substring_conf(p, str, strend - str, 0);
770     }
771
772     while (*strend && apr_isspace(*strend))
773         ++strend;
774     *line = strend;
775     return res;
776 }
777
778 /* Check a string for any ${ENV} environment variable
779  * construct and replace each them by the value of
780  * that environment variable, if it exists. If the
781  * environment value does not exist, leave the ${ENV}
782  * construct alone; it means something else.
783  */
784 AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
785 {
786 # define SMALL_EXPANSION 5
787     struct sll {
788         struct sll *next;
789         const char *string;
790         apr_size_t len;
791     } *result, *current, sresult[SMALL_EXPANSION];
792     char *res_buf, *cp;
793     const char *s, *e, *ep;
794     unsigned spc;
795     apr_size_t outlen;
796
797     s = ap_strchr_c(word, '$');
798     if (!s) {
799         return word;
800     }
801
802     /* well, actually something to do */
803     ep = word + strlen(word);
804     spc = 0;
805     result = current = &(sresult[spc++]);
806     current->next = NULL;
807     current->string = word;
808     current->len = s - word;
809     outlen = current->len;
810
811     do {
812         /* prepare next entry */
813         if (current->len) {
814             current->next = (spc < SMALL_EXPANSION)
815                             ? &(sresult[spc++])
816                             : (struct sll *)apr_palloc(p,
817                                                        sizeof(*current->next));
818             current = current->next;
819             current->next = NULL;
820             current->len = 0;
821         }
822
823         if (*s == '$') {
824             if (s[1] == '{' && (e = ap_strchr_c(s, '}'))) {
825                 word = getenv(apr_pstrndup(p, s+2, e-s-2));
826                 if (word) {
827                     current->string = word;
828                     current->len = strlen(word);
829                     outlen += current->len;
830                 }
831                 else {
832                     current->string = s;
833                     current->len = e - s + 1;
834                     outlen += current->len;
835                 }
836                 s = e + 1;
837             }
838             else {
839                 current->string = s++;
840                 current->len = 1;
841                 ++outlen;
842             }
843         }
844         else {
845             word = s;
846             s = ap_strchr_c(s, '$');
847             current->string = word;
848             current->len = s ? s - word : ep - word;
849             outlen += current->len;
850         }
851     } while (s && *s);
852
853     /* assemble result */
854     res_buf = cp = apr_palloc(p, outlen + 1);
855     do {
856         if (result->len) {
857             memcpy(cp, result->string, result->len);
858             cp += result->len;
859         }
860         result = result->next;
861     } while (result);
862     res_buf[outlen] = '\0';
863
864     return res_buf;
865 }
866
867 AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
868 {
869 #ifdef DEBUG
870     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, 
871         "Done with config file %s", cfp->name);
872 #endif
873     return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
874 }
875
876 static apr_status_t cfg_close(void *param)
877 {
878     apr_file_t *cfp = (apr_file_t *) param;
879     return (apr_file_close(cfp));
880 }
881
882 static int cfg_getch(void *param)
883 {
884     char ch;
885     apr_file_t *cfp = (apr_file_t *) param;
886     if (apr_file_getc(&ch, cfp) == APR_SUCCESS)
887         return ch;
888     return (int)EOF;
889 }
890
891 static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
892 {
893     apr_file_t *cfp = (apr_file_t *) param;
894     apr_status_t rv;
895     rv = apr_file_gets(buf, bufsiz, cfp);
896     if (rv == APR_SUCCESS || (APR_STATUS_IS_EOF(rv) && strcmp(buf, "")))
897         return buf;
898     return NULL;
899 }
900
901 /* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
902 AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg,
903                                           apr_pool_t *p, const char *name)
904 {
905     ap_configfile_t *new_cfg;
906     apr_file_t *file = NULL;
907     apr_finfo_t finfo;
908     apr_status_t status;
909 #ifdef DEBUG
910     char buf[120];
911 #endif
912
913     if (name == NULL) {
914         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
915                "Internal error: pcfg_openfile() called with NULL filename");
916         return APR_EBADF;
917     }
918
919     status = apr_file_open(&file, name, APR_READ | APR_BUFFERED,
920                            APR_OS_DEFAULT, p);
921 #ifdef DEBUG
922     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
923                 "Opening config file %s (%s)",
924                 name, (status != APR_SUCCESS) ? 
925                 apr_strerror(status, buf, sizeof(buf)) : "successful");
926 #endif
927     if (status != APR_SUCCESS)
928         return status;
929
930     status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
931     if (status != APR_SUCCESS)
932         return status;
933
934     if (finfo.filetype != APR_REG &&
935 #if defined(WIN32) || defined(OS2) || defined(NETWARE)
936         strcasecmp(apr_filename_of_pathname(name), "nul") != 0) {
937 #else
938         strcmp(name, "/dev/null") != 0) {
939 #endif /* WIN32 || OS2 */
940         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
941                      "Access to file %s denied by server: not a regular file",
942                      name);
943         apr_file_close(file);
944         return APR_EBADF;
945     }
946
947 #ifdef WIN32
948     /* Some twisted character [no pun intended] at MS decided that a
949      * zero width joiner as the lead wide character would be ideal for
950      * describing Unicode text files.  This was further convoluted to
951      * another MSism that the same character mapped into utf-8, EF BB BF
952      * would signify utf-8 text files.
953      *
954      * Since MS configuration files are all protecting utf-8 encoded
955      * Unicode path, file and resource names, we already have the correct 
956      * WinNT encoding.  But at least eat the stupid three bytes up front.
957      */
958     {
959         unsigned char buf[4];
960         apr_size_t len = 3;
961         status = apr_file_read(file, buf, &len);
962         if ((status != APR_SUCCESS) || (len < 3) 
963               || memcmp(buf, "\xEF\xBB\xBF", 3) != 0) {
964             apr_off_t zero = 0;
965             apr_file_seek(file, APR_SET, &zero);
966         }
967     }
968 #endif
969
970     new_cfg = apr_palloc(p, sizeof(*new_cfg));
971     new_cfg->param = file;
972     new_cfg->name = apr_pstrdup(p, name);
973     new_cfg->getch = (int (*)(void *)) cfg_getch;
974     new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
975     new_cfg->close = (int (*)(void *)) cfg_close;
976     new_cfg->line_number = 0;
977     *ret_cfg = new_cfg;
978     return APR_SUCCESS;
979 }
980
981
982 /* Allocate a ap_configfile_t handle with user defined functions and params */
983 AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(apr_pool_t *p,
984                        const char *descr,
985                        void *param,
986                        int(*getch)(void *param),
987                        void *(*getstr) (void *buf, size_t bufsiz, void *param),
988                        int(*close_func)(void *param))
989 {
990     ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
991 #ifdef DEBUG
992     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
993                  "Opening config handler %s", descr);
994 #endif
995     new_cfg->param = param;
996     new_cfg->name = descr;
997     new_cfg->getch = getch;
998     new_cfg->getstr = getstr;
999     new_cfg->close = close_func;
1000     new_cfg->line_number = 0;
1001     return new_cfg;
1002 }
1003    
1004 /* Read one character from a configfile_t */
1005 AP_DECLARE(int) ap_cfg_getc(ap_configfile_t *cfp)
1006 {
1007     register int ch = cfp->getch(cfp->param);
1008     if (ch == LF) 
1009         ++cfp->line_number;
1010     return ch;
1011 }
1012   
1013 /* Read one line from open ap_configfile_t, strip LF, increase line number */
1014 /* If custom handler does not define a getstr() function, read char by char */
1015 AP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, ap_configfile_t *cfp)
1016 {
1017     /* If a "get string" function is defined, use it */
1018     if (cfp->getstr != NULL) {
1019         char *src, *dst;
1020         char *cp;
1021         char *cbuf = buf;
1022         size_t cbufsize = bufsize;
1023
1024         while (1) {
1025             ++cfp->line_number;
1026             if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
1027                 return 1;
1028
1029             /*
1030              *  check for line continuation,
1031              *  i.e. match [^\\]\\[\r]\n only
1032              */
1033             cp = cbuf;
1034             while (cp < cbuf+cbufsize && *cp != '\0')
1035                 cp++;
1036             if (cp > cbuf && cp[-1] == LF) {
1037                 cp--;
1038                 if (cp > cbuf && cp[-1] == CR)
1039                     cp--;
1040                 if (cp > cbuf && cp[-1] == '\\') {
1041                     cp--;
1042                     if (!(cp > cbuf && cp[-1] == '\\')) {
1043                         /*
1044                          * line continuation requested -
1045                          * then remove backslash and continue
1046                          */
1047                         cbufsize -= (cp-cbuf);
1048                         cbuf = cp;
1049                         continue;
1050                     }
1051                     else {
1052                         /* 
1053                          * no real continuation because escaped -
1054                          * then just remove escape character
1055                          */
1056                         for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
1057                             cp[0] = cp[1];
1058                     }   
1059                 }
1060             }
1061             break;
1062         }
1063
1064         /*
1065          * Leading and trailing white space is eliminated completely
1066          */
1067         src = buf;
1068         while (apr_isspace(*src))
1069             ++src;
1070         /* blast trailing whitespace */
1071         dst = &src[strlen(src)];
1072         while (--dst >= src && apr_isspace(*dst))
1073             *dst = '\0';
1074         /* Zap leading whitespace by shifting */
1075         if (src != buf)
1076             for (dst = buf; (*dst++ = *src++) != '\0'; )
1077                 ;
1078
1079 #ifdef DEBUG_CFG_LINES
1080         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, "Read config: %s", buf);
1081 #endif
1082         return 0;
1083     } else {
1084         /* No "get string" function defined; read character by character */
1085         register int c;
1086         register size_t i = 0;
1087
1088         buf[0] = '\0';
1089         /* skip leading whitespace */
1090         do {
1091             c = cfp->getch(cfp->param);
1092         } while (c == '\t' || c == ' ');
1093
1094         if (c == EOF)
1095             return 1;
1096         
1097         if(bufsize < 2) {
1098             /* too small, assume caller is crazy */
1099             return 1;
1100         }
1101
1102         while (1) {
1103             if ((c == '\t') || (c == ' ')) {
1104                 buf[i++] = ' ';
1105                 while ((c == '\t') || (c == ' '))
1106                     c = cfp->getch(cfp->param);
1107             }
1108             if (c == CR) {
1109                 /* silently ignore CR (_assume_ that a LF follows) */
1110                 c = cfp->getch(cfp->param);
1111             }
1112             if (c == LF) {
1113                 /* increase line number and return on LF */
1114                 ++cfp->line_number;
1115             }
1116             if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
1117                 /* 
1118                  *  check for line continuation
1119                  */
1120                 if (i > 0 && buf[i-1] == '\\') {
1121                     i--;
1122                     if (!(i > 0 && buf[i-1] == '\\')) {
1123                         /* line is continued */
1124                         c = cfp->getch(cfp->param);
1125                         continue;
1126                     }
1127                     /* else nothing needs be done because
1128                      * then the backslash is escaped and
1129                      * we just strip to a single one
1130                      */
1131                 }
1132                 /* blast trailing whitespace */
1133                 while (i > 0 && apr_isspace(buf[i - 1]))
1134                     --i;
1135                 buf[i] = '\0';
1136 #ifdef DEBUG_CFG_LINES
1137                 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
1138                              "Read config: %s", buf);
1139 #endif
1140                 return 0;
1141             }
1142             buf[i] = c;
1143             ++i;
1144             c = cfp->getch(cfp->param);
1145         }
1146     }
1147 }
1148
1149 /* Size an HTTP header field list item, as separated by a comma.
1150  * The return value is a pointer to the beginning of the non-empty list item
1151  * within the original string (or NULL if there is none) and the address
1152  * of field is shifted to the next non-comma, non-whitespace character.
1153  * len is the length of the item excluding any beginning whitespace.
1154  */
1155 AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1156 {
1157     const unsigned char *ptr = (const unsigned char *)*field;
1158     const unsigned char *token;
1159     int in_qpair, in_qstr, in_com;
1160
1161     /* Find first non-comma, non-whitespace byte */
1162
1163     while (*ptr == ',' || apr_isspace(*ptr))
1164         ++ptr;
1165
1166     token = ptr;
1167
1168     /* Find the end of this item, skipping over dead bits */
1169
1170     for (in_qpair = in_qstr = in_com = 0;
1171          *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1172          ++ptr) {
1173
1174         if (in_qpair) {
1175             in_qpair = 0;
1176         }
1177         else {
1178             switch (*ptr) {
1179                 case '\\': in_qpair = 1;      /* quoted-pair         */
1180                            break;
1181                 case '"' : if (!in_com)       /* quoted string delim */
1182                                in_qstr = !in_qstr;
1183                            break;
1184                 case '(' : if (!in_qstr)      /* comment (may nest)  */
1185                                ++in_com;
1186                            break;
1187                 case ')' : if (in_com)        /* end comment         */
1188                                --in_com;
1189                            break;
1190                 default  : break;
1191             }
1192         }
1193     }
1194
1195     if ((*len = (ptr - token)) == 0) {
1196         *field = (const char *)ptr;
1197         return NULL;
1198     }
1199
1200     /* Advance field pointer to the next non-comma, non-white byte */
1201
1202     while (*ptr == ',' || apr_isspace(*ptr))
1203         ++ptr;
1204
1205     *field = (const char *)ptr;
1206     return (const char *)token;
1207 }
1208
1209 /* Retrieve an HTTP header field list item, as separated by a comma,
1210  * while stripping insignificant whitespace and lowercasing anything not in
1211  * a quoted string or comment.  The return value is a new string containing
1212  * the converted list item (or NULL if none) and the address pointed to by
1213  * field is shifted to the next non-comma, non-whitespace.
1214  */
1215 AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1216 {
1217     const char *tok_start;
1218     const unsigned char *ptr;
1219     unsigned char *pos;
1220     char *token;
1221     int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1222
1223     /* Find the beginning and maximum length of the list item so that
1224      * we can allocate a buffer for the new string and reset the field.
1225      */
1226     if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1227         return NULL;
1228     }
1229     token = apr_palloc(p, tok_len + 1);
1230
1231     /* Scan the token again, but this time copy only the good bytes.
1232      * We skip extra whitespace and any whitespace around a '=', '/',
1233      * or ';' and lowercase normal characters not within a comment,
1234      * quoted-string or quoted-pair.
1235      */
1236     for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1237          *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1238          ++ptr) {
1239
1240         if (in_qpair) {
1241             in_qpair = 0;
1242             *pos++ = *ptr;
1243         }
1244         else {
1245             switch (*ptr) {
1246                 case '\\': in_qpair = 1;
1247                            if (addspace == 1)
1248                                *pos++ = ' ';
1249                            *pos++ = *ptr;
1250                            addspace = 0;
1251                            break;
1252                 case '"' : if (!in_com)
1253                                in_qstr = !in_qstr;
1254                            if (addspace == 1)
1255                                *pos++ = ' ';
1256                            *pos++ = *ptr;
1257                            addspace = 0;
1258                            break;
1259                 case '(' : if (!in_qstr)
1260                                ++in_com;
1261                            if (addspace == 1)
1262                                *pos++ = ' ';
1263                            *pos++ = *ptr;
1264                            addspace = 0;
1265                            break;
1266                 case ')' : if (in_com)
1267                                --in_com;
1268                            *pos++ = *ptr;
1269                            addspace = 0;
1270                            break;
1271                 case ' ' :
1272                 case '\t': if (addspace)
1273                                break;
1274                            if (in_com || in_qstr)
1275                                *pos++ = *ptr;
1276                            else
1277                                addspace = 1;
1278                            break;
1279                 case '=' :
1280                 case '/' :
1281                 case ';' : if (!(in_com || in_qstr))
1282                                addspace = -1;
1283                            *pos++ = *ptr;
1284                            break;
1285                 default  : if (addspace == 1)
1286                                *pos++ = ' ';
1287                            *pos++ = (in_com || in_qstr) ? *ptr
1288                                                         : apr_tolower(*ptr);
1289                            addspace = 0;
1290                            break;
1291             }
1292         }
1293     }
1294     *pos = '\0';
1295
1296     return token;
1297 }
1298
1299 /* Find an item in canonical form (lowercase, no extra spaces) within
1300  * an HTTP field value list.  Returns 1 if found, 0 if not found.
1301  * This would be much more efficient if we stored header fields as
1302  * an array of list items as they are received instead of a plain string.
1303  */
1304 AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line,
1305                                   const char *tok)
1306 {
1307     const unsigned char *pos;
1308     const unsigned char *ptr = (const unsigned char *)line;
1309     int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1310
1311     if (!line || !tok)
1312         return 0;
1313
1314     do {  /* loop for each item in line's list */
1315
1316         /* Find first non-comma, non-whitespace byte */
1317
1318         while (*ptr == ',' || apr_isspace(*ptr))
1319             ++ptr;
1320
1321         if (*ptr)
1322             good = 1;  /* until proven otherwise for this item */
1323         else
1324             break;     /* no items left and nothing good found */
1325
1326         /* We skip extra whitespace and any whitespace around a '=', '/',
1327          * or ';' and lowercase normal characters not within a comment,
1328          * quoted-string or quoted-pair.
1329          */
1330         for (pos = (const unsigned char *)tok;
1331              *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1332              ++ptr) {
1333
1334             if (in_qpair) {
1335                 in_qpair = 0;
1336                 if (good)
1337                     good = (*pos++ == *ptr);
1338             }
1339             else {
1340                 switch (*ptr) {
1341                     case '\\': in_qpair = 1;
1342                                if (addspace == 1)
1343                                    good = good && (*pos++ == ' ');
1344                                good = good && (*pos++ == *ptr);
1345                                addspace = 0;
1346                                break;
1347                     case '"' : if (!in_com)
1348                                    in_qstr = !in_qstr;
1349                                if (addspace == 1)
1350                                    good = good && (*pos++ == ' ');
1351                                good = good && (*pos++ == *ptr);
1352                                addspace = 0;
1353                                break;
1354                     case '(' : if (!in_qstr)
1355                                    ++in_com;
1356                                if (addspace == 1)
1357                                    good = good && (*pos++ == ' ');
1358                                good = good && (*pos++ == *ptr);
1359                                addspace = 0;
1360                                break;
1361                     case ')' : if (in_com)
1362                                    --in_com;
1363                                good = good && (*pos++ == *ptr);
1364                                addspace = 0;
1365                                break;
1366                     case ' ' :
1367                     case '\t': if (addspace || !good)
1368                                    break;
1369                                if (in_com || in_qstr)
1370                                    good = (*pos++ == *ptr);
1371                                else
1372                                    addspace = 1;
1373                                break;
1374                     case '=' :
1375                     case '/' :
1376                     case ';' : if (!(in_com || in_qstr))
1377                                    addspace = -1;
1378                                good = good && (*pos++ == *ptr);
1379                                break;
1380                     default  : if (!good)
1381                                    break;
1382                                if (addspace == 1)
1383                                    good = (*pos++ == ' ');
1384                                if (in_com || in_qstr)
1385                                    good = good && (*pos++ == *ptr);
1386                                else
1387                                    good = good && (*pos++ == apr_tolower(*ptr));
1388                                addspace = 0;
1389                                break;
1390                 }
1391             }
1392         }
1393         if (good && *pos)
1394             good = 0;          /* not good if only a prefix was matched */
1395
1396     } while (*ptr && !good);
1397
1398     return good;
1399 }
1400
1401
1402 /* Retrieve a token, spacing over it and returning a pointer to
1403  * the first non-white byte afterwards.  Note that these tokens
1404  * are delimited by semis and commas; and can also be delimited
1405  * by whitespace at the caller's option.
1406  */
1407
1408 AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line,
1409                                 int accept_white)
1410 {
1411     const char *ptr = *accept_line;
1412     const char *tok_start;
1413     char *token;
1414     int tok_len;
1415
1416     /* Find first non-white byte */
1417
1418     while (*ptr && apr_isspace(*ptr))
1419         ++ptr;
1420
1421     tok_start = ptr;
1422
1423     /* find token end, skipping over quoted strings.
1424      * (comments are already gone).
1425      */
1426
1427     while (*ptr && (accept_white || !apr_isspace(*ptr))
1428            && *ptr != ';' && *ptr != ',') {
1429         if (*ptr++ == '"')
1430             while (*ptr)
1431                 if (*ptr++ == '"')
1432                     break;
1433     }
1434
1435     tok_len = ptr - tok_start;
1436     token = apr_pstrndup(p, tok_start, tok_len);
1437
1438     /* Advance accept_line pointer to the next non-white byte */
1439
1440     while (*ptr && apr_isspace(*ptr))
1441         ++ptr;
1442
1443     *accept_line = ptr;
1444     return token;
1445 }
1446
1447
1448 /* find http tokens, see the definition of token from RFC2068 */
1449 AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1450 {
1451     const unsigned char *start_token;
1452     const unsigned char *s;
1453
1454     if (!line)
1455         return 0;
1456
1457     s = (const unsigned char *)line;
1458     for (;;) {
1459         /* find start of token, skip all stop characters, note NUL
1460          * isn't a token stop, so we don't need to test for it
1461          */
1462         while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1463             ++s;
1464         }
1465         if (!*s) {
1466             return 0;
1467         }
1468         start_token = s;
1469         /* find end of the token */
1470         while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1471             ++s;
1472         }
1473         if (!strncasecmp((const char *)start_token, (const char *)tok,
1474                          s - start_token)) {
1475             return 1;
1476         }
1477         if (!*s) {
1478             return 0;
1479         }
1480     }
1481 }
1482
1483
1484 AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line,
1485                                    const char *tok)
1486 {
1487     int llen, tlen, lidx;
1488
1489     if (!line)
1490         return 0;
1491
1492     llen = strlen(line);
1493     tlen = strlen(tok);
1494     lidx = llen - tlen;
1495
1496     if (lidx < 0 ||
1497         (lidx > 0 && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1498         return 0;
1499
1500     return (strncasecmp(&line[lidx], tok, tlen) == 0);
1501 }
1502
1503 AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1504 {
1505     char *cmd;
1506     unsigned char *d;
1507     const unsigned char *s;
1508
1509     cmd = apr_palloc(p, 2 * strlen(str) + 1);        /* Be safe */
1510     d = (unsigned char *)cmd;
1511     s = (const unsigned char *)str;
1512     for (; *s; ++s) {
1513
1514 #if defined(OS2) || defined(WIN32)
1515         /* 
1516          * Newlines to Win32/OS2 CreateProcess() are ill advised.
1517          * Convert them to spaces since they are effectively white
1518          * space to most applications
1519          */
1520         if (*s == '\r' || *s == '\n') {
1521              *d++ = ' ';
1522              continue;
1523          }
1524 #endif
1525
1526         if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1527             *d++ = '\\';
1528         }
1529         *d++ = *s;
1530     }
1531     *d = '\0';
1532
1533     return cmd;
1534 }
1535
1536 static char x2c(const char *what)
1537 {
1538     register char digit;
1539
1540 #if !APR_CHARSET_EBCDIC
1541     digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
1542              : (what[0] - '0'));
1543     digit *= 16;
1544     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
1545               : (what[1] - '0'));
1546 #else /*APR_CHARSET_EBCDIC*/
1547     char xstr[5];
1548     xstr[0]='0';
1549     xstr[1]='x';
1550     xstr[2]=what[0];
1551     xstr[3]=what[1];
1552     xstr[4]='\0';
1553     digit = apr_xlate_conv_byte(ap_hdrs_from_ascii,
1554                                 0xFF & strtol(xstr, NULL, 16));
1555 #endif /*APR_CHARSET_EBCDIC*/
1556     return (digit);
1557 }
1558
1559 /*
1560  * Unescapes a URL.
1561  * Returns 0 on success, non-zero on error
1562  * Failure is due to
1563  *   bad % escape       returns HTTP_BAD_REQUEST
1564  *
1565  *   decoding %00 -> \0  (the null character)
1566  *   decoding %2f -> /   (a special character)
1567  *                      returns HTTP_NOT_FOUND
1568  */
1569 AP_DECLARE(int) ap_unescape_url(char *url)
1570 {
1571     register int badesc, badpath;
1572     char *x, *y;
1573
1574     badesc = 0;
1575     badpath = 0;
1576     /* Initial scan for first '%'. Don't bother writing values before
1577      * seeing a '%' */
1578     y = strchr(url, '%');
1579     if (y == NULL) {
1580         return OK;
1581     }
1582     for (x = y; *y; ++x, ++y) {
1583         if (*y != '%')
1584             *x = *y;
1585         else {
1586             if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1587                 badesc = 1;
1588                 *x = '%';
1589             }
1590             else {
1591                 *x = x2c(y + 1);
1592                 y += 2;
1593                 if (IS_SLASH(*x) || *x == '\0')
1594                     badpath = 1;
1595             }
1596         }
1597     }
1598     *x = '\0';
1599     if (badesc)
1600         return HTTP_BAD_REQUEST;
1601     else if (badpath)
1602         return HTTP_NOT_FOUND;
1603     else
1604         return OK;
1605 }
1606
1607 AP_DECLARE(int) ap_unescape_url_keep2f(char *url)
1608 {
1609     register int badesc, badpath;
1610     char *x, *y;
1611
1612     badesc = 0;
1613     badpath = 0;
1614     /* Initial scan for first '%'. Don't bother writing values before
1615      * seeing a '%' */
1616     y = strchr(url, '%');
1617     if (y == NULL) {
1618         return OK;
1619     }
1620     for (x = y; *y; ++x, ++y) {
1621         if (*y != '%') {
1622             *x = *y;
1623         }
1624         else {
1625             if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1626                 badesc = 1;
1627                 *x = '%';
1628             }
1629             else {
1630                 char decoded;
1631                 decoded = x2c(y + 1);
1632                 if (decoded == '\0') {
1633                     badpath = 1;
1634                 }
1635                 else {
1636                     *x = decoded;
1637                     y += 2;
1638                 }
1639             }
1640         }
1641     }
1642     *x = '\0';
1643     if (badesc) {
1644         return HTTP_BAD_REQUEST;
1645     }
1646     else if (badpath) {
1647         return HTTP_NOT_FOUND;
1648     }
1649     else {
1650         return OK;
1651     }
1652 }
1653
1654 AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1655                                        apr_port_t port, const request_rec *r)
1656 {
1657     if (ap_is_default_port(port, r)) {
1658         return apr_pstrdup(p, hostname);
1659     }
1660     else {
1661         return apr_psprintf(p, "%s:%u", hostname, port);
1662     }
1663 }
1664
1665 /* c2x takes an unsigned, and expects the caller has guaranteed that
1666  * 0 <= what < 256... which usually means that you have to cast to
1667  * unsigned char first, because (unsigned)(char)(x) first goes through
1668  * signed extension to an int before the unsigned cast.
1669  *
1670  * The reason for this assumption is to assist gcc code generation --
1671  * the unsigned char -> unsigned extension is already done earlier in
1672  * both uses of this code, so there's no need to waste time doing it
1673  * again.
1674  */
1675 static const char c2x_table[] = "0123456789abcdef";
1676
1677 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char *where)
1678 {
1679 #if APR_CHARSET_EBCDIC
1680     what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1681 #endif /*APR_CHARSET_EBCDIC*/
1682     *where++ = '%';
1683     *where++ = c2x_table[what >> 4];
1684     *where++ = c2x_table[what & 0xf];
1685     return where;
1686 }
1687
1688 /*
1689  * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1690  * routine is (should be) OS independent.
1691  *
1692  * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1693  * cases if a ':' occurs before the first '/' in the URL, the URL should be
1694  * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1695  * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1696  * efficiency reasons, we don't use escape_path_segment(), which is provided for
1697  * reference. Again, RFC 1808 is where this stuff is defined.
1698  *
1699  * If partial is set, os_escape_path() assumes that the path will be appended to
1700  * something with a '/' in it (and thus does not prefix "./").
1701  */
1702
1703 AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1704 {
1705     char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
1706     const unsigned char *s = (const unsigned char *)segment;
1707     unsigned char *d = (unsigned char *)copy;
1708     unsigned c;
1709
1710     while ((c = *s)) {
1711         if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1712             d = c2x(c, d);
1713         }
1714         else {
1715             *d++ = c;
1716         }
1717         ++s;
1718     }
1719     *d = '\0';
1720     return copy;
1721 }
1722
1723 AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1724 {
1725     char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1726     const unsigned char *s = (const unsigned char *)path;
1727     unsigned char *d = (unsigned char *)copy;
1728     unsigned c;
1729
1730     if (!partial) {
1731         const char *colon = ap_strchr_c(path, ':');
1732         const char *slash = ap_strchr_c(path, '/');
1733
1734         if (colon && (!slash || colon < slash)) {
1735             *d++ = '.';
1736             *d++ = '/';
1737         }
1738     }
1739     while ((c = *s)) {
1740         if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1741             d = c2x(c, d);
1742         }
1743         else {
1744             *d++ = c;
1745         }
1746         ++s;
1747     }
1748     *d = '\0';
1749     return copy;
1750 }
1751
1752 /* ap_escape_uri is now a macro for os_escape_path */
1753
1754 AP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
1755 {
1756     int i, j;
1757     char *x;
1758
1759     /* first, count the number of extra characters */
1760     for (i = 0, j = 0; s[i] != '\0'; i++)
1761         if (s[i] == '<' || s[i] == '>')
1762             j += 3;
1763         else if (s[i] == '&')
1764             j += 4;
1765         else if (s[i] == '"')
1766             j += 5;
1767
1768     if (j == 0)
1769         return apr_pstrmemdup(p, s, i);
1770
1771     x = apr_palloc(p, i + j + 1);
1772     for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1773         if (s[i] == '<') {
1774             memcpy(&x[j], "&lt;", 4);
1775             j += 3;
1776         }
1777         else if (s[i] == '>') {
1778             memcpy(&x[j], "&gt;", 4);
1779             j += 3;
1780         }
1781         else if (s[i] == '&') {
1782             memcpy(&x[j], "&amp;", 5);
1783             j += 4;
1784         }
1785         else if (s[i] == '"') {
1786             memcpy(&x[j], "&quot;", 6);
1787             j += 5;
1788         }
1789         else
1790             x[j] = s[i];
1791
1792     x[j] = '\0';
1793     return x;
1794 }
1795
1796 AP_DECLARE(char *) ap_escape_logitem(apr_pool_t *p, const char *str)
1797 {
1798     char *ret;
1799     unsigned char *d;
1800     const unsigned char *s;
1801
1802     if (!str) {
1803         return NULL;
1804     }
1805
1806     ret = apr_palloc(p, 4 * strlen(str) + 1); /* Be safe */
1807     d = (unsigned char *)ret;
1808     s = (const unsigned char *)str;
1809     for (; *s; ++s) {
1810
1811         if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1812             *d++ = '\\';
1813             switch(*s) {
1814             case '\b':
1815                 *d++ = 'b';
1816                 break;
1817             case '\n':
1818                 *d++ = 'n';
1819                 break;
1820             case '\r':
1821                 *d++ = 'r';
1822                 break;
1823             case '\t':
1824                 *d++ = 't';
1825                 break;
1826             case '\v':
1827                 *d++ = 'v';
1828                 break;
1829             case '\\':
1830             case '"':
1831                 *d++ = *s;
1832                 break;
1833             default:
1834                 c2x(*s, d);
1835                 *d = 'x';
1836                 d += 3;
1837             }
1838         }
1839         else {
1840             *d++ = *s;
1841         }
1842     }
1843     *d = '\0';
1844
1845     return ret;
1846 }
1847
1848 AP_DECLARE(apr_size_t) ap_escape_errorlog_item(char *dest, const char *source,
1849                                                apr_size_t buflen)
1850 {
1851     unsigned char *d, *ep;
1852     const unsigned char *s;
1853
1854     if (!source || !buflen) { /* be safe */
1855         return 0;
1856     }
1857
1858     d = (unsigned char *)dest;
1859     s = (const unsigned char *)source;
1860     ep = d + buflen - 1;
1861
1862     for (; d < ep && *s; ++s) {
1863
1864         if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1865             *d++ = '\\';
1866             if (d >= ep) {
1867                 --d;
1868                 break;
1869             }
1870
1871             switch(*s) {
1872             case '\b':
1873                 *d++ = 'b';
1874                 break;
1875             case '\n':
1876                 *d++ = 'n';
1877                 break;
1878             case '\r':
1879                 *d++ = 'r';
1880                 break;
1881             case '\t':
1882                 *d++ = 't';
1883                 break;
1884             case '\v':
1885                 *d++ = 'v';
1886                 break;
1887             case '\\':
1888                 *d++ = *s;
1889                 break;
1890             case '"': /* no need for this in error log */
1891                 d[-1] = *s;
1892                 break;
1893             default:
1894                 if (d >= ep - 2) {
1895                     ep = --d; /* break the for loop as well */
1896                     break;
1897                 }
1898                 c2x(*s, d);
1899                 *d = 'x';
1900                 d += 3;
1901             }
1902         }
1903         else {
1904             *d++ = *s;
1905         }
1906     }
1907     *d = '\0';
1908
1909     return (d - (unsigned char *)dest);
1910 }
1911
1912 AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
1913 {
1914     apr_finfo_t finfo;
1915
1916     if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1917         return 0;                /* in error condition, just return no */
1918
1919     return (finfo.filetype == APR_DIR);
1920 }
1921
1922 AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
1923 {
1924     apr_finfo_t finfo;
1925
1926     if (apr_lstat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1927         return 0;                /* in error condition, just return no */
1928
1929     return (finfo.filetype == APR_DIR);
1930 }
1931
1932 AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1933                                   const char *src2)
1934 {
1935     apr_size_t len1, len2;
1936     char *path;
1937
1938     len1 = strlen(src1);
1939     len2 = strlen(src2);
1940      /* allocate +3 for '/' delimiter, trailing NULL and overallocate
1941       * one extra byte to allow the caller to add a trailing '/'
1942       */
1943     path = (char *)apr_palloc(a, len1 + len2 + 3);
1944     if (len1 == 0) {
1945         *path = '/';
1946         memcpy(path + 1, src2, len2 + 1);
1947     }
1948     else {
1949         char *next;
1950         memcpy(path, src1, len1);
1951         next = path + len1;
1952         if (next[-1] != '/') {
1953             *next++ = '/';
1954         }
1955         memcpy(next, src2, len2 + 1);
1956     }
1957     return path;
1958 }
1959
1960 /*
1961  * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1962  */
1963 AP_DECLARE(int) ap_is_url(const char *u)
1964 {
1965     register int x;
1966
1967     for (x = 0; u[x] != ':'; x++) {
1968         if ((!u[x]) ||
1969             ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1970              (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1971             return 0;
1972         }
1973     }
1974
1975     return (x ? 1 : 0);                /* If the first character is ':', it's broken, too */
1976 }
1977
1978 AP_DECLARE(int) ap_ind(const char *s, char c)
1979 {
1980     const char *p = ap_strchr_c(s, c);
1981
1982     if (p == NULL)
1983         return -1;
1984     return p - s;
1985 }
1986
1987 AP_DECLARE(int) ap_rind(const char *s, char c)
1988 {
1989     const char *p = ap_strrchr_c(s, c);
1990
1991     if (p == NULL)
1992         return -1;
1993     return p - s;
1994 }
1995
1996 AP_DECLARE(void) ap_str_tolower(char *str)
1997 {
1998     while (*str) {
1999         *str = apr_tolower(*str);
2000         ++str;
2001     }
2002 }
2003
2004 static char *find_fqdn(apr_pool_t *a, struct hostent *p)
2005 {
2006     int x;
2007
2008     if (!strchr(p->h_name, '.')) {
2009         if (p->h_aliases) {
2010             for (x = 0; p->h_aliases[x]; ++x) {
2011                 if (strchr(p->h_aliases[x], '.') &&
2012                     (!strncasecmp(p->h_aliases[x], p->h_name,
2013                                   strlen(p->h_name))))
2014                     return apr_pstrdup(a, p->h_aliases[x]);
2015             }
2016         }
2017         return NULL;
2018     }
2019     return apr_pstrdup(a, (void *) p->h_name);
2020 }
2021
2022 char *ap_get_local_host(apr_pool_t *a)
2023 {
2024 #ifndef MAXHOSTNAMELEN
2025 #define MAXHOSTNAMELEN 256
2026 #endif
2027     char str[MAXHOSTNAMELEN + 1];
2028     char *server_hostname = NULL;
2029     struct hostent *p;
2030
2031 #ifdef BEOS_R5
2032     if (gethostname(str, sizeof(str) - 1) == 0)
2033 #else
2034     if (gethostname(str, sizeof(str) - 1) != 0)
2035 #endif
2036     {
2037         ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
2038                      "%s: gethostname() failed to determine ServerName",
2039                      ap_server_argv0);
2040     }
2041     else 
2042     {
2043         str[sizeof(str) - 1] = '\0';
2044         /* TODO: Screaming for APR-ization */
2045         if ((!(p = gethostbyname(str))) 
2046             || (!(server_hostname = find_fqdn(a, p)))) {
2047             /* Recovery - return the default servername by IP: */
2048             if (p && p->h_addr_list[0]) {
2049                 apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
2050                 server_hostname = apr_pstrdup(a, str);
2051                 /* We will drop through to report the IP-named server */
2052             }
2053         }
2054         else {
2055             /* Since we found a fdqn, return it with no logged message. */
2056             return server_hostname;
2057         }
2058     }
2059
2060     if (!server_hostname) 
2061         server_hostname = apr_pstrdup(a, "127.0.0.1");
2062
2063     ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, a,
2064                  "%s: Could not determine the server's fully qualified "
2065                  "domain name, using %s for ServerName",
2066                  ap_server_argv0, server_hostname);
2067              
2068     return server_hostname;
2069 }
2070
2071 /* simple 'pool' alloc()ing glue to apr_base64.c
2072  */
2073 AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
2074 {
2075     char *decoded;
2076     int l;
2077
2078     decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
2079     l = apr_base64_decode(decoded, bufcoded);
2080     decoded[l] = '\0'; /* make binary sequence into string */
2081
2082     return decoded;
2083 }
2084
2085 AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string) 
2086
2087     char *encoded;
2088     int l = strlen(string);
2089
2090     encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
2091     l = apr_base64_encode(encoded, string, l);
2092     encoded[l] = '\0'; /* make binary sequence into string */
2093
2094     return encoded;
2095 }
2096
2097 /* we want to downcase the type/subtype for comparison purposes
2098  * but nothing else because ;parameter=foo values are case sensitive.
2099  * XXX: in truth we want to downcase parameter names... but really,
2100  * apache has never handled parameters and such correctly.  You
2101  * also need to compress spaces and such to be able to compare
2102  * properly. -djg
2103  */
2104 AP_DECLARE(void) ap_content_type_tolower(char *str)
2105 {
2106     char *semi;
2107
2108     semi = strchr(str, ';');
2109     if (semi) {
2110         *semi = '\0';
2111     }
2112     while (*str) {
2113         *str = apr_tolower(*str);
2114         ++str;
2115     }
2116     if (semi) {
2117         *semi = ';';
2118     }
2119 }
2120
2121 /*
2122  * Given a string, replace any bare " with \" .
2123  */
2124 AP_DECLARE(char *) ap_escape_quotes(apr_pool_t *p, const char *instring)
2125 {
2126     int newlen = 0;
2127     const char *inchr = instring;
2128     char *outchr, *outstring;
2129
2130     /*
2131      * Look through the input string, jogging the length of the output
2132      * string up by an extra byte each time we find an unescaped ".
2133      */
2134     while (*inchr != '\0') {
2135         newlen++;
2136         if (*inchr == '"') {
2137             newlen++;
2138         }
2139         /*
2140          * If we find a slosh, and it's not the last byte in the string,
2141          * it's escaping something - advance past both bytes.
2142          */
2143         if ((*inchr == '\\') && (inchr[1] != '\0')) {
2144             inchr++;
2145             newlen++;
2146         }
2147         inchr++;
2148     }
2149     outstring = apr_palloc(p, newlen + 1);
2150     inchr = instring;
2151     outchr = outstring;
2152     /*
2153      * Now copy the input string to the output string, inserting a slosh
2154      * in front of every " that doesn't already have one.
2155      */
2156     while (*inchr != '\0') {
2157         if ((*inchr == '\\') && (inchr[1] != '\0')) {
2158             *outchr++ = *inchr++;
2159             *outchr++ = *inchr++;
2160         }
2161         if (*inchr == '"') {
2162             *outchr++ = '\\';
2163         }
2164         if (*inchr != '\0') {
2165             *outchr++ = *inchr++;
2166         }
2167     }
2168     *outchr = '\0';
2169     return outstring;
2170 }