~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to server/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
 
2
 * contributor license agreements.  See the NOTICE file distributed with
 
3
 * this work for additional information regarding copyright ownership.
 
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
5
 * (the "License"); you may not use this file except in compliance with
 
6
 * the License.  You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
/*
 
18
 * util.c: string utility things
 
19
 *
 
20
 * 3/21/93 Rob McCool
 
21
 * 1995-96 Many changes by the Apache Software Foundation
 
22
 *
 
23
 */
 
24
 
 
25
/* Debugging aid:
 
26
 * #define DEBUG            to trace all cfg_open*()/cfg_closefile() calls
 
27
 * #define DEBUG_CFG_LINES  to trace every line read from the config files
 
28
 */
 
29
 
 
30
#include "apr.h"
 
31
#include "apr_strings.h"
 
32
#include "apr_lib.h"
 
33
 
 
34
#define APR_WANT_STDIO
 
35
#define APR_WANT_STRFUNC
 
36
#include "apr_want.h"
 
37
 
 
38
#if APR_HAVE_UNISTD_H
 
39
#include <unistd.h>
 
40
#endif
 
41
#if APR_HAVE_NETDB_H
 
42
#include <netdb.h>              /* for gethostbyname() */
 
43
#endif
 
44
 
 
45
#define CORE_PRIVATE
 
46
 
 
47
#include "ap_config.h"
 
48
#include "apr_base64.h"
 
49
#include "httpd.h"
 
50
#include "http_main.h"
 
51
#include "http_log.h"
 
52
#include "http_protocol.h"
 
53
#include "http_config.h"
 
54
#include "util_ebcdic.h"
 
55
 
 
56
#ifdef HAVE_PWD_H
 
57
#include <pwd.h>
 
58
#endif
 
59
#ifdef HAVE_GRP_H
 
60
#include <grp.h>
 
61
#endif
 
62
 
 
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.
 
66
 */
 
67
#include "test_char.h"
 
68
 
 
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.
 
73
 */
 
74
#define TEST_CHAR(c, f)        (test_char_table[(unsigned)(c)] & (f))
 
75
 
 
76
/* Win32/NetWare/OS2 need to check for both forward and back slashes
 
77
 * in ap_getparents() and ap_escape_url.
 
78
 */
 
79
#ifdef CASE_BLIND_FILESYSTEM
 
80
#define IS_SLASH(s) ((s == '/') || (s == '\\'))
 
81
#else
 
82
#define IS_SLASH(s) (s == '/')
 
83
#endif
 
84
 
 
85
 
 
86
/*
 
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.
 
89
 */
 
90
AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
 
91
{
 
92
    const char *semi;
 
93
 
 
94
    if (intype == NULL) return NULL;
 
95
 
 
96
    semi = ap_strchr_c(intype, ';');
 
97
    if (semi == NULL) {
 
98
        return apr_pstrdup(p, intype);
 
99
    }
 
100
    else {
 
101
        while ((semi > intype) && apr_isspace(semi[-1])) {
 
102
            semi--;
 
103
        }
 
104
        return apr_pstrndup(p, intype, semi - intype);
 
105
    }
 
106
}
 
107
 
 
108
AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
 
109
                              int gmt)
 
110
{
 
111
    apr_size_t retcode;
 
112
    char ts[MAX_STRING_LEN];
 
113
    char tf[MAX_STRING_LEN];
 
114
    apr_time_exp_t xt;
 
115
 
 
116
    if (gmt) {
 
117
        const char *f;
 
118
        char *strp;
 
119
 
 
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.
 
124
         */
 
125
        for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
 
126
            ; f++, strp++) {
 
127
            if (*f != '%') continue;
 
128
            switch (f[1]) {
 
129
            case '%':
 
130
                *++strp = *++f;
 
131
                break;
 
132
            case 'Z':
 
133
                *strp++ = 'G';
 
134
                *strp++ = 'M';
 
135
                *strp = 'T';
 
136
                f++;
 
137
                break;
 
138
            case 'z': /* common extension */
 
139
                *strp++ = '+';
 
140
                *strp++ = '0';
 
141
                *strp++ = '0';
 
142
                *strp++ = '0';
 
143
                *strp = '0';
 
144
                f++;
 
145
                break;
 
146
            }
 
147
        }
 
148
        *strp = '\0';
 
149
        fmt = tf;
 
150
    }
 
151
    else {
 
152
        apr_time_exp_lt(&xt, t);
 
153
    }
 
154
 
 
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);
 
159
}
 
160
 
 
161
/* Roy owes Rob beer. */
 
162
/* Rob owes Roy dinner. */
 
163
 
 
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.
 
166
 *
 
167
 * Well, okay, they still wouldn't make any sense.
 
168
 */
 
169
 
 
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?
 
173
 */
 
174
AP_DECLARE(int) ap_strcmp_match(const char *str, const char *expected)
 
175
{
 
176
    int x, y;
 
177
 
 
178
    for (x = 0, y = 0; expected[y]; ++y, ++x) {
 
179
        if ((!str[x]) && (expected[y] != '*'))
 
180
            return -1;
 
181
        if (expected[y] == '*') {
 
182
            while (expected[++y] == '*');
 
183
            if (!expected[y])
 
184
                return 0;
 
185
            while (str[x]) {
 
186
                int ret;
 
187
                if ((ret = ap_strcmp_match(&str[x++], &expected[y])) != 1)
 
188
                    return ret;
 
189
            }
 
190
            return -1;
 
191
        }
 
192
        else if ((expected[y] != '?') && (str[x] != expected[y]))
 
193
            return 1;
 
194
    }
 
195
    return (str[x] != '\0');
 
196
}
 
197
 
 
198
AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *expected)
 
199
{
 
200
    int x, y;
 
201
 
 
202
    for (x = 0, y = 0; expected[y]; ++y, ++x) {
 
203
        if (!str[x] && expected[y] != '*')
 
204
            return -1;
 
205
        if (expected[y] == '*') {
 
206
            while (expected[++y] == '*');
 
207
            if (!expected[y])
 
208
                return 0;
 
209
            while (str[x]) {
 
210
                int ret;
 
211
                if ((ret = ap_strcasecmp_match(&str[x++], &expected[y])) != 1)
 
212
                    return ret;
 
213
            }
 
214
            return -1;
 
215
        }
 
216
        else if (expected[y] != '?'
 
217
                 && apr_tolower(str[x]) != apr_tolower(expected[y]))
 
218
            return 1;
 
219
    }
 
220
    return (str[x] != '\0');
 
221
}
 
222
 
 
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.
 
227
 */
 
228
AP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
 
229
{
 
230
    const char *newpath;
 
231
    const char *ourdir = dir;
 
232
    if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
 
233
            || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
 
234
        return 0;
 
235
    }
 
236
    return 1;
 
237
}
 
238
 
 
239
AP_DECLARE(int) ap_is_matchexp(const char *str)
 
240
{
 
241
    register int x;
 
242
 
 
243
    for (x = 0; str[x]; x++)
 
244
        if ((str[x] == '*') || (str[x] == '?'))
 
245
            return 1;
 
246
    return 0;
 
247
}
 
248
 
 
249
/*
 
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.
 
255
 */
 
256
 
 
257
static apr_status_t regex_cleanup(void *preg)
 
258
{
 
259
    ap_regfree((ap_regex_t *) preg);
 
260
    return APR_SUCCESS;
 
261
}
 
262
 
 
263
AP_DECLARE(ap_regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
 
264
                                     int cflags)
 
265
{
 
266
    ap_regex_t *preg = apr_palloc(p, sizeof *preg);
 
267
 
 
268
    if (ap_regcomp(preg, pattern, cflags)) {
 
269
        return NULL;
 
270
    }
 
271
 
 
272
    apr_pool_cleanup_register(p, (void *) preg, regex_cleanup,
 
273
                              apr_pool_cleanup_null);
 
274
 
 
275
    return preg;
 
276
}
 
277
 
 
278
AP_DECLARE(void) ap_pregfree(apr_pool_t *p, ap_regex_t *reg)
 
279
{
 
280
    ap_regfree(reg);
 
281
    apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
 
282
}
 
