mod_proxy.c revision 5c24acdc757b7557d33c389fafdb7cc455ce3740
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * http://www.apache.org/licenses/LICENSE-2.0
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * Unless required by applicable law or agreed to in writing, software
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * distributed under the License is distributed on an "AS IS" BASIS,
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * See the License for the specific language governing permissions and
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
3568de757bac0b47256647504c186d17ca272f85rbb#include "mod_proxy.h"
3568de757bac0b47256647504c186d17ca272f85rbb#include "mod_core.h"
3568de757bac0b47256647504c186d17ca272f85rbb#include "apr_optional.h"
3568de757bac0b47256647504c186d17ca272f85rbb#include "scoreboard.h"
3568de757bac0b47256647504c186d17ca272f85rbb#include "mod_status.h"
3568de757bac0b47256647504c186d17ca272f85rbb#include "proxy_util.h"
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb#if (MODULE_MAGIC_NUMBER_MAJOR > 20020903)
3568de757bac0b47256647504c186d17ca272f85rbb#include "mod_ssl.h"
3568de757bac0b47256647504c186d17ca272f85rbb#else
3568de757bac0b47256647504c186d17ca272f85rbbAPR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
3568de757bac0b47256647504c186d17ca272f85rbbAPR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
3568de757bac0b47256647504c186d17ca272f85rbbAPR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
3568de757bac0b47256647504c186d17ca272f85rbbAPR_DECLARE_OPTIONAL_FN(char *, ssl_var_lookup,
3568de757bac0b47256647504c186d17ca272f85rbb (apr_pool_t *, server_rec *,
3568de757bac0b47256647504c186d17ca272f85rbb conn_rec *, request_rec *, char *));
3568de757bac0b47256647504c186d17ca272f85rbb#endif
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb#ifndef MAX
3568de757bac0b47256647504c186d17ca272f85rbb#define MAX(x,y) ((x) >= (y) ? (x) : (y))
3568de757bac0b47256647504c186d17ca272f85rbb#endif
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb/*
3568de757bac0b47256647504c186d17ca272f85rbb * A Web proxy module. Stages:
3568de757bac0b47256647504c186d17ca272f85rbb *
3568de757bac0b47256647504c186d17ca272f85rbb * translate_name: set filename to proxy:<URL>
3568de757bac0b47256647504c186d17ca272f85rbb * map_to_storage: run proxy_walk (rather than directory_walk/file_walk)
3568de757bac0b47256647504c186d17ca272f85rbb * can't trust directory_walk/file_walk since these are
3568de757bac0b47256647504c186d17ca272f85rbb * not in our filesystem. Prevents mod_http from serving
3568de757bac0b47256647504c186d17ca272f85rbb * the TRACE request we will set aside to handle later.
3568de757bac0b47256647504c186d17ca272f85rbb * type_checker: set type to PROXY_MAGIC_TYPE if filename begins proxy:
3568de757bac0b47256647504c186d17ca272f85rbb * fix_ups: convert the URL stored in the filename to the
3568de757bac0b47256647504c186d17ca272f85rbb * canonical form.
3568de757bac0b47256647504c186d17ca272f85rbb * handler: handle proxy requests
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb/* -------------------------------------------------------------- */
3568de757bac0b47256647504c186d17ca272f85rbb/* Translate the URL into a 'filename' */
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic const char *set_worker_param(apr_pool_t *p,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding proxy_worker *worker,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *key,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *val)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
3568de757bac0b47256647504c186d17ca272f85rbb
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick int ival;
3568de757bac0b47256647504c186d17ca272f85rbb apr_interval_time_t timeout;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (!strcasecmp(key, "loadfactor")) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Normalized load factor. Used with BalancerMamber,
98fb535f829e2a95aabd82420931f476661fa8e3jorton * it is a number between 1 and 100.
db12cd62083041bf90945eeb90cc40fbd2340797trawick */
db12cd62083041bf90945eeb90cc40fbd2340797trawick worker->s->lbfactor = atoi(val);
db12cd62083041bf90945eeb90cc40fbd2340797trawick if (worker->s->lbfactor < 1 || worker->s->lbfactor > 100)
333eac96e4fb7d6901cb75e6ca7bb22b2ccb84cetrawick return "LoadFactor must be a number between 1..100";
333eac96e4fb7d6901cb75e6ca7bb22b2ccb84cetrawick }
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem else if (!strcasecmp(key, "retry")) {
3568de757bac0b47256647504c186d17ca272f85rbb /* If set it will give the retry timeout for the worker
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * The default value is 60 seconds, meaning that if
3568de757bac0b47256647504c186d17ca272f85rbb * in error state, it will be retried after that timeout.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ival = atoi(val);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (ival < 0)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return "Retry must be a positive value";
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz worker->s->retry = apr_time_from_sec(ival);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding worker->s->retry_set = 1;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (!strcasecmp(key, "ttl")) {
8f3ec4772d2aeb347cf40e87c77627bb784dd018rbb /* Time in seconds that will destroy all the connections
8f3ec4772d2aeb347cf40e87c77627bb784dd018rbb * that exceed the smax
3d96ee83babeec32482c9082c9426340cee8c44dwrowe */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ival = atoi(val);
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf if (ival < 1)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return "TTL must be at least one second";
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick worker->s->ttl = apr_time_from_sec(ival);
98fb535f829e2a95aabd82420931f476661fa8e3jorton }
f0e395a55abfcad3d2bd7c63470003b08a93d567nd else if (!strcasecmp(key, "min")) {
f0e395a55abfcad3d2bd7c63470003b08a93d567nd /* Initial number of connections to remote
f0e395a55abfcad3d2bd7c63470003b08a93d567nd */
f0e395a55abfcad3d2bd7c63470003b08a93d567nd ival = atoi(val);
98fb535f829e2a95aabd82420931f476661fa8e3jorton if (ival < 0)
7cd5419264796cfeaf8215383cf0f89130a81fectrawick return "Min must be a positive number";
7cd5419264796cfeaf8215383cf0f89130a81fectrawick worker->s->min = ival;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick }
7cd5419264796cfeaf8215383cf0f89130a81fectrawick else if (!strcasecmp(key, "max")) {
7cd5419264796cfeaf8215383cf0f89130a81fectrawick /* Maximum number of connections to remote
7cd5419264796cfeaf8215383cf0f89130a81fectrawick */
7cd5419264796cfeaf8215383cf0f89130a81fectrawick ival = atoi(val);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick if (ival < 0)
3568de757bac0b47256647504c186d17ca272f85rbb return "Max must be a positive number";
41634f717c623556a16b27b25d7d909a66fe20f8wrowe worker->s->hmax = ival;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb /* XXX: More inteligent naming needed */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz else if (!strcasecmp(key, "smax")) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Maximum number of connections to remote that
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * will not be destroyed
3568de757bac0b47256647504c186d17ca272f85rbb */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ival = atoi(val);
3568de757bac0b47256647504c186d17ca272f85rbb if (ival < 0)
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz return "Smax must be a positive number";
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->smax = ival;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
41634f717c623556a16b27b25d7d909a66fe20f8wrowe else if (!strcasecmp(key, "acquire")) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Acquire timeout in given unit (default is milliseconds).
3568de757bac0b47256647504c186d17ca272f85rbb * If set this will be the maximum time to
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * wait for a free connection.
3568de757bac0b47256647504c186d17ca272f85rbb */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (ap_timeout_parameter_parse(val, &timeout, "ms") != APR_SUCCESS)
3568de757bac0b47256647504c186d17ca272f85rbb return "Acquire timeout has wrong format";
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (timeout < 1000)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return "Acquire must be at least one millisecond";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding worker->s->acquire = timeout;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz worker->s->acquire_set = 1;
3568de757bac0b47256647504c186d17ca272f85rbb }
fc1efab92032301e317f07e1b3a00082d9d71f3frbb else if (!strcasecmp(key, "timeout")) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* Connection timeout in seconds.
24b534291150023e6b68eca89ddd33e475ccddc0wrowe * Defaults to server timeout.
3568de757bac0b47256647504c186d17ca272f85rbb */
24b534291150023e6b68eca89ddd33e475ccddc0wrowe ival = atoi(val);
3568de757bac0b47256647504c186d17ca272f85rbb if (ival < 1)
24b534291150023e6b68eca89ddd33e475ccddc0wrowe return "Timeout must be at least one second";
24b534291150023e6b68eca89ddd33e475ccddc0wrowe worker->s->timeout = apr_time_from_sec(ival);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz worker->s->timeout_set = 1;
3568de757bac0b47256647504c186d17ca272f85rbb }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz else if (!strcasecmp(key, "iobuffersize")) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz long s = atol(val);
3568de757bac0b47256647504c186d17ca272f85rbb if (s < 512 && s) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz return "IOBufferSize must be >= 512 bytes, or 0 for system default.";
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz }
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->io_buffer_size = (s ? s : AP_IOBUFSIZE);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz worker->s->io_buffer_size_set = 1;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz }
3568de757bac0b47256647504c186d17ca272f85rbb else if (!strcasecmp(key, "receivebuffersize")) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ival = atoi(val);
3568de757bac0b47256647504c186d17ca272f85rbb if (ival < 512 && ival != 0) {
3568de757bac0b47256647504c186d17ca272f85rbb return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->recv_buffer_size = ival;
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->recv_buffer_size_set = 1;
3568de757bac0b47256647504c186d17ca272f85rbb }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz else if (!strcasecmp(key, "keepalive")) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (!strcasecmp(val, "on"))
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz worker->s->keepalive = 1;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz else if (!strcasecmp(val, "off"))
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->keepalive = 0;
3568de757bac0b47256647504c186d17ca272f85rbb else
3568de757bac0b47256647504c186d17ca272f85rbb return "KeepAlive must be On|Off";
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->keepalive_set = 1;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else if (!strcasecmp(key, "disablereuse")) {
3568de757bac0b47256647504c186d17ca272f85rbb if (!strcasecmp(val, "on"))
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz worker->s->disablereuse = 1;
3568de757bac0b47256647504c186d17ca272f85rbb else if (!strcasecmp(val, "off"))
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->disablereuse = 0;
3568de757bac0b47256647504c186d17ca272f85rbb else
3568de757bac0b47256647504c186d17ca272f85rbb return "DisableReuse must be On|Off";
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->disablereuse_set = 1;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else if (!strcasecmp(key, "route")) {
3fa816e4832a1c70600bdfd6fc5ef60e9f1c18bbsf /* Worker route.
397df70abe0bdd78a84fb6c38c02641bcfeadceasf */
397df70abe0bdd78a84fb6c38c02641bcfeadceasf if (strlen(val) >= PROXY_WORKER_MAX_ROUTE_SIZE)
397df70abe0bdd78a84fb6c38c02641bcfeadceasf return "Route length must be < 64 characters";
397df70abe0bdd78a84fb6c38c02641bcfeadceasf PROXY_STRNCPY(worker->s->route, val);
3568de757bac0b47256647504c186d17ca272f85rbb }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (!strcasecmp(key, "redirect")) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Worker redirection route.
3568de757bac0b47256647504c186d17ca272f85rbb */
239f998fbee5ac5b114b965bb76e217cce0003edstoddard if (strlen(val) >= PROXY_WORKER_MAX_ROUTE_SIZE)
78ae889ffe0fdfab72f56c6993b0f302cb48da55rbb return "Redirect length must be < 64 characters";
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick PROXY_STRNCPY(worker->s->redirect, val);
6653a33e820463abd4f81915b7a1eba0f602e200brianp }
6653a33e820463abd4f81915b7a1eba0f602e200brianp else if (!strcasecmp(key, "status")) {
6653a33e820463abd4f81915b7a1eba0f602e200brianp const char *v;
41634f717c623556a16b27b25d7d909a66fe20f8wrowe int mode = 1;
41634f717c623556a16b27b25d7d909a66fe20f8wrowe apr_status_t rv;
6653a33e820463abd4f81915b7a1eba0f602e200brianp /* Worker status.
3568de757bac0b47256647504c186d17ca272f85rbb */
6653a33e820463abd4f81915b7a1eba0f602e200brianp for (v = val; *v; v++) {
6653a33e820463abd4f81915b7a1eba0f602e200brianp if (*v == '+') {
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm mode = 1;
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm v++;
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick }
36c8049de63c446926139936c3d195330a0539cetrawick else if (*v == '-') {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf mode = 0;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf v++;
3568de757bac0b47256647504c186d17ca272f85rbb }
dd028aa8111afb6534fece555e8c2d408894671etrawick rv = ap_proxy_set_wstatus(*v, mode, worker);
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin if (rv != APR_SUCCESS)
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin return "Unknown status parameter option";
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin }
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (!strcasecmp(key, "flushpackets")) {
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin if (!strcasecmp(val, "on"))
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin worker->s->flush_packets = flush_on;
ca53a74f4012a45cbad48e940eddf27d866981f9dougm else if (!strcasecmp(val, "off"))
ca53a74f4012a45cbad48e940eddf27d866981f9dougm worker->s->flush_packets = flush_off;
ca53a74f4012a45cbad48e940eddf27d866981f9dougm else if (!strcasecmp(val, "auto"))
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin worker->s->flush_packets = flush_auto;
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin else
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin return "flushpackets must be on|off|auto";
d90b36a9e6f6ea9a583694f4db5e5edd54a750b3minfrin }
dd028aa8111afb6534fece555e8c2d408894671etrawick else if (!strcasecmp(key, "flushwait")) {
dd028aa8111afb6534fece555e8c2d408894671etrawick ival = atoi(val);
6653a33e820463abd4f81915b7a1eba0f602e200brianp if (ival > 1000 || ival < 0) {
6653a33e820463abd4f81915b7a1eba0f602e200brianp return "flushwait must be <= 1000, or 0 for system default of 10 millseconds.";
6653a33e820463abd4f81915b7a1eba0f602e200brianp }
6653a33e820463abd4f81915b7a1eba0f602e200brianp if (ival == 0)
6653a33e820463abd4f81915b7a1eba0f602e200brianp worker->s->flush_wait = PROXY_FLUSH_WAIT;
6653a33e820463abd4f81915b7a1eba0f602e200brianp else
6653a33e820463abd4f81915b7a1eba0f602e200brianp worker->s->flush_wait = ival * 1000; /* change to microseconds */
6653a33e820463abd4f81915b7a1eba0f602e200brianp }
6653a33e820463abd4f81915b7a1eba0f602e200brianp else if (!strcasecmp(key, "ping")) {
6653a33e820463abd4f81915b7a1eba0f602e200brianp /* Ping/Pong timeout in given unit (default is second).
6653a33e820463abd4f81915b7a1eba0f602e200brianp */
6653a33e820463abd4f81915b7a1eba0f602e200brianp if (ap_timeout_parameter_parse(val, &timeout, "s") != APR_SUCCESS)
6653a33e820463abd4f81915b7a1eba0f602e200brianp return "Ping/Pong timeout has wrong format";
6653a33e820463abd4f81915b7a1eba0f602e200brianp if (timeout < 1000)
6653a33e820463abd4f81915b7a1eba0f602e200brianp return "Ping/Pong timeout must be at least one millisecond";
6653a33e820463abd4f81915b7a1eba0f602e200brianp worker->s->ping_timeout = timeout;
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick worker->s->ping_timeout_set = 1;
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf else if (!strcasecmp(key, "lbset")) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf ival = atoi(val);
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf if (ival < 0 || ival > 99)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return "lbset must be between 0 and 99";
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick worker->s->lbset = ival;
239f998fbee5ac5b114b965bb76e217cce0003edstoddard }
3568de757bac0b47256647504c186d17ca272f85rbb else if (!strcasecmp(key, "connectiontimeout")) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Request timeout in given unit (default is second).
3568de757bac0b47256647504c186d17ca272f85rbb * Defaults to connection timeout
12901074f5d6b36d08be84d8637b6f2c21e0da26trawick */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (ap_timeout_parameter_parse(val, &timeout, "s") != APR_SUCCESS)
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return "Connectiontimeout has wrong format";
3568de757bac0b47256647504c186d17ca272f85rbb if (timeout < 1000)
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz return "Connectiontimeout must be at least one millisecond.";
48d2edbfb84e5559b5da0f8d614ccab805cc67a8rbb worker->s->conn_timeout = timeout;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding worker->s->conn_timeout_set = 1;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (!strcasecmp(key, "flusher")) {
f2e009134c7e279f99dfca5bd421f721bf1f7840jorton if (strlen(val) >= PROXY_WORKER_MAX_SCHEME_SIZE)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return "flusher name length must be < 16 characters";
3568de757bac0b47256647504c186d17ca272f85rbb PROXY_STRNCPY(worker->s->flusher, val);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else {
3568de757bac0b47256647504c186d17ca272f85rbb return "unknown Worker parameter";
3568de757bac0b47256647504c186d17ca272f85rbb }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return NULL;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard}
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantzstatic const char *set_balancer_param(proxy_server_conf *conf,
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard apr_pool_t *p,
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem proxy_balancer *balancer,
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem const char *key,
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem const char *val)
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem{
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem int ival;
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem if (!strcasecmp(key, "stickysession")) {
e2de0e939faab767454a164c7d2e8ea710fd1a26sf char *path;
e2de0e939faab767454a164c7d2e8ea710fd1a26sf /* Balancer sticky session name.
e2de0e939faab767454a164c7d2e8ea710fd1a26sf * Set to something like JSESSIONID or
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * PHPSESSIONID, etc..,
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (strlen(val) > (PROXY_BALANCER_MAX_STICKY_SIZE-1))
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return "stickysession length must be < 64 characters";
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard PROXY_STRNCPY(balancer->s->sticky_path, val);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard PROXY_STRNCPY(balancer->s->sticky, val);
f2e009134c7e279f99dfca5bd421f721bf1f7840jorton
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if ((path = strchr((char *)balancer->s->sticky, '|'))) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard *path++ = '\0';
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard PROXY_STRNCPY(balancer->s->sticky_path, path);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard else if (!strcasecmp(key, "nofailover")) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* If set to 'on' the session will break
3568de757bac0b47256647504c186d17ca272f85rbb * if the worker is in error state or
3568de757bac0b47256647504c186d17ca272f85rbb * disabled.
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick */
7cd5419264796cfeaf8215383cf0f89130a81fectrawick if (!strcasecmp(val, "on"))
7cd5419264796cfeaf8215383cf0f89130a81fectrawick balancer->s->sticky_force = 1;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick else if (!strcasecmp(val, "off"))
7cd5419264796cfeaf8215383cf0f89130a81fectrawick balancer->s->sticky_force = 0;
e8f95a682820a599fe41b22977010636be5c2717jim else
98cd3186185bb28ae6c95a3f159899fcf56a663ftrawick return "failover must be On|Off";
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf else if (!strcasecmp(key, "timeout")) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf /* Balancer timeout in seconds.
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf * If set this will be the maximum time to
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick * wait for a free worker.
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick * Default is not to wait.
3568de757bac0b47256647504c186d17ca272f85rbb */
a72ba68ecbbc61e4b513e50d6000245c33f753dcwrowe ival = atoi(val);
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm if (ival < 1)
397df70abe0bdd78a84fb6c38c02641bcfeadceasf return "timeout must be at least one second";
397df70abe0bdd78a84fb6c38c02641bcfeadceasf balancer->s->timeout = apr_time_from_sec(ival);
397df70abe0bdd78a84fb6c38c02641bcfeadceasf }
397df70abe0bdd78a84fb6c38c02641bcfeadceasf else if (!strcasecmp(key, "maxattempts")) {
397df70abe0bdd78a84fb6c38c02641bcfeadceasf /* Maximum number of failover attempts before
397df70abe0bdd78a84fb6c38c02641bcfeadceasf * giving up.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm ival = atoi(val);
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm if (ival < 0)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return "maximum number of attempts must be a positive number";
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf balancer->s->max_attempts = ival;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf balancer->s->max_attempts_set = 1;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf else if (!strcasecmp(key, "lbmethod")) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf proxy_balancer_method *provider;
3cbd177a6c885562f9ad0cf11695f044489c881dgregames if (strlen(val) > (sizeof(balancer->s->lbpname)-1))
3cbd177a6c885562f9ad0cf11695f044489c881dgregames return "unknown lbmethod";
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (provider) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf balancer->lbmethod = provider;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf if (PROXY_STRNCPY(balancer->s->lbpname, val) == APR_SUCCESS) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return NULL;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf else {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return "lbmethod name too large";
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
5a0f707b48da7703cbe6bc087f13a6735b1c742dgregames }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz return "unknown lbmethod";
5a0f707b48da7703cbe6bc087f13a6735b1c742dgregames }
5a0f707b48da7703cbe6bc087f13a6735b1c742dgregames else if (!strcasecmp(key, "scolonpathdelim")) {
5a0f707b48da7703cbe6bc087f13a6735b1c742dgregames /* If set to 'on' then ';' will also be
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * used as a session path separator/delim (ala
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * mod_jk)
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (!strcasecmp(val, "on"))
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard balancer->s->scolonsep = 1;
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard else if (!strcasecmp(val, "off"))
7cd5419264796cfeaf8215383cf0f89130a81fectrawick balancer->s->scolonsep = 0;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick else
7cd5419264796cfeaf8215383cf0f89130a81fectrawick return "scolonpathdelim must be On|Off";
7cd5419264796cfeaf8215383cf0f89130a81fectrawick }
7cd5419264796cfeaf8215383cf0f89130a81fectrawick else if (!strcasecmp(key, "failonstatus")) {
7cd5419264796cfeaf8215383cf0f89130a81fectrawick char *val_split;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick char *status;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick char *tok_state;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick
4f017cb91aa6591385966ccaf0c326e6b2f2c2b8sf val_split = apr_pstrdup(p, val);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick
7cd5419264796cfeaf8215383cf0f89130a81fectrawick balancer->errstatuses = apr_array_make(p, 1, sizeof(int));
7cd5419264796cfeaf8215383cf0f89130a81fectrawick
7cd5419264796cfeaf8215383cf0f89130a81fectrawick status = apr_strtok(val_split, ", ", &tok_state);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick while (status != NULL) {
7cd5419264796cfeaf8215383cf0f89130a81fectrawick ival = atoi(status);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick if (ap_is_HTTP_VALID_RESPONSE(ival)) {
7cd5419264796cfeaf8215383cf0f89130a81fectrawick *(int *)apr_array_push(balancer->errstatuses) = ival;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick }
4f017cb91aa6591385966ccaf0c326e6b2f2c2b8sf else {
7cd5419264796cfeaf8215383cf0f89130a81fectrawick return "failonstatus must be one or more HTTP response codes";
7cd5419264796cfeaf8215383cf0f89130a81fectrawick }
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard status = apr_strtok(NULL, ", ", &tok_state);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz else if (!strcasecmp(key, "nonce")) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (!strcasecmp(val, "None")) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard *balancer->s->nonce = '\0';
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard else {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (PROXY_STRNCPY(balancer->s->nonce, val) != APR_SUCCESS) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return "Provided nonce is too large";
3568de757bac0b47256647504c186d17ca272f85rbb }
7cd5419264796cfeaf8215383cf0f89130a81fectrawick }
7cd5419264796cfeaf8215383cf0f89130a81fectrawick }
7cd5419264796cfeaf8215383cf0f89130a81fectrawick else if (!strcasecmp(key, "growth")) {
7cd5419264796cfeaf8215383cf0f89130a81fectrawick ival = atoi(val);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick if (ival < 1 || ival > 100) /* arbitrary limit here */
7cd5419264796cfeaf8215383cf0f89130a81fectrawick return "growth must be between 1 and 100";
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm balancer->growth = ival;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else {
74fd6d9aeadb9022086259c5c1ae00bc0dda9c9astoddard return "unknown Balancer parameter";
72b6f1cf3e616473e1c26464b3193b13c2c09e87brianp }
72b6f1cf3e616473e1c26464b3193b13c2c09e87brianp return NULL;
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddardstatic int alias_match(const char *uri, const char *alias_fakename)
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf{
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf const char *end_fakename = alias_fakename + strlen(alias_fakename);
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf const char *aliasp = alias_fakename, *urip = uri;
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf const char *end_uri = uri + strlen(uri);
ddf3c03e4798f13fe6eea22039864261ea09da18sf
b5ffe4f30780fb159db08bd9f628980d2a092711sf while (aliasp < end_fakename && urip < end_uri) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (*aliasp == '/') {
1ce78cf71b5baaf2c1ab48e818cb1f2397df5010trawick /* any number of '/' in the alias matches any number in
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * the supplied URI, but there must be at least one...
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (*urip != '/')
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz return 0;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard while (*aliasp == '/')
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ++aliasp;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz while (*urip == '/')
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ++urip;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
01d315c948a50cb511dbaee108b9571ee9a4d287jim else {
01d315c948a50cb511dbaee108b9571ee9a4d287jim /* Other characters are compared literally */
01d315c948a50cb511dbaee108b9571ee9a4d287jim if (*urip++ != *aliasp++)
dd028aa8111afb6534fece555e8c2d408894671etrawick return 0;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard
3568de757bac0b47256647504c186d17ca272f85rbb /* fixup badly encoded stuff (e.g. % as last character) */
3568de757bac0b47256647504c186d17ca272f85rbb if (aliasp > end_fakename) {
3568de757bac0b47256647504c186d17ca272f85rbb aliasp = end_fakename;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (urip > end_uri) {
f714f1a7002928d785e53e70349700a7f595fee3trawick urip = end_uri;
f714f1a7002928d785e53e70349700a7f595fee3trawick }
3568de757bac0b47256647504c186d17ca272f85rbb
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard /* We reach the end of the uri before the end of "alias_fakename"
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh * for example uri is "/" and alias_fakename "/examples"
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh */
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh if (urip == end_uri && aliasp != end_fakename) {
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh return 0;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* Check last alias path component matched all the way */
663237d6bcc59ac0997d71d48a1baa55fa29a3d8jim if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return 0;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard
3568de757bac0b47256647504c186d17ca272f85rbb /* Return number of characters from URI which matched (may be
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * greater than length of alias, since we may have matched
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * doubled slashes)
663237d6bcc59ac0997d71d48a1baa55fa29a3d8jim */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return urip - uri;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard}
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz/* Detect if an absoluteURI should be proxied or not. Note that we
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * have to do this during this phase because later phases are
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * "short-circuiting"... i.e. translate_names will end when the first
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * module returns OK. So for example, if the request is something like:
3568de757bac0b47256647504c186d17ca272f85rbb *
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard * GET http://othervhost/cgi-bin/printenv HTTP/1.0
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh *
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * mod_alias will notice the /cgi-bin part and ScriptAlias it and
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * short-circuit the proxy... just because of the ordering in the
3568de757bac0b47256647504c186d17ca272f85rbb * configuration file.
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh */
4a13940dc2990df0a798718d3a3f9cf1566c2217bjhstatic int proxy_detect(request_rec *r)
4a13940dc2990df0a798718d3a3f9cf1566c2217bjh{
3568de757bac0b47256647504c186d17ca272f85rbb void *sconf = r->server->module_config;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_server_conf *conf =
663237d6bcc59ac0997d71d48a1baa55fa29a3d8jim (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* Ick... msvc (perhaps others) promotes ternary short results to int */
3568de757bac0b47256647504c186d17ca272f85rbb
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (conf->req && r->parsed_uri.scheme) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* but it might be something vhosted */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (!(r->parsed_uri.hostname
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard && !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))
3568de757bac0b47256647504c186d17ca272f85rbb && ap_matches_request_vhost(r, r->parsed_uri.hostname,
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz : ap_default_port(r))))) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard r->proxyreq = PROXYREQ_PROXY;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard r->uri = r->unparsed_uri;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
3568de757bac0b47256647504c186d17ca272f85rbb r->handler = "proxy-server";
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* We need special treatment for CONNECT proxying: it has no scheme part */
3568de757bac0b47256647504c186d17ca272f85rbb else if (conf->req && r->method_number == M_CONNECT
3568de757bac0b47256647504c186d17ca272f85rbb && r->parsed_uri.hostname
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz && r->parsed_uri.port_str) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz r->proxyreq = PROXYREQ_PROXY;
3568de757bac0b47256647504c186d17ca272f85rbb r->uri = r->unparsed_uri;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
3568de757bac0b47256647504c186d17ca272f85rbb r->handler = "proxy-server";
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return DECLINED;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf}
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf
49aa87d735a13ae3d04012ee0df91ddb51f7c36esfstatic const char *proxy_interpolate(request_rec *r, const char *str)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf{
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf /* Interpolate an env str in a configuration string
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf * Syntax ${var} --> value_of(var)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf * Method: replace one var, and recurse on remainder of string
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf * Nothing clever here, and crap like nested vars may do silly things
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf * but we'll at least avoid sending the unwary into a loop
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf */
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf const char *start;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf const char *end;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf const char *var;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf const char *val;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf const char *firstpart;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf start = ap_strstr_c(str, "${");
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf if (start == NULL) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return str;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf end = ap_strchr_c(start+2, '}');
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf if (end == NULL) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return str;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf /* OK, this is syntax we want to interpolate. Is there such a var ? */
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf var = apr_pstrndup(r->pool, start+2, end-(start+2));
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf val = apr_table_get(r->subprocess_env, var);
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf firstpart = apr_pstrndup(r->pool, str, (start-str));
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf if (val == NULL) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return apr_pstrcat(r->pool, firstpart,
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf proxy_interpolate(r, end+1), NULL);
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf else {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return apr_pstrcat(r->pool, firstpart, val,
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf proxy_interpolate(r, end+1), NULL);
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf}
49aa87d735a13ae3d04012ee0df91ddb51f7c36esfstatic apr_array_header_t *proxy_vars(request_rec *r,
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf apr_array_header_t *hdr)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf{
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf int i;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf apr_array_header_t *ret = apr_array_make(r->pool, hdr->nelts,
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf sizeof (struct proxy_alias));
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf struct proxy_alias *old = (struct proxy_alias *) hdr->elts;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf for (i = 0; i < hdr->nelts; ++i) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf struct proxy_alias *newcopy = apr_array_push(ret);
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf newcopy->fake = (old[i].flags & PROXYPASS_INTERPOLATE)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf ? proxy_interpolate(r, old[i].fake) : old[i].fake;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf newcopy->real = (old[i].flags & PROXYPASS_INTERPOLATE)
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf ? proxy_interpolate(r, old[i].real) : old[i].real;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf }
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf return ret;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf}
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf
49aa87d735a13ae3d04012ee0df91ddb51f7c36esfPROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r, struct proxy_alias *ent,
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf proxy_dir_conf *dconf)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb int len;
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf const char *fake;
3568de757bac0b47256647504c186d17ca272f85rbb const char *real;
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard ap_regmatch_t regm[AP_MAX_REG_MATCH];
3568de757bac0b47256647504c186d17ca272f85rbb ap_regmatch_t reg1[AP_MAX_REG_MATCH];
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick char *found = NULL;
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick int mismatch = 0;
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick unsigned int nocanon = ent->flags & PROXYPASS_NOCANON;
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick const char *use_uri = nocanon ? r->unparsed_uri : r->uri;
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick if (dconf && (dconf->interpolate_env == 1) && (ent->flags & PROXYPASS_INTERPOLATE)) {
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick fake = proxy_interpolate(r, ent->fake);
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick real = proxy_interpolate(r, ent->real);
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick }
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick else {
d69e1ed15b5db3d832c1f6c8c403ef397248857atrawick fake = ent->fake;
beb70d51e435dc36c56a72b6cd83556c04db9283wrowe real = ent->real;
fe6baec9bbcd36f932b71a355120cd7b5a685d6cfielding }
3568de757bac0b47256647504c186d17ca272f85rbb if (ent->regex) {
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf if (!ap_regexec(ent->regex, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
3568de757bac0b47256647504c186d17ca272f85rbb if ((real[0] == '!') && (real[1] == '\0')) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return DECLINED;
3568de757bac0b47256647504c186d17ca272f85rbb }
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf /* test that we haven't reduced the URI */
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf if (nocanon && ap_regexec(ent->regex, r->unparsed_uri,
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf AP_MAX_REG_MATCH, reg1, 0)) {
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf mismatch = 1;
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf use_uri = r->uri;
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf }
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf found = ap_pregsub(r->pool, real, use_uri, AP_MAX_REG_MATCH,
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf (use_uri == r->uri) ? regm : reg1);
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf if (!found) {
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01135)
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf "Substitution in regular expression failed. "
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf "Replacement too long?");
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf return HTTP_INTERNAL_SERVER_ERROR;
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* Note: The strcmp() below catches cases where there
8e9734d1a4af74c141e2a0f880bb51bb061fa03atrawick * was no regex substitution. This is so cases like:
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf *
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf * ProxyPassMatch \.gif balancer://foo
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard *
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick * will work "as expected". The upshot is that the 2
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick * directives below act the exact same way (ie: $1 is implied):
3568de757bac0b47256647504c186d17ca272f85rbb *
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * ProxyPassMatch ^(/.*\.gif)$ balancer://foo
cb97ae2ff6969c2789b8e03f1bc4187fa73b6bafwrowe * ProxyPassMatch ^(/.*\.gif)$ balancer://foo$1
0f113d7123e8bd3e3e2e9b6373461a1a773bfccatrawick *
0f113d7123e8bd3e3e2e9b6373461a1a773bfccatrawick * which may be confusing.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (strcmp(found, real) != 0) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard found = apr_pstrcat(r->pool, "proxy:", found, NULL);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard else {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard found = apr_pstrcat(r->pool, "proxy:", real, use_uri, NULL);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
3568de757bac0b47256647504c186d17ca272f85rbb }
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz else {
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf len = alias_match(r->uri, fake);
8dfa8c6f60f12e0b65eebbb652b629f911f0f84bsf
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (len != 0) {
3568de757bac0b47256647504c186d17ca272f85rbb if ((real[0] == '!') && (real[1] == '\0')) {
3568de757bac0b47256647504c186d17ca272f85rbb return DECLINED;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (nocanon && len != alias_match(r->unparsed_uri, ent->fake)) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick mismatch = 1;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick use_uri = r->uri;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick found = apr_pstrcat(r->pool, "proxy:", real, use_uri + len, NULL);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
f886987cd0bd4220c14043c4d9be77ec22902e73trawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (mismatch) {
f886987cd0bd4220c14043c4d9be77ec22902e73trawick /* We made a reducing transformation, so we can't safely use
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * unparsed_uri. Safe fallback is to ignore nocanon.
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01136)
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "Unescaped URL path matched ProxyPass; ignoring unsafe nocanon");
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (found) {
a72ba68ecbbc61e4b513e50d6000245c33f753dcwrowe r->filename = found;
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick r->handler = "proxy-server";
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm r->proxyreq = PROXYREQ_REVERSE;
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm if (nocanon && !mismatch) {
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* mod_proxy_http needs to be told. Different module. */
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick apr_table_setn(r->notes, "proxy-nocanon", "1");
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick }
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick return OK;
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm }
3568de757bac0b47256647504c186d17ca272f85rbb
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm return DONE;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz}
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantzstatic int proxy_trans(request_rec *r)
e8f95a682820a599fe41b22977010636be5c2717jim{
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm int i;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz struct proxy_alias *ent;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz proxy_dir_conf *dconf;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz proxy_server_conf *conf;
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm if (r->proxyreq) {
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* someone has already set up the proxy, it was possibly ourselves
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * in proxy_detect
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm return OK;
36c8049de63c446926139936c3d195330a0539cetrawick }
36c8049de63c446926139936c3d195330a0539cetrawick
36c8049de63c446926139936c3d195330a0539cetrawick if (strcmp(r->unparsed_uri, "*") == 0) {
36c8049de63c446926139936c3d195330a0539cetrawick /* "*" cannot be proxied. */
36c8049de63c446926139936c3d195330a0539cetrawick return DECLINED;
36c8049de63c446926139936c3d195330a0539cetrawick }
36c8049de63c446926139936c3d195330a0539cetrawick
36c8049de63c446926139936c3d195330a0539cetrawick /* Check that the URI is valid. */
e8f95a682820a599fe41b22977010636be5c2717jim if (!r->uri || r->uri[0] != '/') {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01137)
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm "Invalid URI in request %s", r->the_request);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return HTTP_BAD_REQUEST;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
36c8049de63c446926139936c3d195330a0539cetrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* XXX: since r->uri has been manipulated already we're not really
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * compliant with RFC1945 at this point. But this probably isn't
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * an issue because this is a hybrid proxy/origin server.
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick */
cb97ae2ff6969c2789b8e03f1bc4187fa73b6bafwrowe
36c8049de63c446926139936c3d195330a0539cetrawick dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
36c8049de63c446926139936c3d195330a0539cetrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* short way - this location is reverse proxied? */
e8f95a682820a599fe41b22977010636be5c2717jim if (dconf->alias) {
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm int rv = ap_proxy_trans_match(r, dconf->alias, dconf);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (DONE != rv) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return rv;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick conf = (proxy_server_conf *) ap_get_module_config(r->server->module_config,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick &proxy_module);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* long way - walk the list of aliases, find a match */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (conf->aliases->nelts) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ent = (struct proxy_alias *) conf->aliases->elts;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick for (i = 0; i < conf->aliases->nelts; i++) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick int rv = ap_proxy_trans_match(r, &ent[i], dconf);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (DONE != rv) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return rv;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return DECLINED;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick}
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawickstatic int proxy_walk(request_rec *r)
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick{
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick proxy_server_conf *sconf = ap_get_module_config(r->server->module_config,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick &proxy_module);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_conf_vector_t *per_dir_defaults = r->server->lookup_defaults;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_conf_vector_t **sec_proxy = (ap_conf_vector_t **) sconf->sec_proxy->elts;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_conf_vector_t *entry_config;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick proxy_dir_conf *entry_proxy;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick int num_sec = sconf->sec_proxy->nelts;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* XXX: shouldn't we use URI here? Canonicalize it first?
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * Pass over "proxy:" prefix
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick const char *proxyname = r->filename + 6;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick int j;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick for (j = 0; j < num_sec; ++j)
e8f95a682820a599fe41b22977010636be5c2717jim {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick entry_config = sec_proxy[j];
f886987cd0bd4220c14043c4d9be77ec22902e73trawick entry_proxy = ap_get_module_config(entry_config, &proxy_module);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
f886987cd0bd4220c14043c4d9be77ec22902e73trawick /* XXX: What about case insensitive matching ???
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * Compare regex, fnmatch or string as appropriate
f886987cd0bd4220c14043c4d9be77ec22902e73trawick * If the entry doesn't relate, then continue
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm if (entry_proxy->r
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm ? ap_regexec(entry_proxy->r, proxyname, 0, NULL, 0)
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm : (entry_proxy->p_is_fnmatch
3568de757bac0b47256647504c186d17ca272f85rbb ? apr_fnmatch(entry_proxy->p, proxyname, 0)
72b6f1cf3e616473e1c26464b3193b13c2c09e87brianp : strncmp(proxyname, entry_proxy->p,
72b6f1cf3e616473e1c26464b3193b13c2c09e87brianp strlen(entry_proxy->p)))) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick continue;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick per_dir_defaults = ap_merge_per_dir_configs(r->pool, per_dir_defaults,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick entry_config);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick r->per_dir_config = per_dir_defaults;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return OK;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick}
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
64c351fd973428b5bb4c28e983fa86875ea4e60fdougmstatic int proxy_map_location(request_rec *r)
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick{
f2e009134c7e279f99dfca5bd421f721bf1f7840jorton int access_status;
f2e009134c7e279f99dfca5bd421f721bf1f7840jorton
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm return DECLINED;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* Don't let the core or mod_http map_to_storage hooks handle this,
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * We don't need directory/file_walk, and we want to TRACE on our own.
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if ((access_status = proxy_walk(r))) {
36c8049de63c446926139936c3d195330a0539cetrawick ap_die(access_status, r);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return access_status;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
f886987cd0bd4220c14043c4d9be77ec22902e73trawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return OK;
cb97ae2ff6969c2789b8e03f1bc4187fa73b6bafwrowe}
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
36c8049de63c446926139936c3d195330a0539cetrawick/* -------------------------------------------------------------- */
36c8049de63c446926139936c3d195330a0539cetrawick/* Fixup the filename */
36c8049de63c446926139936c3d195330a0539cetrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick/*
e8f95a682820a599fe41b22977010636be5c2717jim * Canonicalise the URL
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawickstatic int proxy_fixup(request_rec *r)
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick{
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick char *url, *p;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick int access_status;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick &proxy_module);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return DECLINED;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* XXX: Shouldn't we try this before we run the proxy_walk? */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick url = &r->filename[6];
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if ((dconf->interpolate_env == 1) && (r->proxyreq == PROXYREQ_REVERSE)) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* create per-request copy of reverse proxy conf,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * and interpolate vars in it
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick proxy_req_conf *rconf = apr_palloc(r->pool, sizeof(proxy_req_conf));
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_set_module_config(r->request_config, &proxy_module, rconf);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick rconf->raliases = proxy_vars(r, dconf->raliases);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick rconf->cookie_paths = proxy_vars(r, dconf->cookie_paths);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick rconf->cookie_domains = proxy_vars(r, dconf->cookie_domains);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* canonicalise each specific scheme */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if ((access_status = proxy_run_canon_handler(r, url))) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return access_status;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick }
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick p = strchr(url, ':');
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (p == NULL || p == url)
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return HTTP_BAD_REQUEST;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return OK; /* otherwise; we've done the best we can */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick}
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick/* Send a redirection if the request contains a hostname which is not */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick/* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
e8f95a682820a599fe41b22977010636be5c2717jim/* servers like Netscape's allow this and access hosts from the local */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick/* domain in this case. I think it is better to redirect to a FQDN, since */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick/* these will later be found in the bookmarks files. */
f886987cd0bd4220c14043c4d9be77ec22902e73trawick/* The "ProxyDomain" directive determines what domain will be appended */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawickstatic int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
f886987cd0bd4220c14043c4d9be77ec22902e73trawick{
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick char *nuri;
f886987cd0bd4220c14043c4d9be77ec22902e73trawick const char *ref;
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* We only want to worry about GETs */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm return DECLINED;
3568de757bac0b47256647504c186d17ca272f85rbb
72b6f1cf3e616473e1c26464b3193b13c2c09e87brianp /* If host does contain a dot already, or it is "localhost", decline */
72b6f1cf3e616473e1c26464b3193b13c2c09e87brianp if (strchr(r->parsed_uri.hostname, '.') != NULL /* has domain, or IPv4 literal */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick || strchr(r->parsed_uri.hostname, ':') != NULL /* IPv6 literal */
e8f95a682820a599fe41b22977010636be5c2717jim || strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return DECLINED; /* host name has a dot already */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ref = apr_table_get(r->headers_in, "Referer");
e8f95a682820a599fe41b22977010636be5c2717jim
44d2e75323651320b480d8bc2f098448a08de4fcwrowe /* Reassemble the request, but insert the domain after the host name */
44d2e75323651320b480d8bc2f098448a08de4fcwrowe /* Note that the domain name always starts with a dot */
44d2e75323651320b480d8bc2f098448a08de4fcwrowe r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
44d2e75323651320b480d8bc2f098448a08de4fcwrowe domain, NULL);
44d2e75323651320b480d8bc2f098448a08de4fcwrowe nuri = apr_uri_unparse(r->pool,
44d2e75323651320b480d8bc2f098448a08de4fcwrowe &r->parsed_uri,
44d2e75323651320b480d8bc2f098448a08de4fcwrowe APR_URI_UNP_REVEALPASSWORD);
44d2e75323651320b480d8bc2f098448a08de4fcwrowe
44d2e75323651320b480d8bc2f098448a08de4fcwrowe apr_table_setn(r->headers_out, "Location", nuri);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01138)
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick "Domain missing: %s sent to %s%s%s", r->uri,
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick apr_uri_unparse(r->pool, &r->parsed_uri,
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz APR_URI_UNP_OMITUSERINFO),
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ref ? " from " : "", ref ? ref : "");
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe return HTTP_MOVED_PERMANENTLY;
3568de757bac0b47256647504c186d17ca272f85rbb}
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe/* -------------------------------------------------------------- */
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe/* Invoke handler */
f886987cd0bd4220c14043c4d9be77ec22902e73trawick
f886987cd0bd4220c14043c4d9be77ec22902e73trawickstatic int proxy_handler(request_rec *r)
f886987cd0bd4220c14043c4d9be77ec22902e73trawick{
f886987cd0bd4220c14043c4d9be77ec22902e73trawick char *uri, *scheme, *p;
f886987cd0bd4220c14043c4d9be77ec22902e73trawick const char *p2;
f886987cd0bd4220c14043c4d9be77ec22902e73trawick void *sconf = r->server->module_config;
f886987cd0bd4220c14043c4d9be77ec22902e73trawick proxy_server_conf *conf = (proxy_server_conf *)
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe ap_get_module_config(sconf, &proxy_module);
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe apr_array_header_t *proxies = conf->proxies;
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe int i, rc, access_status;
3568de757bac0b47256647504c186d17ca272f85rbb int direct_connect = 0;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz const char *str;
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick long maxfwd;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz proxy_balancer *balancer = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *worker = NULL;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz int attempts = 0, max_attempts = 0;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz struct dirconn_entry *list = (struct dirconn_entry *)conf->dirconn->elts;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* is this for us? */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
3568de757bac0b47256647504c186d17ca272f85rbb return DECLINED;
3568de757bac0b47256647504c186d17ca272f85rbb
98fb535f829e2a95aabd82420931f476661fa8e3jorton /* handle max-forwards / OPTIONS / TRACE */
98fb535f829e2a95aabd82420931f476661fa8e3jorton if ((str = apr_table_get(r->headers_in, "Max-Forwards"))) {
e8f95a682820a599fe41b22977010636be5c2717jim maxfwd = strtol(str, NULL, 10);
e8f95a682820a599fe41b22977010636be5c2717jim if (maxfwd < 1) {
98fb535f829e2a95aabd82420931f476661fa8e3jorton switch (r->method_number) {
98fb535f829e2a95aabd82420931f476661fa8e3jorton case M_TRACE: {
e8f95a682820a599fe41b22977010636be5c2717jim int access_status;
98fb535f829e2a95aabd82420931f476661fa8e3jorton r->proxyreq = PROXYREQ_NONE;
98fb535f829e2a95aabd82420931f476661fa8e3jorton if ((access_status = ap_send_http_trace(r)))
98fb535f829e2a95aabd82420931f476661fa8e3jorton ap_die(access_status, r);
3568de757bac0b47256647504c186d17ca272f85rbb else
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ap_finalize_request_protocol(r);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz return OK;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb case M_OPTIONS: {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int access_status;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->proxyreq = PROXYREQ_NONE;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((access_status = ap_send_http_options(r)))
85c5af648e9f414bd4f157490766d2fd5515a9f5rpluem ap_die(access_status, r);
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe else
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe ap_finalize_request_protocol(r);
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe return OK;
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe }
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe default: {
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe return ap_proxyerror(r, HTTP_BAD_GATEWAY,
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe "Max-Forwards has reached zero - proxy loop?");
85c5af648e9f414bd4f157490766d2fd5515a9f5rpluem }
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe }
3568de757bac0b47256647504c186d17ca272f85rbb }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz maxfwd = (maxfwd > 0) ? maxfwd - 1 : 0;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz }
3568de757bac0b47256647504c186d17ca272f85rbb else {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* set configured max-forwards */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz maxfwd = conf->maxfwd;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb if (maxfwd >= 0) {
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->headers_in, "Max-Forwards",
3568de757bac0b47256647504c186d17ca272f85rbb apr_psprintf(r->pool, "%ld", maxfwd));
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
ec020ca384efb30d501a5af796ddc3bb237d7b8fgregames if (r->method_number == M_TRACE) {
3568de757bac0b47256647504c186d17ca272f85rbb core_server_config *coreconf = (core_server_config *)
ce03576b2434cec77f2921db9d5b6a0510581c23rederpj ap_get_core_module_config(sconf);
397df70abe0bdd78a84fb6c38c02641bcfeadceasf
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick if (coreconf->trace_enable == AP_TRACE_DISABLE)
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick {
397df70abe0bdd78a84fb6c38c02641bcfeadceasf /* Allow "error-notes" string to be printed by ap_send_error_response()
397df70abe0bdd78a84fb6c38c02641bcfeadceasf * Note; this goes nowhere, canned error response need an overhaul.
397df70abe0bdd78a84fb6c38c02641bcfeadceasf */
397df70abe0bdd78a84fb6c38c02641bcfeadceasf apr_table_setn(r->notes, "error-notes",
397df70abe0bdd78a84fb6c38c02641bcfeadceasf "TRACE forbidden by server configuration");
49aa87d735a13ae3d04012ee0df91ddb51f7c36esf apr_table_setn(r->notes, "verbose-error-to", "*");
9d0665da83d1e22c0ea0e5f6f940f70f75bf5237ianh ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01139)
3568de757bac0b47256647504c186d17ca272f85rbb "TRACE forbidden by server configuration");
3568de757bac0b47256647504c186d17ca272f85rbb return HTTP_METHOD_NOT_ALLOWED;
7cd5419264796cfeaf8215383cf0f89130a81fectrawick }
7cd5419264796cfeaf8215383cf0f89130a81fectrawick
7cd5419264796cfeaf8215383cf0f89130a81fectrawick /* Can't test ap_should_client_block, we aren't ready to send
7cd5419264796cfeaf8215383cf0f89130a81fectrawick * the client a 100 Continue response till the connection has
7cd5419264796cfeaf8215383cf0f89130a81fectrawick * been established
7cd5419264796cfeaf8215383cf0f89130a81fectrawick */
7cd5419264796cfeaf8215383cf0f89130a81fectrawick if (coreconf->trace_enable != AP_TRACE_EXTENDED
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe && (r->read_length || r->read_chunked || r->remaining))
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding {
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe /* Allow "error-notes" string to be printed by ap_send_error_response()
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe * Note; this goes nowhere, canned error response need an overhaul.
3568de757bac0b47256647504c186d17ca272f85rbb */
7cd5419264796cfeaf8215383cf0f89130a81fectrawick apr_table_setn(r->notes, "error-notes",
7cd5419264796cfeaf8215383cf0f89130a81fectrawick "TRACE with request body is not allowed");
7cd5419264796cfeaf8215383cf0f89130a81fectrawick apr_table_setn(r->notes, "verbose-error-to", "*");
ca53a74f4012a45cbad48e940eddf27d866981f9dougm ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01140)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "TRACE with request body is not allowed");
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf return HTTP_REQUEST_ENTITY_TOO_LARGE;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
302dc1f7b3feee23a91ad8f3cf3cb2edd95a557bmanoj }
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz uri = r->filename + 6;
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz p = strchr(uri, ':');
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (p == NULL) {
0cb6873985efbf0cc9644114925df6baa4b32d5awrowe ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01141)
3568de757bac0b47256647504c186d17ca272f85rbb "proxy_handler no URL in %s", r->filename);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return HTTP_BAD_REQUEST;
3568de757bac0b47256647504c186d17ca272f85rbb }
/* If the host doesn't have a domain name, add one and redirect. */
if (conf->domain != NULL) {
rc = proxy_needsdomain(r, uri, conf->domain);
if (ap_is_HTTP_REDIRECT(rc))
return HTTP_MOVED_PERMANENTLY;
}
scheme = apr_pstrndup(r->pool, uri, p - uri);
/* Check URI's destination host against NoProxy hosts */
/* Bypass ProxyRemote server lookup if configured as NoProxy */
for (direct_connect = i = 0; i < conf->dirconn->nelts &&
!direct_connect; i++) {
direct_connect = list[i].matcher(&list[i], r);
}
#if DEBUGGING
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
(direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
r->uri);
#endif
do {
char *url = uri;
/* Try to obtain the most suitable worker */
access_status = ap_proxy_pre_request(&worker, &balancer, r, conf, &url);
if (access_status != OK) {
/*
* Only return if access_status is not HTTP_SERVICE_UNAVAILABLE
* This gives other modules the chance to hook into the
* request_status hook and decide what to do in this situation.
*/
if (access_status != HTTP_SERVICE_UNAVAILABLE)
return access_status;
/*
* Ensure that balancer is NULL if worker is NULL to prevent
* potential problems in the post_request hook.
*/
if (!worker)
balancer = NULL;
goto cleanup;
}
/* Initialise worker if needed, note the shared area must be initialized by the balancer logic */
if (balancer) {
ap_proxy_initialize_worker(worker, r->server, conf->pool);
}
if (balancer && balancer->s->max_attempts_set && !max_attempts)
max_attempts = balancer->s->max_attempts;
/* firstly, try a proxy, unless a NoProxy directive is active */
if (!direct_connect) {
for (i = 0; i < proxies->nelts; i++) {
p2 = ap_strchr_c(ents[i].scheme, ':'); /* is it a partial URL? */
if (strcmp(ents[i].scheme, "*") == 0 ||
(ents[i].use_regex &&
ap_regexec(ents[i].regexp, url, 0, NULL, 0) == 0) ||
(p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
(p2 != NULL &&
strncasecmp(url, ents[i].scheme,
strlen(ents[i].scheme)) == 0)) {
/* handle the scheme */
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01142)
"Trying to run scheme_handler against proxy");
access_status = proxy_run_scheme_handler(r, worker,
conf, url,
ents[i].hostname,
ents[i].port);
/* Did the scheme handler process the request? */
if (access_status != DECLINED) {
const char *cl_a;
char *end;
apr_off_t cl;
/*
* An fatal error or success, so no point in
* retrying with a direct connection.
*/
if (access_status != HTTP_BAD_GATEWAY) {
goto cleanup;
}
cl_a = apr_table_get(r->headers_in, "Content-Length");
if (cl_a) {
apr_strtoff(&cl, cl_a, &end, 10);
/*
* The request body is of length > 0. We cannot
* retry with a direct connection since we already
* sent (parts of) the request body to the proxy
* and do not have any longer.
*/
if (cl > 0) {
goto cleanup;
}
}
/*
* Transfer-Encoding was set as input header, so we had
* a request body. We cannot retry with a direct
* connection for the same reason as above.
*/
if (apr_table_get(r->headers_in, "Transfer-Encoding")) {
goto cleanup;
}
}
}
}
}
/* 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 */
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01143)
"Running scheme %s handler (attempt %d)",
scheme, attempts);
AP_PROXY_RUN(r, worker, conf, url, attempts);
access_status = proxy_run_scheme_handler(r, worker, conf,
url, NULL, 0);
if (access_status == OK)
break;
else if (access_status == HTTP_INTERNAL_SERVER_ERROR) {
/* Unrecoverable server error.
* We can not failover to another worker.
* Mark the worker as unusable if member of load balancer
*/
if (balancer) {
worker->s->status |= PROXY_WORKER_IN_ERROR;
worker->s->error_time = apr_time_now();
}
break;
}
else if (access_status == HTTP_SERVICE_UNAVAILABLE) {
/* Recoverable server error.
* We can failover to another worker
* Mark the worker as unusable if member of load balancer
*/
if (balancer) {
worker->s->status |= PROXY_WORKER_IN_ERROR;
worker->s->error_time = apr_time_now();
}
}
else {
/* Unrecoverable error.
* Return the origin status code to the client.
*/
break;
}
/* Try again if the worker is unusable and the service is
* unavailable.
*/
} while (!PROXY_WORKER_IS_USABLE(worker) &&
max_attempts > attempts++);
if (DECLINED == access_status) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01144)
"No protocol handler was valid for the URL %s. "
"If you are using a DSO version of mod_proxy, make sure "
"the proxy submodules are included in the configuration "
"using LoadModule.", r->uri);
access_status = HTTP_INTERNAL_SERVER_ERROR;
goto cleanup;
}
cleanup:
ap_proxy_post_request(worker, balancer, r, conf);
proxy_run_request_status(&access_status, r);
AP_PROXY_RUN_FINISHED(r, attempts, access_status);
return access_status;
}
/* -------------------------------------------------------------- */
/* Setup configurable data */
static void * create_proxy_config(apr_pool_t *p, server_rec *s)
{
unsigned int id;
proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
ps->sec_proxy = apr_array_make(p, 10, sizeof(ap_conf_vector_t *));
ps->proxies = apr_array_make(p, 10, sizeof(struct proxy_remote));
ps->aliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
ps->noproxies = apr_array_make(p, 10, sizeof(struct noproxy_entry));
ps->dirconn = apr_array_make(p, 10, sizeof(struct dirconn_entry));
ps->workers = apr_array_make(p, 10, sizeof(proxy_worker));
ps->balancers = apr_array_make(p, 10, sizeof(proxy_balancer));
ps->forward = NULL;
ps->reverse = NULL;
ps->domain = NULL;
#if 0
id = ap_proxy_hashfunc(apr_psprintf(p, "%pp-%" APR_TIME_T_FMT, ps, apr_time_now()), PROXY_HASHFUNC_DEFAULT);
#else
id = ap_proxy_hashfunc(apr_psprintf(p, "%pp", ps), PROXY_HASHFUNC_DEFAULT);
#endif
ps->id = apr_psprintf(p, "s%x", id);
ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
ps->viaopt_set = 0; /* 0 means default */
ps->req = 0;
ps->max_balancers = 0;
ps->bgrowth = 5;
ps->bgrowth_set = 0;
ps->req_set = 0;
ps->recv_buffer_size = 0; /* this default was left unset for some reason */
ps->recv_buffer_size_set = 0;
ps->io_buffer_size = AP_IOBUFSIZE;
ps->io_buffer_size_set = 0;
ps->maxfwd = DEFAULT_MAX_FORWARDS;
ps->maxfwd_set = 0;
ps->timeout = 0;
ps->timeout_set = 0;
ps->badopt = bad_error;
ps->badopt_set = 0;
ps->source_address = NULL;
ps->source_address_set = 0;
ps->pool = p;
return ps;
}
static void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
{
proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
proxy_server_conf *base = (proxy_server_conf *) basev;
proxy_server_conf *overrides = (proxy_server_conf *) overridesv;
ps->proxies = apr_array_append(p, base->proxies, overrides->proxies);
ps->sec_proxy = apr_array_append(p, base->sec_proxy, overrides->sec_proxy);
ps->aliases = apr_array_append(p, base->aliases, overrides->aliases);
ps->noproxies = apr_array_append(p, base->noproxies, overrides->noproxies);
ps->dirconn = apr_array_append(p, base->dirconn, overrides->dirconn);
ps->workers = apr_array_append(p, base->workers, overrides->workers);
ps->balancers = apr_array_append(p, base->balancers, overrides->balancers);
ps->forward = overrides->forward ? overrides->forward : base->forward;
ps->reverse = overrides->reverse ? overrides->reverse : base->reverse;
ps->domain = (overrides->domain == NULL) ? base->domain : overrides->domain;
ps->id = (overrides->id == NULL) ? base->id : overrides->id;
ps->viaopt = (overrides->viaopt_set == 0) ? base->viaopt : overrides->viaopt;
ps->viaopt_set = overrides->viaopt_set || base->viaopt_set;
ps->req = (overrides->req_set == 0) ? base->req : overrides->req;
ps->req_set = overrides->req_set || base->req_set;
ps->bgrowth = (overrides->bgrowth_set == 0) ? base->bgrowth : overrides->bgrowth;
ps->bgrowth_set = overrides->bgrowth_set || base->bgrowth_set;
ps->max_balancers = overrides->max_balancers || base->max_balancers;
ps->recv_buffer_size = (overrides->recv_buffer_size_set == 0) ? base->recv_buffer_size : overrides->recv_buffer_size;
ps->recv_buffer_size_set = overrides->recv_buffer_size_set || base->recv_buffer_size_set;
ps->io_buffer_size = (overrides->io_buffer_size_set == 0) ? base->io_buffer_size : overrides->io_buffer_size;
ps->io_buffer_size_set = overrides->io_buffer_size_set || base->io_buffer_size_set;
ps->maxfwd = (overrides->maxfwd_set == 0) ? base->maxfwd : overrides->maxfwd;
ps->maxfwd_set = overrides->maxfwd_set || base->maxfwd_set;
ps->timeout = (overrides->timeout_set == 0) ? base->timeout : overrides->timeout;
ps->timeout_set = overrides->timeout_set || base->timeout_set;
ps->badopt = (overrides->badopt_set == 0) ? base->badopt : overrides->badopt;
ps->badopt_set = overrides->badopt_set || base->badopt_set;
ps->proxy_status = (overrides->proxy_status_set == 0) ? base->proxy_status : overrides->proxy_status;
ps->proxy_status_set = overrides->proxy_status_set || base->proxy_status_set;
ps->source_address = (overrides->source_address_set == 0) ? base->source_address : overrides->source_address;
ps->source_address_set = overrides->source_address_set || base->source_address_set;
ps->pool = p;
return ps;
}
static const char *set_source_address(cmd_parms *parms, void *dummy,
const char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
struct apr_sockaddr_t *addr;
if (APR_SUCCESS == apr_sockaddr_info_get(&addr, arg, APR_UNSPEC, 0, 0,
psf->pool)) {
psf->source_address = addr;
psf->source_address_set = 1;
}
else {
return "ProxySourceAddress invalid value";
}
return NULL;
}
static void *create_proxy_dir_config(apr_pool_t *p, char *dummy)
{
proxy_dir_conf *new =
(proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
/* Filled in by proxysection, when applicable */
/* Put these in the dir config so they work inside <Location> */
new->raliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
new->cookie_paths = apr_array_make(p, 10, sizeof(struct proxy_alias));
new->cookie_domains = apr_array_make(p, 10, sizeof(struct proxy_alias));
new->preserve_host_set = 0;
new->preserve_host = 0;
new->interpolate_env = -1; /* unset */
new->error_override = 0;
new->error_override_set = 0;
new->add_forwarded_headers = 1;
return (void *) new;
}
static void *merge_proxy_dir_config(apr_pool_t *p, void *basev, void *addv)
{
proxy_dir_conf *new = (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
proxy_dir_conf *add = (proxy_dir_conf *) addv;
proxy_dir_conf *base = (proxy_dir_conf *) basev;
new->p = add->p;
new->p_is_fnmatch = add->p_is_fnmatch;
new->r = add->r;
/* Put these in the dir config so they work inside <Location> */
new->raliases = apr_array_append(p, base->raliases, add->raliases);
new->cookie_paths
= apr_array_append(p, base->cookie_paths, add->cookie_paths);
new->cookie_domains
= apr_array_append(p, base->cookie_domains, add->cookie_domains);
new->interpolate_env = (add->interpolate_env == -1) ? base->interpolate_env
: add->interpolate_env;
new->preserve_host = (add->preserve_host_set == 0) ? base->preserve_host
: add->preserve_host;
new->preserve_host_set = add->preserve_host_set || base->preserve_host_set;
new->error_override = (add->error_override_set == 0) ? base->error_override
: add->error_override;
new->error_override_set = add->error_override_set || base->error_override_set;
new->alias = (add->alias_set == 0) ? base->alias : add->alias;
new->alias_set = add->alias_set || base->alias_set;
new->add_forwarded_headers = add->add_forwarded_headers;
return new;
}
static const char *
add_proxy(cmd_parms *cmd, void *dummy, const char *f1, const char *r1, int regex)
{
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;
char *r, *f, *scheme;
ap_regex_t *reg = NULL;
int port;
r = apr_pstrdup(cmd->pool, r1);
scheme = apr_pstrdup(cmd->pool, r1);
f = apr_pstrdup(cmd->pool, f1);
p = strchr(r, ':');
if (p == NULL || p[1] != '/' || p[2] != '/' || p[3] == '\0') {
if (regex)
return "ProxyRemoteMatch: Bad syntax for a remote proxy server";
else
return "ProxyRemote: Bad syntax for a remote proxy server";
}
else {
scheme[p-r] = 0;
}
q = strchr(p + 3, ':');
if (q != NULL) {
if (sscanf(q + 1, "%u", &port) != 1 || port > 65535) {
if (regex)
return "ProxyRemoteMatch: Bad syntax for a remote proxy server (bad port number)";
else
return "ProxyRemote: Bad syntax for a remote proxy server (bad port number)";
}
*q = '\0';
}
else
port = -1;
*p = '\0';
if (regex) {
reg = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
if (!reg)
return "Regular expression for ProxyRemoteMatch could not be compiled.";
}
else
if (strchr(f, ':') == NULL)
ap_str_tolower(f); /* lowercase scheme */
ap_str_tolower(p + 3); /* lowercase hostname */
if (port == -1) {
port = apr_uri_port_of_scheme(scheme);
}
new = apr_array_push(conf->proxies);
new->scheme = f;
new->protocol = r;
new->hostname = p + 3;
new->port = port;
new->regexp = reg;
new->use_regex = regex;
return NULL;
}
static const char *
add_proxy_noregex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
{
return add_proxy(cmd, dummy, f1, r1, 0);
}
static const char *
add_proxy_regex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
{
return add_proxy(cmd, dummy, f1, r1, 1);
}
static const char *
add_pass(cmd_parms *cmd, void *dummy, const char *arg, int is_regex)
{
proxy_dir_conf *dconf = (proxy_dir_conf *)dummy;
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;
char *f = cmd->path;
char *r = NULL;
char *word;
apr_table_t *params = apr_table_make(cmd->pool, 5);
const apr_array_header_t *arr;
const apr_table_entry_t *elts;
int i;
int use_regex = is_regex;
unsigned int flags = 0;
const char *err;
err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES);
if (err) {
return err;
}
while (*arg) {
word = ap_getword_conf(cmd->pool, &arg);
if (!f) {
if (!strcmp(word, "~")) {
if (is_regex) {
return "ProxyPassMatch invalid syntax ('~' usage).";
}
use_regex = 1;
continue;
}
f = word;
}
else if (!r) {
r = word;
}
else if (!strcasecmp(word,"nocanon")) {
flags |= PROXYPASS_NOCANON;
}
else if (!strcasecmp(word,"interpolate")) {
flags |= PROXYPASS_INTERPOLATE;
}
else {
char *val = strchr(word, '=');
if (!val) {
if (cmd->path) {
if (*r == '/') {
return "ProxyPass|ProxyPassMatch can not have a path when defined in "
"a location.";
}
else {
return "Invalid ProxyPass|ProxyPassMatch parameter. Parameter must "
"be in the form 'key=value'.";
}
}
else {
return "Invalid ProxyPass|ProxyPassMatch parameter. Parameter must be "
"in the form 'key=value'.";
}
}
else
*val++ = '\0';
apr_table_setn(params, word, val);
}
};
if (r == NULL) {
return "ProxyPass|ProxyPassMatch needs a path when not defined in a location";
}
/* if per directory, save away the single alias */
if (cmd->path) {
dconf->alias = apr_pcalloc(cmd->pool, sizeof(struct proxy_alias));
dconf->alias_set = 1;
new = dconf->alias;
if (apr_fnmatch_test(f)) {
use_regex = 1;
}
}
/* if per server, add to the alias array */
else {
new = apr_array_push(conf->aliases);
}
new->fake = apr_pstrdup(cmd->pool, f);
new->real = apr_pstrdup(cmd->pool, r);
new->flags = flags;
if (use_regex) {
new->regex = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
if (new->regex == NULL)
return "Regular expression could not be compiled.";
}
else {
new->regex = NULL;
}
if (r[0] == '!' && r[1] == '\0')
return NULL;
arr = apr_table_elts(params);
elts = (const apr_table_entry_t *)arr->elts;
/* Distinguish the balancer from worker */
if (ap_proxy_valid_balancer_name(r, 9)) {
proxy_balancer *balancer = ap_proxy_get_balancer(cmd->pool, conf, r, 0);
if (!balancer) {
const char *err = ap_proxy_define_balancer(cmd->pool, &balancer, conf, r, f, 0);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
}
else {
ap_proxy_update_balancer(cmd->pool, balancer, f);
}
for (i = 0; i < arr->nelts; i++) {
const char *err = set_balancer_param(conf, cmd->pool, balancer, elts[i].key,
elts[i].val);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
}
new->balancer = balancer;
}
else {
proxy_worker *worker = ap_proxy_get_worker(cmd->temp_pool, NULL, conf, r);
int reuse = 0;
if (!worker) {
const char *err = ap_proxy_define_worker(cmd->pool, &worker, NULL, conf, r, 0);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
PROXY_COPY_CONF_PARAMS(worker, conf);
} else {
reuse = 1;
ap_log_error(APLOG_MARK, APLOG_INFO, 0, cmd->server, APLOGNO(01145)
"Sharing worker '%s' instead of creating new worker '%s'",
worker->s->name, new->real);
}
for (i = 0; i < arr->nelts; i++) {
if (reuse) {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(01146)
"Ignoring parameter '%s=%s' for worker '%s' because of worker sharing",
elts[i].key, elts[i].val, worker->s->name);
} else {
const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
elts[i].val);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
}
}
}
return NULL;
}
static const char *
add_pass_noregex(cmd_parms *cmd, void *dummy, const char *arg)
{
return add_pass(cmd, dummy, arg, 0);
}
static const char *
add_pass_regex(cmd_parms *cmd, void *dummy, const char *arg)
{
return add_pass(cmd, dummy, arg, 1);
}
static const char * add_pass_reverse(cmd_parms *cmd, void *dconf, const char *f,
const char *r, const char *i)
{
proxy_dir_conf *conf = dconf;
struct proxy_alias *new;
const char *fake;
const char *real;
const char *interp;
const char *err;
err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES);
if (err) {
return err;
}
if (cmd->path == NULL) {
if (r == NULL || !strcasecmp(r, "interpolate")) {
return "ProxyPassReverse needs a path when not defined in a location";
}
fake = f;
real = r;
interp = i;
}
else {
if (r && strcasecmp(r, "interpolate")) {
return "ProxyPassReverse can not have a path when defined in a location";
}
fake = cmd->path;
real = f;
interp = r;
}
new = apr_array_push(conf->raliases);
new->fake = fake;
new->real = real;
new->flags = interp ? PROXYPASS_INTERPOLATE : 0;
return NULL;
}
static const char* cookie_path(cmd_parms *cmd, void *dconf, const char *f,
const char *r, const char *interp)
{
proxy_dir_conf *conf = dconf;
struct proxy_alias *new;
new = apr_array_push(conf->cookie_paths);
new->fake = f;
new->real = r;
new->flags = interp ? PROXYPASS_INTERPOLATE : 0;
return NULL;
}
static const char* cookie_domain(cmd_parms *cmd, void *dconf, const char *f,
const char *r, const char *interp)
{
proxy_dir_conf *conf = dconf;
struct proxy_alias *new;
new = apr_array_push(conf->cookie_domains);
new->fake = f;
new->real = r;
new->flags = interp ? PROXYPASS_INTERPOLATE : 0;
return NULL;
}
static const char *
set_proxy_exclude(cmd_parms *parms, void *dummy, const 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 apr_sockaddr_t *addr;
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 = apr_array_push(conf->noproxies);
new->name = arg;
if (APR_SUCCESS == apr_sockaddr_info_get(&addr, new->name, APR_UNSPEC, 0, 0, parms->pool)) {
new->addr = addr;
}
else {
new->addr = NULL;
}
}
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, const 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 = apr_array_push(conf->dirconn);
New->name = apr_pstrdup(parms->pool, arg);
New->hostaddr = NULL;
if (ap_proxy_is_ipaddr(New, parms->pool)) {
#if DEBUGGING
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"Parsed addr %s", inet_ntoa(New->addr));
ap_log_error(APLOG_MARK, APLOG_STARTUP, 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, 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, 0, NULL,
"Parsed host %s", New->name);
#endif
}
else {
ap_proxy_is_word(New, parms->pool);
#if DEBUGGING
fprintf(stderr, "Parsed word %s\n", New->name);
#endif
}
}
return NULL;
}
static const char *
set_proxy_domain(cmd_parms *parms, void *dummy, const 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;
psf->req_set = 1;
return NULL;
}
static const char *
set_proxy_error_override(cmd_parms *parms, void *dconf, int flag)
{
proxy_dir_conf *conf = dconf;
conf->error_override = flag;
conf->error_override_set = 1;
return NULL;
}
static const char *
add_proxy_http_headers(cmd_parms *parms, void *dconf, int flag)
{
proxy_dir_conf *conf = dconf;
conf->add_forwarded_headers = flag;
return NULL;
}
static const char *
set_preserve_host(cmd_parms *parms, void *dconf, int flag)
{
proxy_dir_conf *conf = dconf;
conf->preserve_host = flag;
conf->preserve_host_set = 1;
return NULL;
}
static const char *
set_recv_buffer_size(cmd_parms *parms, void *dummy, const 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;
psf->recv_buffer_size_set = 1;
return NULL;
}
static const char *
set_io_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
long s = atol(arg);
if (s < 512 && s) {
return "ProxyIOBufferSize must be >= 512 bytes, or 0 for system default.";
}
psf->io_buffer_size = (s ? s : AP_IOBUFSIZE);
psf->io_buffer_size_set = 1;
return NULL;
}
static const char *
set_max_forwards(cmd_parms *parms, void *dummy, const char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
long s = atol(arg);
psf->maxfwd = s;
psf->maxfwd_set = 1;
return NULL;
}
static const char*
set_proxy_timeout(cmd_parms *parms, void *dummy, const char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
int timeout;
timeout = atoi(arg);
if (timeout<1) {
return "Proxy Timeout must be at least 1 second.";
}
psf->timeout_set = 1;
psf->timeout = apr_time_from_sec(timeout);
return NULL;
}
static const char*
set_via_opt(cmd_parms *parms, void *dummy, const 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";
}
psf->viaopt_set = 1;
return NULL;
}
static const char*
set_bad_opt(cmd_parms *parms, void *dummy, const char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
if (strcasecmp(arg, "IsError") == 0)
psf->badopt = bad_error;
else if (strcasecmp(arg, "Ignore") == 0)
psf->badopt = bad_ignore;
else if (strcasecmp(arg, "StartBody") == 0)
psf->badopt = bad_body;
else {
return "ProxyBadHeader must be one of: "
"IsError | Ignore | StartBody";
}
psf->badopt_set = 1;
return NULL;
}
static const char*
set_status_opt(cmd_parms *parms, void *dummy, const char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
if (strcasecmp(arg, "Off") == 0)
psf->proxy_status = status_off;
else if (strcasecmp(arg, "On") == 0)
psf->proxy_status = status_on;
else if (strcasecmp(arg, "Full") == 0)
psf->proxy_status = status_full;
else {
return "ProxyStatus must be one of: "
"off | on | full";
}
psf->proxy_status_set = 1;
return NULL;
}
static const char *set_bgrowth(cmd_parms *parms, void *dummy, const char *arg)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
int growth = atoi(arg);
if (growth < 0 || growth > 1000) {
return "BalancerGrowth must be between 0 and 1000";
}
psf->bgrowth = growth;
psf->bgrowth_set = 1;
return NULL;
}
static const char *add_member(cmd_parms *cmd, void *dummy, const char *arg)
{
server_rec *s = cmd->server;
proxy_server_conf *conf =
ap_get_module_config(s->module_config, &proxy_module);
proxy_balancer *balancer;
proxy_worker *worker;
char *path = cmd->path;
char *name = NULL;
char *word;
apr_table_t *params = apr_table_make(cmd->pool, 5);
const apr_array_header_t *arr;
const apr_table_entry_t *elts;
int reuse = 0;
int i;
/* XXX: Should this be NOT_IN_DIRECTORY|NOT_IN_FILES? */
const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
if (err)
return err;
if (cmd->path)
path = apr_pstrdup(cmd->pool, cmd->path);
while (*arg) {
char *val;
word = ap_getword_conf(cmd->pool, &arg);
val = strchr(word, '=');
if (!val) {
if (!path)
path = word;
else if (!name)
name = word;
else {
if (cmd->path)
return "BalancerMember can not have a balancer name when defined in a location";
else
return "Invalid BalancerMember parameter. Parameter must "
"be in the form 'key=value'";
}
} else {
*val++ = '\0';
apr_table_setn(params, word, val);
}
}
if (!path)
return "BalancerMember must define balancer name when outside <Proxy > section";
if (!name)
return "BalancerMember must define remote proxy server";
ap_str_tolower(path); /* lowercase scheme://hostname */
/* Try to find the balancer */
balancer = ap_proxy_get_balancer(cmd->temp_pool, conf, path, 0);
if (!balancer) {
err = ap_proxy_define_balancer(cmd->pool, &balancer, conf, path, "/", 0);
if (err)
return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
}
/* Try to find existing worker */
worker = ap_proxy_get_worker(cmd->temp_pool, balancer, conf, name);
if (!worker) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, APLOGNO(01147)
"Defining worker '%s' for balancer '%s'",
name, balancer->s->name);
if ((err = ap_proxy_define_worker(cmd->pool, &worker, balancer, conf, name, 0)) != NULL)
return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, APLOGNO(01148)
"Defined worker '%s' for balancer '%s'",
worker->s->name, balancer->s->name);
PROXY_COPY_CONF_PARAMS(worker, conf);
} else {
reuse = 1;
ap_log_error(APLOG_MARK, APLOG_INFO, 0, cmd->server, APLOGNO(01149)
"Sharing worker '%s' instead of creating new worker '%s'",
worker->s->name, name);
}
arr = apr_table_elts(params);
elts = (const apr_table_entry_t *)arr->elts;
for (i = 0; i < arr->nelts; i++) {
if (reuse) {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(01150)
"Ignoring parameter '%s=%s' for worker '%s' because of worker sharing",
elts[i].key, elts[i].val, worker->s->name);
} else {
err = set_worker_param(cmd->pool, worker, elts[i].key,
elts[i].val);
if (err)
return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
}
}
return NULL;
}
static const char *
set_proxy_param(cmd_parms *cmd, void *dummy, const char *arg)
{
server_rec *s = cmd->server;
proxy_server_conf *conf =
(proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
char *name = NULL;
char *word, *val;
proxy_balancer *balancer = NULL;
proxy_worker *worker = NULL;
int in_proxy_section = 0;
/* XXX: Should this be NOT_IN_DIRECTORY|NOT_IN_FILES? */
const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
if (err)
return err;
if (cmd->directive->parent &&
strncasecmp(cmd->directive->parent->directive,
"<Proxy", 6) == 0) {
const char *pargs = cmd->directive->parent->args;
/* Directive inside <Proxy section
* Parent directive arg is the worker/balancer name.
*/
name = ap_getword_conf(cmd->temp_pool, &pargs);
if ((word = ap_strchr(name, '>')))
*word = '\0';
in_proxy_section = 1;
}
else {
/* Standard set directive with worker/balancer
* name as first param.
*/
name = ap_getword_conf(cmd->temp_pool, &arg);
}
if (ap_proxy_valid_balancer_name(name, 9)) {
balancer = ap_proxy_get_balancer(cmd->pool, conf, name, 0);
if (!balancer) {
if (in_proxy_section) {
err = ap_proxy_define_balancer(cmd->pool, &balancer, conf, name, "/", 0);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxySet ",
err, NULL);
}
else
return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
name, "' Balancer.", NULL);
}
}
else {
worker = ap_proxy_get_worker(cmd->temp_pool, NULL, conf, name);
if (!worker) {
if (in_proxy_section) {
err = ap_proxy_define_worker(cmd->pool, &worker, NULL,
conf, name, 0);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxySet ",
err, NULL);
}
else
return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
name, "' Worker.", NULL);
}
}
while (*arg) {
word = ap_getword_conf(cmd->pool, &arg);
val = strchr(word, '=');
if (!val) {
return "Invalid ProxySet parameter. Parameter must be "
"in the form 'key=value'";
}
else
*val++ = '\0';
if (worker)
err = set_worker_param(cmd->pool, worker, word, val);
else
err = set_balancer_param(conf, cmd->pool, balancer, word, val);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxySet: ", err, " ", word, "=", val, "; ", name, NULL);
}
return NULL;
}
static void ap_add_per_proxy_conf(server_rec *s, ap_conf_vector_t *dir_config)
{
proxy_server_conf *sconf = ap_get_module_config(s->module_config,
&proxy_module);
void **new_space = (void **)apr_array_push(sconf->sec_proxy);
*new_space = dir_config;
}
static const char *proxysection(cmd_parms *cmd, void *mconfig, const char *arg)
{
const char *errmsg;
const char *endp = ap_strrchr_c(arg, '>');
int old_overrides = cmd->override;
char *old_path = cmd->path;
proxy_dir_conf *conf;
ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
ap_regex_t *r = NULL;
const command_rec *thiscmd = cmd->cmd;
char *word, *val;
proxy_balancer *balancer = NULL;
proxy_worker *worker = NULL;
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
proxy_server_conf *sconf =
(proxy_server_conf *) ap_get_module_config(cmd->server->module_config, &proxy_module);
if (err != NULL) {
return err;
}
if (endp == NULL) {
return apr_pstrcat(cmd->pool, cmd->cmd->name,
"> directive missing closing '>'", NULL);
}
arg = apr_pstrndup(cmd->pool, arg, endp-arg);
if (!arg) {
if (thiscmd->cmd_data)
return "<ProxyMatch > block must specify a path";
else
return "<Proxy > block must specify a path";
}
cmd->path = ap_getword_conf(cmd->pool, &arg);
cmd->override = OR_ALL|ACCESS_CONF;
if (!strncasecmp(cmd->path, "proxy:", 6))
cmd->path += 6;
/* XXX Ignore case? What if we proxy a case-insensitive server?!?
* While we are at it, shouldn't we also canonicalize the entire
* scheme? See proxy_fixup()
*/
if (thiscmd->cmd_data) { /* <ProxyMatch> */
r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
if (!r) {
return "Regex could not be compiled";
}
}
else if (!strcmp(cmd->path, "~")) {
cmd->path = ap_getword_conf(cmd->pool, &arg);
if (!cmd->path)
return "<Proxy ~ > block must specify a path";
if (strncasecmp(cmd->path, "proxy:", 6))
cmd->path += 6;
r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
if (!r) {
return "Regex could not be compiled";
}
}
/* initialize our config and fetch it */
conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
&proxy_module, cmd->pool);
errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
if (errmsg != NULL)
return errmsg;
conf->r = r;
conf->p = cmd->path;
conf->p_is_fnmatch = apr_fnmatch_test(conf->p);
ap_add_per_proxy_conf(cmd->server, new_dir_conf);
if (*arg != '\0') {
if (thiscmd->cmd_data)
return "Multiple <ProxyMatch> arguments not (yet) supported.";
if (conf->p_is_fnmatch)
return apr_pstrcat(cmd->pool, thiscmd->name,
"> arguments are not supported for wildchar url.",
NULL);
if (!ap_strchr_c(conf->p, ':'))
return apr_pstrcat(cmd->pool, thiscmd->name,
"> arguments are not supported for non url.",
NULL);
if (ap_proxy_valid_balancer_name((char *)conf->p, 9)) {
balancer = ap_proxy_get_balancer(cmd->pool, sconf, conf->p, 0);
if (!balancer) {
err = ap_proxy_define_balancer(cmd->pool, &balancer,
sconf, conf->p, "/", 0);
if (err)
return apr_pstrcat(cmd->temp_pool, thiscmd->name,
" ", err, NULL);
}
}
else {
worker = ap_proxy_get_worker(cmd->temp_pool, NULL, sconf,
conf->p);
if (!worker) {
err = ap_proxy_define_worker(cmd->pool, &worker, NULL,
sconf, conf->p, 0);
if (err)
return apr_pstrcat(cmd->temp_pool, thiscmd->name,
" ", err, NULL);
}
}
if (worker == NULL && balancer == NULL) {
return apr_pstrcat(cmd->pool, thiscmd->name,
"> arguments are supported only for workers.",
NULL);
}
while (*arg) {
word = ap_getword_conf(cmd->pool, &arg);
val = strchr(word, '=');
if (!val) {
return "Invalid Proxy parameter. Parameter must be "
"in the form 'key=value'";
}
else
*val++ = '\0';
if (worker)
err = set_worker_param(cmd->pool, worker, word, val);
else
err = set_balancer_param(sconf, cmd->pool, balancer,
word, val);
if (err)
return apr_pstrcat(cmd->temp_pool, thiscmd->name, " ", err, " ",
word, "=", val, "; ", conf->p, NULL);
}
}
cmd->path = old_path;
cmd->override = old_overrides;
return NULL;
}
static const command_rec proxy_cmds[] =
{
AP_INIT_RAW_ARGS("<Proxy", proxysection, NULL, RSRC_CONF,
"Container for directives affecting resources located in the proxied "
"location"),
AP_INIT_RAW_ARGS("<ProxyMatch", proxysection, (void*)1, RSRC_CONF,
"Container for directives affecting resources located in the proxied "
"location, in regular expression syntax"),
AP_INIT_FLAG("ProxyRequests", set_proxy_req, NULL, RSRC_CONF,
"on if the true proxy requests should be accepted"),
AP_INIT_TAKE2("ProxyRemote", add_proxy_noregex, NULL, RSRC_CONF,
"a scheme, partial URL or '*' and a proxy server"),
AP_INIT_TAKE2("ProxyRemoteMatch", add_proxy_regex, NULL, RSRC_CONF,
"a regex pattern and a proxy server"),
AP_INIT_FLAG("ProxyPassInterpolateEnv", ap_set_flag_slot_char,
(void*)APR_OFFSETOF(proxy_dir_conf, interpolate_env),
RSRC_CONF|ACCESS_CONF, "Interpolate Env Vars in reverse Proxy") ,
AP_INIT_RAW_ARGS("ProxyPass", add_pass_noregex, NULL, RSRC_CONF|ACCESS_CONF,
"a virtual path and a URL"),
AP_INIT_RAW_ARGS("ProxyPassMatch", add_pass_regex, NULL, RSRC_CONF|ACCESS_CONF,
"a virtual path and a URL"),
AP_INIT_TAKE123("ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF|ACCESS_CONF,
"a virtual path and a URL for reverse proxy behaviour"),
AP_INIT_TAKE23("ProxyPassReverseCookiePath", cookie_path, NULL,
RSRC_CONF|ACCESS_CONF, "Path rewrite rule for proxying cookies"),
AP_INIT_TAKE23("ProxyPassReverseCookieDomain", cookie_domain, NULL,
RSRC_CONF|ACCESS_CONF, "Domain rewrite rule for proxying cookies"),
AP_INIT_ITERATE("ProxyBlock", set_proxy_exclude, NULL, RSRC_CONF,
"A list of names, hosts or domains to which the proxy will not connect"),
AP_INIT_TAKE1("ProxyReceiveBufferSize", set_recv_buffer_size, NULL, RSRC_CONF,
"Receive buffer size for outgoing HTTP and FTP connections in bytes"),
AP_INIT_TAKE1("ProxyIOBufferSize", set_io_buffer_size, NULL, RSRC_CONF,
"IO buffer size for outgoing HTTP and FTP connections in bytes"),
AP_INIT_TAKE1("ProxyMaxForwards", set_max_forwards, NULL, RSRC_CONF,
"The maximum number of proxies a request may be forwarded through."),
AP_INIT_ITERATE("NoProxy", set_proxy_dirconn, NULL, RSRC_CONF,
"A list of domains, hosts, or subnets to which the proxy will connect directly"),
AP_INIT_TAKE1("ProxyDomain", set_proxy_domain, NULL, RSRC_CONF,
"The default intranet domain name (in absence of a domain in the URL)"),
AP_INIT_TAKE1("ProxyVia", set_via_opt, NULL, RSRC_CONF,
"Configure Via: proxy header header to one of: on | off | block | full"),
AP_INIT_FLAG("ProxyErrorOverride", set_proxy_error_override, NULL, RSRC_CONF|ACCESS_CONF,
"use our error handling pages instead of the servers' we are proxying"),
AP_INIT_FLAG("ProxyPreserveHost", set_preserve_host, NULL, RSRC_CONF|ACCESS_CONF,
"on if we should preserve host header while proxying"),
AP_INIT_TAKE1("ProxyTimeout", set_proxy_timeout, NULL, RSRC_CONF,
"Set the timeout (in seconds) for a proxied connection. "
"This overrides the server timeout"),
AP_INIT_TAKE1("ProxyBadHeader", set_bad_opt, NULL, RSRC_CONF,
"How to handle bad header line in response: IsError | Ignore | StartBody"),
AP_INIT_RAW_ARGS("BalancerMember", add_member, NULL, RSRC_CONF|ACCESS_CONF,
"A balancer name and scheme with list of params"),
AP_INIT_TAKE1("BalancerGrowth", set_bgrowth, NULL, RSRC_CONF,
"Number of additional Balancers that can be added post-config"),
AP_INIT_TAKE1("ProxyStatus", set_status_opt, NULL, RSRC_CONF,
"Configure Status: proxy status to one of: on | off | full"),
AP_INIT_RAW_ARGS("ProxySet", set_proxy_param, NULL, RSRC_CONF|ACCESS_CONF,
"A balancer or worker name with list of params"),
AP_INIT_TAKE1("ProxySourceAddress", set_source_address, NULL, RSRC_CONF,
"Configure local source IP used for request forward"),
AP_INIT_FLAG("ProxyAddHeaders", add_proxy_http_headers, NULL, RSRC_CONF|ACCESS_CONF,
"on if X-Forwarded-* headers should be added or completed"),
{NULL}
};
static APR_OPTIONAL_FN_TYPE(ssl_proxy_enable) *proxy_ssl_enable = NULL;
static APR_OPTIONAL_FN_TYPE(ssl_engine_disable) *proxy_ssl_disable = NULL;
static APR_OPTIONAL_FN_TYPE(ssl_is_https) *proxy_is_https = NULL;
static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *proxy_ssl_val = NULL;
PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c)
{
/*
* if c == NULL just check if the optional function was imported
* else run the optional function so ssl filters are inserted
*/
if (proxy_ssl_enable) {
return c ? proxy_ssl_enable(c) : 1;
}
return 0;
}
PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c)
{
if (proxy_ssl_disable) {
return proxy_ssl_disable(c);
}
return 0;
}
PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c)
{
if (proxy_is_https) {
return proxy_is_https(c);
}
else
return 0;
}
PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s,
conn_rec *c, request_rec *r,
const char *var)
{
if (proxy_ssl_val) {
/* XXX Perhaps the casting useless */
return (const char *)proxy_ssl_val(p, s, c, r, (char *)var);
}
else
return NULL;
}
static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
proxy_ssl_enable = APR_RETRIEVE_OPTIONAL_FN(ssl_proxy_enable);
proxy_ssl_disable = APR_RETRIEVE_OPTIONAL_FN(ssl_engine_disable);
proxy_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
proxy_ssl_val = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
ap_proxy_strmatch_path = apr_strmatch_precompile(pconf, "path=", 0);
ap_proxy_strmatch_domain = apr_strmatch_precompile(pconf, "domain=", 0);
return OK;
}
/*
* proxy Extension to mod_status
*/
static int proxy_status_hook(request_rec *r, int flags)
{
int i, n;
void *sconf = r->server->module_config;
proxy_server_conf *conf = (proxy_server_conf *)
ap_get_module_config(sconf, &proxy_module);
proxy_balancer *balancer = NULL;
proxy_worker **worker = NULL;
if (flags & AP_STATUS_SHORT || conf->balancers->nelts == 0 ||
conf->proxy_status == status_off)
return OK;
balancer = (proxy_balancer *)conf->balancers->elts;
for (i = 0; i < conf->balancers->nelts; i++) {
ap_rputs("<hr />\n<h1>Proxy LoadBalancer Status for ", r);
ap_rvputs(r, balancer->s->name, "</h1>\n\n", NULL);
ap_rputs("\n\n<table border=\"0\"><tr>"
"<th>SSes</th><th>Timeout</th><th>Method</th>"
"</tr>\n<tr>", r);
if (*balancer->s->sticky) {
if (strcmp(balancer->s->sticky, balancer->s->sticky_path)) {
ap_rvputs(r, "<td>", balancer->s->sticky, " | ",
balancer->s->sticky_path, NULL);
}
else {
ap_rvputs(r, "<td>", balancer->s->sticky, NULL);
}
}
else {
ap_rputs("<td> - ", r);
}
ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
apr_time_sec(balancer->s->timeout));
ap_rprintf(r, "<td>%s</td>\n",
balancer->lbmethod->name);
ap_rputs("</table>\n", r);
ap_rputs("\n\n<table border=\"0\"><tr>"
"<th>Sch</th><th>Host</th><th>Stat</th>"
"<th>Route</th><th>Redir</th>"
"<th>F</th><th>Set</th><th>Acc</th><th>Wr</th><th>Rd</th>"
"</tr>\n", r);
worker = (proxy_worker **)balancer->workers->elts;
for (n = 0; n < balancer->workers->nelts; n++) {
char fbuf[50];
ap_rvputs(r, "<tr>\n<td>", (*worker)->s->scheme, "</td>", NULL);
ap_rvputs(r, "<td>", (*worker)->s->hostname, "</td><td>", NULL);
ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, *worker), NULL);
ap_rvputs(r, "</td><td>", (*worker)->s->route, NULL);
ap_rvputs(r, "</td><td>", (*worker)->s->redirect, NULL);
ap_rprintf(r, "</td><td>%d</td>", (*worker)->s->lbfactor);
ap_rprintf(r, "<td>%d</td>", (*worker)->s->lbset);
ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", (*worker)->s->elected);
ap_rputs(apr_strfsize((*worker)->s->transferred, fbuf), r);
ap_rputs("</td><td>", r);
ap_rputs(apr_strfsize((*worker)->s->read, fbuf), r);
ap_rputs("</td>\n", r);
/* TODO: Add the rest of dynamic worker data */
ap_rputs("</tr>\n", r);
++worker;
}
ap_rputs("</table>\n", r);
++balancer;
}
ap_rputs("<hr /><table>\n"
"<tr><th>SSes</th><td>Sticky session name</td></tr>\n"
"<tr><th>Timeout</th><td>Balancer Timeout</td></tr>\n"
"<tr><th>Sch</th><td>Connection scheme</td></tr>\n"
"<tr><th>Host</th><td>Backend Hostname</td></tr>\n"
"<tr><th>Stat</th><td>Worker status</td></tr>\n"
"<tr><th>Route</th><td>Session Route</td></tr>\n"
"<tr><th>Redir</th><td>Session Route Redirection</td></tr>\n"
"<tr><th>F</th><td>Load Balancer Factor</td></tr>\n"
"<tr><th>Acc</th><td>Number of uses</td></tr>\n"
"<tr><th>Wr</th><td>Number of bytes transferred</td></tr>\n"
"<tr><th>Rd</th><td>Number of bytes read</td></tr>\n"
"</table>", r);
return OK;
}
static void child_init(apr_pool_t *p, server_rec *s)
{
proxy_worker *reverse = NULL;
/* TODO */
while (s) {
void *sconf = s->module_config;
proxy_server_conf *conf;
proxy_worker *worker;
int i;
conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
/*
* NOTE: non-balancer members don't use shm at all...
* after all, why should they?
*/
worker = (proxy_worker *)conf->workers->elts;
for (i = 0; i < conf->workers->nelts; i++, worker++) {
ap_proxy_initialize_worker(worker, s, conf->pool);
}
/* Create and initialize forward worker if defined */
if (conf->req_set && conf->req) {
proxy_worker *forward;
ap_proxy_define_worker(p, &forward, NULL, NULL, "http://www.apache.org", 0);
conf->forward = forward;
PROXY_STRNCPY(conf->forward->s->name, "proxy:forward");
PROXY_STRNCPY(conf->forward->s->hostname, "*");
PROXY_STRNCPY(conf->forward->s->scheme, "*");
conf->forward->hash.def = conf->forward->s->hash.def =
ap_proxy_hashfunc(conf->forward->s->name, PROXY_HASHFUNC_DEFAULT);
conf->forward->hash.fnv = conf->forward->s->hash.fnv =
ap_proxy_hashfunc(conf->forward->s->name, PROXY_HASHFUNC_FNV);
/* Do not disable worker in case of errors */
conf->forward->s->status |= PROXY_WORKER_IGNORE_ERRORS;
ap_proxy_initialize_worker(conf->forward, s, conf->pool);
/* Disable address cache for generic forward worker */
conf->forward->s->is_address_reusable = 0;
}
if (!reverse) {
ap_proxy_define_worker(p, &reverse, NULL, NULL, "http://www.apache.org", 0);
PROXY_STRNCPY(reverse->s->name, "proxy:reverse");
PROXY_STRNCPY(reverse->s->hostname, "*");
PROXY_STRNCPY(reverse->s->scheme, "*");
reverse->hash.def = reverse->s->hash.def =
ap_proxy_hashfunc(reverse->s->name, PROXY_HASHFUNC_DEFAULT);
reverse->hash.fnv = reverse->s->hash.fnv =
ap_proxy_hashfunc(reverse->s->name, PROXY_HASHFUNC_FNV);
/* Do not disable worker in case of errors */
reverse->s->status |= PROXY_WORKER_IGNORE_ERRORS;
conf->reverse = reverse;
ap_proxy_initialize_worker(conf->reverse, s, conf->pool);
/* Disable address cache for generic reverse worker */
reverse->s->is_address_reusable = 0;
}
s = s->next;
}
}
/*
* This routine is called before the server processes the configuration
* files. There is no return value.
*/
static int proxy_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp)
{
APR_OPTIONAL_HOOK(ap, status_hook, proxy_status_hook, NULL, NULL,
APR_HOOK_MIDDLE);
/* Reset workers count on gracefull restart */
proxy_lb_workers = 0;
return OK;
}
static void register_hooks(apr_pool_t *p)
{
/* fixup before mod_rewrite, so that the proxied url will not
* escaped accidentally by our fixup.
*/
static const char * const aszSucc[] = { "mod_rewrite.c", NULL};
/* Only the mpm_winnt has child init hook handler.
* make sure that we are called after the mpm
* initializes.
*/
static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy_balancer.c", NULL};
/* handler */
ap_hook_handler(proxy_handler, NULL, NULL, APR_HOOK_FIRST);
/* filename-to-URI translation */
ap_hook_translate_name(proxy_trans, aszSucc, NULL, APR_HOOK_FIRST);
/* walk <Proxy > entries and suppress default TRACE behavior */
ap_hook_map_to_storage(proxy_map_location, NULL,NULL, APR_HOOK_FIRST);
/* fixups */
ap_hook_fixups(proxy_fixup, NULL, aszSucc, APR_HOOK_FIRST);
/* post read_request handling */
ap_hook_post_read_request(proxy_detect, NULL, NULL, APR_HOOK_FIRST);
/* pre config handling */
ap_hook_pre_config(proxy_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
/* post config handling */
ap_hook_post_config(proxy_post_config, NULL, NULL, APR_HOOK_MIDDLE);
/* child init handling */
ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
/* register optional functions within proxy_util.c */
proxy_util_register_hooks(p);
}
AP_DECLARE_MODULE(proxy) =
{
STANDARD20_MODULE_STUFF,
create_proxy_dir_config, /* create per-directory config structure */
merge_proxy_dir_config, /* merge per-directory config structures */
create_proxy_config, /* create per-server config structure */
merge_proxy_config, /* merge per-server config structures */
proxy_cmds, /* command table */
register_hooks
};
APR_HOOK_STRUCT(
APR_HOOK_LINK(scheme_handler)
APR_HOOK_LINK(canon_handler)
APR_HOOK_LINK(pre_request)
APR_HOOK_LINK(post_request)
APR_HOOK_LINK(request_status)
)
APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, scheme_handler,
(request_rec *r, proxy_worker *worker,
proxy_server_conf *conf,
char *url, const char *proxyhost,
apr_port_t proxyport),(r,worker,conf,
url,proxyhost,proxyport),DECLINED)
APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, canon_handler,
(request_rec *r, char *url),(r,
url),DECLINED)
APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, pre_request, (
proxy_worker **worker,
proxy_balancer **balancer,
request_rec *r,
proxy_server_conf *conf,
char **url),(worker,balancer,
r,conf,url),DECLINED)
APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, post_request,
(proxy_worker *worker,
proxy_balancer *balancer,
request_rec *r,
proxy_server_conf *conf),(worker,
balancer,r,conf),DECLINED)
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, fixups,
(request_rec *r), (r),
OK, DECLINED)
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, request_status,
(int *status, request_rec *r),
(status, r),
OK, DECLINED)