util.c revision 70a2c0407879c11b29adc782d1665a0131f30b3f
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
842ae4bd224140319ae7feec1872b93dfd491143fielding *
842ae4bd224140319ae7feec1872b93dfd491143fielding * Redistribution and use in source and binary forms, with or without
842ae4bd224140319ae7feec1872b93dfd491143fielding * modification, are permitted provided that the following conditions
842ae4bd224140319ae7feec1872b93dfd491143fielding * 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
e8f95a682820a599fe41b22977010636be5c2717jim * for use in the Apache HTTP server project (http://www.apache.org/)."
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
e8f95a682820a599fe41b22977010636be5c2717jim * 4. The names "Apache Server" and "Apache Group" must not be used to
1747d30b98aa1bdbc43994c02cd46ab4cb9319e4fielding * endorse or promote products derived from this software without
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * prior written permission. For written permission, please contact
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * apache@apache.org.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
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
11f2c481e1d57bedb3f758565307501e9a2730ddtrawick * acknowledgment:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * "This product includes software developed by the Apache Group
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * for use in the Apache HTTP server project (http://www.apache.org/)."
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
cd3bbd6d2df78d6c75e5d159a81ef8bdd5f70df9trawick * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * OF THE POSSIBILITY OF SUCH DAMAGE.
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * ====================================================================
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf *
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * This software consists of voluntary contributions made by many
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * individuals on behalf of the Apache Group and was originally based
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * on public domain software written at the National Center for
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Supercomputing Applications, University of Illinois, Urbana-Champaign.
bede2929837dfd23863ad4b39199c63126566d61jorton * For more information on the Apache Group and the Apache HTTP server
0f60998368b493f90120180a93fc2e1e74490872covener * project, please see <http://www.apache.org/>.
0f60998368b493f90120180a93fc2e1e74490872covener *
0f60998368b493f90120180a93fc2e1e74490872covener */
0f60998368b493f90120180a93fc2e1e74490872covener
0f60998368b493f90120180a93fc2e1e74490872covener/*
0f60998368b493f90120180a93fc2e1e74490872covener * util.c: string utility things
0f60998368b493f90120180a93fc2e1e74490872covener *
0f60998368b493f90120180a93fc2e1e74490872covener * 3/21/93 Rob McCool
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * 1995-96 Many changes by the Apache Group
87587593f1a53030e840acc0dec6cc881022ea40covener *
87587593f1a53030e840acc0dec6cc881022ea40covener */
87587593f1a53030e840acc0dec6cc881022ea40covener
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener/* Debugging aid:
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener * #define DEBUG_CFG_LINES to trace every line read from the config files
97cd2f98ad4abe68aaaba96b5bfc9ebf7109a2c1covener */
97cd2f98ad4abe68aaaba96b5bfc9ebf7109a2c1covener
97cd2f98ad4abe68aaaba96b5bfc9ebf7109a2c1covener#include "httpd.h"
97cd2f98ad4abe68aaaba96b5bfc9ebf7109a2c1covener#include "http_main.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "http_log.h"
43997561b2302d13dee973998e77743a3ddd2374trawick#include "http_protocol.h"
fa123db15501821e36e513afa78e839775ad2800covener#if defined(SUNOS4)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* stdio.h has been read in ap_config.h already. Add missing prototypes here: */
0568280364eb026393be492ebc732795c4934643jortonextern int fgetc(FILE *);
0568280364eb026393be492ebc732795c4934643jortonextern char *fgets(char *s, int, FILE*);
0568280364eb026393be492ebc732795c4934643jortonextern int fclose(FILE *);
0568280364eb026393be492ebc732795c4934643jorton#endif
0568280364eb026393be492ebc732795c4934643jorton
0568280364eb026393be492ebc732795c4934643jorton/* A bunch of functions in util.c scan strings looking for certain characters.
0568280364eb026393be492ebc732795c4934643jorton * To make that more efficient we encode a lookup table. The test_char_table
0568280364eb026393be492ebc732795c4934643jorton * is generated automatically by gen_test_char.c.
0568280364eb026393be492ebc732795c4934643jorton */
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.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
796e4a7141265d8ed7036e4628161c6eafb2a789jortonAPI_VAR_EXPORT const char ap_month_snames[12][4] =
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes};
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_VAR_EXPORT const char ap_day_snames[7][4] =
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes};
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_get_time()
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ZZZ When we abstract out time, this whole function should change to use
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes AP funcs. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_t t;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *time_string;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe t = time(NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_string = ctime(&t);
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin time_string[strlen(time_string) - 1] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (time_string);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin * Examine a field value (such as a media-/content-type) string and return
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_field_noparam(ap_context_t *p, const char *intype)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *semi;
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin semi = strchr(intype, ';');
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin if (semi == NULL) {
a1790fb35c4b352dab721370985c623a9f8f5062rpluem return ap_pstrdup(p, intype);
713a2b68bac4aeb1e9c48785006c0732451039depquerna }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while ((semi > intype) && ap_isspace(semi[-1])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes semi--;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrndup(p, intype, semi - intype);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_ht_time(ap_context_t *p, time_t t, const char *fmt, int gmt)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ZZZ this function can be replaced by calls to time formatting routines
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes in APR. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char ts[MAX_STRING_LEN];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char tf[MAX_STRING_LEN];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct tm *tms;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes tms = (gmt ? gmtime(&t) : localtime(&t));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if(gmt) {
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes /* Convert %Z to "GMT" and %z to "+0000";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * on hosts that do not have a time zone string in struct tm,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * strftime must assume its argument is local time.
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *f;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *strp;
fa123db15501821e36e513afa78e839775ad2800covener for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
fa123db15501821e36e513afa78e839775ad2800covener ; f++, strp++) {
fa123db15501821e36e513afa78e839775ad2800covener if (*f != '%') continue;
fa123db15501821e36e513afa78e839775ad2800covener switch (f[1]) {
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim case '%':
fa123db15501821e36e513afa78e839775ad2800covener *++strp = *++f;
307219eca940aa30b873bfd68a44484dd3d3fa88covener break;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener case 'Z':
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim *strp++ = 'G';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *strp++ = 'M';
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener *strp = 'T';
307219eca940aa30b873bfd68a44484dd3d3fa88covener f++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener case 'z': /* common extension */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = '+';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *strp++ = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp++ = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = '0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes f++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = '\0';
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe fmt = tf;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* check return code? */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strftime(ts, MAX_STRING_LEN, fmt, tms);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ts[MAX_STRING_LEN - 1] = '\0';
307219eca940aa30b873bfd68a44484dd3d3fa88covener return ap_pstrdup(p, ts);
307219eca940aa30b873bfd68a44484dd3d3fa88covener}
307219eca940aa30b873bfd68a44484dd3d3fa88covener
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_gm_timestr_822(ap_context_t *p, time_t sec)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct tm *tms;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *date_str = ap_palloc(p, 48 * sizeof(char));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *date_str_ptr = date_str;
fa123db15501821e36e513afa78e839775ad2800covener int real_year;
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf tms = gmtime(&sec); /* ZZZ replace with AP time routine */
fa123db15501821e36e513afa78e839775ad2800covener
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener /* Assumption: this is always 3 */
f2be127030aa4190033084f0a6add531c9bc41desf /* i = strlen(ap_day_snames[tms->tm_wday]); */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener memcpy(date_str_ptr, ap_day_snames[tms->tm_wday], 3);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener date_str_ptr += 3;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ',';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ' ';
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener *date_str_ptr++ = tms->tm_mday / 10 + '0';
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener *date_str_ptr++ = tms->tm_mday % 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ' ';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* Assumption: this is also always 3 */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* i = strlen(ap_month_snames[tms->tm_mon]); */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener memcpy(date_str_ptr, ap_month_snames[tms->tm_mon], 3);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener date_str_ptr += 3;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ' ';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener real_year = 1900 + tms->tm_year;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim /* This routine isn't y10k ready. */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = real_year / 1000 + '0';
6683642c1e0032eeeed5f99e8c14880692ef84c5sf *date_str_ptr++ = real_year % 1000 / 100 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = real_year % 100 / 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = real_year % 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ' ';
6683642c1e0032eeeed5f99e8c14880692ef84c5sf *date_str_ptr++ = tms->tm_hour / 10 + '0';
6683642c1e0032eeeed5f99e8c14880692ef84c5sf *date_str_ptr++ = tms->tm_hour % 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ':';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = tms->tm_min / 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = tms->tm_min % 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ':';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = tms->tm_sec / 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = tms->tm_sec % 10 + '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = ' ';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = 'G';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = 'M';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *date_str_ptr++ = 'T';
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim *date_str_ptr = '\0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim return date_str;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* RFC date format; as strftime '%a, %d %b %Y %T GMT' */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* The equivalent using sprintf. Use this for more legible but slower code
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return ap_psprintf(p,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[tms->tm_wday],
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes tms->tm_mday, ap_month_snames[tms->tm_mon], tms->tm_year + 1900,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes tms->tm_hour, tms->tm_min, tms->tm_sec);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener}
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* What a pain in the ass. */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener#if defined(HAVE_GMTOFF)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerAPI_EXPORT(struct tm *) ap_get_gmtoff(int *tz)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener time_t tt = time(NULL);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener struct tm *t;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener t = localtime(&tt);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *tz = (int) (t->tm_gmtoff / 60);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return t;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener}
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim#else
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimAPI_EXPORT(struct tm *) ap_get_gmtoff(int *tz)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_t tt = time(NULL);
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener struct tm gmt;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct tm *t;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes int days, hours, minutes;
0e05808dc59a321566303084c84b9826a4353cefrederpj
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Assume we are never more than 24 hours away. */
b08925593f214f621161742925dcf074a8047e0acovener gmt = *gmtime(&tt); /* remember gmtime/localtime return ptr to static */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes t = localtime(&tt); /* buffer... so be careful */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf days = t->tm_yday - gmt.tm_yday;
465bb68501690d7a47bfd2a6129580047d76d8f1rederpj hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24)
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim + t->tm_hour - gmt.tm_hour);
465bb68501690d7a47bfd2a6129580047d76d8f1rederpj minutes = hours * 60 + t->tm_min - gmt.tm_min;
e8f95a682820a599fe41b22977010636be5c2717jim *tz = minutes;
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes return t;
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes}
55e2e59e5910072e51c870afc68b0907f41a28e0sf#endif
55e2e59e5910072e51c870afc68b0907f41a28e0sf
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* Roy owes Rob beer. */
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* Rob owes Roy dinner. */
55e2e59e5910072e51c870afc68b0907f41a28e0sf
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* These legacy comments would make a lot more sense if Roy hadn't
55e2e59e5910072e51c870afc68b0907f41a28e0sf * replaced the old later_than() routine with util_date.c.
55e2e59e5910072e51c870afc68b0907f41a28e0sf *
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Well, okay, they still wouldn't make any sense.
55e2e59e5910072e51c870afc68b0907f41a28e0sf */
55e2e59e5910072e51c870afc68b0907f41a28e0sf
55e2e59e5910072e51c870afc68b0907f41a28e0sf/* Match = 0, NoMatch = 1, Abort = -1
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Based loosely on sections of wildmat.c by Rich Salz
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Hmmm... shouldn't this really go component by component?
55e2e59e5910072e51c870afc68b0907f41a28e0sf */
55e2e59e5910072e51c870afc68b0907f41a28e0sfAPI_EXPORT(int) ap_strcmp_match(const char *str, const char *exp)
55e2e59e5910072e51c870afc68b0907f41a28e0sf{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin int x, y;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes for (x = 0, y = 0; exp[y]; ++y, ++x) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if ((!str[x]) && (exp[y] != '*'))
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return -1;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (exp[y] == '*') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (exp[++y] == '*');
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (!exp[y])
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return 0;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf while (str[x]) {
55e2e59e5910072e51c870afc68b0907f41a28e0sf int ret;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
513b324e774c559b579896df131fd7c8471ed529rederpj return ret;
513b324e774c559b579896df131fd7c8471ed529rederpj }
513b324e774c559b579896df131fd7c8471ed529rederpj return -1;
513b324e774c559b579896df131fd7c8471ed529rederpj }
513b324e774c559b579896df131fd7c8471ed529rederpj else if ((exp[y] != '?') && (str[x] != exp[y]))
513b324e774c559b579896df131fd7c8471ed529rederpj return 1;
513b324e774c559b579896df131fd7c8471ed529rederpj }
513b324e774c559b579896df131fd7c8471ed529rederpj return (str[x] != '\0');
513b324e774c559b579896df131fd7c8471ed529rederpj}
513b324e774c559b579896df131fd7c8471ed529rederpj
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesAPI_EXPORT(int) ap_strcasecmp_match(const char *str, const char *exp)
02fd88c85a9850109753b87612955ad372de1575sf{
02fd88c85a9850109753b87612955ad372de1575sf int x, y;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, y = 0; exp[y]; ++y, ++x) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((!str[x]) && (exp[y] != '*'))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return -1;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (exp[y] == '*') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (exp[++y] == '*');
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (!exp[y])
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return 0;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (str[x]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int ret;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return ret;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return -1;
307219eca940aa30b873bfd68a44484dd3d3fa88covener }
307219eca940aa30b873bfd68a44484dd3d3fa88covener else if ((exp[y] != '?') && (ap_tolower(str[x]) != ap_tolower(exp[y])))
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return 1;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener }
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf return (str[x] != '\0');
707f6d077f73cc948deead8df5b40ea42c1eaa78covener}
185aa71728867671e105178b4c66fbc22b65ae26sf
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerAPI_EXPORT(int) ap_is_matchexp(const char *str)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener{
707f6d077f73cc948deead8df5b40ea42c1eaa78covener register int x;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener for (x = 0; str[x]; x++)
9ad7b260be233be7d7b5576979825cac72e15498rederpj if ((str[x] == '*') || (str[x] == '?'))
9ad7b260be233be7d7b5576979825cac72e15498rederpj return 1;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return 0;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes/*
55e2e59e5910072e51c870afc68b0907f41a28e0sf * Here's a pool-based interface to POSIX regex's regcomp().
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Note that we return regex_t instead of being passed one.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * The reason is that if you use an already-used regex_t structure,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * the memory that you've already allocated gets forgotten, and
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * regfree() doesn't clear it. So we don't allow it.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrinstatic ap_status_t regex_cleanup(void *preg)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin regfree((regex_t *) preg);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return APR_SUCCESS;
55e2e59e5910072e51c870afc68b0907f41a28e0sf}
55e2e59e5910072e51c870afc68b0907f41a28e0sf
55e2e59e5910072e51c870afc68b0907f41a28e0sfAPI_EXPORT(regex_t *) ap_pregcomp(ap_context_t *p, const char *pattern,
55e2e59e5910072e51c870afc68b0907f41a28e0sf int cflags)
55e2e59e5910072e51c870afc68b0907f41a28e0sf{
55e2e59e5910072e51c870afc68b0907f41a28e0sf regex_t *preg = ap_palloc(p, sizeof(regex_t));
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (regcomp(preg, pattern, cflags)) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return NULL;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_register_cleanup(p, (void *) preg, regex_cleanup, regex_cleanup);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return preg;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
b08925593f214f621161742925dcf074a8047e0acovenerAPI_EXPORT(void) ap_pregfree(ap_context_t *p, regex_t * reg)
b08925593f214f621161742925dcf074a8047e0acovener{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_block_alarms();
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim regfree(reg);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener ap_kill_cleanup(p, (void *) reg, regex_cleanup);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_unblock_alarms();
707f6d077f73cc948deead8df5b40ea42c1eaa78covener}
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf/*
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Apache stub function for the regex libraries regexec() to make sure the
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * whole regex(3) API is available through the Apache (exported) namespace.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * This is especially important for the DSO situations of modules.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
9ad7b260be233be7d7b5576979825cac72e15498rederpj */
185aa71728867671e105178b4c66fbc22b65ae26sfAPI_EXPORT(int) ap_regexec(const regex_t *preg, const char *string,
707f6d077f73cc948deead8df5b40ea42c1eaa78covener size_t nmatch, regmatch_t pmatch[], int eflags)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener{
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return regexec(preg, string, nmatch, pmatch, eflags);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener}
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerAPI_EXPORT(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener{
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return regerror(errcode, preg, errbuf, errbuf_size);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener}
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
185aa71728867671e105178b4c66fbc22b65ae26sf/* This function substitutes for $0-$9, filling in regular expression
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * submatches. Pass it the same nmatch and pmatch arguments that you
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * passed ap_regexec(). pmatch should not be greater than the maximum number
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * of subexpressions - i.e. one more than the re_nsub member of regex_t.
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf *
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * input should be the string with the $-expressions, source should be the
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * string that was matched against.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener *
185aa71728867671e105178b4c66fbc22b65ae26sf * It returns the substituted string, or NULL on error.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener *
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * Parts of this code are based on Henry Spencer's regsub(), from his
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * AT&T V8 regexp package.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerAPI_EXPORT(char *) ap_pregsub(ap_context_t *p, const char *input, const char *source,
707f6d077f73cc948deead8df5b40ea42c1eaa78covener size_t nmatch, regmatch_t pmatch[])
707f6d077f73cc948deead8df5b40ea42c1eaa78covener{
9ad7b260be233be7d7b5576979825cac72e15498rederpj const char *src = input;
9ad7b260be233be7d7b5576979825cac72e15498rederpj char *dest, *dst;
9ad7b260be233be7d7b5576979825cac72e15498rederpj char c;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf size_t no;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf int len;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (!source)
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf return NULL;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (!nmatch)
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf return ap_pstrdup(p, src);
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf /* First pass, find the size */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf len = 0;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf while ((c = *src++) != '\0') {
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (c == '&')
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf no = 0;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf else if (c == '$' && ap_isdigit(*src))
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf no = *src++ - '0';
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf else
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf no = 10;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (no > 9) { /* Ordinary character. */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (c == '\\' && (*src == '$' || *src == '&'))
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf c = *src++;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf len++;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf }
141e1368614dc7564e1627671361b01b4869b491bnicholes else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes len += pmatch[no].rm_eo - pmatch[no].rm_so;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim
1ae7a5fbce5d4f65f3da355792258fe5dbc4ef55covener dest = dst = ap_pcalloc(p, len + 1);
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Now actually fill in the string */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
185aa71728867671e105178b4c66fbc22b65ae26sf src = input;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes while ((c = *src++) != '\0') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (c == '&')
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes no = 0;
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes else if (c == '$' && ap_isdigit(*src))
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf no = *src++ - '0';
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf else
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf no = 10;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (no > 9) { /* Ordinary character. */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (c == '\\' && (*src == '$' || *src == '&'))
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf c = *src++;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf *dst++ = c;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf len = pmatch[no].rm_eo - pmatch[no].rm_so;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf memcpy(dst, source + pmatch[no].rm_so, len);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf dst += len;
185aa71728867671e105178b4c66fbc22b65ae26sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf *dst = '\0';
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return dest;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes}
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
e8f95a682820a599fe41b22977010636be5c2717jim/*
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * Parse .. so we don't compromise security
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim */
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covenerAPI_EXPORT(void) ap_getparents(char *name)
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener{
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener int l, w;
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener /* Four paseses, as per RFC 1808 */
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener /* a) remove ./ path segments */
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener for (l = 0, w = 0; name[l] != '\0';) {
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
783874b86bfe13d7a4fe0922f344a3779cdccea3covener l += 2;
7dbf29be626018bc389ef94c1846aeac4b72633bsf else
7dbf29be626018bc389ef94c1846aeac4b72633bsf name[w++] = name[l++];
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim }
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* b) remove trailing . path, segment */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (w == 1 && name[0] == '.')
7dbf29be626018bc389ef94c1846aeac4b72633bsf w--;
7dbf29be626018bc389ef94c1846aeac4b72633bsf else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
7dbf29be626018bc389ef94c1846aeac4b72633bsf w--;
7dbf29be626018bc389ef94c1846aeac4b72633bsf name[w] = '\0';
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* c) remove all xx/../ segments. (including leading ../ and /../) */
7dbf29be626018bc389ef94c1846aeac4b72633bsf l = 0;
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf while (name[l] != '\0') {
783874b86bfe13d7a4fe0922f344a3779cdccea3covener if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
7dbf29be626018bc389ef94c1846aeac4b72633bsf (l == 0 || name[l - 1] == '/')) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf register int m = l + 3, n;
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf l = l - 2;
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (l >= 0) {
783874b86bfe13d7a4fe0922f344a3779cdccea3covener while (l >= 0 && name[l] != '/')
7dbf29be626018bc389ef94c1846aeac4b72633bsf l--;
7dbf29be626018bc389ef94c1846aeac4b72633bsf l++;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf else
7dbf29be626018bc389ef94c1846aeac4b72633bsf l = 0;
7dbf29be626018bc389ef94c1846aeac4b72633bsf n = l;
7dbf29be626018bc389ef94c1846aeac4b72633bsf while ((name[n] = name[m]))
783874b86bfe13d7a4fe0922f344a3779cdccea3covener (++n, ++m);
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
307219eca940aa30b873bfd68a44484dd3d3fa88covener else
307219eca940aa30b873bfd68a44484dd3d3fa88covener ++l;
307219eca940aa30b873bfd68a44484dd3d3fa88covener }
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* d) remove trailing xx/.. segment. */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (l == 2 && name[0] == '.' && name[1] == '.')
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes name[0] = '\0';
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes l = l - 4;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (l >= 0) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (l >= 0 && name[l] != '/')
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes l--;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes l++;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes l = 0;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes name[l] = '\0';
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf}
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesAPI_EXPORT(void) ap_no2slash(char *name)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes{
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes char *d, *s;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes s = d = name;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes#ifdef WIN32
307219eca940aa30b873bfd68a44484dd3d3fa88covener /* Check for UNC names. Leave leading two slashes. */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (s[0] == '/' && s[1] == '/')
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *d++ = *s++;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes#endif
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (*s) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if ((*d++ = *s) == '/') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes do {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ++s;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes } while (*s == '/');
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ++s;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *d = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf/*
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * copy at most n leading directories of s into d
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * d should be at least as large as s plus 1 extra byte
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * assumes n > 0
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * the return value is the ever useful pointer to the trailing \0 of d
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener *
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * examples:
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * /a/b, 1 ==> /
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * /a/b, 2 ==> /a/
e8f95a682820a599fe41b22977010636be5c2717jim * /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)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf{
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener for (;;) {
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener if (*s == '\0' || (*s == '/' && (--n) == 0)) {
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim *d = '/';
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener break;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
7dbf29be626018bc389ef94c1846aeac4b72633bsf *d++ = *s++;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener *++d = 0;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener return (d);
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener}
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim/*
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * return the parent directory name including trailing / of the file s
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener */
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covenerAPI_EXPORT(char *) ap_make_dirstr_parent(ap_context_t *p, const char *s)
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener{
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener char *last_slash = strrchr(s, '/');
185aa71728867671e105178b4c66fbc22b65ae26sf char *d;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener int l;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener if (last_slash == NULL) {
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* XXX: well this is really broken if this happens */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim return (ap_pstrdup(p, "/"));
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener l = (last_slash - s) + 1;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener d = ap_palloc(p, l + 1);
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener memcpy(d, s, l);
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener d[l] = 0;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim return (d);
9c63a05713cb83a44a1590b4af33edeebf39f118sf}
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener/*
9c63a05713cb83a44a1590b4af33edeebf39f118sf * This function is deprecated. Use one of the preceeding two functions
9c63a05713cb83a44a1590b4af33edeebf39f118sf * which are faster.
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimAPI_EXPORT(char *) ap_make_dirstr(ap_context_t *p, const char *s, int n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x, f;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *res;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0, f = 0; s[x]; x++) {
9c63a05713cb83a44a1590b4af33edeebf39f118sf if (s[x] == '/')
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if ((++f) == n) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_palloc(p, x + 2);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memcpy(res, s, x);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res[x] = '/';
307219eca940aa30b873bfd68a44484dd3d3fa88covener res[x + 1] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (s[strlen(s) - 1] == '/')
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrdup(p, s);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_pstrcat(p, s, "/", NULL);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(int) ap_count_dirs(const char *path)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
e8f95a682820a599fe41b22977010636be5c2717jim register int x, n;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
8a03cd420b800a2428f49f4617293de9b2387b20jorton for (x = 0, n = 0; path[x]; x++)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (path[x] == '/')
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf n++;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return n;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(void) ap_chdir_file(const char *file)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin const char *x;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char buf[HUGE_STRING_LEN];
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin x = strrchr(file, '/');
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (x == NULL) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin chdir(file);
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf }
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf else if (x - file < sizeof(buf) - 1) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin memcpy(buf, file, x - file);
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener buf[x - file] = '\0';
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener chdir(buf);
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener }
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener /* XXX: well, this is a silly function, no method of reporting an
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener * error... ah well. */
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_EXPORT(char *) ap_getword_nc(ap_context_t *atrans, char **line, char stop)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ap_getword(atrans, (const char **) line, stop);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinAPI_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);
e8f95a682820a599fe41b22977010636be5c2717jim return res;
e8f95a682820a599fe41b22977010636be5c2717jim }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_pstrndup(atrans, *line, pos - *line);
e8f95a682820a599fe41b22977010636be5c2717jim
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe while (*pos == stop) {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe ++pos;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe }
f0f6f1b90ab582896f8a7d56d85bd62a55e57d90covener
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe *line = pos;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
560fd0658902ab57754616c172d8953e69fc4722bnicholes return res;
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jimAPI_EXPORT(char *) ap_getword_white_nc(ap_context_t *atrans, char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_getword_white(atrans, (const char **) line);
9ad7b260be233be7d7b5576979825cac72e15498rederpj}
9ad7b260be233be7d7b5576979825cac72e15498rederpj
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_getword_white(ap_context_t *atrans, const char **line)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int pos = -1, x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0; (*line)[x]; x++) {
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if (ap_isspace((*line)[x])) {
560fd0658902ab57754616c172d8953e69fc4722bnicholes pos = x;
560fd0658902ab57754616c172d8953e69fc4722bnicholes break;
560fd0658902ab57754616c172d8953e69fc4722bnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (pos == -1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes res = ap_pstrdup(atrans, *line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *line += strlen(*line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return res;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim res = ap_palloc(atrans, pos + 1);
e8f95a682820a599fe41b22977010636be5c2717jim ap_cpystrn(res, *line, pos + 1);
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim while (ap_isspace((*line)[pos]))
e8f95a682820a599fe41b22977010636be5c2717jim ++pos;
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *line += pos;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim return res;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim}
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerAPI_EXPORT(char *) ap_getword_nulls_nc(ap_context_t *atrans, char **line, char stop)
307219eca940aa30b873bfd68a44484dd3d3fa88covener{
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener return ap_getword_nulls(atrans, (const char **) line, stop);
54091ac5c596337658fc568231ca1a900abdc5fecovener}
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener
307219eca940aa30b873bfd68a44484dd3d3fa88covenerAPI_EXPORT(char *) ap_getword_nulls(ap_context_t *atrans, const char **line, char stop)
307219eca940aa30b873bfd68a44484dd3d3fa88covener{
307219eca940aa30b873bfd68a44484dd3d3fa88covener char *pos = strchr(*line, stop);
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener char *res;
54091ac5c596337658fc568231ca1a900abdc5fecovener
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 ++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)
e8f95a682820a599fe41b22977010636be5c2717jim{
560fd0658902ab57754616c172d8953e69fc4722bnicholes char *result = ap_palloc(p, len + 2);
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener char *resp = result;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int i;
307219eca940aa30b873bfd68a44484dd3d3fa88covener
307219eca940aa30b873bfd68a44484dd3d3fa88covener for (i = 0; i < len; ++i) {
307219eca940aa30b873bfd68a44484dd3d3fa88covener if (start[i] == '\\' && (start[i + 1] == '\\'
307219eca940aa30b873bfd68a44484dd3d3fa88covener || (quote && start[i + 1] == quote)))
307219eca940aa30b873bfd68a44484dd3d3fa88covener *resp++ = start[++i];
307219eca940aa30b873bfd68a44484dd3d3fa88covener else
307219eca940aa30b873bfd68a44484dd3d3fa88covener *resp++ = start[i];
307219eca940aa30b873bfd68a44484dd3d3fa88covener }
307219eca940aa30b873bfd68a44484dd3d3fa88covener
307219eca940aa30b873bfd68a44484dd3d3fa88covener *resp++ = '\0';
307219eca940aa30b873bfd68a44484dd3d3fa88covener return result;
307219eca940aa30b873bfd68a44484dd3d3fa88covener}
307219eca940aa30b873bfd68a44484dd3d3fa88covener
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jimAPI_EXPORT(char *) ap_getword_conf_nc(ap_context_t *p, char **line)
307219eca940aa30b873bfd68a44484dd3d3fa88covener{
307219eca940aa30b873bfd68a44484dd3d3fa88covener return ap_getword_conf(p, (const char **) line);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
307219eca940aa30b873bfd68a44484dd3d3fa88covenerAPI_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) {
0e05808dc59a321566303084c84b9826a4353cefrederpj if (*strend == '\\' && strend[1] && strend[1] == quote)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strend += 2;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++strend;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
0e05808dc59a321566303084c84b9826a4353cefrederpj res = substring_conf(p, str + 1, strend - str - 1, quote);
185aa71728867671e105178b4c66fbc22b65ae26sf
ebe5305f8b22507374358f32b74d12fb50c05a25covener if (*strend == quote)
ebe5305f8b22507374358f32b74d12fb50c05a25covener ++strend;
ebe5305f8b22507374358f32b74d12fb50c05a25covener }
ebe5305f8b22507374358f32b74d12fb50c05a25covener else {
ebe5305f8b22507374358f32b74d12fb50c05a25covener strend = str;
ebe5305f8b22507374358f32b74d12fb50c05a25covener while (*strend && !ap_isspace(*strend))
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim ++strend;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener res = substring_conf(p, str, strend - str, 0);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener while (*strend && ap_isspace(*strend))
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ++strend;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *line = strend;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return res;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener}
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
0e05808dc59a321566303084c84b9826a4353cefrederpjAPI_EXPORT(int) ap_cfg_closefile(configfile_t *cfp)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener#ifdef DEBUG
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Done with config file %s", cfp->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic ap_status_t cfg_close(void *param)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
9ad7b260be233be7d7b5576979825cac72e15498rederpj ap_file_t *cfp = (ap_file_t *) param;
9ad7b260be233be7d7b5576979825cac72e15498rederpj return (ap_close(cfp));
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sfstatic int cfg_getch(void *param)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char ch;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_file_t *cfp = (ap_file_t *) param;
560fd0658902ab57754616c172d8953e69fc4722bnicholes if (ap_getc(&ch, cfp) == APR_SUCCESS)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return ch;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return (int)EOF;
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerstatic void *cfg_getstr(void *buf, size_t bufsiz, void *param)
7a55c294da84865fe13262ed66ffd0c5841a9da5covener{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_file_t *cfp = (ap_file_t *) param;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim if (ap_fgets(buf, bufsiz, cfp) == APR_SUCCESS)
fa123db15501821e36e513afa78e839775ad2800covener return buf;
185aa71728867671e105178b4c66fbc22b65ae26sf return NULL;
fa123db15501821e36e513afa78e839775ad2800covener}
fa123db15501821e36e513afa78e839775ad2800covener
fa123db15501821e36e513afa78e839775ad2800covener/* Open a configfile_t as FILE, return open configfile_t struct pointer */
fa123db15501821e36e513afa78e839775ad2800covenerAPI_EXPORT(configfile_t *) ap_pcfg_openfile(ap_context_t *p, const char *name)
fa123db15501821e36e513afa78e839775ad2800covener{
fa123db15501821e36e513afa78e839775ad2800covener configfile_t *new_cfg;
fa123db15501821e36e513afa78e839775ad2800covener ap_file_t *file;
fa123db15501821e36e513afa78e839775ad2800covener int saved_errno;
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, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Internal error: pcfg_openfile() called with NULL filename");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!ap_os_is_filename_valid(name)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
307219eca940aa30b873bfd68a44484dd3d3fa88covener "Access to config file %s denied: not a valid filename",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes errno = EACCES;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes stat = ap_open(&file, p, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#ifdef DEBUG
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe saved_errno = errno;
e8f95a682820a599fe41b22977010636be5c2717jim ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, NULL,
e8f95a682820a599fe41b22977010636be5c2717jim "Opening config file %s (%s)",
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe name, (stat != APR_SUCCESS) ? strerror(errno) : "successful");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes errno = saved_errno;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (stat != APR_SUCCESS)
e8f95a682820a599fe41b22977010636be5c2717jim return NULL;
e8f95a682820a599fe41b22977010636be5c2717jim
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (ap_get_filetype(&type, file) == APR_SUCCESS &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes type != APR_REG &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if defined(WIN32) || defined(OS2)
e8f95a682820a599fe41b22977010636be5c2717jim !(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 saved_errno = errno;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, NULL,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "Access to file %s denied by server: not a regular file",
e8f95a682820a599fe41b22977010636be5c2717jim name);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_close(file);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes errno = saved_errno;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg = ap_palloc(p, sizeof(*new_cfg));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->param = file;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes 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;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->close = (int (*)(void *)) cfg_close;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes new_cfg->line_number = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return new_cfg;
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;
e8f95a682820a599fe41b22977010636be5c2717jim new_cfg->name = descr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes 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 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_cfg_getc(configfile_t *cfp)
e8f95a682820a599fe41b22977010636be5c2717jim{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int ch = cfp->getch(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (ch == LF)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++cfp->line_number;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Read one line from open configfile_t, strip LF, increase line number */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* If custom handler does not define a getstr() function, read char by char */
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covenerAPI_EXPORT(int) ap_cfg_getline(char *buf, size_t bufsize, configfile_t *cfp)
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener{
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* If a "get string" function is defined, use it */
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener if (cfp->getstr != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *src, *dst;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes char *cp;
43c3e6a4b559b76b750c245ee95e2782c15b4296jim char *cbuf = buf;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes size_t cbufsize = bufsize;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++cfp->line_number;
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener return 1;
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener /*
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * check for line continuation,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * i.e. match [^\\]\\[\r]\n only
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe */
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes cp = cbuf;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener while (cp < cbuf+cbufsize && *cp != '\0')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cp++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (cp > cbuf && cp[-1] == LF) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf cp--;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (cp > cbuf && cp[-1] == CR)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf cp--;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (cp > cbuf && cp[-1] == '\\') {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf cp--;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (!(cp > cbuf && cp[-1] == '\\')) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /*
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * line continuation requested -
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener * then remove backslash and continue
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf cbufsize -= (cp-cbuf);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cbuf = cp;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * no real continuation because escaped -
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * then just remove escape character
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cp[0] = cp[1];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Leading and trailing white space is eliminated completely
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes src = buf;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (ap_isspace(*src))
e8f95a682820a599fe41b22977010636be5c2717jim ++src;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* blast trailing whitespace */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe dst = &src[strlen(src)];
e8f95a682820a599fe41b22977010636be5c2717jim while (--dst >= src && ap_isspace(*dst))
e8f95a682820a599fe41b22977010636be5c2717jim *dst = '\0';
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* Zap leading whitespace by shifting */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (src != buf)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (dst = buf; (*dst++ = *src++) != '\0'; )
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
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 } 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';
e8f95a682820a599fe41b22977010636be5c2717jim /* skip leading whitespace */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem do {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem c = cfp->getch(cfp->param);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem } while (c == '\t' || c == ' ');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == EOF)
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe return 1;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if(bufsize < 2) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* too small, assume caller is crazy */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
e8f95a682820a599fe41b22977010636be5c2717jim }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((c == '\t') || (c == ' ')) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[i++] = ' ';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while ((c == '\t') || (c == ' '))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c = cfp->getch(cfp->param);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (c == CR) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* silently ignore CR (_assume_ that a LF follows) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c = cfp->getch(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == LF) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* increase line number and return on LF */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++cfp->line_number;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
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 */
e8f95a682820a599fe41b22977010636be5c2717jim c = cfp->getch(cfp->param);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* else nothing needs be done because
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * then the backslash is escaped and
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * we just strip to a single one
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj */
e8f95a682820a599fe41b22977010636be5c2717jim }
e8f95a682820a599fe41b22977010636be5c2717jim /* blast trailing whitespace */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (i > 0 && ap_isspace(buf[i - 1]))
e8f95a682820a599fe41b22977010636be5c2717jim --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 }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
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 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(const char *) ap_size_list_item(const char **field, int *len)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj{
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj const unsigned char *ptr = (const unsigned char *)*field;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj const unsigned char *token;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj int in_qpair, in_qstr, in_com;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* 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 */
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (in_qpair = in_qstr = in_com = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj ++ptr) {
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener if (in_qpair) {
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener in_qpair = 0;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes switch (*ptr) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case '\\': in_qpair = 1; /* quoted-pair */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case '"' : if (!in_com) /* quoted string delim */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener in_qstr = !in_qstr;
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener break;
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener case '(' : if (!in_qstr) /* comment (may nest) */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener ++in_com;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case ')' : if (in_com) /* end comment */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes --in_com;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes break;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener default : break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if ((*len = (ptr - token)) == 0) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf *field = (const char *)ptr;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return NULL;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* Advance field pointer to the next non-comma, non-white byte */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf while (*ptr == ',' || ap_isspace(*ptr))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim *field = (const char *)ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (const char *)token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Retrieve an HTTP header field list item, as separated by a comma,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * while stripping insignificant whitespace and lowercasing anything not in
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * a quoted string or comment. The return value is a new string containing
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the converted list item (or NULL if none) and the address pointed to by
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * field is shifted to the next non-comma, non-whitespace.
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_get_list_item(ap_context_t *p, const char **field)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
e8f95a682820a599fe41b22977010636be5c2717jim const char *tok_start;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe const unsigned char *ptr;
e8f95a682820a599fe41b22977010636be5c2717jim unsigned char *pos;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *token;
e8f95a682820a599fe41b22977010636be5c2717jim int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim /* Find the beginning and maximum length of the list item so that
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * we can allocate a buffer for the new string and reset the field.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return NULL;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem token = ap_palloc(p, tok_len + 1);
9c63a05713cb83a44a1590b4af33edeebf39f118sf
185aa71728867671e105178b4c66fbc22b65ae26sf /* Scan the token again, but this time copy only the good bytes.
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * We skip extra whitespace and any whitespace around a '=', '/',
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * or ';' and lowercase normal characters not within a comment,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * quoted-string or quoted-pair.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++ptr) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (in_qpair) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes in_qpair = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *pos++ = *ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes switch (*ptr) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case '\\': in_qpair = 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (addspace == 1)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *pos++ = ' ';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *pos++ = *ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes addspace = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case '"' : if (!in_com)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes in_qstr = !in_qstr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (addspace == 1)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *pos++ = ' ';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *pos++ = *ptr;
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = 0;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem break;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem case '(' : if (!in_qstr)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ++in_com;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (addspace == 1)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *pos++ = ' ';
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *pos++ = *ptr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem addspace = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj case ')' : if (in_com)
8869662bb1a4078297020e94ae5e928626d877c6rederpj --in_com;
8869662bb1a4078297020e94ae5e928626d877c6rederpj *pos++ = *ptr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem addspace = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener case ' ' :
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener case '\t': if (addspace)
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (in_com || in_qstr)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *pos++ = *ptr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = 1;
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj case '=' :
8869662bb1a4078297020e94ae5e928626d877c6rederpj case '/' :
8869662bb1a4078297020e94ae5e928626d877c6rederpj case ';' : if (!(in_com || in_qstr))
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem addspace = -1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *pos++ = *ptr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem break;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener default : if (addspace == 1)
8869662bb1a4078297020e94ae5e928626d877c6rederpj *pos++ = ' ';
8869662bb1a4078297020e94ae5e928626d877c6rederpj *pos++ = (in_com || in_qstr) ? *ptr
8869662bb1a4078297020e94ae5e928626d877c6rederpj : ap_tolower(*ptr);
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener addspace = 0;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener break;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
8869662bb1a4078297020e94ae5e928626d877c6rederpj *pos = '\0';
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj return token;
8869662bb1a4078297020e94ae5e928626d877c6rederpj}
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj/* Find an item in canonical form (lowercase, no extra spaces) within
8869662bb1a4078297020e94ae5e928626d877c6rederpj * an HTTP field value list. Returns 1 if found, 0 if not found.
8869662bb1a4078297020e94ae5e928626d877c6rederpj * This would be much more efficient if we stored header fields as
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * an array of list items as they are received instead of a plain string.
6733d943c9e8d0f27dd077a04037e8c49eb090ffcovener */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemAPI_EXPORT(int) ap_find_list_item(ap_context_t *p, const char *line, const char *tok)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem{
8869662bb1a4078297020e94ae5e928626d877c6rederpj const unsigned char *pos;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener const unsigned char *ptr = (const unsigned char *)line;
8869662bb1a4078297020e94ae5e928626d877c6rederpj int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (!line || !tok)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return 0;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf do { /* loop for each item in line's list */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Find first non-comma, non-whitespace byte */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener while (*ptr == ',' || ap_isspace(*ptr))
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf ++ptr;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (*ptr)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = 1; /* until proven otherwise for this item */
8869662bb1a4078297020e94ae5e928626d877c6rederpj else
8869662bb1a4078297020e94ae5e928626d877c6rederpj break; /* no items left and nothing good found */
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* We skip extra whitespace and any whitespace around a '=', '/',
8869662bb1a4078297020e94ae5e928626d877c6rederpj * or ';' and lowercase normal characters not within a comment,
8869662bb1a4078297020e94ae5e928626d877c6rederpj * quoted-string or quoted-pair.
8869662bb1a4078297020e94ae5e928626d877c6rederpj */
8869662bb1a4078297020e94ae5e928626d877c6rederpj for (pos = (const unsigned char *)tok;
8869662bb1a4078297020e94ae5e928626d877c6rederpj *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
8869662bb1a4078297020e94ae5e928626d877c6rederpj ++ptr) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (in_qpair) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj in_qpair = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (good)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = (*pos++ == *ptr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj switch (*ptr) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj case '\\': in_qpair = 1;
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (addspace == 1)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = good && (*pos++ == ' ');
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = good && (*pos++ == *ptr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = 0;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem break;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem case '"' : if (!in_com)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem in_qstr = !in_qstr;
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (addspace == 1)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = good && (*pos++ == ' ');
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = good && (*pos++ == *ptr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem case '(' : if (!in_qstr)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ++in_com;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (addspace == 1)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem good = good && (*pos++ == ' ');
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem good = good && (*pos++ == *ptr);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem addspace = 0;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj case ')' : if (in_com)
8869662bb1a4078297020e94ae5e928626d877c6rederpj --in_com;
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = good && (*pos++ == *ptr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj case ' ' :
8869662bb1a4078297020e94ae5e928626d877c6rederpj case '\t': if (addspace || !good)
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (in_com || in_qstr)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = (*pos++ == *ptr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj else
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = 1;
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj case '=' :
8869662bb1a4078297020e94ae5e928626d877c6rederpj case '/' :
8869662bb1a4078297020e94ae5e928626d877c6rederpj case ';' : if (!(in_com || in_qstr))
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = -1;
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = good && (*pos++ == *ptr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj default : if (!good)
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (addspace == 1)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = (*pos++ == ' ');
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (in_com || in_qstr)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = good && (*pos++ == *ptr);
342377e75b69a70c16175cce7f694472f9f8a6adjailletc else
342377e75b69a70c16175cce7f694472f9f8a6adjailletc good = good && (*pos++ == ap_tolower(*ptr));
8869662bb1a4078297020e94ae5e928626d877c6rederpj addspace = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj break;
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (good && *pos)
8869662bb1a4078297020e94ae5e928626d877c6rederpj good = 0; /* not good if only a prefix was matched */
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj } while (*ptr && !good);
8869662bb1a4078297020e94ae5e928626d877c6rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return good;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem/* Retrieve a token, spacing over it and returning a pointer to
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * the first non-white byte afterwards. Note that these tokens
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * are delimited by semis and commas; and can also be delimited
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * by whitespace at the caller's option.
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemAPI_EXPORT(char *) ap_get_token(ap_context_t *p, const char **accept_line, int accept_white)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem{
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem const char *ptr = *accept_line;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem const char *tok_start;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem char *token;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem int tok_len;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* Find first non-white byte */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem while (*ptr && ap_isspace(*ptr))
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ++ptr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem tok_start = ptr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* find token end, skipping over quoted strings.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * (comments are already gone).
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem while (*ptr && (accept_white || !ap_isspace(*ptr))
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem && *ptr != ';' && *ptr != ',') {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (*ptr++ == '"')
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem while (*ptr)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (*ptr++ == '"')
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem break;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem tok_len = ptr - tok_start;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem token = ap_pstrndup(p, tok_start, tok_len);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* Advance accept_line pointer to the next non-white byte */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem while (*ptr && ap_isspace(*ptr))
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj ++ptr;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *accept_line = ptr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return token;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj/* find http tokens, see the definition of token from RFC2068 */
11ca38a20ab9b2d00258f745620e2724838e7e21rederpjAPI_EXPORT(int) ap_find_token(ap_context_t *p, const char *line, const char *tok)
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj{
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj const unsigned char *start_token;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj const unsigned char *s;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj if (!line)
a9c4332dc6241dc11dd104826bd179d42ccc0f12fuankg return 0;
a9c4332dc6241dc11dd104826bd179d42ccc0f12fuankg
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj s = (const unsigned char *)line;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj for (;;) {
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj /* find start of token, skip all stop characters, note NUL
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj * isn't a token stop, so we don't need to test for it
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ++s;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (!*s) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return 0;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem start_token = s;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj /* find end of the token */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ++s;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return 1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (!*s) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return 0;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpjAPI_EXPORT(int) ap_find_last_token(ap_context_t *p, const char *line, const char *tok)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj{
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj int llen, tlen, lidx;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (!line)
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return 0;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj llen = strlen(line);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj tlen = strlen(tok);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem lidx = llen - tlen;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if ((lidx < 0) ||
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ((lidx > 0) && !(ap_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return 0;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return (strncasecmp(&line[lidx], tok, tlen) == 0);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj}
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpjAPI_EXPORT(char *) ap_escape_shell_cmd(ap_context_t *p, const char *str)
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj{
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj char *cmd;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj unsigned char *d;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj const unsigned char *s;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj cmd = ap_palloc(p, 2 * strlen(str) + 1); /* Be safe */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj d = (unsigned char *)cmd;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj s = (const unsigned char *)str;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj for (; *s; ++s) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem#if defined(OS2) || defined(WIN32)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* Don't allow '&' in parameters under OS/2. */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* This can be used to send commands to the shell. */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (*s == '&') {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *d++ = ' ';
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem continue;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem#endif
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
0e05808dc59a321566303084c84b9826a4353cefrederpj *d++ = '\\';
0e05808dc59a321566303084c84b9826a4353cefrederpj }
185aa71728867671e105178b4c66fbc22b65ae26sf *d++ = *s;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf }
0e05808dc59a321566303084c84b9826a4353cefrederpj *d = '\0';
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
0e05808dc59a321566303084c84b9826a4353cefrederpj return cmd;
0e05808dc59a321566303084c84b9826a4353cefrederpj}
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpjstatic char x2c(const char *what)
342377e75b69a70c16175cce7f694472f9f8a6adjailletc{
342377e75b69a70c16175cce7f694472f9f8a6adjailletc register char digit;
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj#ifndef CHARSET_EBCDIC
0e05808dc59a321566303084c84b9826a4353cefrederpj digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
0e05808dc59a321566303084c84b9826a4353cefrederpj digit *= 16;
0e05808dc59a321566303084c84b9826a4353cefrederpj digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
0e05808dc59a321566303084c84b9826a4353cefrederpj#else /*CHARSET_EBCDIC*/
0e05808dc59a321566303084c84b9826a4353cefrederpj char xstr[5];
0e05808dc59a321566303084c84b9826a4353cefrederpj xstr[0]='0';
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj xstr[1]='x';
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj xstr[2]=what[0];
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj xstr[3]=what[1];
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj xstr[4]='\0';
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj digit = os_toebcdic[0xFF & strtol(xstr, NULL, 16)];
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj#endif /*CHARSET_EBCDIC*/
0e05808dc59a321566303084c84b9826a4353cefrederpj return (digit);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj}
185aa71728867671e105178b4c66fbc22b65ae26sf
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf/*
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Unescapes a URL.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Returns 0 on success, non-zero on error
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * Failure is due to
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * bad % escape returns BAD_REQUEST
185aa71728867671e105178b4c66fbc22b65ae26sf *
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * decoding %00 -> \0
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * decoding %2f -> / (a special character)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * returns NOT_FOUND
0e05808dc59a321566303084c84b9826a4353cefrederpj */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemAPI_EXPORT(int) ap_unescape_url(char *url)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem{
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem register int badesc, badpath;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj char *x, *y;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj badesc = 0;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj badpath = 0;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* Initial scan for first '%'. Don't bother writing values before
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * seeing a '%' */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj y = strchr(url, '%');
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (y == NULL) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return OK;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem for (x = y; *y; ++x, ++y) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (*y != '%')
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *x = *y;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
0e05808dc59a321566303084c84b9826a4353cefrederpj if (!ap_isxdigit(*(y + 1)) || !ap_isxdigit(*(y + 2))) {
0e05808dc59a321566303084c84b9826a4353cefrederpj badesc = 1;
0e05808dc59a321566303084c84b9826a4353cefrederpj *x = '%';
0e05808dc59a321566303084c84b9826a4353cefrederpj }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
185aa71728867671e105178b4c66fbc22b65ae26sf *x = x2c(y + 1);
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf y += 2;
0e05808dc59a321566303084c84b9826a4353cefrederpj if (*x == '/' || *x == '\0')
0e05808dc59a321566303084c84b9826a4353cefrederpj badpath = 1;
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj }
185aa71728867671e105178b4c66fbc22b65ae26sf *x = '\0';
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf if (badesc)
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf return BAD_REQUEST;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf else if (badpath)
0e05808dc59a321566303084c84b9826a4353cefrederpj return NOT_FOUND;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj else
0e05808dc59a321566303084c84b9826a4353cefrederpj return OK;
40a1aee60a66f7c8dbd0835fdd4f09334e12fc15rpluem}
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpjAPI_EXPORT(char *) ap_construct_server(ap_context_t *p, const char *hostname,
40a1aee60a66f7c8dbd0835fdd4f09334e12fc15rpluem unsigned port, const request_rec *r)
0e05808dc59a321566303084c84b9826a4353cefrederpj{
0e05808dc59a321566303084c84b9826a4353cefrederpj if (ap_is_default_port(port, r))
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_pstrdup(p, hostname);
0e05808dc59a321566303084c84b9826a4353cefrederpj else {
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_psprintf(p, "%s:%u", hostname, port);
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj}
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj/* c2x takes an unsigned, and expects the caller has guaranteed that
0e05808dc59a321566303084c84b9826a4353cefrederpj * 0 <= what < 256... which usually means that you have to cast to
185aa71728867671e105178b4c66fbc22b65ae26sf * unsigned char first, because (unsigned)(char)(x) fist goes through
0e05808dc59a321566303084c84b9826a4353cefrederpj * signed extension to an int before the unsigned cast.
0e05808dc59a321566303084c84b9826a4353cefrederpj *
0e05808dc59a321566303084c84b9826a4353cefrederpj * The reason for this assumption is to assist gcc code generation --
0e05808dc59a321566303084c84b9826a4353cefrederpj * the unsigned char -> unsigned extension is already done earlier in
0e05808dc59a321566303084c84b9826a4353cefrederpj * both uses of this code, so there's no need to waste time doing it
0e05808dc59a321566303084c84b9826a4353cefrederpj * again.
0e05808dc59a321566303084c84b9826a4353cefrederpj */
0e05808dc59a321566303084c84b9826a4353cefrederpjstatic const char c2x_table[] = "0123456789abcdef";
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpjstatic ap_inline unsigned char *c2x(unsigned what, unsigned char *where)
0e05808dc59a321566303084c84b9826a4353cefrederpj{
185aa71728867671e105178b4c66fbc22b65ae26sf *where++ = '%';
0e05808dc59a321566303084c84b9826a4353cefrederpj *where++ = c2x_table[what >> 4];
0e05808dc59a321566303084c84b9826a4353cefrederpj *where++ = c2x_table[what & 0xf];
0e05808dc59a321566303084c84b9826a4353cefrederpj return where;
0e05808dc59a321566303084c84b9826a4353cefrederpj}
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj/*
0e05808dc59a321566303084c84b9826a4353cefrederpj * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * routine is (should be) OS independent.
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj *
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * cases if a ':' occurs before the first '/' in the URL, the URL should be
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * efficiency reasons, we don't use escape_path_segment(), which is provided for
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * reference. Again, RFC 1808 is where this stuff is defined.
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * If partial is set, os_escape_path() assumes that the path will be appended to
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * something with a '/' in it (and thus does not prefix "./").
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpjAPI_EXPORT(char *) ap_escape_path_segment(ap_context_t *p, const char *segment)
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj{
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem char *copy = ap_palloc(p, 3 * strlen(segment) + 1);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem const unsigned char *s = (const unsigned char *)segment;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem unsigned char *d = (unsigned char *)copy;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem unsigned c;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj while ((c = *s)) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem d = c2x(c, d);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
185aa71728867671e105178b4c66fbc22b65ae26sf *d++ = c;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf }
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf ++s;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *d = '\0';
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return copy;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemAPI_EXPORT(char *) ap_os_escape_path(ap_context_t *p, const char *path, int partial)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem{
185aa71728867671e105178b4c66fbc22b65ae26sf char *copy = ap_palloc(p, 3 * strlen(path) + 3);
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf const unsigned char *s = (const unsigned char *)path;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf unsigned char *d = (unsigned char *)copy;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf unsigned c;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (!partial) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem char *colon = strchr(path, ':');
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem char *slash = strchr(path, '/');
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (colon && (!slash || colon < slash)) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *d++ = '.';
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj *d++ = '/';
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj while ((c = *s)) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj d = c2x(c, d);
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe }
e8f95a682820a599fe41b22977010636be5c2717jim else {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe *d++ = c;
e8f95a682820a599fe41b22977010636be5c2717jim }
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe ++s;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *d = '\0';
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes return copy;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* ap_escape_uri is now a macro for os_escape_path */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(char *) ap_escape_html(ap_context_t *p, const char *s)
f05787953018140838ad51456c86c965d6a86267jim{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i, j;
f05787953018140838ad51456c86c965d6a86267jim char *x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* first, count the number of extra characters */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0, j = 0; s[i] != '\0'; i++)
e8f95a682820a599fe41b22977010636be5c2717jim if (s[i] == '<' || s[i] == '>')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes j += 3;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (s[i] == '&')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes j += 4;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (j == 0)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrndup(p, s, i);
e8f95a682820a599fe41b22977010636be5c2717jim
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe x = ap_palloc(p, i + j + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0, j = 0; s[i] != '\0'; i++, j++)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[i] == '<') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memcpy(&x[j], "&lt;", 4);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes j += 3;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (s[i] == '>') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memcpy(&x[j], "&gt;", 4);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes j += 3;
e8f95a682820a599fe41b22977010636be5c2717jim }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe else if (s[i] == '&') {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes memcpy(&x[j], "&amp;", 5);
e8f95a682820a599fe41b22977010636be5c2717jim j += 4;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
e8f95a682820a599fe41b22977010636be5c2717jim x[j] = s[i];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim x[j] = '\0';
e8f95a682820a599fe41b22977010636be5c2717jim return x;
e8f95a682820a599fe41b22977010636be5c2717jim}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_is_directory(const char *path)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct stat finfo;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ZZZ replace with AP File Info func. */
e8f95a682820a599fe41b22977010636be5c2717jim if (stat(path, &finfo) == -1)
e8f95a682820a599fe41b22977010636be5c2717jim return 0; /* in error condition, just return no */
e8f95a682820a599fe41b22977010636be5c2717jim
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return (S_ISDIR(finfo.st_mode));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covenerAPI_EXPORT(char *) ap_make_full_path(ap_context_t *a, const char *src1,
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener const char *src2)
9597d9521d858a60fd123c9cf6e9d779a1dfa466jailletc{
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener register int x;
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener x = strlen(src1);
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener if (x == 0)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrcat(a, "/", src2, NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (src1[x - 1] != '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrcat(a, src1, "/", src2, NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_pstrcat(a, src1, src2, NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
e8f95a682820a599fe41b22977010636be5c2717jim * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(int) ap_is_url(const char *u)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (x = 0; u[x] != ':'; x++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((!u[x]) ||
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener ((!ap_isalpha(u[x])) && (!ap_isdigit(u[x])) &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener }
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener return (x ? 1 : 0); /* If the first character is ':', it's broken, too */
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener}
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef NEED_STRDUP
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholeschar *strdup(const char *str)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *sdup;
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (!(sdup = (char *) malloc(strlen(str) + 1))) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener fprintf(stderr, "Ouch! Out of memory in our strdup()!\n");
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf return NULL;
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe sdup = strcpy(sdup, str);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes return sdup;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* The following two routines were donated for SVR4 by Andreas Vogel */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef NEED_STRCASECMP
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint strcasecmp(const char *a, const char *b)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *p = a;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *q = b;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (p = a, q = b; *p && *q; p++, q++) {
e8f95a682820a599fe41b22977010636be5c2717jim int diff = ap_tolower(*p) - ap_tolower(*q);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (diff)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return diff;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*p)
e8f95a682820a599fe41b22977010636be5c2717jim return 1; /* p was longer than q */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*q)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return -1; /* p was shorter than q */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0; /* Exact match */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef NEED_STRNCASECMP
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint strncasecmp(const char *a, const char *b, int n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *p = a;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *q = b;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (p = a, q = b; /*NOTHING */ ; p++, q++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int diff;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (p == a + n)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0; /* Match up to n characters */
e8f95a682820a599fe41b22977010636be5c2717jim if (!(*p && *q))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return *p - *q;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes diff = ap_tolower(*p) - ap_tolower(*q);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (diff)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return diff;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*NOTREACHED */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* The following routine was donated for UTS21 by dwd@bell-labs.com */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef NEED_STRSTR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholeschar *strstr(char *s1, char *s2)
e8f95a682820a599fe41b22977010636be5c2717jim{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *p1, *p2;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*s2 == '\0') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* an empty s2 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return(s1);
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf while((s1 = strchr(s1, *s2)) != NULL) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* found first character of s2, see if the rest matches */
7dbf29be626018bc389ef94c1846aeac4b72633bsf p1 = s1;
7dbf29be626018bc389ef94c1846aeac4b72633bsf p2 = s2;
7dbf29be626018bc389ef94c1846aeac4b72633bsf while (*++p1 == *++p2) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (*p1 == '\0') {
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* both strings ended together */
7dbf29be626018bc389ef94c1846aeac4b72633bsf return(s1);
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes if (*p2 == '\0') {
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener /* second string ended, a match */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* didn't find a match here, try starting at next character in s1 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes s1++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
7dbf29be626018bc389ef94c1846aeac4b72633bsf return(s1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef NEED_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. */
307219eca940aa30b873bfd68a44484dd3d3fa88covener return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else /* ndef QNX */
307219eca940aa30b873bfd68a44484dd3d3fa88covener gid_t groups[NGROUPS_MAX];
307219eca940aa30b873bfd68a44484dd3d3fa88covener struct group *g;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int index = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes setgrent();
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes groups[index++] = basegid;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (g->gr_gid != basegid) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char **names;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes for (names = g->gr_mem; *names != NULL; ++names)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!strcmp(*names, name))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes groups[index++] = g->gr_gid;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes endgrent();
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return setgroups(index, groups);
e8f95a682820a599fe41b22977010636be5c2717jim#endif /* def QNX */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif /* def NEED_INITGROUPS */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#ifdef NEED_WAITPID
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* From ikluft@amdahl.com
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * this is not ideal but it works for SVR3 variants
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Modified by dwd@bell-labs.com to call wait3 instead of wait because
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * apache started to use the WNOHANG option.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint waitpid(pid_t pid, int *statusp, int options)
e8f95a682820a599fe41b22977010636be5c2717jim{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int tmp_pid;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (kill(pid, 0) == -1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes 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;
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jimAPI_EXPORT(int) ap_ind(const char *s, char c)
e8f95a682820a599fe41b22977010636be5c2717jim{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe register int x;
e8f95a682820a599fe41b22977010636be5c2717jim
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe for (x = 0; s[x]; x++)
e8f95a682820a599fe41b22977010636be5c2717jim if (s[x] == c)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return x;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return -1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(int) ap_rind(const char *s, char c)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes register int x;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes for (x = strlen(s) - 1; x != -1; x--)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (s[x] == c)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return x;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return -1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPI_EXPORT(void) ap_str_tolower(char *str)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*str) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *str = ap_tolower(*str);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ++str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwroweAPI_EXPORT(uid_t) ap_uname2id(const char *name)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#ifdef WIN32
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return (1);
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes#else
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes struct passwd *ent;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (name[0] == '#')
e8f95a682820a599fe41b22977010636be5c2717jim return (atoi(&name[1]));
e8f95a682820a599fe41b22977010636be5c2717jim
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (!(ent = getpwnam(name))) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "%s: bad user name %s\n", ap_server_argv0, name);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes exit(1);
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return (ent->pw_uid);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#endif
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(gid_t) ap_gname2id(const char *name)
f05787953018140838ad51456c86c965d6a86267jim{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#ifdef WIN32
f05787953018140838ad51456c86c965d6a86267jim return (1);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#else
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes struct group *ent;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
e8f95a682820a599fe41b22977010636be5c2717jim if (name[0] == '#')
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return (atoi(&name[1]));
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (!(ent = getgrnam(name))) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "%s: bad group name %s\n", ap_server_argv0, name);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes exit(1);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim return (ent->gr_gid);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#endif
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/*
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Parses a host of the form <address>[:port]
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * :port is permitted if 'port' is not NULL
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesunsigned long ap_get_virthost_addr(char *w, unsigned short *ports)
e8f95a682820a599fe41b22977010636be5c2717jim{
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* ZZZ Redesign for AP func changes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes struct hostent *hep;
e8f95a682820a599fe41b22977010636be5c2717jim unsigned long my_addr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char *p;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
e8f95a682820a599fe41b22977010636be5c2717jim p = strchr(w, ':');
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (ports != NULL) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *ports = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (p != NULL && strcmp(p + 1, "*") != 0)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *ports = atoi(p + 1);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (p != NULL)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *p = '\0';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (strcmp(w, "*") == 0) {
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener if (p != NULL)
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener *p = ':';
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener return htonl(INADDR_ANY);
342377e75b69a70c16175cce7f694472f9f8a6adjailletc }
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener my_addr = ap_inet_addr((char *)w);
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener if (my_addr != INADDR_NONE) {
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener if (p != NULL)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *p = ':';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return my_addr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes hep = gethostbyname(w);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "Cannot resolve host name %s --- exiting!\n", w);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes exit(1);
e8f95a682820a599fe41b22977010636be5c2717jim }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (hep->h_addr_list[1]) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "Host %s has multiple addresses ---\n", w);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "you must choose one explicitly for use as\n");
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe fprintf(stderr, "a virtual host. Exiting!!!\n");
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes exit(1);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (p != NULL)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *p = ':';
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener return ((struct in_addr *) (hep->h_addr))->s_addr;
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener}
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholesstatic char *find_fqdn(ap_context_t *a, struct hostent *p)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes int x;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (!strchr(p->h_name, '.')) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener for (x = 0; p->h_aliases[x]; ++x) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (strchr(p->h_aliases[x], '.') &&
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener (!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf return ap_pstrdup(a, p->h_aliases[x]);
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return NULL;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes return ap_pstrdup(a, (void *) p->h_name);
60d81cab99dccfbb0c8d378cf6aa7338be0fdb74covener}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholeschar *ap_get_local_host(ap_context_t *a)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#ifndef MAXHOSTNAMELEN
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#define MAXHOSTNAMELEN 256
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#endif
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char str[MAXHOSTNAMELEN + 1];
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char *server_hostname;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes struct hostent *p;
e8f95a682820a599fe41b22977010636be5c2717jim
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* ZZZ change to use AP funcs. */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#ifdef BEOS
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (gethostname(str, sizeof(str) - 1) == 0)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#else
e8f95a682820a599fe41b22977010636be5c2717jim if (gethostname(str, sizeof(str) - 1) != 0)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#endif
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes perror("Unable to gethostname");
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes exit(1);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe str[MAXHOSTNAMELEN] = '\0';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if ((!(p = gethostbyname(str))) || (!(server_hostname = find_fqdn(a, p)))) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "%s: cannot determine local host name.\n",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ap_server_argv0);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes fprintf(stderr, "Use the ServerName directive to set it manually.\n");
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes exit(1);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return server_hostname;
490046d2a164ad86cc63bd789f32eaed663d239abnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/* simple 'pool' alloc()ing glue to ap_base64.c
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(char *) ap_pbase64decode(ap_context_t *p, const char *bufcoded)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char *decoded;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes int l;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes decoded = (char *) ap_palloc(p, 1 + ap_base64decode_len(bufcoded));
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes l = ap_base64decode(decoded, bufcoded);
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes decoded[l] = '\0'; /* make binary sequence into string */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return decoded;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(char *) ap_pbase64encode(ap_context_t *p, char *string)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char *encoded;
e8f95a682820a599fe41b22977010636be5c2717jim int l = strlen(string);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes encoded = (char *) ap_palloc(p, 1 + ap_base64encode_len(l));
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes l = ap_base64encode(encoded, string, l);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes encoded[l] = '\0'; /* make binary sequence into string */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return encoded;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/* deprecated names for the above two functions, here for compatibility
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
e8f95a682820a599fe41b22977010636be5c2717jimAPI_EXPORT(char *) ap_uudecode(ap_context_t *p, const char *bufcoded)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return ap_pbase64decode(p, bufcoded);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesAPI_EXPORT(char *) ap_uuencode(ap_context_t *p, char *string)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return ap_pbase64encode(p, string);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes#ifdef OS2
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesvoid os2pathname(char *path)
e8f95a682820a599fe41b22977010636be5c2717jim{
e8f95a682820a599fe41b22977010636be5c2717jim char newpath[MAX_STRING_LEN];
e8f95a682820a599fe41b22977010636be5c2717jim int loop;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int offset;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe offset = 0;
e8f95a682820a599fe41b22977010636be5c2717jim for (loop = 0; loop < (strlen(path) + 1) && loop < sizeof(newpath) - 1; loop++) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (path[loop] == '/') {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes newpath[offset] = '\\';
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /*
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes offset = offset + 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes newpath[offset] = '\\';
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes */
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes }
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes else
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes newpath[offset] = path[loop];
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes offset = offset + 1;
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes };
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* Debugging code */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* fprintf(stderr, "%s \n", newpath); */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes strcpy(path, newpath);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes};
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes/* quotes in the string are doubled up.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Used to escape quotes in args passed to OS/2's cmd.exe
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholeschar *ap_double_quotes(ap_context_t *p, char *str)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int num_quotes = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int len = 0;
e8f95a682820a599fe41b22977010636be5c2717jim char *quote_doubled_str, *dest;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (str[len]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes num_quotes += str[len++] == '\"';
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes quote_doubled_str = ap_palloc(p, len + num_quotes + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes dest = quote_doubled_str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin while (*str) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*str == '\"')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *(dest++) = '\"';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *(dest++) = *(str++);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *dest = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return quote_doubled_str;
e8f95a682820a599fe41b22977010636be5c2717jim}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim#ifdef NEED_STRERROR
43c3e6a4b559b76b750c245ee95e2782c15b4296jimchar *
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes strerror(int err)
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes{
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes char *p;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes extern char *const sys_errlist[];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes p = sys_errlist[err];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (p);
185aa71728867671e105178b4c66fbc22b65ae26sf}
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf#endif
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf#if defined(NEED_DIFFTIME)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesdouble difftime(time_t time1, time_t time0)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return (time1 - time0);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
e8f95a682820a599fe41b22977010636be5c2717jim#endif
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* we want to downcase the type/subtype for comparison purposes
e8f95a682820a599fe41b22977010636be5c2717jim * but nothing else because ;parameter=foo values are case sensitive.
e8f95a682820a599fe41b22977010636be5c2717jim * XXX: in truth we want to downcase parameter names... but really,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * apache has never handled parameters and such correctly. You
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes * also need to compress spaces and such to be able to compare
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes * properly. -djg
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes */
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholesAPI_EXPORT(void) ap_content_type_tolower(char *str)
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *semi;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
f474c7d8026377782a5cf4d95af6a3443759c0b0trawick semi = strchr(str, ';');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (semi) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *semi = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*str) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *str = ap_tolower(*str);
185aa71728867671e105178b4c66fbc22b65ae26sf ++str;
851ee1e10feb6c88b7dad7572bd9b4f3d9e91fcftrawick }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (semi) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *semi = ';';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim/*
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * Given a string, replace any bare " with \" .
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
e8f95a682820a599fe41b22977010636be5c2717jimAPI_EXPORT(char *) ap_escape_quotes (ap_context_t *p, const char *instring)
e8f95a682820a599fe41b22977010636be5c2717jim{
43c3e6a4b559b76b750c245ee95e2782c15b4296jim int newlen = 0;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes const char *inchr = instring;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes char *outchr, *outstring;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes /*
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes * Look through the input string, jogging the length of the output
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * string up by an extra byte each time we find an unescaped ".
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*inchr != '\0') {
185aa71728867671e105178b4c66fbc22b65ae26sf newlen++;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf if (*inchr == '"') {
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf newlen++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If we find a slosh, and it's not the last byte in the string,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * it's escaping something - advance past both bytes.
e8f95a682820a599fe41b22977010636be5c2717jim */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if ((*inchr == '\\') && (inchr[1] != '\0')) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes inchr++;
e8f95a682820a599fe41b22977010636be5c2717jim newlen++;
e8f95a682820a599fe41b22977010636be5c2717jim }
43c3e6a4b559b76b750c245ee95e2782c15b4296jim inchr++;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes outstring = ap_palloc(p, newlen + 1);
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes inchr = instring;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes outchr = outstring;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Now copy the input string to the output string, inserting a slosh
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * in front of every " that doesn't already have one.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*inchr != '\0') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((*inchr == '\\') && (inchr[1] != '\0')) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *outchr++ = *inchr++;
185aa71728867671e105178b4c66fbc22b65ae26sf *outchr++ = *inchr++;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf }
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf if (*inchr == '"') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *outchr++ = '\\';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*inchr != '\0') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *outchr++ = *inchr++;
e8f95a682820a599fe41b22977010636be5c2717jim }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *outchr = '\0';
e8f95a682820a599fe41b22977010636be5c2717jim return outstring;
e8f95a682820a599fe41b22977010636be5c2717jim}
43c3e6a4b559b76b750c245ee95e2782c15b4296jim