283
 
 
284
/*
 
285
 * Similar to standard strstr() but we ignore case in this version.
 
286
 * Based on the strstr() implementation further below.
 
287
 */
 
288
AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
 
289
{
 
290
    char *p1, *p2;
 
291
    if (*s2 == '\0') {
 
292
        /* an empty s2 */
 
293
        return((char *)s1);
 
294
    }
 
295
    while(1) {
 
296
        for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
 
297
        if (*s1 == '\0') {
 
298
            return(NULL);
 
299
        }
 
300
        /* found first character of s2, see if the rest matches */
 
301
        p1 = (char *)s1;
 
302
        p2 = (char *)s2;
 
303
        for (++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
 
304
            if (*p1 == '\0') {
 
305
                /* both strings ended together */
 
306
                return((char *)s1);
 
307
            }
 
308
        }
 
309
        if (*p2 == '\0') {
 
310
            /* second string ended, a match */
 
311
            break;
 
312
        }
 
313
        /* didn't find a match here, try starting at next character in s1 */
 
314
        s1++;
 
315
    }
 
316
    return((char *)s1);
 
317
}
 
318
 
 
319
/*
 
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)
 
326
 */
 
327
AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
 
328
                                        const char *prefix)
 
329
{
 
330
    const char *p1;
 
331
 
 
332
    if (*prefix == '\0')
 
333
        return bigstring;
 
334
 
 
335
    p1 = bigstring;
 
336
    while (*p1 && *prefix) {
 
337
        if (*p1++ != *prefix++)
 
338
            return bigstring;
 
339
    }
 
340
    if (*prefix == '\0')
 
341
        return p1;
 
342
 
 
343
    /* hit the end of bigstring! */
 
344
    return bigstring;
 
345
}
 
346
 
 
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.
 
351
 *
 
352
 * input should be the string with the $-expressions, source should be the
 
353
 * string that was matched against.
 
354
 *
 
355
 * It returns the substituted string, or NULL on error.
 
356
 *
 
357
 * Parts of this code are based on Henry Spencer's regsub(), from his
 
358
 * AT&T V8 regexp package.
 
359
 */
 
360
 
 
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[])
 
364
{
 
365
    const char *src = input;
 
366
    char *dest, *dst;
 
367
    char c;
 
368
    size_t no;
 
369
    int len;
 
370
 
 
371
    if (!source)
 
372
        return NULL;
 
373
    if (!nmatch)
 
374
        return apr_pstrdup(p, src);
 
375
 
 
376
    /* First pass, find the size */
 
377
 
 
378
    len = 0;
 
379
 
 
380
    while ((c = *src++) != '\0') {
 
381
        if (c == '&')
 
382
            no = 0;
 
383
        else if (c == '$' && apr_isdigit(*src))
 
384
            no = *src++ - '0';
 
385
        else
 
386
            no = 10;
 
387
 
 
388
        if (no > 9) {                /* Ordinary character. */
 
389
            if (c == '\\' && (*src == '$' || *src == '&'))
 
390
                c = *src++;
 
391
            len++;
 
392
        }
 
393
        else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
 
394
            len += pmatch[no].rm_eo - pmatch[no].rm_so;
 
395
        }
 
396
 
 
397
    }
 
398
 
 
399
    dest = dst = apr_pcalloc(p, len + 1);
 
400
 
 
401
    /* Now actually fill in the string */
 
402
 
 
403
    src = input;
 
404
 
 
405
    while ((c = *src++) != '\0') {
 
406
        if (c == '&')
 
407
            no = 0;
 
408
        else if (c == '$' && apr_isdigit(*src))
 
409
            no = *src++ - '0';
 
410
        else
 
411
            no = 10;
 
412
 
 
413
        if (no > 9) {                /* Ordinary character. */
 
414
            if (c == '\\' && (*src == '$' || *src == '&'))
 
415
                c = *src++;
 
416
            *dst++ = c;
 
417
        }
 
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);
 
421
            dst += len;
 
422
        }
 
423
 
 
424
    }
 
425
    *dst = '\0';
 
426
 
 
427
    return dest;
 
428
}
 
429
 
 
430
/*
 
431
 * Parse .. so we don't compromise security
 
432
 */
 
433
AP_DECLARE(void) ap_getparents(char *name)
 
434
{
 
435
    char *next;
 
436
    int l, w, first_dot;
 
437
 
 
438
    /* Four paseses, as per RFC 1808 */
 
439
    /* a) remove ./ path segments */
 
440
    for (next = name; *next && (*next != '.'); next++) {
 
441
    }
 
442
 
 
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])))
 
447
            l += 2;
 
448
        else
 
449
            name[w++] = name[l++];
 
450
    }
 
451
 
 
452
    /* b) remove trailing . path, segment */
 
453
    if (w == 1 && name[0] == '.')
 
454
        w--;
 
455
    else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
 
456
        w--;
 
457
    name[w] = '\0';
 
458
 
 
459
    /* c) remove all xx/../ segments. (including leading ../ and /../) */
 
460
    l = first_dot;
 
461
 
 
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;
 
466
 
 
467
            l = l - 2;
 
468
            if (l >= 0) {
 
469
                while (l >= 0 && !IS_SLASH(name[l]))
 
470
                    l--;
 
471
                l++;
 
472
            }
 
473
            else
 
474
                l = 0;
 
475
            n = l;
 
476
            while ((name[n] = name[m]))
 
477
                (++n, ++m);
 
478
        }
 
479
        else
 
480
            ++l;
 
481
    }
 
482
 
 
483
    /* d) remove trailing xx/.. segment. */
 
484
    if (l == 2 && name[0] == '.' && name[1] == '.')
 
485
        name[0] = '\0';
 
486
    else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
 
487
             && IS_SLASH(name[l - 3])) {
 
488
        l = l - 4;
 
489
        if (l >= 0) {
 
490
            while (l >= 0 && !IS_SLASH(name[l]))
 
491
                l--;
 
492
            l++;
 
493
        }
 
494
        else
 
495
            l = 0;
 
496
        name[l] = '\0';
 
497
    }
 
498
}
 
499
 
 
500
AP_DECLARE(void) ap_no2slash(char *name)
 
501
{
 
502
    char *d, *s;
 
503
 
 
504
    s = d = name;
 
505
 
 
506
#ifdef HAVE_UNC_PATHS
 
507
    /* Check for UNC names.  Leave leading two slashes. */
 
508
    if (s[0] == '/' && s[1] == '/')
 
509
        *d++ = *s++;
 
510
#endif
 
511
 
 
512
    while (*s) {
 
513
        if ((*d++ = *s) == '/') {
 
514
            do {
 
515
                ++s;
 
516
            } while (*s == '/');
 
517
        }
 
518
        else {
 
519
            ++s;
 
520
        }
 
521
    }
 
522
    *d = '\0';
 
523
}
 
524
 
 
525
 
 
526
/*
 
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
 
529
 * assumes n > 0
 
530
 * the return value is the ever useful pointer to the trailing \0 of d
 
531
 *
 
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
 
536
 
 
537
 * examples:
 
538
 *    /a/b, 0  ==> /  (true for all platforms)
 
539
 *    /a/b, 1  ==> /
 
540
 *    /a/b, 2  ==> /a/
 
541
 *    /a/b, 3  ==> /a/b/
 
542
 *    /a/b, 4  ==> /a/b/
 
543
 *
 
544
 *    c:/a/b 0 ==> /
 
545
 *    c:/a/b 1 ==> c:/
 
546
 *    c:/a/b 2 ==> c:/a/
 
547
 *    c:/a/b 3 ==> c:/a/b
 
548
 *    c:/a/b 4 ==> c:/a/b
 
549
 */
 
550
AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
 
551
{
 
552
    if (n < 1) {
 
553
        *d = '/';
 
554
        *++d = '\0';
 
555
        return (d);
 
556
    }
 
557
 
 
558
    for (;;) {
 
559
        if (*s == '\0' || (*s == '/' && (--n) == 0)) {
 
560
            *d = '/';
 
561
            break;
 
562
        }
 
563
        *d++ = *s++;
 
564
    }
 
565
    *++d = 0;
 
566
    return (d);
 
567
}
 
568
 
 
569
 
 
570
/*
 
571
 * return the parent directory name including trailing / of the file s
 
572
 */
 
573
AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
 
