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