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
42
#include <netdb.h> /* for gethostbyname() */
47
#include "ap_config.h"
48
#include "apr_base64.h"
50
#include "http_main.h"
52
#include "http_protocol.h"
53
#include "http_config.h"
54
#include "util_ebcdic.h"
63
/* A bunch of functions in util.c scan strings looking for certain characters.
64
* To make that more efficient we encode a lookup table. The test_char_table
65
* is generated automatically by gen_test_char.c.
67
#include "test_char.h"
69
/* we assume the folks using this ensure 0 <= c < 256... which means
70
* you need a cast to (unsigned char) first, you can't just plug a
71
* char in here and get it to work, because if char is signed then it
72
* will first be sign extended.
74
#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
76
/* Win32/NetWare/OS2 need to check for both forward and back slashes
77
* in ap_getparents() and ap_escape_url.
79
#ifdef CASE_BLIND_FILESYSTEM
80
#define IS_SLASH(s) ((s == '/') || (s == '\\'))
82
#define IS_SLASH(s) (s == '/')
87
* Examine a field value (such as a media-/content-type) string and return
88
* it sans any parameters; e.g., strip off any ';charset=foo' and the like.
90
AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
94
if (intype == NULL) return NULL;
96
semi = ap_strchr_c(intype, ';');
98
return apr_pstrdup(p, intype);
101
while ((semi > intype) && apr_isspace(semi[-1])) {
104
return apr_pstrndup(p, intype, semi - intype);
108
AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
112
char ts[MAX_STRING_LEN];
113
char tf[MAX_STRING_LEN];
120
apr_time_exp_gmt(&xt, t);
121
/* Convert %Z to "GMT" and %z to "+0000";
122
* on hosts that do not have a time zone string in struct tm,
123
* strftime must assume its argument is local time.
125
for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
127
if (*f != '%') continue;
138
case 'z': /* common extension */
152
apr_time_exp_lt(&xt, t);
155
/* check return code? */
156
apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
157
ts[MAX_STRING_LEN - 1] = '\0';
158
return apr_pstrdup(p, ts);
161
/* Roy owes Rob beer. */
162
/* Rob owes Roy dinner. */
164
/* These legacy comments would make a lot more sense if Roy hadn't
165
* replaced the old later_than() routine with util_date.c.
167
* Well, okay, they still wouldn't make any sense.
170
/* Match = 0, NoMatch = 1, Abort = -1
171
* Based loosely on sections of wildmat.c by Rich Salz
172
* Hmmm... shouldn't this really go component by component?
174
AP_DECLARE(int) ap_strcmp_match(const char *str, const char *expected)
178
for (x = 0, y = 0; expected[y]; ++y, ++x) {
179
if ((!str[x]) && (expected[y] != '*'))
181
if (expected[y] == '*') {
182
while (expected[++y] == '*');
187
if ((ret = ap_strcmp_match(&str[x++], &expected[y])) != 1)
192
else if ((expected[y] != '?') && (str[x] != expected[y]))
195
return (str[x] != '\0');
198
AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *expected)
202
for (x = 0, y = 0; expected[y]; ++y, ++x) {
203
if (!str[x] && expected[y] != '*')
205
if (expected[y] == '*') {
206
while (expected[++y] == '*');
211
if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1)
216
else if (expected[y] != '?'
217
&& apr_tolower(str[x]) != apr_tolower(expected[y]))
220
return (str[x] != '\0');
223
/* We actually compare the canonical root to this root, (but we don't
224
* waste time checking the case), since every use of this function in
225
* httpd-2.1 tests if the path is 'proper', meaning we've already passed
226
* it through apr_filepath_merge, or we haven't.
228
AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
231
const char *ourdir = dir;
232
if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
233
|| strncmp(newpath, ourdir, strlen(newpath)) != 0) {
239
AP_DECLARE(int) ap_is_matchexp(const char *str)
243
for (x = 0; str[x]; x++)
244
if ((str[x] == '*') || (str[x] == '?'))
250
* Here's a pool-based interface to the POSIX-esque ap_regcomp().
251
* Note that we return ap_regex_t instead of being passed one.
252
* The reason is that if you use an already-used ap_regex_t structure,
253
* the memory that you've already allocated gets forgotten, and
254
* regfree() doesn't clear it. So we don't allow it.
257
static apr_status_t regex_cleanup(void *preg)
259
ap_regfree((ap_regex_t *) preg);
263
AP_DECLARE(ap_regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
266
ap_regex_t *preg = apr_palloc(p, sizeof *preg);
268
if (ap_regcomp(preg, pattern, cflags)) {
272
apr_pool_cleanup_register(p, (void *) preg, regex_cleanup,
273
apr_pool_cleanup_null);
278
AP_DECLARE(void) ap_pregfree(apr_pool_t *p, ap_regex_t *reg)
281
apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
285
* Similar to standard strstr() but we ignore case in this version.
286
* Based on the strstr() implementation further below.
288
AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
296
for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
300
/* found first character of s2, see if the rest matches */
303
for (++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
305
/* both strings ended together */
310
/* second string ended, a match */
313
/* didn't find a match here, try starting at next character in s1 */
320
* Returns an offsetted pointer in bigstring immediately after
321
* prefix. Returns bigstring if bigstring doesn't start with
322
* prefix or if prefix is longer than bigstring while still matching.
323
* NOTE: pointer returned is relative to bigstring, so we
324
* can use standard pointer comparisons in the calling function
325
* (eg: test if ap_stripprefix(a,b) == a)
327
AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
336
while (*p1 && *prefix) {
337
if (*p1++ != *prefix++)
343
/* hit the end of bigstring! */
347
/* This function substitutes for $0-$9, filling in regular expression
348
* submatches. Pass it the same nmatch and pmatch arguments that you
349
* passed ap_regexec(). pmatch should not be greater than the maximum number
350
* of subexpressions - i.e. one more than the re_nsub member of ap_regex_t.
352
* input should be the string with the $-expressions, source should be the
353
* string that was matched against.
355
* It returns the substituted string, or NULL on error.
357
* Parts of this code are based on Henry Spencer's regsub(), from his
358
* AT&T V8 regexp package.
361
AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input,
362
const char *source, size_t nmatch,
363
ap_regmatch_t pmatch[])
365
const char *src = input;
374
return apr_pstrdup(p, src);
376
/* First pass, find the size */
380
while ((c = *src++) != '\0') {
383
else if (c == '$' && apr_isdigit(*src))
388
if (no > 9) { /* Ordinary character. */
389
if (c == '\\' && (*src == '$' || *src == '&'))
393
else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
394
len += pmatch[no].rm_eo - pmatch[no].rm_so;
399
dest = dst = apr_pcalloc(p, len + 1);
401
/* Now actually fill in the string */
405
while ((c = *src++) != '\0') {
408
else if (c == '$' && apr_isdigit(*src))
413
if (no > 9) { /* Ordinary character. */
414
if (c == '\\' && (*src == '$' || *src == '&'))
418
else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
419
len = pmatch[no].rm_eo - pmatch[no].rm_so;
420
memcpy(dst, source + pmatch[no].rm_so, len);
431
* Parse .. so we don't compromise security
433
AP_DECLARE(void) ap_getparents(char *name)
438
/* Four paseses, as per RFC 1808 */
439
/* a) remove ./ path segments */
440
for (next = name; *next && (*next != '.'); next++) {
443
l = w = first_dot = next - name;
444
while (name[l] != '\0') {
445
if (name[l] == '.' && IS_SLASH(name[l + 1])
446
&& (l == 0 || IS_SLASH(name[l - 1])))
449
name[w++] = name[l++];
452
/* b) remove trailing . path, segment */
453
if (w == 1 && name[0] == '.')
455
else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
459
/* c) remove all xx/../ segments. (including leading ../ and /../) */
462
while (name[l] != '\0') {
463
if (name[l] == '.' && name[l + 1] == '.' && IS_SLASH(name[l + 2])
464
&& (l == 0 || IS_SLASH(name[l - 1]))) {
465
register int m = l + 3, n;
469
while (l >= 0 && !IS_SLASH(name[l]))
476
while ((name[n] = name[m]))
483
/* d) remove trailing xx/.. segment. */
484
if (l == 2 && name[0] == '.' && name[1] == '.')
486
else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
487
&& IS_SLASH(name[l - 3])) {
490
while (l >= 0 && !IS_SLASH(name[l]))
500
AP_DECLARE(void) ap_no2slash(char *name)
506
#ifdef HAVE_UNC_PATHS
507
/* Check for UNC names. Leave leading two slashes. */
508
if (s[0] == '/' && s[1] == '/')
513
if ((*d++ = *s) == '/') {
527
* copy at most n leading directories of s into d
528
* d should be at least as large as s plus 1 extra byte
530
* the return value is the ever useful pointer to the trailing \0 of d
532
* MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
533
* so that if n == 0, "/" is returned in d with n == 1
534
* and s == "e:/test.html", "e:/" is returned in d
535
* *** See also directory_walk in modules/http/http_request.c
538
* /a/b, 0 ==> / (true for all platforms)
547
* c:/a/b 3 ==> c:/a/b
548
* c:/a/b 4 ==> c:/a/b
550
AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
559
if (*s == '\0' || (*s == '/' && (--n) == 0)) {
571
* return the parent directory name including trailing / of the file s
573
AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
575
const char *last_slash = ap_strrchr_c(s, '/');
579
if (last_slash == NULL) {
580
return apr_pstrdup(p, "");
582
l = (last_slash - s) + 1;
583
d = apr_palloc(p, l + 1);
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 = (char *)apr_palloc(atrans, len + 1);
617
memcpy(res, *line, len);
621
while (*pos == stop) {
630
AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
632
return ap_getword_white(atrans, (const char **) line);
635
AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
637
const char *pos = *line;
641
while (!apr_isspace(*pos) && *pos) {
646
res = (char *)apr_palloc(atrans, len + 1);
647
memcpy(res, *line, len);
650
while (apr_isspace(*pos)) {
659
AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line,
662
return ap_getword_nulls(atrans, (const char **) line, stop);
665
AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line,
668
const char *pos = ap_strchr_c(*line, stop);
672
res = apr_pstrdup(atrans, *line);
673
*line += strlen(*line);
677
res = apr_pstrndup(atrans, *line, pos - *line);
686
/* Get a word, (new) config-file style --- quoted strings and backslashes
690
static char *substring_conf(apr_pool_t *p, const char *start, int len,
693
char *result = apr_palloc(p, len + 2);
697
for (i = 0; i < len; ++i) {
698
if (start[i] == '\\' && (start[i + 1] == '\\'
699
|| (quote && start[i + 1] == quote)))
700
*resp++ = start[++i];
706
#if RESOLVE_ENV_PER_TOKEN
707
return (char *)ap_resolve_env(p,result);
713
AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
715
return ap_getword_conf(p, (const char **) line);
718
AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
720
const char *str = *line, *strend;
724
while (*str && apr_isspace(*str))
732
if ((quote = *str) == '"' || quote == '\'') {
734
while (*strend && *strend != quote) {
735
if (*strend == '\\' && strend[1] &&
736
(strend[1] == quote || strend[1] == '\\')) {
743
res = substring_conf(p, str + 1, strend - str - 1, quote);
745
if (*strend == quote)
750
while (*strend && !apr_isspace(*strend))
753
res = substring_conf(p, str, strend - str, 0);
756
while (*strend && apr_isspace(*strend))
762
/* Check a string for any ${ENV} environment variable
763
* construct and replace each them by the value of
764
* that environment variable, if it exists. If the
765
* environment value does not exist, leave the ${ENV}
766
* construct alone; it means something else.
768
AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
770
# define SMALL_EXPANSION 5
775
} *result, *current, sresult[SMALL_EXPANSION];
777
const char *s, *e, *ep;
781
s = ap_strchr_c(word, '$');
786
/* well, actually something to do */
787
ep = word + strlen(word);
789
result = current = &(sresult[spc++]);
790
current->next = NULL;
791
current->string = word;
792
current->len = s - word;
793
outlen = current->len;
796
/* prepare next entry */
798
current->next = (spc < SMALL_EXPANSION)
800
: (struct sll *)apr_palloc(p,
801
sizeof(*current->next));
802
current = current->next;
803
current->next = NULL;
808
if (s[1] == '{' && (e = ap_strchr_c(s, '}'))) {
809
word = getenv(apr_pstrndup(p, s+2, e-s-2));
811
current->string = word;
812
current->len = strlen(word);
813
outlen += current->len;
817
current->len = e - s + 1;
818
outlen += current->len;
823
current->string = s++;
830
s = ap_strchr_c(s, '$');
831
current->string = word;
832
current->len = s ? s - word : ep - word;
833
outlen += current->len;
837
/* assemble result */
838
res_buf = cp = apr_palloc(p, outlen + 1);
841
memcpy(cp, result->string, result->len);
844
result = result->next;
846
res_buf[outlen] = '\0';
851
AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
854
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
855
"Done with config file %s", cfp->name);
857
return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
860
static apr_status_t cfg_close(void *param)
862
apr_file_t *cfp = (apr_file_t *) param;
863
return (apr_file_close(cfp));
866
static int cfg_getch(void *param)
869
apr_file_t *cfp = (apr_file_t *) param;
870
if (apr_file_getc(&ch, cfp) == APR_SUCCESS)
875
static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
877
apr_file_t *cfp = (apr_file_t *) param;
879
rv = apr_file_gets(buf, bufsiz, cfp);
880
if (rv == APR_SUCCESS) {
886
/* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
887
AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg,
888
apr_pool_t *p, const char *name)
890
ap_configfile_t *new_cfg;
891
apr_file_t *file = NULL;
899
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
900
"Internal error: pcfg_openfile() called with NULL filename");
904
status = apr_file_open(&file, name, APR_READ | APR_BUFFERED,
907
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
908
"Opening config file %s (%s)",
909
name, (status != APR_SUCCESS) ?
910
apr_strerror(status, buf, sizeof(buf)) : "successful");
912
if (status != APR_SUCCESS)
915
status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
916
if (status != APR_SUCCESS)
919
if (finfo.filetype != APR_REG &&
920
#if defined(WIN32) || defined(OS2) || defined(NETWARE)
921
strcasecmp(apr_filepath_name_get(name), "nul") != 0) {
923
strcmp(name, "/dev/null") != 0) {
924
#endif /* WIN32 || OS2 */
925
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
926
"Access to file %s denied by server: not a regular file",
928
apr_file_close(file);
933
/* Some twisted character [no pun intended] at MS decided that a
934
* zero width joiner as the lead wide character would be ideal for
935
* describing Unicode text files. This was further convoluted to
936
* another MSism that the same character mapped into utf-8, EF BB BF
937
* would signify utf-8 text files.
939
* Since MS configuration files are all protecting utf-8 encoded
940
* Unicode path, file and resource names, we already have the correct
941
* WinNT encoding. But at least eat the stupid three bytes up front.
944
unsigned char buf[4];
946
status = apr_file_read(file, buf, &len);
947
if ((status != APR_SUCCESS) || (len < 3)
948
|| memcmp(buf, "\xEF\xBB\xBF", 3) != 0) {
950
apr_file_seek(file, APR_SET, &zero);
955
new_cfg = apr_palloc(p, sizeof(*new_cfg));
956
new_cfg->param = file;
957
new_cfg->name = apr_pstrdup(p, name);
958
new_cfg->getch = (int (*)(void *)) cfg_getch;
959
new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
960
new_cfg->close = (int (*)(void *)) cfg_close;
961
new_cfg->line_number = 0;
967
/* Allocate a ap_configfile_t handle with user defined functions and params */
968
AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(apr_pool_t *p,
971
int(*getch)(void *param),
972
void *(*getstr) (void *buf, size_t bufsiz, void *param),
973
int(*close_func)(void *param))
975
ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
977
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
978
"Opening config handler %s", descr);
980
new_cfg->param = param;
981
new_cfg->name = descr;
982
new_cfg->getch = getch;
983
new_cfg->getstr = getstr;
984
new_cfg->close = close_func;
985
new_cfg->line_number = 0;
989
/* Read one character from a configfile_t */
990
AP_DECLARE(int) ap_cfg_getc(ap_configfile_t *cfp)
992
register int ch = cfp->getch(cfp->param);
998
/* Read one line from open ap_configfile_t, strip LF, increase line number */
999
/* If custom handler does not define a getstr() function, read char by char */
1000
AP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, ap_configfile_t *cfp)
1002
/* If a "get string" function is defined, use it */
1003
if (cfp->getstr != NULL) {
1007
size_t cbufsize = bufsize;
1011
if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
1015
* check for line continuation,
1016
* i.e. match [^\\]\\[\r]\n only
1019
while (cp < cbuf+cbufsize && *cp != '\0')
1021
if (cp > cbuf && cp[-1] == LF) {
1023
if (cp > cbuf && cp[-1] == CR)
1025
if (cp > cbuf && cp[-1] == '\\') {
1027
if (!(cp > cbuf && cp[-1] == '\\')) {
1029
* line continuation requested -
1030
* then remove backslash and continue
1032
cbufsize -= (cp-cbuf);
1038
* no real continuation because escaped -
1039
* then just remove escape character
1041
for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
1050
* Leading and trailing white space is eliminated completely
1053
while (apr_isspace(*src))
1055
/* blast trailing whitespace */
1056
dst = &src[strlen(src)];
1057
while (--dst >= src && apr_isspace(*dst))
1059
/* Zap leading whitespace by shifting */
1061
for (dst = buf; (*dst++ = *src++) != '\0'; )
1064
#ifdef DEBUG_CFG_LINES
1065
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, "Read config: %s", buf);
1069
/* No "get string" function defined; read character by character */
1071
register size_t i = 0;
1074
/* skip leading whitespace */
1076
c = cfp->getch(cfp->param);
1077
} while (c == '\t' || c == ' ');
1083
/* too small, assume caller is crazy */
1088
if ((c == '\t') || (c == ' ')) {
1090
while ((c == '\t') || (c == ' '))
1091
c = cfp->getch(cfp->param);
1094
/* silently ignore CR (_assume_ that a LF follows) */
1095
c = cfp->getch(cfp->param);
1098
/* increase line number and return on LF */
1101
if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
1103
* check for line continuation
1105
if (i > 0 && buf[i-1] == '\\') {
1107
if (!(i > 0 && buf[i-1] == '\\')) {
1108
/* line is continued */
1109
c = cfp->getch(cfp->param);
1112
/* else nothing needs be done because
1113
* then the backslash is escaped and
1114
* we just strip to a single one
1117
/* blast trailing whitespace */
1118
while (i > 0 && apr_isspace(buf[i - 1]))
1121
#ifdef DEBUG_CFG_LINES
1122
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
1123
"Read config: %s", buf);
1129
c = cfp->getch(cfp->param);
1134
/* Size an HTTP header field list item, as separated by a comma.
1135
* The return value is a pointer to the beginning of the non-empty list item
1136
* within the original string (or NULL if there is none) and the address
1137
* of field is shifted to the next non-comma, non-whitespace character.
1138
* len is the length of the item excluding any beginning whitespace.
1140
AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1142
const unsigned char *ptr = (const unsigned char *)*field;
1143
const unsigned char *token;
1144
int in_qpair, in_qstr, in_com;
1146
/* Find first non-comma, non-whitespace byte */
1148
while (*ptr == ',' || apr_isspace(*ptr))
1153
/* Find the end of this item, skipping over dead bits */
1155
for (in_qpair = in_qstr = in_com = 0;
1156
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1164
case '\\': in_qpair = 1; /* quoted-pair */
1166
case '"' : if (!in_com) /* quoted string delim */
1169
case '(' : if (!in_qstr) /* comment (may nest) */
1172
case ')' : if (in_com) /* end comment */
1180
if ((*len = (ptr - token)) == 0) {
1181
*field = (const char *)ptr;
1185
/* Advance field pointer to the next non-comma, non-white byte */
1187
while (*ptr == ',' || apr_isspace(*ptr))
1190
*field = (const char *)ptr;
1191
return (const char *)token;
1194
/* Retrieve an HTTP header field list item, as separated by a comma,
1195
* while stripping insignificant whitespace and lowercasing anything not in
1196
* a quoted string or comment. The return value is a new string containing
1197
* the converted list item (or NULL if none) and the address pointed to by
1198
* field is shifted to the next non-comma, non-whitespace.
1200
AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1202
const char *tok_start;
1203
const unsigned char *ptr;
1206
int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1208
/* Find the beginning and maximum length of the list item so that
1209
* we can allocate a buffer for the new string and reset the field.
1211
if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1214
token = apr_palloc(p, tok_len + 1);
1216
/* Scan the token again, but this time copy only the good bytes.
1217
* We skip extra whitespace and any whitespace around a '=', '/',
1218
* or ';' and lowercase normal characters not within a comment,
1219
* quoted-string or quoted-pair.
1221
for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1222
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1231
case '\\': in_qpair = 1;
1237
case '"' : if (!in_com)
1244
case '(' : if (!in_qstr)
1251
case ')' : if (in_com)
1257
case '\t': if (addspace)
1259
if (in_com || in_qstr)
1266
case ';' : if (!(in_com || in_qstr))
1270
default : if (addspace == 1)
1272
*pos++ = (in_com || in_qstr) ? *ptr
1273
: apr_tolower(*ptr);
1284
/* Find an item in canonical form (lowercase, no extra spaces) within
1285
* an HTTP field value list. Returns 1 if found, 0 if not found.
1286
* This would be much more efficient if we stored header fields as
1287
* an array of list items as they are received instead of a plain string.
1289
AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line,
1292
const unsigned char *pos;
1293
const unsigned char *ptr = (const unsigned char *)line;
1294
int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1299
do { /* loop for each item in line's list */
1301
/* Find first non-comma, non-whitespace byte */
1303
while (*ptr == ',' || apr_isspace(*ptr))
1307
good = 1; /* until proven otherwise for this item */
1309
break; /* no items left and nothing good found */
1311
/* We skip extra whitespace and any whitespace around a '=', '/',
1312
* or ';' and lowercase normal characters not within a comment,
1313
* quoted-string or quoted-pair.
1315
for (pos = (const unsigned char *)tok;
1316
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1322
good = (*pos++ == *ptr);
1326
case '\\': in_qpair = 1;
1328
good = good && (*pos++ == ' ');
1329
good = good && (*pos++ == *ptr);
1332
case '"' : if (!in_com)
1335
good = good && (*pos++ == ' ');
1336
good = good && (*pos++ == *ptr);
1339
case '(' : if (!in_qstr)
1342
good = good && (*pos++ == ' ');
1343
good = good && (*pos++ == *ptr);
1346
case ')' : if (in_com)
1348
good = good && (*pos++ == *ptr);
1352
case '\t': if (addspace || !good)
1354
if (in_com || in_qstr)
1355
good = (*pos++ == *ptr);
1361
case ';' : if (!(in_com || in_qstr))
1363
good = good && (*pos++ == *ptr);
1365
default : if (!good)
1368
good = (*pos++ == ' ');
1369
if (in_com || in_qstr)
1370
good = good && (*pos++ == *ptr);
1372
good = good && (*pos++ == apr_tolower(*ptr));
1379
good = 0; /* not good if only a prefix was matched */
1381
} while (*ptr && !good);
1387
/* Retrieve a token, spacing over it and returning a pointer to
1388
* the first non-white byte afterwards. Note that these tokens
1389
* are delimited by semis and commas; and can also be delimited
1390
* by whitespace at the caller's option.
1393
AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line,
1396
const char *ptr = *accept_line;
1397
const char *tok_start;
1401
/* Find first non-white byte */
1403
while (*ptr && apr_isspace(*ptr))
1408
/* find token end, skipping over quoted strings.
1409
* (comments are already gone).
1412
while (*ptr && (accept_white || !apr_isspace(*ptr))
1413
&& *ptr != ';' && *ptr != ',') {
1420
tok_len = ptr - tok_start;
1421
token = apr_pstrndup(p, tok_start, tok_len);
1423
/* Advance accept_line pointer to the next non-white byte */
1425
while (*ptr && apr_isspace(*ptr))
1433
/* find http tokens, see the definition of token from RFC2068 */
1434
AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1436
const unsigned char *start_token;
1437
const unsigned char *s;
1442
s = (const unsigned char *)line;
1444
/* find start of token, skip all stop characters, note NUL
1445
* isn't a token stop, so we don't need to test for it
1447
while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1454
/* find end of the token */
1455
while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1458
if (!strncasecmp((const char *)start_token, (const char *)tok,
1469
AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line,
1472
int llen, tlen, lidx;
1477
llen = strlen(line);
1482
(lidx > 0 && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1485
return (strncasecmp(&line[lidx], tok, tlen) == 0);
1488
AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1492
const unsigned char *s;
1494
cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1495
d = (unsigned char *)cmd;
1496
s = (const unsigned char *)str;
1499
#if defined(OS2) || defined(WIN32)
1501
* Newlines to Win32/OS2 CreateProcess() are ill advised.
1502
* Convert them to spaces since they are effectively white
1503
* space to most applications
1505
if (*s == '\r' || *s == '\n') {
1511
if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1521
static char x2c(const char *what)
1523
register char digit;
1525
#if !APR_CHARSET_EBCDIC
1526
digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
1529
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
1531
#else /*APR_CHARSET_EBCDIC*/
1538
digit = apr_xlate_conv_byte(ap_hdrs_from_ascii,
1539
0xFF & strtol(xstr, NULL, 16));
1540
#endif /*APR_CHARSET_EBCDIC*/
1546
* Returns 0 on success, non-zero on error
1548
* bad % escape returns HTTP_BAD_REQUEST
1550
* decoding %00 -> \0 (the null character)
1551
* decoding %2f -> / (a special character)
1552
* returns HTTP_NOT_FOUND
1554
AP_DECLARE(int) ap_unescape_url(char *url)
1556
register int badesc, badpath;
1561
/* Initial scan for first '%'. Don't bother writing values before
1563
y = strchr(url, '%');
1567
for (x = y; *y; ++x, ++y) {
1571
if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1578
if (IS_SLASH(*x) || *x == '\0')
1585
return HTTP_BAD_REQUEST;
1587
return HTTP_NOT_FOUND;
1592
AP_DECLARE(int) ap_unescape_url_keep2f(char *url)
1594
register int badesc, badpath;
1599
/* Initial scan for first '%'. Don't bother writing values before
1601
y = strchr(url, '%');
1605
for (x = y; *y; ++x, ++y) {
1610
if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1616
decoded = x2c(y + 1);
1617
if (decoded == '\0') {
1629
return HTTP_BAD_REQUEST;
1632
return HTTP_NOT_FOUND;
1639
AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1640
apr_port_t port, const request_rec *r)
1642
if (ap_is_default_port(port, r)) {
1643
return apr_pstrdup(p, hostname);
1646
return apr_psprintf(p, "%s:%u", hostname, port);
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(apr_pool_t *p, const char *segment)
1691
char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
1692
const unsigned char *s = (const unsigned char *)segment;
1693
unsigned char *d = (unsigned char *)copy;
1697
if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1709
AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1711
char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1712
const unsigned char *s = (const unsigned char *)path;
1713
unsigned char *d = (unsigned char *)copy;
1717
const char *colon = ap_strchr_c(path, ':');
1718
const char *slash = ap_strchr_c(path, '/');
1720
if (colon && (!slash || colon < slash)) {
1726
if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1738
/* ap_escape_uri is now a macro for os_escape_path */
1740
AP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
1745
/* first, count the number of extra characters */
1746
for (i = 0, j = 0; s[i] != '\0'; i++)
1747
if (s[i] == '<' || s[i] == '>')
1749
else if (s[i] == '&')
1751
else if (s[i] == '"')
1755
return apr_pstrmemdup(p, s, i);
1757
x = apr_palloc(p, i + j + 1);
1758
for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1760
memcpy(&x[j], "<", 4);
1763
else if (s[i] == '>') {
1764
memcpy(&x[j], ">", 4);
1767
else if (s[i] == '&') {
1768
memcpy(&x[j], "&", 5);
1771
else if (s[i] == '"') {
1772
memcpy(&x[j], """, 6);
1782
AP_DECLARE(char *) ap_escape_logitem(apr_pool_t *p, const char *str)
1786
const unsigned char *s;
1792
ret = apr_palloc(p, 4 * strlen(str) + 1); /* Be safe */
1793
d = (unsigned char *)ret;
1794
s = (const unsigned char *)str;
1797
if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1833
AP_DECLARE(apr_size_t) ap_escape_errorlog_item(char *dest, const char *source,
1836
unsigned char *d, *ep;
1837
const unsigned char *s;
1839
if (!source || !buflen) { /* be safe */
1843
d = (unsigned char *)dest;
1844
s = (const unsigned char *)source;
1845
ep = d + buflen - 1;
1847
for (; d < ep && *s; ++s) {
1849
if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
1875
case '"': /* no need for this in error log */
1880
ep = --d; /* break the for loop as well */
1893
return (d - (unsigned char *)dest);
1896
AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
1900
if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
1901
return 0; /* in error condition, just return no */
1903
return (finfo.filetype == APR_DIR);
1906
AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
1910
if (apr_stat(&finfo, path, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
1911
return 0; /* in error condition, just return no */
1913
return (finfo.filetype == APR_DIR);
1916
AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1919
apr_size_t len1, len2;
1922
len1 = strlen(src1);
1923
len2 = strlen(src2);
1924
/* allocate +3 for '/' delimiter, trailing NULL and overallocate
1925
* one extra byte to allow the caller to add a trailing '/'
1927
path = (char *)apr_palloc(a, len1 + len2 + 3);
1930
memcpy(path + 1, src2, len2 + 1);
1934
memcpy(path, src1, len1);
1936
if (next[-1] != '/') {
1939
memcpy(next, src2, len2 + 1);
1945
* Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1947
AP_DECLARE(int) ap_is_url(const char *u)
1951
for (x = 0; u[x] != ':'; x++) {
1953
((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1954
(u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1959
return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
1962
AP_DECLARE(int) ap_ind(const char *s, char c)
1964
const char *p = ap_strchr_c(s, c);
1971
AP_DECLARE(int) ap_rind(const char *s, char c)
1973
const char *p = ap_strrchr_c(s, c);
1980
AP_DECLARE(void) ap_str_tolower(char *str)
1983
*str = apr_tolower(*str);
1989
* We must return a FQDN
1991
char *ap_get_local_host(apr_pool_t *a)
1993
#ifndef MAXHOSTNAMELEN
1994
#define MAXHOSTNAMELEN 256
1996
char str[MAXHOSTNAMELEN + 1];
1997
char *server_hostname = NULL;
1998
apr_sockaddr_t *sockaddr;
2001
if (apr_gethostname(str, sizeof(str) - 1, a) != APR_SUCCESS) {
2002
ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
2003
"%s: apr_gethostname() failed to determine ServerName",
2006
str[sizeof(str) - 1] = '\0';
2007
if (apr_sockaddr_info_get(&sockaddr, str, APR_UNSPEC, 0, 0, a) == APR_SUCCESS) {
2008
if ( (apr_getnameinfo(&hostname, sockaddr, 0) == APR_SUCCESS) &&
2009
(ap_strchr_c(hostname, '.')) ) {
2010
server_hostname = apr_pstrdup(a, hostname);
2011
return server_hostname;
2012
} else if (ap_strchr_c(str, '.')) {
2013
server_hostname = apr_pstrdup(a, str);
2015
apr_sockaddr_ip_get(&hostname, sockaddr);
2016
server_hostname = apr_pstrdup(a, hostname);
2019
ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
2020
"%s: apr_sockaddr_info_get() failed for %s",
2021
ap_server_argv0, str);
2025
if (!server_hostname)
2026
server_hostname = apr_pstrdup(a, "127.0.0.1");
2028
ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, a,
2029
"%s: Could not reliably determine the server's fully qualified "
2030
"domain name, using %s for ServerName",
2031
ap_server_argv0, server_hostname);
2033
return server_hostname;
2036
/* simple 'pool' alloc()ing glue to apr_base64.c
2038
AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
2043
decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
2044
l = apr_base64_decode(decoded, bufcoded);
2045
decoded[l] = '\0'; /* make binary sequence into string */
2050
AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
2053
int l = strlen(string);
2055
encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
2056
l = apr_base64_encode(encoded, string, l);
2057
encoded[l] = '\0'; /* make binary sequence into string */
2062
/* we want to downcase the type/subtype for comparison purposes
2063
* but nothing else because ;parameter=foo values are case sensitive.
2064
* XXX: in truth we want to downcase parameter names... but really,
2065
* apache has never handled parameters and such correctly. You
2066
* also need to compress spaces and such to be able to compare
2069
AP_DECLARE(void) ap_content_type_tolower(char *str)
2073
semi = strchr(str, ';');
2078
*str = apr_tolower(*str);
2087
* Given a string, replace any bare " with \" .
2089
AP_DECLARE(char *) ap_escape_quotes(apr_pool_t *p, const char *instring)
2092
const char *inchr = instring;
2093
char *outchr, *outstring;
2096
* Look through the input string, jogging the length of the output
2097
* string up by an extra byte each time we find an unescaped ".
2099
while (*inchr != '\0') {
2101
if (*inchr == '"') {
2105
* If we find a slosh, and it's not the last byte in the string,
2106
* it's escaping something - advance past both bytes.
2108
if ((*inchr == '\\') && (inchr[1] != '\0')) {
2114
outstring = apr_palloc(p, newlen + 1);
2118
* Now copy the input string to the output string, inserting a slosh
2119
* in front of every " that doesn't already have one.
2121
while (*inchr != '\0') {
2122
if ((*inchr == '\\') && (inchr[1] != '\0')) {
2123
*outchr++ = *inchr++;
2124
*outchr++ = *inchr++;
2126
if (*inchr == '"') {
2129
if (*inchr != '\0') {
2130
*outchr++ = *inchr++;
2138
* Given a string, append the PID deliminated by delim.
2139
* Usually used to create a pid-appended filepath name
2140
* (eg: /a/b/foo -> /a/b/foo.6726). A function, and not
2141
* a macro, to avoid unistd.h dependency
2143
AP_DECLARE(char *) ap_append_pid(apr_pool_t *p, const char *string,
2146
return apr_psprintf(p, "%s%s%" APR_PID_T_FMT, string,