574
{
 
575
    const char *last_slash = ap_strrchr_c(s, '/');
 
576
    char *d;
 
577
    int l;
 
578
 
 
579
    if (last_slash == NULL) {
 
580
        return apr_pstrdup(p, "");
 
581
    }
 
582
    l = (last_slash - s) + 1;
 
583
    d = apr_palloc(p, l + 1);
 
584
    memcpy(d, s, l);
 
585
    d[l] = 0;
 
586
    return (d);
 
587
}
 
588
 
 
589
 
 
590
AP_DECLARE(int) ap_count_dirs(const char *path)
 
591
{
 
592
    register int x, n;
 
593
 
 
594
    for (x = 0, n = 0; path[x]; x++)
 
595
        if (path[x] == '/')
 
596
            n++;
 
597
    return n;
 
598
}
 
599
 
 
600
AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
 
601
{
 
602
    return ap_getword(atrans, (const char **) line, stop);
 
603
}
 
604
 
 
605
AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
 
606
{
 
607
    const char *pos = *line;
 
608
    int len;
 
609
    char *res;
 
610
 
 
611
    while ((*pos != stop) && *pos) {
 
612
        ++pos;
 
613
    }
 
614
 
 
615
    len = pos - *line;
 
616
    res = (char *)apr_palloc(atrans, len + 1);
 
617
    memcpy(res, *line, len);
 
618
    res[len] = 0;
 
619
 
 
620
    if (stop) {
 
621
        while (*pos == stop) {
 
622
            ++pos;
 
623
        }
 
624
    }
 
625
    *line = pos;
 
626
 
 
627
    return res;
 
628
}
 
629
 
 
630
AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
 
631
{
 
632
    return ap_getword_white(atrans, (const char **) line);
 
633
}
 
634
 
 
635
AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
 
636
{
 
637
    const char *pos = *line;
 
638
    int len;
 
639
    char *res;
 
640
 
 
641
    while (!apr_isspace(*pos) && *pos) {
 
642
        ++pos;
 
643
    }
 
644
 
 
645
    len = pos - *line;
 
646
    res = (char *)apr_palloc(atrans, len + 1);
 
647
    memcpy(res, *line, len);
 
648
    res[len] = 0;
 
649
 
 
650
    while (apr_isspace(*pos)) {
 
651
        ++pos;
 
652
    }
 
653
 
 
654
    *line = pos;
 
655
 
 
656
    return res;
 
657
}
 
658
 
 
659
AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line,
 
660
                                       char stop)
 
661
{
 
662
    return ap_getword_nulls(atrans, (const char **) line, stop);
 
663
}
 
664
 
 
665
AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line,
 
666
                                    char stop)
 
667
{
 
668
    const char *pos = ap_strchr_c(*line, stop);
 
669
    char *res;
 
670
 
 
671
    if (!pos) {
 
672
        res = apr_pstrdup(atrans, *line);
 
673
        *line += strlen(*line);
 
674
        return res;
 
675
    }
 
676
 
 
677
    res = apr_pstrndup(atrans, *line, pos - *line);
 
678
 
 
679
    ++pos;
 
680
 
 
681
    *line = pos;
 
682
 
 
683
    return res;
 
684
}
 
685
 
 
686
/* Get a word, (new) config-file style --- quoted strings and backslashes
 
687
 * all honored
 
688
 */
 
689
 
 
690
static char *substring_conf(apr_pool_t *p, const char *start, int len,
 
691
                            char quote)
 
692
{
 
693
    char *result = apr_palloc(p, len + 2);
 
694
    char *resp = result;
 
695
    int i;
 
696
 
 
697
    for (i = 0; i < len; ++i) {
 
698
        if (start[i] == '\\' && (start[i + 1] == '\\'
 
699
                                 || (quote && start[i + 1] == quote)))
 
700
            *resp++ = start[++i];
 
701
        else
 
702
            *resp++ = start[i];
 
703
    }
 
704
 
 
705
    *resp++ = '\0';
 
706
#if RESOLVE_ENV_PER_TOKEN
 
707
    return (char *)ap_resolve_env(p,result);
 
708
#else
 
709
    return result;
 
710
#endif
 
711
}
 
712
 
 
713
AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
 
714
{
 
715
    return ap_getword_conf(p, (const char **) line);
 
716
}
 
717
 
 
718
AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
 
719
{
 
720
    const char *str = *line, *strend;
 
721
    char *res;
 
722
    char quote;
 
723
 
 
724
    while (*str && apr_isspace(*str))
 
725
        ++str;
 
726
 
 
727
    if (!*str) {
 
728
        *line = str;
 
729
        return "";
 
730
    }
 
731
 
 
732
    if ((quote = *str) == '"' || quote == '\'') {
 
733
        strend = str + 1;
 
734
        while (*strend && *strend != quote) {
 
735
            if (*strend == '\\' && strend[1] &&
 
736
                (strend[1] == quote || strend[1] == '\\')) {
 
737
                strend += 2;
 
738
            }
 
739
            else {
 
740
                ++strend;
 
741
            }
 
742
        }
 
743
        res = substring_conf(p, str + 1, strend - str - 1, quote);
 
744
 
 
745
        if (*strend == quote)
 
746
            ++strend;
 
747
    }
 
748
    else {
 
749
        strend = str;
 
750
        while (*strend && !apr_isspace(*strend))
 
751
            ++strend;
 
752
 
 
753
        res = substring_conf(p, str, strend - str, 0);
 
754
    }
 
755
 
 
756
    while (*strend && apr_isspace(*strend))
 
757
        ++strend;
 
758
    *line = strend;
 
759
    return res;
 
760
}
 
761
 
 
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.
 
767
 */
 
768
AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
 
769
{
 
770
# define SMALL_EXPANSION 5
 
771
    struct sll {
 
772
        struct sll *next;
 
773
        const char *string;
 
774
        apr_size_t len;
 
775
    } *result, *current, sresult[SMALL_EXPANSION];
 
776
    char *res_buf, *cp;
 
777
    const char *s, *e, *ep;
 
778
    unsigned spc;
 
779
    apr_size_t outlen;
 
780
 
 
781
    s = ap_strchr_c(word, '$');
 
782
    if (!s) {
 
783
        return word;
 
784
    }
 
785
 
 
786
    /* well, actually something to do */
 
787
    ep = word + strlen(word);
 
788
    spc = 0;
 
789
    result = current = &(sresult[spc++]);
 
790
    current->next = NULL;
 
791
    current->string = word;
 
792
    current->len = s - word;
 
793
    outlen = current->len;
 
794
 
 
795
    do {
 
796
        /* prepare next entry */
 
797
        if (current->len) {
 
798
            current->next = (spc < SMALL_EXPANSION)
 
799
                            ? &(sresult[spc++])
 
800
                            : (struct sll *)apr_palloc(p,
 
801
                                                       sizeof(*current->next));
 
802
            current = current->next;
 
803
            current->next = NULL;
 
804
            current->len = 0;
 
805
        }
 
806
 
 
807
        if (*s == '$') {
 
808
            if (s[1] == '{' && (e = ap_strchr_c(s, '}'))) {
 
809
                word = getenv(apr_pstrndup(p, s+2, e-s-2));
 
810
                if (word) {
 
811
                    current->string = word;
 
812
                    current->len = strlen(word);
 
813
                    outlen += current->len;
 
814
                }
 
815
                else {
 
816
                    current->string = s;
 
817
                    current->len = e - s + 1;
 
818
                    outlen += current->len;
 
819
                }
 
820
                s = e + 1;
 
821
            }
 
822
            else {
 
823
                current->string = s++;
 
824
                current->len = 1;
 
825
                ++outlen;
 
826
            }
 
827
        }
 
828
        else {
 
829
            word = s;
 
830
            s = ap_strchr_c(s, '$');
 
831
            current->string = word;
 
832
            current->len = s ? s - word : ep - word;
 
833
            outlen += current->len;
 
834
        }
 
835
    } while (s && *s);
 
836
 
 
837
    /* assemble result */
 
838
    res_buf = cp = apr_palloc(p, outlen + 1);
 
839
    do {
 
840
        if (result->len) {
 
841
            memcpy(cp, result->string, result->len);
 
842
            cp += result->len;
 
843
        }
 
844
        result = result->next;
 
845
    } while (result);
 
846
    res_buf[outlen] = '\0';
 
847
 
 
848
    return res_buf;
 
849
}
 
