mod_proxy.c revision 9cb787a8a3d06704ab9c51ffd51525b364151182
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* ====================================================================
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The Apache Software License, Version 1.1
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Copyright (c) 2000 The Apache Software Foundation. All rights
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * reserved.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Redistribution and use in source and binary forms, with or without
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * modification, are permitted provided that the following conditions
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * are met:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 1. Redistributions of source code must retain the above copyright
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * notice, this list of conditions and the following disclaimer.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * 2. Redistributions in binary form must reproduce the above copyright
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * notice, this list of conditions and the following disclaimer in
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * the documentation and/or other materials provided with the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * distribution.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 3. The end-user documentation included with the redistribution,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * if any, must include the following acknowledgment:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * "This product includes software developed by the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Apache Software Foundation (http://www.apache.org/)."
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Alternately, this acknowledgment may appear in the software itself,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * if and wherever such third-party acknowledgments normally appear.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 4. The names "Apache" and "Apache Software Foundation" must
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * not be used to endorse or promote products derived from this
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * software without prior written permission. For written
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * permission, please contact apache@apache.org.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * 5. Products derived from this software may not be called "Apache",
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * nor may "Apache" appear in their name, without prior written
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * permission of the Apache Software Foundation.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * SUCH DAMAGE.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ====================================================================
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * This software consists of voluntary contributions made by many
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * individuals on behalf of the Apache Software Foundation. For more
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * information on the Apache Software Foundation, please see
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * <http://www.apache.org/>.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * Portions of this software are based upon public domain software
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * originally written at the National Center for Supercomputing Applications,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * University of Illinois, Urbana-Champaign.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "mod_proxy.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#define CORE_PRIVATE
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "http_log.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "http_vhost.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "http_request.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "util_date.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Some WWW schemes and their default ports; this is basically /etc/services */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* This will become global when the protocol abstraction comes */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic struct proxy_services defports[] =
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"http", DEFAULT_HTTP_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"ftp", DEFAULT_FTP_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"https", DEFAULT_HTTPS_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"gopher", DEFAULT_GOPHER_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"nntp", DEFAULT_NNTP_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"wais", DEFAULT_WAIS_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"snews", DEFAULT_SNEWS_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {"prospero", DEFAULT_PROSPERO_PORT},
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {NULL, -1} /* unknown port */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse};
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/*
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * A Web proxy module. Stages:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * translate_name: set filename to proxy:<URL>
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * type_checker: set type to PROXY_MAGIC_TYPE if filename begins proxy:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * fix_ups: convert the URL stored in the filename to the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * canonical form.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * handler: handle proxy requests
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* -------------------------------------------------------------- */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Translate the URL into a 'filename' */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int alias_match(const char *uri, const char *alias_fakename)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *end_fakename = alias_fakename + strlen(alias_fakename);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *aliasp = alias_fakename, *urip = uri;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse while (aliasp < end_fakename) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (*aliasp == '/') {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* any number of '/' in the alias matches any number in
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * the supplied URI, but there must be at least one...
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (*urip != '/')
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse while (*aliasp == '/')
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ++aliasp;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse while (*urip == '/')
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ++urip;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Other characters are compared literally */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (*urip++ != *aliasp++)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Check last alias path component matched all the way */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Return number of characters from URI which matched (may be
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * greater than length of alias, since we may have matched
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * doubled slashes)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return urip - uri;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Detect if an absoluteURI should be proxied or not. Note that we
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * have to do this during this phase because later phases are
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * "short-circuiting"... i.e. translate_names will end when the first
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * module returns OK. So for example, if the request is something like:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * GET http://othervhost/cgi-bin/printenv HTTP/1.0
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * mod_alias will notice the /cgi-bin part and ScriptAlias it and
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * short-circuit the proxy... just because of the ordering in the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * configuration file.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int proxy_detect(request_rec *r)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse void *sconf = r->server->module_config;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse proxy_server_conf *conf;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (conf->req && r->parsed_uri.scheme) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* but it might be something vhosted */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (!(r->parsed_uri.hostname
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse && !strcasecmp(r->parsed_uri.scheme, ap_http_method(r))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse && ap_matches_request_vhost(r, r->parsed_uri.hostname,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->parsed_uri.port_str ? r->parsed_uri.port : ap_default_port(r)))) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->proxyreq = 1;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->uri = r->unparsed_uri;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->filename = ap_pstrcat(r->pool, "proxy:", r->uri, NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->handler = "proxy-server";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* We need special treatment for CONNECT proxying: it has no scheme part */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else if (conf->req && r->method_number == M_CONNECT
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse && r->parsed_uri.hostname
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse && r->parsed_uri.port_str) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->proxyreq = 1;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->uri = r->unparsed_uri;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->filename = ap_pstrcat(r->pool, "proxy:", r->uri, NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->handler = "proxy-server";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return DECLINED;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int proxy_trans(request_rec *r)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse void *sconf = r->server->module_config;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse proxy_server_conf *conf =
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int i, len;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse struct proxy_alias *ent = (struct proxy_alias *) conf->aliases->elts;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (r->proxyreq) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* someone has already set up the proxy, it was possibly ourselves
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * in proxy_detect
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return OK;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* XXX: since r->uri has been manipulated already we're not really
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * compliant with RFC1945 at this point. But this probably isn't
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * an issue because this is a hybrid proxy/origin server.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (i = 0; i < conf->aliases->nelts; i++) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse len = alias_match(r->uri, ent[i].fake);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (len > 0) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->filename = ap_pstrcat(r->pool, "proxy:", ent[i].real,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->uri + len, NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->handler = "proxy-server";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->proxyreq = 1;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return OK;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return DECLINED;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* -------------------------------------------------------------- */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Fixup the filename */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/*
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Canonicalise the URL
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int proxy_fixup(request_rec *r)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *url, *p;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (!r->proxyreq || strncmp(r->filename, "proxy:", 6) != 0)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return DECLINED;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse url = &r->filename[6];
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* canonicalise each specific scheme */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (strncasecmp(url, "http:", 5) == 0)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return ap_proxy_http_canon(r, url + 5, "http", DEFAULT_HTTP_PORT);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else if (strncasecmp(url, "ftp:", 4) == 0)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return ap_proxy_ftp_canon(r, url + 4);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse p = strchr(url, ':');
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (p == NULL || p == url)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_BAD_REQUEST;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return OK; /* otherwise; we've done the best we can */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Send a redirection if the request contains a hostname which is not
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * fully qualified, i.e. doesn't have a domain name appended. Some proxy
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * servers like Netscape's allow this and access hosts from the local
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * domain in this case. I think it is better to redirect to a FQDN, since
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * these will later be found in the bookmarks files.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The "ProxyDomain" directive determines what domain will be appended
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *nuri;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *ref;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* We only want to worry about GETs */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return DECLINED;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* If host does contain a dot already, or it is "localhost", decline */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (strchr(r->parsed_uri.hostname, '.') != NULL
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse || strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return DECLINED; /* host name has a dot already */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ref = ap_table_get(r->headers_in, "Referer");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Reassemble the request, but insert the domain after the host name */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Note that the domain name always starts with a dot */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->parsed_uri.hostname = ap_pstrcat(r->pool, r->parsed_uri.hostname,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse domain, NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse nuri = ap_unparse_uri_components(r->pool,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse &r->parsed_uri,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse UNP_REVEALPASSWORD);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_table_set(r->headers_out, "Location", nuri);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "Domain missing: %s sent to %s%s%s", r->uri,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_unparse_uri_components(r->pool, &r->parsed_uri,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse UNP_OMITUSERINFO),
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ref ? " from " : "", ref ? ref : "");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_MOVED_PERMANENTLY;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* -------------------------------------------------------------- */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Invoke handler */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic int proxy_handler(request_rec *r)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *url, *scheme, *p;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse void *sconf = r->server->module_config;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse proxy_server_conf *conf = (proxy_server_conf *)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_get_module_config(sconf, &proxy_module);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_array_header_t *proxies = conf->proxies;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int i, rc;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_cache_el *cr=NULL;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int direct_connect = 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *maxfwd_str;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *pragma, *auth, *imstr;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (!r->proxyreq || strncmp(r->filename, "proxy:", 6) != 0)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return DECLINED;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (r->method_number == M_TRACE && (maxfwd_str =
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_table_get(r->headers_in, "Max-Forwards")) != NULL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int maxfwd = strtol(maxfwd_str, NULL, 10);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (maxfwd < 1) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int access_status;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse r->proxyreq = 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((access_status = ap_send_http_trace(r)))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_die(access_status, r);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_finalize_request_protocol(r);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return OK;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_table_setn(r->headers_in, "Max-Forwards",
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_psprintf(r->pool, "%d", (maxfwd > 0) ? maxfwd-1 : 0));
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return rc;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse url = r->filename + 6;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse p = strchr(url, ':');
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (p == NULL)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_BAD_REQUEST;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse pragma = ap_table_get(r->headers_in, "Pragma");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse auth = ap_table_get(r->headers_in, "Authorization");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse imstr = ap_table_get(r->headers_in, "If-Modified-Since");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "Request for %s, pragma=%s, auth=%s, imstr=%s", url,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse pragma, auth, imstr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* can this request be cached at all? */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (r->method_number == M_GET && strlen(url) < 1024 &&
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse !ap_proxy_liststr(pragma, "no-cache") && auth == NULL)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if(ap_cache_seek(conf->cache, url, &cr) == APR_SUCCESS)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int has_m = 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* now we need to check if the last modified date is write if */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if(imstr)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse time_t ims = (time_t)ap_parseHTTPdate(ap_proxy_date_canon(r->pool, imstr));
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if(ims == BAD_DATE)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_table_unset(r->headers_in, "If-Modified-Since");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* ok we were asked to check, so let's do that */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if(ap_cache_el_header(cr, "Last-Modified",
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (char **)&imstr) == APR_SUCCESS)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse time_t lm =
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_parseHTTPdate(ap_proxy_date_canon(r->pool, imstr));
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if(lm != BAD_DATE)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if(ims < lm)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_table_set(r->headers_in,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "If-Modified-Since", imstr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse has_m = 1;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return has_m ? HTTP_NOT_MODIFIED : ap_proxy_cache_send(r, cr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* if there wasn't an entry in the cache we get here,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse we need to create one */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_cache_create(conf->cache, url, &cr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* If the host doesn't have a domain name, add one and redirect. */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (conf->domain != NULL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse rc = proxy_needsdomain(r, url, conf->domain);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (ap_is_HTTP_REDIRECT(rc))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_MOVED_PERMANENTLY;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *p = '\0';
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse scheme = ap_pstrdup(r->pool, url);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *p = ':';
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Check URI's destination host against NoProxy hosts */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Bypass ProxyRemote server lookup if configured as NoProxy */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* we only know how to handle communication to a proxy via http */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /*if (strcasecmp(scheme, "http") == 0) */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int ii;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
for (direct_connect = ii = 0; ii < conf->dirconn->nelts && !direct_connect; ii++) {
direct_connect = list[ii].matcher(&list[ii], r);
}
#if DEBUGGING
ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
(direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
r->uri);
#endif
}
/* firstly, try a proxy, unless a NoProxy directive is active */
if (!direct_connect)
for (i = 0; i < proxies->nelts; i++) {
p = strchr(ents[i].scheme, ':'); /* is it a partial URL? */
if (strcmp(ents[i].scheme, "*") == 0 ||
(p == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
(p != NULL &&
strncasecmp(url, ents[i].scheme, strlen(ents[i].scheme)) == 0)) {
/* CONNECT is a special method that bypasses the normal
* proxy code.
*/
if (r->method_number == M_CONNECT)
rc = ap_proxy_connect_handler(r, cr, url, ents[i].hostname,
ents[i].port);
/* we only know how to handle communication to a proxy via http */
else if (strcasecmp(ents[i].protocol, "http") == 0)
rc = ap_proxy_http_handler(r, cr, url, ents[i].hostname,
ents[i].port);
else
rc = DECLINED;
/* an error or success */
if (rc != DECLINED && rc != HTTP_BAD_GATEWAY)
return rc;
/* we failed to talk to the upstream proxy */
}
}
/* otherwise, try it direct */
/* N.B. what if we're behind a firewall, where we must use a proxy or
* give up??
*/
/* handle the scheme */
if (r->method_number == M_CONNECT)
return ap_proxy_connect_handler(r, cr, url, NULL, 0);
if (strcasecmp(scheme, "http") == 0)
return ap_proxy_http_handler(r, cr, url, NULL, 0);
if (strcasecmp(scheme, "ftp") == 0)
return ap_proxy_ftp_handler(r, cr, url);
else
return HTTP_FORBIDDEN;
}
/* -------------------------------------------------------------- */
/* Setup configurable data */
static void *create_proxy_config(ap_pool_t *p, server_rec *s)
{
proxy_server_conf *ps = ap_pcalloc(p, sizeof(proxy_server_conf));
ps->proxies = ap_make_array(p, 10, sizeof(struct proxy_remote));
ps->aliases = ap_make_array(p, 10, sizeof(struct proxy_alias));
ps->raliases = ap_make_array(p, 10, sizeof(struct proxy_alias));
ps->noproxies = ap_make_array(p, 10, sizeof(struct noproxy_entry));
ps->dirconn = ap_make_array(p, 10, sizeof(struct dirconn_entry));
ps->nocaches = ap_make_array(p, 10, sizeof(struct nocache_entry));
ps->allowed_connect_ports = ap_make_array(p, 10, sizeof(int));
ps->cache_completion = DEFAULT_CACHE_COMPLETION;
ps->domain = NULL;
ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
ps->req = 0;
ap_cache_init(&ps->cache, "mod_proxy cache", s);
return ps;
}
static const char *
add_proxy(cmd_parms *cmd, void *dummy, char *f, char *r)
{
server_rec *s = cmd->server;
proxy_server_conf *conf =
(proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
struct proxy_remote *new;
char *p, *q;
int port;
p = strchr(r, ':');
if (p == NULL || p[1] != '/' || p[2] != '/' || p[3] == '\0')
return "ProxyRemote: Bad syntax for a remote proxy server";
q = strchr(p + 3, ':');
if (q != NULL) {
if (sscanf(q + 1, "%u", &port) != 1 || port > 65535)
return "ProxyRemote: Bad syntax for a remote proxy server (bad port number)";
*q = '\0';
}
else
port = -1;
*p = '\0';
if (strchr(f, ':') == NULL)
ap_str_tolower(f); /* lowercase scheme */
ap_str_tolower(p + 3); /* lowercase hostname */
if (port == -1) {
int i;
for (i = 0; defports[i].scheme != NULL; i++)
if (strcasecmp(defports[i].scheme, r) == 0)
break;
port = defports[i].port;
}
new = ap_push_array(conf->proxies);
new->scheme = f;
new->protocol = r;
new->hostname = p + 3;
new->port = port;
return NULL;
}
static const char *
set_cache_exclude(cmd_parms *cmd, void *dummy, char *arg)
{
server_rec *s = cmd->server;
proxy_server_conf *psf = (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
struct nocache_entry *new;
struct nocache_entry *list = (struct nocache_entry *) psf->nocaches->elts;
struct hostent hp;
int found = 0;
int i;
/* Don't duplicate entries */
for (i = 0; i < psf->nocaches->nelts; i++) {
if (strcasecmp(arg, list[i].name) == 0) /* ignore case for host names */
found = 1;
}
if (!found) {
new = ap_push_array(psf->nocaches);
new->name = arg;
/* Don't do name lookups on things that aren't dotted */
if (strchr(arg, '.') != NULL && ap_proxy_host2addr(new->name, &hp) == NULL)
/*@@@FIXME: This copies only the first of (possibly many) IP addrs */
memcpy(&new->addr, hp.h_addr, sizeof(struct in_addr));
else
new->addr.s_addr = 0;
}
return NULL;
}
static const char *
add_pass(cmd_parms *cmd, void *dummy, char *f, char *r)
{
server_rec *s = cmd->server;
proxy_server_conf *conf =
(proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
struct proxy_alias *new;
new = ap_push_array(conf->aliases);
new->fake = f;
new->real = r;
return NULL;
}
static const char *
add_pass_reverse(cmd_parms *cmd, void *dummy, char *f, char *r)
{
server_rec *s = cmd->server;
proxy_server_conf *conf;
struct proxy_alias *new;
conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
&proxy_module);
new = ap_push_array(conf->raliases);
new->fake = f;
new->real = r;
return NULL;
}
static const char *set_proxy_exclude(cmd_parms *parms, void *dummy, char *arg)
{
server_rec *s = parms->server;
proxy_server_conf *conf =
ap_get_module_config(s->module_config, &proxy_module);
struct noproxy_entry *new;
struct noproxy_entry *list = (struct noproxy_entry *) conf->noproxies->elts;
struct hostent hp;
int found = 0;
int i;
/* Don't duplicate entries */
for (i = 0; i < conf->noproxies->nelts; i++) {
if (strcasecmp(arg, list[i].name) == 0) /* ignore case for host names */
found = 1;
}
if (!found) {
new = ap_push_array(conf->noproxies);
new->name = arg;
/* Don't do name lookups on things that aren't dotted */
if (strchr(arg, '.') != NULL && ap_proxy_host2addr(new->name, &hp) == NULL)
/*@@@FIXME: This copies only the first of (possibly many) IP addrs */
memcpy(&new->addr, hp.h_addr, sizeof(struct in_addr));
else
new->addr.s_addr = 0;
}
return NULL;
}
/*
* Set the ports CONNECT can use
*/
static const char *
set_allowed_ports(cmd_parms *parms, void *dummy, char *arg)
{
server_rec *s = parms->server;
proxy_server_conf *conf =
ap_get_module_config(s->module_config, &proxy_module);
int *New;
if (!ap_isdigit(arg[0]))
return "AllowCONNECT: port number must be numeric";
New = ap_push_array(conf->allowed_connect_ports);
*New = atoi(arg);
return NULL;
}
/* Similar to set_proxy_exclude(), but defining directly connected hosts,
* which should never be accessed via the configured ProxyRemote servers
*/
static const char *
set_proxy_dirconn(cmd_parms *parms, void *dummy, char *arg)
{
server_rec *s = parms->server;
proxy_server_conf *conf =
ap_get_module_config(s->module_config, &proxy_module);
struct dirconn_entry *New;
struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
int found = 0;
int i;
/* Don't duplicate entries */
for (i = 0; i < conf->dirconn->nelts; i++) {
if (strcasecmp(arg, list[i].name) == 0)
found = 1;
}
if (!found) {
New = ap_push_array(conf->dirconn);
New->name = arg;
New->hostentry = NULL;
if (ap_proxy_is_ipaddr(New, parms->pool)) {
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Parsed addr %s", inet_ntoa(New->addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Parsed mask %s", inet_ntoa(New->mask));
#endif
}
else if (ap_proxy_is_domainname(New, parms->pool)) {
ap_str_tolower(New->name);
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Parsed domain %s", New->name);
#endif
}
else if (ap_proxy_is_hostname(New, parms->pool)) {
ap_str_tolower(New->name);
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Parsed host %s", New->name);
#endif
}
else {
ap_proxy_is_word(New, parms->pool);
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"Parsed word %s", New->name);
#endif
}
}
return NULL;
}
static const char *
set_proxy_domain(cmd_parms *parms, void *dummy, char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
if (arg[0] != '.')
return "ProxyDomain: domain name must start with a dot.";
psf->domain = arg;
return NULL;
}
static const char *
set_proxy_req(cmd_parms *parms, void *dummy, int flag)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
psf->req = flag;
return NULL;
}
static const char *
set_recv_buffer_size(cmd_parms *parms, void *dummy, char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
int s = atoi(arg);
if (s < 512 && s != 0) {
return "ProxyReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
}
psf->recv_buffer_size = s;
return NULL;
}
static const char*
set_via_opt(cmd_parms *parms, void *dummy, char *arg)
{
proxy_server_conf *psf = ap_get_module_config(parms->server->module_config, &proxy_module);
if (strcasecmp(arg, "Off") == 0)
psf->viaopt = via_off;
else if (strcasecmp(arg, "On") == 0)
psf->viaopt = via_on;
else if (strcasecmp(arg, "Block") == 0)
psf->viaopt = via_block;
else if (strcasecmp(arg, "Full") == 0)
psf->viaopt = via_full;
else {
return "ProxyVia must be one of: "
"off | on | full | block";
}
return NULL;
}
static const char*
set_cache_completion(cmd_parms *parms, void *dummy, char *arg)
{
proxy_server_conf *psf = ap_get_module_config(parms->server->module_config, &proxy_module);
int s = atoi(arg);
if (s > 100 || s < 0) {
return "CacheForceCompletion must be <= 100 percent, "
"or 0 for system default.";
}
if (s > 0)
psf->cache_completion = ((float)s / 100);
return NULL;
}
static const handler_rec proxy_handlers[] =
{
{"proxy-server", proxy_handler},
{NULL}
};
static const command_rec proxy_cmds[] =
{
{"ProxyRequests", set_proxy_req, NULL, RSRC_CONF, FLAG,
"on if the true proxy requests should be accepted"},
{"ProxyRemote", add_proxy, NULL, RSRC_CONF, TAKE2,
"a scheme, partial URL or '*' and a proxy server"},
{"ProxyPass", add_pass, NULL, RSRC_CONF, TAKE2,
"a virtual path and a URL"},
{"ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF, TAKE2,
"a virtual path and a URL for reverse proxy behaviour"},
{"ProxyBlock", set_proxy_exclude, NULL, RSRC_CONF, ITERATE,
"A list of names, hosts or domains to which the proxy will not connect"},
{"ProxyReceiveBufferSize", set_recv_buffer_size, NULL, RSRC_CONF, TAKE1,
"Receive buffer size for outgoing HTTP and FTP connections in bytes"},
{"NoProxy", set_proxy_dirconn, NULL, RSRC_CONF, ITERATE,
"A list of domains, hosts, or subnets to which the proxy will connect directly"},
{"ProxyDomain", set_proxy_domain, NULL, RSRC_CONF, TAKE1,
"The default intranet domain name (in absence of a domain in the URL)"},
{"AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF, ITERATE,
"A list of ports which CONNECT may connect to"},
{"ProxyVia", set_via_opt, NULL, RSRC_CONF, TAKE1,
"Configure Via: proxy header header to one of: on | off | block | full"},
{"ProxyNoCache", set_cache_exclude, NULL, RSRC_CONF, ITERATE,
"A list of names, hosts or domains for which caching is *not* provided"},
{"ProxyForceCacheCompletion", set_cache_completion, NULL, RSRC_CONF, TAKE1,
"Force a http cache completion after this percentage is loaded"},
{NULL}
};
static void register_hooks(void)
{
/* [2] filename-to-URI translation */
ap_hook_translate_name(proxy_trans, NULL, NULL, AP_HOOK_FIRST);
/* [8] fixups */
ap_hook_fixups(proxy_fixup, NULL, NULL, AP_HOOK_FIRST);
/* [1] post read_request handling */
ap_hook_post_read_request(proxy_detect, NULL, NULL, AP_HOOK_FIRST);
}
module MODULE_VAR_EXPORT proxy_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
create_proxy_config, /* create per-server config structure */
NULL, /* merge per-server config structures */
proxy_cmds, /* command ap_table_t */
proxy_handlers, /* handlers */
register_hooks
};