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