util.c revision 01e8aca9299a0b872414c24c8b7724d6f88ae665
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * The Apache Software License, Version 1.1
842ae4bd224140319ae7feec1872b93dfd491143fielding *
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
842ae4bd224140319ae7feec1872b93dfd491143fielding * reserved.
842ae4bd224140319ae7feec1872b93dfd491143fielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * Redistribution and use in source and binary forms, with or without
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * modification, are permitted provided that the following conditions
2d2eda71267231c2526be701fe655db125852c1ffielding * are met:
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * 1. Redistributions of source code must retain the above copyright
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * notice, this list of conditions and the following disclaimer.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * 2. Redistributions in binary form must reproduce the above copyright
2d2eda71267231c2526be701fe655db125852c1ffielding * notice, this list of conditions and the following disclaimer in
2d2eda71267231c2526be701fe655db125852c1ffielding * the documentation and/or other materials provided with the
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * distribution.
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh *
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * 3. The end-user documentation included with the redistribution,
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * if any, must include the following acknowledgment:
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * "This product includes software developed by the
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * Apache Software Foundation (http://www.apache.org/)."
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * Alternately, this acknowledgment may appear in the software itself,
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * if and wherever such third-party acknowledgments normally appear.
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh *
2d2eda71267231c2526be701fe655db125852c1ffielding * 4. The names "Apache" and "Apache Software Foundation" must
2d2eda71267231c2526be701fe655db125852c1ffielding * not be used to endorse or promote products derived from this
2d2eda71267231c2526be701fe655db125852c1ffielding * software without prior written permission. For written
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb * permission, please contact apache@apache.org.
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb *
92f3af936ce61f25358a3ee4f28df2f6d62040dfdreid * 5. Products derived from this software may not be called "Apache",
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb * nor may "Apache" appear in their name, without prior written
c9a95767fbf0f5fb0976a06b97a256033925e433rbb * permission of the Apache Software Foundation.
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
2d2eda71267231c2526be701fe655db125852c1ffielding * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2d2eda71267231c2526be701fe655db125852c1ffielding * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2d2eda71267231c2526be701fe655db125852c1ffielding * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
52de7a47876ce1748910cf3a0ee97f78842fab54rederpj * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52de7a47876ce1748910cf3a0ee97f78842fab54rederpj * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52de7a47876ce1748910cf3a0ee97f78842fab54rederpj * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52de7a47876ce1748910cf3a0ee97f78842fab54rederpj * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52de7a47876ce1748910cf3a0ee97f78842fab54rederpj * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52de7a47876ce1748910cf3a0ee97f78842fab54rederpj * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52de7a47876ce1748910cf3a0ee97f78842fab54rederpj * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * SUCH DAMAGE.
fd492f9543f14fb5bae78e04b135c3448eb9cc56rbb * ====================================================================
fd492f9543f14fb5bae78e04b135c3448eb9cc56rbb *
fd492f9543f14fb5bae78e04b135c3448eb9cc56rbb * This software consists of voluntary contributions made by many
fd492f9543f14fb5bae78e04b135c3448eb9cc56rbb * individuals on behalf of the Apache Software Foundation. For more
2d2eda71267231c2526be701fe655db125852c1ffielding * information on the Apache Software Foundation, please see
2d2eda71267231c2526be701fe655db125852c1ffielding * <http://www.apache.org/>.
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * Portions of this software are based upon public domain software
2d2eda71267231c2526be701fe655db125852c1ffielding * originally written at the National Center for Supercomputing Applications,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * University of Illinois, Urbana-Champaign.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/*
742318b93e89c311f66b55f426c4d9cf2c14628bjim * util.c: string utility things
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * 3/21/93 Rob McCool
8af88bd6958b80c224e964892b8237720b13ab1ajerenkrantz * 1995-96 Many changes by the Apache Software Foundation
8af88bd6958b80c224e964892b8237720b13ab1ajerenkrantz *
8af88bd6958b80c224e964892b8237720b13ab1ajerenkrantz */
8af88bd6958b80c224e964892b8237720b13ab1ajerenkrantz
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp/* Debugging aid:
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp * #define DEBUG_CFG_LINES to trace every line read from the config files
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp */
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp#include "apr.h"
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp#include "apr_strings.h"
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp#include "apr_lib.h"
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp#define APR_WANT_STDIO
8af88bd6958b80c224e964892b8237720b13ab1ajerenkrantz#define APR_WANT_STRFUNC
bfb62a96023822c56c9120e4ee627d4091cc59c2rbb#include "apr_want.h"
bfb62a96023822c56c9120e4ee627d4091cc59c2rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#if APR_HAVE_UNISTD_H
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#include <unistd.h>
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#endif
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#if APR_HAVE_NETDB_H
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#include <netdb.h> /* for gethostbyname() */
3d96ee83babeec32482c9082c9426340cee8c44dwrowe#endif
2d2eda71267231c2526be701fe655db125852c1ffielding
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#define CORE_PRIVATE
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#include "ap_config.h"
742318b93e89c311f66b55f426c4d9cf2c14628bjim#include "apr_base64.h"
742318b93e89c311f66b55f426c4d9cf2c14628bjim#include "httpd.h"
742318b93e89c311f66b55f426c4d9cf2c14628bjim#include "http_main.h"
742318b93e89c311f66b55f426c4d9cf2c14628bjim#include "http_log.h"
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#include "http_protocol.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_config.h"
3d96ee83babeec32482c9082c9426340cee8c44dwrowe#include "util_ebcdic.h"
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding#ifdef HAVE_PWD_H
2d2eda71267231c2526be701fe655db125852c1ffielding#include <pwd.h>
2d2eda71267231c2526be701fe655db125852c1ffielding#endif
000b67449410515eac43e76ef6667915bfd4d2abgstein#ifdef HAVE_GRP_H
2d2eda71267231c2526be701fe655db125852c1ffielding#include <grp.h>
2d2eda71267231c2526be701fe655db125852c1ffielding#endif
2d2eda71267231c2526be701fe655db125852c1ffielding
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* A bunch of functions in util.c scan strings looking for certain characters.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * To make that more efficient we encode a lookup table. The test_char_table
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * is generated automatically by gen_test_char.c.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#include "test_char.h"
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein/* we assume the folks using this ensure 0 <= c < 256... which means
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * you need a cast to (unsigned char) first, you can't just plug a
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * char in here and get it to work, because if char is signed then it
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * will first be sign extended.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein/*
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Examine a field value (such as a media-/content-type) string and return
742318b93e89c311f66b55f426c4d9cf2c14628bjim * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb const char *semi;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowe if (intype == NULL) return NULL;
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein
c9a95767fbf0f5fb0976a06b97a256033925e433rbb semi = ap_strchr_c(intype, ';');
c9a95767fbf0f5fb0976a06b97a256033925e433rbb if (semi == NULL) {
c9a95767fbf0f5fb0976a06b97a256033925e433rbb return apr_pstrdup(p, intype);
cf8d02ea0c91653917b044529f3133c5a1bb9200fielding }
c9a95767fbf0f5fb0976a06b97a256033925e433rbb else {
c9a95767fbf0f5fb0976a06b97a256033925e433rbb while ((semi > intype) && apr_isspace(semi[-1])) {
c9a95767fbf0f5fb0976a06b97a256033925e433rbb semi--;
c9a95767fbf0f5fb0976a06b97a256033925e433rbb }
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh return apr_pstrndup(p, intype, semi - intype);
c9a95767fbf0f5fb0976a06b97a256033925e433rbb }
742318b93e89c311f66b55f426c4d9cf2c14628bjim}
d82d78a97558238d16c52ec5278fe921bb7d7ec3brianp
d82d78a97558238d16c52ec5278fe921bb7d7ec3brianpAP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt, int gmt)
d82d78a97558238d16c52ec5278fe921bb7d7ec3brianp{
d82d78a97558238d16c52ec5278fe921bb7d7ec3brianp apr_size_t retcode;
d82d78a97558238d16c52ec5278fe921bb7d7ec3brianp char ts[MAX_STRING_LEN];
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh char tf[MAX_STRING_LEN];
d82d78a97558238d16c52ec5278fe921bb7d7ec3brianp apr_time_exp_t xt;
d82d78a97558238d16c52ec5278fe921bb7d7ec3brianp
c9a95767fbf0f5fb0976a06b97a256033925e433rbb if (gmt) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb const char *f;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *strp;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb apr_explode_gmt(&xt, t);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Convert %Z to "GMT" and %z to "+0000";
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * on hosts that do not have a time zone string in struct tm,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * strftime must assume its argument is local time.
742318b93e89c311f66b55f426c4d9cf2c14628bjim */
3d96ee83babeec32482c9082c9426340cee8c44dwrowe for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein ; f++, strp++) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (*f != '%') continue;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb switch (f[1]) {
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh case '%':
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *++strp = *++f;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe break;
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein case 'Z':
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *strp++ = 'G';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *strp++ = 'M';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *strp = 'T';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb f++;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe break;
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein case 'z': /* common extension */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *strp++ = '+';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *strp++ = '0';
742318b93e89c311f66b55f426c4d9cf2c14628bjim *strp++ = '0';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *strp++ = '0';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *strp = '0';
78ae1cbba5a848a24ca551629bbf0a90d2b931f0stas f++;
78ae1cbba5a848a24ca551629bbf0a90d2b931f0stas break;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
2d2eda71267231c2526be701fe655db125852c1ffielding *strp = '\0';
2d2eda71267231c2526be701fe655db125852c1ffielding fmt = tf;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding else {
2d2eda71267231c2526be701fe655db125852c1ffielding apr_explode_localtime(&xt, t);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* check return code? */
2d2eda71267231c2526be701fe655db125852c1ffielding apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
2d2eda71267231c2526be701fe655db125852c1ffielding ts[MAX_STRING_LEN - 1] = '\0';
2d2eda71267231c2526be701fe655db125852c1ffielding return apr_pstrdup(p, ts);
2d2eda71267231c2526be701fe655db125852c1ffielding}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
742318b93e89c311f66b55f426c4d9cf2c14628bjim/* Roy owes Rob beer. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* Rob owes Roy dinner. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* These legacy comments would make a lot more sense if Roy hadn't
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * replaced the old later_than() routine with util_date.c.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Well, okay, they still wouldn't make any sense.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
742318b93e89c311f66b55f426c4d9cf2c14628bjim
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm/* Match = 0, NoMatch = 1, Abort = -1
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein * Based loosely on sections of wildmat.c by Rich Salz
23ff73a56371e21f16744cb94d06399b877829f1rbb * Hmmm... shouldn't this really go component by component?
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(int) ap_strcmp_match(const char *str, const char *exp)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int x, y;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (x = 0, y = 0; exp[y]; ++y, ++x) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if ((!str[x]) && (exp[y] != '*'))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return -1;
b02dfdb611f84136664b3c05e4d4d704aeabbf63wrowe if (exp[y] == '*') {
b02dfdb611f84136664b3c05e4d4d704aeabbf63wrowe while (exp[++y] == '*');
b02dfdb611f84136664b3c05e4d4d704aeabbf63wrowe if (!exp[y])
b02dfdb611f84136664b3c05e4d4d704aeabbf63wrowe return 0;
23ff73a56371e21f16744cb94d06399b877829f1rbb while (str[x]) {
2d2eda71267231c2526be701fe655db125852c1ffielding int ret;
62db15de4c1f335a64d45821796ae197cff94ef8rbb if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
62db15de4c1f335a64d45821796ae197cff94ef8rbb return ret;
62db15de4c1f335a64d45821796ae197cff94ef8rbb }
62db15de4c1f335a64d45821796ae197cff94ef8rbb return -1;
62db15de4c1f335a64d45821796ae197cff94ef8rbb }
62db15de4c1f335a64d45821796ae197cff94ef8rbb else if ((exp[y] != '?') && (str[x] != exp[y]))
62db15de4c1f335a64d45821796ae197cff94ef8rbb return 1;
62db15de4c1f335a64d45821796ae197cff94ef8rbb }
62db15de4c1f335a64d45821796ae197cff94ef8rbb return (str[x] != '\0');
48d2edbfb84e5559b5da0f8d614ccab805cc67a8rbb}
62db15de4c1f335a64d45821796ae197cff94ef8rbb
62db15de4c1f335a64d45821796ae197cff94ef8rbbAP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *exp)
62db15de4c1f335a64d45821796ae197cff94ef8rbb{
62db15de4c1f335a64d45821796ae197cff94ef8rbb int x, y;
62db15de4c1f335a64d45821796ae197cff94ef8rbb
62db15de4c1f335a64d45821796ae197cff94ef8rbb for (x = 0, y = 0; exp[y]; ++y, ++x) {
62db15de4c1f335a64d45821796ae197cff94ef8rbb if ((!str[x]) && (exp[y] != '*'))
62db15de4c1f335a64d45821796ae197cff94ef8rbb return -1;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh if (exp[y] == '*') {
62db15de4c1f335a64d45821796ae197cff94ef8rbb while (exp[++y] == '*');
62db15de4c1f335a64d45821796ae197cff94ef8rbb if (!exp[y])
62db15de4c1f335a64d45821796ae197cff94ef8rbb return 0;
48d2edbfb84e5559b5da0f8d614ccab805cc67a8rbb while (str[x]) {
48d2edbfb84e5559b5da0f8d614ccab805cc67a8rbb int ret;
62db15de4c1f335a64d45821796ae197cff94ef8rbb if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return ret;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return -1;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar else if ((exp[y] != '?') && (apr_tolower(str[x]) != apr_tolower(exp[y])))
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return 1;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return (str[x] != '\0');
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar}
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh/* We actually compare the canonical root to this root, (but we don't
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * waste time checking the case), since every use of this function in
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * httpd-2.0 tests if the path is 'proper', meaning we've already passed
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * it through apr_filepath_merge, or we haven't.
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh */
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhAP_DECLARE(int) ap_os_is_path_absolute(apr_pool_t *p, const char *dir)
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh{
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh const char *newpath;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe const char *ourdir = dir;
11e076839c8d5a82d55e710194d0daac51390dbdsf if (apr_filepath_root(&newpath, &dir, 0, p) != APR_SUCCESS
599c5abcc7fec60611956f56b732eca033c287d2pquerna || strncmp(newpath, ourdir, strlen(newpath)) != 0) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return 0;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return 1;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar}
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
41287245b1632a4722b55f6dfe5fcf81f361f043wroweAP_DECLARE(int) ap_is_matchexp(const char *str)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar{
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar register int x;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar for (x = 0; str[x]; x++)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if ((str[x] == '*') || (str[x] == '?'))
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return 1;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return 0;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar}
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
41287245b1632a4722b55f6dfe5fcf81f361f043wrowe/*
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar * Here's a pool-based interface to POSIX regex's regcomp().
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar * Note that we return regex_t instead of being passed one.
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * The reason is that if you use an already-used regex_t structure,
742318b93e89c311f66b55f426c4d9cf2c14628bjim * the memory that you've already allocated gets forgotten, and
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar * regfree() doesn't clear it. So we don't allow it.
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar */
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
41287245b1632a4722b55f6dfe5fcf81f361f043wrowestatic apr_status_t regex_cleanup(void *preg)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar{
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar regfree((regex_t *) preg);
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return APR_SUCCESS;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe}
11e076839c8d5a82d55e710194d0daac51390dbdsf
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coarAP_DECLARE(regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar int cflags)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar{
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar regex_t *preg = apr_palloc(p, sizeof(regex_t));
41287245b1632a4722b55f6dfe5fcf81f361f043wrowe
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (regcomp(preg, pattern, cflags)) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return NULL;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
742318b93e89c311f66b55f426c4d9cf2c14628bjim
2864362ca8266097928e84f101010bdf814ffa08stoddard apr_pool_cleanup_register(p, (void *) preg, regex_cleanup, regex_cleanup);
742318b93e89c311f66b55f426c4d9cf2c14628bjim
db08da9ddcd65c31f9ea44b823898b72a1b24fbestoddard return preg;
bf648cbfa4a723c9c02fe8444103cd41ed8642d0wrowe}
742318b93e89c311f66b55f426c4d9cf2c14628bjim
bf648cbfa4a723c9c02fe8444103cd41ed8642d0wroweAP_DECLARE(void) ap_pregfree(apr_pool_t *p, regex_t * reg)
2864362ca8266097928e84f101010bdf814ffa08stoddard{
db08da9ddcd65c31f9ea44b823898b72a1b24fbestoddard regfree(reg);
2864362ca8266097928e84f101010bdf814ffa08stoddard apr_pool_cleanup_kill(p, (void *) reg, regex_cleanup);
af7cd2711a9737c0a2aa1b8475455a7dd8ce6113covener}
beb207f53c871585c4858040f0a8c0e2005c3c6dcovener
af7cd2711a9737c0a2aa1b8475455a7dd8ce6113covener/*
af7cd2711a9737c0a2aa1b8475455a7dd8ce6113covener * Similar to standard strstr() but we ignore case in this version.
af7cd2711a9737c0a2aa1b8475455a7dd8ce6113covener * Based on the strstr() implementation further below.
af7cd2711a9737c0a2aa1b8475455a7dd8ce6113covener */
af7cd2711a9737c0a2aa1b8475455a7dd8ce6113covenerAP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding char *p1, *p2;
2d2eda71267231c2526be701fe655db125852c1ffielding if (*s2 == '\0') {
2d2eda71267231c2526be701fe655db125852c1ffielding /* an empty s2 */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return((char *)s1);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while(1) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (*s1 == '\0') return(NULL);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* found first character of s2, see if the rest matches */
3d96ee83babeec32482c9082c9426340cee8c44dwrowe p1 = (char *)s1;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein p2 = (char *)s2;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while (apr_tolower(*++p1) == apr_tolower(*++p2)) {
b5c963a560a55d08763229330dc661b16b726da0sf if (*p1 == '\0') {
b5c963a560a55d08763229330dc661b16b726da0sf /* both strings ended together */
b5c963a560a55d08763229330dc661b16b726da0sf return((char *)s1);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (*p2 == '\0') {
b5c963a560a55d08763229330dc661b16b726da0sf /* second string ended, a match */
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein break;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
b5c963a560a55d08763229330dc661b16b726da0sf /* didn't find a match here, try starting at next character in s1 */
b5c963a560a55d08763229330dc661b16b726da0sf s1++;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return((char *)s1);
b5c963a560a55d08763229330dc661b16b726da0sf}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
2968569cc3b1440bdb874d2d597ea4a49f0c0acfsf/*
b5c963a560a55d08763229330dc661b16b726da0sf * Returns an offsetted pointer in bigstring immediately after
b5c963a560a55d08763229330dc661b16b726da0sf * prefix. Returns bigstring if bigstring doesn't start with
b5c963a560a55d08763229330dc661b16b726da0sf * prefix or if prefix is longer than bigstring while still matching.
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein * NOTE: pointer returned is relative to bigstring, so we
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * can use standard pointer comparisons in the calling function
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * (eg: test if ap_stripprefix(a,b) == a)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb const char *prefix)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
64945a581b504264270920efbad4f6af0d5a1978sf const char *p1;
a054609255eb3b10ed30e5471b18ea0e7d5f735esf
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein if (*prefix == '\0')
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return bigstring;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb p1 = bigstring;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while (*p1 && *prefix) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (*p1++ != *prefix++)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return bigstring;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
aecb17a45c6d3ee4729ed5f68dc4270f211ee7a8fielding if (*prefix == '\0')
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein return p1;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* hit the end of bigstring! */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return bigstring;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/*
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Apache stub function for the regex libraries regexec() to make sure the
aecb17a45c6d3ee4729ed5f68dc4270f211ee7a8fielding * whole regex(3) API is available through the Apache (exported) namespace.
11e076839c8d5a82d55e710194d0daac51390dbdsf * This is especially important for the DSO situations of modules.
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(int) ap_regexec(regex_t *preg, const char *string,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb size_t nmatch, regmatch_t pmatch[], int eflags)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return regexec(preg, string, nmatch, pmatch, eflags);
3d96ee83babeec32482c9082c9426340cee8c44dwrowe}
2d2eda71267231c2526be701fe655db125852c1ffielding
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding return regerror(errcode, preg, errbuf, errbuf_size);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
2d2eda71267231c2526be701fe655db125852c1ffielding
3d96ee83babeec32482c9082c9426340cee8c44dwrowe/* This function substitutes for $0-$9, filling in regular expression
2d2eda71267231c2526be701fe655db125852c1ffielding * submatches. Pass it the same nmatch and pmatch arguments that you
742318b93e89c311f66b55f426c4d9cf2c14628bjim * passed ap_regexec(). pmatch should not be greater than the maximum number
d839a9822ee53ce00da24c15f2d9fe054233d342gstein * of subexpressions - i.e. one more than the re_nsub member of regex_t.
d839a9822ee53ce00da24c15f2d9fe054233d342gstein *
742318b93e89c311f66b55f426c4d9cf2c14628bjim * input should be the string with the $-expressions, source should be the
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * string that was matched against.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *
d839a9822ee53ce00da24c15f2d9fe054233d342gstein * It returns the substituted string, or NULL on error.
3d96ee83babeec32482c9082c9426340cee8c44dwrowe *
d839a9822ee53ce00da24c15f2d9fe054233d342gstein * Parts of this code are based on Henry Spencer's regsub(), from his
2d2eda71267231c2526be701fe655db125852c1ffielding * AT&T V8 regexp package.
2d2eda71267231c2526be701fe655db125852c1ffielding */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input, const char *source,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb size_t nmatch, regmatch_t pmatch[])
742318b93e89c311f66b55f426c4d9cf2c14628bjim{
346029f34d03eb20d84fc35664426d3874b00f9ewrowe const char *src = input;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *dest, *dst;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char c;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb size_t no;
346029f34d03eb20d84fc35664426d3874b00f9ewrowe int len;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (!source)
3d96ee83babeec32482c9082c9426340cee8c44dwrowe return NULL;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein if (!nmatch)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return apr_pstrdup(p, src);
742318b93e89c311f66b55f426c4d9cf2c14628bjim
face099b681c052cb1f52176a5499661c44b2982trawick /* First pass, find the size */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb len = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while ((c = *src++) != '\0') {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (c == '&')
3d96ee83babeec32482c9082c9426340cee8c44dwrowe no = 0;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein else if (c == '$' && apr_isdigit(*src))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb no = *src++ - '0';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb no = 10;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (no > 9) { /* Ordinary character. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (c == '\\' && (*src == '$' || *src == '&'))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb c = *src++;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb len++;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
aecb17a45c6d3ee4729ed5f68dc4270f211ee7a8fielding else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein len += pmatch[no].rm_eo - pmatch[no].rm_so;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb dest = dst = apr_pcalloc(p, len + 1);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Now actually fill in the string */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
742318b93e89c311f66b55f426c4d9cf2c14628bjim src = input;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowe while ((c = *src++) != '\0') {
2d2eda71267231c2526be701fe655db125852c1ffielding if (c == '&')
61fd0cab072a05b855cbef9c585702401ac5ae29rbb no = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else if (c == '$' && apr_isdigit(*src))
a44d29a3794110c558c940bd903a1930d717a7d7sf no = *src++ - '0';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else
742318b93e89c311f66b55f426c4d9cf2c14628bjim no = 10;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein if (no > 9) { /* Ordinary character. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (c == '\\' && (*src == '$' || *src == '&'))
a44d29a3794110c558c940bd903a1930d717a7d7sf c = *src++;
742318b93e89c311f66b55f426c4d9cf2c14628bjim *dst++ = c;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb len = pmatch[no].rm_eo - pmatch[no].rm_so;
a44d29a3794110c558c940bd903a1930d717a7d7sf memcpy(dst, source + pmatch[no].rm_so, len);
742318b93e89c311f66b55f426c4d9cf2c14628bjim dst += len;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein
a44d29a3794110c558c940bd903a1930d717a7d7sf }
a44d29a3794110c558c940bd903a1930d717a7d7sf *dst = '\0';
a44d29a3794110c558c940bd903a1930d717a7d7sf
a44d29a3794110c558c940bd903a1930d717a7d7sf return dest;
a44d29a3794110c558c940bd903a1930d717a7d7sf}
a44d29a3794110c558c940bd903a1930d717a7d7sf
a44d29a3794110c558c940bd903a1930d717a7d7sf/*
a44d29a3794110c558c940bd903a1930d717a7d7sf * Parse .. so we don't compromise security
a44d29a3794110c558c940bd903a1930d717a7d7sf */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(void) ap_getparents(char *name)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *next;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int l, w, first_dot;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
742318b93e89c311f66b55f426c4d9cf2c14628bjim /* Four paseses, as per RFC 1808 */
742318b93e89c311f66b55f426c4d9cf2c14628bjim /* a) remove ./ path segments */
742318b93e89c311f66b55f426c4d9cf2c14628bjim for (next = name; *next && (*next != '.'); next++) {
742318b93e89c311f66b55f426c4d9cf2c14628bjim }
742318b93e89c311f66b55f426c4d9cf2c14628bjim l = w = first_dot = next - name;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while (name[l] != '\0') {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
3d96ee83babeec32482c9082c9426340cee8c44dwrowe l += 2;
2d2eda71267231c2526be701fe655db125852c1ffielding else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb name[w++] = name[l++];
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
742318b93e89c311f66b55f426c4d9cf2c14628bjim
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh /* b) remove trailing . path, segment */
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh if (w == 1 && name[0] == '.')
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh w--;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
61fd0cab072a05b855cbef9c585702401ac5ae29rbb w--;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb name[w] = '\0';
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
2d2eda71267231c2526be701fe655db125852c1ffielding /* c) remove all xx/../ segments. (including leading ../ and /../) */
fd8b91502bc200ed4cca3810560a2a570522b3debrianp l = first_dot;
fd8b91502bc200ed4cca3810560a2a570522b3debrianp
fd8b91502bc200ed4cca3810560a2a570522b3debrianp while (name[l] != '\0') {
fd8b91502bc200ed4cca3810560a2a570522b3debrianp if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
fd8b91502bc200ed4cca3810560a2a570522b3debrianp (l == 0 || name[l - 1] == '/')) {
fd8b91502bc200ed4cca3810560a2a570522b3debrianp register int m = l + 3, n;
fd8b91502bc200ed4cca3810560a2a570522b3debrianp
fd8b91502bc200ed4cca3810560a2a570522b3debrianp l = l - 2;
fd8b91502bc200ed4cca3810560a2a570522b3debrianp if (l >= 0) {
fd8b91502bc200ed4cca3810560a2a570522b3debrianp while (l >= 0 && name[l] != '/')
aa4af3da5c68fe5dbd9d2c3fd7b2fe7103daa2b7dougm l--;
e351a4349a3dcc2e8d9c27bcdf72414bdde0942frbb l++;
fd8b91502bc200ed4cca3810560a2a570522b3debrianp }
fd8b91502bc200ed4cca3810560a2a570522b3debrianp else
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames l = 0;
742318b93e89c311f66b55f426c4d9cf2c14628bjim n = l;
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames while ((name[n] = name[m]))
742318b93e89c311f66b55f426c4d9cf2c14628bjim (++n, ++m);
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames }
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames else
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames ++l;
742318b93e89c311f66b55f426c4d9cf2c14628bjim }
fd8b91502bc200ed4cca3810560a2a570522b3debrianp
fd8b91502bc200ed4cca3810560a2a570522b3debrianp /* d) remove trailing xx/.. segment. */
fd8b91502bc200ed4cca3810560a2a570522b3debrianp if (l == 2 && name[0] == '.' && name[1] == '.')
fd8b91502bc200ed4cca3810560a2a570522b3debrianp name[0] = '\0';
45613d36b9466a48def0498cffa07f48980720f8jerenkrantz else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
fd8b91502bc200ed4cca3810560a2a570522b3debrianp l = l - 4;
fd8b91502bc200ed4cca3810560a2a570522b3debrianp if (l >= 0) {
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp while (l >= 0 && name[l] != '/')
45613d36b9466a48def0498cffa07f48980720f8jerenkrantz l--;
45613d36b9466a48def0498cffa07f48980720f8jerenkrantz l++;
45613d36b9466a48def0498cffa07f48980720f8jerenkrantz }
fd8b91502bc200ed4cca3810560a2a570522b3debrianp else
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames l = 0;
742318b93e89c311f66b55f426c4d9cf2c14628bjim name[l] = '\0';
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames }
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp}
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregamesAP_DECLARE(void) ap_no2slash(char *name)
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp{
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp char *d, *s;
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh s = d = name;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
742318b93e89c311f66b55f426c4d9cf2c14628bjim#ifdef HAVE_UNC_PATHS
4111de96e9f75c58e77c2bdda23be83b8ebf81ccgregames /* Check for UNC names. Leave leading two slashes. */
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp if (s[0] == '/' && s[1] == '/')
50e60f30bdc074fbc887f0b98f4d570457ac97c9brianp *d++ = *s++;
5a9667916c79d8c699b069068e5570aa1c331c80gstein#endif
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while (*s) {
2d2eda71267231c2526be701fe655db125852c1ffielding if ((*d++ = *s) == '/') {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb do {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ++s;
2d2eda71267231c2526be701fe655db125852c1ffielding } while (*s == '/');
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
2d2eda71267231c2526be701fe655db125852c1ffielding else {
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar ++s;
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar }
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar }
5a9667916c79d8c699b069068e5570aa1c331c80gstein *d = '\0';
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar}
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar
5a9667916c79d8c699b069068e5570aa1c331c80gstein/*
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar * copy at most n leading directories of s into d
d5df46b7972c0c4a5ca0ba5068e238f6053c2e6ftrawick * d should be at least as large as s plus 1 extra byte
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * assumes n > 0
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * the return value is the ever useful pointer to the trailing \0 of d
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim *
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * so that if n == 0, "/" is returned in d with n == 1
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * and s == "e:/test.html", "e:/" is returned in d
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * *** See also directory_walk in modules/http/http_request.c
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * examples:
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * /a/b, 0 ==> / (true for all platforms)
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * /a/b, 1 ==> /
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * /a/b, 2 ==> /a/
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * /a/b, 3 ==> /a/b/
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * /a/b, 4 ==> /a/b/
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim *
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * c:/a/b 0 ==> /
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * c:/a/b 1 ==> c:/
a2930b0b01bfdbaa9e5c3324266bce9ef74158c2jim * c:/a/b 2 ==> c:/a/
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * c:/a/b 3 ==> c:/a/b
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * c:/a/b 4 ==> c:/a/b
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (n < 1) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *d = '/';
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm *++d = '\0';
742318b93e89c311f66b55f426c4d9cf2c14628bjim return (d);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (;;) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (*s == '\0' || (*s == '/' && (--n) == 0)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *d = '/';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb break;
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm }
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein *d++ = *s++;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
7b6ba9c468f26bdb3492d5e8cb79628a3b04e8c8wrowe *++d = 0;
7b6ba9c468f26bdb3492d5e8cb79628a3b04e8c8wrowe return (d);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb}
7b6ba9c468f26bdb3492d5e8cb79628a3b04e8c8wrowe
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
7b6ba9c468f26bdb3492d5e8cb79628a3b04e8c8wrowe/*
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein * return the parent directory name including trailing / of the file s
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbAP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb const char *last_slash = ap_strrchr_c(s, '/');
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *d;
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm int l;
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh if (last_slash == NULL) {
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb return apr_pstrdup(p, "");
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein }
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb l = (last_slash - s) + 1;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh d = apr_palloc(p, l + 1);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh memcpy(d, s, l);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh d[l] = 0;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb return (d);
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb}
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbbAP_DECLARE(int) ap_count_dirs(const char *path)
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb{
0de8f88b22d24dd57ed10b96fb594e69b6aa0072jwoolley register int x, n;
0de8f88b22d24dd57ed10b96fb594e69b6aa0072jwoolley
8b7047e519340545e6807c9749576a40a76b6d3frbb for (x = 0, n = 0; path[x]; x++)
8b7047e519340545e6807c9749576a40a76b6d3frbb if (path[x] == '/')
8b7047e519340545e6807c9749576a40a76b6d3frbb n++;
6d00a43be9ab145c89e8cc22bad59e3aa746f761jwoolley return n;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb}
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhAP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
9484ae61c7cc5fa8d8d9a836efdbdb1e88d3036dwrowe{
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb return ap_getword(atrans, (const char **) line, stop);
2e75499d05677dd589f7ce6b5139ae15ba049419jerenkrantz}
2e75499d05677dd589f7ce6b5139ae15ba049419jerenkrantz
2e75499d05677dd589f7ce6b5139ae15ba049419jerenkrantzAP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
2e75499d05677dd589f7ce6b5139ae15ba049419jerenkrantz{
2e75499d05677dd589f7ce6b5139ae15ba049419jerenkrantz const char *pos = *line;
2e75499d05677dd589f7ce6b5139ae15ba049419jerenkrantz int len;
2e75499d05677dd589f7ce6b5139ae15ba049419jerenkrantz char *res;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb while ((*pos != stop) && *pos) {
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb ++pos;
742318b93e89c311f66b55f426c4d9cf2c14628bjim }
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb len = pos - *line;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb res = (char *)apr_palloc(atrans, len + 1);
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb memcpy(res, *line, len);
561e5f16a2f9fb397aac4c283aaa87a752520a4ddougm res[len] = 0;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb if (stop) {
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb while (*pos == stop) {
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb ++pos;
742318b93e89c311f66b55f426c4d9cf2c14628bjim }
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb }
2fc50921b88defeb7127985dfe4b4130175e069ejwoolley *line = pos;
2fc50921b88defeb7127985dfe4b4130175e069ejwoolley
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb return res;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb}
2fc50921b88defeb7127985dfe4b4130175e069ejwoolley
2fc50921b88defeb7127985dfe4b4130175e069ejwoolleyAP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
2fc50921b88defeb7127985dfe4b4130175e069ejwoolley{
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb return ap_getword_white(atrans, (const char **) line);
c9a95767fbf0f5fb0976a06b97a256033925e433rbb}
c9a95767fbf0f5fb0976a06b97a256033925e433rbb
c9a95767fbf0f5fb0976a06b97a256033925e433rbbAP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
c9a95767fbf0f5fb0976a06b97a256033925e433rbb{
c9a95767fbf0f5fb0976a06b97a256033925e433rbb int pos = -1, x;
c9a95767fbf0f5fb0976a06b97a256033925e433rbb char *res;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh for (x = 0; (*line)[x]; x++) {
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh if (apr_isspace((*line)[x])) {
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh pos = x;
c9a95767fbf0f5fb0976a06b97a256033925e433rbb break;
3311183b8638d2ba2ff8496cad5e01c27d329a05nd }
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh }
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh if (pos == -1) {
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh res = apr_pstrdup(atrans, *line);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh *line += strlen(*line);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh return res;
3311183b8638d2ba2ff8496cad5e01c27d329a05nd }
f5abdb59b6579717b5de18de9de340f4b67d6f26niq
f5abdb59b6579717b5de18de9de340f4b67d6f26niq res = apr_palloc(atrans, pos + 1);
f5abdb59b6579717b5de18de9de340f4b67d6f26niq apr_cpystrn(res, *line, pos + 1);
f5abdb59b6579717b5de18de9de340f4b67d6f26niq
3347ef7496eff25aaca86b2852a14bab667d0e34niq while (apr_isspace((*line)[pos]))
f5abdb59b6579717b5de18de9de340f4b67d6f26niq ++pos;
3347ef7496eff25aaca86b2852a14bab667d0e34niq
c2213b3a46a2666e2e7606ceec509cc4978f187fminfrin *line += pos;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return res;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
11e076839c8d5a82d55e710194d0daac51390dbdsfAP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line, char stop)
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh{
return ap_getword_nulls(atrans, (const char **) line, stop);
}
AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line, char stop)
{
const char *pos = ap_strchr_c(*line, stop);
char *res;
if (!pos) {
res = apr_pstrdup(atrans, *line);
*line += strlen(*line);
return res;
}
res = apr_pstrndup(atrans, *line, pos - *line);
++pos;
*line = pos;
return res;
}
/* Get a word, (new) config-file style --- quoted strings and backslashes
* all honored
*/
static char *substring_conf(apr_pool_t *p, const char *start, int len, char quote)
{
char *result = apr_palloc(p, len + 2);
char *resp = result;
int i;
for (i = 0; i < len; ++i) {
if (start[i] == '\\' && (start[i + 1] == '\\'
|| (quote && start[i + 1] == quote)))
*resp++ = start[++i];
else
*resp++ = start[i];
}
*resp++ = '\0';
#if RESOLVE_ENV_PER_TOKEN
return ap_resolve_env(p,result);
#else
return result;
#endif
}
AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
{
return ap_getword_conf(p, (const char **) line);
}
AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
{
const char *str = *line, *strend;
char *res;
char quote;
while (*str && apr_isspace(*str))
++str;
if (!*str) {
*line = str;
return "";
}
if ((quote = *str) == '"' || quote == '\'') {
strend = str + 1;
while (*strend && *strend != quote) {
if (*strend == '\\' && strend[1] && strend[1] == quote)
strend += 2;
else
++strend;
}
res = substring_conf(p, str + 1, strend - str - 1, quote);
if (*strend == quote)
++strend;
}
else {
strend = str;
while (*strend && !apr_isspace(*strend))
++strend;
res = substring_conf(p, str, strend - str, 0);
}
while (*strend && apr_isspace(*strend))
++strend;
*line = strend;
return res;
}
/* Check a string for any ${ENV} environment variable
* construct and replace each them by the value of
* that environment variable, if it exists. If the
* environment value does not exist, leave the ${ENV}
* construct alone; it means something else.
*/
AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
{
char tmp[ MAX_STRING_LEN ];
const char *s, *e;
tmp[0] = '\0';
if (!(s=ap_strchr_c(word,'$')))
return word;
do {
/* XXX - relies on strncat() to add '\0'
*/
strncat(tmp,word,s - word);
if ((s[1] == '{') && (e=ap_strchr_c(s,'}'))) {
const char *e2 = e;
word = e + 1;
e = getenv(s+2);
if (e) {
strcat(tmp,e);
} else {
strncat(tmp, s, e2-s);
strcat(tmp,"}");
}
} else {
/* ignore invalid strings */
word = s+1;
strcat(tmp,"$");
};
} while ((s=ap_strchr_c(word,'$')));
strcat(tmp,word);
return apr_pstrdup(p,tmp);
}
AP_DECLARE(int) ap_cfg_closefile(ap_configfile_t *cfp)
{
#ifdef DEBUG
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
"Done with config file %s", cfp->name);
#endif
return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
}
static apr_status_t cfg_close(void *param)
{
apr_file_t *cfp = (apr_file_t *) param;
return (apr_file_close(cfp));
}
static int cfg_getch(void *param)
{
char ch;
apr_file_t *cfp = (apr_file_t *) param;
if (apr_file_getc(&ch, cfp) == APR_SUCCESS)
return ch;
return (int)EOF;
}
static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
{
apr_file_t *cfp = (apr_file_t *) param;
apr_status_t rv;
rv = apr_file_gets(buf, bufsiz, cfp);
if (rv == APR_SUCCESS || (rv == APR_EOF && strcmp(buf, "")))
return buf;
return NULL;
}
/* Open a ap_configfile_t as FILE, return open ap_configfile_t struct pointer */
AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg, apr_pool_t *p, const char *name)
{
ap_configfile_t *new_cfg;
apr_file_t *file = NULL;
apr_finfo_t finfo;
apr_status_t status;
#ifdef DEBUG
char buf[120];
#endif
if (name == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
"Internal error: pcfg_openfile() called with NULL filename");
return APR_EBADF;
}
status = apr_file_open(&file, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
#ifdef DEBUG
ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL,
"Opening config file %s (%s)",
name, (status != APR_SUCCESS) ?
apr_strerror(status, buf, sizeof(buf)) : "successful");
#endif
if (status != APR_SUCCESS)
return status;
status = apr_file_info_get(&finfo, APR_FINFO_TYPE, file);
if (status != APR_SUCCESS)
return status;
if (finfo.filetype != APR_REG &&
#if defined(WIN32) || defined(OS2) || defined(NETWARE)
strcasecmp(apr_filename_of_pathname(name), "nul") != 0) {
#else
strcmp(name, "/dev/null") != 0) {
#endif /* WIN32 || OS2 */
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
"Access to file %s denied by server: not a regular file",
name);
apr_file_close(file);
return APR_EBADF;
}
new_cfg = apr_palloc(p, sizeof(*new_cfg));
new_cfg->param = file;
new_cfg->name = apr_pstrdup(p, name);
new_cfg->getch = (int (*)(void *)) cfg_getch;
new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
new_cfg->close = (int (*)(void *)) cfg_close;
new_cfg->line_number = 0;
*ret_cfg = new_cfg;
return APR_SUCCESS;
}
/* Allocate a ap_configfile_t handle with user defined functions and params */
AP_DECLARE(ap_configfile_t *) ap_pcfg_open_custom(apr_pool_t *p, const char *descr,
void *param,
int(*getch)(void *param),
void *(*getstr) (void *buf, size_t bufsiz, void *param),
int(*close_func)(void *param))
{
ap_configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
#ifdef DEBUG
ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL, "Opening config handler %s", descr);
#endif
new_cfg->param = param;
new_cfg->name = descr;
new_cfg->getch = getch;
new_cfg->getstr = getstr;
new_cfg->close = close_func;
new_cfg->line_number = 0;
return new_cfg;
}
/* Read one character from a configfile_t */
AP_DECLARE(int) ap_cfg_getc(ap_configfile_t *cfp)
{
register int ch = cfp->getch(cfp->param);
if (ch == LF)
++cfp->line_number;
return ch;
}
/* Read one line from open ap_configfile_t, strip LF, increase line number */
/* If custom handler does not define a getstr() function, read char by char */
AP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, ap_configfile_t *cfp)
{
/* If a "get string" function is defined, use it */
if (cfp->getstr != NULL) {
char *src, *dst;
char *cp;
char *cbuf = buf;
size_t cbufsize = bufsize;
while (1) {
++cfp->line_number;
if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
return 1;
/*
* check for line continuation,
* i.e. match [^\\]\\[\r]\n only
*/
cp = cbuf;
while (cp < cbuf+cbufsize && *cp != '\0')
cp++;
if (cp > cbuf && cp[-1] == LF) {
cp--;
if (cp > cbuf && cp[-1] == CR)
cp--;
if (cp > cbuf && cp[-1] == '\\') {
cp--;
if (!(cp > cbuf && cp[-1] == '\\')) {
/*
* line continuation requested -
* then remove backslash and continue
*/
cbufsize -= (cp-cbuf);
cbuf = cp;
continue;
}
else {
/*
* no real continuation because escaped -
* then just remove escape character
*/
for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
cp[0] = cp[1];
}
}
}
break;
}
/*
* Leading and trailing white space is eliminated completely
*/
src = buf;
while (apr_isspace(*src))
++src;
/* blast trailing whitespace */
dst = &src[strlen(src)];
while (--dst >= src && apr_isspace(*dst))
*dst = '\0';
/* Zap leading whitespace by shifting */
if (src != buf)
for (dst = buf; (*dst++ = *src++) != '\0'; )
;
#ifdef DEBUG_CFG_LINES
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
#endif
return 0;
} else {
/* No "get string" function defined; read character by character */
register int c;
register size_t i = 0;
buf[0] = '\0';
/* skip leading whitespace */
do {
c = cfp->getch(cfp->param);
} while (c == '\t' || c == ' ');
if (c == EOF)
return 1;
if(bufsize < 2) {
/* too small, assume caller is crazy */
return 1;
}
while (1) {
if ((c == '\t') || (c == ' ')) {
buf[i++] = ' ';
while ((c == '\t') || (c == ' '))
c = cfp->getch(cfp->param);
}
if (c == CR) {
/* silently ignore CR (_assume_ that a LF follows) */
c = cfp->getch(cfp->param);
}
if (c == LF) {
/* increase line number and return on LF */
++cfp->line_number;
}
if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
/*
* check for line continuation
*/
if (i > 0 && buf[i-1] == '\\') {
i--;
if (!(i > 0 && buf[i-1] == '\\')) {
/* line is continued */
c = cfp->getch(cfp->param);
continue;
}
/* else nothing needs be done because
* then the backslash is escaped and
* we just strip to a single one
*/
}
/* blast trailing whitespace */
while (i > 0 && apr_isspace(buf[i - 1]))
--i;
buf[i] = '\0';
#ifdef DEBUG_CFG_LINES
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
#endif
return 0;
}
buf[i] = c;
++i;
c = cfp->getch(cfp->param);
}
}
}
/* Size an HTTP header field list item, as separated by a comma.
* The return value is a pointer to the beginning of the non-empty list item
* within the original string (or NULL if there is none) and the address
* of field is shifted to the next non-comma, non-whitespace character.
* len is the length of the item excluding any beginning whitespace.
*/
AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
{
const unsigned char *ptr = (const unsigned char *)*field;
const unsigned char *token;
int in_qpair, in_qstr, in_com;
/* Find first non-comma, non-whitespace byte */
while (*ptr == ',' || apr_isspace(*ptr))
++ptr;
token = ptr;
/* Find the end of this item, skipping over dead bits */
for (in_qpair = in_qstr = in_com = 0;
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
++ptr) {
if (in_qpair) {
in_qpair = 0;
}
else {
switch (*ptr) {
case '\\': in_qpair = 1; /* quoted-pair */
break;
case '"' : if (!in_com) /* quoted string delim */
in_qstr = !in_qstr;
break;
case '(' : if (!in_qstr) /* comment (may nest) */
++in_com;
break;
case ')' : if (in_com) /* end comment */
--in_com;
break;
default : break;
}
}
}
if ((*len = (ptr - token)) == 0) {
*field = (const char *)ptr;
return NULL;
}
/* Advance field pointer to the next non-comma, non-white byte */
while (*ptr == ',' || apr_isspace(*ptr))
++ptr;
*field = (const char *)ptr;
return (const char *)token;
}
/* Retrieve an HTTP header field list item, as separated by a comma,
* while stripping insignificant whitespace and lowercasing anything not in
* a quoted string or comment. The return value is a new string containing
* the converted list item (or NULL if none) and the address pointed to by
* field is shifted to the next non-comma, non-whitespace.
*/
AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
{
const char *tok_start;
const unsigned char *ptr;
unsigned char *pos;
char *token;
int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
/* Find the beginning and maximum length of the list item so that
* we can allocate a buffer for the new string and reset the field.
*/
if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
return NULL;
}
token = apr_palloc(p, tok_len + 1);
/* Scan the token again, but this time copy only the good bytes.
* We skip extra whitespace and any whitespace around a '=', '/',
* or ';' and lowercase normal characters not within a comment,
* quoted-string or quoted-pair.
*/
for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
++ptr) {
if (in_qpair) {
in_qpair = 0;
*pos++ = *ptr;
}
else {
switch (*ptr) {
case '\\': in_qpair = 1;
if (addspace == 1)
*pos++ = ' ';
*pos++ = *ptr;
addspace = 0;
break;
case '"' : if (!in_com)
in_qstr = !in_qstr;
if (addspace == 1)
*pos++ = ' ';
*pos++ = *ptr;
addspace = 0;
break;
case '(' : if (!in_qstr)
++in_com;
if (addspace == 1)
*pos++ = ' ';
*pos++ = *ptr;
addspace = 0;
break;
case ')' : if (in_com)
--in_com;
*pos++ = *ptr;
addspace = 0;
break;
case ' ' :
case '\t': if (addspace)
break;
if (in_com || in_qstr)
*pos++ = *ptr;
else
addspace = 1;
break;
case '=' :
case '/' :
case ';' : if (!(in_com || in_qstr))
addspace = -1;
*pos++ = *ptr;
break;
default : if (addspace == 1)
*pos++ = ' ';
*pos++ = (in_com || in_qstr) ? *ptr
: apr_tolower(*ptr);
addspace = 0;
break;
}
}
}
*pos = '\0';
return token;
}
/* Find an item in canonical form (lowercase, no extra spaces) within
* an HTTP field value list. Returns 1 if found, 0 if not found.
* This would be much more efficient if we stored header fields as
* an array of list items as they are received instead of a plain string.
*/
AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line, const char *tok)
{
const unsigned char *pos;
const unsigned char *ptr = (const unsigned char *)line;
int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
if (!line || !tok)
return 0;
do { /* loop for each item in line's list */
/* Find first non-comma, non-whitespace byte */
while (*ptr == ',' || apr_isspace(*ptr))
++ptr;
if (*ptr)
good = 1; /* until proven otherwise for this item */
else
break; /* no items left and nothing good found */
/* We skip extra whitespace and any whitespace around a '=', '/',
* or ';' and lowercase normal characters not within a comment,
* quoted-string or quoted-pair.
*/
for (pos = (const unsigned char *)tok;
*ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
++ptr) {
if (in_qpair) {
in_qpair = 0;
if (good)
good = (*pos++ == *ptr);
}
else {
switch (*ptr) {
case '\\': in_qpair = 1;
if (addspace == 1)
good = good && (*pos++ == ' ');
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case '"' : if (!in_com)
in_qstr = !in_qstr;
if (addspace == 1)
good = good && (*pos++ == ' ');
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case '(' : if (!in_qstr)
++in_com;
if (addspace == 1)
good = good && (*pos++ == ' ');
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case ')' : if (in_com)
--in_com;
good = good && (*pos++ == *ptr);
addspace = 0;
break;
case ' ' :
case '\t': if (addspace || !good)
break;
if (in_com || in_qstr)
good = (*pos++ == *ptr);
else
addspace = 1;
break;
case '=' :
case '/' :
case ';' : if (!(in_com || in_qstr))
addspace = -1;
good = good && (*pos++ == *ptr);
break;
default : if (!good)
break;
if (addspace == 1)
good = (*pos++ == ' ');
if (in_com || in_qstr)
good = good && (*pos++ == *ptr);
else
good = good && (*pos++ == apr_tolower(*ptr));
addspace = 0;
break;
}
}
}
if (good && *pos)
good = 0; /* not good if only a prefix was matched */
} while (*ptr && !good);
return good;
}
/* Retrieve a token, spacing over it and returning a pointer to
* the first non-white byte afterwards. Note that these tokens
* are delimited by semis and commas; and can also be delimited
* by whitespace at the caller's option.
*/
AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line, int accept_white)
{
const char *ptr = *accept_line;
const char *tok_start;
char *token;
int tok_len;
/* Find first non-white byte */
while (*ptr && apr_isspace(*ptr))
++ptr;
tok_start = ptr;
/* find token end, skipping over quoted strings.
* (comments are already gone).
*/
while (*ptr && (accept_white || !apr_isspace(*ptr))
&& *ptr != ';' && *ptr != ',') {
if (*ptr++ == '"')
while (*ptr)
if (*ptr++ == '"')
break;
}
tok_len = ptr - tok_start;
token = apr_pstrndup(p, tok_start, tok_len);
/* Advance accept_line pointer to the next non-white byte */
while (*ptr && apr_isspace(*ptr))
++ptr;
*accept_line = ptr;
return token;
}
/* find http tokens, see the definition of token from RFC2068 */
AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
{
const unsigned char *start_token;
const unsigned char *s;
if (!line)
return 0;
s = (const unsigned char *)line;
for (;;) {
/* find start of token, skip all stop characters, note NUL
* isn't a token stop, so we don't need to test for it
*/
while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
++s;
}
if (!*s) {
return 0;
}
start_token = s;
/* find end of the token */
while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
++s;
}
if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
return 1;
}
if (!*s) {
return 0;
}
}
}
AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line, const char *tok)
{
int llen, tlen, lidx;
if (!line)
return 0;
llen = strlen(line);
tlen = strlen(tok);
lidx = llen - tlen;
if ((lidx < 0) ||
((lidx > 0) && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
return 0;
return (strncasecmp(&line[lidx], tok, tlen) == 0);
}
AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
{
char *cmd;
unsigned char *d;
const unsigned char *s;
cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
for (; *s; ++s) {
#if defined(OS2) || defined(WIN32)
/* Don't allow '&' in parameters under OS/2. */
/* This can be used to send commands to the shell. */
if (*s == '&') {
*d++ = ' ';
continue;
}
#endif
if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
*d++ = '\\';
}
*d++ = *s;
}
*d = '\0';
return cmd;
}
static char x2c(const char *what)
{
register char digit;
#if !APR_CHARSET_EBCDIC
digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
#else /*APR_CHARSET_EBCDIC*/
char xstr[5];
xstr[0]='0';
xstr[1]='x';
xstr[2]=what[0];
xstr[3]=what[1];
xstr[4]='\0';
digit = apr_xlate_conv_byte(ap_hdrs_from_ascii, 0xFF & strtol(xstr, NULL, 16));
#endif /*APR_CHARSET_EBCDIC*/
return (digit);
}
/*
* Unescapes a URL.
* Returns 0 on success, non-zero on error
* Failure is due to
* bad % escape returns HTTP_BAD_REQUEST
*
* decoding %00 -> \0
* decoding %2f -> / (a special character)
* returns HTTP_NOT_FOUND
*/
AP_DECLARE(int) ap_unescape_url(char *url)
{
register int badesc, badpath;
char *x, *y;
badesc = 0;
badpath = 0;
/* Initial scan for first '%'. Don't bother writing values before
* seeing a '%' */
y = strchr(url, '%');
if (y == NULL) {
return OK;
}
for (x = y; *y; ++x, ++y) {
if (*y != '%')
*x = *y;
else {
if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
badesc = 1;
*x = '%';
}
else {
*x = x2c(y + 1);
y += 2;
if (*x == '/' || *x == '\0')
badpath = 1;
}
}
}
*x = '\0';
if (badesc)
return HTTP_BAD_REQUEST;
else if (badpath)
return HTTP_NOT_FOUND;
else
return OK;
}
AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
apr_port_t port, const request_rec *r)
{
if (ap_is_default_port(port, r))
return apr_pstrdup(p, hostname);
else {
return apr_psprintf(p, "%s:%u", hostname, port);
}
}
/* c2x takes an unsigned, and expects the caller has guaranteed that
* 0 <= what < 256... which usually means that you have to cast to
* unsigned char first, because (unsigned)(char)(x) first goes through
* signed extension to an int before the unsigned cast.
*
* The reason for this assumption is to assist gcc code generation --
* the unsigned char -> unsigned extension is already done earlier in
* both uses of this code, so there's no need to waste time doing it
* again.
*/
static const char c2x_table[] = "0123456789abcdef";
static APR_INLINE unsigned char *c2x(unsigned what, unsigned char *where)
{
#if APR_CHARSET_EBCDIC
what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
#endif /*APR_CHARSET_EBCDIC*/
*where++ = '%';
*where++ = c2x_table[what >> 4];
*where++ = c2x_table[what & 0xf];
return where;
}
/*
* escape_path_segment() escapes a path segment, as defined in RFC 1808. This
* routine is (should be) OS independent.
*
* os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
* cases if a ':' occurs before the first '/' in the URL, the URL should be
* prefixed with "./" (or the ':' escaped). In the case of Unix, this means
* leaving '/' alone, but otherwise doing what escape_path_segment() does. For
* efficiency reasons, we don't use escape_path_segment(), which is provided for
* reference. Again, RFC 1808 is where this stuff is defined.
*
* If partial is set, os_escape_path() assumes that the path will be appended to
* something with a '/' in it (and thus does not prefix "./").
*/
AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
{
char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
const unsigned char *s = (const unsigned char *)segment;
unsigned char *d = (unsigned char *)copy;
unsigned c;
while ((c = *s)) {
if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
d = c2x(c, d);
}
else {
*d++ = c;
}
++s;
}
*d = '\0';
return copy;
}
AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
{
char *copy = apr_palloc(p, 3 * strlen(path) + 3);
const unsigned char *s = (const unsigned char *)path;
unsigned char *d = (unsigned char *)copy;
unsigned c;
if (!partial) {
const char *colon = ap_strchr_c(path, ':');
const char *slash = ap_strchr_c(path, '/');
if (colon && (!slash || colon < slash)) {
*d++ = '.';
*d++ = '/';
}
}
while ((c = *s)) {
if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
d = c2x(c, d);
}
else {
*d++ = c;
}
++s;
}
*d = '\0';
return copy;
}
/* ap_escape_uri is now a macro for os_escape_path */
AP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
{
int i, j;
char *x;
/* first, count the number of extra characters */
for (i = 0, j = 0; s[i] != '\0'; i++)
if (s[i] == '<' || s[i] == '>')
j += 3;
else if (s[i] == '&')
j += 4;
if (j == 0)
return apr_pstrmemdup(p, s, i);
x = apr_palloc(p, i + j + 1);
for (i = 0, j = 0; s[i] != '\0'; i++, j++)
if (s[i] == '<') {
memcpy(&x[j], "&lt;", 4);
j += 3;
}
else if (s[i] == '>') {
memcpy(&x[j], "&gt;", 4);
j += 3;
}
else if (s[i] == '&') {
memcpy(&x[j], "&amp;", 5);
j += 4;
}
else
x[j] = s[i];
x[j] = '\0';
return x;
}
AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
{
apr_finfo_t finfo;
if (apr_stat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
return 0; /* in error condition, just return no */
return (finfo.filetype == APR_DIR);
}
AP_DECLARE(int) ap_is_rdirectory(apr_pool_t *p, const char *path)
{
apr_finfo_t finfo;
if (apr_lstat(&finfo, path, APR_FINFO_TYPE, p) != APR_SUCCESS)
return 0; /* in error condition, just return no */
return (finfo.filetype == APR_DIR);
}
AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
const char *src2)
{
apr_size_t len1, len2;
char *path;
len1 = strlen(src1);
len2 = strlen(src2);
/* allocate +3 for '/' delimiter, trailing NULL and overallocate
* one extra byte to allow the caller to add a trailing '/'
*/
path = (char *)apr_palloc(a, len1 + len2 + 3);
if (len1 == 0) {
*path = '/';
memcpy(path + 1, src2, len2 + 1);
}
else {
char *next;
memcpy(path, src1, len1);
next = path + len1;
if (next[-1] != '/') {
*next++ = '/';
}
memcpy(next, src2, len2 + 1);
}
return path;
}
/*
* Check for an absoluteURI syntax (see section 3.2 in RFC2068).
*/
AP_DECLARE(int) ap_is_url(const char *u)
{
register int x;
for (x = 0; u[x] != ':'; x++) {
if ((!u[x]) ||
((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
(u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
return 0;
}
}
return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
}
AP_DECLARE(int) ap_ind(const char *s, char c)
{
const char *p = ap_strchr_c(s, c);
if (p == NULL)
return -1;
return p - s;
}
AP_DECLARE(int) ap_rind(const char *s, char c)
{
const char *p = ap_strrchr_c(s, c);
if (p == NULL)
return -1;
return p - s;
}
AP_DECLARE(void) ap_str_tolower(char *str)
{
while (*str) {
*str = apr_tolower(*str);
++str;
}
}
static char *find_fqdn(apr_pool_t *a, struct hostent *p)
{
int x;
if (!strchr(p->h_name, '.')) {
for (x = 0; p->h_aliases[x]; ++x) {
if (strchr(p->h_aliases[x], '.') &&
(!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
return apr_pstrdup(a, p->h_aliases[x]);
}
return NULL;
}
return apr_pstrdup(a, (void *) p->h_name);
}
char *ap_get_local_host(apr_pool_t *a)
{
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif
char str[MAXHOSTNAMELEN + 1];
char *server_hostname = NULL;
struct hostent *p;
#ifdef BEOS_R5
if (gethostname(str, sizeof(str) - 1) == 0)
#else
if (gethostname(str, sizeof(str) - 1) != 0)
#endif
{
ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, a,
"%s: gethostname() failed to determine ServerName",
ap_server_argv0);
}
else
{
str[sizeof(str) - 1] = '\0';
if ((!(p = gethostbyname(str)))
|| (!(server_hostname = find_fqdn(a, p)))) {
/* Recovery - return the default servername by IP: */
if (p && p->h_addr_list[0]) {
apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
server_hostname = apr_pstrdup(a, str);
/* We will drop through to report the IP-named server */
}
}
else {
/* Since we found a fdqn, return it with no logged message. */
return server_hostname;
}
}
if (!server_hostname)
server_hostname = apr_pstrdup(a, "127.0.0.1");
ap_log_perror(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO|APLOG_STARTUP, 0, a,
"%s: Could not determine the server's fully qualified "
"domain name, using %s for ServerName",
ap_server_argv0, server_hostname);
return server_hostname;
}
/* simple 'pool' alloc()ing glue to apr_base64.c
*/
AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
{
char *decoded;
int l;
decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded));
l = apr_base64_decode(decoded, bufcoded);
decoded[l] = '\0'; /* make binary sequence into string */
return decoded;
}
AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
{
char *encoded;
int l = strlen(string);
encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l));
l = apr_base64_encode(encoded, string, l);
encoded[l] = '\0'; /* make binary sequence into string */
return encoded;
}
/* we want to downcase the type/subtype for comparison purposes
* but nothing else because ;parameter=foo values are case sensitive.
* XXX: in truth we want to downcase parameter names... but really,
* apache has never handled parameters and such correctly. You
* also need to compress spaces and such to be able to compare
* properly. -djg
*/
AP_DECLARE(void) ap_content_type_tolower(char *str)
{
char *semi;
semi = strchr(str, ';');
if (semi) {
*semi = '\0';
}
while (*str) {
*str = apr_tolower(*str);
++str;
}
if (semi) {
*semi = ';';
}
}
/*
* Given a string, replace any bare " with \" .
*/
AP_DECLARE(char *) ap_escape_quotes (apr_pool_t *p, const char *instring)
{
int newlen = 0;
const char *inchr = instring;
char *outchr, *outstring;
/*
* Look through the input string, jogging the length of the output
* string up by an extra byte each time we find an unescaped ".
*/
while (*inchr != '\0') {
newlen++;
if (*inchr == '"') {
newlen++;
}
/*
* If we find a slosh, and it's not the last byte in the string,
* it's escaping something - advance past both bytes.
*/
if ((*inchr == '\\') && (inchr[1] != '\0')) {
inchr++;
newlen++;
}
inchr++;
}
outstring = apr_palloc(p, newlen + 1);
inchr = instring;
outchr = outstring;
/*
* Now copy the input string to the output string, inserting a slosh
* in front of every " that doesn't already have one.
*/
while (*inchr != '\0') {
if ((*inchr == '\\') && (inchr[1] != '\0')) {
*outchr++ = *inchr++;
*outchr++ = *inchr++;
}
if (*inchr == '"') {
*outchr++ = '\\';
}
if (*inchr != '\0') {
*outchr++ = *inchr++;
}
}
*outchr = '\0';
return outstring;
}