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 == '\\'))
83 #define IS_SLASH(s) (s == '/')
86 APLOG_USE_MODULE(core);
90 * Examine a field value (such as a media-/content-type) string and return
91 * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
93 AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
97 if (intype == NULL) return NULL;
99 semi = ap_strchr_c(intype, ';');
101 return apr_pstrdup(p, intype);
104 while ((semi > intype) && apr_isspace(semi[-1])) {
107 return apr_pstrndup(p, intype, semi - intype);
111 AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
115 char ts[MAX_STRING_LEN];
116 char tf[MAX_STRING_LEN];
123 apr_time_exp_gmt(&xt, t);
124 /* Convert %Z to "GMT" and %z to "+0000";
125 * on hosts that do not have a time zone string in struct tm,
126 * strftime must assume its argument is local time.
128 for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
130 if (*f != '%') continue;
141 case 'z': /* common extension */
155 apr_time_exp_lt(&xt, t);
158 /* check return code? */
159 apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
160 ts[MAX_STRING_LEN - 1] = '\0';
161 return apr_pstrdup(p, ts);
164 /* Roy owes Rob beer. */
165 /* Rob owes Roy dinner. */
167 /* These legacy comments would make a lot more sense if Roy hadn't
168 * replaced the old later_than() routine with util_date.c.
170 * Well, okay, they still wouldn't make any sense.
173 /* Match = 0, NoMatch = 1, Abort = -1
174 * Based loosely on sections of wildmat.c by Rich Salz
175 * Hmmm... shouldn't this really go component by component?
177 AP_DECLARE(int) ap_strcmp_match(const char *str, const char *expected)
181 for (x = 0, y = 0; expected[y]; ++y, ++x) {
182 if ((!str[x]) && (expected[y] != '*'))
184 if (expected[y] == '*') {
185 while (expected[++y] == '*');
190 if ((ret = ap_strcmp_match(&str[x++], &expected[y])) != 1)
195 else if ((expected[y] != '?') && (str[x] != expected[y]))
198 return (str[x] != '\0');
201 AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *expected)
205 for (x = 0, y = 0; expected[y]; ++y, ++x) {
206 if (!str[x] && expected[y] != '*')
208 if (expected[y] == '*') {
209 while (expected[++y] == '*');
214 if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1)
219 else if (expected[y] != '?'
220 && apr_tolower(str[x]) != apr_tolower(expected[y]))
223 return (str[x] != '\0');
226 /* We actually compare the canonical root to this root, (but we don't
227 * waste time checking the case), since every use of this function in
228 * httpd-2.1 tests if the path is 'proper', meaning we've already passed
229 * it through apr_filepath_merge, or we haven't.
231 AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
234 const char *ourdir = dir;
235 if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
236 || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
242 AP_DECLARE(int) ap_is_matchexp(const char *str)
246 for (x = 0; str[x]; x++)
247 if ((str[x] == '*') || (str[x] == '?'))
253 * Here's a pool-based interface to the POSIX-esque ap_regcomp().
254 * Note that we return ap_regex_t instead of being passed one.
255 * The reason is that if you use an already-used ap_regex_t structure,
256 * the memory that you've already allocated gets forgotten, and
257 * regfree() doesn't clear it. So we don't allow it.
260 static apr_status_t regex_cleanup(void *preg)
262 ap_regfree((ap_regex_t *) preg);
266 AP_DECLARE(ap_regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
269 ap_regex_t *preg = apr_palloc(p, sizeof *preg);
271 if (ap_regcomp(preg, pattern, cflags)) {
275 apr_pool_cleanup_register(p, (void *) preg, regex_cleanup,
276 apr_pool_cleanup_null);
281 AP_DECLARE(void) ap_pregfree(apr_pool_t *p, ap_regex_t *reg)
284 apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
288 * Similar to standard strstr() but we ignore case in this version.
289 * Based on the strstr() implementation further below.
291 AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
299 for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
303 /* found first character of s2, see if the rest matches */
306 for (++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
308 /* both strings ended together */
313 /* second string ended, a match */
316 /* didn't find a match here, try starting at next character in s1 */
323 * Returns an offsetted pointer in bigstring immediately after
324 * prefix. Returns bigstring if bigstring doesn't start with
325 * prefix or if prefix is longer than bigstring while still matching.
326 * NOTE: pointer returned is relative to bigstring, so we
327 * can use standard pointer comparisons in the calling function
328 * (eg: test if ap_stripprefix(a,b) == a)
330 AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
339 while (*p1 && *prefix) {
340 if (*p1++ != *prefix++)
346 /* hit the end of bigstring! */
350 /* This function substitutes for $0-$9, filling in regular expression
351 * submatches. Pass it the same nmatch and pmatch arguments that you
352 * passed ap_regexec(). pmatch should not be greater than the maximum number
353 * of subexpressions - i.e. one more than the re_nsub member of ap_regex_t.
355 * input should be the string with the $-expressions, source should be the
356 * string that was matched against.
358 * It returns the substituted string, or NULL on error.
360 * Parts of this code are based on Henry Spencer's regsub(), from his
361 * AT&T V8 regexp package.
364 AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input,
365 const char *source, size_t nmatch,
366 ap_regmatch_t pmatch[])
368 const char *src = input;
377 return apr_pstrdup(p, src);
379 /* First pass, find the size */
383 while ((c = *src++) != '\0') {
384 if (c == '$' && apr_isdigit(*src))
389 if (no > 9) { /* Ordinary character. */
390 if (c == '\\' && *src)
394 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
395 len += pmatch[no].rm_eo - pmatch[no].rm_so;
400 dest = dst = apr_pcalloc(p, len + 1);
402 /* Now actually fill in the string */
406 while ((c = *src++) != '\0') {
409 else if (c == '$' && apr_isdigit(*src))
414 if (no > 9) { /* Ordinary character. */
415 if (c == '\\' && (*src == '$' || *src == '&'))
419 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
420 len = pmatch[no].rm_eo - pmatch[no].rm_so;
421 memcpy(dst, source + pmatch[no].rm_so, len);
432 * Parse .. so we don't compromise security
434 AP_DECLARE(void) ap_getparents(char *name)
439 /* Four paseses, as per RFC 1808 */
440 /* a) remove ./ path segments */
441 for (next = name; *next && (*next != '.'); next++) {
444 l = w = first_dot = next - name;
445 while (name[l] != '\0') {
446 if (name[l] == '.' && IS_SLASH(name[l + 1])
447 && (l == 0 || IS_SLASH(name[l - 1])))
450 name[w++] = name[l++];
453 /* b) remove trailing . path, segment */
454 if (w == 1 && name[0] == '.')
456 else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
460 /* c) remove all xx/../ segments. (including leading ../ and /../) */
463 while (name[l] != '\0') {
464 if (name[l] == '.' && name[l + 1] == '.' && IS_SLASH(name[l + 2])
465 && (l == 0 || IS_SLASH(name[l - 1]))) {
466 register int m = l + 3, n;
470 while (l >= 0 && !IS_SLASH(name[l]))
477 while ((name[n] = name[m]))
484 /* d) remove trailing xx/.. segment. */
485 if (l == 2 && name[0] == '.' && name[1] == '.')
487 else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
488 && IS_SLASH(name[l - 3])) {
491 while (l >= 0 && !IS_SLASH(name[l]))
501 AP_DECLARE(void) ap_no2slash(char *name)
507 #ifdef HAVE_UNC_PATHS
508 /* Check for UNC names. Leave leading two slashes. */
509 if (s[0] == '/' && s[1] == '/')
514 if ((*d++ = *s) == '/') {
528 * copy at most n leading directories of s into d
529 * d should be at least as large as s plus 1 extra byte
531 * the return value is the ever useful pointer to the trailing \0 of d
533 * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
534 * so that if n == 0, "/" is returned in d with n == 1
535 * and s == "e:/test.html", "e:/" is returned in d
536 * *** See also directory_walk in modules/http/http_request.c
539 * /a/b, 0 ==> / (true for all platforms)
548 * c:/a/b 3 ==> c:/a/b
549 * c:/a/b 4 ==> c:/a/b
551 AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
560 if (*s == '\0' || (*s == '/' && (--n) == 0)) {
572 * return the parent directory name including trailing / of the file s
574 AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
576 const char *last_slash = ap_strrchr_c(s, '/');
580 if (last_slash == NULL) {
581 return apr_pstrdup(p, "");
583 l = (last_slash - s) + 1;
584 d = apr_pstrmemdup(p, s, l);
590 AP_DECLARE(int) ap_count_dirs(const char *path)
594 for (x = 0, n = 0; path[x]; x++)
600 AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
602 return ap_getword(atrans, (const char **) line, stop);
605 AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
607 const char *pos = *line;
611 while ((*pos != stop) && *pos) {
616 res = apr_pstrmemdup(atrans, *line, len);
619 while (*pos == stop) {
628 AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
630 return ap_getword_white(atrans, (const char **) line);
633 AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
635 const char *pos = *line;
639 while (!apr_isspace(*pos) && *pos) {
644 res = apr_pstrmemdup(atrans, *line, len);
646 while (apr_isspace(*pos)) {
655 AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line,
658 return ap_getword_nulls(atrans, (const char **) line, stop);
661 AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line,
664 const char *pos = ap_strchr_c(*line, stop);
668 res = apr_pstrdup(atrans, *line);
669 *line += strlen(*line);
673 res = apr_pstrndup(atrans, *line, pos - *line);
682 /* Get a word, (new) config-file style --- quoted strings and backslashes
686 static char *substring_conf(apr_pool_t *p, const char *start, int len,
689 char *result = apr_palloc(p, len + 2);
693 for (i = 0; i < len; ++i) {
694 if (start[i] == '\\' && (start[i + 1] == '\\'
695 || (quote && start[i + 1] == quote)))
696 *resp++ = start[++i];
702 #if RESOLVE_ENV_PER_TOKEN
703 return (char *)ap_resolve_env(p,result);
709 AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
711 return ap_getword_conf(p, (const char **) line);
714 AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
716 const char *str = *line, *strend;
720 while (*str && apr_isspace(*str))
728 if ((quote = *str) == '"' || quote == '\'') {
730 while (*strend && *strend != quote) {
731 if (*strend == '\\' && strend[1] &&
732 (strend[1] == quote || strend[1] == '\\')) {
739 res = substring_conf(p, str + 1, strend - str - 1, quote);
741 if (*strend == quote)
746 while (*strend && !apr_isspace(*strend))
749 res = substring_conf(p, str, strend - str, 0);
752 while (*strend && apr_isspace(*strend))
758 /* Check a string for any ${ENV} environment variable
759 * construct and replace each them by the value of
760 * that environment variable, if it exists. If the
761 * environment value does not exist, leave the ${ENV}
762 * construct alone; it means something else.
764 AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
766 # define SMALL_EXPANSION 5
771 } *result, *current, sresult[SMALL_EXPANSION];
773 const char *s, *e, *ep;
777 s = ap_strchr_c(word, '$');
782 /* well, actually something to do */
783 ep = word + strlen(word);
785 result = current = &(sresult[spc++]);
786 current->next = NULL;
787 current->string = word;
788 current->len = s - word;
789 outlen = current->len;
792 /* prepare next entry */
794 current->next = (spc < SMALL_EXPANSION)
796 : (struct sll *)apr_palloc(p,
797 sizeof(*current->next));
798 current = current->next;
799 current->next = NULL;
804 if (s[1] == '{' && (e = ap_strchr_c(s, '}'))) {
805 word = getenv(apr_pstrndup(p, s+2, e-s-2));
807 current->string = word;
808 current->len = strlen(word);
809 outlen += current->len;
813 current->len = e - s + 1;
814 outlen += current->len;
819 current->string = s++;
826 s = ap_strchr_c(s, '$');
827 current->string = word;
828 current->len = s ? s - word : ep - word;
829 outlen += current->len;
833 /* assemble result */
834 res_buf = cp = apr_palloc(p, outlen + 1);
837 memcpy(cp, result->string, result->len);
840 result = result->next;
842 res_buf[outlen] = '\0';
847 AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
850 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
851 "Done with config file %s", cfp->name);
853 return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
856 static apr_status_t cfg_close(void *param)
858 apr_file_t *cfp = (apr_file_t *) param;
859 return (apr_file_close(cfp));
862 static int cfg_getch(void *param)
865 apr_file_t *cfp = (apr_file_t *) param;
866 if (apr_file_getc(&ch, cfp) == APR_SUCCESS)
871 static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
873 apr_file_t *cfp = (apr_file_t *) param;
875 rv = apr_file_gets(buf, bufsiz, cfp);
876 if (rv == APR_SUCCESS) {
882 /* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
883 AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg,
884 apr_pool_t *p, const char *name)
886 ap_configfile_t *new_cfg;
887 apr_file_t *file = NULL;
895 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
896 "Internal error: pcfg_openfile() called with NULL filename");
900 status = apr_file_open(&file, name, APR_READ | APR_BUFFERED,
903 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
904 "Opening config file %s (%s)",
905 name, (status != APR_SUCCESS) ?
906 apr_strerror(status, buf, sizeof(buf)) : "successful");
908 if (status != APR_SUCCESS)
911 status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
912 if (status != APR_SUCCESS)
915 if (finfo.filetype != APR_REG &&
916 #if defined(WIN32) || defined(OS2) || defined(NETWARE)
917 strcasecmp(apr_filepath_name_get(name), "nul") != 0) {
919 strcmp(name, "/dev/null") != 0) {
920 #endif /* WIN32 || OS2 */
921 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
922 "Access to file %s denied by server: not a regular file",
924 apr_file_close(file);
929 /* Some twisted character [no pun intended] at MS decided that a
930 * zero width joiner as the lead wide character would be ideal for
931 * describing Unicode text files. This was further convoluted to
932 * another MSism that the same character mapped into utf-8, EF BB BF
933 * would signify utf-8 text files.
935 * Since MS configuration files are all protecting utf-8 encoded
936 * Unicode path, file and resource names, we already have the correct
937 * WinNT encoding. But at least eat the stupid three bytes up front.
940 unsigned char buf[4];
942 status = apr_file_read(file, buf, &len);
943 if ((status != APR_SUCCESS) || (len < 3)
944 || memcmp(buf, "\xEF\xBB\xBF", 3) != 0) {
946 apr_file_seek(file, APR_SET, &zero);
951 new_cfg = apr_palloc(p, sizeof(*new_cfg));
952 new_cfg->param = file;
953 new_cfg->name = apr_pstrdup(p, name);
954 new_cfg->getch = (int (*)(void *)) cfg_getch;
955 new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
956 new_cfg->close = (int (*)(void *)) cfg_close;
957 new_cfg->line_number = 0;
963 /* Allocate a ap_configfile_t handle with user defined functions and params */
964 AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(apr_pool_t *p,
967 int(*getch)(void *param),
968 void *(*getstr) (void *buf, size_t bufsiz, void *param),
969 int(*close_func)(void *param))
971 ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
973 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
974 "Opening config handler %s", descr);
976 new_cfg->param = param;
977 new_cfg->name = descr;
978 new_cfg->getch = getch;
979 new_cfg->getstr = getstr;
980 new_cfg->close = close_func;
981 new_cfg->line_number = 0;
985 /* Read one character from a configfile_t */
986 AP_DECLARE(int) ap_cfg_getc(ap_configfile_t *cfp)
988 register int ch = cfp->getch(cfp->param);
994 /* Read one line from open ap_configfile_t, strip LF, increase line number */
995 /* If custom handler does not define a getstr() function, read char by char */
996 AP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, ap_configfile_t *cfp)
998 /* If a "get string" function is defined, use it */
999 if (cfp->getstr != NULL) {
1003 size_t cbufsize = bufsize;
1007 if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
1011 * check for line continuation,
1012 * i.e. match [^\\]\\[\r]\n only
1015 while (cp < cbuf+cbufsize && *cp != '\0')
1017 if (cp > cbuf && cp[-1] == LF) {
1019 if (cp > cbuf && cp[-1] == CR)
1021 if (cp > cbuf && cp[-1] == '\\') {
1023 if (!(cp > cbuf && cp[-1] == '\\')) {
1025 * line continuation requested -
1026 * then remove backslash and continue
1028 cbufsize -= (cp-cbuf);
1034 * no real continuation because escaped -
1035 * then just remove escape character
1037 for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
1046 * Leading and trailing white space is eliminated completely
1049 while (apr_isspace(*src))
1051 /* blast trailing whitespace */
1052 dst = &src[strlen(src)];
1053 while (--dst >= src && apr_isspace(*dst))
1055 /* Zap leading whitespace by shifting */
1057 for (dst = buf; (*dst++ = *src++) != '\0'; )
1060 #ifdef DEBUG_CFG_LINES
1061 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, "Read config: %s", buf);
1065 /* No "get string" function defined; read character by character */
1067 register size_t i = 0;
1070 /* skip leading whitespace */
1072 c = cfp->getch(cfp->param);
1073 } while (c == '\t' || c == ' ');
1079 /* too small, assume caller is crazy */
1084 if ((c == '\t') || (c == ' ')) {
1086 while ((c == '\t') || (c == ' '))
1087 c = cfp->getch(cfp->param);
1090 /* silently ignore CR (_assume_ that a LF follows) */
1091 c = cfp->getch(cfp->param);
1094 /* increase line number and return on LF */
1097 if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
1099 * check for line continuation
1101 if (i > 0 && buf[i-1] == '\\') {
1103 if (!(i > 0 && buf[i-1] == '\\')) {
1104 /* line is continued */
1105 c = cfp->getch(cfp->param);
1108 /* else nothing needs be done because
1109 * then the backslash is escaped and
1110 * we just strip to a single one
1113 /* blast trailing whitespace */
1114 while (i > 0 && apr_isspace(buf[i - 1]))
1117 #ifdef DEBUG_CFG_LINES
1118 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
1119 "Read config: %s", buf);
1125 c = cfp->getch(cfp->param);
1130 /* Size an HTTP header field list item, as separated by a comma.
1131 * The return value is a pointer to the beginning of the non-empty list item
1132 * within the original string (or NULL if there is none) and the address
1133 * of field is shifted to the next non-comma, non-whitespace character.
1134 * len is the length of the item excluding any beginning whitespace.
1136 AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1138 const unsigned char *ptr = (const unsigned char *)*field;
1139 const unsigned char *token;
1140 int in_qpair, in_qstr, in_com;
1142 /* Find first non-comma, non-whitespace byte */
1144 while (*ptr == ',' || apr_isspace(*ptr))
1149 /* Find the end of this item, skipping over dead bits */
1151 for (in_qpair = in_qstr = in_com = 0;
1152 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1160 case '\\': in_qpair = 1; /* quoted-pair */
1162 case '"' : if (!in_com) /* quoted string delim */
1165 case '(' : if (!in_qstr) /* comment (may nest) */
1168 case ')' : if (in_com) /* end comment */
1176 if ((*len = (ptr - token)) == 0) {
1177 *field = (const char *)ptr;
1181 /* Advance field pointer to the next non-comma, non-white byte */
1183 while (*ptr == ',' || apr_isspace(*ptr))
1186 *field = (const char *)ptr;
1187 return (const char *)token;
1190 /* Retrieve an HTTP header field list item, as separated by a comma,
1191 * while stripping insignificant whitespace and lowercasing anything not in
1192 * a quoted string or comment. The return value is a new string containing
1193 * the converted list item (or NULL if none) and the address pointed to by
1194 * field is shifted to the next non-comma, non-whitespace.
1196 AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1198 const char *tok_start;
1199 const unsigned char *ptr;
1202 int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1204 /* Find the beginning and maximum length of the list item so that
1205 * we can allocate a buffer for the new string and reset the field.
1207 if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1210 token = apr_palloc(p, tok_len + 1);
1212 /* Scan the token again, but this time copy only the good bytes.
1213 * We skip extra whitespace and any whitespace around a '=', '/',
1214 * or ';' and lowercase normal characters not within a comment,
1215 * quoted-string or quoted-pair.
1217 for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1218 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1227 case '\\': in_qpair = 1;
1233 case '"' : if (!in_com)
1240 case '(' : if (!in_qstr)
1247 case ')' : if (in_com)
1253 case '\t': if (addspace)
1255 if (in_com || in_qstr)
1262 case ';' : if (!(in_com || in_qstr))
1266 default : if (addspace == 1)
1268 *pos++ = (in_com || in_qstr) ? *ptr
1269 : apr_tolower(*ptr);
1280 /* Find an item in canonical form (lowercase, no extra spaces) within
1281 * an HTTP field value list. Returns 1 if found, 0 if not found.
1282 * This would be much more efficient if we stored header fields as
1283 * an array of list items as they are received instead of a plain string.
1285 AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line,
1288 const unsigned char *pos;
1289 const unsigned char *ptr = (const unsigned char *)line;
1290 int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1295 do { /* loop for each item in line's list */
1297 /* Find first non-comma, non-whitespace byte */
1299 while (*ptr == ',' || apr_isspace(*ptr))
1303 good = 1; /* until proven otherwise for this item */
1305 break; /* no items left and nothing good found */
1307 /* We skip extra whitespace and any whitespace around a '=', '/',
1308 * or ';' and lowercase normal characters not within a comment,
1309 * quoted-string or quoted-pair.
1311 for (pos = (const unsigned char *)tok;
1312 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1318 good = (*pos++ == *ptr);
1322 case '\\': in_qpair = 1;
1324 good = good && (*pos++ == ' ');
1325 good = good && (*pos++ == *ptr);
1328 case '"' : if (!in_com)
1331 good = good && (*pos++ == ' ');
1332 good = good && (*pos++ == *ptr);
1335 case '(' : if (!in_qstr)
1338 good = good && (*pos++ == ' ');
1339 good = good && (*pos++ == *ptr);
1342 case ')' : if (in_com)
1344 good = good && (*pos++ == *ptr);
1348 case '\t': if (addspace || !good)
1350 if (in_com || in_qstr)
1351 good = (*pos++ == *ptr);
1357 case ';' : if (!(in_com || in_qstr))
1359 good = good && (*pos++ == *ptr);
1361 default : if (!good)
1364 good = (*pos++ == ' ');
1365 if (in_com || in_qstr)
1366 good = good && (*pos++ == *ptr);
1368 good = good && (*pos++ == apr_tolower(*ptr));
1375 good = 0; /* not good if only a prefix was matched */
1377 } while (*ptr && !good);
1383 /* Retrieve a token, spacing over it and returning a pointer to
1384 * the first non-white byte afterwards. Note that these tokens
1385 * are delimited by semis and commas; and can also be delimited
1386 * by whitespace at the caller's option.
1389 AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line,
1392 const char *ptr = *accept_line;
1393 const char *tok_start;
1397 /* Find first non-white byte */
1399 while (*ptr && apr_isspace(*ptr))
1404 /* find token end, skipping over quoted strings.
1405 * (comments are already gone).
1408 while (*ptr && (accept_white || !apr_isspace(*ptr))
1409 && *ptr != ';' && *ptr != ',') {
1416 tok_len = ptr - tok_start;
1417 token = apr_pstrndup(p, tok_start, tok_len);
1419 /* Advance accept_line pointer to the next non-white byte */
1421 while (*ptr && apr_isspace(*ptr))
1429 /* find http tokens, see the definition of token from RFC2068 */
1430 AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1432 const unsigned char *start_token;
1433 const unsigned char *s;
1438 s = (const unsigned char *)line;
1440 /* find start of token, skip all stop characters, note NUL
1441 * isn't a token stop, so we don't need to test for it
1443 while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1450 /* find end of the token */
1451 while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1454 if (!strncasecmp((const char *)start_token, (const char *)tok,
1465 AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line,
1468 int llen, tlen, lidx;
1473 llen = strlen(line);
1478 (lidx > 0 && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1481 return (strncasecmp(&line[lidx], tok, tlen) == 0);
1484 AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1488 const unsigned char *s;
1490 cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1491 d = (unsigned char *)cmd;
1492 s = (const unsigned char *)str;
1495 #if defined(OS2) || defined(WIN32)
1497 * Newlines to Win32/OS2 CreateProcess() are ill advised.
1498 * Convert them to spaces since they are effectively white
1499 * space to most applications
1501 if (*s == '\r' || *s == '\n') {
1507 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1517 static char x2c(const char *what)
1519 register char digit;
1521 #if !APR_CHARSET_EBCDIC
1522 digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
1525 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
1527 #else /*APR_CHARSET_EBCDIC*/
1534 digit = apr_xlate_conv_byte(ap_hdrs_from_ascii,
1535 0xFF & strtol(xstr, NULL, 16));
1536 #endif /*APR_CHARSET_EBCDIC*/
1541 * Unescapes a URL, leaving reserved characters intact.
1542 * Returns 0 on success, non-zero on error
1544 * bad % escape returns HTTP_BAD_REQUEST
1546 * decoding %00 or a forbidden character returns HTTP_NOT_FOUND
1549 static int unescape_url(char *url, const char *forbid, const char *reserved)
1551 register int badesc, badpath;
1556 /* Initial scan for first '%'. Don't bother writing values before
1558 y = strchr(url, '%');
1562 for (x = y; *y; ++x, ++y) {
1567 if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1573 decoded = x2c(y + 1);
1574 if ((decoded == '\0')
1575 || (forbid && ap_strchr_c(forbid, decoded))) {
1580 else if (reserved && ap_strchr_c(reserved, decoded)) {
1594 return HTTP_BAD_REQUEST;
1597 return HTTP_NOT_FOUND;
1603 AP_DECLARE(int) ap_unescape_url(char *url)
1606 #ifdef CASE_BLIND_FILESYSTEM
1607 return unescape_url(url, "/\\", NULL);
1609 return unescape_url(url, "/", NULL);
1612 AP_DECLARE(int) ap_unescape_url_keep2f(char *url)
1614 /* AllowEncodedSlashes (corrected) */
1615 return unescape_url(url, NULL, "/");
1618 /* IFDEF these out until they've been thought through.
1619 * Just a germ of an API extension for now
1621 AP_DECLARE(int) ap_unescape_url_proxy(char *url)
1623 /* leave RFC1738 reserved characters intact, * so proxied URLs
1624 * don't get mangled. Where does that leave encoded '&' ?
1626 return unescape_url(url, NULL, "/;?");
1628 AP_DECLARE(int) ap_unescape_url_reserved(char *url, const char *reserved)
1630 return unescape_url(url, NULL, reserved);
1634 AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1635 apr_port_t port, const request_rec *r)
1637 if (ap_is_default_port(port, r)) {
1638 return apr_pstrdup(p, hostname);
1641 return apr_psprintf(p, "%s:%u", hostname, port);
1645 AP_DECLARE(int) ap_unescape_all(char *url)
1647 return unescape_url(url, NULL, NULL);
1650 /* c2x takes an unsigned, and expects the caller has guaranteed that
1651 * 0 <= what < 256... which usually means that you have to cast to
1652 * unsigned char first, because (unsigned)(char)(x) first goes through
1653 * signed extension to an int before the unsigned cast.
1655 * The reason for this assumption is to assist gcc code generation --
1656 * the unsigned char -> unsigned extension is already done earlier in
1657 * both uses of this code, so there's no need to waste time doing it
1660 static const char c2x_table[] = "0123456789abcdef";
1662 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
1663 unsigned char *where)
1665 #if APR_CHARSET_EBCDIC
1666 what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1667 #endif /*APR_CHARSET_EBCDIC*/
1669 *where++ = c2x_table[what >> 4];
1670 *where++ = c2x_table[what & 0xf];
1675 * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1676 * routine is (should be) OS independent.
1678 * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1679 * cases if a ':' occurs before the first '/' in the URL, the URL should be
1680 * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1681 * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1682 * efficiency reasons, we don't use escape_path_segment(), which is provided for
1683 * reference. Again, RFC 1808 is where this stuff is defined.
1685 * If partial is set, os_escape_path() assumes that the path will be appended to
1686 * something with a '/' in it (and thus does not prefix "./").
1689 AP_DECLARE(char *) ap_escape_path_segment_buffer(char *copy, const char *segment)
1691 const unsigned char *s = (const unsigned char *)segment;
1692 unsigned char *d = (unsigned char *)copy;
1696 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1708 AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1710 return ap_escape_path_segment_buffer(apr_palloc(p, 3 * strlen(segment) + 1), segment);
1713 AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1715 char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1716 const unsigned char *s = (const unsigned char *)path;
1717 unsigned char *d = (unsigned char *)copy;
1721 const char *colon = ap_strchr_c(path, ':');
1722 const char *slash = ap_strchr_c(path, '/');
1724 if (colon && (!slash || colon < slash)) {
1730 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1742 /* ap_escape_uri is now a macro for os_escape_path */
1744 AP_DECLARE(char *) ap_escape_html2(apr_pool_t *p, const char *s, int toasc)
1749 /* first, count the number of extra characters */
1750 for (i = 0, j = 0; s[i] != '\0'; i++)
1751 if (s[i] == '<' || s[i] == '>')
1753 else if (s[i] == '&')
1755 else if (s[i] == '"')
1757 else if (toasc && !apr_isascii(s[i]))
1761 return apr_pstrmemdup(p, s, i);
1763 x = apr_palloc(p, i + j + 1);
1764 for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1766 memcpy(&x[j], "<", 4);
1769 else if (s[i] == '>') {
1770 memcpy(&x[j], ">", 4);
1773 else if (s[i] == '&') {
1774 memcpy(&x[j], "&", 5);
1777 else if (s[i] == '"') {
1778 memcpy(&x[j], """, 6);
1781 else if (toasc && !apr_isascii(s[i])) {
1782 char *esc = apr_psprintf(p, "&#%3.3d;", (unsigned char)s[i]);
1783 memcpy(&x[j], esc, 6);
1792 AP_DECLARE(char *) ap_escape_logitem(apr_pool_t *p, const char *str)
1796 const unsigned char *s;
1802 ret = apr_palloc(p, 4 * strlen(str) + 1); /* Be safe */
1803 d = (unsigned char *)ret;
1804 s = (const unsigned char *)str;
1807 if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1843 AP_DECLARE(apr_size_t) ap_escape_errorlog_item(char *dest, const char *source,
1846 unsigned char *d, *ep;
1847 const unsigned char *s;
1849 if (!source || !buflen) { /* be safe */
1853 d = (unsigned char *)dest;
1854 s = (const unsigned char *)source;
1855 ep = d + buflen - 1;
1857 for (; d < ep && *s; ++s) {
1859 if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1885 case '"': /* no need for this in error log */
1890 ep = --d; /* break the for loop as well */
1903 return (d - (unsigned char *)dest);
1906 AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
1910 if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1911 return 0; /* in error condition, just return no */
1913 return (finfo.filetype == APR_DIR);
1916 AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
1920 if (apr_stat(&finfo, path, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
1921 return 0; /* in error condition, just return no */
1923 return (finfo.filetype == APR_DIR);
1926 AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1929 apr_size_t len1, len2;
1932 len1 = strlen(src1);
1933 len2 = strlen(src2);
1934 /* allocate +3 for '/' delimiter, trailing NULL and overallocate
1935 * one extra byte to allow the caller to add a trailing '/'
1937 path = (char *)apr_palloc(a, len1 + len2 + 3);
1940 memcpy(path + 1, src2, len2 + 1);
1944 memcpy(path, src1, len1);
1946 if (next[-1] != '/') {
1949 memcpy(next, src2, len2 + 1);
1955 * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1957 AP_DECLARE(int) ap_is_url(const char *u)
1961 for (x = 0; u[x] != ':'; x++) {
1963 ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1964 (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1969 return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
1972 AP_DECLARE(int) ap_ind(const char *s, char c)
1974 const char *p = ap_strchr_c(s, c);
1981 AP_DECLARE(int) ap_rind(const char *s, char c)
1983 const char *p = ap_strrchr_c(s, c);
1990 AP_DECLARE(void) ap_str_tolower(char *str)
1993 *str = apr_tolower(*str);
1999 * We must return a FQDN
2001 char *ap_get_local_host(apr_pool_t *a)
2003 #ifndef MAXHOSTNAMELEN
2004 #define MAXHOSTNAMELEN 256
2006 char str[MAXHOSTNAMELEN + 1];
2007 char *server_hostname = NULL;
2008 apr_sockaddr_t *sockaddr;
2011 if (apr_gethostname(str, sizeof(str) - 1, a) != APR_SUCCESS) {
2012 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
2013 "%s: apr_gethostname() failed to determine ServerName",
2016 str[sizeof(str) - 1] = '\0';
2017 if (apr_sockaddr_info_get(&sockaddr, str, APR_UNSPEC, 0, 0, a) == APR_SUCCESS) {
2018 if ( (apr_getnameinfo(&hostname, sockaddr, 0) == APR_SUCCESS) &&
2019 (ap_strchr_c(hostname, '.')) ) {
2020 server_hostname = apr_pstrdup(a, hostname);
2021 return server_hostname;
2022 } else if (ap_strchr_c(str, '.')) {
2023 server_hostname = apr_pstrdup(a, str);
2025 apr_sockaddr_ip_get(&hostname, sockaddr);
2026 server_hostname = apr_pstrdup(a, hostname);
2029 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
2030 "%s: apr_sockaddr_info_get() failed for %s",
2031 ap_server_argv0, str);
2035 if (!server_hostname)
2036 server_hostname = apr_pstrdup(a, "127.0.0.1");
2038 ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, a,
2039 "%s: Could not reliably determine the server's fully qualified "
2040 "domain name, using %s for ServerName",
2041 ap_server_argv0, server_hostname);
2043 return server_hostname;
2046 /* simple 'pool' alloc()ing glue to apr_base64.c
2048 AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
2053 decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
2054 l = apr_base64_decode(decoded, bufcoded);
2055 decoded[l] = '\0'; /* make binary sequence into string */
2060 AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
2063 int l = strlen(string);
2065 encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
2066 l = apr_base64_encode(encoded, string, l);
2067 encoded[l] = '\0'; /* make binary sequence into string */
2072 /* we want to downcase the type/subtype for comparison purposes
2073 * but nothing else because ;parameter=foo values are case sensitive.
2074 * XXX: in truth we want to downcase parameter names... but really,
2075 * apache has never handled parameters and such correctly. You
2076 * also need to compress spaces and such to be able to compare
2079 AP_DECLARE(void) ap_content_type_tolower(char *str)
2083 semi = strchr(str, ';');
2088 ap_str_tolower(str);
2096 * Given a string, replace any bare " with \" .
2098 AP_DECLARE(char *) ap_escape_quotes(apr_pool_t *p, const char *instring)
2101 const char *inchr = instring;
2102 char *outchr, *outstring;
2105 * Look through the input string, jogging the length of the output
2106 * string up by an extra byte each time we find an unescaped ".
2108 while (*inchr != '\0') {
2110 if (*inchr == '"') {
2114 * If we find a slosh, and it's not the last byte in the string,
2115 * it's escaping something - advance past both bytes.
2117 if ((*inchr == '\\') && (inchr[1] != '\0')) {
2123 outstring = apr_palloc(p, newlen + 1);
2127 * Now copy the input string to the output string, inserting a slosh
2128 * in front of every " that doesn't already have one.
2130 while (*inchr != '\0') {
2131 if ((*inchr == '\\') && (inchr[1] != '\0')) {
2132 *outchr++ = *inchr++;
2133 *outchr++ = *inchr++;
2135 if (*inchr == '"') {
2138 if (*inchr != '\0') {
2139 *outchr++ = *inchr++;
2147 * Given a string, append the PID deliminated by delim.
2148 * Usually used to create a pid-appended filepath name
2149 * (eg: /a/b/foo -> /a/b/foo.6726). A function, and not
2150 * a macro, to avoid unistd.h dependency
2152 AP_DECLARE(char *) ap_append_pid(apr_pool_t *p, const char *string,
2155 return apr_psprintf(p, "%s%s%" APR_PID_T_FMT, string,
2161 * Parse a given timeout parameter string into an apr_interval_time_t value.
2162 * The unit of the time interval is given as postfix string to the numeric
2163 * string. Currently the following units are understood:
2170 * If no unit is contained in the given timeout parameter the default_time_unit
2171 * will be used instead.
2172 * @param timeout_parameter The string containing the timeout parameter.
2173 * @param timeout The timeout value to be returned.
2174 * @param default_time_unit The default time unit to use if none is specified
2175 * in timeout_parameter.
2176 * @return Status value indicating whether the parsing was successful or not.
2178 AP_DECLARE(apr_status_t) ap_timeout_parameter_parse(
2179 const char *timeout_parameter,
2180 apr_interval_time_t *timeout,
2181 const char *default_time_unit)
2184 const char *time_str;
2187 tout = apr_strtoi64(timeout_parameter, &endp, 10);
2191 if (!endp || !*endp) {
2192 time_str = default_time_unit;
2198 switch (*time_str) {
2199 /* Time is in seconds */
2201 *timeout = (apr_interval_time_t) apr_time_from_sec(tout);
2204 /* Time is in hours */
2205 *timeout = (apr_interval_time_t) apr_time_from_sec(tout * 3600);
2208 switch (*(++time_str)) {
2209 /* Time is in milliseconds */
2211 *timeout = (apr_interval_time_t) tout * 1000;
2213 /* Time is in minutes */
2215 *timeout = (apr_interval_time_t) apr_time_from_sec(tout * 60);
2218 return APR_EGENERAL;
2222 return APR_EGENERAL;
2228 * Determine if a request has a request body or not.
2230 * @param r the request_rec of the request
2231 * @return truth value
2233 AP_DECLARE(int) ap_request_has_body(request_rec *r)
2240 has_body = (!r->header_only
2242 || apr_table_get(r->headers_in, "Transfer-Encoding")
2243 || ( (cls = apr_table_get(r->headers_in, "Content-Length"))
2244 && (apr_strtoff(&cl, cls, &estr, 10) == APR_SUCCESS)
2252 AP_DECLARE_NONSTD(apr_status_t) ap_pool_cleanup_set_null(void *data_)
2254 void **ptr = (void **)data_;
2259 AP_DECLARE(apr_status_t) ap_str2_alnum(const char *src, char *dest) {
2261 for ( ; *src; src++, dest++)
2263 if (!apr_isprint(*src))
2265 else if (!apr_isalnum(*src))
2275 AP_DECLARE(apr_status_t) ap_pstr2_alnum(apr_pool_t *p, const char *src, char **dest)
2277 *dest = apr_palloc(p, strlen(src)+1);
2280 return ap_str2_alnum(src, *dest);