util.c revision 42df154a9b4c440ec42879d9e215be1b0468f47d
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein/* ====================================================================
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * The Apache Software License, Version 1.1
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b99dbaab171d91e1b664397cc40e039d0c087c65fielding * Copyright (c) 2000 The Apache Software Foundation. All rights
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * reserved.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * Redistribution and use in source and binary forms, with or without
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * modification, are permitted provided that the following conditions
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * are met:
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 1. Redistributions of source code must retain the above copyright
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * notice, this list of conditions and the following disclaimer.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 2. Redistributions in binary form must reproduce the above copyright
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * notice, this list of conditions and the following disclaimer in
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * the documentation and/or other materials provided with the
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * distribution.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 3. The end-user documentation included with the redistribution,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * if any, must include the following acknowledgment:
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * "This product includes software developed by the
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * Apache Software Foundation (http://www.apache.org/)."
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * Alternately, this acknowledgment may appear in the software itself,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * if and wherever such third-party acknowledgments normally appear.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 4. The names "Apache" and "Apache Software Foundation" must
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * not be used to endorse or promote products derived from this
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * software without prior written permission. For written
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * permission, please contact apache@apache.org.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 5. Products derived from this software may not be called "Apache",
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * nor may "Apache" appear in their name, without prior written
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * permission of the Apache Software Foundation.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * SUCH DAMAGE.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * ====================================================================
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * This software consists of voluntary contributions made by many
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * individuals on behalf of the Apache Software Foundation. For more
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * information on the Apache Software Foundation, please see
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * <http://www.apache.org/>.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Portions of this software are based upon public domain software
f4c310fd2555c6faca1f980f00b161eadb089023gstein * originally written at the National Center for Supercomputing Applications,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * University of Illinois, Urbana-Champaign.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * util.c: string utility things
952023a04a2f9d51553babd9094fb857f1c97548trawick *
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * 3/21/93 Rob McCool
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * 1995-96 Many changes by the Apache Software Foundation
1b21d7b3d97def358b2e923655edeb16613a1c31gstein *
1b21d7b3d97def358b2e923655edeb16613a1c31gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Debugging aid:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
f4c310fd2555c6faca1f980f00b161eadb089023gstein * #define DEBUG_CFG_LINES to trace every line read from the config files
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#define CORE_PRIVATE
f4c310fd2555c6faca1f980f00b161eadb089023gstein
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "apr.h"
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "apr_strings.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if APR_HAVE_STDIO_H
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#include <stdio.h> /* for EOF */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if APR_HAVE_NETINET_IN_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <netinet/in.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if APR_HAVE_SYS_SOCKET_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <sys/socket.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include "ap_config.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include "ap_base64.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include "httpd.h"
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "http_main.h"
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "http_log.h"
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "http_protocol.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include "http_config.h"
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#include "util_ebcdic.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_UNISTD_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <unistd.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_NETDB_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <netdb.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_ARPA_INET_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <arpa/inet.h>
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#endif
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#ifdef HAVE_PWD_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <pwd.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_GRP_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <grp.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_STRINGS_H
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#include <strings.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* A bunch of functions in util.c scan strings looking for certain characters.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * To make that more efficient we encode a lookup table. The test_char_table
f4c310fd2555c6faca1f980f00b161eadb089023gstein * is generated automatically by gen_test_char.c.
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe */
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "test_char.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* we assume the folks using this ensure 0 <= c < 256... which means
f4c310fd2555c6faca1f980f00b161eadb089023gstein * you need a cast to (unsigned char) first, you can't just plug a
f4c310fd2555c6faca1f980f00b161eadb089023gstein * char in here and get it to work, because if char is signed then it
f4c310fd2555c6faca1f980f00b161eadb089023gstein * will first be sign extended.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Examine a field value (such as a media-/content-type) string and return
f4c310fd2555c6faca1f980f00b161eadb089023gstein * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *semi;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (intype == NULL) return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein semi = ap_strchr_c(intype, ';');
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (semi == NULL) {
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe return apr_pstrdup(p, intype);
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein while ((semi > intype) && apr_isspace(semi[-1])) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein semi--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein return apr_pstrndup(p, intype, semi - intype);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe}
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt, int gmt)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_size_t retcode;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char ts[MAX_STRING_LEN];
f4c310fd2555c6faca1f980f00b161eadb089023gstein char tf[MAX_STRING_LEN];
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_exploded_time_t xt;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (gmt) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *f;
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe char *strp;
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_explode_gmt(&xt, t);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Convert %Z to "GMT" and %z to "+0000";
f4c310fd2555c6faca1f980f00b161eadb089023gstein * on hosts that do not have a time zone string in struct tm,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * strftime must assume its argument is local time.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ; f++, strp++) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*f != '%') continue;
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe switch (f[1]) {
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe case '%':
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe *++strp = *++f;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case 'Z':
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp++ = 'G';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp++ = 'M';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp = 'T';
f4c310fd2555c6faca1f980f00b161eadb089023gstein f++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case 'z': /* common extension */
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp++ = '+';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp++ = '0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp++ = '0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp++ = '0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp = '0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein f++;
48f35e10f195dd594d75738fc536bb885eda537cgstein break;
48f35e10f195dd594d75738fc536bb885eda537cgstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *strp = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein fmt = tf;
91644a5f4d3e992dc208304b50e80bbb236fca89trawick }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_explode_localtime(&xt, t);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* check return code? */
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ts[MAX_STRING_LEN - 1] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein return apr_pstrdup(p, ts);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Roy owes Rob beer. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Rob owes Roy dinner. */
48f35e10f195dd594d75738fc536bb885eda537cgstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* These legacy comments would make a lot more sense if Roy hadn't
f4c310fd2555c6faca1f980f00b161eadb089023gstein * replaced the old later_than() routine with util_date.c.
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Well, okay, they still wouldn't make any sense.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
48f35e10f195dd594d75738fc536bb885eda537cgstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Match = 0, NoMatch = 1, Abort = -1
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Based loosely on sections of wildmat.c by Rich Salz
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Hmmm... shouldn't this really go component by component?
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_strcmp_match(const char *str, const char *exp)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int x, y;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
48f35e10f195dd594d75738fc536bb885eda537cgstein for (x = 0, y = 0; exp[y]; ++y, ++x) {
48f35e10f195dd594d75738fc536bb885eda537cgstein if ((!str[x]) && (exp[y] != '*'))
48f35e10f195dd594d75738fc536bb885eda537cgstein return -1;
48f35e10f195dd594d75738fc536bb885eda537cgstein if (exp[y] == '*') {
48f35e10f195dd594d75738fc536bb885eda537cgstein while (exp[++y] == '*');
48f35e10f195dd594d75738fc536bb885eda537cgstein if (!exp[y])
48f35e10f195dd594d75738fc536bb885eda537cgstein return 0;
48f35e10f195dd594d75738fc536bb885eda537cgstein while (str[x]) {
48f35e10f195dd594d75738fc536bb885eda537cgstein int ret;
48f35e10f195dd594d75738fc536bb885eda537cgstein if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
48f35e10f195dd594d75738fc536bb885eda537cgstein return ret;
48f35e10f195dd594d75738fc536bb885eda537cgstein }
48f35e10f195dd594d75738fc536bb885eda537cgstein return -1;
48f35e10f195dd594d75738fc536bb885eda537cgstein }
48f35e10f195dd594d75738fc536bb885eda537cgstein else if ((exp[y] != '?') && (str[x] != exp[y]))
48f35e10f195dd594d75738fc536bb885eda537cgstein return 1;
48f35e10f195dd594d75738fc536bb885eda537cgstein }
48f35e10f195dd594d75738fc536bb885eda537cgstein return (str[x] != '\0');
48f35e10f195dd594d75738fc536bb885eda537cgstein}
48f35e10f195dd594d75738fc536bb885eda537cgstein
48f35e10f195dd594d75738fc536bb885eda537cgsteinAP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *exp)
48f35e10f195dd594d75738fc536bb885eda537cgstein{
48f35e10f195dd594d75738fc536bb885eda537cgstein int x, y;
48f35e10f195dd594d75738fc536bb885eda537cgstein
48f35e10f195dd594d75738fc536bb885eda537cgstein for (x = 0, y = 0; exp[y]; ++y, ++x) {
48f35e10f195dd594d75738fc536bb885eda537cgstein if ((!str[x]) && (exp[y] != '*'))
48f35e10f195dd594d75738fc536bb885eda537cgstein return -1;
48f35e10f195dd594d75738fc536bb885eda537cgstein if (exp[y] == '*') {
48f35e10f195dd594d75738fc536bb885eda537cgstein while (exp[++y] == '*');
48f35e10f195dd594d75738fc536bb885eda537cgstein if (!exp[y])
48f35e10f195dd594d75738fc536bb885eda537cgstein return 0;
48f35e10f195dd594d75738fc536bb885eda537cgstein while (str[x]) {
48f35e10f195dd594d75738fc536bb885eda537cgstein int ret;
48f35e10f195dd594d75738fc536bb885eda537cgstein if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
48f35e10f195dd594d75738fc536bb885eda537cgstein return ret;
48f35e10f195dd594d75738fc536bb885eda537cgstein }
48f35e10f195dd594d75738fc536bb885eda537cgstein return -1;
48f35e10f195dd594d75738fc536bb885eda537cgstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if ((exp[y] != '?') && (apr_tolower(str[x]) != apr_tolower(exp[y])))
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (str[x] != '\0');
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_is_matchexp(const char *str)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein register int x;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (x = 0; str[x]; x++)
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((str[x] == '*') || (str[x] == '?'))
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
48f35e10f195dd594d75738fc536bb885eda537cgstein}
48f35e10f195dd594d75738fc536bb885eda537cgstein
48f35e10f195dd594d75738fc536bb885eda537cgstein/*
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * Here's a pool-based interface to POSIX regex's regcomp().
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Note that we return regex_t instead of being passed one.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * The reason is that if you use an already-used regex_t structure,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the memory that you've already allocated gets forgotten, and
f4c310fd2555c6faca1f980f00b161eadb089023gstein * regfree() doesn't clear it. So we don't allow it.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic apr_status_t regex_cleanup(void *preg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein regfree((regex_t *) preg);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return APR_SUCCESS;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
f4c310fd2555c6faca1f980f00b161eadb089023gstein int cflags)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein regex_t *preg = apr_palloc(p, sizeof(regex_t));
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (regcomp(preg, pattern, cflags)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_register_cleanup(p, (void *) preg, regex_cleanup, regex_cleanup);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f958dac1550254a59b45f4655138bb34dad5e76egstein return preg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(void) ap_pregfree(apr_pool_t *p, regex_t * reg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein regfree(reg);
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_kill_cleanup(p, (void *) reg, regex_cleanup);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Similar to standard strstr() but we ignore case in this version.
252e2478cb56afb5ca8585b50bc2ffb780d2efb6gstein * Based on the strstr() implementation further below.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
252e2478cb56afb5ca8585b50bc2ffb780d2efb6gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *p1, *p2;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*s2 == '\0') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* an empty s2 */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return((char *)s1);
252e2478cb56afb5ca8585b50bc2ffb780d2efb6gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein while(1) {
252e2478cb56afb5ca8585b50bc2ffb780d2efb6gstein for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*s1 == '\0') return(NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* found first character of s2, see if the rest matches */
252e2478cb56afb5ca8585b50bc2ffb780d2efb6gstein p1 = (char *)s1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein p2 = (char *)s2;
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (apr_tolower(*++p1) == apr_tolower(*++p2)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*p1 == '\0') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* both strings ended together */
f39230a531b23d94f86a087963299bbe2e431a4agstein return((char *)s1);
f39230a531b23d94f86a087963299bbe2e431a4agstein }
f39230a531b23d94f86a087963299bbe2e431a4agstein }
f39230a531b23d94f86a087963299bbe2e431a4agstein if (*p2 == '\0') {
f39230a531b23d94f86a087963299bbe2e431a4agstein /* second string ended, a match */
f39230a531b23d94f86a087963299bbe2e431a4agstein break;
f39230a531b23d94f86a087963299bbe2e431a4agstein }
f39230a531b23d94f86a087963299bbe2e431a4agstein /* didn't find a match here, try starting at next character in s1 */
f39230a531b23d94f86a087963299bbe2e431a4agstein s1++;
f39230a531b23d94f86a087963299bbe2e431a4agstein }
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein return((char *)s1);
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein}
f39230a531b23d94f86a087963299bbe2e431a4agstein
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein/*
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein * Returns an offsetted pointer in bigstring immediately after
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein * prefix. Returns bigstring if bigstring doesn't start with
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein * prefix or if prefix is longer than bigstring while still matching.
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein * NOTE: pointer returned is relative to bigstring, so we
f39230a531b23d94f86a087963299bbe2e431a4agstein * can use standard pointer comparisons in the calling function
f39230a531b23d94f86a087963299bbe2e431a4agstein * (eg: test if ap_stripprefix(a,b) == a)
f39230a531b23d94f86a087963299bbe2e431a4agstein */
f39230a531b23d94f86a087963299bbe2e431a4agsteinAP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein const char *prefix)
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein{
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein const char *p1;
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein if (*prefix == '\0')
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein return bigstring;
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein p1 = bigstring;
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein while (*p1 && *prefix) {
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein if (*p1++ != *prefix++)
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein return bigstring;
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein }
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein if (*prefix == '\0')
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein return p1;
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein /* hit the end of bigstring! */
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein return bigstring;
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein}
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein/*
f39230a531b23d94f86a087963299bbe2e431a4agstein * Apache stub function for the regex libraries regexec() to make sure the
f39230a531b23d94f86a087963299bbe2e431a4agstein * whole regex(3) API is available through the Apache (exported) namespace.
f39230a531b23d94f86a087963299bbe2e431a4agstein * This is especially important for the DSO situations of modules.
f39230a531b23d94f86a087963299bbe2e431a4agstein * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
f39230a531b23d94f86a087963299bbe2e431a4agstein */
f39230a531b23d94f86a087963299bbe2e431a4agsteinAP_DECLARE(int) ap_regexec(regex_t *preg, const char *string,
f39230a531b23d94f86a087963299bbe2e431a4agstein size_t nmatch, regmatch_t pmatch[], int eflags)
f39230a531b23d94f86a087963299bbe2e431a4agstein{
f39230a531b23d94f86a087963299bbe2e431a4agstein return regexec(preg, string, nmatch, pmatch, eflags);
f39230a531b23d94f86a087963299bbe2e431a4agstein}
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agsteinAP_DECLARE(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
f39230a531b23d94f86a087963299bbe2e431a4agstein{
f39230a531b23d94f86a087963299bbe2e431a4agstein return regerror(errcode, preg, errbuf, errbuf_size);
f39230a531b23d94f86a087963299bbe2e431a4agstein}
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein/* This function substitutes for $0-$9, filling in regular expression
f39230a531b23d94f86a087963299bbe2e431a4agstein * submatches. Pass it the same nmatch and pmatch arguments that you
f39230a531b23d94f86a087963299bbe2e431a4agstein * passed ap_regexec(). pmatch should not be greater than the maximum number
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein * of subexpressions - i.e. one more than the re_nsub member of regex_t.
f39230a531b23d94f86a087963299bbe2e431a4agstein *
f39230a531b23d94f86a087963299bbe2e431a4agstein * input should be the string with the $-expressions, source should be the
f39230a531b23d94f86a087963299bbe2e431a4agstein * string that was matched against.
f39230a531b23d94f86a087963299bbe2e431a4agstein *
f39230a531b23d94f86a087963299bbe2e431a4agstein * It returns the substituted string, or NULL on error.
f39230a531b23d94f86a087963299bbe2e431a4agstein *
f39230a531b23d94f86a087963299bbe2e431a4agstein * Parts of this code are based on Henry Spencer's regsub(), from his
f39230a531b23d94f86a087963299bbe2e431a4agstein * AT&T V8 regexp package.
f39230a531b23d94f86a087963299bbe2e431a4agstein */
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agsteinAP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input, const char *source,
f39230a531b23d94f86a087963299bbe2e431a4agstein size_t nmatch, regmatch_t pmatch[])
f39230a531b23d94f86a087963299bbe2e431a4agstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *src = input;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *dest, *dst;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char c;
f4c310fd2555c6faca1f980f00b161eadb089023gstein size_t no;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int len;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!source)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!nmatch)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return apr_pstrdup(p, src);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* First pass, find the size */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein len = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while ((c = *src++) != '\0') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == '&')
f4c310fd2555c6faca1f980f00b161eadb089023gstein no = 0;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm else if (c == '$' && apr_isdigit(*src))
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm no = *src++ - '0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein no = 10;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (no > 9) { /* Ordinary character. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == '\\' && (*src == '$' || *src == '&'))
f4c310fd2555c6faca1f980f00b161eadb089023gstein c = *src++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein len++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein len += pmatch[no].rm_eo - pmatch[no].rm_so;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein dest = dst = apr_pcalloc(p, len + 1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Now actually fill in the string */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein src = input;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while ((c = *src++) != '\0') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == '&')
f4c310fd2555c6faca1f980f00b161eadb089023gstein no = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (c == '$' && apr_isdigit(*src))
f4c310fd2555c6faca1f980f00b161eadb089023gstein no = *src++ - '0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein no = 10;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (no > 9) { /* Ordinary character. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == '\\' && (*src == '$' || *src == '&'))
f4c310fd2555c6faca1f980f00b161eadb089023gstein c = *src++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *dst++ = c;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein len = pmatch[no].rm_eo - pmatch[no].rm_so;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm memcpy(dst, source + pmatch[no].rm_so, len);
f4c310fd2555c6faca1f980f00b161eadb089023gstein dst += len;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm *dst = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return dest;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Parse .. so we don't compromise security
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(void) ap_getparents(char *name)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int l, w;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* Four paseses, as per RFC 1808 */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* a) remove ./ path segments */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (l = 0, w = 0; name[l] != '\0';) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
f4c310fd2555c6faca1f980f00b161eadb089023gstein l += 2;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm name[w++] = name[l++];
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* b) remove trailing . path, segment */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (w == 1 && name[0] == '.')
f4c310fd2555c6faca1f980f00b161eadb089023gstein w--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
f4c310fd2555c6faca1f980f00b161eadb089023gstein w--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein name[w] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* c) remove all xx/../ segments. (including leading ../ and /../) */
f4c310fd2555c6faca1f980f00b161eadb089023gstein l = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (name[l] != '\0') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
f4c310fd2555c6faca1f980f00b161eadb089023gstein (l == 0 || name[l - 1] == '/')) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein register int m = l + 3, n;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein l = l - 2;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (l >= 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (l >= 0 && name[l] != '/')
f4c310fd2555c6faca1f980f00b161eadb089023gstein l--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein l++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein l = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein n = l;
f4c310fd2555c6faca1f980f00b161eadb089023gstein while ((name[n] = name[m]))
f4c310fd2555c6faca1f980f00b161eadb089023gstein (++n, ++m);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++l;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* d) remove trailing xx/.. segment. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (l == 2 && name[0] == '.' && name[1] == '.')
f4c310fd2555c6faca1f980f00b161eadb089023gstein name[0] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein l = l - 4;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (l >= 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (l >= 0 && name[l] != '/')
f4c310fd2555c6faca1f980f00b161eadb089023gstein l--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein l++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein l = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein name[l] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(void) ap_no2slash(char *name)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *d, *s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein s = d = name;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_UNC_PATHS
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Check for UNC names. Leave leading two slashes. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (s[0] == '/' && s[1] == '/')
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d++ = *s++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
26250b0077972bf21b6d8a8d23772a4cf53f9477gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*s) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((*d++ = *s) == '/') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein do {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein } while (*s == '/');
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++s;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * copy at most n leading directories of s into d
f4c310fd2555c6faca1f980f00b161eadb089023gstein * d should be at least as large as s plus 1 extra byte
f4c310fd2555c6faca1f980f00b161eadb089023gstein * assumes n > 0
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the return value is the ever useful pointer to the trailing \0 of d
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * so that if n == 0, "/" is returned in d with n == 1
f4c310fd2555c6faca1f980f00b161eadb089023gstein * and s == "e:/test.html", "e:/" is returned in d
f4c310fd2555c6faca1f980f00b161eadb089023gstein * *** See also directory_walk in src/main/http_request.c
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein * examples:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * /a/b, 0 ==> / (true for all platforms)
f4c310fd2555c6faca1f980f00b161eadb089023gstein * /a/b, 1 ==> /
f4c310fd2555c6faca1f980f00b161eadb089023gstein * /a/b, 2 ==> /a/
f4c310fd2555c6faca1f980f00b161eadb089023gstein * /a/b, 3 ==> /a/b/
f4c310fd2555c6faca1f980f00b161eadb089023gstein * /a/b, 4 ==> /a/b/
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * c:/a/b 0 ==> /
f4c310fd2555c6faca1f980f00b161eadb089023gstein * c:/a/b 1 ==> c:/
f4c310fd2555c6faca1f980f00b161eadb089023gstein * c:/a/b 2 ==> c:/a/
f4c310fd2555c6faca1f980f00b161eadb089023gstein * c:/a/b 3 ==> c:/a/b
f4c310fd2555c6faca1f980f00b161eadb089023gstein * c:/a/b 4 ==> c:/a/b
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (n < 1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d = '/';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *++d = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (d);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (;;) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*s == '\0' || (*s == '/' && (--n) == 0)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d = '/';
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d++ = *s++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *++d = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (d);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * return the parent directory name including trailing / of the file s
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *last_slash = ap_strrchr_c(s, '/');
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *d;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int l;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (last_slash == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* XXX: well this is really broken if this happens */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (apr_pstrdup(p, "/"));
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein l = (last_slash - s) + 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein d = apr_palloc(p, l + 1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein memcpy(d, s, l);
f4c310fd2555c6faca1f980f00b161eadb089023gstein d[l] = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (d);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_count_dirs(const char *path)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein register int x, n;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (x = 0, n = 0; path[x]; x++)
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (path[x] == '/')
f4c310fd2555c6faca1f980f00b161eadb089023gstein n++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return n;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(void) ap_chdir_file(const char *file)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *x;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char buf[HUGE_STRING_LEN];
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein x = ap_strrchr_c(file, '/');
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (x == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein chdir(file);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (x - file < sizeof(buf) - 1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein memcpy(buf, file, x - file);
f4c310fd2555c6faca1f980f00b161eadb089023gstein buf[x - file] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein chdir(buf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* XXX: well, this is a silly function, no method of reporting an
f4c310fd2555c6faca1f980f00b161eadb089023gstein * error... ah well. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein return ap_getword(atrans, (const char **) line, stop);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *pos = ap_strchr_c(*line, stop);
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!pos) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = apr_pstrdup(atrans, *line);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm *line += strlen(*line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = apr_pstrndup(atrans, *line, pos - *line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*pos == stop) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein *line = pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein return ap_getword_white(atrans, (const char **) line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int pos = -1, x;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (x = 0; (*line)[x]; x++) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (apr_isspace((*line)[x])) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein pos = x;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (pos == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = apr_pstrdup(atrans, *line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein *line += strlen(*line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = apr_palloc(atrans, pos + 1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_cpystrn(res, *line, pos + 1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (apr_isspace((*line)[pos]))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein *line += pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line, char stop)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein return ap_getword_nulls(atrans, (const char **) line, stop);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line, char stop)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *pos = ap_strchr_c(*line, stop);
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!pos) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = apr_pstrdup(atrans, *line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein *line += strlen(*line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = apr_pstrndup(atrans, *line, pos - *line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein *line = pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Get a word, (new) config-file style --- quoted strings and backslashes
f4c310fd2555c6faca1f980f00b161eadb089023gstein * all honored
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic char *substring_conf(apr_pool_t *p, const char *start, int len, char quote)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *result = apr_palloc(p, len + 2);
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *resp = result;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < len; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (start[i] == '\\' && (start[i + 1] == '\\'
f4c310fd2555c6faca1f980f00b161eadb089023gstein || (quote && start[i + 1] == quote)))
f4c310fd2555c6faca1f980f00b161eadb089023gstein *resp++ = start[++i];
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein *resp++ = start[i];
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein *resp++ = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if RESOLVE_ENV_PER_TOKEN
f4c310fd2555c6faca1f980f00b161eadb089023gstein return ap_resolve_env(p,result);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein return result;
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein return ap_getword_conf(p, (const char **) line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *str = *line, *strend;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char quote;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*str && apr_isspace(*str))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++str;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!*str) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *line = str;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return "";
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((quote = *str) == '"' || quote == '\'') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein strend = str + 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*strend && *strend != quote) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*strend == '\\' && strend[1] && strend[1] == quote)
f4c310fd2555c6faca1f980f00b161eadb089023gstein strend += 2;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++strend;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = substring_conf(p, str + 1, strend - str - 1, quote);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*strend == quote)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++strend;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein strend = str;
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*strend && !apr_isspace(*strend))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++strend;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein res = substring_conf(p, str, strend - str, 0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*strend && apr_isspace(*strend))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++strend;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *line = strend;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return res;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Check a string for any ${ENV} environment variable
f4c310fd2555c6faca1f980f00b161eadb089023gstein * construct and replace each them by the value of
f4c310fd2555c6faca1f980f00b161eadb089023gstein * that environment variable, if it exists. If the
f4c310fd2555c6faca1f980f00b161eadb089023gstein * environment value does not exist, leave the ${ENV}
f4c310fd2555c6faca1f980f00b161eadb089023gstein * construct alone; it means something else.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein char tmp[ MAX_STRING_LEN ];
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *s, *e;
f4c310fd2555c6faca1f980f00b161eadb089023gstein tmp[0] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!(s=ap_strchr_c(word,'$')))
f4c310fd2555c6faca1f980f00b161eadb089023gstein return word;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein do {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* XXX - relies on strncat() to add '\0'
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein strncat(tmp,word,s - word);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((s[1] == '{') && (e=ap_strchr_c(s,'}'))) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *e2 = e;
f4c310fd2555c6faca1f980f00b161eadb089023gstein word = e + 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein e = getenv(s+2);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (e) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein strcat(tmp,e);
f4c310fd2555c6faca1f980f00b161eadb089023gstein } else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein strncat(tmp, s, e2-s);
f4c310fd2555c6faca1f980f00b161eadb089023gstein strcat(tmp,"}");
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein } else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* ignore invalid strings */
f4c310fd2555c6faca1f980f00b161eadb089023gstein word = s+1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein strcat(tmp,"$");
f4c310fd2555c6faca1f980f00b161eadb089023gstein };
f4c310fd2555c6faca1f980f00b161eadb089023gstein } while ((s=ap_strchr_c(word,'$')));
f4c310fd2555c6faca1f980f00b161eadb089023gstein strcat(tmp,word);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return apr_pstrdup(p,tmp);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_cfg_closefile(configfile_t *cfp)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef DEBUG
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Done with config file %s", cfp->name);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic apr_status_t cfg_close(void *param)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_file_t *cfp = (apr_file_t *) param;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (apr_close(cfp));
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int cfg_getch(void *param)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein char ch;
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_file_t *cfp = (apr_file_t *) param;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (apr_getc(&ch, cfp) == APR_SUCCESS)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return ch;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (int)EOF;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void *cfg_getstr(void *buf, size_t bufsiz, void *param)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_file_t *cfp = (apr_file_t *) param;
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_status_t rv;
f4c310fd2555c6faca1f980f00b161eadb089023gstein rv = apr_fgets(buf, bufsiz, cfp);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (rv == APR_SUCCESS || (rv == APR_EOF && strcmp(buf, "")))
f4c310fd2555c6faca1f980f00b161eadb089023gstein return buf;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Open a configfile_t as FILE, return open configfile_t struct pointer */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(apr_status_t) ap_pcfg_openfile(configfile_t **ret_cfg, apr_pool_t *p, const char *name)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein configfile_t *new_cfg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_file_t *file = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_finfo_t finfo;
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_status_t status;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef DEBUG
f4c310fd2555c6faca1f980f00b161eadb089023gstein char buf[120];
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (name == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Internal error: pcfg_openfile() called with NULL filename");
f4c310fd2555c6faca1f980f00b161eadb089023gstein return APR_EBADF;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* ### We no longer need the test ap_os_is_filename_valid() here
f4c310fd2555c6faca1f980f00b161eadb089023gstein * The directory was already walked on a segment by segment basis,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * so we should never be called with a bad path element, and device
f4c310fd2555c6faca1f980f00b161eadb089023gstein * names as access file names never posed as security threats, since
f4c310fd2555c6faca1f980f00b161eadb089023gstein * it was the admin's choice to assign the .htaccess file's name.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein status = apr_open(&file, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef DEBUG
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Opening config file %s (%s)",
f4c310fd2555c6faca1f980f00b161eadb089023gstein name, (status != APR_SUCCESS) ?
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_strerror(status, buf, sizeof(buf)) : "successful");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status != APR_SUCCESS)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return status;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein status = apr_getfileinfo(&finfo, file);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status != APR_SUCCESS)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return status;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (finfo.filetype != APR_REG &&
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if defined(WIN32) || defined(OS2)
f4c310fd2555c6faca1f980f00b161eadb089023gstein !(strcasecmp(name, "nul") == 0 ||
f4c310fd2555c6faca1f980f00b161eadb089023gstein (strlen(name) >= 4 &&
f4c310fd2555c6faca1f980f00b161eadb089023gstein strcasecmp(name + strlen(name) - 4, "/nul") == 0))) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein strcmp(name, "/dev/null") != 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* WIN32 || OS2 */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Access to file %s denied by server: not a regular file",
f4c310fd2555c6faca1f980f00b161eadb089023gstein name);
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_close(file);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return APR_EBADF;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg = apr_palloc(p, sizeof(*new_cfg));
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->param = file;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->name = apr_pstrdup(p, name);
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->getch = (int (*)(void *)) cfg_getch;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->close = (int (*)(void *)) cfg_close;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->line_number = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *ret_cfg = new_cfg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return APR_SUCCESS;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Allocate a configfile_t handle with user defined functions and params */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(configfile_t *) ap_pcfg_open_custom(apr_pool_t *p, const char *descr,
f4c310fd2555c6faca1f980f00b161eadb089023gstein void *param,
f4c310fd2555c6faca1f980f00b161eadb089023gstein int(*getch)(void *param),
f4c310fd2555c6faca1f980f00b161eadb089023gstein void *(*getstr) (void *buf, size_t bufsiz, void *param),
f4c310fd2555c6faca1f980f00b161eadb089023gstein int(*close_func)(void *param))
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef DEBUG
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL, "Opening config handler %s", descr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->param = param;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->name = descr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->getch = getch;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->getstr = getstr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->close = close_func;
f4c310fd2555c6faca1f980f00b161eadb089023gstein new_cfg->line_number = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return new_cfg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Read one character from a configfile_t */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_cfg_getc(configfile_t *cfp)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein register int ch = cfp->getch(cfp->param);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ch == LF)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++cfp->line_number;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return ch;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Read one line from open configfile_t, strip LF, increase line number */
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* If custom handler does not define a getstr() function, read char by char */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, configfile_t *cfp)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* If a "get string" function is defined, use it */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (cfp->getstr != NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *src, *dst;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *cp;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *cbuf = buf;
f4c310fd2555c6faca1f980f00b161eadb089023gstein size_t cbufsize = bufsize;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++cfp->line_number;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * check for line continuation,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * i.e. match [^\\]\\[\r]\n only
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein cp = cbuf;
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (cp < cbuf+cbufsize && *cp != '\0')
f4c310fd2555c6faca1f980f00b161eadb089023gstein cp++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (cp > cbuf && cp[-1] == LF) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein cp--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (cp > cbuf && cp[-1] == CR)
f4c310fd2555c6faca1f980f00b161eadb089023gstein cp--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (cp > cbuf && cp[-1] == '\\') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein cp--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!(cp > cbuf && cp[-1] == '\\')) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * line continuation requested -
f4c310fd2555c6faca1f980f00b161eadb089023gstein * then remove backslash and continue
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein cbufsize -= (cp-cbuf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein cbuf = cp;
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * no real continuation because escaped -
f4c310fd2555c6faca1f980f00b161eadb089023gstein * then just remove escape character
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
f4c310fd2555c6faca1f980f00b161eadb089023gstein cp[0] = cp[1];
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Leading and trailing white space is eliminated completely
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein src = buf;
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (apr_isspace(*src))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++src;
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* blast trailing whitespace */
f4c310fd2555c6faca1f980f00b161eadb089023gstein dst = &src[strlen(src)];
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (--dst >= src && apr_isspace(*dst))
f4c310fd2555c6faca1f980f00b161eadb089023gstein *dst = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Zap leading whitespace by shifting */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (src != buf)
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (dst = buf; (*dst++ = *src++) != '\0'; )
f4c310fd2555c6faca1f980f00b161eadb089023gstein ;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef DEBUG_CFG_LINES
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein } else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* No "get string" function defined; read character by character */
f4c310fd2555c6faca1f980f00b161eadb089023gstein register int c;
f4c310fd2555c6faca1f980f00b161eadb089023gstein register size_t i = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein buf[0] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* skip leading whitespace */
f4c310fd2555c6faca1f980f00b161eadb089023gstein do {
f4c310fd2555c6faca1f980f00b161eadb089023gstein c = cfp->getch(cfp->param);
f4c310fd2555c6faca1f980f00b161eadb089023gstein } while (c == '\t' || c == ' ');
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == EOF)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if(bufsize < 2) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* too small, assume caller is crazy */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((c == '\t') || (c == ' ')) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein buf[i++] = ' ';
f4c310fd2555c6faca1f980f00b161eadb089023gstein while ((c == '\t') || (c == ' '))
f4c310fd2555c6faca1f980f00b161eadb089023gstein c = cfp->getch(cfp->param);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == CR) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* silently ignore CR (_assume_ that a LF follows) */
f4c310fd2555c6faca1f980f00b161eadb089023gstein c = cfp->getch(cfp->param);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == LF) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* increase line number and return on LF */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++cfp->line_number;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * check for line continuation
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein */
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein if (i > 0 && buf[i-1] == '\\') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein i--;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!(i > 0 && buf[i-1] == '\\')) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* line is continued */
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein c = cfp->getch(cfp->param);
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* else nothing needs be done because
f4c310fd2555c6faca1f980f00b161eadb089023gstein * then the backslash is escaped and
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we just strip to a single one
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* blast trailing whitespace */
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (i > 0 && apr_isspace(buf[i - 1]))
f4c310fd2555c6faca1f980f00b161eadb089023gstein --i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein buf[i] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef DEBUG_CFG_LINES
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein buf[i] = c;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein c = cfp->getch(cfp->param);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Size an HTTP header field list item, as separated by a comma.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * The return value is a pointer to the beginning of the non-empty list item
f4c310fd2555c6faca1f980f00b161eadb089023gstein * within the original string (or NULL if there is none) and the address
f4c310fd2555c6faca1f980f00b161eadb089023gstein * of field is shifted to the next non-comma, non-whitespace character.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * len is the length of the item excluding any beginning whitespace.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const unsigned char *ptr = (const unsigned char *)*field;
f4c310fd2555c6faca1f980f00b161eadb089023gstein const unsigned char *token;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int in_qpair, in_qstr, in_com;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Find first non-comma, non-whitespace byte */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*ptr == ',' || apr_isspace(*ptr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein token = ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Find the end of this item, skipping over dead bits */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (in_qpair = in_qstr = in_com = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (in_qpair) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein in_qpair = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein switch (*ptr) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '\\': in_qpair = 1; /* quoted-pair */
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '"' : if (!in_com) /* quoted string delim */
f4c310fd2555c6faca1f980f00b161eadb089023gstein in_qstr = !in_qstr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '(' : if (!in_qstr) /* comment (may nest) */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++in_com;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ')' : if (in_com) /* end comment */
f4c310fd2555c6faca1f980f00b161eadb089023gstein --in_com;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein default : break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((*len = (ptr - token)) == 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *field = (const char *)ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Advance field pointer to the next non-comma, non-white byte */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*ptr == ',' || apr_isspace(*ptr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein *field = (const char *)ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (const char *)token;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Retrieve an HTTP header field list item, as separated by a comma,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * while stripping insignificant whitespace and lowercasing anything not in
f4c310fd2555c6faca1f980f00b161eadb089023gstein * a quoted string or comment. The return value is a new string containing
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the converted list item (or NULL if none) and the address pointed to by
f4c310fd2555c6faca1f980f00b161eadb089023gstein * field is shifted to the next non-comma, non-whitespace.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *tok_start;
f4c310fd2555c6faca1f980f00b161eadb089023gstein const unsigned char *ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein unsigned char *pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *token;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f80b9e9d38dff8bc3f51406475adb99d7fe888cegstein /* Find the beginning and maximum length of the list item so that
f80b9e9d38dff8bc3f51406475adb99d7fe888cegstein * we can allocate a buffer for the new string and reset the field.
f80b9e9d38dff8bc3f51406475adb99d7fe888cegstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein token = apr_palloc(p, tok_len + 1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Scan the token again, but this time copy only the good bytes.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * We skip extra whitespace and any whitespace around a '=', '/',
f4c310fd2555c6faca1f980f00b161eadb089023gstein * or ';' and lowercase normal characters not within a comment,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * quoted-string or quoted-pair.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (in_qpair) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein in_qpair = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = *ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein switch (*ptr) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '\\': in_qpair = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = ' ';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = *ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '"' : if (!in_com)
f4c310fd2555c6faca1f980f00b161eadb089023gstein in_qstr = !in_qstr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = ' ';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = *ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '(' : if (!in_qstr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++in_com;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = ' ';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = *ptr;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ')' : if (in_com)
f4c310fd2555c6faca1f980f00b161eadb089023gstein --in_com;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = *ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ' ' :
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '\t': if (addspace)
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (in_com || in_qstr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = *ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '=' :
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '/' :
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ';' : if (!(in_com || in_qstr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = *ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein default : if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = ' ';
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos++ = (in_com || in_qstr) ? *ptr
f4c310fd2555c6faca1f980f00b161eadb089023gstein : apr_tolower(*ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *pos = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return token;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Find an item in canonical form (lowercase, no extra spaces) within
f4c310fd2555c6faca1f980f00b161eadb089023gstein * an HTTP field value list. Returns 1 if found, 0 if not found.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * This would be much more efficient if we stored header fields as
f4c310fd2555c6faca1f980f00b161eadb089023gstein * an array of list items as they are received instead of a plain string.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line, const char *tok)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const unsigned char *pos;
f4c310fd2555c6faca1f980f00b161eadb089023gstein const unsigned char *ptr = (const unsigned char *)line;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!line || !tok)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein do { /* loop for each item in line's list */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Find first non-comma, non-whitespace byte */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*ptr == ',' || apr_isspace(*ptr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*ptr)
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein good = 1; /* until proven otherwise for this item */
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein break; /* no items left and nothing good found */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* We skip extra whitespace and any whitespace around a '=', '/',
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein * or ';' and lowercase normal characters not within a comment,
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein * quoted-string or quoted-pair.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (pos = (const unsigned char *)tok;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (in_qpair) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein in_qpair = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (good)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein switch (*ptr) {
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein case '\\': in_qpair = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == ' ');
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein case '"' : if (!in_com)
f4c310fd2555c6faca1f980f00b161eadb089023gstein in_qstr = !in_qstr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == ' ');
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '(' : if (!in_qstr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++in_com;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == ' ');
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ')' : if (in_com)
f4c310fd2555c6faca1f980f00b161eadb089023gstein --in_com;
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ' ' :
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '\t': if (addspace || !good)
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (in_com || in_qstr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '=' :
f4c310fd2555c6faca1f980f00b161eadb089023gstein case '/' :
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ';' : if (!(in_com || in_qstr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein default : if (!good)
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (addspace == 1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = (*pos++ == ' ');
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (in_com || in_qstr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == *ptr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = good && (*pos++ == apr_tolower(*ptr));
f4c310fd2555c6faca1f980f00b161eadb089023gstein addspace = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (good && *pos)
f4c310fd2555c6faca1f980f00b161eadb089023gstein good = 0; /* not good if only a prefix was matched */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein } while (*ptr && !good);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return good;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Retrieve a token, spacing over it and returning a pointer to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the first non-white byte afterwards. Note that these tokens
f4c310fd2555c6faca1f980f00b161eadb089023gstein * are delimited by semis and commas; and can also be delimited
f4c310fd2555c6faca1f980f00b161eadb089023gstein * by whitespace at the caller's option.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line, int accept_white)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *ptr = *accept_line;
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *tok_start;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *token;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int tok_len;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* Find first non-white byte */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*ptr && apr_isspace(*ptr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm tok_start = ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* find token end, skipping over quoted strings.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * (comments are already gone).
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*ptr && (accept_white || !apr_isspace(*ptr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein && *ptr != ';' && *ptr != ',') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*ptr++ == '"')
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*ptr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*ptr++ == '"')
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein tok_len = ptr - tok_start;
f4c310fd2555c6faca1f980f00b161eadb089023gstein token = apr_pstrndup(p, tok_start, tok_len);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Advance accept_line pointer to the next non-white byte */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*ptr && apr_isspace(*ptr))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein *accept_line = ptr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return token;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein/* find http tokens, see the definition of token from RFC2068 */
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gsteinAP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein{
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein const unsigned char *start_token;
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein const unsigned char *s;
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein if (!line)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein s = (const unsigned char *)line;
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (;;) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* find start of token, skip all stop characters, note NUL
f4c310fd2555c6faca1f980f00b161eadb089023gstein * isn't a token stop, so we don't need to test for it
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein */
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein if (!*s) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein start_token = s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* find end of the token */
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!*s) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein}
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line, const char *tok)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int llen, tlen, lidx;
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!line)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein llen = strlen(line);
f4c310fd2555c6faca1f980f00b161eadb089023gstein tlen = strlen(tok);
f4c310fd2555c6faca1f980f00b161eadb089023gstein lidx = llen - tlen;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((lidx < 0) ||
f4c310fd2555c6faca1f980f00b161eadb089023gstein ((lidx > 0) && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (strncasecmp(&line[lidx], tok, tlen) == 0);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *cmd;
f4c310fd2555c6faca1f980f00b161eadb089023gstein unsigned char *d;
f4c310fd2555c6faca1f980f00b161eadb089023gstein const unsigned char *s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein cmd = apr_palloc(p, 2 * strlen(str) + 1); /* Be safe */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm d = (unsigned char *)cmd;
f4c310fd2555c6faca1f980f00b161eadb089023gstein s = (const unsigned char *)str;
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (; *s; ++s) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if defined(OS2) || defined(WIN32)
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Don't allow '&' in parameters under OS/2. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* This can be used to send commands to the shell. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*s == '&') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d++ = ' ';
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d++ = '\\';
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d++ = *s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *d = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return cmd;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic char x2c(const char *what)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein register char digit;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
252e2478cb56afb5ca8585b50bc2ffb780d2efb6gstein#ifndef CHARSET_EBCDIC
f4c310fd2555c6faca1f980f00b161eadb089023gstein digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
f4c310fd2555c6faca1f980f00b161eadb089023gstein digit *= 16;
f4c310fd2555c6faca1f980f00b161eadb089023gstein digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else /*CHARSET_EBCDIC*/
f4c310fd2555c6faca1f980f00b161eadb089023gstein char xstr[5];
f4c310fd2555c6faca1f980f00b161eadb089023gstein xstr[0]='0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein xstr[1]='x';
f4c310fd2555c6faca1f980f00b161eadb089023gstein xstr[2]=what[0];
f4c310fd2555c6faca1f980f00b161eadb089023gstein xstr[3]=what[1];
f4c310fd2555c6faca1f980f00b161eadb089023gstein xstr[4]='\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein digit = apr_xlate_conv_byte(ap_hdrs_from_ascii, 0xFF & strtol(xstr, NULL, 16));
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /*CHARSET_EBCDIC*/
f4c310fd2555c6faca1f980f00b161eadb089023gstein return (digit);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Unescapes a URL.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * Returns 0 on success, non-zero on error
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Failure is due to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * bad % escape returns HTTP_BAD_REQUEST
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * decoding %00 -> \0
f4c310fd2555c6faca1f980f00b161eadb089023gstein * decoding %2f -> / (a special character)
f4c310fd2555c6faca1f980f00b161eadb089023gstein * returns HTTP_NOT_FOUND
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(int) ap_unescape_url(char *url)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm{
f4c310fd2555c6faca1f980f00b161eadb089023gstein register int badesc, badpath;
f4c310fd2555c6faca1f980f00b161eadb089023gstein char *x, *y;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein badesc = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein badpath = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Initial scan for first '%'. Don't bother writing values before
f4c310fd2555c6faca1f980f00b161eadb089023gstein * seeing a '%' */
f4c310fd2555c6faca1f980f00b161eadb089023gstein y = strchr(url, '%');
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (y == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return OK;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (x = y; *y; ++x, ++y) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*y != '%')
f4c310fd2555c6faca1f980f00b161eadb089023gstein *x = *y;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein badesc = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein *x = '%';
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *x = x2c(y + 1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein y += 2;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (*x == '/' || *x == '\0')
f4c310fd2555c6faca1f980f00b161eadb089023gstein badpath = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein *x = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (badesc)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return HTTP_BAD_REQUEST;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (badpath)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return HTTP_NOT_FOUND;
f4c310fd2555c6faca1f980f00b161eadb089023gstein else
f4c310fd2555c6faca1f980f00b161eadb089023gstein return OK;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_port_t port, const request_rec *r)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_is_default_port(port, r))
f4c310fd2555c6faca1f980f00b161eadb089023gstein return apr_pstrdup(p, hostname);
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return apr_psprintf(p, "%s:%u", hostname, port);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* c2x takes an unsigned, and expects the caller has guaranteed that
f4c310fd2555c6faca1f980f00b161eadb089023gstein * 0 <= what < 256... which usually means that you have to cast to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * unsigned char first, because (unsigned)(char)(x) first goes through
f4c310fd2555c6faca1f980f00b161eadb089023gstein * signed extension to an int before the unsigned cast.
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * The reason for this assumption is to assist gcc code generation --
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the unsigned char -> unsigned extension is already done earlier in
f4c310fd2555c6faca1f980f00b161eadb089023gstein * both uses of this code, so there's no need to waste time doing it
f4c310fd2555c6faca1f980f00b161eadb089023gstein * again.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char c2x_table[] = "0123456789abcdef";
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic apr_inline unsigned char *c2x(unsigned what, unsigned char *where)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef CHARSET_EBCDIC
f4c310fd2555c6faca1f980f00b161eadb089023gstein what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
f39230a531b23d94f86a087963299bbe2e431a4agstein#endif /*CHARSET_EBCDIC*/
f39230a531b23d94f86a087963299bbe2e431a4agstein *where++ = '%';
50bd75672ef114fb839dd9643c192b432fdf344cgstein *where++ = c2x_table[what >> 4];
50bd75672ef114fb839dd9643c192b432fdf344cgstein *where++ = c2x_table[what & 0xf];
50bd75672ef114fb839dd9643c192b432fdf344cgstein return where;
50bd75672ef114fb839dd9643c192b432fdf344cgstein}
50bd75672ef114fb839dd9643c192b432fdf344cgstein
50bd75672ef114fb839dd9643c192b432fdf344cgstein/*
50bd75672ef114fb839dd9643c192b432fdf344cgstein * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
50bd75672ef114fb839dd9643c192b432fdf344cgstein * routine is (should be) OS independent.
50bd75672ef114fb839dd9643c192b432fdf344cgstein *
50bd75672ef114fb839dd9643c192b432fdf344cgstein * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
50bd75672ef114fb839dd9643c192b432fdf344cgstein * cases if a ':' occurs before the first '/' in the URL, the URL should be
50bd75672ef114fb839dd9643c192b432fdf344cgstein * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
50bd75672ef114fb839dd9643c192b432fdf344cgstein * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
50bd75672ef114fb839dd9643c192b432fdf344cgstein * efficiency reasons, we don't use escape_path_segment(), which is provided for
50bd75672ef114fb839dd9643c192b432fdf344cgstein * reference. Again, RFC 1808 is where this stuff is defined.
50bd75672ef114fb839dd9643c192b432fdf344cgstein *
50bd75672ef114fb839dd9643c192b432fdf344cgstein * If partial is set, os_escape_path() assumes that the path will be appended to
50bd75672ef114fb839dd9643c192b432fdf344cgstein * something with a '/' in it (and thus does not prefix "./").
50bd75672ef114fb839dd9643c192b432fdf344cgstein */
50bd75672ef114fb839dd9643c192b432fdf344cgstein
50bd75672ef114fb839dd9643c192b432fdf344cgsteinAP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
f39230a531b23d94f86a087963299bbe2e431a4agstein{
50bd75672ef114fb839dd9643c192b432fdf344cgstein char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
f39230a531b23d94f86a087963299bbe2e431a4agstein const unsigned char *s = (const unsigned char *)segment;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein unsigned char *d = (unsigned char *)copy;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein unsigned c;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein while ((c = *s)) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein d = c2x(c, d);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein else {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein *d++ = c;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ++s;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein }
f39230a531b23d94f86a087963299bbe2e431a4agstein *d = '\0';
f39230a531b23d94f86a087963299bbe2e431a4agstein return copy;
f39230a531b23d94f86a087963299bbe2e431a4agstein}
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinAP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein{
f39230a531b23d94f86a087963299bbe2e431a4agstein char *copy = apr_palloc(p, 3 * strlen(path) + 3);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein const unsigned char *s = (const unsigned char *)path;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein unsigned char *d = (unsigned char *)copy;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein unsigned c;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (!partial) {
f39230a531b23d94f86a087963299bbe2e431a4agstein const char *colon = ap_strchr_c(path, ':');
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein const char *slash = ap_strchr_c(path, '/');
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein if (colon && (!slash || colon < slash)) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein *d++ = '.';
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein *d++ = '/';
7281ea331999debdc337b02ce37a3169e0e033a2gstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein while ((c = *s)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein d = c2x(c, d);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein *d++ = c;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ++s;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein *d = '\0';
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return copy;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/* ap_escape_uri is now a macro for os_escape_path */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein int i, j;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char *x;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* first, count the number of extra characters */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein for (i = 0, j = 0; s[i] != '\0'; i++)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (s[i] == '<' || s[i] == '>')
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein j += 3;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else if (s[i] == '&')
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein j += 4;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (j == 0)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return apr_pstrndup(p, s, i);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein x = apr_palloc(p, i + j + 1);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein for (i = 0, j = 0; s[i] != '\0'; i++, j++)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (s[i] == '<') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein memcpy(&x[j], "&lt;", 4);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein j += 3;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else if (s[i] == '>') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein memcpy(&x[j], "&gt;", 4);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein j += 3;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else if (s[i] == '&') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein memcpy(&x[j], "&amp;", 5);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein j += 4;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein x[j] = s[i];
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein x[j] = '\0';
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return x;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(int) ap_is_directory(const char *path)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein apr_finfo_t finfo;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (apr_stat(&finfo, path, NULL) == -1)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return 0; /* in error condition, just return no */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return (finfo.filetype == APR_DIR);
50bd75672ef114fb839dd9643c192b432fdf344cgstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(int) ap_is_rdirectory(const char *path)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein apr_finfo_t finfo;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (apr_lstat(&finfo, path, NULL) == -1)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0; /* in error condition, just return no */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return (finfo.filetype == APR_DIR);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinAP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *src2)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein register int x;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein x = strlen(src1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (x == 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return apr_pstrcat(a, "/", src2, NULL);
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein if (src1[x - 1] != '/')
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein return apr_pstrcat(a, src1, "/", src2, NULL);
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein else
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return apr_pstrcat(a, src1, src2, NULL);
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/*
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(int) ap_is_url(const char *u)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein register int x;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (x = 0; u[x] != ':'; x++) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if ((!u[x]) ||
f4c310fd2555c6faca1f980f00b161eadb089023gstein ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return 0;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#ifndef HAVE_INITGROUPS
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinint initgroups(const char *name, gid_t basegid)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/* QNX, MPE and BeOS do not appear to support supplementary groups. */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return 0;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#else /* ndef QNX */
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein gid_t groups[NGROUPS_MAX];
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein struct group *g;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein int index = 0;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein setgrent();
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein groups[index++] = basegid;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (g->gr_gid != basegid) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char **names;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein for (names = g->gr_mem; *names != NULL; ++names)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (!strcmp(*names, name))
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein groups[index++] = g->gr_gid;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein endgrent();
50bd75672ef114fb839dd9643c192b432fdf344cgstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein return setgroups(index, groups);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein#endif /* def QNX */
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#endif /* def NEED_INITGROUPS */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(int) ap_ind(const char *s, char c)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein register int x;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein for (x = 0; s[x]; x++)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (s[x] == c)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return x;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return -1;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(int) ap_rind(const char *s, char c)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein register int x;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein for (x = strlen(s) - 1; x != -1; x--)
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (s[x] == c)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return x;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(void) ap_str_tolower(char *str)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein while (*str) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein *str = apr_tolower(*str);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ++str;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(uid_t) ap_uname2id(const char *name)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#ifdef WIN32
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return (1);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#else
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein struct passwd *ent;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (name[0] == '#')
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein return (atoi(&name[1]));
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (!(ent = getpwnam(name))) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein exit(1);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return (ent->pw_uid);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#endif
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(gid_t) ap_gname2id(const char *name)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
50bd75672ef114fb839dd9643c192b432fdf344cgstein#ifdef WIN32
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein return (1);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein struct group *ent;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (name[0] == '#')
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return (atoi(&name[1]));
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (!(ent = getgrnam(name))) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein exit(1);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return (ent->gr_gid);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#endif
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic char *find_fqdn(apr_pool_t *a, struct hostent *p)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int x;
50bd75672ef114fb839dd9643c192b432fdf344cgstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (!strchr(p->h_name, '.')) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein for (x = 0; p->h_aliases[x]; ++x) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (strchr(p->h_aliases[x], '.') &&
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein (!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return apr_pstrdup(a, p->h_aliases[x]);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return apr_pstrdup(a, (void *) p->h_name);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinchar *ap_get_local_host(apr_pool_t *a)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#ifndef MAXHOSTNAMELEN
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#define MAXHOSTNAMELEN 256
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#endif
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char str[MAXHOSTNAMELEN + 1];
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char *server_hostname = NULL;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein struct hostent *p;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#ifdef BEOS
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (gethostname(str, sizeof(str) - 1) == 0)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#else
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (gethostname(str, sizeof(str) - 1) != 0)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein#endif
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, NULL,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "%s: gethostname() failed to determine ServerName",
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_server_argv0);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein str[sizeof(str) - 1] = '\0';
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if ((!(p = gethostbyname(str)))
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein || (!(server_hostname = find_fqdn(a, p)))) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* Recovery - return the default servername by IP: */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (!str && p->h_addr_list[0]) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein server_hostname = apr_pstrdup(a, str);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* We will drop through to report the IP-named server */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Since we found a fdqn, return it with no logged message. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return server_hostname;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (!server_hostname)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein server_hostname = apr_pstrdup(a, "127.0.0.1");
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO|APLOG_STARTUP, 0, NULL,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "%s: Could not determine the server's fully qualified "
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "domain name, using %s for ServerName",
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_server_argv0, server_hostname);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return server_hostname;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/* simple 'pool' alloc()ing glue to ap_base64.c
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char *decoded;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein int l;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein decoded = (char *) apr_palloc(p, 1 + ap_base64decode_len(bufcoded));
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein l = ap_base64decode(decoded, bufcoded);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein decoded[l] = '\0'; /* make binary sequence into string */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return decoded;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char *encoded;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein int l = strlen(string);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein encoded = (char *) apr_palloc(p, 1 + ap_base64encode_len(l));
f4c310fd2555c6faca1f980f00b161eadb089023gstein l = ap_base64encode(encoded, string, l);
f4c310fd2555c6faca1f980f00b161eadb089023gstein encoded[l] = '\0'; /* make binary sequence into string */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return encoded;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/* we want to downcase the type/subtype for comparison purposes
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * but nothing else because ;parameter=foo values are case sensitive.
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * XXX: in truth we want to downcase parameter names... but really,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * apache has never handled parameters and such correctly. You
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * also need to compress spaces and such to be able to compare
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * properly. -djg
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_DECLARE(void) ap_content_type_tolower(char *str)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char *semi;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein semi = strchr(str, ';');
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (semi) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein *semi = '\0';
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein while (*str) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein *str = apr_tolower(*str);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ++str;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (semi) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein *semi = ';';
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
}
/*
* 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;
}