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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * util.c: string utility things
21 * 1995-96 Many changes by the Apache Software Foundation
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
31 #include "apr_strings.h"
34 #define APR_WANT_STDIO
35 #define APR_WANT_STRFUNC
41 #if APR_HAVE_PROCESS_H
42 #include <process.h> /* for getpid() on Win32 */
45 #include <netdb.h> /* for gethostbyname() */
48 #include "ap_config.h"
49 #include "apr_base64.h"
51 #include "http_main.h"
53 #include "http_protocol.h"
54 #include "http_config.h"
55 #include "util_ebcdic.h"
64 /* A bunch of functions in util.c scan strings looking for certain characters.
65 * To make that more efficient we encode a lookup table. The test_char_table
66 * is generated automatically by gen_test_char.c.
68 #include "test_char.h"
70 /* we assume the folks using this ensure 0 <= c < 256... which means
71 * you need a cast to (unsigned char) first, you can't just plug a
72 * char in here and get it to work, because if char is signed then it
73 * will first be sign extended.
75 #define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
77 /* Win32/NetWare/OS2 need to check for both forward and back slashes
78 * in ap_getparents() and ap_escape_url.
80 #ifdef CASE_BLIND_FILESYSTEM
81 #define IS_SLASH(s) ((s == '/') || (s == '\\'))
84 #define IS_SLASH(s) (s == '/')
88 APLOG_USE_MODULE(core);
92 * Examine a field value (such as a media-/content-type) string and return
93 * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
95 AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
99 if (intype == NULL) return NULL;
101 semi = ap_strchr_c(intype, ';');
103 return apr_pstrdup(p, intype);
106 while ((semi > intype) && apr_isspace(semi[-1])) {
109 return apr_pstrndup(p, intype, semi - intype);
113 AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
117 char ts[MAX_STRING_LEN];
118 char tf[MAX_STRING_LEN];
125 apr_time_exp_gmt(&xt, t);
126 /* Convert %Z to "GMT" and %z to "+0000";
127 * on hosts that do not have a time zone string in struct tm,
128 * strftime must assume its argument is local time.
130 for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
132 if (*f != '%') continue;
143 case 'z': /* common extension */
157 apr_time_exp_lt(&xt, t);
160 /* check return code? */
161 apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
162 ts[MAX_STRING_LEN - 1] = '\0';
163 return apr_pstrdup(p, ts);
166 /* Roy owes Rob beer. */
167 /* Rob owes Roy dinner. */
169 /* These legacy comments would make a lot more sense if Roy hadn't
170 * replaced the old later_than() routine with util_date.c.
172 * Well, okay, they still wouldn't make any sense.
175 /* Match = 0, NoMatch = 1, Abort = -1
176 * Based loosely on sections of wildmat.c by Rich Salz
177 * Hmmm... shouldn't this really go component by component?
179 AP_DECLARE(int) ap_strcmp_match(const char *str, const char *expected)
183 for (x = 0, y = 0; expected[y]; ++y, ++x) {
184 if ((!str[x]) && (expected[y] != '*'))
186 if (expected[y] == '*') {
187 while (expected[++y] == '*');
192 if ((ret = ap_strcmp_match(&str[x++], &expected[y])) != 1)
197 else if ((expected[y] != '?') && (str[x] != expected[y]))
200 return (str[x] != '\0');
203 AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *expected)
207 for (x = 0, y = 0; expected[y]; ++y, ++x) {
208 if (!str[x] && expected[y] != '*')
210 if (expected[y] == '*') {
211 while (expected[++y] == '*');
216 if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1)
221 else if (expected[y] != '?'
222 && apr_tolower(str[x]) != apr_tolower(expected[y]))
225 return (str[x] != '\0');
228 /* We actually compare the canonical root to this root, (but we don't
229 * waste time checking the case), since every use of this function in
230 * httpd-2.1 tests if the path is 'proper', meaning we've already passed
231 * it through apr_filepath_merge, or we haven't.
233 AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
236 const char *ourdir = dir;
237 if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
238 || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
244 AP_DECLARE(int) ap_is_matchexp(const char *str)
248 for (x = 0; str[x]; x++)
249 if ((str[x] == '*') || (str[x] == '?'))
255 * Here's a pool-based interface to the POSIX-esque ap_regcomp().
256 * Note that we return ap_regex_t instead of being passed one.
257 * The reason is that if you use an already-used ap_regex_t structure,
258 * the memory that you've already allocated gets forgotten, and
259 * regfree() doesn't clear it. So we don't allow it.
262 static apr_status_t regex_cleanup(void *preg)
264 ap_regfree((ap_regex_t *) preg);
268 AP_DECLARE(ap_regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
271 ap_regex_t *preg = apr_palloc(p, sizeof *preg);
273 if (ap_regcomp(preg, pattern, cflags)) {
277 apr_pool_cleanup_register(p, (void *) preg, regex_cleanup,
278 apr_pool_cleanup_null);
283 AP_DECLARE(void) ap_pregfree(apr_pool_t *p, ap_regex_t *reg)
286 apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
290 * Similar to standard strstr() but we ignore case in this version.
291 * Based on the strstr() implementation further below.
293 AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
301 for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
305 /* found first character of s2, see if the rest matches */
308 for (++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
310 /* both strings ended together */
315 /* second string ended, a match */
318 /* didn't find a match here, try starting at next character in s1 */
325 * Returns an offsetted pointer in bigstring immediately after
326 * prefix. Returns bigstring if bigstring doesn't start with
327 * prefix or if prefix is longer than bigstring while still matching.
328 * NOTE: pointer returned is relative to bigstring, so we
329 * can use standard pointer comparisons in the calling function
330 * (eg: test if ap_stripprefix(a,b) == a)
332 AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
341 while (*p1 && *prefix) {
342 if (*p1++ != *prefix++)
348 /* hit the end of bigstring! */
352 /* This function substitutes for $0-$9, filling in regular expression
353 * submatches. Pass it the same nmatch and pmatch arguments that you
354 * passed ap_regexec(). pmatch should not be greater than the maximum number
355 * of subexpressions - i.e. one more than the re_nsub member of ap_regex_t.
357 * input should be the string with the $-expressions, source should be the
358 * string that was matched against.
360 * It returns the substituted string, or NULL on error.
362 * Parts of this code are based on Henry Spencer's regsub(), from his
363 * AT&T V8 regexp package.
366 AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input,
367 const char *source, size_t nmatch,
368 ap_regmatch_t pmatch[])
370 const char *src = input;
379 return apr_pstrdup(p, src);
381 /* First pass, find the size */
385 while ((c = *src++) != '\0') {
386 if (c == '$' && apr_isdigit(*src))
391 if (no > 9) { /* Ordinary character. */
392 if (c == '\\' && *src)
396 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
397 len += pmatch[no].rm_eo - pmatch[no].rm_so;
402 dest = dst = apr_pcalloc(p, len + 1);
404 /* Now actually fill in the string */
408 while ((c = *src++) != '\0') {
411 else if (c == '$' && apr_isdigit(*src))
416 if (no > 9) { /* Ordinary character. */
417 if (c == '\\' && (*src == '$' || *src == '&'))
421 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
422 len = pmatch[no].rm_eo - pmatch[no].rm_so;
423 memcpy(dst, source + pmatch[no].rm_so, len);
434 * Parse .. so we don't compromise security
436 AP_DECLARE(void) ap_getparents(char *name)
441 /* Four paseses, as per RFC 1808 */
442 /* a) remove ./ path segments */
443 for (next = name; *next && (*next != '.'); next++) {
446 l = w = first_dot = next - name;
447 while (name[l] != '\0') {
448 if (name[l] == '.' && IS_SLASH(name[l + 1])
449 && (l == 0 || IS_SLASH(name[l - 1])))
452 name[w++] = name[l++];
455 /* b) remove trailing . path, segment */
456 if (w == 1 && name[0] == '.')
458 else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
462 /* c) remove all xx/../ segments. (including leading ../ and /../) */
465 while (name[l] != '\0') {
466 if (name[l] == '.' && name[l + 1] == '.' && IS_SLASH(name[l + 2])
467 && (l == 0 || IS_SLASH(name[l - 1]))) {
468 register int m = l + 3, n;
472 while (l >= 0 && !IS_SLASH(name[l]))
479 while ((name[n] = name[m]))
486 /* d) remove trailing xx/.. segment. */
487 if (l == 2 && name[0] == '.' && name[1] == '.')
489 else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
490 && IS_SLASH(name[l - 3])) {
493 while (l >= 0 && !IS_SLASH(name[l]))
503 AP_DECLARE(void) ap_no2slash(char *name)
509 #ifdef HAVE_UNC_PATHS
510 /* Check for UNC names. Leave leading two slashes. */
511 if (s[0] == '/' && s[1] == '/')
516 if ((*d++ = *s) == '/') {
530 * copy at most n leading directories of s into d
531 * d should be at least as large as s plus 1 extra byte
533 * the return value is the ever useful pointer to the trailing \0 of d
535 * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
536 * so that if n == 0, "/" is returned in d with n == 1
537 * and s == "e:/test.html", "e:/" is returned in d
538 * *** See also directory_walk in modules/http/http_request.c
541 * /a/b, 0 ==> / (true for all platforms)
550 * c:/a/b 3 ==> c:/a/b
551 * c:/a/b 4 ==> c:/a/b
553 AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
562 if (*s == '\0' || (*s == '/' && (--n) == 0)) {
574 * return the parent directory name including trailing / of the file s
576 AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
578 const char *last_slash = ap_strrchr_c(s, '/');
582 if (last_slash == NULL) {
583 return apr_pstrdup(p, "");
585 l = (last_slash - s) + 1;
586 d = apr_pstrmemdup(p, s, l);
592 AP_DECLARE(int) ap_count_dirs(const char *path)
596 for (x = 0, n = 0; path[x]; x++)
602 AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
604 return ap_getword(atrans, (const char **) line, stop);
607 AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
609 const char *pos = *line;
613 while ((*pos != stop) && *pos) {
618 res = apr_pstrmemdup(atrans, *line, len);
621 while (*pos == stop) {
630 AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
632 return ap_getword_white(atrans, (const char **) line);
635 AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
637 const char *pos = *line;
641 while (!apr_isspace(*pos) && *pos) {
646 res = apr_pstrmemdup(atrans, *line, len);
648 while (apr_isspace(*pos)) {
657 AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line,
660 return ap_getword_nulls(atrans, (const char **) line, stop);
663 AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line,
666 const char *pos = ap_strchr_c(*line, stop);
670 res = apr_pstrdup(atrans, *line);
671 *line += strlen(*line);
675 res = apr_pstrndup(atrans, *line, pos - *line);
684 /* Get a word, (new) config-file style --- quoted strings and backslashes
688 static char *substring_conf(apr_pool_t *p, const char *start, int len,
691 char *result = apr_palloc(p, len + 2);
695 for (i = 0; i < len; ++i) {
696 if (start[i] == '\\' && (start[i + 1] == '\\'
697 || (quote && start[i + 1] == quote)))
698 *resp++ = start[++i];
704 #if RESOLVE_ENV_PER_TOKEN
705 return (char *)ap_resolve_env(p,result);
711 AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
713 return ap_getword_conf(p, (const char **) line);
716 AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
718 const char *str = *line, *strend;
722 while (*str && apr_isspace(*str))
730 if ((quote = *str) == '"' || quote == '\'') {
732 while (*strend && *strend != quote) {
733 if (*strend == '\\' && strend[1] &&
734 (strend[1] == quote || strend[1] == '\\')) {
741 res = substring_conf(p, str + 1, strend - str - 1, quote);
743 if (*strend == quote)
748 while (*strend && !apr_isspace(*strend))
751 res = substring_conf(p, str, strend - str, 0);
754 while (*strend && apr_isspace(*strend))
760 AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
763 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
764 "Done with config file %s", cfp->name);
766 return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
769 /* we can't use apr_file_* directly because of linking issues on Windows */
770 static apr_status_t cfg_close(void *param)
772 return apr_file_close(param);
775 static apr_status_t cfg_getch(char *ch, void *param)
777 return apr_file_getc(ch, param);
780 static apr_status_t cfg_getstr(void *buf, size_t bufsiz, void *param)
782 return apr_file_gets(buf, bufsiz, param);
785 /* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
786 AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg,
787 apr_pool_t *p, const char *name)
789 ap_configfile_t *new_cfg;
790 apr_file_t *file = NULL;
798 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
799 "Internal error: pcfg_openfile() called with NULL filename");
803 status = apr_file_open(&file, name, APR_READ | APR_BUFFERED,
806 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
807 "Opening config file %s (%s)",
808 name, (status != APR_SUCCESS) ?
809 apr_strerror(status, buf, sizeof(buf)) : "successful");
811 if (status != APR_SUCCESS)
814 status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
815 if (status != APR_SUCCESS)
818 if (finfo.filetype != APR_REG &&
819 #if defined(WIN32) || defined(OS2) || defined(NETWARE)
820 strcasecmp(apr_filepath_name_get(name), "nul") != 0) {
822 strcmp(name, "/dev/null") != 0) {
823 #endif /* WIN32 || OS2 */
824 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
825 "Access to file %s denied by server: not a regular file",
827 apr_file_close(file);
832 /* Some twisted character [no pun intended] at MS decided that a
833 * zero width joiner as the lead wide character would be ideal for
834 * describing Unicode text files. This was further convoluted to
835 * another MSism that the same character mapped into utf-8, EF BB BF
836 * would signify utf-8 text files.
838 * Since MS configuration files are all protecting utf-8 encoded
839 * Unicode path, file and resource names, we already have the correct
840 * WinNT encoding. But at least eat the stupid three bytes up front.
843 unsigned char buf[4];
845 status = apr_file_read(file, buf, &len);
846 if ((status != APR_SUCCESS) || (len < 3)
847 || memcmp(buf, "\xEF\xBB\xBF", 3) != 0) {
849 apr_file_seek(file, APR_SET, &zero);
854 new_cfg = apr_palloc(p, sizeof(*new_cfg));
855 new_cfg->param = file;
856 new_cfg->name = apr_pstrdup(p, name);
857 new_cfg->getch = cfg_getch;
858 new_cfg->getstr = cfg_getstr;
859 new_cfg->close = cfg_close;
860 new_cfg->line_number = 0;
866 /* Allocate a ap_configfile_t handle with user defined functions and params */
867 AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(
868 apr_pool_t *p, const char *descr, void *param,
869 apr_status_t (*getc_func) (char *ch, void *param),
870 apr_status_t (*gets_func) (void *buf, size_t bufsize, void *param),
871 apr_status_t (*close_func) (void *param))
873 ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
874 new_cfg->param = param;
875 new_cfg->name = descr;
876 new_cfg->getch = getc_func;
877 new_cfg->getstr = gets_func;
878 new_cfg->close = close_func;
879 new_cfg->line_number = 0;
883 /* Read one character from a configfile_t */
884 AP_DECLARE(apr_status_t) ap_cfg_getc(char *ch, ap_configfile_t *cfp)
886 apr_status_t rc = cfp->getch(ch, cfp->param);
887 if (rc == APR_SUCCESS && *ch == LF)
892 AP_DECLARE(const char *) ap_pcfg_strerror(apr_pool_t *p, ap_configfile_t *cfp,
895 char buf[MAX_STRING_LEN];
896 if (rc == APR_SUCCESS)
898 return apr_psprintf(p, "Error reading %s at line %d: %s",
899 cfp->name, cfp->line_number,
900 rc == APR_ENOSPC ? "Line too long"
901 : apr_strerror(rc, buf, sizeof(buf)));
904 /* Read one line from open ap_configfile_t, strip LF, increase line number */
905 /* If custom handler does not define a getstr() function, read char by char */
906 AP_DECLARE(apr_status_t) ap_cfg_getline(char *buf, size_t bufsize, ap_configfile_t *cfp)
910 /* If a "get string" function is defined, use it */
911 if (cfp->getstr != NULL) {
914 size_t cbufsize = bufsize;
918 rc = cfp->getstr(cbuf, cbufsize, cfp->param);
928 if (rc != APR_SUCCESS) {
933 * check for line continuation,
934 * i.e. match [^\\]\\[\r]\n only
938 if (cp > cbuf && cp[-1] == LF) {
940 if (cp > cbuf && cp[-1] == CR)
942 if (cp > cbuf && cp[-1] == '\\') {
945 * line continuation requested -
946 * then remove backslash and continue
948 cbufsize -= (cp-cbuf);
953 else if (cp - buf >= bufsize - 1) {
959 /* No "get string" function defined; read character by character */
963 /* too small, assume caller is crazy */
970 rc = cfp->getch(&c, cfp->param);
977 if (rc != APR_SUCCESS)
981 /* check for line continuation */
982 if (i > 0 && buf[i-1] == '\\') {
990 else if (i >= bufsize - 2) {
1000 * Leading and trailing white space is eliminated completely
1003 while (apr_isspace(*src))
1005 /* blast trailing whitespace */
1006 dst = &src[strlen(src)];
1007 while (--dst >= src && apr_isspace(*dst))
1009 /* Zap leading whitespace by shifting */
1011 memmove(buf, src, dst - src + 2);
1013 #ifdef DEBUG_CFG_LINES
1014 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, NULL, "Read config: '%s'", buf);
1019 /* Size an HTTP header field list item, as separated by a comma.
1020 * The return value is a pointer to the beginning of the non-empty list item
1021 * within the original string (or NULL if there is none) and the address
1022 * of field is shifted to the next non-comma, non-whitespace character.
1023 * len is the length of the item excluding any beginning whitespace.
1025 AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1027 const unsigned char *ptr = (const unsigned char *)*field;
1028 const unsigned char *token;
1029 int in_qpair, in_qstr, in_com;
1031 /* Find first non-comma, non-whitespace byte */
1033 while (*ptr == ',' || apr_isspace(*ptr))
1038 /* Find the end of this item, skipping over dead bits */
1040 for (in_qpair = in_qstr = in_com = 0;
1041 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1049 case '\\': in_qpair = 1; /* quoted-pair */
1051 case '"' : if (!in_com) /* quoted string delim */
1054 case '(' : if (!in_qstr) /* comment (may nest) */
1057 case ')' : if (in_com) /* end comment */
1065 if ((*len = (ptr - token)) == 0) {
1066 *field = (const char *)ptr;
1070 /* Advance field pointer to the next non-comma, non-white byte */
1072 while (*ptr == ',' || apr_isspace(*ptr))
1075 *field = (const char *)ptr;
1076 return (const char *)token;
1079 /* Retrieve an HTTP header field list item, as separated by a comma,
1080 * while stripping insignificant whitespace and lowercasing anything not in
1081 * a quoted string or comment. The return value is a new string containing
1082 * the converted list item (or NULL if none) and the address pointed to by
1083 * field is shifted to the next non-comma, non-whitespace.
1085 AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1087 const char *tok_start;
1088 const unsigned char *ptr;
1091 int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1093 /* Find the beginning and maximum length of the list item so that
1094 * we can allocate a buffer for the new string and reset the field.
1096 if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1099 token = apr_palloc(p, tok_len + 1);
1101 /* Scan the token again, but this time copy only the good bytes.
1102 * We skip extra whitespace and any whitespace around a '=', '/',
1103 * or ';' and lowercase normal characters not within a comment,
1104 * quoted-string or quoted-pair.
1106 for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1107 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1116 case '\\': in_qpair = 1;
1122 case '"' : if (!in_com)
1129 case '(' : if (!in_qstr)
1136 case ')' : if (in_com)
1142 case '\t': if (addspace)
1144 if (in_com || in_qstr)
1151 case ';' : if (!(in_com || in_qstr))
1155 default : if (addspace == 1)
1157 *pos++ = (in_com || in_qstr) ? *ptr
1158 : apr_tolower(*ptr);
1169 /* Find an item in canonical form (lowercase, no extra spaces) within
1170 * an HTTP field value list. Returns 1 if found, 0 if not found.
1171 * This would be much more efficient if we stored header fields as
1172 * an array of list items as they are received instead of a plain string.
1174 AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line,
1177 const unsigned char *pos;
1178 const unsigned char *ptr = (const unsigned char *)line;
1179 int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1184 do { /* loop for each item in line's list */
1186 /* Find first non-comma, non-whitespace byte */
1188 while (*ptr == ',' || apr_isspace(*ptr))
1192 good = 1; /* until proven otherwise for this item */
1194 break; /* no items left and nothing good found */
1196 /* We skip extra whitespace and any whitespace around a '=', '/',
1197 * or ';' and lowercase normal characters not within a comment,
1198 * quoted-string or quoted-pair.
1200 for (pos = (const unsigned char *)tok;
1201 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1207 good = (*pos++ == *ptr);
1211 case '\\': in_qpair = 1;
1213 good = good && (*pos++ == ' ');
1214 good = good && (*pos++ == *ptr);
1217 case '"' : if (!in_com)
1220 good = good && (*pos++ == ' ');
1221 good = good && (*pos++ == *ptr);
1224 case '(' : if (!in_qstr)
1227 good = good && (*pos++ == ' ');
1228 good = good && (*pos++ == *ptr);
1231 case ')' : if (in_com)
1233 good = good && (*pos++ == *ptr);
1237 case '\t': if (addspace || !good)
1239 if (in_com || in_qstr)
1240 good = (*pos++ == *ptr);
1246 case ';' : if (!(in_com || in_qstr))
1248 good = good && (*pos++ == *ptr);
1250 default : if (!good)
1253 good = (*pos++ == ' ');
1254 if (in_com || in_qstr)
1255 good = good && (*pos++ == *ptr);
1257 good = good && (*pos++ == apr_tolower(*ptr));
1264 good = 0; /* not good if only a prefix was matched */
1266 } while (*ptr && !good);
1272 /* Retrieve a token, spacing over it and returning a pointer to
1273 * the first non-white byte afterwards. Note that these tokens
1274 * are delimited by semis and commas; and can also be delimited
1275 * by whitespace at the caller's option.
1278 AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line,
1281 const char *ptr = *accept_line;
1282 const char *tok_start;
1286 /* Find first non-white byte */
1288 while (*ptr && apr_isspace(*ptr))
1293 /* find token end, skipping over quoted strings.
1294 * (comments are already gone).
1297 while (*ptr && (accept_white || !apr_isspace(*ptr))
1298 && *ptr != ';' && *ptr != ',') {
1305 tok_len = ptr - tok_start;
1306 token = apr_pstrndup(p, tok_start, tok_len);
1308 /* Advance accept_line pointer to the next non-white byte */
1310 while (*ptr && apr_isspace(*ptr))
1318 /* find http tokens, see the definition of token from RFC2068 */
1319 AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1321 const unsigned char *start_token;
1322 const unsigned char *s;
1327 s = (const unsigned char *)line;
1329 /* find start of token, skip all stop characters, note NUL
1330 * isn't a token stop, so we don't need to test for it
1332 while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1339 /* find end of the token */
1340 while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1343 if (!strncasecmp((const char *)start_token, (const char *)tok,
1354 AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line,
1357 int llen, tlen, lidx;
1362 llen = strlen(line);
1367 (lidx > 0 && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1370 return (strncasecmp(&line[lidx], tok, tlen) == 0);
1373 AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1377 const unsigned char *s;
1379 cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1380 d = (unsigned char *)cmd;
1381 s = (const unsigned char *)str;
1384 #if defined(OS2) || defined(WIN32)
1386 * Newlines to Win32/OS2 CreateProcess() are ill advised.
1387 * Convert them to spaces since they are effectively white
1388 * space to most applications
1390 if (*s == '\r' || *s == '\n') {
1396 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1406 static char x2c(const char *what)
1408 register char digit;
1410 #if !APR_CHARSET_EBCDIC
1411 digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
1414 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
1416 #else /*APR_CHARSET_EBCDIC*/
1423 digit = apr_xlate_conv_byte(ap_hdrs_from_ascii,
1424 0xFF & strtol(xstr, NULL, 16));
1425 #endif /*APR_CHARSET_EBCDIC*/
1430 * Unescapes a URL, leaving reserved characters intact.
1431 * Returns 0 on success, non-zero on error
1433 * bad % escape returns HTTP_BAD_REQUEST
1435 * decoding %00 or a forbidden character returns HTTP_NOT_FOUND
1438 static int unescape_url(char *url, const char *forbid, const char *reserved)
1440 register int badesc, badpath;
1445 /* Initial scan for first '%'. Don't bother writing values before
1447 y = strchr(url, '%');
1451 for (x = y; *y; ++x, ++y) {
1456 if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1462 decoded = x2c(y + 1);
1463 if ((decoded == '\0')
1464 || (forbid && ap_strchr_c(forbid, decoded))) {
1469 else if (reserved && ap_strchr_c(reserved, decoded)) {
1483 return HTTP_BAD_REQUEST;
1486 return HTTP_NOT_FOUND;
1492 AP_DECLARE(int) ap_unescape_url(char *url)
1495 return unescape_url(url, SLASHES, NULL);
1497 AP_DECLARE(int) ap_unescape_url_keep2f(char *url, int decode_slashes)
1499 /* AllowEncodedSlashes (corrected) */
1500 if (decode_slashes) {
1501 /* no chars reserved */
1502 return unescape_url(url, NULL, NULL);
1504 /* reserve (do not decode) encoded slashes */
1505 return unescape_url(url, NULL, SLASHES);
1509 /* IFDEF these out until they've been thought through.
1510 * Just a germ of an API extension for now
1512 AP_DECLARE(int) ap_unescape_url_proxy(char *url)
1514 /* leave RFC1738 reserved characters intact, * so proxied URLs
1515 * don't get mangled. Where does that leave encoded '&' ?
1517 return unescape_url(url, NULL, "/;?");
1519 AP_DECLARE(int) ap_unescape_url_reserved(char *url, const char *reserved)
1521 return unescape_url(url, NULL, reserved);
1525 AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1526 apr_port_t port, const request_rec *r)
1528 if (ap_is_default_port(port, r)) {
1529 return apr_pstrdup(p, hostname);
1532 return apr_psprintf(p, "%s:%u", hostname, port);
1536 AP_DECLARE(int) ap_unescape_all(char *url)
1538 return unescape_url(url, NULL, NULL);
1541 /* c2x takes an unsigned, and expects the caller has guaranteed that
1542 * 0 <= what < 256... which usually means that you have to cast to
1543 * unsigned char first, because (unsigned)(char)(x) first goes through
1544 * signed extension to an int before the unsigned cast.
1546 * The reason for this assumption is to assist gcc code generation --
1547 * the unsigned char -> unsigned extension is already done earlier in
1548 * both uses of this code, so there's no need to waste time doing it
1551 static const char c2x_table[] = "0123456789abcdef";
1553 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
1554 unsigned char *where)
1556 #if APR_CHARSET_EBCDIC
1557 what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1558 #endif /*APR_CHARSET_EBCDIC*/
1560 *where++ = c2x_table[what >> 4];
1561 *where++ = c2x_table[what & 0xf];
1566 * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1567 * routine is (should be) OS independent.
1569 * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1570 * cases if a ':' occurs before the first '/' in the URL, the URL should be
1571 * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1572 * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1573 * efficiency reasons, we don't use escape_path_segment(), which is provided for
1574 * reference. Again, RFC 1808 is where this stuff is defined.
1576 * If partial is set, os_escape_path() assumes that the path will be appended to
1577 * something with a '/' in it (and thus does not prefix "./").
1580 AP_DECLARE(char *) ap_escape_path_segment_buffer(char *copy, const char *segment)
1582 const unsigned char *s = (const unsigned char *)segment;
1583 unsigned char *d = (unsigned char *)copy;
1587 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1599 AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1601 return ap_escape_path_segment_buffer(apr_palloc(p, 3 * strlen(segment) + 1), segment);
1604 AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1606 char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1607 const unsigned char *s = (const unsigned char *)path;
1608 unsigned char *d = (unsigned char *)copy;
1612 const char *colon = ap_strchr_c(path, ':');
1613 const char *slash = ap_strchr_c(path, '/');
1615 if (colon && (!slash || colon < slash)) {
1621 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1633 /* ap_escape_uri is now a macro for os_escape_path */
1635 AP_DECLARE(char *) ap_escape_html2(apr_pool_t *p, const char *s, int toasc)
1640 /* first, count the number of extra characters */
1641 for (i = 0, j = 0; s[i] != '\0'; i++)
1642 if (s[i] == '<' || s[i] == '>')
1644 else if (s[i] == '&')
1646 else if (s[i] == '"')
1648 else if (toasc && !apr_isascii(s[i]))
1652 return apr_pstrmemdup(p, s, i);
1654 x = apr_palloc(p, i + j + 1);
1655 for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1657 memcpy(&x[j], "<", 4);
1660 else if (s[i] == '>') {
1661 memcpy(&x[j], ">", 4);
1664 else if (s[i] == '&') {
1665 memcpy(&x[j], "&", 5);
1668 else if (s[i] == '"') {
1669 memcpy(&x[j], """, 6);
1672 else if (toasc && !apr_isascii(s[i])) {
1673 char *esc = apr_psprintf(p, "&#%3.3d;", (unsigned char)s[i]);
1674 memcpy(&x[j], esc, 6);
1683 AP_DECLARE(char *) ap_escape_logitem(apr_pool_t *p, const char *str)
1687 const unsigned char *s;
1693 ret = apr_palloc(p, 4 * strlen(str) + 1); /* Be safe */
1694 d = (unsigned char *)ret;
1695 s = (const unsigned char *)str;
1698 if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1734 AP_DECLARE(apr_size_t) ap_escape_errorlog_item(char *dest, const char *source,
1737 unsigned char *d, *ep;
1738 const unsigned char *s;
1740 if (!source || !buflen) { /* be safe */
1744 d = (unsigned char *)dest;
1745 s = (const unsigned char *)source;
1746 ep = d + buflen - 1;
1748 for (; d < ep && *s; ++s) {
1750 if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1776 case '"': /* no need for this in error log */
1781 ep = --d; /* break the for loop as well */
1794 return (d - (unsigned char *)dest);
1797 AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
1801 if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1802 return 0; /* in error condition, just return no */
1804 return (finfo.filetype == APR_DIR);
1807 AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
1811 if (apr_stat(&finfo, path, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
1812 return 0; /* in error condition, just return no */
1814 return (finfo.filetype == APR_DIR);
1817 AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1820 apr_size_t len1, len2;
1823 len1 = strlen(src1);
1824 len2 = strlen(src2);
1825 /* allocate +3 for '/' delimiter, trailing NULL and overallocate
1826 * one extra byte to allow the caller to add a trailing '/'
1828 path = (char *)apr_palloc(a, len1 + len2 + 3);
1831 memcpy(path + 1, src2, len2 + 1);
1835 memcpy(path, src1, len1);
1837 if (next[-1] != '/') {
1840 memcpy(next, src2, len2 + 1);
1846 * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1848 AP_DECLARE(int) ap_is_url(const char *u)
1852 for (x = 0; u[x] != ':'; x++) {
1854 ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1855 (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1860 return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
1863 AP_DECLARE(int) ap_ind(const char *s, char c)
1865 const char *p = ap_strchr_c(s, c);
1872 AP_DECLARE(int) ap_rind(const char *s, char c)
1874 const char *p = ap_strrchr_c(s, c);
1881 AP_DECLARE(void) ap_str_tolower(char *str)
1884 *str = apr_tolower(*str);
1890 * We must return a FQDN
1892 char *ap_get_local_host(apr_pool_t *a)
1894 #ifndef MAXHOSTNAMELEN
1895 #define MAXHOSTNAMELEN 256
1897 char str[MAXHOSTNAMELEN + 1];
1898 char *server_hostname = NULL;
1899 apr_sockaddr_t *sockaddr;
1902 if (apr_gethostname(str, sizeof(str) - 1, a) != APR_SUCCESS) {
1903 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
1904 "%s: apr_gethostname() failed to determine ServerName",
1907 str[sizeof(str) - 1] = '\0';
1908 if (apr_sockaddr_info_get(&sockaddr, str, APR_UNSPEC, 0, 0, a) == APR_SUCCESS) {
1909 if ( (apr_getnameinfo(&hostname, sockaddr, 0) == APR_SUCCESS) &&
1910 (ap_strchr_c(hostname, '.')) ) {
1911 server_hostname = apr_pstrdup(a, hostname);
1912 return server_hostname;
1913 } else if (ap_strchr_c(str, '.')) {
1914 server_hostname = apr_pstrdup(a, str);
1916 apr_sockaddr_ip_get(&hostname, sockaddr);
1917 server_hostname = apr_pstrdup(a, hostname);
1920 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
1921 "%s: apr_sockaddr_info_get() failed for %s",
1922 ap_server_argv0, str);
1926 if (!server_hostname)
1927 server_hostname = apr_pstrdup(a, "127.0.0.1");
1929 ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, a,
1930 "%s: Could not reliably determine the server's fully qualified "
1931 "domain name, using %s for ServerName",
1932 ap_server_argv0, server_hostname);
1934 return server_hostname;
1937 /* simple 'pool' alloc()ing glue to apr_base64.c
1939 AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
1944 decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
1945 l = apr_base64_decode(decoded, bufcoded);
1946 decoded[l] = '\0'; /* make binary sequence into string */
1951 AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
1954 int l = strlen(string);
1956 encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
1957 l = apr_base64_encode(encoded, string, l);
1958 encoded[l] = '\0'; /* make binary sequence into string */
1963 /* we want to downcase the type/subtype for comparison purposes
1964 * but nothing else because ;parameter=foo values are case sensitive.
1965 * XXX: in truth we want to downcase parameter names... but really,
1966 * apache has never handled parameters and such correctly. You
1967 * also need to compress spaces and such to be able to compare
1970 AP_DECLARE(void) ap_content_type_tolower(char *str)
1974 semi = strchr(str, ';');
1979 ap_str_tolower(str);
1987 * Given a string, replace any bare " with \" .
1989 AP_DECLARE(char *) ap_escape_quotes(apr_pool_t *p, const char *instring)
1992 const char *inchr = instring;
1993 char *outchr, *outstring;
1996 * Look through the input string, jogging the length of the output
1997 * string up by an extra byte each time we find an unescaped ".
1999 while (*inchr != '\0') {
2001 if (*inchr == '"') {
2005 * If we find a slosh, and it's not the last byte in the string,
2006 * it's escaping something - advance past both bytes.
2008 if ((*inchr == '\\') && (inchr[1] != '\0')) {
2014 outstring = apr_palloc(p, newlen + 1);
2018 * Now copy the input string to the output string, inserting a slosh
2019 * in front of every " that doesn't already have one.
2021 while (*inchr != '\0') {
2022 if ((*inchr == '\\') && (inchr[1] != '\0')) {
2023 *outchr++ = *inchr++;
2024 *outchr++ = *inchr++;
2026 if (*inchr == '"') {
2029 if (*inchr != '\0') {
2030 *outchr++ = *inchr++;
2038 * Given a string, append the PID deliminated by delim.
2039 * Usually used to create a pid-appended filepath name
2040 * (eg: /a/b/foo -> /a/b/foo.6726). A function, and not
2041 * a macro, to avoid unistd.h dependency
2043 AP_DECLARE(char *) ap_append_pid(apr_pool_t *p, const char *string,
2046 return apr_psprintf(p, "%s%s%" APR_PID_T_FMT, string,
2052 * Parse a given timeout parameter string into an apr_interval_time_t value.
2053 * The unit of the time interval is given as postfix string to the numeric
2054 * string. Currently the following units are understood:
2061 * If no unit is contained in the given timeout parameter the default_time_unit
2062 * will be used instead.
2063 * @param timeout_parameter The string containing the timeout parameter.
2064 * @param timeout The timeout value to be returned.
2065 * @param default_time_unit The default time unit to use if none is specified
2066 * in timeout_parameter.
2067 * @return Status value indicating whether the parsing was successful or not.
2069 AP_DECLARE(apr_status_t) ap_timeout_parameter_parse(
2070 const char *timeout_parameter,
2071 apr_interval_time_t *timeout,
2072 const char *default_time_unit)
2075 const char *time_str;
2078 tout = apr_strtoi64(timeout_parameter, &endp, 10);
2082 if (!endp || !*endp) {
2083 time_str = default_time_unit;
2089 switch (*time_str) {
2090 /* Time is in seconds */
2092 *timeout = (apr_interval_time_t) apr_time_from_sec(tout);
2095 /* Time is in hours */
2096 *timeout = (apr_interval_time_t) apr_time_from_sec(tout * 3600);
2099 switch (*(++time_str)) {
2100 /* Time is in milliseconds */
2102 *timeout = (apr_interval_time_t) tout * 1000;
2104 /* Time is in minutes */
2106 *timeout = (apr_interval_time_t) apr_time_from_sec(tout * 60);
2109 return APR_EGENERAL;
2113 return APR_EGENERAL;
2119 * Determine if a request has a request body or not.
2121 * @param r the request_rec of the request
2122 * @return truth value
2124 AP_DECLARE(int) ap_request_has_body(request_rec *r)
2131 has_body = (!r->header_only
2133 || apr_table_get(r->headers_in, "Transfer-Encoding")
2134 || ( (cls = apr_table_get(r->headers_in, "Content-Length"))
2135 && (apr_strtoff(&cl, cls, &estr, 10) == APR_SUCCESS)
2143 AP_DECLARE_NONSTD(apr_status_t) ap_pool_cleanup_set_null(void *data_)
2145 void **ptr = (void **)data_;
2150 AP_DECLARE(apr_status_t) ap_str2_alnum(const char *src, char *dest) {
2152 for ( ; *src; src++, dest++)
2154 if (!apr_isprint(*src))
2156 else if (!apr_isalnum(*src))
2166 AP_DECLARE(apr_status_t) ap_pstr2_alnum(apr_pool_t *p, const char *src,
2169 char *new = apr_palloc(p, strlen(src)+1);
2173 return ap_str2_alnum(src, new);
2177 * Read the body and parse any form found, which must be of the
2178 * type application/x-www-form-urlencoded.
2180 * Name/value pairs are returned in an array, with the names as
2181 * strings with a maximum length of HUGE_STRING_LEN, and the
2182 * values as bucket brigades. This allows values to be arbitrarily
2185 * All url-encoding is removed from both the names and the values
2186 * on the fly. The names are interpreted as strings, while the
2187 * values are interpreted as blocks of binary data, that may
2188 * contain the 0 character.
2190 * In order to ensure that resource limits are not exceeded, a
2191 * maximum size must be provided. If the sum of the lengths of
2192 * the names and the values exceed this size, this function
2193 * will return HTTP_REQUEST_ENTITY_TOO_LARGE.
2195 * An optional number of parameters can be provided, if the number
2196 * of parameters provided exceeds this amount, this function will
2197 * return HTTP_REQUEST_ENTITY_TOO_LARGE. If this value is negative,
2198 * no limit is imposed, and the number of parameters is in turn
2199 * constrained by the size parameter above.
2201 * This function honours any kept_body configuration, and the
2202 * original raw request body will be saved to the kept_body brigade
2203 * if so configured, just as ap_discard_request_body does.
2205 * NOTE: File upload is not yet supported, but can be without change
2206 * to the function call.
2209 /* form parsing stuff */
2220 AP_DECLARE(int) ap_parse_form_data(request_rec *r, ap_filter_t *f,
2221 apr_array_header_t **ptr,
2222 apr_size_t num, apr_size_t usize)
2224 apr_bucket_brigade *bb = NULL;
2226 char buffer[HUGE_STRING_LEN + 1];
2228 apr_size_t offset = 0;
2230 ap_form_type_t state = FORM_NAME, percent = FORM_NORMAL;
2231 ap_form_pair_t *pair = NULL;
2232 apr_array_header_t *pairs = apr_array_make(r->pool, 4, sizeof(ap_form_pair_t));
2239 /* sanity check - we only support forms for now */
2240 ct = apr_table_get(r->headers_in, "Content-Type");
2241 if (!ct || strcmp("application/x-www-form-urlencoded", ct)) {
2242 return ap_discard_request_body(r);
2245 if (usize > APR_SIZE_MAX >> 1)
2246 size = APR_SIZE_MAX >> 1;
2251 f = r->input_filters;
2254 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
2256 apr_bucket *bucket = NULL, *last = NULL;
2258 int rv = ap_get_brigade(f, bb, AP_MODE_READBYTES,
2259 APR_BLOCK_READ, HUGE_STRING_LEN);
2260 if (rv != APR_SUCCESS) {
2261 apr_brigade_destroy(bb);
2262 return (rv == AP_FILTER_ERROR) ? rv : HTTP_BAD_REQUEST;
2265 for (bucket = APR_BRIGADE_FIRST(bb);
2266 bucket != APR_BRIGADE_SENTINEL(bb);
2267 last = bucket, bucket = APR_BUCKET_NEXT(bucket)) {
2269 apr_size_t len, slide;
2272 apr_bucket_delete(last);
2274 if (APR_BUCKET_IS_EOS(bucket)) {
2278 if (bucket->length == 0) {
2282 rv = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
2283 if (rv != APR_SUCCESS) {
2284 apr_brigade_destroy(bb);
2285 return HTTP_BAD_REQUEST;
2289 while (state != FORM_ABORT && slide-- > 0 && size >= 0 && num != 0) {
2294 else if ('&' == c) {
2298 percent = FORM_PERCENTA;
2301 if (FORM_PERCENTA == percent) {
2305 else if (c >= 'A') {
2308 else if (c >= '0') {
2312 percent = FORM_PERCENTB;
2315 if (FORM_PERCENTB == percent) {
2319 else if (c >= 'A') {
2322 else if (c >= '0') {
2326 percent = FORM_NORMAL;
2331 const char *tmp = apr_pmemdup(r->pool, buffer, offset);
2332 apr_bucket *b = apr_bucket_pool_create(tmp, offset, r->pool, r->connection->bucket_alloc);
2333 APR_BRIGADE_INSERT_TAIL(pair->value, b);
2341 if (offset < HUGE_STRING_LEN) {
2345 pair = (ap_form_pair_t *) apr_array_push(pairs);
2346 pair->name = apr_pstrdup(r->pool, buffer);
2347 pair->value = apr_brigade_create(r->pool, r->connection->bucket_alloc);
2351 buffer[offset++] = c;
2360 if (offset >= HUGE_STRING_LEN) {
2361 const char *tmp = apr_pmemdup(r->pool, buffer, offset);
2362 apr_bucket *b = apr_bucket_pool_create(tmp, offset, r->pool, r->connection->bucket_alloc);
2363 APR_BRIGADE_INSERT_TAIL(pair->value, b);
2366 buffer[offset++] = c;
2376 apr_brigade_cleanup(bb);
2377 } while (!seen_eos);
2379 if (FORM_ABORT == state || size < 0 || num == 0) {
2380 return HTTP_REQUEST_ENTITY_TOO_LARGE;
2382 else if (FORM_VALUE == state && pair && offset > 0) {
2383 const char *tmp = apr_pmemdup(r->pool, buffer, offset);
2384 apr_bucket *b = apr_bucket_pool_create(tmp, offset, r->pool, r->connection->bucket_alloc);
2385 APR_BRIGADE_INSERT_TAIL(pair->value, b);