850
 
 
851
AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
 
852
{
 
853
#ifdef DEBUG
 
854
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
 
855
        "Done with config file %s", cfp->name);
 
856
#endif
 
857
    return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
 
858
}
 
859
 
 
860
static apr_status_t cfg_close(void *param)
 
861
{
 
862
    apr_file_t *cfp = (apr_file_t *) param;
 
863
    return (apr_file_close(cfp));
 
864
}
 
865
 
 
866
static int cfg_getch(void *param)
 
867
{
 
868
    char ch;
 
869
    apr_file_t *cfp = (apr_file_t *) param;
 
870
    if (apr_file_getc(&ch, cfp) == APR_SUCCESS)
 
871
        return ch;
 
872
    return (int)EOF;
 
873
}
 
874
 
 
875
static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
 
876
{
 
877
    apr_file_t *cfp = (apr_file_t *) param;
 
878
    apr_status_t rv;
 
879
    rv = apr_file_gets(buf, bufsiz, cfp);
 
880
    if (rv == APR_SUCCESS) {
 
881
        return buf;
 
882
    }
 
883
    return NULL;
 
884
}
 
885
 
 
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)
 
889
{
 
890
    ap_configfile_t *new_cfg;
 
891
    apr_file_t *file = NULL;
 
892
    apr_finfo_t finfo;
 
893
    apr_status_t status;
 
894
#ifdef DEBUG
 
895
    char buf[120];
 
896
#endif
 
897
 
 
898
    if (name == NULL) {
 
899
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
 
900
               "Internal error: pcfg_openfile() called with NULL filename");
 
901
        return APR_EBADF;
 
902
    }
 
903
 
 
904
    status = apr_file_open(&file, name, APR_READ | APR_BUFFERED,
 
905
                           APR_OS_DEFAULT, p);
 
906
#ifdef DEBUG
 
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");
 
911
#endif
 
912
    if (status != APR_SUCCESS)
 
913
        return status;
 
914
 
 
915
    status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
 
916
    if (status != APR_SUCCESS)
 
917
        return status;
 
918
 
 
919
    if (finfo.filetype != APR_REG &&
 
920
#if defined(WIN32) || defined(OS2) || defined(NETWARE)
 
921
        strcasecmp(apr_filepath_name_get(name), "nul") != 0) {
 
922
#else
 
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",
 
927
                     name);
 
928
        apr_file_close(file);
 
929
        return APR_EBADF;
 
930
    }
 
931
 
 
932
#ifdef WIN32
 
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.
 
938
     *
 
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.
 
942
     */
 
943
    {
 
944
        unsigned char buf[4];
 
945
        apr_size_t len = 3;
 
946
        status = apr_file_read(file, buf, &len);
 
947
        if ((status != APR_SUCCESS) || (len < 3)
 
948
              || memcmp(buf, "\xEF\xBB\xBF", 3) != 0) {
 
949
            apr_off_t zero = 0;
 
950
            apr_file_seek(file, APR_SET, &zero);
 
951
        }
 
952
    }
 
953
#endif
 
954
 
 
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;
 
962
    *ret_cfg = new_cfg;
 
963
    return APR_SUCCESS;
 
964
}
 
965
 
 
966
 
 
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,
 
969
                       const char *descr,
 
970
                       void *param,
 
971
                       int(*getch)(void *param),
 
972
                       void *(*getstr) (void *buf, size_t bufsiz, void *param),
 
973
                       int(*close_func)(void *param))
 
974
{
 
975
    ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
 
976
#ifdef DEBUG
 
977
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
 
978
                 "Opening config handler %s", descr);
 
979
#endif
 
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;
 
986
    return new_cfg;
 
987
}
 
988
 
 
989
/* Read one character from a configfile_t */
 
990
AP_DECLARE(int) ap_cfg_getc(ap_configfile_t *cfp)
 
991
{
 
992
    register int ch = cfp->getch(cfp->param);
 
993
    if (ch == LF)
 
994
        ++cfp->line_number;
 
995
    return ch;
 
996
}
 
997
 
 
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)
 
1001
{
 
1002
    /* If a "get string" function is defined, use it */
 
1003
    if (cfp->getstr != NULL) {
 
1004
        char *src, *dst;
 
1005
        char *cp;
 
1006
        char *cbuf = buf;
 
1007
        size_t cbufsize = bufsize;
 
1008
 
 
1009
        while (1) {
 
1010
            ++cfp->line_number;
 
1011
            if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
 
1012
                return 1;
 
1013
 
 
1014
            /*
 
1015
             *  check for line continuation,
 
1016
             *  i.e. match [^\\]\\[\r]\n only
 
1017
             */
 
1018
            cp = cbuf;
 
1019
            while (cp < cbuf+cbufsize && *cp != '\0')
 
1020
                cp++;
 
1021
            if (cp > cbuf && cp[-1] == LF) {
 
1022
                cp--;
 
1023
                if (cp > cbuf && cp[-1] == CR)
 
1024
                    cp--;
 
1025
                if (cp > cbuf && cp[-1] == '\\') {
 
1026
                    cp--;
 
1027
                    if (!(cp > cbuf && cp[-1] == '\\')) {
 
1028
                        /*
 
1029
                         * line continuation requested -
 
1030
                         * then remove backslash and continue
 
1031
                         */
 
1032
                        cbufsize -= (cp-cbuf);
 
1033
                        cbuf = cp;
 
1034
                        continue;
 
1035
                    }
 
1036
                    else {
 
1037
                        /*
 
1038
                         * no real continuation because escaped -
 
1039
                         * then just remove escape character
 
1040
                         */
 
1041
                        for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
 
1042
                            cp[0] = cp[1];
 
1043
                    }
 
1044
                }
 
1045
            }
 
1046
            break;
 
1047
        }
 
1048
 
 
1049
        /*
 
1050
         * Leading and trailing white space is eliminated completely
 
1051
         */
 
1052
        src = buf;
 
1053
        while (apr_isspace(*src))
 
1054
            ++src;
 
1055
        /* blast trailing whitespace */
 
1056
        dst = &src[strlen(src)];
 
1057
        while (--dst >= src && apr_isspace(*dst))
 
1058
            *dst = '\0';
 
1059
        /* Zap leading whitespace by shifting */
 
1060
        if (src != buf)
 
1061
            for (dst = buf; (*dst++ = *src++) != '\0'; )
 
1062
                ;
 
1063
 
 
1064
#ifdef DEBUG_CFG_LINES
 
1065
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, "Read config: %s", buf);
 
1066
#endif
 
1067
        return 0;
 
1068
    } else {
 
1069
        /* No "get string" function defined; read character by character */
 
1070
        register int c;
 
1071
        register size_t i = 0;
 
1072
 
 
1073
        buf[0] = '\0';
 
1074
        /* skip leading whitespace */
 
1075
        do {
 
1076
            c = cfp->getch(cfp->param);
 
1077
        } while (c == '\t' || c == ' ');
 
1078
 
 
1079
        if (c == EOF)
 
1080
            return 1;
 
1081
 
 
1082
        if(bufsize < 2) {
 
1083
            /* too small, assume caller is crazy */
 
1084
            return 1;
 
1085
        }
 
1086
 
 
1087
        while (1) {
 
1088
            if ((c == '\t') || (c == ' ')) {
 
1089
                buf[i++] = ' ';
 
1090
                while ((c == '\t') || (c == ' '))
 
1091
                    c = cfp->getch(cfp->param);
 
1092
            }
 
1093
            if (c == CR) {
 
1094
                /* silently ignore CR (_assume_ that a LF follows) */
 
1095
                c = cfp->getch(cfp->param);
 
1096
            }
 
1097
            if (c == LF) {
 
1098
                /* increase line number and return on LF */
 
1099
                ++cfp->line_number;
 
1100
            }
 
1101
            if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
 
1102
                /*
 
1103
                 *  check for line continuation
 
1104
                 */
 
1105
                if (i > 0 && buf[i-1] == '\\') {
 
1106
                    i--;
 
1107
                    if (!(i > 0 && buf[i-1] == '\\')) {
 
1108
                        /* line is continued */
 
1109
                        c = cfp->getch(cfp->param);
 
1110
                        continue;
 
1111
                    }
 
1112
                    /* else nothing needs be done because
 
1113
                     * then the backslash is escaped and
 
1114
                     * we just strip to a single one
 
1115
                     */
 
1116
                }
 
1117
                /* blast trailing whitespace */
 
1118
                while (i > 0 && apr_isspace(buf[i - 1]))
 
1119
                    --i;
 
1120
                buf[i] = '\0';
 
1121
#ifdef DEBUG_CFG_LINES
 
1122
                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
 
1123
                             "Read config: %s", buf);
 
1124
#endif
 
1125
                return 0;
 
1126
            }
 
