util.c revision 70a2c0407879c11b29adc782d1665a0131f30b3f
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
842ae4bd224140319ae7feec1872b93dfd491143fielding * Redistribution and use in source and binary forms, with or without
842ae4bd224140319ae7feec1872b93dfd491143fielding * modification, are permitted provided that the following conditions
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 1. Redistributions of source code must retain the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 2. Redistributions in binary form must reproduce the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer in
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the documentation and/or other materials provided with the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distribution.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 3. All advertising materials mentioning features or use of this
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * software must display the following acknowledgment:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * "This product includes software developed by the Apache Group
e8f95a682820a599fe41b22977010636be5c2717jim * for use in the Apache HTTP server project (http://www.apache.org/)."
e8f95a682820a599fe41b22977010636be5c2717jim * 4. The names "Apache Server" and "Apache Group" must not be used to
1747d30b98aa1bdbc43994c02cd46ab4cb9319e4fielding * endorse or promote products derived from this software without
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * prior written permission. For written permission, please contact
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * apache@apache.org.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 5. Products derived from this software may not be called "Apache"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * nor may "Apache" appear in their names without prior written
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * permission of the Apache Group.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 6. Redistributions of any form whatsoever must retain the following
11f2c481e1d57bedb3f758565307501e9a2730ddtrawick * acknowledgment:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * "This product includes software developed by the Apache Group
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * for use in the Apache HTTP server project (http://www.apache.org/)."
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
cd3bbd6d2df78d6c75e5d159a81ef8bdd5f70df9trawick * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * OF THE POSSIBILITY OF SUCH DAMAGE.
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * ====================================================================
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * This software consists of voluntary contributions made by many
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * individuals on behalf of the Apache Group and was originally based
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * on public domain software written at the National Center for
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Supercomputing Applications, University of Illinois, Urbana-Champaign.
bede2929837dfd23863ad4b39199c63126566d61jorton * For more information on the Apache Group and the Apache HTTP server
0f60998368b493f90120180a93fc2e1e74490872covener * project, please see <http://www.apache.org/>.
0f60998368b493f90120180a93fc2e1e74490872covener * util.c: string utility things
0f60998368b493f90120180a93fc2e1e74490872covener * 3/21/93 Rob McCool
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * 1995-96 Many changes by the Apache Group
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener/* Debugging aid:
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener * #define DEBUG_CFG_LINES to trace every line read from the config files
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* stdio.h has been read in ap_config.h already. Add missing prototypes here: */
0568280364eb026393be492ebc732795c4934643jorton/* A bunch of functions in util.c scan strings looking for certain characters.
0568280364eb026393be492ebc732795c4934643jorton * To make that more efficient we encode a lookup table. The test_char_table
0568280364eb026393be492ebc732795c4934643jorton * is generated automatically by gen_test_char.c.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* we assume the folks using this ensure 0 <= c < 256... which means
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * you need a cast to (unsigned char) first, you can't just plug a
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * char in here and get it to work, because if char is signed then it
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * will first be sign extended.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ZZZ When we abstract out time, this whole function should change to use
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes AP funcs. */
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin * Examine a field value (such as a media-/content-type) string and return
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_field_noparam(ap_context_t *p, const char *intype)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *semi;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_ht_time(ap_context_t *p, time_t t, const char *fmt, int gmt)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ZZZ this function can be replaced by calls to time formatting routines
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes /* Convert %Z to "GMT" and %z to "+0000";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * on hosts that do not have a time zone string in struct tm,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * strftime must assume its argument is local time.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *f;
fa123db15501821e36e513afa78e839775ad2800covener for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
fa123db15501821e36e513afa78e839775ad2800covener ; f++, strp++) {
fa123db15501821e36e513afa78e839775ad2800covener if (*f != '%') continue;
fa123db15501821e36e513afa78e839775ad2800covener switch (f[1]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* check return code? */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_gm_timestr_822(ap_context_t *p, time_t sec)
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf tms = gmtime(&sec); /* ZZZ replace with AP time routine */
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener /* Assumption: this is always 3 */
f2be127030aa4190033084f0a6add531c9bc41desf /* i = strlen(ap_day_snames[tms->tm_wday]); */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener memcpy(date_str_ptr, ap_day_snames[tms->tm_wday], 3);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* Assumption: this is also always 3 */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* i = strlen(ap_month_snames[tms->tm_mon]); */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener memcpy(date_str_ptr, ap_month_snames[tms->tm_mon], 3);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim /* This routine isn't y10k ready. */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* RFC date format; as strftime '%a, %d %b %Y %T GMT' */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* The equivalent using sprintf. Use this for more legible but slower code
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return ap_psprintf(p,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[tms->tm_wday],
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes tms->tm_mday, ap_month_snames[tms->tm_mon], tms->tm_year + 1900,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes tms->tm_hour, tms->tm_min, tms->tm_sec);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* What a pain in the ass. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Assume we are never more than 24 hours away. */
b08925593f214f621161742925dcf074a8047e0acovener gmt = *gmtime(&tt); /* remember gmtime/localtime return ptr to static */
465bb68501690d7a47bfd2a6129580047d76d8f1rederpj hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24)
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* Roy owes Rob beer. */
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* Rob owes Roy dinner. */
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* These legacy comments would make a lot more sense if Roy hadn't
55e2e59e5910072e51c870afc68b0907f41a28e0sf * replaced the old later_than() routine with util_date.c.
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Well, okay, they still wouldn't make any sense.
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* Match = 0, NoMatch = 1, Abort = -1
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Based loosely on sections of wildmat.c by Rich Salz
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Hmmm... shouldn't this really go component by component?
55e2e59e5910072e51c870afc68b0907f41a28e0sfAPI_EXPORT(int) ap_strcmp_match(const char *str, const char *exp)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes for (x = 0, y = 0; exp[y]; ++y, ++x) {
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf while (str[x]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesAPI_EXPORT(int) ap_strcasecmp_match(const char *str, const char *exp)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, y = 0; exp[y]; ++y, ++x) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (str[x]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
307219eca940aa30b873bfd68a44484dd3d3fa88covener else if ((exp[y] != '?') && (ap_tolower(str[x]) != ap_tolower(exp[y])))
707f6d077f73cc948deead8df5b40ea42c1eaa78covener register int x;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener for (x = 0; str[x]; x++)
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Here's a pool-based interface to POSIX regex's regcomp().
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Note that we return regex_t instead of being passed one.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * The reason is that if you use an already-used regex_t structure,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * the memory that you've already allocated gets forgotten, and
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * regfree() doesn't clear it. So we don't allow it.
55e2e59e5910072e51c870afc68b0907f41a28e0sfAPI_EXPORT(regex_t *) ap_pregcomp(ap_context_t *p, const char *pattern,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_register_cleanup(p, (void *) preg, regex_cleanup, regex_cleanup);
b08925593f214f621161742925dcf074a8047e0acovenerAPI_EXPORT(void) ap_pregfree(ap_context_t *p, regex_t * reg)
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Apache stub function for the regex libraries regexec() to make sure the
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * whole regex(3) API is available through the Apache (exported) namespace.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * This is especially important for the DSO situations of modules.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
185aa71728867671e105178b4c66fbc22b65ae26sfAPI_EXPORT(int) ap_regexec(const regex_t *preg, const char *string,
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return regexec(preg, string, nmatch, pmatch, eflags);
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerAPI_EXPORT(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return regerror(errcode, preg, errbuf, errbuf_size);
185aa71728867671e105178b4c66fbc22b65ae26sf/* This function substitutes for $0-$9, filling in regular expression
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * submatches. Pass it the same nmatch and pmatch arguments that you
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * passed ap_regexec(). pmatch should not be greater than the maximum number
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * of subexpressions - i.e. one more than the re_nsub member of regex_t.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * input should be the string with the $-expressions, source should be the
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * string that was matched against.
185aa71728867671e105178b4c66fbc22b65ae26sf * It returns the substituted string, or NULL on error.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * Parts of this code are based on Henry Spencer's regsub(), from his
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * AT&T V8 regexp package.
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerAPI_EXPORT(char *) ap_pregsub(ap_context_t *p, const char *input, const char *source,
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf /* First pass, find the size */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (c == '&')
141e1368614dc7564e1627671361b01b4869b491bnicholes else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Now actually fill in the string */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (c == '&')
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * Parse .. so we don't compromise security
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener /* Four paseses, as per RFC 1808 */
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener /* a) remove ./ path segments */
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* b) remove trailing . path, segment */
7dbf29be626018bc389ef94c1846aeac4b72633bsf else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* c) remove all xx/../ segments. (including leading ../ and /../) */
783874b86bfe13d7a4fe0922f344a3779cdccea3covener if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
7dbf29be626018bc389ef94c1846aeac4b72633bsf register int m = l + 3, n;
7dbf29be626018bc389ef94c1846aeac4b72633bsf l = l - 2;
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (l >= 0) {
783874b86bfe13d7a4fe0922f344a3779cdccea3covener (++n, ++m);
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* d) remove trailing xx/.. segment. */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (l >= 0) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes char *d, *s;
307219eca940aa30b873bfd68a44484dd3d3fa88covener /* Check for UNC names. Leave leading two slashes. */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *d++ = *s++;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (*s) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if ((*d++ = *s) == '/') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes } while (*s == '/');
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * copy at most n leading directories of s into d
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * d should be at least as large as s plus 1 extra byte
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * assumes n > 0
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * the return value is the ever useful pointer to the trailing \0 of d
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * examples:
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * /a/b, 1 ==> /
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * /a/b, 2 ==> /a/
e8f95a682820a599fe41b22977010636be5c2717jim * /a/b, 3 ==> /a/b/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * /a/b, 4 ==> /a/b/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
7dbf29be626018bc389ef94c1846aeac4b72633bsf *d++ = *s++;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener return (d);
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * return the parent directory name including trailing / of the file s
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covenerAPI_EXPORT(char *) ap_make_dirstr_parent(ap_context_t *p, const char *s)
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* XXX: well this is really broken if this happens */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim return (d);
9c63a05713cb83a44a1590b4af33edeebf39f118sf * This function is deprecated. Use one of the preceeding two functions
9c63a05713cb83a44a1590b4af33edeebf39f118sf * which are faster.
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimAPI_EXPORT(char *) ap_make_dirstr(ap_context_t *p, const char *s, int n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x, f;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, f = 0; s[x]; x++) {
9c63a05713cb83a44a1590b4af33edeebf39f118sf if (s[x] == '/')
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if ((++f) == n) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrdup(p, s);
e8f95a682820a599fe41b22977010636be5c2717jim register int x, n;
8a03cd420b800a2428f49f4617293de9b2387b20jorton for (x = 0, n = 0; path[x]; x++)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin const char *x;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (x == NULL) {
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener /* XXX: well, this is a silly function, no method of reporting an
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener * error... ah well. */
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(char *) ap_getword_nc(ap_context_t *atrans, char **line, char stop)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_getword(atrans, (const char **) line, stop);
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(char *) ap_getword(ap_context_t *atrans, const char **line, char stop)
e8f95a682820a599fe41b22977010636be5c2717jimAPI_EXPORT(char *) ap_getword_white_nc(ap_context_t *atrans, char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_getword_white(atrans, (const char **) line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_white(ap_context_t *atrans, const char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0; (*line)[x]; x++) {
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerAPI_EXPORT(char *) ap_getword_nulls_nc(ap_context_t *atrans, char **line, char stop)
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener return ap_getword_nulls(atrans, (const char **) line, stop);
307219eca940aa30b873bfd68a44484dd3d3fa88covenerAPI_EXPORT(char *) ap_getword_nulls(ap_context_t *atrans, const char **line, char stop)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Get a word, (new) config-file style --- quoted strings and backslashes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * all honored
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic char *substring_conf(ap_context_t *p, const char *start, int len, char quote)
307219eca940aa30b873bfd68a44484dd3d3fa88covener for (i = 0; i < len; ++i) {
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimAPI_EXPORT(char *) ap_getword_conf_nc(ap_context_t *p, char **line)
307219eca940aa30b873bfd68a44484dd3d3fa88covenerAPI_EXPORT(char *) ap_getword_conf(ap_context_t *p, const char **line)
0e05808dc59a321566303084c84b9826a4353cefrederpj if (*strend == '\\' && strend[1] && strend[1] == quote)
0e05808dc59a321566303084c84b9826a4353cefrederpj res = substring_conf(p, str + 1, strend - str - 1, quote);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return (int)EOF;
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerstatic void *cfg_getstr(void *buf, size_t bufsiz, void *param)
fa123db15501821e36e513afa78e839775ad2800covener/* Open a configfile_t as FILE, return open configfile_t struct pointer */
fa123db15501821e36e513afa78e839775ad2800covenerAPI_EXPORT(configfile_t *) ap_pcfg_openfile(ap_context_t *p, const char *name)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Internal error: pcfg_openfile() called with NULL filename");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
307219eca940aa30b873bfd68a44484dd3d3fa88covener "Access to config file %s denied: not a valid filename",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes stat = ap_open(&file, p, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT);
e8f95a682820a599fe41b22977010636be5c2717jim ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL,
e8f95a682820a599fe41b22977010636be5c2717jim "Opening config file %s (%s)",
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe name, (stat != APR_SUCCESS) ? strerror(errno) : "successful");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strcasecmp(name + strlen(name) - 4, "/nul") == 0))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif /* WIN32 || OS2 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "Access to file %s denied by server: not a regular file",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Allocate a configfile_t handle with user defined functions and params */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(configfile_t *) ap_pcfg_open_custom(ap_context_t *p, const char *descr,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void *(*getstr) (void *buf, size_t bufsiz, void *param),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes configfile_t *new_cfg = ap_palloc(p, sizeof(*new_cfg));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL, "Opening config handler %s", descr);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Read one character from a configfile_t */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Read one line from open configfile_t, strip LF, increase line number */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* If custom handler does not define a getstr() function, read char by char */
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covenerAPI_EXPORT(int) ap_cfg_getline(char *buf, size_t bufsize, configfile_t *cfp)
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* If a "get string" function is defined, use it */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * check for line continuation,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * i.e. match [^\\]\\[\r]\n only
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * line continuation requested -
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * then remove backslash and continue
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * no real continuation because escaped -
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * then just remove escape character
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Leading and trailing white space is eliminated completely
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* blast trailing whitespace */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* Zap leading whitespace by shifting */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL, "Read config: %s", buf);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* No "get string" function defined; read character by character */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int c;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register size_t i = 0;
e8f95a682820a599fe41b22977010636be5c2717jim /* skip leading whitespace */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* too small, assume caller is crazy */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (c == CR) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* silently ignore CR (_assume_ that a LF follows) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == LF) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* increase line number and return on LF */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * check for line continuation
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* line is continued */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* else nothing needs be done because
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * then the backslash is escaped and
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * we just strip to a single one
e8f95a682820a599fe41b22977010636be5c2717jim /* blast trailing whitespace */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL, "Read config: %s", buf);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Size an HTTP header field list item, as separated by a comma.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The return value is a pointer to the beginning of the non-empty list item
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * within the original string (or NULL if there is none) and the address
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * of field is shifted to the next non-comma, non-whitespace character.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * len is the length of the item excluding any beginning whitespace.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(const char *) ap_size_list_item(const char **field, int *len)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj const unsigned char *ptr = (const unsigned char *)*field;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj const unsigned char *token;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* Find first non-comma, non-whitespace byte */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Find the end of this item, skipping over dead bits */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes switch (*ptr) {
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener default : break;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* Advance field pointer to the next non-comma, non-white byte */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (const char *)token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Retrieve an HTTP header field list item, as separated by a comma,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * while stripping insignificant whitespace and lowercasing anything not in
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * a quoted string or comment. The return value is a new string containing
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the converted list item (or NULL if none) and the address pointed to by
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * field is shifted to the next non-comma, non-whitespace.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_get_list_item(ap_context_t *p, const char **field)
e8f95a682820a599fe41b22977010636be5c2717jim const char *tok_start;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe const unsigned char *ptr;
e8f95a682820a599fe41b22977010636be5c2717jim unsigned char *pos;
e8f95a682820a599fe41b22977010636be5c2717jim int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
e8f95a682820a599fe41b22977010636be5c2717jim /* Find the beginning and maximum length of the list item so that
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * we can allocate a buffer for the new string and reset the field.
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf /* Scan the token again, but this time copy only the good bytes.
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * We skip extra whitespace and any whitespace around a '=', '/',
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * or ';' and lowercase normal characters not within a comment,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * quoted-string or quoted-pair.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes switch (*ptr) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj/* Find an item in canonical form (lowercase, no extra spaces) within
8869662bb1a4078297020e94ae5e928626d877c6rederpj * an HTTP field value list. Returns 1 if found, 0 if not found.
8869662bb1a4078297020e94ae5e928626d877c6rederpj * This would be much more efficient if we stored header fields as
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * an array of list items as they are received instead of a plain string.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemAPI_EXPORT(int) ap_find_list_item(ap_context_t *p, const char *line, const char *tok)
8869662bb1a4078297020e94ae5e928626d877c6rederpj const unsigned char *pos;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener const unsigned char *ptr = (const unsigned char *)line;
8869662bb1a4078297020e94ae5e928626d877c6rederpj int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf do { /* loop for each item in line's list */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Find first non-comma, non-whitespace byte */
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = 1; /* until proven otherwise for this item */
8869662bb1a4078297020e94ae5e928626d877c6rederpj break; /* no items left and nothing good found */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* We skip extra whitespace and any whitespace around a '=', '/',
8869662bb1a4078297020e94ae5e928626d877c6rederpj * or ';' and lowercase normal characters not within a comment,
8869662bb1a4078297020e94ae5e928626d877c6rederpj * quoted-string or quoted-pair.
8869662bb1a4078297020e94ae5e928626d877c6rederpj *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
8869662bb1a4078297020e94ae5e928626d877c6rederpj switch (*ptr) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj default : if (!good)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = 0; /* not good if only a prefix was matched */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem/* Retrieve a token, spacing over it and returning a pointer to
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * the first non-white byte afterwards. Note that these tokens
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * are delimited by semis and commas; and can also be delimited
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * by whitespace at the caller's option.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemAPI_EXPORT(char *) ap_get_token(ap_context_t *p, const char **accept_line, int accept_white)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem const char *tok_start;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* Find first non-white byte */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* find token end, skipping over quoted strings.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * (comments are already gone).
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* Advance accept_line pointer to the next non-white byte */
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj/* find http tokens, see the definition of token from RFC2068 */
11ca38a20ab9b2d00258f745620e2724838e7e21rederpjAPI_EXPORT(int) ap_find_token(ap_context_t *p, const char *line, const char *tok)
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj const unsigned char *start_token;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj const unsigned char *s;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj s = (const unsigned char *)line;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj /* find start of token, skip all stop characters, note NUL
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj * isn't a token stop, so we don't need to test for it
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj /* find end of the token */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpjAPI_EXPORT(int) ap_find_last_token(ap_context_t *p, const char *line, const char *tok)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if ((lidx < 0) ||
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ((lidx > 0) && !(ap_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpjAPI_EXPORT(char *) ap_escape_shell_cmd(ap_context_t *p, const char *str)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj unsigned char *d;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj const unsigned char *s;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj cmd = ap_palloc(p, 2 * strlen(str) + 1); /* Be safe */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj d = (unsigned char *)cmd;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj s = (const unsigned char *)str;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj for (; *s; ++s) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* Don't allow '&' in parameters under OS/2. */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* This can be used to send commands to the shell. */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (*s == '&') {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *d++ = ' ';
0e05808dc59a321566303084c84b9826a4353cefrederpj *d++ = '\\';
185aa71728867671e105178b4c66fbc22b65ae26sf *d++ = *s;
342377e75b69a70c16175cce7f694472f9f8a6adjailletc register char digit;
0e05808dc59a321566303084c84b9826a4353cefrederpj digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
0e05808dc59a321566303084c84b9826a4353cefrederpj digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
0e05808dc59a321566303084c84b9826a4353cefrederpj#else /*CHARSET_EBCDIC*/
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj#endif /*CHARSET_EBCDIC*/
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Unescapes a URL.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Returns 0 on success, non-zero on error
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * Failure is due to
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * bad % escape returns BAD_REQUEST
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * decoding %00 -> \0
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * decoding %2f -> / (a special character)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * returns NOT_FOUND
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj char *x, *y;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* Initial scan for first '%'. Don't bother writing values before
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * seeing a '%' */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (y == NULL) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem for (x = y; *y; ++x, ++y) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (*y != '%')
0e05808dc59a321566303084c84b9826a4353cefrederpj if (!ap_isxdigit(*(y + 1)) || !ap_isxdigit(*(y + 2))) {
185aa71728867671e105178b4c66fbc22b65ae26sf *x = '\0';
0e05808dc59a321566303084c84b9826a4353cefrederpjAPI_EXPORT(char *) ap_construct_server(ap_context_t *p, const char *hostname,
0e05808dc59a321566303084c84b9826a4353cefrederpj/* c2x takes an unsigned, and expects the caller has guaranteed that
0e05808dc59a321566303084c84b9826a4353cefrederpj * 0 <= what < 256... which usually means that you have to cast to
185aa71728867671e105178b4c66fbc22b65ae26sf * unsigned char first, because (unsigned)(char)(x) fist goes through
0e05808dc59a321566303084c84b9826a4353cefrederpj * signed extension to an int before the unsigned cast.
0e05808dc59a321566303084c84b9826a4353cefrederpj * The reason for this assumption is to assist gcc code generation --
0e05808dc59a321566303084c84b9826a4353cefrederpj * the unsigned char -> unsigned extension is already done earlier in
0e05808dc59a321566303084c84b9826a4353cefrederpj * both uses of this code, so there's no need to waste time doing it
0e05808dc59a321566303084c84b9826a4353cefrederpjstatic ap_inline unsigned char *c2x(unsigned what, unsigned char *where)
0e05808dc59a321566303084c84b9826a4353cefrederpj * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * routine is (should be) OS independent.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * cases if a ':' occurs before the first '/' in the URL, the URL should be
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * efficiency reasons, we don't use escape_path_segment(), which is provided for
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * reference. Again, RFC 1808 is where this stuff is defined.
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * If partial is set, os_escape_path() assumes that the path will be appended to
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * something with a '/' in it (and thus does not prefix "./").
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpjAPI_EXPORT(char *) ap_escape_path_segment(ap_context_t *p, const char *segment)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem const unsigned char *s = (const unsigned char *)segment;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem unsigned char *d = (unsigned char *)copy;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem unsigned c;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj while ((c = *s)) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemAPI_EXPORT(char *) ap_os_escape_path(ap_context_t *p, const char *path, int partial)
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf const unsigned char *s = (const unsigned char *)path;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf unsigned char *d = (unsigned char *)copy;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf unsigned c;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *d++ = '.';
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj *d++ = '/';
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj while ((c = *s)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* ap_escape_uri is now a macro for os_escape_path */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_escape_html(ap_context_t *p, const char *s)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* first, count the number of extra characters */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0, j = 0; s[i] != '\0'; i++)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (s[i] == '&')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrndup(p, s, i);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0, j = 0; s[i] != '\0'; i++, j++)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[i] == '<') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (s[i] == '>') {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe else if (s[i] == '&') {
e8f95a682820a599fe41b22977010636be5c2717jim x[j] = s[i];
e8f95a682820a599fe41b22977010636be5c2717jim x[j] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ZZZ replace with AP File Info func. */
e8f95a682820a599fe41b22977010636be5c2717jim return 0; /* in error condition, just return no */
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covenerAPI_EXPORT(char *) ap_make_full_path(ap_context_t *a, const char *src1,
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener const char *src2)
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener register int x;
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener if (x == 0)
e8f95a682820a599fe41b22977010636be5c2717jim * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0; u[x] != ':'; x++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((!u[x]) ||
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener fprintf(stderr, "Ouch! Out of memory in our strdup()!\n");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* The following two routines were donated for SVR4 by Andreas Vogel */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint strcasecmp(const char *a, const char *b)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *p = a;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *q = b;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (p = a, q = b; *p && *q; p++, q++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0; /* Exact match */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint strncasecmp(const char *a, const char *b, int n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *p = a;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *q = b;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (p = a, q = b; /*NOTHING */ ; p++, q++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (p == a + n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0; /* Match up to n characters */
e8f95a682820a599fe41b22977010636be5c2717jim if (!(*p && *q))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return *p - *q;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*NOTREACHED */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* The following routine was donated for UTS21 by dwd@bell-labs.com */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* an empty s2 */
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* found first character of s2, see if the rest matches */
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* both strings ended together */
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* second string ended, a match */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* didn't find a match here, try starting at next character in s1 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* QNX, MPE and BeOS do not appear to support supplementary groups. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else /* ndef QNX */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
e8f95a682820a599fe41b22977010636be5c2717jim#endif /* def QNX */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif /* def NEED_INITGROUPS */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* From ikluft@amdahl.com
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * this is not ideal but it works for SVR3 variants
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Modified by dwd@bell-labs.com to call wait3 instead of wait because
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * apache started to use the WNOHANG option.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (((tmp_pid = wait3(statusp, options, 0)) != pid) &&
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe register int x;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe for (x = 0; s[x]; x++)
e8f95a682820a599fe41b22977010636be5c2717jim if (s[x] == c)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[x] == c)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "%s: bad user name %s\n", ap_server_argv0, name);
f05787953018140838ad51456c86c965d6a86267jim return (1);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "%s: bad group name %s\n", ap_server_argv0, name);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Parses a host of the form <address>[:port]
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * :port is permitted if 'port' is not NULL
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesunsigned long ap_get_virthost_addr(char *w, unsigned short *ports)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* ZZZ Redesign for AP func changes */
e8f95a682820a599fe41b22977010636be5c2717jim unsigned long my_addr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "Cannot resolve host name %s --- exiting!\n", w);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "Host %s has multiple addresses ---\n", w);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "you must choose one explicitly for use as\n");
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholesstatic char *find_fqdn(ap_context_t *a, struct hostent *p)
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener for (x = 0; p->h_aliases[x]; ++x) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener (!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* ZZZ change to use AP funcs. */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if ((!(p = gethostbyname(str))) || (!(server_hostname = find_fqdn(a, p)))) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "%s: cannot determine local host name.\n",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "Use the ServerName directive to set it manually.\n");
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/* simple 'pool' alloc()ing glue to ap_base64.c
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(char *) ap_pbase64decode(ap_context_t *p, const char *bufcoded)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes decoded = (char *) ap_palloc(p, 1 + ap_base64decode_len(bufcoded));
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes decoded[l] = '\0'; /* make binary sequence into string */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(char *) ap_pbase64encode(ap_context_t *p, char *string)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes encoded = (char *) ap_palloc(p, 1 + ap_base64encode_len(l));
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes encoded[l] = '\0'; /* make binary sequence into string */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/* deprecated names for the above two functions, here for compatibility
e8f95a682820a599fe41b22977010636be5c2717jimAPI_EXPORT(char *) ap_uudecode(ap_context_t *p, const char *bufcoded)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(char *) ap_uuencode(ap_context_t *p, char *string)
e8f95a682820a599fe41b22977010636be5c2717jim for (loop = 0; loop < (strlen(path) + 1) && loop < sizeof(newpath) - 1; loop++) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes offset = offset + 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes newpath[offset] = '\\';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* Debugging code */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* fprintf(stderr, "%s \n", newpath); */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/* quotes in the string are doubled up.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Used to escape quotes in args passed to OS/2's cmd.exe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes quote_doubled_str = ap_palloc(p, len + num_quotes + 1);
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes extern char *const sys_errlist[];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* we want to downcase the type/subtype for comparison purposes
e8f95a682820a599fe41b22977010636be5c2717jim * but nothing else because ;parameter=foo values are case sensitive.
e8f95a682820a599fe41b22977010636be5c2717jim * XXX: in truth we want to downcase parameter names... but really,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * apache has never handled parameters and such correctly. You
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes * also need to compress spaces and such to be able to compare
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes * properly. -djg
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholesAPI_EXPORT(void) ap_content_type_tolower(char *str)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * Given a string, replace any bare " with \" .
e8f95a682820a599fe41b22977010636be5c2717jimAPI_EXPORT(char *) ap_escape_quotes (ap_context_t *p, const char *instring)
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes * Look through the input string, jogging the length of the output
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * string up by an extra byte each time we find an unescaped ".
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If we find a slosh, and it's not the last byte in the string,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * it's escaping something - advance past both bytes.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Now copy the input string to the output string, inserting a slosh
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * in front of every " that doesn't already have one.