util.c revision 44c46ef733836b32585d135d2d90856e7cfd9929
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz/* ====================================================================
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Redistribution and use in source and binary forms, with or without
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * modification, are permitted provided that the following conditions
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * are met:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 1. Redistributions of source code must retain the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 2. Redistributions in binary form must reproduce the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer in
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the documentation and/or other materials provided with the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distribution.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 3. All advertising materials mentioning features or use of this
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * software must display the following acknowledgment:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * "This product includes software developed by the Apache Group
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * for use in the Apache HTTP server project (http://www.apache.org/)."
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 4. The names "Apache Server" and "Apache Group" must not be used to
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * endorse or promote products derived from this software without
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * prior written permission. For written permission, please contact
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * apache@apache.org.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 5. Products derived from this software may not be called "Apache"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * nor may "Apache" appear in their names without prior written
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * permission of the Apache Group.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 6. Redistributions of any form whatsoever must retain the following
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * acknowledgment:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * "This product includes software developed by the Apache Group
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * for use in the Apache HTTP server project (http://www.apache.org/)."
5c0419d51818eb02045cf923a9fe456127a44c60wrowe *
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * OF THE POSSIBILITY OF SUCH DAMAGE.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ====================================================================
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * This software consists of voluntary contributions made by many
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * individuals on behalf of the Apache Group and was originally based
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * on public domain software written at the National Center for
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Supercomputing Applications, University of Illinois, Urbana-Champaign.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * For more information on the Apache Group and the Apache HTTP server
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * project, please see <http://www.apache.org/>.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
0568280364eb026393be492ebc732795c4934643jorton/*
0568280364eb026393be492ebc732795c4934643jorton * util.c: string utility things
0568280364eb026393be492ebc732795c4934643jorton *
0568280364eb026393be492ebc732795c4934643jorton * 3/21/93 Rob McCool
0568280364eb026393be492ebc732795c4934643jorton * 1995-96 Many changes by the Apache Group
0568280364eb026393be492ebc732795c4934643jorton *
0568280364eb026393be492ebc732795c4934643jorton */
0568280364eb026393be492ebc732795c4934643jorton
0568280364eb026393be492ebc732795c4934643jorton/* Debugging aid:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * #define DEBUG_CFG_LINES to trace every line read from the config files
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "httpd.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "http_main.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "http_log.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "http_protocol.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if defined(SUNOS4)
796e4a7141265d8ed7036e4628161c6eafb2a789jorton/* stdio.h has been read in ap_config.h already. Add missing prototypes here: */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesextern int fgetc(FILE *);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesextern char *fgets(char *s, int, FILE*);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesextern int fclose(FILE *);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* A bunch of functions in util.c scan strings looking for certain characters.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * To make that more efficient we encode a lookup table. The test_char_table
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * is generated automatically by gen_test_char.c.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "test_char.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* we assume the folks using this ensure 0 <= c < 256... which means
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * you need a cast to (unsigned char) first, you can't just plug a
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * char in here and get it to work, because if char is signed then it
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * will first be sign extended.
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(char *) ap_get_time()
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_t t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *time_string;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes t = time(NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_string = ctime(&t);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_string[strlen(time_string) - 1] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (time_string);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Examine a field value (such as a media-/content-type) string and return
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_field_noparam(ap_context_t *p, const char *intype)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe const char *semi;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes semi = strchr(intype, ';');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (semi == NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrdup(p, intype);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while ((semi > intype) && ap_isspace(semi[-1])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes semi--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrndup(p, intype, semi - intype);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_ht_time(ap_context_t *p, ap_time_t *t, const char *fmt, int gmt)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe char ts[MAX_STRING_LEN];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char tf[MAX_STRING_LEN];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (gmt) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_explode_time(t, APR_UTCTIME);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_explode_time(t, APR_LOCALTIME);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if(gmt) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Convert %Z to "GMT" and %z to "+0000";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * on hosts that do not have a time zone string in struct tm,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * strftime must assume its argument is local time.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *f;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *strp;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ; f++, strp++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*f != '%') continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes switch (f[1]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case '%':
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *++strp = *++f;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case 'Z':
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = 'G';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = 'M';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = 'T';
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe f++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case 'z': /* common extension */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = '+';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes f++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes fmt = tf;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* check return code? */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_strftime(ts, MAX_STRING_LEN, fmt, t);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ts[MAX_STRING_LEN - 1] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrdup(p, ts);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* What a pain in the ass. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if defined(HAVE_GMTOFF)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(struct tm *) ap_get_gmtoff(int *tz)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_t tt = time(NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct tm *t;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes t = localtime(&tt);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *tz = (int) (t->tm_gmtoff / 60);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(struct tm *) ap_get_gmtoff(int *tz)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_t tt = time(NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct tm gmt;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct tm *t;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes int days, hours, minutes;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Assume we are never more than 24 hours away. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes gmt = *gmtime(&tt); /* remember gmtime/localtime return ptr to static */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes t = localtime(&tt); /* buffer... so be careful */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes days = t->tm_yday - gmt.tm_yday;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes + t->tm_hour - gmt.tm_hour);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes minutes = hours * 60 + t->tm_min - gmt.tm_min;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *tz = minutes;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe/* Roy owes Rob beer. */
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe/* Rob owes Roy dinner. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/* These legacy comments would make a lot more sense if Roy hadn't
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * replaced the old later_than() routine with util_date.c.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Well, okay, they still wouldn't make any sense.
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes */
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes/* Match = 0, NoMatch = 1, Abort = -1
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes * Based loosely on sections of wildmat.c by Rich Salz
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Hmmm... shouldn't this really go component by component?
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(int) ap_strcmp_match(const char *str, const char *exp)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin int x, y;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, y = 0; exp[y]; ++y, ++x) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((!str[x]) && (exp[y] != '*'))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return -1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (exp[y] == '*') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (exp[++y] == '*');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!exp[y])
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (str[x]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int ret;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ret;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return -1;
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin }
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin else if ((exp[y] != '?') && (str[x] != exp[y]))
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin return 1;
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return (str[x] != '\0');
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(int) ap_strcasecmp_match(const char *str, const char *exp)
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin int x, y;
e18ba90a1e610b43062e90cfa8bf0c1edcad7a49bnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, y = 0; exp[y]; ++y, ++x) {
560fd0658902ab57754616c172d8953e69fc4722bnicholes if ((!str[x]) && (exp[y] != '*'))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return -1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (exp[y] == '*') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (exp[++y] == '*');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!exp[y])
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (str[x]) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin int ret;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ret;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return -1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else if ((exp[y] != '?') && (ap_tolower(str[x]) != ap_tolower(exp[y])))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return (str[x] != '\0');
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(int) ap_is_matchexp(const char *str)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin register int x;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin for (x = 0; str[x]; x++)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if ((str[x] == '*') || (str[x] == '?'))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/*
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin * Here's a pool-based interface to POSIX regex's regcomp().
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * Note that we return regex_t instead of being passed one.
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * The reason is that if you use an already-used regex_t structure,
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * the memory that you've already allocated gets forgotten, and
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * regfree() doesn't clear it. So we don't allow it.
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin */
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrinstatic ap_status_t regex_cleanup(void *preg)
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin{
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin regfree((regex_t *) preg);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_SUCCESS;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(regex_t *) ap_pregcomp(ap_context_t *p, const char *pattern,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int cflags)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes regex_t *preg = ap_palloc(p, sizeof(regex_t));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
7add8f7fb048534390571801b7794f71cd9e127abnicholes if (regcomp(preg, pattern, cflags)) {
7add8f7fb048534390571801b7794f71cd9e127abnicholes return NULL;
141e1368614dc7564e1627671361b01b4869b491bnicholes }
141e1368614dc7564e1627671361b01b4869b491bnicholes
7add8f7fb048534390571801b7794f71cd9e127abnicholes ap_register_cleanup(p, (void *) preg, regex_cleanup, regex_cleanup);
7add8f7fb048534390571801b7794f71cd9e127abnicholes
7add8f7fb048534390571801b7794f71cd9e127abnicholes return preg;
7add8f7fb048534390571801b7794f71cd9e127abnicholes}
7add8f7fb048534390571801b7794f71cd9e127abnicholes
7add8f7fb048534390571801b7794f71cd9e127abnicholesAPI_EXPORT(void) ap_pregfree(ap_context_t *p, regex_t * reg)
7add8f7fb048534390571801b7794f71cd9e127abnicholes{
7add8f7fb048534390571801b7794f71cd9e127abnicholes ap_block_alarms();
7add8f7fb048534390571801b7794f71cd9e127abnicholes regfree(reg);
7add8f7fb048534390571801b7794f71cd9e127abnicholes ap_kill_cleanup(p, (void *) reg, regex_cleanup);
7add8f7fb048534390571801b7794f71cd9e127abnicholes ap_unblock_alarms();
7add8f7fb048534390571801b7794f71cd9e127abnicholes}
7add8f7fb048534390571801b7794f71cd9e127abnicholes
7add8f7fb048534390571801b7794f71cd9e127abnicholes/*
7add8f7fb048534390571801b7794f71cd9e127abnicholes * Apache stub function for the regex libraries regexec() to make sure the
7add8f7fb048534390571801b7794f71cd9e127abnicholes * whole regex(3) API is available through the Apache (exported) namespace.
7add8f7fb048534390571801b7794f71cd9e127abnicholes * This is especially important for the DSO situations of modules.
7add8f7fb048534390571801b7794f71cd9e127abnicholes * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
7add8f7fb048534390571801b7794f71cd9e127abnicholes */
7add8f7fb048534390571801b7794f71cd9e127abnicholesAPI_EXPORT(int) ap_regexec(const regex_t *preg, const char *string,
7add8f7fb048534390571801b7794f71cd9e127abnicholes size_t nmatch, regmatch_t pmatch[], int eflags)
141e1368614dc7564e1627671361b01b4869b491bnicholes{
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes return regexec(preg, string, nmatch, pmatch, eflags);
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes}
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholesAPI_EXPORT(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes{
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes return regerror(errcode, preg, errbuf, errbuf_size);
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes}
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes/* This function substitutes for $0-$9, filling in regular expression
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes * submatches. Pass it the same nmatch and pmatch arguments that you
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes * passed ap_regexec(). pmatch should not be greater than the maximum number
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes * of subexpressions - i.e. one more than the re_nsub member of regex_t.
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes *
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes * input should be the string with the $-expressions, source should be the
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes * string that was matched against.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * It returns the substituted string, or NULL on error.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Parts of this code are based on Henry Spencer's regsub(), from his
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * AT&T V8 regexp package.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_pregsub(ap_context_t *p, const char *input, const char *source,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes size_t nmatch, regmatch_t pmatch[])
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *src = input;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *dest, *dst;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char c;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin size_t no;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin int len;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (!source)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return NULL;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (!nmatch)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrdup(p, src);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* First pass, find the size */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin len = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while ((c = *src++) != '\0') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == '&')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes no = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (c == '$' && ap_isdigit(*src))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes no = *src++ - '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes no = 10;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (no > 9) { /* Ordinary character. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == '\\' && (*src == '$' || *src == '&'))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin c = *src++;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin len++;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin len += pmatch[no].rm_eo - pmatch[no].rm_so;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe dest = dst = ap_pcalloc(p, len + 1);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
8a03cd420b800a2428f49f4617293de9b2387b20jorton /* Now actually fill in the string */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin src = input;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin while ((c = *src++) != '\0') {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (c == '&')
54d22ed1c429b903b029bbd62621f11a9e286137minfrin no = 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else if (c == '$' && ap_isdigit(*src))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin no = *src++ - '0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else
54d22ed1c429b903b029bbd62621f11a9e286137minfrin no = 10;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (no > 9) { /* Ordinary character. */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (c == '\\' && (*src == '$' || *src == '&'))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin c = *src++;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *dst++ = c;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
edc346c3223efd41e6a2057c37cea69744b73dccwrowe len = pmatch[no].rm_eo - pmatch[no].rm_so;
edc346c3223efd41e6a2057c37cea69744b73dccwrowe memcpy(dst, source + pmatch[no].rm_so, len);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin dst += len;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *dst = '\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return dest;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Parse .. so we don't compromise security
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(void) ap_getparents(char *name)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int l, w;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* Four paseses, as per RFC 1808 */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* a) remove ./ path segments */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe for (l = 0, w = 0; name[l] != '\0';) {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe l += 2;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe else
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe name[w++] = name[l++];
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
560fd0658902ab57754616c172d8953e69fc4722bnicholes /* b) remove trailing . path, segment */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (w == 1 && name[0] == '.')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes w--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes w--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes name[w] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* c) remove all xx/../ segments. (including leading ../ and /../) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (name[l] != '\0') {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (l == 0 || name[l - 1] == '/')) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int m = l + 3, n;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l = l - 2;
560fd0658902ab57754616c172d8953e69fc4722bnicholes if (l >= 0) {
560fd0658902ab57754616c172d8953e69fc4722bnicholes while (l >= 0 && name[l] != '/')
560fd0658902ab57754616c172d8953e69fc4722bnicholes l--;
560fd0658902ab57754616c172d8953e69fc4722bnicholes l++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes n = l;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while ((name[n] = name[m]))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (++n, ++m);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe else
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ++l;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* d) remove trailing xx/.. segment. */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (l == 2 && name[0] == '.' && name[1] == '.')
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe name[0] = '\0';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l = l - 4;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (l >= 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (l >= 0 && name[l] != '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes name[l] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(void) ap_no2slash(char *name)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *d, *s;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes s = d = name;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef WIN32
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Check for UNC names. Leave leading two slashes. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[0] == '/' && s[1] == '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d++ = *s++;
560fd0658902ab57754616c172d8953e69fc4722bnicholes#endif
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (*s) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((*d++ = *s) == '/') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes do {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++s;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes } while (*s == '/');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++s;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * copy at most n leading directories of s into d
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * d should be at least as large as s plus 1 extra byte
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * assumes n > 0
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the return value is the ever useful pointer to the trailing \0 of d
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * examples:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * /a/b, 1 ==> /
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * /a/b, 2 ==> /a/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * /a/b, 3 ==> /a/b/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * /a/b, 4 ==> /a/b/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (;;) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*s == '\0' || (*s == '/' && (--n) == 0)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d = '/';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d++ = *s++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *++d = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (d);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * return the parent directory name including trailing / of the file s
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_make_dirstr_parent(ap_context_t *p, const char *s)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char *last_slash = strrchr(s, '/');
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char *d;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin int l;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (last_slash == NULL) {
560fd0658902ab57754616c172d8953e69fc4722bnicholes /* XXX: well this is really broken if this happens */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return (ap_pstrdup(p, "/"));
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin l = (last_slash - s) + 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes d = ap_palloc(p, l + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memcpy(d, s, l);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes d[l] = 0;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes return (d);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * This function is deprecated. Use one of the preceeding two functions
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * which are faster.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_make_dirstr(ap_context_t *p, const char *s, int n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x, f;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, f = 0; s[x]; x++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[x] == '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((++f) == n) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_palloc(p, x + 2);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memcpy(res, s, x);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res[x] = '/';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res[x + 1] = '\0';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return res;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (s[strlen(s) - 1] == '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrdup(p, s);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrcat(p, s, "/", NULL);
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe}
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
8113dac419143273351446c3ad653f3fe5ba5cfdwroweAPI_EXPORT(int) ap_count_dirs(const char *path)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x, n;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, n = 0; path[x]; x++)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (path[x] == '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes n++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return n;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(void) ap_chdir_file(const char *file)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe const char *x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char buf[HUGE_STRING_LEN];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x = strrchr(file, '/');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (x == NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes chdir(file);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (x - file < sizeof(buf) - 1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memcpy(buf, file, x - file);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[x - file] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes chdir(buf);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* XXX: well, this is a silly function, no method of reporting an
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * error... ah well. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_nc(ap_context_t *atrans, char **line, char stop)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_getword(atrans, (const char **) line, stop);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword(ap_context_t *atrans, const char **line, char stop)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *pos = strchr(*line, stop);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!pos) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_pstrdup(atrans, *line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line += strlen(*line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_pstrndup(atrans, *line, pos - *line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*pos == stop) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++pos;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line = pos;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_white_nc(ap_context_t *atrans, char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_getword_white(atrans, (const char **) line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_white(ap_context_t *atrans, const char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int pos = -1, x;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes char *res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0; (*line)[x]; x++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (ap_isspace((*line)[x])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes pos = x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (pos == -1) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe res = ap_pstrdup(atrans, *line);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *line += strlen(*line);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return res;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_palloc(atrans, pos + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_cpystrn(res, *line, pos + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (ap_isspace((*line)[pos]))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++pos;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line += pos;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_nulls_nc(ap_context_t *atrans, char **line, char stop)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_getword_nulls(atrans, (const char **) line, stop);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_nulls(ap_context_t *atrans, const char **line, char stop)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *pos = strchr(*line, stop);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!pos) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_pstrdup(atrans, *line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line += strlen(*line);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return res;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe res = ap_pstrndup(atrans, *line, pos - *line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++pos;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line = pos;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Get a word, (new) config-file style --- quoted strings and backslashes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * all honored
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic char *substring_conf(ap_context_t *p, const char *start, int len, char quote)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *result = ap_palloc(p, len + 2);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *resp = result;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; i < len; ++i) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (start[i] == '\\' && (start[i + 1] == '\\'
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe || (quote && start[i + 1] == quote)))
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe *resp++ = start[++i];
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *resp++ = start[i];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *resp++ = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return result;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_conf_nc(ap_context_t *p, char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_getword_conf(p, (const char **) line);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_conf(ap_context_t *p, const char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *str = *line, *strend;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char quote;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*str && ap_isspace(*str))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!*str) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line = str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return "";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((quote = *str) == '"' || quote == '\'') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strend = str + 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*strend && *strend != quote) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*strend == '\\' && strend[1] && strend[1] == quote)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strend += 2;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ++strend;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = substring_conf(p, str + 1, strend - str - 1, quote);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*strend == quote)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++strend;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strend = str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*strend && !ap_isspace(*strend))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++strend;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = substring_conf(p, str, strend - str, 0);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*strend && ap_isspace(*strend))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++strend;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line = strend;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_cfg_closefile(configfile_t *cfp)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef DEBUG
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Done with config file %s", cfp->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic ap_status_t cfg_close(void *param)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_file_t *cfp = (ap_file_t *) param;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (ap_close(cfp));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int cfg_getch(void *param)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_file_t *cfp = (ap_file_t *) param;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes if (ap_getc(&ch, cfp) == APR_SUCCESS)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (int)EOF;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrinstatic void *cfg_getstr(void *buf, size_t bufsiz, void *param)
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin{
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin ap_file_t *cfp = (ap_file_t *) param;
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin if (ap_fgets(buf, bufsiz, cfp) == APR_SUCCESS)
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin return buf;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Open a configfile_t as FILE, return open configfile_t struct pointer */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(ap_status_t) ap_pcfg_openfile(configfile_t **ret_cfg, ap_context_t *p, const char *name)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes configfile_t *new_cfg;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_file_t *file;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_status_t stat;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_filetype_e type;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (name == NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Internal error: pcfg_openfile() called with NULL filename");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_EBADF;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (!ap_os_is_filename_valid(name)) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "Access to config file %s denied: not a valid filename",
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe name);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return APR_EACCES;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe stat = ap_open(&file, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#ifdef DEBUG
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Opening config file %s (%s)",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes name, (stat != APR_SUCCESS) ? strerror(errno) : "successful");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (stat != APR_SUCCESS)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return stat;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes stat = ap_get_filetype(&type, file);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (stat != APR_SUCCESS)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return stat;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (type != APR_REG &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if defined(WIN32) || defined(OS2)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes !(strcasecmp(name, "nul") == 0 ||
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (strlen(name) >= 4 &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strcasecmp(name + strlen(name) - 4, "/nul") == 0))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strcmp(name, "/dev/null") != 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif /* WIN32 || OS2 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Access to file %s denied by server: not a regular file",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_close(file);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_EBADF;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe }
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe new_cfg = ap_palloc(p, sizeof(*new_cfg));
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe new_cfg->param = file;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe new_cfg->name = ap_pstrdup(p, name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->getch = (int (*)(void *)) cfg_getch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes new_cfg->close = (int (*)(void *)) cfg_close;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->line_number = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *ret_cfg = new_cfg;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_SUCCESS;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Allocate a configfile_t handle with user defined functions and params */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(configfile_t *) ap_pcfg_open_custom(ap_context_t *p, const char *descr,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void *param,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int(*getch)(void *param),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void *(*getstr) (void *buf, size_t bufsiz, void *param),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int(*close_func)(void *param))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes configfile_t *new_cfg = ap_palloc(p, sizeof(*new_cfg));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef DEBUG
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL, "Opening config handler %s", descr);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->param = param;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe new_cfg->name = descr;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe new_cfg->getch = getch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->getstr = getstr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->close = close_func;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->line_number = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return new_cfg;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Read one character from a configfile_t */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(int) ap_cfg_getc(configfile_t *cfp)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes register int ch = cfp->getch(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (ch == LF)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++cfp->line_number;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/* Read one line from open configfile_t, strip LF, increase line number */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/* If custom handler does not define a getstr() function, read char by char */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(int) ap_cfg_getline(char *buf, size_t bufsize, configfile_t *cfp)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* If a "get string" function is defined, use it */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (cfp->getstr != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *src, *dst;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *cp;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *cbuf = buf;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe size_t cbufsize = bufsize;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++cfp->line_number;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * check for line continuation,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * i.e. match [^\\]\\[\r]\n only
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cp = cbuf;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (cp < cbuf+cbufsize && *cp != '\0')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cp++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (cp > cbuf && cp[-1] == LF) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cp--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (cp > cbuf && cp[-1] == CR)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cp--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (cp > cbuf && cp[-1] == '\\') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cp--;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (!(cp > cbuf && cp[-1] == '\\')) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * line continuation requested -
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * then remove backslash and continue
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cbufsize -= (cp-cbuf);
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes cbuf = cp;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * no real continuation because escaped -
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin * then just remove escape character
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe cp[0] = cp[1];
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Leading and trailing white space is eliminated completely
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes src = buf;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (ap_isspace(*src))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++src;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* blast trailing whitespace */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes dst = &src[strlen(src)];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (--dst >= src && ap_isspace(*dst))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *dst = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Zap leading whitespace by shifting */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (src != buf)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (dst = buf; (*dst++ = *src++) != '\0'; )
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef DEBUG_CFG_LINES
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL, "Read config: %s", buf);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes } else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* No "get string" function defined; read character by character */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int c;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register size_t i = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[0] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* skip leading whitespace */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes do {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c = cfp->getch(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes } while (c == '\t' || c == ' ');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == EOF)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if(bufsize < 2) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* too small, assume caller is crazy */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((c == '\t') || (c == ' ')) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[i++] = ' ';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while ((c == '\t') || (c == ' '))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c = cfp->getch(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == CR) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* silently ignore CR (_assume_ that a LF follows) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c = cfp->getch(cfp->param);
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin }
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin if (c == LF) {
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin /* increase line number and return on LF */
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin ++cfp->line_number;
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * check for line continuation
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (i > 0 && buf[i-1] == '\\') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes i--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!(i > 0 && buf[i-1] == '\\')) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* line is continued */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c = cfp->getch(cfp->param);
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* else nothing needs be done because
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * then the backslash is escaped and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * we just strip to a single one
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* blast trailing whitespace */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (i > 0 && ap_isspace(buf[i - 1]))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes --i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[i] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef DEBUG_CFG_LINES
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL, "Read config: %s", buf);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[i] = c;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c = cfp->getch(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Size an HTTP header field list item, as separated by a comma.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The return value is a pointer to the beginning of the non-empty list item
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * within the original string (or NULL if there is none) and the address
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * of field is shifted to the next non-comma, non-whitespace character.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * len is the length of the item excluding any beginning whitespace.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(const char *) ap_size_list_item(const char **field, int *len)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const unsigned char *ptr = (const unsigned char *)*field;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const unsigned char *token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int in_qpair, in_qstr, in_com;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Find first non-comma, non-whitespace byte */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*ptr == ',' || ap_isspace(*ptr))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes token = ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Find the end of this item, skipping over dead bits */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (in_qpair = in_qstr = in_com = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++ptr) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (in_qpair) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes in_qpair = 0;
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe switch (*ptr) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe case '\\': in_qpair = 1; /* quoted-pair */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe break;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe case '"' : if (!in_com) /* quoted string delim */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe in_qstr = !in_qstr;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case '(' : if (!in_qstr) /* comment (may nest) */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ++in_com;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case ')' : if (in_com) /* end comment */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes --in_com;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes default : break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((*len = (ptr - token)) == 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *field = (const char *)ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return NULL;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Advance field pointer to the next non-comma, non-white byte */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*ptr == ',' || ap_isspace(*ptr))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *field = (const char *)ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (const char *)token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe/* Retrieve an HTTP header field list item, as separated by a comma,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * while stripping insignificant whitespace and lowercasing anything not in
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * a quoted string or comment. The return value is a new string containing
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * the converted list item (or NULL if none) and the address pointed to by
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes * field is shifted to the next non-comma, non-whitespace.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(char *) ap_get_list_item(ap_context_t *p, const char **field)
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe{
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe const char *tok_start;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe const unsigned char *ptr;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe unsigned char *pos;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char *token;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* Find the beginning and maximum length of the list item so that
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * we can allocate a buffer for the new string and reset the field.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return NULL;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes token = ap_palloc(p, tok_len + 1);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* Scan the token again, but this time copy only the good bytes.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * We skip extra whitespace and any whitespace around a '=', '/',
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * or ';' and lowercase normal characters not within a comment,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * quoted-string or quoted-pair.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ++ptr) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (in_qpair) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe in_qpair = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *pos++ = *ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes switch (*ptr) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '\\': in_qpair = 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (addspace == 1)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = ' ';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = *ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe break;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe case '"' : if (!in_com)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes in_qstr = !in_qstr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (addspace == 1)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = ' ';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = *ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '(' : if (!in_qstr)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ++in_com;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (addspace == 1)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = ' ';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = *ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case ')' : if (in_com)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes --in_com;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = *ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case ' ' :
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '\t': if (addspace)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (in_com || in_qstr)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = *ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '=' :
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '/' :
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case ';' : if (!(in_com || in_qstr))
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = -1;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *pos++ = *ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes default : if (addspace == 1)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = ' ';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos++ = (in_com || in_qstr) ? *ptr
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes : ap_tolower(*ptr);
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *pos = '\0';
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin return token;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/* Find an item in canonical form (lowercase, no extra spaces) within
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * an HTTP field value list. Returns 1 if found, 0 if not found.
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes * This would be much more efficient if we stored header fields as
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * an array of list items as they are received instead of a plain string.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(int) ap_find_list_item(ap_context_t *p, const char *line, const char *tok)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes const unsigned char *pos;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes const unsigned char *ptr = (const unsigned char *)line;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (!line || !tok)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes do { /* loop for each item in line's list */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* Find first non-comma, non-whitespace byte */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes while (*ptr == ',' || ap_isspace(*ptr))
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ++ptr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (*ptr)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe good = 1; /* until proven otherwise for this item */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe else
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break; /* no items left and nothing good found */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* We skip extra whitespace and any whitespace around a '=', '/',
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * or ';' and lowercase normal characters not within a comment,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * quoted-string or quoted-pair.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes for (pos = (const unsigned char *)tok;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ++ptr) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (in_qpair) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes in_qpair = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (good)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = (*pos++ == *ptr);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes switch (*ptr) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '\\': in_qpair = 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (addspace == 1)
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes good = good && (*pos++ == ' ');
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = good && (*pos++ == *ptr);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '"' : if (!in_com)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes in_qstr = !in_qstr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (addspace == 1)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = good && (*pos++ == ' ');
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe good = good && (*pos++ == *ptr);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '(' : if (!in_qstr)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ++in_com;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (addspace == 1)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = good && (*pos++ == ' ');
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = good && (*pos++ == *ptr);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case ')' : if (in_com)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes --in_com;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = good && (*pos++ == *ptr);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes addspace = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case ' ' :
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes case '\t': if (addspace || !good)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (in_com || in_qstr)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = (*pos++ == *ptr);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes addspace = 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe case '=' :
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe case '/' :
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe case ';' : if (!(in_com || in_qstr))
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe addspace = -1;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe good = good && (*pos++ == *ptr);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes default : if (!good)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes break;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (addspace == 1)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = (*pos++ == ' ');
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (in_com || in_qstr)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = good && (*pos++ == *ptr);
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes else
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes good = good && (*pos++ == ap_tolower(*ptr));
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes addspace = 0;
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes break;
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes }
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (good && *pos)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes good = 0; /* not good if only a prefix was matched */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes } while (*ptr && !good);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return good;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/* Retrieve a token, spacing over it and returning a pointer to
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the first non-white byte afterwards. Note that these tokens
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * are delimited by semis and commas; and can also be delimited
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * by whitespace at the caller's option.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_get_token(ap_context_t *p, const char **accept_line, int accept_white)
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *ptr = *accept_line;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *tok_start;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int tok_len;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Find first non-white byte */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*ptr && ap_isspace(*ptr))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes tok_start = ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* find token end, skipping over quoted strings.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * (comments are already gone).
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*ptr && (accept_white || !ap_isspace(*ptr))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes && *ptr != ';' && *ptr != ',') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*ptr++ == '"')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*ptr)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*ptr++ == '"')
796e4a7141265d8ed7036e4628161c6eafb2a789jorton break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes tok_len = ptr - tok_start;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes token = ap_pstrndup(p, tok_start, tok_len);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Advance accept_line pointer to the next non-white byte */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (*ptr && ap_isspace(*ptr))
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ++ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *accept_line = ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* find http tokens, see the definition of token from RFC2068 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_find_token(ap_context_t *p, const char *line, const char *tok)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const unsigned char *start_token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const unsigned char *s;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
796e4a7141265d8ed7036e4628161c6eafb2a789jorton if (!line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes s = (const unsigned char *)line;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (;;) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* find start of token, skip all stop characters, note NUL
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * isn't a token stop, so we don't need to test for it
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++s;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!*s) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes start_token = s;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* find end of the token */
796e4a7141265d8ed7036e4628161c6eafb2a789jorton while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ++s;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!*s) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_find_last_token(ap_context_t *p, const char *line, const char *tok)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int llen, tlen, lidx;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
796e4a7141265d8ed7036e4628161c6eafb2a789jorton llen = strlen(line);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe tlen = strlen(tok);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe lidx = llen - tlen;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((lidx < 0) ||
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ((lidx > 0) && !(ap_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return (strncasecmp(&line[lidx], tok, tlen) == 0);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_escape_shell_cmd(ap_context_t *p, const char *str)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *cmd;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes unsigned char *d;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const unsigned char *s;
796e4a7141265d8ed7036e4628161c6eafb2a789jorton
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe cmd = ap_palloc(p, 2 * strlen(str) + 1); /* Be safe */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe d = (unsigned char *)cmd;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes s = (const unsigned char *)str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (; *s; ++s) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if defined(OS2) || defined(WIN32)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* Don't allow '&' in parameters under OS/2. */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* This can be used to send commands to the shell. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*s == '&') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d++ = ' ';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d++ = '\\';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d++ = *s;
796e4a7141265d8ed7036e4628161c6eafb2a789jorton }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *d = '\0';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return cmd;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic char x2c(const char *what)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin register char digit;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#ifndef CHARSET_EBCDIC
54d22ed1c429b903b029bbd62621f11a9e286137minfrin digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
54d22ed1c429b903b029bbd62621f11a9e286137minfrin digit *= 16;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#else /*CHARSET_EBCDIC*/
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char xstr[5];
54d22ed1c429b903b029bbd62621f11a9e286137minfrin xstr[0]='0';
88adce5ec0da39b41450ce1d5a77659168097e0cjorton xstr[1]='x';
88adce5ec0da39b41450ce1d5a77659168097e0cjorton xstr[2]=what[0];
54d22ed1c429b903b029bbd62621f11a9e286137minfrin xstr[3]=what[1];
54d22ed1c429b903b029bbd62621f11a9e286137minfrin xstr[4]='\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin digit = os_toebcdic[0xFF & strtol(xstr, NULL, 16)];
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#endif /*CHARSET_EBCDIC*/
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return (digit);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/*
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Unescapes a URL.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Returns 0 on success, non-zero on error
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Failure is due to
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * bad % escape returns BAD_REQUEST
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * decoding %00 -> \0
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * decoding %2f -> / (a special character)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * returns NOT_FOUND
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(int) ap_unescape_url(char *url)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin register int badesc, badpath;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char *x, *y;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin badesc = 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin badpath = 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Initial scan for first '%'. Don't bother writing values before
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * seeing a '%' */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin y = strchr(url, '%');
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (y == NULL) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return OK;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes for (x = y; *y; ++x, ++y) {
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes if (*y != '%')
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes *x = *y;
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes else {
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes if (!ap_isxdigit(*(y + 1)) || !ap_isxdigit(*(y + 2))) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin badesc = 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *x = '%';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *x = x2c(y + 1);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin y += 2;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (*x == '/' || *x == '\0')
54d22ed1c429b903b029bbd62621f11a9e286137minfrin badpath = 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *x = '\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (badesc)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return BAD_REQUEST;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else if (badpath)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return NOT_FOUND;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return OK;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholesAPI_EXPORT(char *) ap_construct_server(ap_context_t *p, const char *hostname,
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes unsigned port, const request_rec *r)
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes{
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes if (ap_is_default_port(port, r))
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes return ap_pstrdup(p, hostname);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_psprintf(p, "%s:%u", hostname, port);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/* c2x takes an unsigned, and expects the caller has guaranteed that
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * 0 <= what < 256... which usually means that you have to cast to
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * unsigned char first, because (unsigned)(char)(x) fist goes through
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * signed extension to an int before the unsigned cast.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * The reason for this assumption is to assist gcc code generation --
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * the unsigned char -> unsigned extension is already done earlier in
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * both uses of this code, so there's no need to waste time doing it
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * again.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrinstatic const char c2x_table[] = "0123456789abcdef";
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowestatic ap_inline unsigned char *c2x(unsigned what, unsigned char *where)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *where++ = '%';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *where++ = c2x_table[what >> 4];
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *where++ = c2x_table[what & 0xf];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return where;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
5aa455d45abacfa675c88d4ff53fbe97c44ce545bnicholes * routine is (should be) OS independent.
1223ef8a85a044b5e3a8df29391a66530153aefcbnicholes *
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes * cases if a ':' occurs before the first '/' in the URL, the URL should be
5aa455d45abacfa675c88d4ff53fbe97c44ce545bnicholes * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * efficiency reasons, we don't use escape_path_segment(), which is provided for
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * reference. Again, RFC 1808 is where this stuff is defined.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * If partial is set, os_escape_path() assumes that the path will be appended to
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * something with a '/' in it (and thus does not prefix "./").
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(char *) ap_escape_path_segment(ap_context_t *p, const char *segment)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char *copy = ap_palloc(p, 3 * strlen(segment) + 1);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin const unsigned char *s = (const unsigned char *)segment;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin unsigned char *d = (unsigned char *)copy;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin unsigned c;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin while ((c = *s)) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin d = c2x(c, d);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else {
796e4a7141265d8ed7036e4628161c6eafb2a789jorton *d++ = c;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ++s;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *d = '\0';
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes return copy;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(char *) ap_os_escape_path(ap_context_t *p, const char *path, int partial)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char *copy = ap_palloc(p, 3 * strlen(path) + 3);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin const unsigned char *s = (const unsigned char *)path;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin unsigned char *d = (unsigned char *)copy;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin unsigned c;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (!partial) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *colon = strchr(path, ':');
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *slash = strchr(path, '/');
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (colon && (!slash || colon < slash)) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *d++ = '.';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *d++ = '/';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin while ((c = *s)) {
5aa455d45abacfa675c88d4ff53fbe97c44ce545bnicholes if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
5aa455d45abacfa675c88d4ff53fbe97c44ce545bnicholes d = c2x(c, d);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d++ = c;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ++s;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *d = '\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return copy;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/* ap_escape_uri is now a macro for os_escape_path */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(char *) ap_escape_html(ap_context_t *p, const char *s)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int i, j;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *x;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* first, count the number of extra characters */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin for (i = 0, j = 0; s[i] != '\0'; i++)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (s[i] == '<' || s[i] == '>')
54d22ed1c429b903b029bbd62621f11a9e286137minfrin j += 3;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else if (s[i] == '&')
54d22ed1c429b903b029bbd62621f11a9e286137minfrin j += 4;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes if (j == 0)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrndup(p, s, i);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin x = ap_palloc(p, i + j + 1);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin for (i = 0, j = 0; s[i] != '\0'; i++, j++)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (s[i] == '<') {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin memcpy(&x[j], "&lt;", 4);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin j += 3;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes else if (s[i] == '>') {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe memcpy(&x[j], "&gt;", 4);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe j += 3;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else if (s[i] == '&') {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin memcpy(&x[j], "&amp;", 5);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin j += 4;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes else
54d22ed1c429b903b029bbd62621f11a9e286137minfrin x[j] = s[i];
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin x[j] = '\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return x;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(int) ap_is_directory(const char *path)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct stat finfo;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (stat(path, &finfo) == -1)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0; /* in error condition, just return no */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
796e4a7141265d8ed7036e4628161c6eafb2a789jorton return (S_ISDIR(finfo.st_mode));
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(char *) ap_make_full_path(ap_context_t *a, const char *src1,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin const char *src2)
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin register int x;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin x = strlen(src1);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (x == 0)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrcat(a, "/", src2, NULL);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (src1[x - 1] != '/')
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrcat(a, src1, "/", src2, NULL);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrcat(a, src1, src2, NULL);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/*
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(int) ap_is_url(const char *u)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin register int x;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0; u[x] != ':'; x++) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if ((!u[x]) ||
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ((!ap_isalpha(u[x])) && (!ap_isdigit(u[x])) &&
54d22ed1c429b903b029bbd62621f11a9e286137minfrin (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#ifndef HAVE_STRDUP
54d22ed1c429b903b029bbd62621f11a9e286137minfrinchar *strdup(const char *str)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char *sdup;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (!(sdup = (char *) malloc(strlen(str) + 1))) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Ouch! Out of memory in our strdup()!");
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return NULL;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin sdup = strcpy(sdup, str);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
796e4a7141265d8ed7036e4628161c6eafb2a789jorton return sdup;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/* The following two routines were donated for SVR4 by Andreas Vogel */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#ifndef HAVE_STRCASECMP
54d22ed1c429b903b029bbd62621f11a9e286137minfrinint strcasecmp(const char *a, const char *b)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin const char *p = a;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin const char *q = b;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe for (p = a, q = b; *p && *q; p++, q++) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int diff = ap_tolower(*p) - ap_tolower(*q);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (diff)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return diff;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (*p)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 1; /* p was longer than q */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (*q)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return -1; /* p was shorter than q */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0; /* Exact match */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
141e1368614dc7564e1627671361b01b4869b491bnicholes
141e1368614dc7564e1627671361b01b4869b491bnicholes#ifndef HAVE_STRNCASECMP
141e1368614dc7564e1627671361b01b4869b491bnicholesint strncasecmp(const char *a, const char *b, int n)
141e1368614dc7564e1627671361b01b4869b491bnicholes{
141e1368614dc7564e1627671361b01b4869b491bnicholes const char *p = a;
141e1368614dc7564e1627671361b01b4869b491bnicholes const char *q = b;
141e1368614dc7564e1627671361b01b4869b491bnicholes
141e1368614dc7564e1627671361b01b4869b491bnicholes for (p = a, q = b; /*NOTHING */ ; p++, q++) {
796e4a7141265d8ed7036e4628161c6eafb2a789jorton int diff;
141e1368614dc7564e1627671361b01b4869b491bnicholes if (p == a + n)
141e1368614dc7564e1627671361b01b4869b491bnicholes return 0; /* Match up to n characters */
141e1368614dc7564e1627671361b01b4869b491bnicholes if (!(*p && *q))
141e1368614dc7564e1627671361b01b4869b491bnicholes return *p - *q;
141e1368614dc7564e1627671361b01b4869b491bnicholes diff = ap_tolower(*p) - ap_tolower(*q);
141e1368614dc7564e1627671361b01b4869b491bnicholes if (diff)
141e1368614dc7564e1627671361b01b4869b491bnicholes return diff;
141e1368614dc7564e1627671361b01b4869b491bnicholes }
141e1368614dc7564e1627671361b01b4869b491bnicholes /*NOTREACHED */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#endif
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes/* The following routine was donated for UTS21 by dwd@bell-labs.com */
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes#ifndef HAVE_STRSTR
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholeschar *strstr(char *s1, char *s2)
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes{
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes char *p1, *p2;
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes if (*s2 == '\0') {
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes /* an empty s2 */
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes return(s1);
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes }
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes while((s1 = strchr(s1, *s2)) != NULL) {
1c9fe70e77b36d36ae34997fe25fe47beacf8709bnicholes /* found first character of s2, see if the rest matches */
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes p1 = s1;
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes p2 = s2;
796e4a7141265d8ed7036e4628161c6eafb2a789jorton while (*++p1 == *++p2) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (*p1 == '\0') {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* both strings ended together */
1c9fe70e77b36d36ae34997fe25fe47beacf8709bnicholes return(s1);
4990e910dd8574b9b40beda0cf1aaa72334d6837bnicholes }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (*p2 == '\0') {
1c9fe70e77b36d36ae34997fe25fe47beacf8709bnicholes /* second string ended, a match */
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes break;
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes }
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes /* didn't find a match here, try starting at next character in s1 */
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes s1++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe return(s1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifndef HAVE_INITGROUPS
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint initgroups(const char *name, gid_t basegid)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* QNX, MPE and BeOS do not appear to support supplementary groups. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else /* ndef QNX */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes gid_t groups[NGROUPS_MAX];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct group *g;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin int index = 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin setgrent();
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin groups[index++] = basegid;
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes
141e1368614dc7564e1627671361b01b4869b491bnicholes while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (g->gr_gid != basegid) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char **names;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin for (names = g->gr_mem; *names != NULL; ++names)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (!strcmp(*names, name))
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe groups[index++] = g->gr_gid;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin endgrent();
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return setgroups(index, groups);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#endif /* def QNX */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#endif /* def NEED_INITGROUPS */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#ifndef HAVE_WAITPID
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/* From ikluft@amdahl.com
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * this is not ideal but it works for SVR3 variants
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Modified by dwd@bell-labs.com to call wait3 instead of wait because
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * apache started to use the WNOHANG option.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweint waitpid(pid_t pid, int *statusp, int options)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int tmp_pid;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (kill(pid, 0) == -1) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe errno = ECHILD;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return -1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (((tmp_pid = wait3(statusp, options, 0)) != pid) &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return tmp_pid;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_ind(const char *s, char c)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin register int x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0; s[x]; x++)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[x] == c)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return -1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_rind(const char *s, char c)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = strlen(s) - 1; x != -1; x--)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[x] == c)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return x;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return -1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(void) ap_str_tolower(char *str)
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes{
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes while (*str) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *str = ap_tolower(*str);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(uid_t) ap_uname2id(const char *name)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef WIN32
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct passwd *ent;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (name[0] == '#')
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return (atoi(&name[1]));
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!(ent = getpwnam(name))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes exit(1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (ent->pw_uid);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(gid_t) ap_gname2id(const char *name)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef WIN32
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct group *ent;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton if (name[0] == '#')
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton return (atoi(&name[1]));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!(ent = getgrnam(name))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes exit(1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return (ent->gr_gid);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/*
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * Parses a host of the form <address>[:port]
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * :port is permitted if 'port' is not NULL
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesunsigned long ap_get_virthost_addr(char *w, unsigned short *ports)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct hostent *hep;
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton unsigned long my_addr;
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton char *p;
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton p = strchr(w, ':');
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton if (ports != NULL) {
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton *ports = 0;
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton if (p != NULL && strcmp(p + 1, "*") != 0)
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton *ports = atoi(p + 1);
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (p != NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *p = '\0';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (strcmp(w, "*") == 0) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (p != NULL)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *p = ':';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return htonl(INADDR_ANY);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes my_addr = ap_inet_addr((char *)w);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (my_addr != INADDR_NONE) {
796e4a7141265d8ed7036e4628161c6eafb2a789jorton if (p != NULL)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *p = ':';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return my_addr;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes hep = gethostbyname(w);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Cannot resolve host name %s --- exiting!", w);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes exit(1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (hep->h_addr_list[1]) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Host %s has multiple addresses ---", w);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "you must choose one explicitly for use as");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "a virtual host. Exiting!!!");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes exit(1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (p != NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *p = ':';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ((struct in_addr *) (hep->h_addr))->s_addr;
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic char *find_fqdn(ap_context_t *a, struct hostent *p)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (!strchr(p->h_name, '.')) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin for (x = 0; p->h_aliases[x]; ++x) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (strchr(p->h_aliases[x], '.') &&
54d22ed1c429b903b029bbd62621f11a9e286137minfrin (!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrdup(a, p->h_aliases[x]);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes return NULL;
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes }
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes return ap_pstrdup(a, (void *) p->h_name);
d5cff0d8e871bf2528aadd8736fb50dc044b1e6dbnicholes}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinchar *ap_get_local_host(ap_context_t *a)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#ifndef MAXHOSTNAMELEN
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#define MAXHOSTNAMELEN 256
54d22ed1c429b903b029bbd62621f11a9e286137minfrin#endif
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char str[MAXHOSTNAMELEN + 1];
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton char *server_hostname;
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton struct hostent *p;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef BEOS
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (gethostname(str, sizeof(str) - 1) == 0)
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton#else
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton if (gethostname(str, sizeof(str) - 1) != 0)
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton#endif
e70cf415769ad0b3704e98b3f6da38e916ff7228jorton {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes perror("Unable to gethostname");
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes exit(1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes str[MAXHOSTNAMELEN] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((!(p = gethostbyname(str))) || (!(server_hostname = find_fqdn(a, p)))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%s: cannot determine local host name.",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_server_argv0);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "Use the ServerName directive to set it manually.");
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton exit(1);
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return server_hostname;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
af6d8b02b2fd4ea9a0f6bfb95940d6075030e9a7minfrin/* simple 'pool' alloc()ing glue to ap_base64.c
41774018aa66ddb54ce7fd8fe049ccf3b3f7262aminfrin */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(char *) ap_pbase64decode(ap_context_t *p, const char *bufcoded)
af6d8b02b2fd4ea9a0f6bfb95940d6075030e9a7minfrin{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *decoded;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int l;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e025b9c2dbc3e2873142a775c37336a75ead222bjorton decoded = (char *) ap_palloc(p, 1 + ap_base64decode_len(bufcoded));
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe l = ap_base64decode(decoded, bufcoded);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe decoded[l] = '\0'; /* make binary sequence into string */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return decoded;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(char *) ap_pbase64encode(ap_context_t *p, char *string)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *encoded;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int l = strlen(string);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe encoded = (char *) ap_palloc(p, 1 + ap_base64encode_len(l));
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe l = ap_base64encode(encoded, string, l);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe encoded[l] = '\0'; /* make binary sequence into string */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return encoded;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/* deprecated names for the above two functions, here for compatibility
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(char *) ap_uudecode(ap_context_t *p, const char *bufcoded)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_pbase64decode(p, bufcoded);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwroweAPI_EXPORT(char *) ap_uuencode(ap_context_t *p, char *string)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_pbase64encode(p, string);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#ifdef OS2
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowevoid os2pathname(char *path)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char newpath[MAX_STRING_LEN];
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int loop;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int offset;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe offset = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe for (loop = 0; loop < (strlen(path) + 1) && loop < sizeof(newpath) - 1; loop++) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (path[loop] == '/') {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe newpath[offset] = '\\';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /*
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe offset = offset + 1;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe newpath[offset] = '\\';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe else
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe newpath[offset] = path[loop];
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe offset = offset + 1;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe };
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* Debugging code */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* fprintf(stderr, "%s \n", newpath); */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe strcpy(path, newpath);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe};
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/* quotes in the string are doubled up.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * Used to escape quotes in args passed to OS/2's cmd.exe
141e1368614dc7564e1627671361b01b4869b491bnicholes */
141e1368614dc7564e1627671361b01b4869b491bnicholeschar *ap_double_quotes(ap_context_t *p, char *str)
141e1368614dc7564e1627671361b01b4869b491bnicholes{
141e1368614dc7564e1627671361b01b4869b491bnicholes int num_quotes = 0;
141e1368614dc7564e1627671361b01b4869b491bnicholes int len = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *quote_doubled_str, *dest;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (str[len]) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe num_quotes += str[len++] == '\"';
0894cd17fe3f614bf01dbf84f1414756bd5a34c3bnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes quote_doubled_str = ap_palloc(p, len + num_quotes + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes dest = quote_doubled_str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*str) {
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes if (*str == '\"')
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes *(dest++) = '\"';
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes *(dest++) = *(str++);
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes }
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes *dest = 0;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes return quote_doubled_str;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes}
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes#endif
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifndef HAVE_STRERROR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholeschar *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strerror(int err)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *p;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes extern char *const sys_errlist[];
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe p = sys_errlist[err];
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return (p);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifndef HAVE_DIFFTIME
double difftime(time_t time1, time_t time0)
{
return (time1 - time0);
}
#endif
/* we want to downcase the type/subtype for comparison purposes
* but nothing else because ;parameter=foo values are case sensitive.
* XXX: in truth we want to downcase parameter names... but really,
* apache has never handled parameters and such correctly. You
* also need to compress spaces and such to be able to compare
* properly. -djg
*/
API_EXPORT(void) ap_content_type_tolower(char *str)
{
char *semi;
semi = strchr(str, ';');
if (semi) {
*semi = '\0';
}
while (*str) {
*str = ap_tolower(*str);
++str;
}
if (semi) {
*semi = ';';
}
}
/*
* Given a string, replace any bare " with \" .
*/
API_EXPORT(char *) ap_escape_quotes (ap_context_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 = ap_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;
}