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