proxy_util.c revision 26720422632ac831c17501d36b4151598cfe3c90
2d2eda71267231c2526be701fe655db125852c1ffielding/* ====================================================================
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * The Apache Software License, Version 1.1
f062ed7bd262a37a909dd77ce5fc23b446818823fielding *
b99dbaab171d91e1b664397cc40e039d0c087c65fielding * Copyright (c) 2000 The Apache Software Foundation. All rights
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * reserved.
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * Redistribution and use in source and binary forms, with or without
2d2eda71267231c2526be701fe655db125852c1ffielding * modification, are permitted provided that the following conditions
2d2eda71267231c2526be701fe655db125852c1ffielding * are met:
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * 1. Redistributions of source code must retain the above copyright
2d2eda71267231c2526be701fe655db125852c1ffielding * notice, this list of conditions and the following disclaimer.
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * 2. Redistributions in binary form must reproduce the above copyright
2d2eda71267231c2526be701fe655db125852c1ffielding * notice, this list of conditions and the following disclaimer in
2d2eda71267231c2526be701fe655db125852c1ffielding * the documentation and/or other materials provided with the
2d2eda71267231c2526be701fe655db125852c1ffielding * distribution.
2d2eda71267231c2526be701fe655db125852c1ffielding *
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * 3. The end-user documentation included with the redistribution,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * if any, must include the following acknowledgment:
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * "This product includes software developed by the
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * Apache Software Foundation (http://www.apache.org/)."
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * Alternately, this acknowledgment may appear in the software itself,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * if and wherever such third-party acknowledgments normally appear.
2d2eda71267231c2526be701fe655db125852c1ffielding *
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * 4. The names "Apache" and "Apache Software Foundation" must
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * not be used to endorse or promote products derived from this
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * software without prior written permission. For written
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * permission, please contact apache@apache.org.
2d2eda71267231c2526be701fe655db125852c1ffielding *
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * 5. Products derived from this software may not be called "Apache",
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * nor may "Apache" appear in their name, without prior written
64185f9824e42f21ca7b9ae6c004484215c031a7rbb * permission of the Apache Software Foundation.
2d2eda71267231c2526be701fe655db125852c1ffielding *
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
2d2eda71267231c2526be701fe655db125852c1ffielding * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * SUCH DAMAGE.
2d2eda71267231c2526be701fe655db125852c1ffielding * ====================================================================
2d2eda71267231c2526be701fe655db125852c1ffielding *
2d2eda71267231c2526be701fe655db125852c1ffielding * This software consists of voluntary contributions made by many
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * individuals on behalf of the Apache Software Foundation. For more
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * information on the Apache Software Foundation, please see
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * <http://www.apache.org/>.
2d2eda71267231c2526be701fe655db125852c1ffielding *
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * Portions of this software are based upon public domain software
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * originally written at the National Center for Supercomputing Applications,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * University of Illinois, Urbana-Champaign.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding/* Utility routines for Apache proxy */
2d2eda71267231c2526be701fe655db125852c1ffielding#include "mod_proxy.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "http_main.h"
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb#include "http_log.h"
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe#include "util_uri.h"
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb#include "util_date.h" /* get ap_checkmask() decl. */
92f3af936ce61f25358a3ee4f28df2f6d62040dfdreid#include "apr_md5.h"
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
c9a95767fbf0f5fb0976a06b97a256033925e433rbbstatic int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fieldingstatic int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int proxy_match_word(struct dirconn_entry *This, request_rec *r);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic struct per_thread_data *get_per_thread_data(void);
2d2eda71267231c2526be701fe655db125852c1ffielding/* already called in the knowledge that the characters are hex digits */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbint ap_proxy_hex2c(const char *x)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int i, ch;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
2d2eda71267231c2526be701fe655db125852c1ffielding#ifndef CHARSET_EBCDIC
2d2eda71267231c2526be701fe655db125852c1ffielding ch = x[0];
2d2eda71267231c2526be701fe655db125852c1ffielding if (apr_isdigit(ch))
2d2eda71267231c2526be701fe655db125852c1ffielding i = ch - '0';
2d2eda71267231c2526be701fe655db125852c1ffielding else if (apr_isupper(ch))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb i = ch - ('A' - 10);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb i = ch - ('a' - 10);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb i <<= 4;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
2d2eda71267231c2526be701fe655db125852c1ffielding ch = x[1];
2d2eda71267231c2526be701fe655db125852c1ffielding if (apr_isdigit(ch))
bfb62a96023822c56c9120e4ee627d4091cc59c2rbb i += ch - '0';
bfb62a96023822c56c9120e4ee627d4091cc59c2rbb else if (apr_isupper(ch))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb i += ch - ('A' - 10);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb i += ch - ('a' - 10);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return i;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#else /*CHARSET_EBCDIC*/
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return (1 == sscanf(x, "%2x", &i)) ? os_toebcdic[i&0xFF] : 0;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe#endif /*CHARSET_EBCDIC*/
2d2eda71267231c2526be701fe655db125852c1ffielding}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbbvoid ap_proxy_c2hex(int ch, char *x)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#ifndef CHARSET_EBCDIC
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int i;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb x[0] = '%';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb i = (ch & 0xF0) >> 4;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (i >= 10)
2d2eda71267231c2526be701fe655db125852c1ffielding x[1] = ('A' - 10) + i;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe else
2d2eda71267231c2526be701fe655db125852c1ffielding x[1] = '0' + i;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding i = ch & 0x0F;
2d2eda71267231c2526be701fe655db125852c1ffielding if (i >= 10)
000b67449410515eac43e76ef6667915bfd4d2abgstein x[2] = ('A' - 10) + i;
2d2eda71267231c2526be701fe655db125852c1ffielding else
2d2eda71267231c2526be701fe655db125852c1ffielding x[2] = '0' + i;
2d2eda71267231c2526be701fe655db125852c1ffielding#else /*CHARSET_EBCDIC*/
61fd0cab072a05b855cbef9c585702401ac5ae29rbb static const char ntoa[] = { "0123456789ABCDEF" };
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ch &= 0xFF;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb x[0] = '%';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb x[1] = ntoa[(os_toascii[ch]>>4)&0x0F];
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein x[2] = ntoa[os_toascii[ch]&0x0F];
61fd0cab072a05b855cbef9c585702401ac5ae29rbb x[3] = '\0';
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein#endif /*CHARSET_EBCDIC*/
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/*
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * canonicalise a URL-encoded string
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/*
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * Convert a URL-encoded string to canonical form.
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein * It decodes characters which need not be encoded,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * and encodes those which must be encoded, and does not touch
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * those which must not be touched.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbchar *ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int isenc)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int i, j, ch;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *y;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe char *allowed; /* characters which should not be encoded */
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein char *reserved; /* characters which much not be en/de-coded */
c9a95767fbf0f5fb0976a06b97a256033925e433rbb
c9a95767fbf0f5fb0976a06b97a256033925e433rbb/* N.B. in addition to :@&=, this allows ';' in an http path
c9a95767fbf0f5fb0976a06b97a256033925e433rbb * and '?' in an ftp path -- this may be revised
c9a95767fbf0f5fb0976a06b97a256033925e433rbb *
c9a95767fbf0f5fb0976a06b97a256033925e433rbb * Also, it makes a '+' character in a search string reserved, as
c9a95767fbf0f5fb0976a06b97a256033925e433rbb * it may be form-encoded. (Although RFC 1738 doesn't allow this -
c9a95767fbf0f5fb0976a06b97a256033925e433rbb * it only permits ; / ? : @ = & as reserved chars.)
c9a95767fbf0f5fb0976a06b97a256033925e433rbb */
c9a95767fbf0f5fb0976a06b97a256033925e433rbb if (t == enc_path)
c9a95767fbf0f5fb0976a06b97a256033925e433rbb allowed = "$-_.+!*'(),;:@&=";
c9a95767fbf0f5fb0976a06b97a256033925e433rbb else if (t == enc_search)
c9a95767fbf0f5fb0976a06b97a256033925e433rbb allowed = "$-_.!*'(),;:@&=";
c9a95767fbf0f5fb0976a06b97a256033925e433rbb else if (t == enc_user)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb allowed = "$-_.+!*'(),;@&=";
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else if (t == enc_fpath)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb allowed = "$-_.+!*'(),?:@&=";
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else /* if (t == enc_parm) */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb allowed = "$-_.+!*'(),?/:@&=";
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (t == enc_path)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb reserved = "/";
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else if (t == enc_search)
3d96ee83babeec32482c9082c9426340cee8c44dwrowe reserved = "+";
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb reserved = "";
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb y = apr_palloc(p, 3 * len + 1);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (i = 0, j = 0; i < len; i++, j++) {
3d96ee83babeec32482c9082c9426340cee8c44dwrowe/* always handle '/' first */
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein ch = x[i];
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (strchr(reserved, ch)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb y[j] = ch;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb continue;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* decode it if not already done */
3d96ee83babeec32482c9082c9426340cee8c44dwrowe if (isenc && ch == '%') {
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein if (!ap_isxdigit(x[i + 1]) || !ap_isxdigit(x[i + 2]))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return NULL;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ch = ap_proxy_hex2c(&x[i + 1]);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb i += 2;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (ch != 0 && strchr(reserved, ch)) { /* keep it encoded */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ap_proxy_c2hex(ch, &y[j]);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb j += 2;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb continue;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
2d2eda71267231c2526be701fe655db125852c1ffielding/* recode it, if necessary */
2d2eda71267231c2526be701fe655db125852c1ffielding if (!apr_isalnum(ch) && !strchr(allowed, ch)) {
2d2eda71267231c2526be701fe655db125852c1ffielding ap_proxy_c2hex(ch, &y[j]);
2d2eda71267231c2526be701fe655db125852c1ffielding j += 2;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding else
2d2eda71267231c2526be701fe655db125852c1ffielding y[j] = ch;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding y[j] = '\0';
2d2eda71267231c2526be701fe655db125852c1ffielding return y;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/*
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Parses network-location.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * urlp on input the URL; on output the path, after the leading /
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * user NULL if no user/password permitted
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * password holder for password
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * host holder for host
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * port port number; only set if one is supplied.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Returns an error string.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
3d96ee83babeec32482c9082c9426340cee8c44dwrowechar *
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
7bdef86e15d47d16dcbe7a5611683191774bd5fbgstein char **passwordp, char **hostp, int *port)
23ff73a56371e21f16744cb94d06399b877829f1rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int i;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *strp, *host, *url = *urlp;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *user = NULL, *password = NULL;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (url[0] != '/' || url[1] != '/')
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return "Malformed URL";
61fd0cab072a05b855cbef9c585702401ac5ae29rbb host = url + 2;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb url = strchr(host, '/');
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (url == NULL)
3d96ee83babeec32482c9082c9426340cee8c44dwrowe url = "";
2d2eda71267231c2526be701fe655db125852c1ffielding else
23ff73a56371e21f16744cb94d06399b877829f1rbb *(url++) = '\0'; /* skip seperating '/' */
2d2eda71267231c2526be701fe655db125852c1ffielding
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar /* find _last_ '@' since it might occur in user/password part */
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar strp = strrchr(host, '@');
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (strp != NULL) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar *strp = '\0';
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar user = host;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar host = strp + 1;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar/* find password */
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar strp = strchr(user, ':');
3d96ee83babeec32482c9082c9426340cee8c44dwrowe if (strp != NULL) {
3d96ee83babeec32482c9082c9426340cee8c44dwrowe *strp = '\0';
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar password = ap_proxy_canonenc(p, strp + 1, strlen(strp + 1), enc_user, 1);
3d96ee83babeec32482c9082c9426340cee8c44dwrowe if (password == NULL)
a19698aebe10b9d41574e4a73794ba7d4cecd78btrawick return "Bad %-escape in URL (password)";
a19698aebe10b9d41574e4a73794ba7d4cecd78btrawick }
a19698aebe10b9d41574e4a73794ba7d4cecd78btrawick
3d96ee83babeec32482c9082c9426340cee8c44dwrowe user = ap_proxy_canonenc(p, user, strlen(user), enc_user, 1);
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (user == NULL)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return "Bad %-escape in URL (username)";
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (userp != NULL) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar *userp = user;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (passwordp != NULL) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar *passwordp = password;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar strp = strrchr(host, ':');
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (strp != NULL) {
3d96ee83babeec32482c9082c9426340cee8c44dwrowe *(strp++) = '\0';
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar for (i = 0; strp[i] != '\0'; i++)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (!apr_isdigit(strp[i]))
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar break;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar /* if (i == 0) the no port was given; keep default */
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (strp[i] != '\0') {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return "Bad port number in URL";
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar } else if (i > 0) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar *port = atoi(strp);
3d96ee83babeec32482c9082c9426340cee8c44dwrowe if (*port > 65535)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return "Port number in URL > 65535";
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar ap_str_tolower(host); /* DNS names are case-insensitive */
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (*host == '\0')
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return "Missing host in URL";
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar/* check hostname syntax */
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar for (i = 0; host[i] != '\0'; i++)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (!apr_isdigit(host[i]) && host[i] != '.')
3d96ee83babeec32482c9082c9426340cee8c44dwrowe break;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar /* must be an IP address */
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar#if defined(WIN32) || defined(NETWARE) || defined(TPF) || defined(BEOS)
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (host[i] == '\0' && (inet_addr(host) == -1))
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar#else
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (host[i] == '\0' && (ap_inet_addr(host) == -1 || inet_network(host) == -1))
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar#endif
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar return "Bad IP address in URL";
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar/* if (strchr(host,'.') == NULL && domain != NULL)
2d2eda71267231c2526be701fe655db125852c1ffielding host = pstrcat(p, host, domain, NULL);
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding *urlp = url;
2d2eda71267231c2526be701fe655db125852c1ffielding *hostp = host;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return NULL;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic const char * const lwday[7] =
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowe/*
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein * If the date is a valid RFC 850 date or asctime() date, then it
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * is converted to the RFC 1123 format, otherwise it is not modified.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * This routine is not very fast at doing conversions, as it uses
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * sscanf and sprintf. However, if the date is already correctly
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * formatted, then it exits very quickly.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbconst char *
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ap_proxy_date_canon(apr_pool_t *p, const char *x1)
3d96ee83babeec32482c9082c9426340cee8c44dwrowe{
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein char *x = apr_pstrdup(p, x1);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int wk, mday, year, hour, min, sec, mon;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *q, month[4], zone[4], week[4];
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb q = strchr(x, ',');
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* check for RFC 850 date */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (q != NULL && q - x > 3 && q[1] == ' ') {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *q = '\0';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (wk = 0; wk < 7; wk++)
3d96ee83babeec32482c9082c9426340cee8c44dwrowe if (strcmp(x, lwday[wk]) == 0)
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein break;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *q = ',';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (wk == 7)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return x; /* not a valid date */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (q[4] != '-' || q[8] != '-' || q[11] != ' ' || q[14] != ':' ||
61fd0cab072a05b855cbef9c585702401ac5ae29rbb q[17] != ':' || strcmp(&q[20], " GMT") != 0)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return x;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (sscanf(q + 2, "%u-%3s-%u %u:%u:%u %3s", &mday, month, &year,
3d96ee83babeec32482c9082c9426340cee8c44dwrowe &hour, &min, &sec, zone) != 7)
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein return x;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (year < 70)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb year += 2000;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb year += 1900;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* check for acstime() date */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (x[3] != ' ' || x[7] != ' ' || x[10] != ' ' || x[13] != ':' ||
3d96ee83babeec32482c9082c9426340cee8c44dwrowe x[16] != ':' || x[19] != ' ' || x[24] != '\0')
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein return x;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (sscanf(x, "%3s %3s %u %u:%u:%u %u", week, month, &mday, &hour,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb &min, &sec, &year) != 7)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return x;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (wk = 0; wk < 7; wk++)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (strcmp(week, ap_day_snames[wk]) == 0)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb break;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (wk == 7)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return x;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
2d2eda71267231c2526be701fe655db125852c1ffielding
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* check date */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (mon = 0; mon < 12; mon++)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (strcmp(month, ap_month_snames[mon]) == 0)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb break;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (mon == 12)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return x;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
2d2eda71267231c2526be701fe655db125852c1ffielding q = apr_palloc(p, 30);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb apr_snprintf(q, 30, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[wk],
2d2eda71267231c2526be701fe655db125852c1ffielding mday, ap_month_snames[mon], year, hour, min, sec);
2d2eda71267231c2526be701fe655db125852c1ffielding return q;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
2d2eda71267231c2526be701fe655db125852c1ffielding/* NOTE: This routine is taken from http_protocol::getline()
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * because the old code found in the proxy module was too
2d2eda71267231c2526be701fe655db125852c1ffielding * difficult to understand and maintain.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
d839a9822ee53ce00da24c15f2d9fe054233d342gstein/* Get a line of protocol input, including any continuation lines
d839a9822ee53ce00da24c15f2d9fe054233d342gstein * caused by MIME folding (or broken clients) if fold != 0, and place it
d839a9822ee53ce00da24c15f2d9fe054233d342gstein * in the buffer s, of size n bytes, without the ending newline.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Returns -1 on error, or the length of s.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *
d839a9822ee53ce00da24c15f2d9fe054233d342gstein * Note: Because bgets uses 1 char for newline and 1 char for NUL,
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * the most we can get is (n - 2) actual characters if it
d839a9822ee53ce00da24c15f2d9fe054233d342gstein * was ended by a newline, or (n - 1) characters if the line
2d2eda71267231c2526be701fe655db125852c1ffielding * length exceeded (n - 1). So, if the result == (n - 1),
2d2eda71267231c2526be701fe655db125852c1ffielding * then the actual input line exceeded the buffer length,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * and it would be a good idea for the caller to puke 400 or 414.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic int proxy_getline(char *s, int n, BUFF *in, int fold)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *pos, next;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int retval;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int total = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb pos = s;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb do {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb retval = ap_bgets(pos, n, in); /* retval == -1 if error, 0 if EOF */
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein if (retval <= 0)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return ((retval < 0) && (total == 0)) ? -1 : total;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* retval is the number of characters read, not including NUL */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb n -= retval; /* Keep track of how much of s is full */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb pos += (retval - 1); /* and where s ends */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb total += retval; /* and how long s has become */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (*pos == '\n') { /* Did we get a full line of input? */
3d96ee83babeec32482c9082c9426340cee8c44dwrowe *pos = '\0';
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein --total;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ++n;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return total; /* if not, input line exceeded buffer size */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Continue appending if line folding is desired and
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * the last line was not empty and we have room in the buffer and
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * the next line begins with a continuation character.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb } while (fold && (retval != 1) && (n > 1)
3d96ee83babeec32482c9082c9426340cee8c44dwrowe && (next = ap_blookc(in))
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein && ((next == ' ') || (next == '\t')));
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return total;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/*
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Reads headers from a buffer and returns an array of headers.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Returns NULL on file error
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * This routine tries to deal with too long lines and continuation lines.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * @@@: XXX: FIXME: currently the headers are passed thru un-merged.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Is that okay, or should they be collapsed where possible?
3d96ee83babeec32482c9082c9426340cee8c44dwrowe */
2d2eda71267231c2526be701fe655db125852c1ffieldingapr_table_t *ap_proxy_read_headers(request_rec *r, char *buffer, int size, BUFF *f)
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb apr_table_t *resp_hdrs;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int len;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char *value, *end;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char field[MAX_STRING_LEN];
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb resp_hdrs = ap_make_table(r->pool, 20);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowe /*
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein * Read header lines until we get the empty separator line, a read error,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * the connection closes (EOF), or we timeout.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while ((len = proxy_getline(buffer, size, f, 1)) > 0) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (!(value = strchr(buffer, ':'))) { /* Find the colon separator */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Buggy MS IIS servers sometimes return invalid headers
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * (an extra "HTTP/1.0 200, OK" line sprinkled in between
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein * the usual MIME headers). Try to deal with it in a sensible
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * way, but log the fact.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * XXX: The mask check is buggy if we ever see an HTTP/1.10 */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (!ap_checkmask(buffer, "HTTP/#.# ###*")) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Nope, it wasn't even an extra HTTP header. Give up. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb return NULL;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r->server,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "proxy: Ignoring duplicate HTTP header "
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "returned by %s (%s)", r->uri, r->method);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb continue;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *value = '\0';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ++value;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* XXX: RFC2068 defines only SP and HT as whitespace, this test is
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * wrong... and so are many others probably.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while (apr_isspace(*value))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ++value; /* Skip to start of value */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
3d96ee83babeec32482c9082c9426340cee8c44dwrowe /* should strip trailing whitespace as well */
2d2eda71267231c2526be701fe655db125852c1ffielding for (end = &value[strlen(value)-1]; end > value && apr_isspace(*end); --end)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *end = '\0';
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ap_table_add(resp_hdrs, buffer, value);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* the header was too long; at the least we should skip extra data */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (len >= size - 1) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb while ((len = proxy_getline(field, MAX_STRING_LEN, f, 1))
61fd0cab072a05b855cbef9c585702401ac5ae29rbb >= MAX_STRING_LEN - 1) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* soak up the extra data */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (len == 0) /* time to exit the larger loop as well */
3d96ee83babeec32482c9082c9426340cee8c44dwrowe break;
2d2eda71267231c2526be701fe655db125852c1ffielding }
e351a4349a3dcc2e8d9c27bcdf72414bdde0942frbb }
e351a4349a3dcc2e8d9c27bcdf72414bdde0942frbb return resp_hdrs;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb}
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
2d2eda71267231c2526be701fe655db125852c1ffieldinglong int ap_proxy_send_fb(proxy_completion *completion, apr_socket_t *f, request_rec *r, ap_cache_el *c)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb{
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int ok;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb char buf[IOBUFSIZE];
2d2eda71267231c2526be701fe655db125852c1ffielding long total_bytes_rcvd, in_buffer;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe apr_ssize_t cntr;
2d2eda71267231c2526be701fe655db125852c1ffielding register int n, o;
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar conn_rec *con = r->connection;
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar int alternate_timeouts = 1; /* 1 if we alternate between soft & hard timeouts */
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar apr_file_t *cachefp = NULL;
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar int written = 0, wrote_to_cache;
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar total_bytes_rcvd = 0;
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar if (c) ap_cache_el_data(c, &cachefp);
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
344f3bc38dfccf6261d5bb8d689794cde113b3d6coar#if 0
d5df46b7972c0c4a5ca0ba5068e238f6053c2e6ftrawick#ifdef CHARSET_EBCDIC
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding /* The cache copy is ASCII, not EBCDIC, even for text/html) */
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding ap_bsetflag(f, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0);
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding if (c != NULL && c->fp != NULL)
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding ap_bsetflag(c->fp, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0);
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding ap_bsetflag(con->client, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0);
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#endif
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#endif
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Since we are reading from one buffer and writing to another,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * it is unsafe to do a soft_timeout here, at least until the proxy
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * has its own timeout handler which can set both buffers to EOUT.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm#if defined(WIN32) || defined(TPF) || defined(NETWARE)
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein /* works fine under win32, so leave it */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb alternate_timeouts = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#else
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* CHECKME! Since hard_timeout won't work in unix on sends with partial
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * cache completion, we have to alternate between hard_timeout
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * for reads, and soft_timeout for send. This is because we need
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * to get a return from ap_bwrite to be able to continue caching.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * BUT, if we *can't* continue anyway, just use hard_timeout.
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm * (Also, if no cache file is written, use hard timeouts)
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (!completion || completion->content_length > 0
61fd0cab072a05b855cbef9c585702401ac5ae29rbb || completion->cache_completion == 1.0) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb alternate_timeouts = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb }
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#endif
61fd0cab072a05b855cbef9c585702401ac5ae29rbb
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm /* Loop and ap_bread() while we can successfully read and write,
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein * or (after the client aborted) while we can successfully
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * read and finish the configured cache_completion.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (ok = 1; ok; cntr = 0) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Read block from server */
dad234382d8424e1c5a30af2838e172aec9d6d1bdreid cntr = IOBUFSIZE;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (apr_recv(f, buf, &cntr) != APR_SUCCESS && !cntr)
42ce672c516baf6e4eaed18ccc1647de2d456d8edougm {
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding if (c != NULL) {
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein "proxy: error reading from %s", c->name);
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb ap_proxy_cache_error(&c);
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb }
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb break;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb }
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb else if(cntr == 0) break;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb /* Write to cache first. */
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb /*@@@ XXX FIXME: Assuming that writing the cache file won't time out?!!? */
8b7047e519340545e6807c9749576a40a76b6d3frbb wrote_to_cache = cntr;
8b7047e519340545e6807c9749576a40a76b6d3frbb if (cachefp && apr_write(cachefp, &buf[0], &wrote_to_cache) != APR_SUCCESS) {
8b7047e519340545e6807c9749576a40a76b6d3frbb ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
6d00a43be9ab145c89e8cc22bad59e3aa746f761jwoolley "proxy: error writing to cache");
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb ap_proxy_cache_error(&c);
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb } else {
9484ae61c7cc5fa8d8d9a836efdbdb1e88d3036dwrowe written += n;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb }
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb o = 0;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb total_bytes_rcvd += cntr;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb in_buffer = cntr;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb /* Write the block to the client, detect aborted transfers */
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb while (!con->aborted && in_buffer > 0) {
561e5f16a2f9fb397aac4c283aaa87a752520a4ddougm cntr = in_buffer;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb if (apr_send(con->client_socket, &buf[o], &cntr) != APR_SUCCESS) {
561e5f16a2f9fb397aac4c283aaa87a752520a4ddougm if (completion) {
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb /* when a send failure occurs, we need to decide
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb * whether to continue loading and caching the
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb * document, or to abort the whole thing
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb */
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb ok = (completion->content_length > 0) &&
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb (completion->cache_completion > 0) &&
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb (completion->content_length * completion->cache_completion < total_bytes_rcvd);
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb
561e5f16a2f9fb397aac4c283aaa87a752520a4ddougm if (!ok)
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb ap_proxy_cache_error(&c);
561e5f16a2f9fb397aac4c283aaa87a752520a4ddougm }
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb con->aborted = 1;
fcc25eda7b150e226d3c1cdaea66a943d3fdee4erbb break;
c9a95767fbf0f5fb0976a06b97a256033925e433rbb }
c9a95767fbf0f5fb0976a06b97a256033925e433rbb in_buffer -= cntr;
c9a95767fbf0f5fb0976a06b97a256033925e433rbb o += cntr;
c9a95767fbf0f5fb0976a06b97a256033925e433rbb } /* while client alive and more data to send */
c9a95767fbf0f5fb0976a06b97a256033925e433rbb } /* loop and ap_bread while "ok" */
c9a95767fbf0f5fb0976a06b97a256033925e433rbb
c9a95767fbf0f5fb0976a06b97a256033925e433rbb/* Remove this stuff, because flushing a socket doesn't make a lot of sense
c9a95767fbf0f5fb0976a06b97a256033925e433rbb * currently.
c9a95767fbf0f5fb0976a06b97a256033925e433rbb if (!con->aborted)
c9a95767fbf0f5fb0976a06b97a256033925e433rbb ap_bflush(con->client);
c9a95767fbf0f5fb0976a06b97a256033925e433rbb*/
c9a95767fbf0f5fb0976a06b97a256033925e433rbb
c9a95767fbf0f5fb0976a06b97a256033925e433rbb return total_bytes_rcvd;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding/*
2d2eda71267231c2526be701fe655db125852c1ffielding * Sends response line and headers. Uses the client fd and the
2d2eda71267231c2526be701fe655db125852c1ffielding * headers_out array from the passed request_rec to talk to the client
* and to properly set the headers it sends for things such as logging.
*
* A timeout should be set before calling this routine.
*/
void ap_proxy_send_headers(request_rec *r, const char *respline, apr_table_t *t)
{
int i;
apr_socket_t *fp = r->connection->client_socket;
apr_table_entry_t *elts = (apr_table_entry_t *) apr_table_elts(t)->elts;
char *temp = apr_pstrcat(r->pool, respline, CRLF, NULL);
apr_size_t len = strlen(temp);
apr_send(fp, temp, &len);
for (i = 0; i < ap_table_elts(t)->nelts; ++i) {
if (elts[i].key != NULL) {
temp = apr_pstrcat(r->pool, elts[i].key, ": ", elts[i].val, CRLF, NULL);
apr_send(fp, temp, &len);
apr_table_addn(r->headers_out, elts[i].key, elts[i].val);
}
}
len = 2;
apr_send(fp, CRLF, &len);
}
/*
* list is a comma-separated list of case-insensitive tokens, with
* optional whitespace around the tokens.
* The return returns 1 if the token val is found in the list, or 0
* otherwise.
*/
int ap_proxy_liststr(const char *list, const char *val)
{
int len, i;
const char *p;
len = strlen(val);
while (list != NULL) {
p = ap_strchr_c(list, ',');
if (p != NULL) {
i = p - list;
do
p++;
while (apr_isspace(*p));
}
else
i = strlen(list);
while (i > 0 && apr_isspace(list[i - 1]))
i--;
if (i == len && strncasecmp(list, val, len) == 0)
return 1;
list = p;
}
return 0;
}
/*
* Converts 8 hex digits to a time integer
*/
int ap_proxy_hex2sec(const char *x)
{
int i, ch;
unsigned int j;
for (i = 0, j = 0; i < 8; i++) {
ch = x[i];
j <<= 4;
if (apr_isdigit(ch))
j |= ch - '0';
else if (apr_isupper(ch))
j |= ch - ('A' - 10);
else
j |= ch - ('a' - 10);
}
if (j == 0xffffffff)
return -1; /* so that it works with 8-byte ints */
else
return j;
}
/*
* Converts a time integer to 8 hex digits
*/
void ap_proxy_sec2hex(int t, char *y)
{
int i, ch;
unsigned int j = t;
for (i = 7; i >= 0; i--) {
ch = j & 0xF;
j >>= 4;
if (ch >= 10)
y[i] = ch + ('A' - 10);
else
y[i] = ch + '0';
}
y[8] = '\0';
}
void ap_proxy_cache_error(ap_cache_el **c)
{
if (c && *c) {
const char *name = (*c)->name;
ap_cache_el_finalize((*c));
ap_cache_remove((*c)->cache, name);
*c = NULL;
}
}
int ap_proxyerror(request_rec *r, int statuscode, const char *message)
{
apr_table_setn(r->notes, "error-notes",
apr_pstrcat(r->pool,
"The proxy server could not handle the request "
"<EM><A HREF=\"", ap_escape_uri(r->pool, r->uri),
"\">", ap_escape_html(r->pool, r->method),
"&nbsp;",
ap_escape_html(r->pool, r->uri), "</A></EM>.<P>\n"
"Reason: <STRONG>",
ap_escape_html(r->pool, message),
"</STRONG>", NULL));
/* Allow "error-notes" string to be printed by ap_send_error_response() */
apr_table_setn(r->notes, "verbose-error-to", apr_pstrdup(r->pool, "*"));
r->status_line = apr_psprintf(r->pool, "%3.3u Proxy Error", statuscode);
return statuscode;
}
/*
* This routine returns its own error message
*/
const char *ap_proxy_host2addr(const char *host, struct hostent *reqhp)
{
int i;
struct hostent *hp;
struct per_thread_data *ptd = get_per_thread_data();
for (i = 0; host[i] != '\0'; i++)
if (!apr_isdigit(host[i]) && host[i] != '.')
break;
if (host[i] != '\0') {
hp = gethostbyname(host);
if (hp == NULL)
return "Host not found";
}
else {
ptd->ipaddr = ap_inet_addr(host);
hp = gethostbyaddr((char *) &ptd->ipaddr, sizeof(ptd->ipaddr), AF_INET);
if (hp == NULL) {
memset(&ptd->hpbuf, 0, sizeof(ptd->hpbuf));
ptd->hpbuf.h_name = 0;
ptd->hpbuf.h_addrtype = AF_INET;
ptd->hpbuf.h_length = sizeof(ptd->ipaddr);
ptd->hpbuf.h_addr_list = ptd->charpbuf;
ptd->hpbuf.h_addr_list[0] = (char *) &ptd->ipaddr;
ptd->hpbuf.h_addr_list[1] = 0;
hp = &ptd->hpbuf;
}
}
*reqhp = *hp;
return NULL;
}
static const char *
proxy_get_host_of_request(request_rec *r)
{
char *url, *user = NULL, *password = NULL, *err, *host;
int port = -1;
if (r->hostname != NULL)
return r->hostname;
/* Set url to the first char after "scheme://" */
if ((url = strchr(r->uri, ':')) == NULL
|| url[1] != '/' || url[2] != '/')
return NULL;
url = apr_pstrdup(r->pool, &url[1]); /* make it point to "//", which is what proxy_canon_netloc expects */
err = ap_proxy_canon_netloc(r->pool, &url, &user, &password, &host, &port);
if (err != NULL)
ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,
"%s", err);
r->hostname = host;
return host; /* ought to return the port, too */
}
/* Return TRUE if addr represents an IP address (or an IP network address) */
int ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p)
{
const char *addr = This->name;
long ip_addr[4];
int i, quads;
long bits;
/* if the address is given with an explicit netmask, use that */
/* Due to a deficiency in ap_inet_addr(), it is impossible to parse */
/* "partial" addresses (with less than 4 quads) correctly, i.e. */
/* 192.168.123 is parsed as 192.168.0.123, which is not what I want. */
/* I therefore have to parse the IP address manually: */
/*if (proxy_readmask(This->name, &This->addr.s_addr, &This->mask.s_addr) == 0) */
/* addr and mask were set by proxy_readmask() */
/*return 1; */
/* Parse IP addr manually, optionally allowing */
/* abbreviated net addresses like 192.168. */
/* Iterate over up to 4 (dotted) quads. */
for (quads = 0; quads < 4 && *addr != '\0'; ++quads) {
char *tmp;
if (*addr == '/' && quads > 0) /* netmask starts here. */
break;
if (!apr_isdigit(*addr))
return 0; /* no digit at start of quad */
ip_addr[quads] = strtol(addr, &tmp, 0);
if (tmp == addr) /* expected a digit, found something else */
return 0;
if (ip_addr[quads] < 0 || ip_addr[quads] > 255) {
/* invalid octet */
return 0;
}
addr = tmp;
if (*addr == '.' && quads != 3)
++addr; /* after the 4th quad, a dot would be illegal */
}
for (This->addr.s_addr = 0, i = 0; i < quads; ++i)
This->addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
if (addr[0] == '/' && apr_isdigit(addr[1])) { /* net mask follows: */
char *tmp;
++addr;
bits = strtol(addr, &tmp, 0);
if (tmp == addr) /* expected a digit, found something else */
return 0;
addr = tmp;
if (bits < 0 || bits > 32) /* netmask must be between 0 and 32 */
return 0;
}
else {
/* Determine (i.e., "guess") netmask by counting the */
/* number of trailing .0's; reduce #quads appropriately */
/* (so that 192.168.0.0 is equivalent to 192.168.) */
while (quads > 0 && ip_addr[quads - 1] == 0)
--quads;
/* "IP Address should be given in dotted-quad form, optionally followed by a netmask (e.g., 192.168.111.0/24)"; */
if (quads < 1)
return 0;
/* every zero-byte counts as 8 zero-bits */
bits = 8 * quads;
if (bits != 32) /* no warning for fully qualified IP address */
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Warning: NetMask not supplied with IP-Addr; guessing: %s/%ld\n",
inet_ntoa(This->addr), bits);
}
This->mask.s_addr = htonl(INADDR_NONE << (32 - bits));
if (*addr == '\0' && (This->addr.s_addr & ~This->mask.s_addr) != 0) {
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Warning: NetMask and IP-Addr disagree in %s/%ld\n",
inet_ntoa(This->addr), bits);
This->addr.s_addr &= This->mask.s_addr;
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
" Set to %s/%ld\n",
inet_ntoa(This->addr), bits);
}
if (*addr == '\0') {
This->matcher = proxy_match_ipaddr;
return 1;
}
else
return (*addr == '\0'); /* okay iff we've parsed the whole string */
}
/* Return TRUE if addr represents an IP address (or an IP network address) */
static int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r)
{
int i;
int ip_addr[4];
struct in_addr addr;
struct in_addr *ip_list;
char **ip_listptr;
const char *found;
const char *host = proxy_get_host_of_request(r);
if (host == NULL) /* oops! */
return 0;
memset(&addr, '\0', sizeof addr);
memset(ip_addr, '\0', sizeof ip_addr);
if (4 == sscanf(host, "%d.%d.%d.%d", &ip_addr[0], &ip_addr[1], &ip_addr[2], &ip_addr[3])) {
for (addr.s_addr = 0, i = 0; i < 4; ++i)
addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
if (This->addr.s_addr == (addr.s_addr & This->mask.s_addr)) {
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"1)IP-Match: %s[%s] <-> ", host, inet_ntoa(addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s/", inet_ntoa(This->addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s", inet_ntoa(This->mask));
#endif
return 1;
}
#if DEBUGGING
else {
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"1)IP-NoMatch: %s[%s] <-> ", host, inet_ntoa(addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s/", inet_ntoa(This->addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s", inet_ntoa(This->mask));
}
#endif
}
else {
struct hostent the_host;
memset(&the_host, '\0', sizeof the_host);
found = ap_proxy_host2addr(host, &the_host);
if (found != NULL) {
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"2)IP-NoMatch: hostname=%s msg=%s", host, found);
#endif
return 0;
}
if (the_host.h_name != NULL)
found = the_host.h_name;
else
found = host;
/* Try to deal with multiple IP addr's for a host */
for (ip_listptr = the_host.h_addr_list; *ip_listptr; ++ip_listptr) {
ip_list = (struct in_addr *) *ip_listptr;
if (This->addr.s_addr == (ip_list->s_addr & This->mask.s_addr)) {
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"3)IP-Match: %s[%s] <-> ", found, inet_ntoa(*ip_list));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s/", inet_ntoa(This->addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s", inet_ntoa(This->mask));
#endif
return 1;
}
#if DEBUGGING
else {
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"3)IP-NoMatch: %s[%s] <-> ", found, inet_ntoa(*ip_list));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s/", inet_ntoa(This->addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"%s", inet_ntoa(This->mask));
}
#endif
}
}
return 0;
}
/* Return TRUE if addr represents a domain name */
int ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p)
{
char *addr = This->name;
int i;
/* Domain name must start with a '.' */
if (addr[0] != '.')
return 0;
/* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
for (i = 0; apr_isalnum(addr[i]) || addr[i] == '-' || addr[i] == '.'; ++i)
continue;
#if 0
if (addr[i] == ':') {
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"@@@@ handle optional port in proxy_is_domainname()");
/* @@@@ handle optional port */
}
#endif
if (addr[i] != '\0')
return 0;
/* Strip trailing dots */
for (i = strlen(addr) - 1; i > 0 && addr[i] == '.'; --i)
addr[i] = '\0';
This->matcher = proxy_match_domainname;
return 1;
}
/* Return TRUE if host "host" is in domain "domain" */
static int proxy_match_domainname(struct dirconn_entry *This, request_rec *r)
{
const char *host = proxy_get_host_of_request(r);
int d_len = strlen(This->name), h_len;
if (host == NULL) /* some error was logged already */
return 0;
h_len = strlen(host);
/* @@@ do this within the setup? */
/* Ignore trailing dots in domain comparison: */
while (d_len > 0 && This->name[d_len - 1] == '.')
--d_len;
while (h_len > 0 && host[h_len - 1] == '.')
--h_len;
return h_len > d_len
&& strncasecmp(&host[h_len - d_len], This->name, d_len) == 0;
}
/* Return TRUE if addr represents a host name */
int ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p)
{
struct hostent host;
char *addr = This->name;
int i;
/* Host names must not start with a '.' */
if (addr[0] == '.')
return 0;
/* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
for (i = 0; apr_isalnum(addr[i]) || addr[i] == '-' || addr[i] == '.'; ++i);
#if 0
if (addr[i] == ':') {
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"@@@@ handle optional port in proxy_is_hostname()");
/* @@@@ handle optional port */
}
#endif
if (addr[i] != '\0' || ap_proxy_host2addr(addr, &host) != NULL)
return 0;
This->hostentry = ap_pduphostent (p, &host);
/* Strip trailing dots */
for (i = strlen(addr) - 1; i > 0 && addr[i] == '.'; --i)
addr[i] = '\0';
This->matcher = proxy_match_hostname;
return 1;
}
/* Return TRUE if host "host" is equal to host2 "host2" */
static int proxy_match_hostname(struct dirconn_entry *This, request_rec *r)
{
char *host = This->name;
const char *host2 = proxy_get_host_of_request(r);
int h2_len;
int h1_len;
if (host == NULL || host2 == NULL)
return 0; /* oops! */
h2_len = strlen(host2);
h1_len = strlen(host);
#if 0
unsigned long *ip_list;
/* Try to deal with multiple IP addr's for a host */
for (ip_list = *This->hostentry->h_addr_list; *ip_list != 0UL; ++ip_list)
if (*ip_list == ? ? ? ? ? ? ? ? ? ? ? ? ?)
return 1;
#endif
/* Ignore trailing dots in host2 comparison: */
while (h2_len > 0 && host2[h2_len - 1] == '.')
--h2_len;
while (h1_len > 0 && host[h1_len - 1] == '.')
--h1_len;
return h1_len == h2_len
&& strncasecmp(host, host2, h1_len) == 0;
}
/* Return TRUE if addr is to be matched as a word */
int ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p)
{
This->matcher = proxy_match_word;
return 1;
}
/* Return TRUE if string "str2" occurs literally in "str1" */
static int proxy_match_word(struct dirconn_entry *This, request_rec *r)
{
const char *host = proxy_get_host_of_request(r);
return host != NULL && ap_strstr_c(host, This->name) != NULL;
}
apr_status_t ap_proxy_doconnect(apr_socket_t *sock, char *host, apr_uint32_t port, request_rec *r)
{
apr_status_t rv;
int i;
for (i = 0; host[i] != '\0'; i++)
if (!apr_isdigit(host[i]) && host[i] != '.')
break;
apr_set_port(sock, APR_REMOTE, port);
if (host[i] == '\0') {
apr_set_ipaddr(sock, APR_REMOTE, host);
host = NULL;
}
do
{
rv = apr_connect(sock, host);
} while (APR_STATUS_IS_EINTR(rv));
if (rv != APR_SUCCESS)
{
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"proxy connect to %s port %d failed", host, port);
}
return rv;
}
/* This function is called by ap_table_do() for all header lines */
/* (from proxy_http.c and proxy_ftp.c) */
/* It is passed a table_do_args struct pointer and a MIME field and value pair */
int ap_proxy_send_hdr_line(void *p, const char *key, const char *value)
{
struct request_rec *r = (struct request_rec *)p;
if (key == NULL || value == NULL || value[0] == '\0')
return 1;
if (!r->assbackwards)
ap_rvputs(r, key, ": ", value, CRLF, NULL);
return 1; /* tell ap_table_do() to continue calling us for more headers */
}
/* send a text line to one or two BUFF's; return line length */
unsigned ap_proxy_bputs2(const char *data, apr_socket_t *client, ap_cache_el *cache)
{
unsigned len = strlen(data);
apr_file_t *cachefp = NULL;
apr_send(client, data, &len);
if (ap_cache_el_data(cache, &cachefp) == APR_SUCCESS)
apr_puts(data, cachefp);
return len;
}
#if defined WIN32
static DWORD tls_index;
BOOL WINAPI DllMain (HINSTANCE dllhandle, DWORD reason, LPVOID reserved)
{
LPVOID memptr;
switch (reason) {
case DLL_PROCESS_ATTACH:
tls_index = TlsAlloc();
case DLL_THREAD_ATTACH: /* intentional no break */
TlsSetValue (tls_index, malloc (sizeof (struct per_thread_data)));
break;
case DLL_THREAD_DETACH:
memptr = TlsGetValue (tls_index);
if (memptr) {
free (memptr);
TlsSetValue (tls_index, 0);
}
break;
}
return TRUE;
}
#endif
static struct per_thread_data *get_per_thread_data(void)
{
#if 0
#if defined(WIN32)
return (struct per_thread_data *) TlsGetValue (tls_index);
#else
static APACHE_TLS struct per_thread_data sptd;
return &sptd;
#endif
#endif
return NULL;
}
int ap_proxy_cache_send(request_rec *r, ap_cache_el *c)
{
apr_file_t *cachefp = NULL;
apr_socket_t *fp = r->connection->client_socket;
char buffer[500];
apr_size_t len;
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
"Sending cache file for %s", c->name);
if(ap_cache_el_data(c, &cachefp) != APR_SUCCESS)
return HTTP_INTERNAL_SERVER_ERROR;
/* send the response */
if(apr_fgets(buffer, sizeof(buffer), cachefp)) {
len = strlen(buffer);
apr_send(fp, buffer, &len);
}
/* send headers */
ap_cache_el_header_walk(c, ap_proxy_send_hdr_line, r, NULL);
len = 2;
apr_send(fp, CRLF, &len);
/* send data */
/* XXX I changed the ap_proxy_send_fb call to use fp instead of cachefp.
* this compiles cleanly, but it is probably the completely wrong
* solution. We need to go through the proxy code, and remove all
* of the BUFF's. rbb
*/
if(!r->header_only && !ap_proxy_send_fb(0, fp, r, NULL))
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
}
int ap_proxy_cache_should_cache(request_rec *r, apr_table_t *resp_hdrs, const int is_HTTP1)
{
const char *expire = apr_table_get(resp_hdrs, "Expires");
time_t expc;
if (expire != NULL)
expc = ap_parseHTTPdate(expire);
else
expc = BAD_DATE;
if((r->status != HTTP_OK && r->status != HTTP_MOVED_PERMANENTLY && r->status != HTTP_NOT_MODIFIED) ||
(r->status == HTTP_NOT_MODIFIED) ||
r->header_only ||
apr_table_get(r->headers_in, "Authorization") != NULL ||
(expire != NULL && expc == BAD_DATE) ||
(r->status == HTTP_OK && !apr_table_get(resp_hdrs, "Last-Modified") && is_HTTP1))
{
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
"proxy: Response is not cacheable: %s", r->unparsed_uri);
return 0;
}
return 1;
}
/*
* what responses should we not cache?
* Unknown status responses and those known to be uncacheable
* 304 HTTP_NOT_MODIFIED response when we have no valid cache file, or
* 200 HTTP_OK response from HTTP/1.0 and up without a Last-Modified header, or
* HEAD requests, or
* requests with an Authorization header, or
* protocol requests nocache (e.g. ftp with user/password)
*/
/* @@@ XXX FIXME: is the test "r->status != HTTP_MOVED_PERMANENTLY" correct?
* or shouldn't it be "ap_is_HTTP_REDIRECT(r->status)" ? -MnKr */
int ap_proxy_cache_update(ap_cache_el *c)
{
ap_cache_handle_t *h = c ? c->cache : NULL;
if(!h) return DECLINED;
ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
"proxy: Cache finalized: %s", c->name);
ap_cache_el_finalize(c);
ap_cache_garbage_collect(h);
return DECLINED;
}