1127
            buf[i] = c;
 
1128
            ++i;
 
1129
            c = cfp->getch(cfp->param);
 
1130
        }
 
1131
    }
 
1132
}
 
1133
 
 
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.
 
1139
 */
 
1140
AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
 
1141
{
 
1142
    const unsigned char *ptr = (const unsigned char *)*field;
 
1143
    const unsigned char *token;
 
1144
    int in_qpair, in_qstr, in_com;
 
1145
 
 
1146
    /* Find first non-comma, non-whitespace byte */
 
1147
 
 
1148
    while (*ptr == ',' || apr_isspace(*ptr))
 
1149
        ++ptr;
 
1150
 
 
1151
    token = ptr;
 
1152
 
 
1153
    /* Find the end of this item, skipping over dead bits */
 
1154
 
 
1155
    for (in_qpair = in_qstr = in_com = 0;
 
1156
         *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
 
1157
         ++ptr) {
 
1158
 
 
1159
        if (in_qpair) {
 
1160
            in_qpair = 0;
 
1161
        }
 
1162
        else {
 
1163
            switch (*ptr) {
 
1164
                case '\\': in_qpair = 1;      /* quoted-pair         */
 
1165
                           break;
 
1166
                case '"' : if (!in_com)       /* quoted string delim */
 
1167
                               in_qstr = !in_qstr;
 
1168
                           break;
 
1169
                case '(' : if (!in_qstr)      /* comment (may nest)  */
 
1170
                               ++in_com;
 
1171
                           break;
 
1172
                case ')' : if (in_com)        /* end comment         */
 
1173
                               --in_com;
 
1174
                           break;
 
1175
                default  : break;
 
1176
            }
 
1177
        }
 
1178
    }
 
1179
 
 
1180
    if ((*len = (ptr - token)) == 0) {
 
1181
        *field = (const char *)ptr;
 
1182
        return NULL;
 
1183
    }
 
1184
 
 
1185
    /* Advance field pointer to the next non-comma, non-white byte */
 
1186
 
 
1187
    while (*ptr == ',' || apr_isspace(*ptr))
 
1188
        ++ptr;
 
1189
 
 
1190
    *field = (const char *)ptr;
 
1191
    return (const char *)token;
 
1192
}
 
1193
 
 
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.
 
1199
 */
 
1200
AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
 
1201
{
 
1202
    const char *tok_start;
 
1203
    const unsigned char *ptr;
 
1204
    unsigned char *pos;
 
1205
    char *token;
 
1206
    int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
 
1207
 
 
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.
 
1210
     */
 
1211
    if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
 
1212
        return NULL;
 
1213
    }
 
1214
    token = apr_palloc(p, tok_len + 1);
 
1215
 
 
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.
 
1220
     */
 
1221
    for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
 
1222
         *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
 
1223
         ++ptr) {
 
1224
 
 
1225
        if (in_qpair) {
 
1226
            in_qpair = 0;
 
1227
            *pos++ = *ptr;
 
1228
        }
 
1229
        else {
 
1230
            switch (*ptr) {
 
1231
                case '\\': in_qpair = 1;
 
1232
                           if (addspace == 1)
 
1233
                               *pos++ = ' ';
 
1234
                           *pos++ = *ptr;
 
1235
                           addspace = 0;
 
1236
                           break;
 
1237
                case '"' : if (!in_com)
 
1238
                               in_qstr = !in_qstr;
 
1239
                           if (addspace == 1)
 
1240
                               *pos++ = ' ';
 
1241
                           *pos++ = *ptr;
 
1242
                           addspace = 0;
 
1243
                           break;
 
1244
                case '(' : if (!in_qstr)
 
1245
                               ++in_com;
 
1246
                           if (addspace == 1)
 
1247
                               *pos++ = ' ';
 
1248
                           *pos++ = *ptr;
 
1249
                           addspace = 0;
 
1250
                           break;
 
1251
                case ')' : if (in_com)
 
1252
                               --in_com;
 
1253
                           *pos++ = *ptr;
 
1254
                           addspace = 0;
 
1255
                           break;
 
1256
                case ' ' :
 
1257
                case '\t': if (addspace)
 
1258
                               break;
 
1259
                           if (in_com || in_qstr)
 
1260
                               *pos++ = *ptr;
 
1261
                           else
 
1262
                               addspace = 1;
 
1263
                           break;
 
1264
                case '=' :
 
1265
                case '/' :
 
1266
                case ';' : if (!(in_com || in_qstr))
 
1267
                               addspace = -1;
 
1268
                           *pos++ = *ptr;
 
1269
                           break;
 
1270
                default  : if (addspace == 1)
 
1271
                               *pos++ = ' ';
 
1272
                           *pos++ = (in_com || in_qstr) ? *ptr
 
1273
                                                        : apr_tolower(*ptr);
 
1274
                           addspace = 0;
 
1275
                           break;
 
1276
            }
 
1277
        }
 
1278
    }
 
1279
    *pos = '\0';
 
1280
 
 
1281
    return token;
 
1282
}
 
1283
 
 
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.
 
1288
 */
 
1289
AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line,
 
1290
                                  const char *tok)
 
1291
{
 
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;
 
1295
 
 
1296
    if (!line || !tok)
 
1297
        return 0;
 
1298
 
 
1299
    do {  /* loop for each item in line's list */
 
1300
 
 
1301
        /* Find first non-comma, non-whitespace byte */
 
1302
 
 
1303
        while (*ptr == ',' || apr_isspace(*ptr))
 
1304
            ++ptr;
 
1305
 
 
1306
        if (*ptr)
 
1307
            good = 1;  /* until proven otherwise for this item */
 
1308
        else
 
1309
            break;     /* no items left and nothing good found */
 
1310
 
 
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.
 
1314
         */
 
1315
        for (pos = (const unsigned char *)tok;
 
1316
             *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
 
1317
             ++ptr) {
 
1318
 
 
1319
            if (in_qpair) {
 
1320
                in_qpair = 0;
 
1321
                if (good)
 
1322
                    good = (*pos++ == *ptr);
 
1323
            }
 
1324
            else {
 
1325
                switch (*ptr) {
 
1326
                    case '\\': in_qpair = 1;
 
1327
                               if (addspace == 1)
 
1328
                                   good = good && (*pos++ == ' ');
 
1329
                               good = good && (*pos++ == *ptr);
 
1330
                               addspace = 0;
 
1331
                               break;
 
1332
                    case '"' : if (!in_com)
 
1333
                                   in_qstr = !in_qstr;
 
1334
                               if (addspace == 1)
 
1335
                                   good = good && (*pos++ == ' ');
 
1336
                               good = good && (*pos++ == *ptr);
 
1337
                               addspace = 0;
 
1338
                               break;
 
1339
                    case '(' : if (!in_qstr)
 
1340
                                   ++in_com;
 
1341
                               if (addspace == 1)
 
1342
                                   good = good && (*pos++ == ' ');
 
1343
                               good = good && (*pos++ == *ptr);
 
1344
                               addspace = 0;
 
1345
                               break;
 
1346
                    case ')' : if (in_com)
 
1347
                                   --in_com;
 
1348
                               good = good && (*pos++ == *ptr);
 
1349
                               addspace = 0;
 
1350
                               break;
 
1351
                    case ' ' :
 
1352
                    case '\t': if (addspace || !good)
 
1353
                                   break;
 
1354
                               if (in_com || in_qstr)
 
1355
                                   good = (*pos++ == *ptr);
 
1356
                               else
 
1357
                                   addspace = 1;
 
1358
                               break;
 
1359
                    case '=' :
 
1360
                    case '/' :
 
1361
                    case ';' : if (!(in_com || in_qstr))
 
1362
                                   addspace = -1;
 
1363
                               good = good && (*pos++ == *ptr);
 
1364
                               break;
 
1365
                    default  : if (!good)
 
1366
                                   break;
 
1367
                               if (addspace == 1)
 
1368
                                   good = (*pos++ == ' ');
 
1369
                               if (in_com || in_qstr)
 
1370
                                   good = good && (*pos++ == *ptr);
 
1371
                               else
 
1372
                                   good = good && (*pos++ == apr_tolower(*ptr));
 
1373
                               addspace = 0;
 
1374
                               break;
 
1375
                }
 
1376
            }
 
1377
        }
 
1378
        if (good && *pos)
 
1379
            good = 0;          /* not good if only a prefix was matched */
 
1380
 
 
1381
    } while (*ptr && !good);
 
1382
 
 
1383
    return good;
 
1384
}
 
