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