mod_proxy.c revision 1dc8fa9c1a71bbbbd54d8dce850eedeaafdd6450
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Licensed to the Apache Software Foundation (ASF) under one or more
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * contributor license agreements. See the NOTICE file distributed with
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * this work for additional information regarding copyright ownership.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The ASF licenses this file to You under the Apache License, Version 2.0
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * (the "License"); you may not use this file except in compliance with
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * the License. You may obtain a copy of the License at
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Unless required by applicable law or agreed to in writing, software
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * distributed under the License is distributed on an "AS IS" BASIS,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * See the License for the specific language governing permissions and
33bdcae1f7a1a65e351dda2a766a0cf28b1e695dnd * limitations under the License.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseAPR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseAPR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rseAPR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse#define MAX(x,y) ((x) >= (y) ? (x) : (y))
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * A Web proxy module. Stages:
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * translate_name: set filename to proxy:<URL>
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * map_to_storage: run proxy_walk (rather than directory_walk/file_walk)
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * can't trust directory_walk/file_walk since these are
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * not in our filesystem. Prevents mod_http from serving
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * the TRACE request we will set aside to handle later.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * fix_ups: convert the URL stored in the filename to the
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * canonical form.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * handler: handle proxy requests
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse/* -------------------------------------------------------------- */
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse/* Translate the URL into a 'filename' */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *key,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *val)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Normalized load factor. Used with BalancerMamber,
8464a9c46b967001e38fe3c8afff51a649e9de51dougm * it is a number between 1 and 100.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (worker->s->lbfactor < 1 || worker->s->lbfactor > 100)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "LoadFactor must be a number between 1..100";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* If set it will give the retry timeout for the worker
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The default value is 60 seconds, meaning that if
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse * in error state, it will be retried after that timeout.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Retry must be a positive value";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Time in seconds that will destroy all the connections
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * that exceed the smax
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "TTL must be at least one second";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Initial number of connections to remote
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Min must be a positive number";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Maximum number of connections to remote
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Max must be a positive number";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* XXX: More inteligent naming needed */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Maximum number of connections to remote that
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * will not be destroyed
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron return "Smax must be a positive number";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Acquire timeout in given unit (default is milliseconds).
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * If set this will be the maximum time to
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * wait for a free connection.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (ap_timeout_parameter_parse(val, &timeout, "ms") != APR_SUCCESS)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Acquire timeout has wrong format";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Acquire must be at least one millisecond";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Connection timeout in seconds.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Defaults to server timeout.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Timeout must be at least one second";
87a1c79b7b37702a254920ca5214fb282a4fb085dougm if (s < 512 && s) {
87a1c79b7b37702a254920ca5214fb282a4fb085dougm return "IOBufferSize must be >= 512 bytes, or 0 for system default.";
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse return "KeepAlive must be On|Off";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "DisableReuse must be On|Off";
87a1c79b7b37702a254920ca5214fb282a4fb085dougm /* Worker route.
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse return "Route length must be < 64 characters";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Worker redirection route.
87a1c79b7b37702a254920ca5214fb282a4fb085dougm return "Redirect length must be < 64 characters";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *v;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Worker status.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (v = val; *v; v++) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (*v == '+') {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else if (*v == '-') {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse return "Unknown status parameter option";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "flushpackets must be on|off|auto";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "flushwait must be <= 1000, or 0 for system default of 10 millseconds.";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse worker->s->flush_wait = ival * 1000; /* change to microseconds */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Ping/Pong timeout in given unit (default is second).
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (ap_timeout_parameter_parse(val, &timeout, "s") != APR_SUCCESS)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Ping/Pong timeout has wrong format";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Ping/Pong timeout must be at least one millisecond";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "lbset must be between 0 and 99";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Request timeout in given unit (default is second).
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Defaults to connection timeout
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (ap_timeout_parameter_parse(val, &timeout, "s") != APR_SUCCESS)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Connectiontimeout has wrong format";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "Connectiontimeout must be at least one millisecond.";
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron return "flusher name length must be < 16 characters";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "unknown Worker parameter";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic const char *set_balancer_param(proxy_server_conf *conf,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *key,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *val)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Balancer sticky session name.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Set to something like JSESSIONID or
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * PHPSESSIONID, etc..,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "stickysession length must be < 64 characters";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((path = strchr((char *)balancer->s->sticky, '|'))) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* If set to 'on' the session will break
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * if the worker is in error state or
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * disabled.
184f5da95d14895f7f33c90b8b8f70653afb0d92wrowe return "failover must be On|Off";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Balancer timeout in seconds.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * If set this will be the maximum time to
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * wait for a free worker.
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse * Default is not to wait.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return "timeout must be at least one second";
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Maximum number of failover attempts before
a0e0d20b666cfc453ac76506079eb50e03997eefdougm * giving up.
a0e0d20b666cfc453ac76506079eb50e03997eefdougm return "maximum number of attempts must be a positive number";
a0e0d20b666cfc453ac76506079eb50e03997eefdougm return "unknown lbmethod";
a0e0d20b666cfc453ac76506079eb50e03997eefdougm provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
a0e0d20b666cfc453ac76506079eb50e03997eefdougm if (PROXY_STRNCPY(balancer->s->lbpname, val) == APR_SUCCESS) {
a0e0d20b666cfc453ac76506079eb50e03997eefdougm return "lbmethod name too large";
a0e0d20b666cfc453ac76506079eb50e03997eefdougm return "unknown lbmethod";
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* If set to 'on' then ';' will also be
a0e0d20b666cfc453ac76506079eb50e03997eefdougm * used as a session path separator/delim (ala
a0e0d20b666cfc453ac76506079eb50e03997eefdougm return "scolonpathdelim must be On|Off";
a0e0d20b666cfc453ac76506079eb50e03997eefdougm balancer->errstatuses = apr_array_make(p, 1, sizeof(int));
a0e0d20b666cfc453ac76506079eb50e03997eefdougm return "failonstatus must be one or more HTTP response codes";
dd7c683f683624b082d430935b594df7406782c2dougm if (PROXY_STRNCPY(balancer->s->nonce, val) != APR_SUCCESS) {
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm return "Provided nonce is too large";
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm if (ival < 1 || ival > 100) /* arbitrary limit here */
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm return "growth must be between 1 and 100";
dd7c683f683624b082d430935b594df7406782c2dougm return "forcerecovery must be On|Off";
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm return "unknown Balancer parameter";
3c65aa88903de7330a07e133dfda779842fadad4wrowestatic int alias_match(const char *uri, const char *alias_fakename)
b40799adcfd0f0a2a465c2934585986f7bbc9bbcwrowe const char *end_fakename = alias_fakename + strlen(alias_fakename);
b40799adcfd0f0a2a465c2934585986f7bbc9bbcwrowe /* any number of '/' in the alias matches any number in
b40799adcfd0f0a2a465c2934585986f7bbc9bbcwrowe * the supplied URI, but there must be at least one...
3c65aa88903de7330a07e133dfda779842fadad4wrowe /* Other characters are compared literally */
b40799adcfd0f0a2a465c2934585986f7bbc9bbcwrowe /* fixup badly encoded stuff (e.g. % as last character) */
e62985c7a1b46a5036a247f35bddac1308985758dougm /* We reach the end of the uri before the end of "alias_fakename"
98f81eac9530d487f05013cda9df99755bb59689trawick * for example uri is "/" and alias_fakename "/examples"
98f81eac9530d487f05013cda9df99755bb59689trawick /* Check last alias path component matched all the way */
98f81eac9530d487f05013cda9df99755bb59689trawick if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
98f81eac9530d487f05013cda9df99755bb59689trawick /* Return number of characters from URI which matched (may be
98f81eac9530d487f05013cda9df99755bb59689trawick * greater than length of alias, since we may have matched
e62985c7a1b46a5036a247f35bddac1308985758dougm * doubled slashes)
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm/* Detect if an absoluteURI should be proxied or not. Note that we
8464a9c46b967001e38fe3c8afff51a649e9de51dougm * have to do this during this phase because later phases are
3c65aa88903de7330a07e133dfda779842fadad4wrowe * "short-circuiting"... i.e. translate_names will end when the first
3c65aa88903de7330a07e133dfda779842fadad4wrowe * module returns OK. So for example, if the request is something like:
3c65aa88903de7330a07e133dfda779842fadad4wrowe * mod_alias will notice the /cgi-bin part and ScriptAlias it and
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm * short-circuit the proxy... just because of the ordering in the
3c65aa88903de7330a07e133dfda779842fadad4wrowe * configuration file.
e18e68b42830409bf48de0df9eed3fe363664aa7aaron (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm /* Ick... msvc (perhaps others) promotes ternary short results to int */
e62985c7a1b46a5036a247f35bddac1308985758dougm /* but it might be something vhosted */
3c65aa88903de7330a07e133dfda779842fadad4wrowe && !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))
3c65aa88903de7330a07e133dfda779842fadad4wrowe && ap_matches_request_vhost(r, r->parsed_uri.hostname,
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
return DECLINED;
const char *start;
const char *end;
const char *var;
const char *val;
const char *firstpart;
return str;
return str;
sizeof (struct proxy_alias));
return ret;
int len;
const char *fake;
const char *real;
int mismatch = 0;
return DECLINED;
if (!found) {
return HTTP_INTERNAL_SERVER_ERROR;
if (len != 0) {
return DECLINED;
if (mismatch) {
if (found) {
return OK;
return DONE;
if (r->proxyreq) {
return OK;
return DECLINED;
return rv;
&proxy_module);
return rv;
return DECLINED;
&proxy_module);
for (j = 0; j < num_sec; ++j)
if (entry_proxy->r
return OK;
int access_status;
return DECLINED;
return access_status;
return OK;
char *url, *p;
int access_status;
&proxy_module);
return DECLINED;
return access_status;
return HTTP_BAD_REQUEST;
char *nuri;
const char *ref;
return DECLINED;
&r->parsed_uri,
return HTTP_MOVED_PERMANENTLY;
const char *p2;
int direct_connect = 0;
const char *str;
long maxfwd;
return DECLINED;
switch (r->method_number) {
case M_TRACE: {
int access_status;
return OK;
case M_OPTIONS: {
int access_status;
return OK;
if (maxfwd >= 0) {
return HTTP_METHOD_NOT_ALLOWED;
return HTTP_REQUEST_ENTITY_TOO_LARGE;
if (p == NULL) {
return HTTP_BAD_REQUEST;
return HTTP_MOVED_PERMANENTLY;
!direct_connect; i++) {
#if DEBUGGING
r->uri);
return access_status;
if (!worker)
goto cleanup;
if (balancer) {
if (!direct_connect) {
const char *cl_a;
char *end;
goto cleanup;
if (cl_a) {
if (cl > 0) {
goto cleanup;
goto cleanup;
if (balancer) {
if (balancer) {
goto cleanup;
return access_status;
return ps;
ps->recv_buffer_size = (overrides->recv_buffer_size_set == 0) ? base->recv_buffer_size : overrides->recv_buffer_size;
ps->io_buffer_size = (overrides->io_buffer_size_set == 0) ? base->io_buffer_size : overrides->io_buffer_size;
ps->proxy_status = (overrides->proxy_status_set == 0) ? base->proxy_status : overrides->proxy_status;
ps->source_address = (overrides->source_address_set == 0) ? base->source_address : overrides->source_address;
return ps;
const char *arg)
return NULL;
return (void *) new;
return new;
char *r, *f, *scheme;
int port;
if (regex)
scheme[p-r] = 0;
if (q != NULL) {
if (regex)
if (regex) {
if (!reg)
return NULL;
char *r = NULL;
char *word;
unsigned int flags = 0;
const char *err;
if (err) {
return err;
while (*arg) {
if (is_regex) {
f = word;
r = word;
if (!val) {
if (r == NULL) {
if (apr_fnmatch_test(f)) {
if (use_regex) {
return NULL;
if (!balancer) {
if (err)
if (err)
int reuse = 0;
if (!worker) {
if (err)
if (reuse) {
if (err)
return NULL;
const char *fake;
const char *real;
const char *interp;
const char *err;
if (err) {
return err;
fake = f;
real = r;
interp = i;
real = f;
interp = r;
return NULL;
const char *r, const char *interp)
return NULL;
const char *r, const char *interp)
return NULL;
int found = 0;
if (!found) {
return NULL;
int found = 0;
if (!found) {
#if DEBUGGING
#if DEBUGGING
#if DEBUGGING
#if DEBUGGING
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
int timeout;
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
return NULL;
char *word;
int reuse = 0;
if (err)
return err;
while (*arg) {
char *val;
if (!val) {
if (!path)
else if (!name)
if (!path)
if (!name)
if (!balancer) {
if (err)
if (!worker) {
if (reuse) {
if (err)
return NULL;
int in_proxy_section = 0;
if (err)
return err;
if (!balancer) {
if (in_proxy_section) {
if (err)
if (!worker) {
if (in_proxy_section) {
if (err)
while (*arg) {
if (!val) {
if (worker)
if (err)
return NULL;
&proxy_module);
const char *errmsg;
return err;
if (!arg) {
return errmsg;
conf->r = r;
NULL);
NULL);
if (!balancer) {
if (err)
conf->p);
if (!worker) {
if (err)
NULL);
while (*arg) {
if (!val) {
if (worker)
if (err)
return NULL;
{NULL}
if (proxy_ssl_enable) {
if (proxy_ssl_disable) {
return proxy_ssl_disable(c);
if (proxy_is_https) {
return proxy_is_https(c);
const char *var)
if (proxy_ssl_val) {
return NULL;
return OK;
return OK;
++worker;
++balancer;
return OK;
if (!reverse) {
s = s->next;
proxy_lb_workers = 0;
return OK;
/* register optional functions within proxy_util.c */
request_rec *r,
request_rec *r,
(request_rec *r), (r),
(status, r),