1385
 
 
1386
 
 
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.
 
1391
 */
 
1392
 
 
1393
AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line,
 
1394
                                int accept_white)
 
1395
{
 
1396
    const char *ptr = *accept_line;
 
1397
    const char *tok_start;
 
1398
    char *token;
 
1399
    int tok_len;
 
1400
 
 
1401
    /* Find first non-white byte */
 
1402
 
 
1403
    while (*ptr && apr_isspace(*ptr))
 
1404
        ++ptr;
 
1405
 
 
1406
    tok_start = ptr;
 
1407
 
 
1408
    /* find token end, skipping over quoted strings.
 
1409
     * (comments are already gone).
 
1410
     */
 
1411
 
 
1412
    while (*ptr && (accept_white || !apr_isspace(*ptr))
 
1413
           && *ptr != ';' && *ptr != ',') {
 
1414
        if (*ptr++ == '"')
 
1415
            while (*ptr)
 
1416
                if (*ptr++ == '"')
 
1417
                    break;
 
1418
    }
 
1419
 
 
1420
    tok_len = ptr - tok_start;
 
1421
    token = apr_pstrndup(p, tok_start, tok_len);
 
1422
 
 
1423
    /* Advance accept_line pointer to the next non-white byte */
 
1424
 
 
1425
    while (*ptr && apr_isspace(*ptr))
 
1426
        ++ptr;
 
1427
 
 
1428
    *accept_line = ptr;
 
1429
    return token;
 
1430
}
 
1431
 
 
1432
 
 
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)
 
1435
{
 
1436
    const unsigned char *start_token;
 
1437
    const unsigned char *s;
 
1438
 
 
1439
    if (!line)
 
1440
        return 0;
 
1441
 
 
1442
    s = (const unsigned char *)line;
 
1443
    for (;;) {
 
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
 
1446
         */
 
1447
        while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
 
1448
            ++s;
 
1449
        }
 
1450
        if (!*s) {
 
1451
            return 0;
 
1452
        }
 
1453
        start_token = s;
 
1454
        /* find end of the token */
 
1455
        while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
 
1456
            ++s;
 
1457
        }
 
1458
        if (!strncasecmp((const char *)start_token, (const char *)tok,
 
1459
                         s - start_token)) {
 
1460
            return 1;
 
1461
        }
 
1462
        if (!*s) {
 
1463
            return 0;
 
1464
        }
 
1465
    }
 
1466
}
 
1467
 
 
1468
 
 
1469
AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line,
 
1470
                                   const char *tok)
 
1471
{
 
1472
    int llen, tlen, lidx;
 
1473
 
 
1474
    if (!line)
 
1475
        return 0;
 
1476
 
 
1477
    llen = strlen(line);
 
1478
    tlen = strlen(tok);
 
1479
    lidx = llen - tlen;
 
1480
 
 
1481
    if (lidx < 0 ||
 
1482
        (lidx > 0 && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
 
1483
        return 0;
 
1484
 
 
1485
    return (strncasecmp(&line[lidx], tok, tlen) == 0);
 
1486
}
 
1487
 
 
1488
AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
 
1489
{
 
1490
    char *cmd;
 
1491
    unsigned char *d;
 
1492
    const unsigned char *s;
 
1493
 
 
1494
    cmd = apr_palloc(p, 2 * strlen(str) + 1);        /* Be safe */
 
1495
    d = (unsigned char *)cmd;
 
1496
    s = (const unsigned char *)str;
 
1497
    for (; *s; ++s) {
 
1498
 
 
1499
#if defined(OS2) || defined(WIN32)
 
1500
        /*
 
1501
         * Newlines to Win32/OS2 CreateProcess() are ill advised.
 
1502
         * Convert them to spaces since they are effectively white
 
1503
         * space to most applications
 
1504
         */
 
1505
        if (*s == '\r' || *s == '\n') {
 
1506
             *d++ = ' ';
 
1507
             continue;
 
1508
         }
 
1509
#endif
 
1510
 
 
1511
        if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
 
1512
            *d++ = '\\';
 
1513
        }
 
1514
        *d++ = *s;
 
1515
    }
 
1516
    *d = '\0';
 
1517
 
 
1518
    return cmd;
 
1519
}
 
1520
 
 
1521
static char x2c(const char *what)
 
1522
{
 
1523
    register char digit;
 
1524
 
 
1525
#if !APR_CHARSET_EBCDIC
 
1526
    digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10
 
1527
             : (what[0] - '0'));
 
1528
    digit *= 16;
 
1529
    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10
 
1530
              : (what[1] - '0'));
 
1531
#else /*APR_CHARSET_EBCDIC*/
 
1532
    char xstr[5];
 
1533
    xstr[0]='0';
 
1534
    xstr[1]='x';
 
1535
    xstr[2]=what[0];
 
1536
    xstr[3]=what[1];
 
1537
    xstr[4]='\0';
 
1538
    digit = apr_xlate_conv_byte(ap_hdrs_from_ascii,
 
1539
                                0xFF & strtol(xstr, NULL, 16));
 
1540
#endif /*APR_CHARSET_EBCDIC*/
 
1541
    return (digit);
 
1542
}
 
1543
 
 
1544
/*
 
1545
 * Unescapes a URL.
 
1546
 * Returns 0 on success, non-zero on error
 
1547
 * Failure is due to
 
1548
 *   bad % escape       returns HTTP_BAD_REQUEST
 
1549
 *
 
1550
 *   decoding %00 -> \0  (the null character)
 
1551
 *   decoding %2f -> /   (a special character)
 
1552
 *                      returns HTTP_NOT_FOUND
 
1553
 */
 
1554
AP_DECLARE(int) ap_unescape_url(char *url)
 
1555
{
 
1556
    register int badesc, badpath;
 
1557
    char *x, *y;
 
1558
 
 
1559
    badesc = 0;
 
1560
    badpath = 0;
 
1561
    /* Initial scan for first '%'. Don't bother writing values before
 
1562
     * seeing a '%' */
 
1563
    y = strchr(url, '%');
 
1564
    if (y == NULL) {
 
1565
        return OK;
 
1566
    }
 
1567
    for (x = y; *y; ++x, ++y) {
 
1568
        if (*y != '%')
 
1569
            *x = *y;
 
1570
        else {
 
1571
            if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
 
1572
                badesc = 1;
 
1573
                *x = '%';
 
1574
            }
 
1575
            else {
 
1576
                *x = x2c(y + 1);
 
1577
                y += 2;
 
1578
                if (IS_SLASH(*x) || *x == '\0')
 
1579
                    badpath = 1;
 
1580
            }
 
1581
        }
 
1582
    }
 
1583
    *x = '\0';
 
1584
    if (badesc)
 
1585
        return HTTP_BAD_REQUEST;
 
1586
    else if (badpath)
 
1587
        return HTTP_NOT_FOUND;
 
1588
    else
 
1589
        return OK;
 
1590
}
 
1591
 
 
1592
AP_DECLARE(int) ap_unescape_url_keep2f(char *url)
 
