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 "http_core.h"
56 #include "util_ebcdic.h"
57 #include "util_varbuf.h"
65 #ifdef HAVE_SYS_LOADAVG_H
66 #include <sys/loadavg.h>
71 /* A bunch of functions in util.c scan strings looking for certain characters.
72 * To make that more efficient we encode a lookup table. The test_char_table
73 * is generated automatically by gen_test_char.c.
75 #include "test_char.h"
77 /* we assume the folks using this ensure 0 <= c < 256... which means
78 * you need a cast to (unsigned char) first, you can't just plug a
79 * char in here and get it to work, because if char is signed then it
80 * will first be sign extended.
82 #define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
84 /* Win32/NetWare/OS2 need to check for both forward and back slashes
85 * in ap_getparents() and ap_escape_url.
87 #ifdef CASE_BLIND_FILESYSTEM
88 #define IS_SLASH(s) ((s == '/') || (s == '\\'))
91 #define IS_SLASH(s) (s == '/')
95 /* we know core's module_index is 0 */
96 #undef APLOG_MODULE_INDEX
97 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
101 * Examine a field value (such as a media-/content-type) string and return
102 * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
104 AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
108 if (intype == NULL) return NULL;
110 semi = ap_strchr_c(intype, ';');
112 return apr_pstrdup(p, intype);
115 while ((semi > intype) && apr_isspace(semi[-1])) {
118 return apr_pstrmemdup(p, intype, semi - intype);
122 AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
126 char ts[MAX_STRING_LEN];
127 char tf[MAX_STRING_LEN];
134 apr_time_exp_gmt(&xt, t);
135 /* Convert %Z to "GMT" and %z to "+0000";
136 * on hosts that do not have a time zone string in struct tm,
137 * strftime must assume its argument is local time.
139 for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
141 if (*f != '%') continue;
152 case 'z': /* common extension */
166 apr_time_exp_lt(&xt, t);
169 /* check return code? */
170 apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
171 ts[MAX_STRING_LEN - 1] = '\0';
172 return apr_pstrdup(p, ts);
175 /* Roy owes Rob beer. */
176 /* Rob owes Roy dinner. */
178 /* These legacy comments would make a lot more sense if Roy hadn't
179 * replaced the old later_than() routine with util_date.c.
181 * Well, okay, they still wouldn't make any sense.
184 /* Match = 0, NoMatch = 1, Abort = -1
185 * Based loosely on sections of wildmat.c by Rich Salz
186 * Hmmm... shouldn't this really go component by component?
188 AP_DECLARE(int) ap_strcmp_match(const char *str, const char *expected)
192 for (x = 0, y = 0; expected[y]; ++y, ++x) {
193 if ((!str[x]) && (expected[y] != '*'))
195 if (expected[y] == '*') {
196 while (expected[++y] == '*');
201 if ((ret = ap_strcmp_match(&str[x++], &expected[y])) != 1)
206 else if ((expected[y] != '?') && (str[x] != expected[y]))
209 return (str[x] != '\0');
212 AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *expected)
216 for (x = 0, y = 0; expected[y]; ++y, ++x) {
217 if (!str[x] && expected[y] != '*')
219 if (expected[y] == '*') {
220 while (expected[++y] == '*');
225 if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1)
230 else if (expected[y] != '?'
231 && apr_tolower(str[x]) != apr_tolower(expected[y]))
234 return (str[x] != '\0');
237 /* We actually compare the canonical root to this root, (but we don't
238 * waste time checking the case), since every use of this function in
239 * httpd-2.1 tests if the path is 'proper', meaning we've already passed
240 * it through apr_filepath_merge, or we haven't.
242 AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
245 const char *ourdir = dir;
246 if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
247 || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
253 AP_DECLARE(int) ap_is_matchexp(const char *str)
257 for (x = 0; str[x]; x++)
258 if ((str[x] == '*') || (str[x] == '?'))
264 * Here's a pool-based interface to the POSIX-esque ap_regcomp().
265 * Note that we return ap_regex_t instead of being passed one.
266 * The reason is that if you use an already-used ap_regex_t structure,
267 * the memory that you've already allocated gets forgotten, and
268 * regfree() doesn't clear it. So we don't allow it.
271 static apr_status_t regex_cleanup(void *preg)
273 ap_regfree((ap_regex_t *) preg);
277 AP_DECLARE(ap_regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
280 ap_regex_t *preg = apr_palloc(p, sizeof *preg);
281 int err = ap_regcomp(preg, pattern, cflags);
283 if (err == AP_REG_ESPACE)
288 apr_pool_cleanup_register(p, (void *) preg, regex_cleanup,
289 apr_pool_cleanup_null);
294 AP_DECLARE(void) ap_pregfree(apr_pool_t *p, ap_regex_t *reg)
297 apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
301 * Similar to standard strstr() but we ignore case in this version.
302 * Based on the strstr() implementation further below.
304 AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
312 for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
316 /* found first character of s2, see if the rest matches */
319 for (++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
321 /* both strings ended together */
326 /* second string ended, a match */
329 /* didn't find a match here, try starting at next character in s1 */
336 * Returns an offsetted pointer in bigstring immediately after
337 * prefix. Returns bigstring if bigstring doesn't start with
338 * prefix or if prefix is longer than bigstring while still matching.
339 * NOTE: pointer returned is relative to bigstring, so we
340 * can use standard pointer comparisons in the calling function
341 * (eg: test if ap_stripprefix(a,b) == a)
343 AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
352 while (*p1 && *prefix) {
353 if (*p1++ != *prefix++)
359 /* hit the end of bigstring! */
363 /* This function substitutes for $0-$9, filling in regular expression
364 * submatches. Pass it the same nmatch and pmatch arguments that you
365 * passed ap_regexec(). pmatch should not be greater than the maximum number
366 * of subexpressions - i.e. one more than the re_nsub member of ap_regex_t.
368 * nmatch must be <=AP_MAX_REG_MATCH (10).
370 * input should be the string with the $-expressions, source should be the
371 * string that was matched against.
373 * It returns the substituted string, or NULL if a vbuf is used.
374 * On errors, returns the orig string.
376 * Parts of this code are based on Henry Spencer's regsub(), from his
377 * AT&T V8 regexp package.
380 static apr_status_t regsub_core(apr_pool_t *p, char **result,
381 struct ap_varbuf *vb, const char *input,
382 const char *source, apr_size_t nmatch,
383 ap_regmatch_t pmatch[], apr_size_t maxlen)
385 const char *src = input;
391 AP_DEBUG_ASSERT((result && p && !vb) || (vb && !p && !result));
392 if (!source || nmatch>AP_MAX_REG_MATCH)
396 if (maxlen > 0 && len >= maxlen)
399 *result = apr_pstrmemdup(p, src, len);
403 ap_varbuf_strmemcat(vb, src, len);
408 /* First pass, find the size */
409 while ((c = *src++) != '\0') {
410 if (c == '$' && apr_isdigit(*src))
413 no = AP_MAX_REG_MATCH;
415 if (no >= AP_MAX_REG_MATCH) { /* Ordinary character. */
416 if (c == '\\' && *src)
420 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
421 if (APR_SIZE_MAX - len <= pmatch[no].rm_eo - pmatch[no].rm_so)
423 len += pmatch[no].rm_eo - pmatch[no].rm_so;
428 if (len >= maxlen && maxlen > 0)
432 *result = dst = apr_palloc(p, len + 1);
435 if (vb->strlen == AP_VARBUF_UNKNOWN)
436 vb->strlen = strlen(vb->buf);
437 ap_varbuf_grow(vb, vb->strlen + len);
438 dst = vb->buf + vb->strlen;
442 /* Now actually fill in the string */
446 while ((c = *src++) != '\0') {
447 if (c == '$' && apr_isdigit(*src))
450 no = AP_MAX_REG_MATCH;
452 if (no >= AP_MAX_REG_MATCH) { /* Ordinary character. */
453 if (c == '\\' && *src)
457 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
458 len = pmatch[no].rm_eo - pmatch[no].rm_so;
459 memcpy(dst, source + pmatch[no].rm_so, len);
469 #ifndef AP_PREGSUB_MAXLEN
470 #define AP_PREGSUB_MAXLEN (HUGE_STRING_LEN * 8)
472 AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input,
473 const char *source, apr_size_t nmatch,
474 ap_regmatch_t pmatch[])
477 apr_status_t rc = regsub_core(p, &result, NULL, input, source, nmatch,
478 pmatch, AP_PREGSUB_MAXLEN);
479 if (rc != APR_SUCCESS)
484 AP_DECLARE(apr_status_t) ap_pregsub_ex(apr_pool_t *p, char **result,
485 const char *input, const char *source,
486 apr_size_t nmatch, ap_regmatch_t pmatch[],
489 apr_status_t rc = regsub_core(p, result, NULL, input, source, nmatch,
491 if (rc != APR_SUCCESS)
497 * Parse .. so we don't compromise security
499 AP_DECLARE(void) ap_getparents(char *name)
504 /* Four paseses, as per RFC 1808 */
505 /* a) remove ./ path segments */
506 for (next = name; *next && (*next != '.'); next++) {
509 l = w = first_dot = next - name;
510 while (name[l] != '\0') {
511 if (name[l] == '.' && IS_SLASH(name[l + 1])
512 && (l == 0 || IS_SLASH(name[l - 1])))
515 name[w++] = name[l++];
518 /* b) remove trailing . path, segment */
519 if (w == 1 && name[0] == '.')
521 else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
525 /* c) remove all xx/../ segments. (including leading ../ and /../) */
528 while (name[l] != '\0') {
529 if (name[l] == '.' && name[l + 1] == '.' && IS_SLASH(name[l + 2])
530 && (l == 0 || IS_SLASH(name[l - 1]))) {
535 while (l >= 0 && !IS_SLASH(name[l]))
542 while ((name[n] = name[m]))
549 /* d) remove trailing xx/.. segment. */
550 if (l == 2 && name[0] == '.' && name[1] == '.')
552 else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
553 && IS_SLASH(name[l - 3])) {
556 while (l >= 0 && !IS_SLASH(name[l]))
566 AP_DECLARE(void) ap_no2slash(char *name)
572 #ifdef HAVE_UNC_PATHS
573 /* Check for UNC names. Leave leading two slashes. */
574 if (s[0] == '/' && s[1] == '/')
579 if ((*d++ = *s) == '/') {
593 * copy at most n leading directories of s into d
594 * d should be at least as large as s plus 1 extra byte
596 * the return value is the ever useful pointer to the trailing \0 of d
598 * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
599 * so that if n == 0, "/" is returned in d with n == 1
600 * and s == "e:/test.html", "e:/" is returned in d
601 * *** See also directory_walk in modules/http/http_request.c
604 * /a/b, 0 ==> / (true for all platforms)
613 * c:/a/b 3 ==> c:/a/b
614 * c:/a/b 4 ==> c:/a/b
616 AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
625 if (*s == '\0' || (*s == '/' && (--n) == 0)) {
637 * return the parent directory name including trailing / of the file s
639 AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
641 const char *last_slash = ap_strrchr_c(s, '/');
645 if (last_slash == NULL) {
646 return apr_pstrdup(p, "");
648 l = (last_slash - s) + 1;
649 d = apr_pstrmemdup(p, s, l);
655 AP_DECLARE(int) ap_count_dirs(const char *path)
659 for (x = 0, n = 0; path[x]; x++)
665 AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
667 return ap_getword(atrans, (const char **) line, stop);
670 AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
672 const char *pos = *line;
676 while ((*pos != stop) && *pos) {
681 res = apr_pstrmemdup(atrans, *line, len);
684 while (*pos == stop) {
693 AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
695 return ap_getword_white(atrans, (const char **) line);
698 AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
700 const char *pos = *line;
704 while (!apr_isspace(*pos) && *pos) {
709 res = apr_pstrmemdup(atrans, *line, len);
711 while (apr_isspace(*pos)) {
720 AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line,
723 return ap_getword_nulls(atrans, (const char **) line, stop);
726 AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line,
729 const char *pos = ap_strchr_c(*line, stop);
733 apr_size_t len = strlen(*line);
734 res = apr_pstrmemdup(atrans, *line, len);
739 res = apr_pstrmemdup(atrans, *line, pos - *line);
748 /* Get a word, (new) config-file style --- quoted strings and backslashes
752 static char *substring_conf(apr_pool_t *p, const char *start, int len,
755 char *result = apr_palloc(p, len + 1);
759 for (i = 0; i < len; ++i) {
760 if (start[i] == '\\' && (start[i + 1] == '\\'
761 || (quote && start[i + 1] == quote)))
762 *resp++ = start[++i];
768 #if RESOLVE_ENV_PER_TOKEN
769 return (char *)ap_resolve_env(p,result);
775 AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
777 return ap_getword_conf(p, (const char **) line);
780 AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
782 const char *str = *line, *strend;
786 while (apr_isspace(*str))
794 if ((quote = *str) == '"' || quote == '\'') {
796 while (*strend && *strend != quote) {
797 if (*strend == '\\' && strend[1] &&
798 (strend[1] == quote || strend[1] == '\\')) {
805 res = substring_conf(p, str + 1, strend - str - 1, quote);
807 if (*strend == quote)
812 while (*strend && !apr_isspace(*strend))
815 res = substring_conf(p, str, strend - str, 0);
818 while (apr_isspace(*strend))
824 AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
827 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, APLOGNO(00551)
828 "Done with config file %s", cfp->name);
830 return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
833 /* we can't use apr_file_* directly because of linking issues on Windows */
834 static apr_status_t cfg_close(void *param)
836 return apr_file_close(param);
839 static apr_status_t cfg_getch(char *ch, void *param)
841 return apr_file_getc(ch, param);
844 static apr_status_t cfg_getstr(void *buf, apr_size_t bufsiz, void *param)
846 return apr_file_gets(buf, bufsiz, param);
849 /* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
850 AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg,
851 apr_pool_t *p, const char *name)
853 ap_configfile_t *new_cfg;
854 apr_file_t *file = NULL;
862 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(00552)
863 "Internal error: pcfg_openfile() called with NULL filename");
867 status = apr_file_open(&file, name, APR_READ | APR_BUFFERED,
870 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, APLOGNO(00553)
871 "Opening config file %s (%s)",
872 name, (status != APR_SUCCESS) ?
873 apr_strerror(status, buf, sizeof(buf)) : "successful");
875 if (status != APR_SUCCESS)
878 status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
879 if (status != APR_SUCCESS)
882 if (finfo.filetype != APR_REG &&
883 #if defined(WIN32) || defined(OS2) || defined(NETWARE)
884 strcasecmp(apr_filepath_name_get(name), "nul") != 0) {
886 strcmp(name, "/dev/null") != 0) {
887 #endif /* WIN32 || OS2 */
888 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(00554)
889 "Access to file %s denied by server: not a regular file",
891 apr_file_close(file);
896 /* Some twisted character [no pun intended] at MS decided that a
897 * zero width joiner as the lead wide character would be ideal for
898 * describing Unicode text files. This was further convoluted to
899 * another MSism that the same character mapped into utf-8, EF BB BF
900 * would signify utf-8 text files.
902 * Since MS configuration files are all protecting utf-8 encoded
903 * Unicode path, file and resource names, we already have the correct
904 * WinNT encoding. But at least eat the stupid three bytes up front.
907 unsigned char buf[4];
909 status = apr_file_read(file, buf, &len);
910 if ((status != APR_SUCCESS) || (len < 3)
911 || memcmp(buf, "\xEF\xBB\xBF", 3) != 0) {
913 apr_file_seek(file, APR_SET, &zero);
918 new_cfg = apr_palloc(p, sizeof(*new_cfg));
919 new_cfg->param = file;
920 new_cfg->name = apr_pstrdup(p, name);
921 new_cfg->getch = cfg_getch;
922 new_cfg->getstr = cfg_getstr;
923 new_cfg->close = cfg_close;
924 new_cfg->line_number = 0;
930 /* Allocate a ap_configfile_t handle with user defined functions and params */
931 AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(
932 apr_pool_t *p, const char *descr, void *param,
933 apr_status_t (*getc_func) (char *ch, void *param),
934 apr_status_t (*gets_func) (void *buf, apr_size_t bufsize, void *param),
935 apr_status_t (*close_func) (void *param))
937 ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
938 new_cfg->param = param;
939 new_cfg->name = descr;
940 new_cfg->getch = getc_func;
941 new_cfg->getstr = gets_func;
942 new_cfg->close = close_func;
943 new_cfg->line_number = 0;
947 /* Read one character from a configfile_t */
948 AP_DECLARE(apr_status_t) ap_cfg_getc(char *ch, ap_configfile_t *cfp)
950 apr_status_t rc = cfp->getch(ch, cfp->param);
951 if (rc == APR_SUCCESS && *ch == LF)
956 AP_DECLARE(const char *) ap_pcfg_strerror(apr_pool_t *p, ap_configfile_t *cfp,
959 if (rc == APR_SUCCESS)
962 if (rc == APR_ENOSPC)
963 return apr_psprintf(p, "Error reading %s at line %d: Line too long",
964 cfp->name, cfp->line_number);
966 return apr_psprintf(p, "Error reading %s at line %d: %pm",
967 cfp->name, cfp->line_number, &rc);
970 /* Read one line from open ap_configfile_t, strip LF, increase line number */
971 /* If custom handler does not define a getstr() function, read char by char */
972 static apr_status_t ap_cfg_getline_core(char *buf, apr_size_t bufsize,
973 apr_size_t offset, ap_configfile_t *cfp)
976 /* If a "get string" function is defined, use it */
977 if (cfp->getstr != NULL) {
979 char *cbuf = buf + offset;
980 apr_size_t cbufsize = bufsize - offset;
984 rc = cfp->getstr(cbuf, cbufsize, cfp->param);
986 if (cbuf != buf + offset) {
994 if (rc != APR_SUCCESS) {
999 * check for line continuation,
1000 * i.e. match [^\\]\\[\r]\n only
1004 if (cp > buf && cp[-1] == LF) {
1006 if (cp > buf && cp[-1] == CR)
1008 if (cp > buf && cp[-1] == '\\') {
1011 * line continuation requested -
1012 * then remove backslash and continue
1014 cbufsize -= (cp-cbuf);
1019 else if (cp - buf >= bufsize - 1) {
1025 /* No "get string" function defined; read character by character */
1026 apr_size_t i = offset;
1029 /* too small, assume caller is crazy */
1036 rc = cfp->getch(&c, cfp->param);
1037 if (rc == APR_EOF) {
1043 if (rc != APR_SUCCESS)
1047 /* check for line continuation */
1048 if (i > 0 && buf[i-1] == '\\') {
1058 if (i >= bufsize - 1) {
1067 static int cfg_trim_line(char *buf)
1071 * Leading and trailing white space is eliminated completely
1074 while (apr_isspace(*start))
1076 /* blast trailing whitespace */
1077 end = &start[strlen(start)];
1078 while (--end >= start && apr_isspace(*end))
1080 /* Zap leading whitespace by shifting */
1082 memmove(buf, start, end - start + 2);
1083 #ifdef DEBUG_CFG_LINES
1084 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, NULL, APLOGNO(00555) "Read config: '%s'", buf);
1086 return end - start + 1;
1089 /* Read one line from open ap_configfile_t, strip LF, increase line number */
1090 /* If custom handler does not define a getstr() function, read char by char */
1091 AP_DECLARE(apr_status_t) ap_cfg_getline(char *buf, apr_size_t bufsize,
1092 ap_configfile_t *cfp)
1094 apr_status_t rc = ap_cfg_getline_core(buf, bufsize, 0, cfp);
1095 if (rc == APR_SUCCESS)
1100 AP_DECLARE(apr_status_t) ap_varbuf_cfg_getline(struct ap_varbuf *vb,
1101 ap_configfile_t *cfp,
1109 if (vb->strlen == AP_VARBUF_UNKNOWN)
1110 vb->strlen = strlen(vb->buf);
1111 if (vb->avail - vb->strlen < 3) {
1112 new_len = vb->avail * 2;
1113 if (new_len > max_len)
1115 else if (new_len < 3)
1117 ap_varbuf_grow(vb, new_len);
1121 rc = ap_cfg_getline_core(vb->buf, vb->avail, vb->strlen, cfp);
1122 if (rc == APR_ENOSPC || rc == APR_SUCCESS)
1123 vb->strlen += strlen(vb->buf + vb->strlen);
1124 if (rc != APR_ENOSPC)
1126 if (vb->avail >= max_len)
1128 new_len = vb->avail * 2;
1129 if (new_len > max_len)
1131 ap_varbuf_grow(vb, new_len);
1134 if (vb->strlen > max_len)
1136 if (rc == APR_SUCCESS)
1137 vb->strlen = cfg_trim_line(vb->buf);
1141 /* Size an HTTP header field list item, as separated by a comma.
1142 * The return value is a pointer to the beginning of the non-empty list item
1143 * within the original string (or NULL if there is none) and the address
1144 * of field is shifted to the next non-comma, non-whitespace character.
1145 * len is the length of the item excluding any beginning whitespace.
1147 AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1149 const unsigned char *ptr = (const unsigned char *)*field;
1150 const unsigned char *token;
1151 int in_qpair, in_qstr, in_com;
1153 /* Find first non-comma, non-whitespace byte */
1155 while (*ptr == ',' || apr_isspace(*ptr))
1160 /* Find the end of this item, skipping over dead bits */
1162 for (in_qpair = in_qstr = in_com = 0;
1163 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1171 case '\\': in_qpair = 1; /* quoted-pair */
1173 case '"' : if (!in_com) /* quoted string delim */
1176 case '(' : if (!in_qstr) /* comment (may nest) */
1179 case ')' : if (in_com) /* end comment */
1187 if ((*len = (ptr - token)) == 0) {
1188 *field = (const char *)ptr;
1192 /* Advance field pointer to the next non-comma, non-white byte */
1194 while (*ptr == ',' || apr_isspace(*ptr))
1197 *field = (const char *)ptr;
1198 return (const char *)token;
1201 /* Retrieve an HTTP header field list item, as separated by a comma,
1202 * while stripping insignificant whitespace and lowercasing anything not in
1203 * a quoted string or comment. The return value is a new string containing
1204 * the converted list item (or NULL if none) and the address pointed to by
1205 * field is shifted to the next non-comma, non-whitespace.
1207 AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1209 const char *tok_start;
1210 const unsigned char *ptr;
1213 int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1215 /* Find the beginning and maximum length of the list item so that
1216 * we can allocate a buffer for the new string and reset the field.
1218 if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1221 token = apr_palloc(p, tok_len + 1);
1223 /* Scan the token again, but this time copy only the good bytes.
1224 * We skip extra whitespace and any whitespace around a '=', '/',
1225 * or ';' and lowercase normal characters not within a comment,
1226 * quoted-string or quoted-pair.
1228 for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1229 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1238 case '\\': in_qpair = 1;
1244 case '"' : if (!in_com)
1251 case '(' : if (!in_qstr)
1258 case ')' : if (in_com)
1264 case '\t': if (addspace)
1266 if (in_com || in_qstr)
1273 case ';' : if (!(in_com || in_qstr))
1277 default : if (addspace == 1)
1279 *pos++ = (in_com || in_qstr) ? *ptr
1280 : apr_tolower(*ptr);
1291 typedef enum ap_etag_e {
1297 /* Find an item in canonical form (lowercase, no extra spaces) within
1298 * an HTTP field value list. Returns 1 if found, 0 if not found.
1299 * This would be much more efficient if we stored header fields as
1300 * an array of list items as they are received instead of a plain string.
1302 static int find_list_item(apr_pool_t *p, const char *line,
1303 const char *tok, ap_etag_e type)
1305 const unsigned char *pos;
1306 const unsigned char *ptr = (const unsigned char *)line;
1307 int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1309 if (!line || !tok) {
1312 if (type == AP_ETAG_STRONG && *tok != '\"') {
1315 if (type == AP_ETAG_WEAK) {
1316 if (*tok == 'W' && (*(tok+1)) == '/' && (*(tok+2)) == '\"') {
1319 else if (*tok != '\"') {
1324 do { /* loop for each item in line's list */
1326 /* Find first non-comma, non-whitespace byte */
1327 while (*ptr == ',' || apr_isspace(*ptr)) {
1331 /* Account for strong or weak Etags, depending on our search */
1332 if (type == AP_ETAG_STRONG && *ptr != '\"') {
1335 if (type == AP_ETAG_WEAK) {
1336 if (*ptr == 'W' && (*(ptr+1)) == '/' && (*(ptr+2)) == '\"') {
1339 else if (*ptr != '\"') {
1345 good = 1; /* until proven otherwise for this item */
1347 break; /* no items left and nothing good found */
1349 /* We skip extra whitespace and any whitespace around a '=', '/',
1350 * or ';' and lowercase normal characters not within a comment,
1351 * quoted-string or quoted-pair.
1353 for (pos = (const unsigned char *)tok;
1354 *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1360 good = (*pos++ == *ptr);
1364 case '\\': in_qpair = 1;
1366 good = good && (*pos++ == ' ');
1367 good = good && (*pos++ == *ptr);
1370 case '"' : if (!in_com)
1373 good = good && (*pos++ == ' ');
1374 good = good && (*pos++ == *ptr);
1377 case '(' : if (!in_qstr)
1380 good = good && (*pos++ == ' ');
1381 good = good && (*pos++ == *ptr);
1384 case ')' : if (in_com)
1386 good = good && (*pos++ == *ptr);
1390 case '\t': if (addspace || !good)
1392 if (in_com || in_qstr)
1393 good = (*pos++ == *ptr);
1399 case ';' : if (!(in_com || in_qstr))
1401 good = good && (*pos++ == *ptr);
1403 default : if (!good)
1406 good = (*pos++ == ' ');
1407 if (in_com || in_qstr)
1408 good = good && (*pos++ == *ptr);
1411 && (apr_tolower(*pos++) == apr_tolower(*ptr));
1418 good = 0; /* not good if only a prefix was matched */
1420 } while (*ptr && !good);
1425 /* Find an item in canonical form (lowercase, no extra spaces) within
1426 * an HTTP field value list. Returns 1 if found, 0 if not found.
1427 * This would be much more efficient if we stored header fields as
1428 * an array of list items as they are received instead of a plain string.
1430 AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line,
1433 return find_list_item(p, line, tok, AP_ETAG_NONE);
1436 /* Find a strong Etag in canonical form (lowercase, no extra spaces) within
1437 * an HTTP field value list. Returns 1 if found, 0 if not found.
1439 AP_DECLARE(int) ap_find_etag_strong(apr_pool_t *p, const char *line,
1442 return find_list_item(p, line, tok, AP_ETAG_STRONG);
1445 /* Find a weak ETag in canonical form (lowercase, no extra spaces) within
1446 * an HTTP field value list. Returns 1 if found, 0 if not found.
1448 AP_DECLARE(int) ap_find_etag_weak(apr_pool_t *p, const char *line,
1451 return find_list_item(p, line, tok, AP_ETAG_WEAK);
1454 /* Grab a list of tokens of the format 1#token (from RFC7230) */
1455 AP_DECLARE(const char *) ap_parse_token_list_strict(apr_pool_t *p,
1457 apr_array_header_t **tokens,
1460 int in_leading_space = 1;
1461 int in_trailing_space = 0;
1463 const char *tok_begin;
1470 tok_begin = cur = str_in;
1472 while (!string_end) {
1473 const unsigned char c = (unsigned char)*cur;
1475 if (!TEST_CHAR(c, T_HTTP_TOKEN_STOP) && c != '\0') {
1476 /* Non-separator character; we are finished with leading
1477 * whitespace. We must never have encountered any trailing
1478 * whitespace before the delimiter (comma) */
1479 in_leading_space = 0;
1480 if (in_trailing_space) {
1481 return "Encountered illegal whitespace in token";
1484 else if (c == ' ' || c == '\t') {
1485 /* "Linear whitespace" only includes ASCII CRLF, space, and tab;
1486 * we can't get a CRLF since headers are split on them already,
1487 * so only look for a space or a tab */
1488 if (in_leading_space) {
1489 /* We're still in leading whitespace */
1493 /* We must be in trailing whitespace */
1494 ++in_trailing_space;
1497 else if (c == ',' || c == '\0') {
1498 if (!in_leading_space) {
1499 /* If we're out of the leading space, we know we've read some
1500 * characters of a token */
1501 if (*tokens == NULL) {
1502 *tokens = apr_array_make(p, 4, sizeof(char *));
1504 APR_ARRAY_PUSH(*tokens, char *) =
1505 apr_pstrmemdup((*tokens)->pool, tok_begin,
1506 (cur - tok_begin) - in_trailing_space);
1508 /* We're allowed to have null elements, just don't add them to the
1511 tok_begin = cur + 1;
1512 in_leading_space = 1;
1513 in_trailing_space = 0;
1514 string_end = (c == '\0');
1517 /* Encountered illegal separator char */
1519 /* Skip to the next separator */
1521 temp = ap_strchr_c(cur, ',');
1523 temp = ap_strchr_c(cur, '\0');
1526 /* Act like we haven't seen a token so we reset */
1528 in_leading_space = 1;
1529 in_trailing_space = 0;
1532 return apr_psprintf(p, "Encountered illegal separator "
1533 "'\\x%.2x'", (unsigned int)c);
1543 /* Retrieve a token, spacing over it and returning a pointer to
1544 * the first non-white byte afterwards. Note that these tokens
1545 * are delimited by semis and commas; and can also be delimited
1546 * by whitespace at the caller's option.
1549 AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line,
1552 const char *ptr = *accept_line;
1553 const char *tok_start;
1556 /* Find first non-white byte */
1558 while (apr_isspace(*ptr))
1563 /* find token end, skipping over quoted strings.
1564 * (comments are already gone).
1567 while (*ptr && (accept_white || !apr_isspace(*ptr))
1568 && *ptr != ';' && *ptr != ',') {
1575 token = apr_pstrmemdup(p, tok_start, ptr - tok_start);
1577 /* Advance accept_line pointer to the next non-white byte */
1579 while (apr_isspace(*ptr))
1587 /* find http tokens, see the definition of token from RFC2068 */
1588 AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1590 const unsigned char *start_token;
1591 const unsigned char *s;
1596 s = (const unsigned char *)line;
1598 /* find start of token, skip all stop characters, note NUL
1599 * isn't a token stop, so we don't need to test for it
1601 while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1608 /* find end of the token */
1609 while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1612 if (!strncasecmp((const char *)start_token, (const char *)tok,
1623 AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line,
1626 int llen, tlen, lidx;
1631 llen = strlen(line);
1636 (lidx > 0 && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1639 return (strncasecmp(&line[lidx], tok, tlen) == 0);
1642 AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1646 const unsigned char *s;
1648 cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1649 d = (unsigned char *)cmd;
1650 s = (const unsigned char *)str;
1653 #if defined(OS2) || defined(WIN32)
1655 * Newlines to Win32/OS2 CreateProcess() are ill advised.
1656 * Convert them to spaces since they are effectively white
1657 * space to most applications
1659 if (*s == '\r' || *s == '\n') {
1665 if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1675 static char x2c(const char *what)
1679 #if !APR_CHARSET_EBCDIC
1680 digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
1683 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
1685 #else /*APR_CHARSET_EBCDIC*/
1692 digit = apr_xlate_conv_byte(ap_hdrs_from_ascii,
1693 0xFF & strtol(xstr, NULL, 16));
1694 #endif /*APR_CHARSET_EBCDIC*/
1699 * Unescapes a URL, leaving reserved characters intact.
1700 * Returns 0 on success, non-zero on error
1702 * bad % escape returns HTTP_BAD_REQUEST
1704 * decoding %00 or a forbidden character returns HTTP_NOT_FOUND
1707 static int unescape_url(char *url, const char *forbid, const char *reserved)
1709 int badesc, badpath;
1714 /* Initial scan for first '%'. Don't bother writing values before
1716 y = strchr(url, '%');
1720 for (x = y; *y; ++x, ++y) {
1725 if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1731 decoded = x2c(y + 1);
1732 if ((decoded == '\0')
1733 || (forbid && ap_strchr_c(forbid, decoded))) {
1738 else if (reserved && ap_strchr_c(reserved, decoded)) {
1752 return HTTP_BAD_REQUEST;
1755 return HTTP_NOT_FOUND;
1761 AP_DECLARE(int) ap_unescape_url(char *url)
1764 return unescape_url(url, SLASHES, NULL);
1766 AP_DECLARE(int) ap_unescape_url_keep2f(char *url, int decode_slashes)
1768 /* AllowEncodedSlashes (corrected) */
1769 if (decode_slashes) {
1770 /* no chars reserved */
1771 return unescape_url(url, NULL, NULL);
1773 /* reserve (do not decode) encoded slashes */
1774 return unescape_url(url, NULL, SLASHES);
1778 /* IFDEF these out until they've been thought through.
1779 * Just a germ of an API extension for now
1781 AP_DECLARE(int) ap_unescape_url_proxy(char *url)
1783 /* leave RFC1738 reserved characters intact, * so proxied URLs
1784 * don't get mangled. Where does that leave encoded '&' ?
1786 return unescape_url(url, NULL, "/;?");
1788 AP_DECLARE(int) ap_unescape_url_reserved(char *url, const char *reserved)
1790 return unescape_url(url, NULL, reserved);
1794 AP_DECLARE(int) ap_unescape_urlencoded(char *query)
1798 /* replace plus with a space */
1800 for (slider = query; *slider; slider++) {
1801 if (*slider == '+') {
1807 /* unescape everything else */
1808 return unescape_url(query, NULL, NULL);
1811 AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1812 apr_port_t port, const request_rec *r)
1814 if (ap_is_default_port(port, r)) {
1815 return apr_pstrdup(p, hostname);
1818 return apr_psprintf(p, "%s:%u", hostname, port);
1822 AP_DECLARE(int) ap_unescape_all(char *url)
1824 return unescape_url(url, NULL, NULL);
1827 /* c2x takes an unsigned, and expects the caller has guaranteed that
1828 * 0 <= what < 256... which usually means that you have to cast to
1829 * unsigned char first, because (unsigned)(char)(x) first goes through
1830 * signed extension to an int before the unsigned cast.
1832 * The reason for this assumption is to assist gcc code generation --
1833 * the unsigned char -> unsigned extension is already done earlier in
1834 * both uses of this code, so there's no need to waste time doing it
1837 static const char c2x_table[] = "0123456789abcdef";
1839 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
1840 unsigned char *where)
1842 #if APR_CHARSET_EBCDIC
1843 what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1844 #endif /*APR_CHARSET_EBCDIC*/
1846 *where++ = c2x_table[what >> 4];
1847 *where++ = c2x_table[what & 0xf];
1852 * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1853 * routine is (should be) OS independent.
1855 * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1856 * cases if a ':' occurs before the first '/' in the URL, the URL should be
1857 * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1858 * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1859 * efficiency reasons, we don't use escape_path_segment(), which is provided for
1860 * reference. Again, RFC 1808 is where this stuff is defined.
1862 * If partial is set, os_escape_path() assumes that the path will be appended to
1863 * something with a '/' in it (and thus does not prefix "./").
1866 AP_DECLARE(char *) ap_escape_path_segment_buffer(char *copy, const char *segment)
1868 const unsigned char *s = (const unsigned char *)segment;
1869 unsigned char *d = (unsigned char *)copy;
1873 if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1885 AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1887 return ap_escape_path_segment_buffer(apr_palloc(p, 3 * strlen(segment) + 1), segment);
1890 AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1892 char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1893 const unsigned char *s = (const unsigned char *)path;
1894 unsigned char *d = (unsigned char *)copy;
1898 const char *colon = ap_strchr_c(path, ':');
1899 const char *slash = ap_strchr_c(path, '/');
1901 if (colon && (!slash || colon < slash)) {
1907 if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1919 AP_DECLARE(char *) ap_escape_urlencoded_buffer(char *copy, const char *buffer)
1921 const unsigned char *s = (const unsigned char *)buffer;
1922 unsigned char *d = (unsigned char *)copy;
1926 if (TEST_CHAR(c, T_ESCAPE_URLENCODED)) {
1929 else if (c == ' ') {
1941 AP_DECLARE(char *) ap_escape_urlencoded(apr_pool_t *p, const char *buffer)
1943 return ap_escape_urlencoded_buffer(apr_palloc(p, 3 * strlen(buffer) + 1), buffer);
1946 /* ap_escape_uri is now a macro for os_escape_path */
1948 AP_DECLARE(char *) ap_escape_html2(apr_pool_t *p, const char *s, int toasc)
1953 /* first, count the number of extra characters */
1954 for (i = 0, j = 0; s[i] != '\0'; i++)
1955 if (s[i] == '<' || s[i] == '>')
1957 else if (s[i] == '&')
1959 else if (s[i] == '"')
1961 else if (toasc && !apr_isascii(s[i]))
1965 return apr_pstrmemdup(p, s, i);
1967 x = apr_palloc(p, i + j + 1);
1968 for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1970 memcpy(&x[j], "<", 4);
1973 else if (s[i] == '>') {
1974 memcpy(&x[j], ">", 4);
1977 else if (s[i] == '&') {
1978 memcpy(&x[j], "&", 5);
1981 else if (s[i] == '"') {
1982 memcpy(&x[j], """, 6);
1985 else if (toasc && !apr_isascii(s[i])) {
1986 char *esc = apr_psprintf(p, "&#%3.3d;", (unsigned char)s[i]);
1987 memcpy(&x[j], esc, 6);
1996 AP_DECLARE(char *) ap_escape_logitem(apr_pool_t *p, const char *str)
2000 const unsigned char *s;
2001 apr_size_t length, escapes = 0;
2007 /* Compute how many characters need to be escaped */
2008 s = (const unsigned char *)str;
2010 if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
2015 /* Compute the length of the input string, including NULL */
2016 length = s - (const unsigned char *)str + 1;
2018 /* Fast path: nothing to escape */
2020 return apr_pmemdup(p, str, length);
2023 /* Each escaped character needs up to 3 extra bytes (0 --> \x00) */
2024 ret = apr_palloc(p, length + 3 * escapes);
2025 d = (unsigned char *)ret;
2026 s = (const unsigned char *)str;
2028 if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
2064 AP_DECLARE(apr_size_t) ap_escape_errorlog_item(char *dest, const char *source,
2067 unsigned char *d, *ep;
2068 const unsigned char *s;
2070 if (!source || !buflen) { /* be safe */
2074 d = (unsigned char *)dest;
2075 s = (const unsigned char *)source;
2076 ep = d + buflen - 1;
2078 for (; d < ep && *s; ++s) {
2080 if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
2106 case '"': /* no need for this in error log */
2111 ep = --d; /* break the for loop as well */
2124 return (d - (unsigned char *)dest);
2127 AP_DECLARE(void) ap_bin2hex(const void *src, apr_size_t srclen, char *dest)
2129 const unsigned char *in = src;
2132 for (i = 0; i < srclen; i++) {
2133 *dest++ = c2x_table[in[i] >> 4];
2134 *dest++ = c2x_table[in[i] & 0xf];
2139 AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
2143 if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
2144 return 0; /* in error condition, just return no */
2146 return (finfo.filetype == APR_DIR);
2149 AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
2153 if (apr_stat(&finfo, path, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
2154 return 0; /* in error condition, just return no */
2156 return (finfo.filetype == APR_DIR);
2159 AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
2162 apr_size_t len1, len2;
2165 len1 = strlen(src1);
2166 len2 = strlen(src2);
2167 /* allocate +3 for '/' delimiter, trailing NULL and overallocate
2168 * one extra byte to allow the caller to add a trailing '/'
2170 path = (char *)apr_palloc(a, len1 + len2 + 3);
2173 memcpy(path + 1, src2, len2 + 1);
2177 memcpy(path, src1, len1);
2179 if (next[-1] != '/') {
2182 memcpy(next, src2, len2 + 1);
2188 * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
2190 AP_DECLARE(int) ap_is_url(const char *u)
2194 for (x = 0; u[x] != ':'; x++) {
2196 ((!apr_isalnum(u[x])) &&
2197 (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
2202 return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
2205 AP_DECLARE(int) ap_ind(const char *s, char c)
2207 const char *p = ap_strchr_c(s, c);
2214 AP_DECLARE(int) ap_rind(const char *s, char c)
2216 const char *p = ap_strrchr_c(s, c);
2223 AP_DECLARE(void) ap_str_tolower(char *str)
2226 *str = apr_tolower(*str);
2231 AP_DECLARE(void) ap_str_toupper(char *str)
2234 *str = apr_toupper(*str);
2240 * We must return a FQDN
2242 char *ap_get_local_host(apr_pool_t *a)
2244 #ifndef MAXHOSTNAMELEN
2245 #define MAXHOSTNAMELEN 256
2247 char str[MAXHOSTNAMELEN + 1];
2248 char *server_hostname = NULL;
2249 apr_sockaddr_t *sockaddr;
2252 if (apr_gethostname(str, sizeof(str) - 1, a) != APR_SUCCESS) {
2253 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a, APLOGNO(00556)
2254 "%s: apr_gethostname() failed to determine ServerName",
2257 str[sizeof(str) - 1] = '\0';
2258 if (apr_sockaddr_info_get(&sockaddr, str, APR_UNSPEC, 0, 0, a) == APR_SUCCESS) {
2259 if ( (apr_getnameinfo(&hostname, sockaddr, 0) == APR_SUCCESS) &&
2260 (ap_strchr_c(hostname, '.')) ) {
2261 server_hostname = apr_pstrdup(a, hostname);
2262 return server_hostname;
2263 } else if (ap_strchr_c(str, '.')) {
2264 server_hostname = apr_pstrdup(a, str);
2266 apr_sockaddr_ip_get(&hostname, sockaddr);
2267 server_hostname = apr_pstrdup(a, hostname);
2270 ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a, APLOGNO(00557)
2271 "%s: apr_sockaddr_info_get() failed for %s",
2272 ap_server_argv0, str);
2276 if (!server_hostname)
2277 server_hostname = apr_pstrdup(a, "127.0.0.1");
2279 ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, a, APLOGNO(00558)
2280 "%s: Could not reliably determine the server's fully qualified "
2281 "domain name, using %s. Set the 'ServerName' directive globally "
2282 "to suppress this message",
2283 ap_server_argv0, server_hostname);
2285 return server_hostname;
2288 /* simple 'pool' alloc()ing glue to apr_base64.c
2290 AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
2295 decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
2296 l = apr_base64_decode(decoded, bufcoded);
2297 decoded[l] = '\0'; /* make binary sequence into string */
2302 AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
2305 int l = strlen(string);
2307 encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
2308 l = apr_base64_encode(encoded, string, l);
2309 encoded[l] = '\0'; /* make binary sequence into string */
2314 /* we want to downcase the type/subtype for comparison purposes
2315 * but nothing else because ;parameter=foo values are case sensitive.
2316 * XXX: in truth we want to downcase parameter names... but really,
2317 * apache has never handled parameters and such correctly. You
2318 * also need to compress spaces and such to be able to compare
2321 AP_DECLARE(void) ap_content_type_tolower(char *str)
2325 semi = strchr(str, ';');
2330 ap_str_tolower(str);
2338 * Given a string, replace any bare " with \" .
2340 AP_DECLARE(char *) ap_escape_quotes(apr_pool_t *p, const char *instring)
2343 const char *inchr = instring;
2344 char *outchr, *outstring;
2347 * Look through the input string, jogging the length of the output
2348 * string up by an extra byte each time we find an unescaped ".
2350 while (*inchr != '\0') {
2352 if (*inchr == '"') {
2356 * If we find a slosh, and it's not the last byte in the string,
2357 * it's escaping something - advance past both bytes.
2359 if ((*inchr == '\\') && (inchr[1] != '\0')) {
2365 outstring = apr_palloc(p, newlen + 1);
2369 * Now copy the input string to the output string, inserting a slosh
2370 * in front of every " that doesn't already have one.
2372 while (*inchr != '\0') {
2373 if ((*inchr == '\\') && (inchr[1] != '\0')) {
2374 *outchr++ = *inchr++;
2375 *outchr++ = *inchr++;
2377 if (*inchr == '"') {
2380 if (*inchr != '\0') {
2381 *outchr++ = *inchr++;
2389 * Given a string, append the PID deliminated by delim.
2390 * Usually used to create a pid-appended filepath name
2391 * (eg: /a/b/foo -> /a/b/foo.6726). A function, and not
2392 * a macro, to avoid unistd.h dependency
2394 AP_DECLARE(char *) ap_append_pid(apr_pool_t *p, const char *string,
2397 return apr_psprintf(p, "%s%s%" APR_PID_T_FMT, string,
2403 * Parse a given timeout parameter string into an apr_interval_time_t value.
2404 * The unit of the time interval is given as postfix string to the numeric
2405 * string. Currently the following units are understood:
2412 * If no unit is contained in the given timeout parameter the default_time_unit
2413 * will be used instead.
2414 * @param timeout_parameter The string containing the timeout parameter.
2415 * @param timeout The timeout value to be returned.
2416 * @param default_time_unit The default time unit to use if none is specified
2417 * in timeout_parameter.
2418 * @return Status value indicating whether the parsing was successful or not.
2420 AP_DECLARE(apr_status_t) ap_timeout_parameter_parse(
2421 const char *timeout_parameter,
2422 apr_interval_time_t *timeout,
2423 const char *default_time_unit)
2426 const char *time_str;
2429 tout = apr_strtoi64(timeout_parameter, &endp, 10);
2433 if (!endp || !*endp) {
2434 time_str = default_time_unit;
2440 switch (*time_str) {
2441 /* Time is in seconds */
2443 *timeout = (apr_interval_time_t) apr_time_from_sec(tout);
2446 /* Time is in hours */
2447 *timeout = (apr_interval_time_t) apr_time_from_sec(tout * 3600);
2450 switch (*(++time_str)) {
2451 /* Time is in milliseconds */
2453 *timeout = (apr_interval_time_t) tout * 1000;
2455 /* Time is in minutes */
2457 *timeout = (apr_interval_time_t) apr_time_from_sec(tout * 60);
2460 return APR_EGENERAL;
2464 return APR_EGENERAL;
2470 * Determine if a request has a request body or not.
2472 * @param r the request_rec of the request
2473 * @return truth value
2475 AP_DECLARE(int) ap_request_has_body(request_rec *r)
2482 has_body = (!r->header_only
2484 || apr_table_get(r->headers_in, "Transfer-Encoding")
2485 || ( (cls = apr_table_get(r->headers_in, "Content-Length"))
2486 && (apr_strtoff(&cl, cls, &estr, 10) == APR_SUCCESS)
2494 AP_DECLARE_NONSTD(apr_status_t) ap_pool_cleanup_set_null(void *data_)
2496 void **ptr = (void **)data_;
2501 AP_DECLARE(apr_status_t) ap_str2_alnum(const char *src, char *dest) {
2503 for ( ; *src; src++, dest++)
2505 if (!apr_isprint(*src))
2507 else if (!apr_isalnum(*src))
2517 AP_DECLARE(apr_status_t) ap_pstr2_alnum(apr_pool_t *p, const char *src,
2520 char *new = apr_palloc(p, strlen(src)+1);
2524 return ap_str2_alnum(src, new);
2528 * Read the body and parse any form found, which must be of the
2529 * type application/x-www-form-urlencoded.
2531 * Name/value pairs are returned in an array, with the names as
2532 * strings with a maximum length of HUGE_STRING_LEN, and the
2533 * values as bucket brigades. This allows values to be arbitrarily
2536 * All url-encoding is removed from both the names and the values
2537 * on the fly. The names are interpreted as strings, while the
2538 * values are interpreted as blocks of binary data, that may
2539 * contain the 0 character.
2541 * In order to ensure that resource limits are not exceeded, a
2542 * maximum size must be provided. If the sum of the lengths of
2543 * the names and the values exceed this size, this function
2544 * will return HTTP_REQUEST_ENTITY_TOO_LARGE.
2546 * An optional number of parameters can be provided, if the number
2547 * of parameters provided exceeds this amount, this function will
2548 * return HTTP_REQUEST_ENTITY_TOO_LARGE. If this value is negative,
2549 * no limit is imposed, and the number of parameters is in turn
2550 * constrained by the size parameter above.
2552 * This function honours any kept_body configuration, and the
2553 * original raw request body will be saved to the kept_body brigade
2554 * if so configured, just as ap_discard_request_body does.
2556 * NOTE: File upload is not yet supported, but can be without change
2557 * to the function call.
2560 /* form parsing stuff */
2571 AP_DECLARE(int) ap_parse_form_data(request_rec *r, ap_filter_t *f,
2572 apr_array_header_t **ptr,
2573 apr_size_t num, apr_size_t usize)
2575 apr_bucket_brigade *bb = NULL;
2577 char buffer[HUGE_STRING_LEN + 1];
2579 apr_size_t offset = 0;
2581 ap_form_type_t state = FORM_NAME, percent = FORM_NORMAL;
2582 ap_form_pair_t *pair = NULL;
2583 apr_array_header_t *pairs = apr_array_make(r->pool, 4, sizeof(ap_form_pair_t));
2590 /* sanity check - we only support forms for now */
2591 ct = apr_table_get(r->headers_in, "Content-Type");
2592 if (!ct || strncasecmp("application/x-www-form-urlencoded", ct, 33)) {
2593 return ap_discard_request_body(r);
2596 if (usize > APR_SIZE_MAX >> 1)
2597 size = APR_SIZE_MAX >> 1;
2602 f = r->input_filters;
2605 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
2607 apr_bucket *bucket = NULL, *last = NULL;
2609 int rv = ap_get_brigade(f, bb, AP_MODE_READBYTES,
2610 APR_BLOCK_READ, HUGE_STRING_LEN);
2611 if (rv != APR_SUCCESS) {
2612 apr_brigade_destroy(bb);
2613 return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
2616 for (bucket = APR_BRIGADE_FIRST(bb);
2617 bucket != APR_BRIGADE_SENTINEL(bb);
2618 last = bucket, bucket = APR_BUCKET_NEXT(bucket)) {
2620 apr_size_t len, slide;
2623 apr_bucket_delete(last);
2625 if (APR_BUCKET_IS_EOS(bucket)) {
2629 if (bucket->length == 0) {
2633 rv = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
2634 if (rv != APR_SUCCESS) {
2635 apr_brigade_destroy(bb);
2636 return HTTP_BAD_REQUEST;
2640 while (state != FORM_ABORT && slide-- > 0 && size >= 0 && num != 0) {
2645 else if ('&' == c) {
2649 percent = FORM_PERCENTA;
2652 if (FORM_PERCENTA == percent) {
2656 else if (c >= 'A') {
2659 else if (c >= '0') {
2663 percent = FORM_PERCENTB;
2666 if (FORM_PERCENTB == percent) {
2670 else if (c >= 'A') {
2673 else if (c >= '0') {
2677 percent = FORM_NORMAL;
2682 const char *tmp = apr_pmemdup(r->pool, buffer, offset);
2683 apr_bucket *b = apr_bucket_pool_create(tmp, offset, r->pool, r->connection->bucket_alloc);
2684 APR_BRIGADE_INSERT_TAIL(pair->value, b);
2692 if (offset < HUGE_STRING_LEN) {
2696 pair = (ap_form_pair_t *) apr_array_push(pairs);
2697 pair->name = apr_pstrdup(r->pool, buffer);
2698 pair->value = apr_brigade_create(r->pool, r->connection->bucket_alloc);
2702 buffer[offset++] = c;
2711 if (offset >= HUGE_STRING_LEN) {
2712 const char *tmp = apr_pmemdup(r->pool, buffer, offset);
2713 apr_bucket *b = apr_bucket_pool_create(tmp, offset, r->pool, r->connection->bucket_alloc);
2714 APR_BRIGADE_INSERT_TAIL(pair->value, b);
2717 buffer[offset++] = c;
2727 apr_brigade_cleanup(bb);
2728 } while (!seen_eos);
2730 if (FORM_ABORT == state || size < 0 || num == 0) {
2731 return HTTP_REQUEST_ENTITY_TOO_LARGE;
2733 else if (FORM_VALUE == state && pair && offset > 0) {
2734 const char *tmp = apr_pmemdup(r->pool, buffer, offset);
2735 apr_bucket *b = apr_bucket_pool_create(tmp, offset, r->pool, r->connection->bucket_alloc);
2736 APR_BRIGADE_INSERT_TAIL(pair->value, b);
2743 #define VARBUF_SMALL_SIZE 2048
2744 #define VARBUF_MAX_SIZE (APR_SIZE_MAX - 1 - \
2745 APR_ALIGN_DEFAULT(sizeof(struct ap_varbuf_info)))
2747 struct ap_varbuf_info {
2748 struct apr_memnode_t *node;
2749 apr_allocator_t *allocator;
2752 static apr_status_t varbuf_cleanup(void *info_)
2754 struct ap_varbuf_info *info = info_;
2755 info->node->next = NULL;
2756 apr_allocator_free(info->allocator, info->node);
2760 const char nul = '\0';
2761 static char * const varbuf_empty = (char *)&nul;
2763 AP_DECLARE(void) ap_varbuf_init(apr_pool_t *p, struct ap_varbuf *vb,
2764 apr_size_t init_size)
2766 vb->buf = varbuf_empty;
2768 vb->strlen = AP_VARBUF_UNKNOWN;
2772 ap_varbuf_grow(vb, init_size);
2775 AP_DECLARE(void) ap_varbuf_grow(struct ap_varbuf *vb, apr_size_t new_len)
2777 apr_memnode_t *new_node = NULL;
2778 apr_allocator_t *allocator;
2779 struct ap_varbuf_info *new_info;
2782 AP_DEBUG_ASSERT(vb->strlen == AP_VARBUF_UNKNOWN || vb->avail >= vb->strlen);
2784 if (new_len <= vb->avail)
2787 if (new_len < 2 * vb->avail && vb->avail < VARBUF_MAX_SIZE/2) {
2788 /* at least double the size, to avoid repeated reallocations */
2789 new_len = 2 * vb->avail;
2791 else if (new_len > VARBUF_MAX_SIZE) {
2792 apr_abortfunc_t abort_fn = apr_pool_abort_get(vb->pool);
2793 ap_assert(abort_fn != NULL);
2794 abort_fn(APR_ENOMEM);
2798 new_len++; /* add space for trailing \0 */
2799 if (new_len <= VARBUF_SMALL_SIZE) {
2800 new_len = APR_ALIGN_DEFAULT(new_len);
2801 new = apr_palloc(vb->pool, new_len);
2802 if (vb->avail && vb->strlen != 0) {
2803 AP_DEBUG_ASSERT(vb->buf != NULL);
2804 AP_DEBUG_ASSERT(vb->buf != varbuf_empty);
2805 if (new == vb->buf + vb->avail + 1) {
2806 /* We are lucky: the new memory lies directly after our old
2807 * buffer, we can now use both.
2809 vb->avail += new_len;
2813 /* copy up to vb->strlen + 1 bytes */
2814 memcpy(new, vb->buf, vb->strlen == AP_VARBUF_UNKNOWN ?
2815 vb->avail + 1 : vb->strlen + 1);
2821 vb->avail = new_len - 1;
2826 /* The required block is rather larger. Use allocator directly so that
2827 * the memory can be freed independently from the pool. */
2828 allocator = apr_pool_allocator_get(vb->pool);
2829 if (new_len <= VARBUF_MAX_SIZE)
2830 new_node = apr_allocator_alloc(allocator,
2831 new_len + APR_ALIGN_DEFAULT(sizeof(*new_info)));
2833 apr_abortfunc_t abort_fn = apr_pool_abort_get(vb->pool);
2834 ap_assert(abort_fn != NULL);
2835 abort_fn(APR_ENOMEM);
2838 new_info = (struct ap_varbuf_info *)new_node->first_avail;
2839 new_node->first_avail += APR_ALIGN_DEFAULT(sizeof(*new_info));
2840 new_info->node = new_node;
2841 new_info->allocator = allocator;
2842 new = new_node->first_avail;
2843 AP_DEBUG_ASSERT(new_node->endp - new_node->first_avail >= new_len);
2844 new_len = new_node->endp - new_node->first_avail;
2846 if (vb->avail && vb->strlen != 0)
2847 memcpy(new, vb->buf, vb->strlen == AP_VARBUF_UNKNOWN ?
2848 vb->avail + 1 : vb->strlen + 1);
2852 apr_pool_cleanup_run(vb->pool, vb->info, varbuf_cleanup);
2853 apr_pool_cleanup_register(vb->pool, new_info, varbuf_cleanup,
2854 apr_pool_cleanup_null);
2855 vb->info = new_info;
2857 vb->avail = new_len - 1;
2860 AP_DECLARE(void) ap_varbuf_strmemcat(struct ap_varbuf *vb, const char *str,
2866 ap_varbuf_grow(vb, len);
2867 memcpy(vb->buf, str, len);
2868 vb->buf[len] = '\0';
2872 if (vb->strlen == AP_VARBUF_UNKNOWN)
2873 vb->strlen = strlen(vb->buf);
2874 ap_varbuf_grow(vb, vb->strlen + len);
2875 memcpy(vb->buf + vb->strlen, str, len);
2877 vb->buf[vb->strlen] = '\0';
2880 AP_DECLARE(void) ap_varbuf_free(struct ap_varbuf *vb)
2883 apr_pool_cleanup_run(vb->pool, vb->info, varbuf_cleanup);
2889 AP_DECLARE(char *) ap_varbuf_pdup(apr_pool_t *p, struct ap_varbuf *buf,
2890 const char *prepend, apr_size_t prepend_len,
2891 const char *append, apr_size_t append_len,
2892 apr_size_t *new_len)
2895 struct iovec vec[3];
2898 vec[i].iov_base = (void *)prepend;
2899 vec[i].iov_len = prepend_len;
2902 if (buf->avail && buf->strlen) {
2903 if (buf->strlen == AP_VARBUF_UNKNOWN)
2904 buf->strlen = strlen(buf->buf);
2905 vec[i].iov_base = (void *)buf->buf;
2906 vec[i].iov_len = buf->strlen;
2910 vec[i].iov_base = (void *)append;
2911 vec[i].iov_len = append_len;
2915 return apr_pstrcatv(p, vec, i, new_len);
2922 AP_DECLARE(apr_status_t) ap_varbuf_regsub(struct ap_varbuf *vb,
2926 ap_regmatch_t pmatch[],
2929 return regsub_core(NULL, NULL, vb, input, source, nmatch, pmatch, maxlen);
2932 static const char * const oom_message = "[crit] Memory allocation failed, "
2933 "aborting process." APR_EOL_STR;
2935 AP_DECLARE(void) ap_abort_on_oom()
2937 int written, count = strlen(oom_message);
2938 const char *buf = oom_message;
2940 written = write(STDERR_FILENO, buf, count);
2941 if (written == count)
2947 } while (written >= 0 || errno == EINTR);
2951 AP_DECLARE(void *) ap_malloc(size_t size)
2953 void *p = malloc(size);
2954 if (p == NULL && size != 0)
2959 AP_DECLARE(void *) ap_calloc(size_t nelem, size_t size)
2961 void *p = calloc(nelem, size);
2962 if (p == NULL && nelem != 0 && size != 0)
2967 AP_DECLARE(void *) ap_realloc(void *ptr, size_t size)
2969 void *p = realloc(ptr, size);
2970 if (p == NULL && size != 0)
2975 AP_DECLARE(void) ap_get_sload(ap_sload_t *ld)
2977 int i, j, server_limit, thread_limit;
2981 ap_generation_t mpm_generation;
2983 /* preload errored fields, we overwrite */
2986 ld->bytes_served = 0;
2987 ld->access_count = 0;
2989 ap_mpm_query(AP_MPMQ_GENERATION, &mpm_generation);
2990 ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
2991 ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
2993 for (i = 0; i < server_limit; i++) {
2995 ps = ap_get_scoreboard_process(i);
2997 for (j = 0; j < thread_limit; j++) {
2999 worker_score *ws = NULL;
3000 ws = &ap_scoreboard_image->servers[i][j];
3003 if (!ps->quiescing && ps->pid) {
3004 if (res == SERVER_READY && ps->generation == mpm_generation) {
3007 else if (res != SERVER_DEAD &&
3008 res != SERVER_STARTING && res != SERVER_IDLE_KILL &&
3009 ps->generation == mpm_generation) {
3014 if (ap_extended_status && !ps->quiescing && ps->pid) {
3015 if (ws->access_count != 0
3016 || (res != SERVER_READY && res != SERVER_DEAD)) {
3017 ld->access_count += ws->access_count;
3018 ld->bytes_served += ws->bytes_served;
3023 total = busy + ready;
3025 ld->idle = ready * 100 / total;
3026 ld->busy = busy * 100 / total;
3030 AP_DECLARE(void) ap_get_loadavg(ap_loadavg_t *ld)
3032 /* preload errored fields, we overwrite */
3034 ld->loadavg5 = -1.0;
3035 ld->loadavg15 = -1.0;
3042 num = getloadavg(la, 3);
3044 ld->loadavg = (float)la[0];
3047 ld->loadavg5 = (float)la[1];
3050 ld->loadavg15 = (float)la[2];
3056 AP_DECLARE(char *) ap_get_exec_line(apr_pool_t *p,
3058 const char * const * argv)
3060 char buf[MAX_STRING_LEN];
3061 apr_procattr_t *procattr;
3064 apr_size_t nbytes = 1;
3068 if (apr_procattr_create(&procattr, p) != APR_SUCCESS)
3070 if (apr_procattr_io_set(procattr, APR_FULL_BLOCK, APR_FULL_BLOCK,
3071 APR_FULL_BLOCK) != APR_SUCCESS)
3073 if (apr_procattr_dir_set(procattr,
3074 ap_make_dirstr_parent(p, cmd)) != APR_SUCCESS)
3076 if (apr_procattr_cmdtype_set(procattr, APR_PROGRAM) != APR_SUCCESS)
3078 proc = apr_pcalloc(p, sizeof(apr_proc_t));
3079 if (apr_proc_create(proc, cmd, argv, NULL, procattr, p) != APR_SUCCESS)
3085 /* XXX: we are reading 1 byte at a time here */
3086 for (k = 0; apr_file_read(fp, &c, &nbytes) == APR_SUCCESS
3087 && nbytes == 1 && (k < MAX_STRING_LEN-1) ; ) {
3088 if (c == '\n' || c == '\r')
3095 return apr_pstrndup(p, buf, k);
3098 AP_DECLARE(int) ap_array_str_index(const apr_array_header_t *array,
3105 for (i = start; i < array->nelts; i++) {
3106 const char *p = APR_ARRAY_IDX(array, i, const char *);
3107 if (!strcmp(p, s)) {
3116 AP_DECLARE(int) ap_array_str_contains(const apr_array_header_t *array,
3119 return (ap_array_str_index(array, s, 0) >= 0);