1593
{
 
1594
    register int badesc, badpath;
 
1595
    char *x, *y;
 
1596
 
 
1597
    badesc = 0;
 
1598
    badpath = 0;
 
1599
    /* Initial scan for first '%'. Don't bother writing values before
 
1600
     * seeing a '%' */
 
1601
    y = strchr(url, '%');
 
1602
    if (y == NULL) {
 
1603
        return OK;
 
1604
    }
 
1605
    for (x = y; *y; ++x, ++y) {
 
1606
        if (*y != '%') {
 
1607
            *x = *y;
 
1608
        }
 
1609
        else {
 
1610
            if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
 
1611
                badesc = 1;
 
1612
                *x = '%';
 
1613
            }
 
1614
            else {
 
1615
                char decoded;
 
1616
                decoded = x2c(y + 1);
 
1617
                if (decoded == '\0') {
 
1618
                    badpath = 1;
 
1619
                }
 
1620
                else {
 
1621
                    *x = decoded;
 
1622
                    y += 2;
 
1623
                }
 
1624
            }
 
1625
        }
 
1626
    }
 
1627
    *x = '\0';
 
1628
    if (badesc) {
 
1629
        return HTTP_BAD_REQUEST;
 
1630
    }
 
1631
    else if (badpath) {
 
1632
        return HTTP_NOT_FOUND;
 
1633
    }
 
1634
    else {
 
1635
        return OK;
 
1636
    }
 
1637
}
 
1638
 
 
1639
AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
 
1640
                                       apr_port_t port, const request_rec *r)
 
1641
{
 
1642
    if (ap_is_default_port(port, r)) {
 
1643
        return apr_pstrdup(p, hostname);
 
1644
    }
 
1645
    else {
 
1646
        return apr_psprintf(p, "%s:%u", hostname, port);
 
1647
    }
 
1648
}
 
1649
 
 
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.
 
1654
 *
 
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
 
1658
 * again.
 
1659
 */
 
1660
static const char c2x_table[] = "0123456789abcdef";
 
1661
 
 
1662
static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
 
1663
                                     unsigned char *where)
 
1664
{
 
1665
#if APR_CHARSET_EBCDIC
 
1666
    what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
 
1667
#endif /*APR_CHARSET_EBCDIC*/
 
1668
    *where++ = prefix;
 
1669
    *where++ = c2x_table[what >> 4];
 
1670
    *where++ = c2x_table[what & 0xf];
 
1671
    return where;
 
1672
}
 
1673
 
 
1674
/*
 
1675
 * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
 
1676
 * routine is (should be) OS independent.
 
1677
 *
 
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.
 
1684
 *
 
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 "./").
 
1687
 */
 
1688
 
 
1689
AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
 
1690
{
 
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;
 
1694
    unsigned c;
 
1695
 
 
1696
    while ((c = *s)) {
 
1697
        if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
 
1698
            d = c2x(c, '%', d);
 
1699
        }
 
1700
        else {
 
1701
            *d++ = c;
 
1702
        }
 
1703
        ++s;
 
1704
    }
 
1705
    *d = '\0';
 
1706
    return copy;
 
1707
}
 
1708
 
 
1709
AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
 
1710
{
 
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;
 
1714
    unsigned c;
 
1715
 
 
1716
    if (!partial) {
 
1717
        const char *colon = ap_strchr_c(path, ':');
 
1718
        const char *slash = ap_strchr_c(path, '/');
 
1719
 
 
1720
        if (colon && (!slash || colon < slash)) {
 
1721
            *d++ = '.';
 
1722
            *d++ = '/';
 
1723
        }
 
1724
    }
 
1725
    while ((c = *s)) {
 
1726
        if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
 
1727
            d = c2x(c, '%', d);
 
1728
        }
 
1729
        else {
 
1730
            *d++ = c;
 
1731
        }
 
1732
        ++s;
 
1733
    }
 
1734
    *d = '\0';
 
1735
    return copy;
 
1736
}
 
1737
 
 
1738
/* ap_escape_uri is now a macro for os_escape_path */
 
1739
 
 
1740
AP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
 
1741
{
 
1742
    int i, j;
 
1743
    char *x;
 
1744
 
 
1745
    /* first, count the number of extra characters */
 
1746
    for (i = 0, j = 0; s[i] != '\0'; i++)
 
1747
        if (s[i] == '<' || s[i] == '>')
 
1748
            j += 3;
 
1749
        else if (s[i] == '&')
 
1750
            j += 4;
 
1751
        else if (s[i] == '"')
 
1752
            j += 5;
 
1753
 
 
1754
    if (j == 0)
 
1755
        return apr_pstrmemdup(p, s, i);
 
1756
 
 
1757
    x = apr_palloc(p, i + j + 1);
 
1758
    for (i = 0, j = 0; s[i] != '\0'; i++, j++)
 
1759
        if (s[i] == '<') {
 
1760
            memcpy(&x[j], "&lt;", 4);
 
1761
            j += 3;
 
1762
        }
 
1763
        else if (s[i] == '>') {
 
1764
            memcpy(&x[j], "&gt;", 4);
 
1765
            j += 3;
 
1766
        }
 
1767
        else if (s[i] == '&') {
 
1768
            memcpy(&x[j], "&amp;", 5);
 
1769
            j += 4;
 
1770
        }
 
1771
        else if (s[i] == '"') {
 
1772
            memcpy(&x[j], "&quot;", 6);
 
1773
            j += 5;
 
1774
        }
 
1775
        else
 
1776
            x[j] = s[i];
 
1777
 
 
1778
    x[j] = '\0';
 
1779
    return x;
 
1780
}
 
1781
 
 
1782
AP_DECLARE(char *) ap_escape_logitem(apr_pool_t *p, const char *str)
 
1783
{
 
1784
    char *ret;
 
1785
    unsigned char *d;
 
1786
    const unsigned char *s;
 
1787
 
 
1788
    if (!str) {
 
1789
        return NULL;
 
1790
    }
 
1791
 
 
1792
    ret = apr_palloc(p, 4 * strlen(str) + 1); /* Be safe */
 
1793
    d = (unsigned char *)ret;
 
1794
    s = (const unsigned char *)str;
 
1795
    for (; *s; ++s) {
 
1796
 
 
1797
        if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
 
1798
            *d++ = '\\';
 
1799
            switch(*s) {
 
1800
            case '\b':
 
1801
                *d++ = 'b';
 
1802
                break;
 
1803
            case '\n':
 
1804
                *d++ = 'n';
 
1805
                break;
 
1806
            case '\r':
 
1807
                *d++ = 'r';
 
1808
                break;
 
1809
            case '\t':
 
1810
                *d++ = 't';
 
1811
                break;
 
1812
            case '\v':
 
1813
                *d++ = 'v';
 
1814
                break;
 
1815
            case '\\':
 
1816
            case '"':
 
1817
                *d++ = *s;
 
1818
                break;
 
1819
            default:
 
1820
                c2x(*s, 'x', d);
 
1821
                d += 3;
 
1822
            }
 
1823
        }
 
1824
        else {
 
1825
            *d++ = *s;
 
1826
        }
 
1827
    }
 
1828
    *d = '\0';
 
1829
 
 
1830
    return ret;
 
1831
}
 
1832
 
 
1833
AP_DECLARE(apr_size_t) ap_escape_errorlog_item(char *dest, const char *source,
 
1834
                                               apr_size_t buflen)
 
1835
{
 
1836
    unsigned char *d, *ep;
 
1837
    const unsigned char *s;
 
1838
 
 
1839
    if (!source || !buflen) { /* be safe */
 
1840
        return 0;
 
1841
    }
 
1842
 
 
1843
    d = (unsigned char *)dest;
 
1844
    s = (const unsigned char *)source;
 
1845
    ep = d + buflen - 1;
 
1846
 
 
1847
    for (; d < ep && *s; ++s) {
 
1848
 
 
1849
        if (TEST_CHAR(*s, T_ESCAPE_LOGITEM)) {
 
1850
            *d++ = '\\';
 
1851
            if (d >= ep) {
 
1852
                --d;
 
1853
                break;
 
1854
            }
 
1855
 
 
1856
            switch(*s) {
 
1857
            case '\b':
 
1858
                *d++ = 'b';
 
1859
                break;
 
1860
            case '\n':
 
1861
                *d++ = 'n';
 
1862
                break;
 
1863
            case '\r':
 
1864
                *d++ = 'r';
 
1865
                break;
 
1866
            case '\t':
 
1867
                *d++ = 't';
 
1868
                break;
 
1869
            case '\v':
 
1870
                *d++ = 'v';
 
1871
                break;
 
1872
            case '\\':
 
1873
                *d++ = *s;
 
1874
                break;
 
1875
            case '"': /* no need for this in error log */
 
1876
                d[-1] = *s;
 
1877
                break;
 
1878
            default:
 
1879
                if (d >= ep - 2) {
 
1880
                    ep = --d; /* break the for loop as well */
 
1881
                    break;
 
1882
                }
 
1883
                c2x(*s, 'x', d);
 
1884
                d += 3;
 
1885
            }
 
1886
        }
 
1887
        else {
 
1888
            *d++ = *s;
 
1889
        }
 
1890
    }
 
1891
    *d = '\0';
 
1892
 
 
1893
    return (d - (unsigned char *)dest);
 
1894
}
 
1895
 
 
1896
AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
 
1897
{
 
1898
    apr_finfo_t finfo;
 
1899
 
 
1900
    if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
 
1901
        return 0;                /* in error condition, just return no */
 
1902
 
 
1903
    return (finfo.filetype == APR_DIR);
 
1904
}
 
1905
 
 
1906
AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
 
1907
{
 
1908
    apr_finfo_t finfo;
 
1909
 
 
1910
    if (apr_stat(&finfo, path, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
 
1911
        return 0;                /* in error condition, just return no */
 
1912
 
 
1913
    return (finfo.filetype == APR_DIR);
 
1914
}
 
1915
 
 
1916
AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
 
1917
                                  const char *src2)
 
1918
{
 
1919
    apr_size_t len1, len2;
 
1920
    char *path;
 
1921
 
 
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 '/'
 
1926
      */
 
1927
    path = (char *)apr_palloc(a, len1 + len2 + 3);
 
1928
    if (len1 == 0) {
 
1929
        *path = '/';
 
1930
        memcpy(path + 1, src2, len2 + 1);
 
1931
    }
 
1932
    else {
 
1933
        char *next;
 
1934
        memcpy(path, src1, len1);
 
1935
        next = path + len1;
 
1936
        if (next[-1] != '/') {
 
1937
            *next++ = '/';
 
1938
        }
 
1939
        memcpy(next, src2, len2 + 1);
 
1940
    }
 
1941
    return path;
 
1942
}
 
1943
 
 
1944
/*
 
1945
 * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
 
1946
 */
 
1947
AP_DECLARE(int) ap_is_url(const char *u)
 
1948
{
 
1949
    register int x;
 
1950
 
 
1951
    for (x = 0; u[x] != ':'; x++) {
 
1952
        if ((!u[x]) ||
 
1953
            ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
 
1954
             (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
 
1955
            return 0;
 
1956
        }
 
1957
    }
 
1958
 
 
1959
    return (x ? 1 : 0);                /* If the first character is ':', it's broken, too */
 
1960
}
 
1961
 
 
1962
AP_DECLARE(int) ap_ind(const char *s, char c)
 
1963
{
 
1964
    const char *p = ap_strchr_c(s, c);
 
1965
 
 
1966
    if (p == NULL)
 
1967
        return -1;
 
1968
    return p - s;
 
1969
}
 
1970
 
 
1971
AP_DECLARE(int) ap_rind(const char *s, char c)
 
1972
{
 
1973
    const char *p = ap_strrchr_c(s, c);
 
1974
 
 
1975
    if (p == NULL)
 
1976
        return -1;
 
1977
    return p - s;
 
1978
}
 
1979
 
 
1980
AP_DECLARE(void) ap_str_tolower(char *str)
 
1981
{
 
1982
    while (*str) {
 
1983
        *str = apr_tolower(*str);
 
1984
        ++str;
 
1985
    }
 
1986
}
 
1987
 
 
1988
/*
 
1989
 * We must return a FQDN
 
1990
 */
 
1991
char *ap_get_local_host(apr_pool_t *a)
 
1992
{
 
1993
#ifndef MAXHOSTNAMELEN
 
1994
#define MAXHOSTNAMELEN 256
 
1995
#endif
 
1996
    char str[MAXHOSTNAMELEN + 1];
 
1997
    char *server_hostname = NULL;
 
1998
    apr_sockaddr_t *sockaddr;
 
1999
    char *hostname;
 
2000
 
 
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",
 
2004
                     ap_server_argv0);
 
2005
    } else {
 
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);
 
2014
            } else {
 
2015
                apr_sockaddr_ip_get(&hostname, sockaddr);
 
2016
                server_hostname = apr_pstrdup(a, hostname);
 
2017
            }
 
2018
        } else {
 
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);
 
2022
        }
 
2023
    }
 
2024
 
 
2025
    if (!server_hostname)
 
2026
        server_hostname = apr_pstrdup(a, "127.0.0.1");
 
2027
 
 
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);
 
2032
 
 
2033
    return server_hostname;
 
2034
}
 
2035
 
 
2036
/* simple 'pool' alloc()ing glue to apr_base64.c
 
2037
 */
 
2038
AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
 
2039
{
 
2040
    char *decoded;
 
2041
    int l;
 
2042
 
 
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 */
 
2046
 
 
2047
    return decoded;
 
2048
}
 
2049
 
 
2050
AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
 
2051
{
 
2052
    char *encoded;
 
2053
    int l = strlen(string);
 
2054
 
 
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 */
 
2058
 
 
2059
    return encoded;
 
2060
}
 
2061
 
 
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
 
2067
 * properly. -djg
 
2068
 */
 
2069
AP_DECLARE(void) ap_content_type_tolower(char *str)
 
2070
{
 
2071
    char *semi;
 
2072
 
 
2073
    semi = strchr(str, ';');
 
2074
    if (semi) {
 
2075
        *semi = '\0';
 
2076
    }
 
2077
    while (*str) {
 
2078
        *str = apr_tolower(*str);
 
2079
        ++str;
 
2080
    }
 
2081
    if (semi) {
 
2082
        *semi = ';';
 
2083
    }
 
2084
}
 
2085
 
 
2086
/*
 
2087
 * Given a string, replace any bare " with \" .
 
2088
 */
 
2089
AP_DECLARE(char *) ap_escape_quotes(apr_pool_t *p, const char *instring)
 
2090
{
 
2091
    int newlen = 0;
 
2092
    const char *inchr = instring;
 
2093
    char *outchr, *outstring;
 
2094
 
 
2095
    /*
 
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 ".
 
2098
     */
 
2099
    while (*inchr != '\0') {
 
2100
        newlen++;
 
2101
        if (*inchr == '"') {
 
2102
            newlen++;
 
2103
        }
 
2104
        /*
 
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.
 
2107
         */
 
2108
        if ((*inchr == '\\') && (inchr[1] != '\0')) {
 
2109
            inchr++;
 
2110
            newlen++;
 
2111
        }
 
2112
        inchr++;
 
2113
    }
 
2114
    outstring = apr_palloc(p, newlen + 1);
 
2115
    inchr = instring;
 
2116
    outchr = outstring;
 
2117
    /*
 
2118
     * Now copy the input string to the output string, inserting a slosh
 
2119
     * in front of every " that doesn't already have one.
 
2120
     */
 
2121
    while (*inchr != '\0') {
 
2122
        if ((*inchr == '\\') && (inchr[1] != '\0')) {
 
2123
            *outchr++ = *inchr++;
 
2124
            *outchr++ = *inchr++;
 
2125
        }
 
2126
        if (*inchr == '"') {
 
2127
            *outchr++ = '\\';
 
2128
        }
 
2129
        if (*inchr != '\0') {
 
2130
            *outchr++ = *inchr++;
 
2131
        }
 
2132
    }
 
2133
    *outchr = '\0';
 
2134
    return outstring;
 
2135
}
 
2136
 
 
2137
/*
 
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
 
2142
 */
 
2143
AP_DECLARE(char *) ap_append_pid(apr_pool_t *p, const char *string,
 
2144
                                    const char *delim)
 
2145
{
 
2146
    return apr_psprintf(p, "%s%s%" APR_PID_T_FMT, string,
 
2147
                        delim, getpid());
 
2148
 
 
2149
}