mod_proxy.c revision c64fb33e0c4634fd352c4a6c143cd1a087c09b13
f743002678eb67b99bbc29fee116b65d9530fec0wrowe/* Licensed to the Apache Software Foundation (ASF) under one or more
80833bb9a1bf25dcf19e814438a4b311d2e1f4cffuankg * contributor license agreements. See the NOTICE file distributed with
a34684a59b60a4173c25035d0c627ef17e6dc215rpluem * this work for additional information regarding copyright ownership.
1337c7673efc1f80f634139fbad7cbb98a0dc657ylavic * The ASF licenses this file to You under the Apache License, Version 2.0
1337c7673efc1f80f634139fbad7cbb98a0dc657ylavic * (the "License"); you may not use this file except in compliance with
1337c7673efc1f80f634139fbad7cbb98a0dc657ylavic * the License. You may obtain a copy of the License at
1337c7673efc1f80f634139fbad7cbb98a0dc657ylavic *
4da61833a1cbbca94094f9653fd970582b97a72etrawick * http://www.apache.org/licenses/LICENSE-2.0
4da61833a1cbbca94094f9653fd970582b97a72etrawick *
4da61833a1cbbca94094f9653fd970582b97a72etrawick * Unless required by applicable law or agreed to in writing, software
4da61833a1cbbca94094f9653fd970582b97a72etrawick * distributed under the License is distributed on an "AS IS" BASIS,
4da61833a1cbbca94094f9653fd970582b97a72etrawick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4789804be088bcd86ae637a29cdb7fda25169521jailletc * See the License for the specific language governing permissions and
4789804be088bcd86ae637a29cdb7fda25169521jailletc * limitations under the License.
4789804be088bcd86ae637a29cdb7fda25169521jailletc */
4789804be088bcd86ae637a29cdb7fda25169521jailletc
e50c3026198fd496f183cda4c32a202925476778covener#define CORE_PRIVATE
e50c3026198fd496f183cda4c32a202925476778covener
e50c3026198fd496f183cda4c32a202925476778covener#include "mod_proxy.h"
5b88c8507d5ef6d0c4cfbc78230294968175b638minfrin#include "mod_core.h"
5b88c8507d5ef6d0c4cfbc78230294968175b638minfrin#include "apr_optional.h"
6c3b9cebb551140fbb25d58bae08b539b3802133ylavic#include "scoreboard.h"
6c3b9cebb551140fbb25d58bae08b539b3802133ylavic#include "mod_status.h"
6c3b9cebb551140fbb25d58bae08b539b3802133ylavic
4f29b65ab4b547ad5dbe506e2d0ff5d12ead9247ylavic#if (MODULE_MAGIC_NUMBER_MAJOR > 20020903)
4f29b65ab4b547ad5dbe506e2d0ff5d12ead9247ylavic#include "mod_ssl.h"
0a0df13b7f1f4f1a74fe295253d89ca3911b301aylavic#else
0a0df13b7f1f4f1a74fe295253d89ca3911b301aylavicAPR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
0a0df13b7f1f4f1a74fe295253d89ca3911b301aylavicAPR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
0a0df13b7f1f4f1a74fe295253d89ca3911b301aylavicAPR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
69301145375a889e7e37caf7cc7321ac0f91801erpluemAPR_DECLARE_OPTIONAL_FN(char *, ssl_var_lookup,
69301145375a889e7e37caf7cc7321ac0f91801erpluem (apr_pool_t *, server_rec *,
69301145375a889e7e37caf7cc7321ac0f91801erpluem conn_rec *, request_rec *, char *));
506bfe33206b2fece40ef25f695af39dd4130facjkaluza#endif
506bfe33206b2fece40ef25f695af39dd4130facjkaluza
506bfe33206b2fece40ef25f695af39dd4130facjkaluza#ifndef MAX
506bfe33206b2fece40ef25f695af39dd4130facjkaluza#define MAX(x,y) ((x) >= (y) ? (x) : (y))
d58a848a016d401b965111e50ef829e1641f7834minfrin#endif
d58a848a016d401b965111e50ef829e1641f7834minfrin
d58a848a016d401b965111e50ef829e1641f7834minfrin/*
2e6f4d654c96c98b761fb012fd25c5d5b1558c44sf * A Web proxy module. Stages:
2e6f4d654c96c98b761fb012fd25c5d5b1558c44sf *
2e6f4d654c96c98b761fb012fd25c5d5b1558c44sf * translate_name: set filename to proxy:<URL>
17e6c95f3b22d18acdf8380fb26a8d0e10c80767ylavic * map_to_storage: run proxy_walk (rather than directory_walk/file_walk)
17e6c95f3b22d18acdf8380fb26a8d0e10c80767ylavic * can't trust directory_walk/file_walk since these are
17e6c95f3b22d18acdf8380fb26a8d0e10c80767ylavic * not in our filesystem. Prevents mod_http from serving
17e6c95f3b22d18acdf8380fb26a8d0e10c80767ylavic * the TRACE request we will set aside to handle later.
17e6c95f3b22d18acdf8380fb26a8d0e10c80767ylavic * type_checker: set type to PROXY_MAGIC_TYPE if filename begins proxy:
e8bd80a4bb88199d2f9a24a50345688e52d9c116ylavic * fix_ups: convert the URL stored in the filename to the
e8bd80a4bb88199d2f9a24a50345688e52d9c116ylavic * canonical form.
e8bd80a4bb88199d2f9a24a50345688e52d9c116ylavic * handler: handle proxy requests
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic */
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic/* -------------------------------------------------------------- */
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic/* Translate the URL into a 'filename' */
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic#define PROXY_COPY_CONF_PARAMS(w, c) \
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic do { \
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener (w)->timeout = (c)->timeout; \
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener (w)->timeout_set = (c)->timeout_set; \
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener (w)->recv_buffer_size = (c)->recv_buffer_size; \
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener (w)->recv_buffer_size_set = (c)->recv_buffer_size_set; \
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener (w)->io_buffer_size = (c)->io_buffer_size; \
44ff304057225e944e220e981d434a046d14cf06covener (w)->io_buffer_size_set = (c)->io_buffer_size_set; \
44ff304057225e944e220e981d434a046d14cf06covener } while (0)
44ff304057225e944e220e981d434a046d14cf06covener
44ff304057225e944e220e981d434a046d14cf06covenerstatic const char *set_worker_param(apr_pool_t *p,
5d1ba75b8794925e67591c209085a49279791de9covener proxy_worker *worker,
5d1ba75b8794925e67591c209085a49279791de9covener const char *key,
5d1ba75b8794925e67591c209085a49279791de9covener const char *val)
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrand{
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrand
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrand int ival;
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrand if (!strcasecmp(key, "loadfactor")) {
caad2986f81ab263f7af41467dd622dc9add17f3ylavic /* Normalized load factor. Used with BalancerMamber,
caad2986f81ab263f7af41467dd622dc9add17f3ylavic * it is a number between 1 and 100.
caad2986f81ab263f7af41467dd622dc9add17f3ylavic */
caad2986f81ab263f7af41467dd622dc9add17f3ylavic worker->lbfactor = atoi(val);
45a10d38e6051fd7bdf9d742aaae633d97ff02abjailletc if (worker->lbfactor < 1 || worker->lbfactor > 100)
f7317ff316c2b141feea31bddb74d5d3fa1584edjorton return "LoadFactor must be number between 1..100";
f7317ff316c2b141feea31bddb74d5d3fa1584edjorton }
2165214331e4afafca4048f66f303d0253d7b001covener else if (!strcasecmp(key, "retry")) {
a34684a59b60a4173c25035d0c627ef17e6dc215rpluem /* If set it will give the retry timeout for the worker
a34684a59b60a4173c25035d0c627ef17e6dc215rpluem * The default value is 60 seconds, meaning that if
1e2d421a36999d292042a5539971070d54aa6c63ylavic * in error state, it will be retried after that timeout.
1e2d421a36999d292042a5539971070d54aa6c63ylavic */
1e2d421a36999d292042a5539971070d54aa6c63ylavic ival = atoi(val);
fa7ed98b9dc94c5845cf845aea0a44ecacd290c9humbedooh if (ival < 0)
fa7ed98b9dc94c5845cf845aea0a44ecacd290c9humbedooh return "Retry must be a positive value";
fa7ed98b9dc94c5845cf845aea0a44ecacd290c9humbedooh worker->retry = apr_time_from_sec(ival);
0b67eb8568cd58bb77082703951679b42cf098actrawick worker->retry_set = 1;
0b67eb8568cd58bb77082703951679b42cf098actrawick }
0b67eb8568cd58bb77082703951679b42cf098actrawick else if (!strcasecmp(key, "ttl")) {
0b67eb8568cd58bb77082703951679b42cf098actrawick /* Time in seconds that will destroy all the connections
5ef3c61605a3a021ff71f488983cb0065f8e1a79covener * that exced the smax
fb1985a97912b25ec6564c73e610a31e5fc6e25fcovener */
09c87c777bed1655621bb20e1c46cb6b1a63279dcovener ival = atoi(val);
6502b7b32f980cc2093bb3ebce37e5e4dc68fba4ylavic if (ival < 1)
6502b7b32f980cc2093bb3ebce37e5e4dc68fba4ylavic return "TTL must be at least one second";
3060ce7f798fbda7999cd4ddf89b525d2b294185covener worker->ttl = apr_time_from_sec(ival);
c1a63b8fad09c419c1a64f75993feb8a343a6801ylavic }
c1a63b8fad09c419c1a64f75993feb8a343a6801ylavic else if (!strcasecmp(key, "min")) {
c1a63b8fad09c419c1a64f75993feb8a343a6801ylavic /* Initial number of connections to remote
e6b4bd1113567627ab6bb6c6a7105e1e01a7d889jailletc */
e6b4bd1113567627ab6bb6c6a7105e1e01a7d889jailletc ival = atoi(val);
e466c40e1801982602ee0200c9e8b61cc148742djailletc if (ival < 0)
e466c40e1801982602ee0200c9e8b61cc148742djailletc return "Min must be a positive number";
457468b82e59d01eba00dd9d0817309c8f5e414ejim worker->min = ival;
457468b82e59d01eba00dd9d0817309c8f5e414ejim }
457468b82e59d01eba00dd9d0817309c8f5e414ejim else if (!strcasecmp(key, "max")) {
04983e3bd1754764eec7d6bb772fe3b0bf391771jorton /* Maximum number of connections to remote
04983e3bd1754764eec7d6bb772fe3b0bf391771jorton */
15890c9306ba98f6fc243e15a3c4778ddc7d773erpluem ival = atoi(val);
15660979a30d251681463de2e0584853890082accovener if (ival < 0)
15660979a30d251681463de2e0584853890082accovener return "Max must be a positive number";
15660979a30d251681463de2e0584853890082accovener worker->hmax = ival;
15660979a30d251681463de2e0584853890082accovener }
cfd9415521847b2f9394fad04fb701cfb955f503rjung /* XXX: More inteligent naming needed */
cfd9415521847b2f9394fad04fb701cfb955f503rjung else if (!strcasecmp(key, "smax")) {
cfd9415521847b2f9394fad04fb701cfb955f503rjung /* Maximum number of connections to remote that
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe * will not be destroyed
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe */
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe ival = atoi(val);
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe if (ival < 0)
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe return "Smax must be a positive number";
8491e0600f69b0405e156ea8a419653c065c645bcovener worker->smax = ival;
63b9f1f5880391261705f696d7d65507bbe9ace3covener }
63b9f1f5880391261705f696d7d65507bbe9ace3covener else if (!strcasecmp(key, "acquire")) {
63b9f1f5880391261705f696d7d65507bbe9ace3covener /* Acquire timeout in milliseconds.
49dacedb6c387b786b7911082ff35121a45f414bcovener * If set this will be the maximum time to
49dacedb6c387b786b7911082ff35121a45f414bcovener * wait for a free connection.
49dacedb6c387b786b7911082ff35121a45f414bcovener */
49dacedb6c387b786b7911082ff35121a45f414bcovener ival = atoi(val);
3c990331fc6702119e4f5b8ba9eae3021aea5265jim if (ival < 1)
3c990331fc6702119e4f5b8ba9eae3021aea5265jim return "Acquire must be at least one mili second";
3c990331fc6702119e4f5b8ba9eae3021aea5265jim worker->acquire = apr_time_make(0, ival * 1000);
3c990331fc6702119e4f5b8ba9eae3021aea5265jim worker->acquire_set = 1;
fc42512879dd0504532f52fe5d0d0383dda96a1eniq }
fc42512879dd0504532f52fe5d0d0383dda96a1eniq else if (!strcasecmp(key, "timeout")) {
fc42512879dd0504532f52fe5d0d0383dda96a1eniq /* Connection timeout in seconds.
0451df5dc50fa5d8b3e07d92ee6a92e36a1181a5niq * Defaults to server timeout.
0451df5dc50fa5d8b3e07d92ee6a92e36a1181a5niq */
0451df5dc50fa5d8b3e07d92ee6a92e36a1181a5niq ival = atoi(val);
da0442c0440caef34706e2c2f3af05cb65921cc0jailletc if (ival < 1)
983528026996668ea295be95aedb9c7a346af470ylavic return "Timeout must be at least one second";
da0442c0440caef34706e2c2f3af05cb65921cc0jailletc worker->timeout = apr_time_from_sec(ival);
da0442c0440caef34706e2c2f3af05cb65921cc0jailletc worker->timeout_set = 1;
06b8f183140c8e02e0974e938a05078b511d1603covener }
06b8f183140c8e02e0974e938a05078b511d1603covener else if (!strcasecmp(key, "iobuffersize")) {
06b8f183140c8e02e0974e938a05078b511d1603covener long s = atol(val);
15890c9306ba98f6fc243e15a3c4778ddc7d773erpluem worker->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
259878293a997ff49f5ddfc53d3739cbdc25444ecovener worker->io_buffer_size_set = 1;
259878293a997ff49f5ddfc53d3739cbdc25444ecovener }
259878293a997ff49f5ddfc53d3739cbdc25444ecovener else if (!strcasecmp(key, "receivebuffersize")) {
259878293a997ff49f5ddfc53d3739cbdc25444ecovener ival = atoi(val);
15890c9306ba98f6fc243e15a3c4778ddc7d773erpluem if (ival < 512 && ival != 0) {
b54b024c06a19926832d77d40ba35ad8c41e4d3dminfrin return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
b54b024c06a19926832d77d40ba35ad8c41e4d3dminfrin }
b54b024c06a19926832d77d40ba35ad8c41e4d3dminfrin worker->recv_buffer_size = ival;
65967d05f839dbf27cf91d91fa79585eeae19660minfrin worker->recv_buffer_size_set = 1;
65967d05f839dbf27cf91d91fa79585eeae19660minfrin }
65967d05f839dbf27cf91d91fa79585eeae19660minfrin else if (!strcasecmp(key, "keepalive")) {
65967d05f839dbf27cf91d91fa79585eeae19660minfrin if (!strcasecmp(val, "on"))
8152945ae46857b170cb227e79bb799f4fc7710dminfrin worker->keepalive = 1;
8152945ae46857b170cb227e79bb799f4fc7710dminfrin else if (!strcasecmp(val, "off"))
8152945ae46857b170cb227e79bb799f4fc7710dminfrin worker->keepalive = 0;
8152945ae46857b170cb227e79bb799f4fc7710dminfrin else
75f5c2db254c0167a0e396254460de09b775d203trawick return "KeepAlive must be On|Off";
75f5c2db254c0167a0e396254460de09b775d203trawick worker->keepalive_set = 1;
75f5c2db254c0167a0e396254460de09b775d203trawick }
4f0358189bfa57b8e75bd6b94db264302a8f336amrumph else if (!strcasecmp(key, "route")) {
4f0358189bfa57b8e75bd6b94db264302a8f336amrumph /* Worker route.
4f0358189bfa57b8e75bd6b94db264302a8f336amrumph */
5716f9c6daa92dde5f2f9d11ed63f7c9549c223atrawick if (strlen(val) > PROXY_WORKER_MAX_ROUTE_SIZ)
5716f9c6daa92dde5f2f9d11ed63f7c9549c223atrawick return "Route length must be < 64 characters";
5716f9c6daa92dde5f2f9d11ed63f7c9549c223atrawick worker->route = apr_pstrdup(p, val);
5716f9c6daa92dde5f2f9d11ed63f7c9549c223atrawick }
54d750a84a175d8e338880514d440773eb986b50covener else if (!strcasecmp(key, "redirect")) {
54d750a84a175d8e338880514d440773eb986b50covener /* Worker redirection route.
54d750a84a175d8e338880514d440773eb986b50covener */
54d750a84a175d8e338880514d440773eb986b50covener if (strlen(val) > PROXY_WORKER_MAX_ROUTE_SIZ)
54d750a84a175d8e338880514d440773eb986b50covener return "Redirect length must be < 64 characters";
54d750a84a175d8e338880514d440773eb986b50covener worker->redirect = apr_pstrdup(p, val);
54d750a84a175d8e338880514d440773eb986b50covener }
54d750a84a175d8e338880514d440773eb986b50covener else if (!strcasecmp(key, "status")) {
7a3aa12f0eda24793ee26d6a179bd53132e9dae8covener const char *v;
54d750a84a175d8e338880514d440773eb986b50covener int mode = 1;
54d750a84a175d8e338880514d440773eb986b50covener /* Worker status.
83b50288fa7d306324bba68832011ea08f5c7832covener */
4e30ef014533a7e93c92d88306291f5e49c9692ftrawick for (v = val; *v; v++) {
83b50288fa7d306324bba68832011ea08f5c7832covener if (*v == '+') {
5f066f496cd9f20a2a701255bc67d44e7cb46daetrawick mode = 1;
5f066f496cd9f20a2a701255bc67d44e7cb46daetrawick v++;
5f066f496cd9f20a2a701255bc67d44e7cb46daetrawick }
2e15620d724fb8e3a5be183b917359a2fd6e9468covener else if (*v == '-') {
2e15620d724fb8e3a5be183b917359a2fd6e9468covener mode = 0;
2e15620d724fb8e3a5be183b917359a2fd6e9468covener v++;
2e15620d724fb8e3a5be183b917359a2fd6e9468covener }
1b988c41ee505962781d110a3e4c2c90f1ea0aa4covener if (*v == 'D' || *v == 'd') {
1b988c41ee505962781d110a3e4c2c90f1ea0aa4covener if (mode)
1b988c41ee505962781d110a3e4c2c90f1ea0aa4covener worker->status |= PROXY_WORKER_DISABLED;
1b988c41ee505962781d110a3e4c2c90f1ea0aa4covener else
b8efdc95bec9cf089aa1be0bfd07d46aa1137a7acovener worker->status &= ~PROXY_WORKER_DISABLED;
b8efdc95bec9cf089aa1be0bfd07d46aa1137a7acovener }
b8efdc95bec9cf089aa1be0bfd07d46aa1137a7acovener else if (*v == 'S' || *v == 's') {
f06e7c4b1bce6b6491e5de0b7998d3f5696b293dchrisd if (mode)
f06e7c4b1bce6b6491e5de0b7998d3f5696b293dchrisd worker->status |= PROXY_WORKER_STOPPED;
f06e7c4b1bce6b6491e5de0b7998d3f5696b293dchrisd else
179565be4043d7e5f9161aa75271fa0a001866d9covener worker->status &= ~PROXY_WORKER_STOPPED;
179565be4043d7e5f9161aa75271fa0a001866d9covener }
179565be4043d7e5f9161aa75271fa0a001866d9covener else if (*v == 'E' || *v == 'e') {
111436a32ba1254291e4883292fb116d15fe8f64covener if (mode)
fce4949fb0b309a5744afcd503c6ed2d35621ee2covener worker->status |= PROXY_WORKER_IN_ERROR;
fce4949fb0b309a5744afcd503c6ed2d35621ee2covener else
fce4949fb0b309a5744afcd503c6ed2d35621ee2covener worker->status &= ~PROXY_WORKER_IN_ERROR;
fce4949fb0b309a5744afcd503c6ed2d35621ee2covener }
7b7430e701e9a31ce809da7c220bb8dfcf68c86etrawick else if (*v == 'H' || *v == 'h') {
7b7430e701e9a31ce809da7c220bb8dfcf68c86etrawick if (mode)
7b7430e701e9a31ce809da7c220bb8dfcf68c86etrawick worker->status |= PROXY_WORKER_HOT_STANDBY;
ccc20788c1e5fc973f36df634399c89acb70deaejerenkrantz else
ccc20788c1e5fc973f36df634399c89acb70deaejerenkrantz worker->status &= ~PROXY_WORKER_HOT_STANDBY;
ccc20788c1e5fc973f36df634399c89acb70deaejerenkrantz }
273e512f20f262e5e2aa8e0e83371d1929fb76adjkaluza else if (*v == 'I' || *v == 'i') {
273e512f20f262e5e2aa8e0e83371d1929fb76adjkaluza if (mode)
273e512f20f262e5e2aa8e0e83371d1929fb76adjkaluza worker->status |= PROXY_WORKER_IGNORE_ERRORS;
efe780dcf13b2b95effabf897d694d8f23feac74trawick else
fe83f60b41477b14a37edcfcd1f7f5c5a1ebfe44minfrin worker->status &= ~PROXY_WORKER_IGNORE_ERRORS;
fe83f60b41477b14a37edcfcd1f7f5c5a1ebfe44minfrin }
fe83f60b41477b14a37edcfcd1f7f5c5a1ebfe44minfrin else {
993d1261a278d7322bccef219101220b7b4fb8c5jkaluza return "Unknown status parameter option";
993d1261a278d7322bccef219101220b7b4fb8c5jkaluza }
993d1261a278d7322bccef219101220b7b4fb8c5jkaluza }
ba050a6f942b9fa0e81ed73437588005c569655ccovener }
ba050a6f942b9fa0e81ed73437588005c569655ccovener else if (!strcasecmp(key, "flushpackets")) {
ba050a6f942b9fa0e81ed73437588005c569655ccovener if (!strcasecmp(val, "on"))
ba050a6f942b9fa0e81ed73437588005c569655ccovener worker->flush_packets = flush_on;
135ddda3a989215d2bedbcf1529bfb269c3eda23niq else if (!strcasecmp(val, "off"))
135ddda3a989215d2bedbcf1529bfb269c3eda23niq worker->flush_packets = flush_off;
135ddda3a989215d2bedbcf1529bfb269c3eda23niq else if (!strcasecmp(val, "auto"))
001a44c352f89c9ec332ffd3e0a6927dcd19432chumbedooh worker->flush_packets = flush_auto;
001a44c352f89c9ec332ffd3e0a6927dcd19432chumbedooh else
001a44c352f89c9ec332ffd3e0a6927dcd19432chumbedooh return "flushpackets must be on|off|auto";
efe780dcf13b2b95effabf897d694d8f23feac74trawick }
793214f67dede32edfd9ee96c664ead04d175cbbjfclere else if (!strcasecmp(key, "flushwait")) {
cc5a4a08dc9783fcbc52ce86f11e01c281a43810minfrin ival = atoi(val);
9b0076ddd1103e5fa9c1f9bafde4b06ce244fbaecovener if (ival > 1000 || ival < 0) {
9b0076ddd1103e5fa9c1f9bafde4b06ce244fbaecovener return "flushwait must be <= 1000, or 0 for system default of 10 millseconds.";
9b0076ddd1103e5fa9c1f9bafde4b06ce244fbaecovener }
249d09d51808cb7981af99762c3b3736ca126cd5jkaluza if (ival == 0)
249d09d51808cb7981af99762c3b3736ca126cd5jkaluza worker->flush_wait = PROXY_FLUSH_WAIT;
249d09d51808cb7981af99762c3b3736ca126cd5jkaluza else
249d09d51808cb7981af99762c3b3736ca126cd5jkaluza worker->flush_wait = ival * 1000; /* change to microseconds */
56589be3d7a3e9343370df240010c6928cc78b39jkaluza }
56589be3d7a3e9343370df240010c6928cc78b39jkaluza else if (!strcasecmp(key, "ping")) {
56589be3d7a3e9343370df240010c6928cc78b39jkaluza /* Ping/Pong timeout in seconds.
77ca16c5676da23155311e13cee61e7eaba9fa3ejailletc */
77ca16c5676da23155311e13cee61e7eaba9fa3ejailletc ival = atoi(val);
77ca16c5676da23155311e13cee61e7eaba9fa3ejailletc if (ival < 1)
77ca16c5676da23155311e13cee61e7eaba9fa3ejailletc return "Ping/Pong timeout must be at least one second";
f87299dab99bc04b51a6b8cad51b6795db862c0atrawick worker->ping_timeout = apr_time_from_sec(ival);
f87299dab99bc04b51a6b8cad51b6795db862c0atrawick worker->ping_timeout_set = 1;
f87299dab99bc04b51a6b8cad51b6795db862c0atrawick }
4d12805e6c18253040223ea637acd6b3b3c18f60jorton else if (!strcasecmp(key, "lbset")) {
4d12805e6c18253040223ea637acd6b3b3c18f60jorton ival = atoi(val);
4d12805e6c18253040223ea637acd6b3b3c18f60jorton if (ival < 0 || ival > 99)
4d12805e6c18253040223ea637acd6b3b3c18f60jorton return "lbset must be between 0 and 99";
4d12805e6c18253040223ea637acd6b3b3c18f60jorton worker->lbset = ival;
e5d909f2b06bd880fb3675cd49363df981caa631trawick }
a4df2cd1e1391575a327c2a90ba4315f805a0a78covener else {
a4df2cd1e1391575a327c2a90ba4315f805a0a78covener return "unknown Worker parameter";
a4df2cd1e1391575a327c2a90ba4315f805a0a78covener }
cb666b29f81df1d11d65002250153353568021fccovener return NULL;
cb666b29f81df1d11d65002250153353568021fccovener}
cb666b29f81df1d11d65002250153353568021fccovener
6a80c3c6f4b8ea7ba5e89402b8b779b09ce020e0covenerstatic const char *set_balancer_param(proxy_server_conf *conf,
1c2cab00d988fc48cbe59032cf76cc0bab20d6f7covener apr_pool_t *p,
6a80c3c6f4b8ea7ba5e89402b8b779b09ce020e0covener proxy_balancer *balancer,
75a230a728338d84dcfe81edd375352f34de22d0covener const char *key,
75a230a728338d84dcfe81edd375352f34de22d0covener const char *val)
75a230a728338d84dcfe81edd375352f34de22d0covener{
1f50dc34ae069adeed20b2986e5ffdefa5c410e0covener
1f50dc34ae069adeed20b2986e5ffdefa5c410e0covener int ival;
1f50dc34ae069adeed20b2986e5ffdefa5c410e0covener if (!strcasecmp(key, "stickysession")) {
63a5ea80bddcc84a462e40f402b4f330e0e05411covener char *path;
63a5ea80bddcc84a462e40f402b4f330e0e05411covener /* Balancer sticky session name.
63a5ea80bddcc84a462e40f402b4f330e0e05411covener * Set to something like JSESSIONID or
63a5ea80bddcc84a462e40f402b4f330e0e05411covener * PHPSESSIONID, etc..,
65a4e663b82f8bce28ac22ab2edfd7502de36998sf */
65a4e663b82f8bce28ac22ab2edfd7502de36998sf path = apr_pstrdup(p, val);
65a4e663b82f8bce28ac22ab2edfd7502de36998sf balancer->sticky = balancer->sticky_path = path;
65a4e663b82f8bce28ac22ab2edfd7502de36998sf if ((path = strchr(path, '|'))) {
c7de1955eb0eaeabf7042902476397692672d549sf *path++ = '\0';
74e7f6c55fd67b10cb400b3f6d1dc718a303d944minfrin balancer->sticky_path = path;
74e7f6c55fd67b10cb400b3f6d1dc718a303d944minfrin }
74e7f6c55fd67b10cb400b3f6d1dc718a303d944minfrin }
74e7f6c55fd67b10cb400b3f6d1dc718a303d944minfrin else if (!strcasecmp(key, "nofailover")) {
a511a29faf2ff7ead3b67680154a624effb31aafminfrin /* If set to 'on' the session will break
a511a29faf2ff7ead3b67680154a624effb31aafminfrin * if the worker is in error state or
a511a29faf2ff7ead3b67680154a624effb31aafminfrin * disabled.
a511a29faf2ff7ead3b67680154a624effb31aafminfrin */
a511a29faf2ff7ead3b67680154a624effb31aafminfrin if (!strcasecmp(val, "on"))
63921358ef93fcb41bc71d9894221ba3d7fbb87bminfrin balancer->sticky_force = 1;
63921358ef93fcb41bc71d9894221ba3d7fbb87bminfrin else if (!strcasecmp(val, "off"))
63921358ef93fcb41bc71d9894221ba3d7fbb87bminfrin balancer->sticky_force = 0;
deec48c67d4786bc77112ffbf3a4e70b931097edminfrin else
6d601599d3d65df0410eae6e573e75b2dbfb1fb4minfrin return "failover must be On|Off";
6d601599d3d65df0410eae6e573e75b2dbfb1fb4minfrin }
6d601599d3d65df0410eae6e573e75b2dbfb1fb4minfrin else if (!strcasecmp(key, "timeout")) {
6d601599d3d65df0410eae6e573e75b2dbfb1fb4minfrin /* Balancer timeout in seconds.
684e0cfc200f66287a93bbd1708d1dd8a92a7eefcovener * If set this will be the maximum time to
684e0cfc200f66287a93bbd1708d1dd8a92a7eefcovener * wait for a free worker.
5c43d2fb853f84497b5ece2d414ef9484aa87e5fsf * Default is not to wait.
05a5a9c3e16f21566e1b61f4bd68025ce1b741ccjoes */
05a5a9c3e16f21566e1b61f4bd68025ce1b741ccjoes ival = atoi(val);
ef82e8fa164e0a1f8b813f7deb6b7ead96018c94niq if (ival < 1)
26c5829347f6a355c00f1ba0301d575056b69536niq return "timeout must be at least one second";
ef82e8fa164e0a1f8b813f7deb6b7ead96018c94niq balancer->timeout = apr_time_from_sec(ival);
ef82e8fa164e0a1f8b813f7deb6b7ead96018c94niq }
ef82e8fa164e0a1f8b813f7deb6b7ead96018c94niq else if (!strcasecmp(key, "maxattempts")) {
ef82e8fa164e0a1f8b813f7deb6b7ead96018c94niq /* Maximum number of failover attempts before
ef82e8fa164e0a1f8b813f7deb6b7ead96018c94niq * giving up.
ef82e8fa164e0a1f8b813f7deb6b7ead96018c94niq */
413ee814748f37be168ff12407fa6dba0ceeabe6trawick ival = atoi(val);
c12917da693bae4028a1d5a5e8224bceed8c739dsf if (ival < 0)
c12917da693bae4028a1d5a5e8224bceed8c739dsf return "maximum number of attempts must be a positive number";
eafcc0ebf263d0ba69855b6e10958c4c1a2361bdsf balancer->max_attempts = ival;
eafcc0ebf263d0ba69855b6e10958c4c1a2361bdsf balancer->max_attempts_set = 1;
eafcc0ebf263d0ba69855b6e10958c4c1a2361bdsf }
eafcc0ebf263d0ba69855b6e10958c4c1a2361bdsf else if (!strcasecmp(key, "lbmethod")) {
eafcc0ebf263d0ba69855b6e10958c4c1a2361bdsf proxy_balancer_method *provider;
d7ffd2da16d58b1a0de212e4d56f7aebb72bef26sf provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
d7ffd2da16d58b1a0de212e4d56f7aebb72bef26sf if (provider) {
d7ffd2da16d58b1a0de212e4d56f7aebb72bef26sf balancer->lbmethod = provider;
4576c1a9ef54cd1e5555ee07d016a7f559f80338sf return NULL;
4576c1a9ef54cd1e5555ee07d016a7f559f80338sf }
4576c1a9ef54cd1e5555ee07d016a7f559f80338sf return "unknown lbmethod";
9811aed12bbc71783d2e544ccb5fecd193843eadsf }
9811aed12bbc71783d2e544ccb5fecd193843eadsf else {
9811aed12bbc71783d2e544ccb5fecd193843eadsf return "unknown Balancer parameter";
88fac54d9d64f85bbdab5d7010816f4377f95bd7rjung }
88fac54d9d64f85bbdab5d7010816f4377f95bd7rjung return NULL;
bd3f5647b96d378d9c75c954e3f13582af32c643sf}
bd3f5647b96d378d9c75c954e3f13582af32c643sf
bd3f5647b96d378d9c75c954e3f13582af32c643sfstatic int alias_match(const char *uri, const char *alias_fakename)
bd3f5647b96d378d9c75c954e3f13582af32c643sf{
bd3f5647b96d378d9c75c954e3f13582af32c643sf const char *end_fakename = alias_fakename + strlen(alias_fakename);
2a7beea91d46beb41f043a84eaad060047ee04aafabien const char *aliasp = alias_fakename, *urip = uri;
2a7beea91d46beb41f043a84eaad060047ee04aafabien const char *end_uri = uri + strlen(uri);
2a7beea91d46beb41f043a84eaad060047ee04aafabien
2a7beea91d46beb41f043a84eaad060047ee04aafabien while (aliasp < end_fakename && urip < end_uri) {
584a85dd4047e38d3ed3a29b6662fcc9d100ae4csf if (*aliasp == '/') {
584a85dd4047e38d3ed3a29b6662fcc9d100ae4csf /* any number of '/' in the alias matches any number in
584a85dd4047e38d3ed3a29b6662fcc9d100ae4csf * the supplied URI, but there must be at least one...
f21e9e3d0bfb7a507ecc5bc963f2159d693503d1sf */
f21e9e3d0bfb7a507ecc5bc963f2159d693503d1sf if (*urip != '/')
f21e9e3d0bfb7a507ecc5bc963f2159d693503d1sf return 0;
f6b9c755a0b793e8a3a3aebd327ca20a86478117sf
f6b9c755a0b793e8a3a3aebd327ca20a86478117sf while (*aliasp == '/')
f6b9c755a0b793e8a3a3aebd327ca20a86478117sf ++aliasp;
132ee6ac1c26d6e8953836316ba50734eefab47bsf while (*urip == '/')
132ee6ac1c26d6e8953836316ba50734eefab47bsf ++urip;
132ee6ac1c26d6e8953836316ba50734eefab47bsf }
85eacfc96a04547ef25aabbc06440039715084c2jorton else {
85eacfc96a04547ef25aabbc06440039715084c2jorton /* Other characters are compared literally */
85eacfc96a04547ef25aabbc06440039715084c2jorton if (*urip++ != *aliasp++)
536d2e7cd1fdec1255b8c3bdf41fdc714c506a54trawick return 0;
536d2e7cd1fdec1255b8c3bdf41fdc714c506a54trawick }
536d2e7cd1fdec1255b8c3bdf41fdc714c506a54trawick }
536d2e7cd1fdec1255b8c3bdf41fdc714c506a54trawick
79c5787b92ac5f0e1cc82393816c77a006399316trawick /* fixup badly encoded stuff (e.g. % as last character) */
79c5787b92ac5f0e1cc82393816c77a006399316trawick if (aliasp > end_fakename) {
79c5787b92ac5f0e1cc82393816c77a006399316trawick aliasp = end_fakename;
79c5787b92ac5f0e1cc82393816c77a006399316trawick }
c967bf3bc89e8aa60dbd30d9da388e448ddc1cc4trawick if (urip > end_uri) {
79c5787b92ac5f0e1cc82393816c77a006399316trawick urip = end_uri;
79c5787b92ac5f0e1cc82393816c77a006399316trawick }
79c5787b92ac5f0e1cc82393816c77a006399316trawick
79c5787b92ac5f0e1cc82393816c77a006399316trawick /* We reach the end of the uri before the end of "alias_fakename"
79c5787b92ac5f0e1cc82393816c77a006399316trawick * for example uri is "/" and alias_fakename "/examples"
7b395e4e878c28a4784919cfd2e704ddd14a3390jorton */
7b395e4e878c28a4784919cfd2e704ddd14a3390jorton if (urip == end_uri && aliasp!=end_fakename) {
7b395e4e878c28a4784919cfd2e704ddd14a3390jorton return 0;
7b395e4e878c28a4784919cfd2e704ddd14a3390jorton }
536e48c08d674acac5d44929318f2ad928edc361jorton
536e48c08d674acac5d44929318f2ad928edc361jorton /* Check last alias path component matched all the way */
e81785da447b469da66f218b3f0244aab507958djorton if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
e81785da447b469da66f218b3f0244aab507958djorton return 0;
3e4e54d4e3fc0123c63d57aa84ac7ad7a8c73ff8jorton
3e4e54d4e3fc0123c63d57aa84ac7ad7a8c73ff8jorton /* Return number of characters from URI which matched (may be
3e4e54d4e3fc0123c63d57aa84ac7ad7a8c73ff8jorton * greater than length of alias, since we may have matched
53e9b27aba029b18be814df40bcf6f0428771d1efuankg * doubled slashes)
53e9b27aba029b18be814df40bcf6f0428771d1efuankg */
53e9b27aba029b18be814df40bcf6f0428771d1efuankg
53e9b27aba029b18be814df40bcf6f0428771d1efuankg return urip - uri;
53e9b27aba029b18be814df40bcf6f0428771d1efuankg}
6bb524f1895f30265a1431afc460977d391cb36bsf
6bb524f1895f30265a1431afc460977d391cb36bsf/* Detect if an absoluteURI should be proxied or not. Note that we
ca61ccd0c306c2c72df153688ba1b49f3eceed80sf * have to do this during this phase because later phases are
6bb524f1895f30265a1431afc460977d391cb36bsf * "short-circuiting"... i.e. translate_names will end when the first
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrin * module returns OK. So for example, if the request is something like:
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrin *
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrin * GET http://othervhost/cgi-bin/printenv HTTP/1.0
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrin *
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin * mod_alias will notice the /cgi-bin part and ScriptAlias it and
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin * short-circuit the proxy... just because of the ordering in the
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin * configuration file.
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin */
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjungstatic int proxy_detect(request_rec *r)
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung{
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung void *sconf = r->server->module_config;
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung proxy_server_conf *conf =
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung /* Ick... msvc (perhaps others) promotes ternary short results to int */
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick if (conf->req && r->parsed_uri.scheme) {
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick /* but it might be something vhosted */
0827cb14e550f6f65018431c22c2c913631c8f25kbrand if (!(r->parsed_uri.hostname
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick && !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))
ae600ca541efc686b34f8b1f21bd3d0741d37674covener && ap_matches_request_vhost(r, r->parsed_uri.hostname,
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
cfa64348224b66dd1c9979b809406c4d15b1c137fielding : ap_default_port(r))))) {
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajim r->proxyreq = PROXYREQ_PROXY;
cfa64348224b66dd1c9979b809406c4d15b1c137fielding r->uri = r->unparsed_uri;
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajim r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
cfa64348224b66dd1c9979b809406c4d15b1c137fielding r->handler = "proxy-server";
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajim }
cfa64348224b66dd1c9979b809406c4d15b1c137fielding }
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajim /* We need special treatment for CONNECT proxying: it has no scheme part */
cfa64348224b66dd1c9979b809406c4d15b1c137fielding else if (conf->req && r->method_number == M_CONNECT
&& r->parsed_uri.hostname
&& r->parsed_uri.port_str) {
r->proxyreq = PROXYREQ_PROXY;
r->uri = r->unparsed_uri;
r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
r->handler = "proxy-server";
}
return DECLINED;
}
static const char *proxy_interpolate(request_rec *r, const char *str)
{
/* Interpolate an env str in a configuration string
* Syntax ${var} --> value_of(var)
* Method: replace one var, and recurse on remainder of string
* Nothing clever here, and crap like nested vars may do silly things
* but we'll at least avoid sending the unwary into a loop
*/
const char *start;
const char *end;
const char *var;
const char *val;
const char *firstpart;
start = ap_strstr_c(str, "${");
if (start == NULL) {
return str;
}
end = ap_strchr_c(start+2, '}');
if (end == NULL) {
return str;
}
/* OK, this is syntax we want to interpolate. Is there such a var ? */
var = apr_pstrndup(r->pool, start+2, end-(start+2));
val = apr_table_get(r->subprocess_env, var);
firstpart = apr_pstrndup(r->pool, str, (start-str));
if (val == NULL) {
return apr_pstrcat(r->pool, firstpart,
proxy_interpolate(r, end+1), NULL);
}
else {
return apr_pstrcat(r->pool, firstpart, val,
proxy_interpolate(r, end+1), NULL);
}
}
static apr_array_header_t *proxy_vars(request_rec *r,
apr_array_header_t *hdr)
{
int i;
apr_array_header_t *ret = apr_array_make(r->pool, hdr->nelts,
sizeof (struct proxy_alias));
struct proxy_alias *old = (struct proxy_alias *) hdr->elts;
for (i = 0; i < hdr->nelts; ++i) {
struct proxy_alias *newcopy = apr_array_push(ret);
newcopy->fake = proxy_interpolate(r, old[i].fake);
newcopy->real = proxy_interpolate(r, old[i].real);
}
return ret;
}
static int proxy_trans(request_rec *r)
{
void *sconf = r->server->module_config;
proxy_server_conf *conf =
(proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
int i, len;
struct proxy_alias *ent = (struct proxy_alias *) conf->aliases->elts;
proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
&proxy_module);
const char *fake;
const char *real;
ap_regmatch_t regm[AP_MAX_REG_MATCH];
char *found = NULL;
if (r->proxyreq) {
/* someone has already set up the proxy, it was possibly ourselves
* in proxy_detect
*/
return OK;
}
/* XXX: since r->uri has been manipulated already we're not really
* compliant with RFC1945 at this point. But this probably isn't
* an issue because this is a hybrid proxy/origin server.
*/
for (i = 0; i < conf->aliases->nelts; i++) {
if (dconf->interpolate_env == 1) {
fake = proxy_interpolate(r, ent[i].fake);
real = proxy_interpolate(r, ent[i].real);
}
else {
fake = ent[i].fake;
real = ent[i].real;
}
if (ent[i].regex) {
if (!ap_regexec(ent[i].regex, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
if ((real[0] == '!') && (real[1] == '\0')) {
return DECLINED;
}
found = ap_pregsub(r->pool, real, r->uri, AP_MAX_REG_MATCH,
regm);
/* Note: The strcmp() below catches cases where there
* was no regex substitution. This is so cases like:
*
* ProxyPassMatch \.gif balancer://foo
*
* will work "as expected". The upshot is that the 2
* directives below act the exact same way (ie: $1 is implied):
*
* ProxyPassMatch ^(/.*\.gif)$ balancer://foo
* ProxyPassMatch ^(/.*\.gif)$ balancer://foo$1
*
* which may be confusing.
*/
if (found && strcmp(found, real)) {
found = apr_pstrcat(r->pool, "proxy:", found, NULL);
}
else {
found = apr_pstrcat(r->pool, "proxy:", real, r->uri,
NULL);
}
}
}
else {
len = alias_match(r->uri, fake);
if (len != 0) {
if ((real[0] == '!') && (real[1] == '\0')) {
return DECLINED;
}
found = apr_pstrcat(r->pool, "proxy:", real,
r->uri + len, NULL);
}
}
if (found) {
r->filename = found;
r->handler = "proxy-server";
r->proxyreq = PROXYREQ_REVERSE;
return OK;
}
}
return DECLINED;
}
static int proxy_walk(request_rec *r)
{
proxy_server_conf *sconf = ap_get_module_config(r->server->module_config,
&proxy_module);
ap_conf_vector_t *per_dir_defaults = r->server->lookup_defaults;
ap_conf_vector_t **sec_proxy = (ap_conf_vector_t **) sconf->sec_proxy->elts;
ap_conf_vector_t *entry_config;
proxy_dir_conf *entry_proxy;
int num_sec = sconf->sec_proxy->nelts;
/* XXX: shouldn't we use URI here? Canonicalize it first?
* Pass over "proxy:" prefix
*/
const char *proxyname = r->filename + 6;
int j;
for (j = 0; j < num_sec; ++j)
{
entry_config = sec_proxy[j];
entry_proxy = ap_get_module_config(entry_config, &proxy_module);
/* XXX: What about case insensitive matching ???
* Compare regex, fnmatch or string as appropriate
* If the entry doesn't relate, then continue
*/
if (entry_proxy->r
? ap_regexec(entry_proxy->r, proxyname, 0, NULL, 0)
: (entry_proxy->p_is_fnmatch
? apr_fnmatch(entry_proxy->p, proxyname, 0)
: strncmp(proxyname, entry_proxy->p,
strlen(entry_proxy->p)))) {
continue;
}
per_dir_defaults = ap_merge_per_dir_configs(r->pool, per_dir_defaults,
entry_config);
}
r->per_dir_config = per_dir_defaults;
return OK;
}
static int proxy_map_location(request_rec *r)
{
int access_status;
if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
return DECLINED;
/* Don't let the core or mod_http map_to_storage hooks handle this,
* We don't need directory/file_walk, and we want to TRACE on our own.
*/
if ((access_status = proxy_walk(r))) {
ap_die(access_status, r);
return access_status;
}
return OK;
}
/* -------------------------------------------------------------- */
/* Fixup the filename */
/*
* Canonicalise the URL
*/
static int proxy_fixup(request_rec *r)
{
char *url, *p;
int access_status;
proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
&proxy_module);
if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
return DECLINED;
/* XXX: Shouldn't we try this before we run the proxy_walk? */
url = &r->filename[6];
if ((dconf->interpolate_env == 1) && (r->proxyreq == PROXYREQ_REVERSE)) {
/* create per-request copy of reverse proxy conf,
* and interpolate vars in it
*/
proxy_req_conf *rconf = apr_palloc(r->pool, sizeof(proxy_req_conf));
ap_set_module_config(r->request_config, &proxy_module, rconf);
rconf->raliases = proxy_vars(r, dconf->raliases);
rconf->cookie_paths = proxy_vars(r, dconf->cookie_paths);
rconf->cookie_domains = proxy_vars(r, dconf->cookie_domains);
}
/* canonicalise each specific scheme */
if ((access_status = proxy_run_canon_handler(r, url))) {
return access_status;
}
p = strchr(url, ':');
if (p == NULL || p == url)
return HTTP_BAD_REQUEST;
return OK; /* otherwise; we've done the best we can */
}
/* Send a redirection if the request contains a hostname which is not */
/* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
/* servers like Netscape's allow this and access hosts from the local */
/* domain in this case. I think it is better to redirect to a FQDN, since */
/* these will later be found in the bookmarks files. */
/* The "ProxyDomain" directive determines what domain will be appended */
static int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
{
char *nuri;
const char *ref;
/* We only want to worry about GETs */
if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
return DECLINED;
/* If host does contain a dot already, or it is "localhost", decline */
if (strchr(r->parsed_uri.hostname, '.') != NULL
|| strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
return DECLINED; /* host name has a dot already */
ref = apr_table_get(r->headers_in, "Referer");
/* Reassemble the request, but insert the domain after the host name */
/* Note that the domain name always starts with a dot */
r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
domain, NULL);
nuri = apr_uri_unparse(r->pool,
&r->parsed_uri,
APR_URI_UNP_REVEALPASSWORD);
apr_table_set(r->headers_out, "Location", nuri);
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
"Domain missing: %s sent to %s%s%s", r->uri,
apr_uri_unparse(r->pool, &r->parsed_uri,
APR_URI_UNP_OMITUSERINFO),
ref ? " from " : "", ref ? ref : "");
return HTTP_MOVED_PERMANENTLY;
}
/* -------------------------------------------------------------- */
/* Invoke handler */
static int proxy_handler(request_rec *r)
{
char *uri, *scheme, *p;
const char *p2;
void *sconf = r->server->module_config;
proxy_server_conf *conf = (proxy_server_conf *)
ap_get_module_config(sconf, &proxy_module);
apr_array_header_t *proxies = conf->proxies;
struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
int i, rc, access_status;
int direct_connect = 0;
const char *str;
long maxfwd;
proxy_balancer *balancer = NULL;
proxy_worker *worker = NULL;
int attempts = 0, max_attempts = 0;
struct dirconn_entry *list = (struct dirconn_entry *)conf->dirconn->elts;
/* is this for us? */
if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
return DECLINED;
/* handle max-forwards / OPTIONS / TRACE */
if ((str = apr_table_get(r->headers_in, "Max-Forwards"))) {
maxfwd = strtol(str, NULL, 10);
if (maxfwd < 1) {
switch (r->method_number) {
case M_TRACE: {
int access_status;
r->proxyreq = PROXYREQ_NONE;
if ((access_status = ap_send_http_trace(r)))
ap_die(access_status, r);
else
ap_finalize_request_protocol(r);
return OK;
}
case M_OPTIONS: {
int access_status;
r->proxyreq = PROXYREQ_NONE;
if ((access_status = ap_send_http_options(r)))
ap_die(access_status, r);
else
ap_finalize_request_protocol(r);
return OK;
}
default: {
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
"Max-Forwards has reached zero - proxy loop?");
}
}
}
maxfwd = (maxfwd > 0) ? maxfwd - 1 : 0;
}
else {
/* set configured max-forwards */
maxfwd = conf->maxfwd;
}
apr_table_set(r->headers_in, "Max-Forwards",
apr_psprintf(r->pool, "%ld", (maxfwd > 0) ? maxfwd : 0));
if (r->method_number == M_TRACE) {
core_server_config *coreconf = (core_server_config *)
ap_get_module_config(sconf, &core_module);
if (coreconf->trace_enable == AP_TRACE_DISABLE)
{
/* Allow "error-notes" string to be printed by ap_send_error_response()
* Note; this goes nowhere, canned error response need an overhaul.
*/
apr_table_setn(r->notes, "error-notes",
"TRACE forbidden by server configuration");
apr_table_setn(r->notes, "verbose-error-to", "*");
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"proxy: TRACE forbidden by server configuration");
return HTTP_METHOD_NOT_ALLOWED;
}
/* Can't test ap_should_client_block, we aren't ready to send
* the client a 100 Continue response till the connection has
* been established
*/
if (coreconf->trace_enable != AP_TRACE_EXTENDED
&& (r->read_length || r->read_chunked || r->remaining))
{
/* Allow "error-notes" string to be printed by ap_send_error_response()
* Note; this goes nowhere, canned error response need an overhaul.
*/
apr_table_setn(r->notes, "error-notes",
"TRACE with request body is not allowed");
apr_table_setn(r->notes, "verbose-error-to", "*");
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"proxy: TRACE with request body is not allowed");
return HTTP_REQUEST_ENTITY_TOO_LARGE;
}
}
uri = r->filename + 6;
p = strchr(uri, ':');
if (p == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"proxy_handler no URL in %s", r->filename);
return HTTP_BAD_REQUEST;
}
/* 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;
}
if (balancer && balancer->max_attempts_set && !max_attempts)
max_attempts = balancer->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_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"Trying to run scheme_handler against proxy");
access_status = proxy_run_scheme_handler(r, worker,
conf, url,
ents[i].hostname,
ents[i].port);
/* an error or success */
if (access_status != DECLINED &&
access_status != HTTP_BAD_GATEWAY) {
goto cleanup;
}
/* we failed to talk to the upstream proxy */
}
}
}
/* otherwise, try it direct */
/* N.B. what if we're behind a firewall, where we must use a proxy or
* give up??
*/
/* handle the scheme */
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"Running scheme %s handler (attempt %d)",
scheme, 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;
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;
}
}
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_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
"proxy: 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_FORBIDDEN;
goto cleanup;
}
cleanup:
if (balancer) {
int post_status = proxy_run_post_request(worker, balancer, r, conf);
if (post_status == DECLINED) {
post_status = OK; /* no post_request handler available */
/* TODO: recycle direct worker */
}
}
proxy_run_request_status(&access_status, r);
return access_status;
}
/* -------------------------------------------------------------- */
/* Setup configurable data */
static void * create_proxy_config(apr_pool_t *p, server_rec *s)
{
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->allowed_connect_ports = apr_array_make(p, 10, sizeof(int));
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;
ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
ps->viaopt_set = 0; /* 0 means default */
ps->req = 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->error_override = 0;
ps->error_override_set = 0;
ps->preserve_host_set = 0;
ps->preserve_host = 0;
ps->timeout = 0;
ps->timeout_set = 0;
ps->badopt = bad_error;
ps->badopt_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->allowed_connect_ports = apr_array_append(p, base->allowed_connect_ports, overrides->allowed_connect_ports);
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->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->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->error_override = (overrides->error_override_set == 0) ? base->error_override : overrides->error_override;
ps->error_override_set = overrides->error_override_set || base->error_override_set;
ps->preserve_host = (overrides->preserve_host_set == 0) ? base->preserve_host : overrides->preserve_host;
ps->preserve_host_set = overrides->preserve_host_set || base->preserve_host_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->pool = p;
return ps;
}
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->cookie_path_str = apr_strmatch_precompile(p, "path=", 0);
new->cookie_domain_str = apr_strmatch_precompile(p, "domain=", 0);
new->interpolate_env = -1; /* unset */
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->cookie_path_str = base->cookie_path_str;
new->cookie_domain_str = base->cookie_domain_str;
new->interpolate_env = (add->interpolate_env == -1) ? base->interpolate_env
: add->interpolate_env;
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)
{
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;
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 {
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";
new = apr_array_push(conf->aliases);
new->fake = apr_pstrdup(cmd->pool, f);
new->real = apr_pstrdup(cmd->pool, r);
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 (strncasecmp(r, "balancer:", 9) == 0) {
proxy_balancer *balancer = ap_proxy_get_balancer(cmd->pool, conf, r);
if (!balancer) {
const char *err = ap_proxy_add_balancer(&balancer,
cmd->pool,
conf, r);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
}
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);
}
}
else {
proxy_worker *worker = ap_proxy_get_worker(cmd->temp_pool, conf, r);
if (!worker) {
const char *err = ap_proxy_add_worker(&worker, cmd->pool, conf, r);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
} else {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
"worker %s already used by another worker", worker->name);
}
PROXY_COPY_CONF_PARAMS(worker, conf);
for (i = 0; i < arr->nelts; i++) {
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)
{
proxy_dir_conf *conf = dconf;
struct proxy_alias *new;
if (r!=NULL && cmd->path == NULL ) {
new = apr_array_push(conf->raliases);
new->fake = f;
new->real = r;
} else if (r==NULL && cmd->path != NULL) {
new = apr_array_push(conf->raliases);
new->fake = cmd->path;
new->real = f;
} else {
if ( r == NULL)
return "ProxyPassReverse needs a path when not defined in a location";
else
return "ProxyPassReverse can not have a path when defined in a location";
}
return NULL;
}
static const char*
cookie_path(cmd_parms *cmd, void *dconf, const char *f, const char *r)
{
proxy_dir_conf *conf = dconf;
struct proxy_alias *new;
new = apr_array_push(conf->cookie_paths);
new->fake = f;
new->real = r;
return NULL;
}
static const char*
cookie_domain(cmd_parms *cmd, void *dconf, const char *f, const char *r)
{
proxy_dir_conf *conf = dconf;
struct proxy_alias *new;
new = apr_array_push(conf->cookie_domains);
new->fake = f;
new->real = r;
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;
}
/*
* Set the ports CONNECT can use
*/
static const char *
set_allowed_ports(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);
int *New;
if (!apr_isdigit(arg[0]))
return "AllowCONNECT: port number must be numeric";
New = apr_array_push(conf->allowed_connect_ports);
*New = atoi(arg);
return NULL;
}
/* Similar to set_proxy_exclude(), but defining directly connected hosts,
* which should never be accessed via the configured ProxyRemote servers
*/
static const char *
set_proxy_dirconn(cmd_parms *parms, void *dummy, 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;
if (flag && !psf->forward) {
psf->forward = ap_proxy_create_worker(parms->pool);
psf->forward->name = "proxy:forward";
psf->forward->hostname = "*";
psf->forward->scheme = "*";
}
return NULL;
}
static const char *
set_proxy_error_override(cmd_parms *parms, void *dummy, int flag)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
psf->error_override = flag;
psf->error_override_set = 1;
return NULL;
}
static const char *
set_preserve_host(cmd_parms *parms, void *dummy, int flag)
{
proxy_server_conf *psf =
ap_get_module_config(parms->server->module_config, &proxy_module);
psf->preserve_host = flag;
psf->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);
psf->io_buffer_size = ((s > AP_IOBUFSIZE) ? 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);
if (s < 0) {
return "ProxyMaxForwards must be greater or equal to zero..";
}
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 *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 i;
if (cmd->path)
path = apr_pstrdup(cmd->pool, cmd->path);
while (*arg) {
word = ap_getword_conf(cmd->pool, &arg);
if (!path)
path = word;
else if (!name)
name = word;
else {
char *val = strchr(word, '=');
if (!val)
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 existing worker */
worker = ap_proxy_get_worker(cmd->temp_pool, conf, name);
if (!worker) {
const char *err;
if ((err = ap_proxy_add_worker(&worker, cmd->pool, conf, name)) != NULL)
return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
} else {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
"worker %s already used by another worker", worker->name);
}
PROXY_COPY_CONF_PARAMS(worker, conf);
arr = apr_table_elts(params);
elts = (const apr_table_entry_t *)arr->elts;
for (i = 0; i < arr->nelts; i++) {
const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
elts[i].val);
if (err)
return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
}
/* Try to find the balancer */
balancer = ap_proxy_get_balancer(cmd->temp_pool, conf, path);
if (!balancer) {
const char *err = ap_proxy_add_balancer(&balancer,
cmd->pool,
conf, path);
if (err)
return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
}
/* Add the worker to the load balancer */
ap_proxy_add_worker_to_balancer(cmd->pool, balancer, worker);
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;
const char *err;
int in_proxy_section = 0;
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 (strncasecmp(name, "balancer:", 9) == 0) {
balancer = ap_proxy_get_balancer(cmd->pool, conf, name);
if (!balancer) {
if (in_proxy_section) {
err = ap_proxy_add_balancer(&balancer,
cmd->pool,
conf, name);
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, conf, name);
if (!worker) {
if (in_proxy_section) {
err = ap_proxy_add_worker(&worker, cmd->pool,
conf, name);
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|NOT_IN_LIMIT);
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 (strncasecmp(conf->p, "balancer:", 9) == 0) {
balancer = ap_proxy_get_balancer(cmd->pool, sconf, conf->p);
if (!balancer) {
err = ap_proxy_add_balancer(&balancer,
cmd->pool,
sconf, conf->p);
if (err)
return apr_pstrcat(cmd->temp_pool, thiscmd->name,
" ", err, NULL);
}
}
else {
worker = ap_proxy_get_worker(cmd->temp_pool, sconf,
conf->p);
if (!worker) {
err = ap_proxy_add_worker(&worker, cmd->pool,
sconf, conf->p);
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,
(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_TAKE12("ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF|ACCESS_CONF,
"a virtual path and a URL for reverse proxy behaviour"),
AP_INIT_TAKE2("ProxyPassReverseCookiePath", cookie_path, NULL,
RSRC_CONF|ACCESS_CONF, "Path rewrite rule for proxying cookies"),
AP_INIT_TAKE2("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_ITERATE("AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF,
"A list of ports which CONNECT may connect to"),
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,
"use our error handling pages instead of the servers' we are proxying"),
AP_INIT_FLAG("ProxyPreserveHost", set_preserve_host, NULL, RSRC_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("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"),
{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);
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->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->sticky) {
if (strcmp(balancer->sticky, balancer->sticky_path)) {
ap_rvputs(r, "<td>", balancer->sticky, " | ",
balancer->sticky_path, NULL);
}
else {
ap_rvputs(r, "<td>", balancer->sticky, NULL);
}
}
else {
ap_rputs("<td> - ", r);
}
ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
apr_time_sec(balancer->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->scheme, "</td>", NULL);
ap_rvputs(r, "<td>", worker->hostname, "</td><td>", NULL);
if (worker->s->status & PROXY_WORKER_DISABLED)
ap_rputs("Dis", r);
else if (worker->s->status & PROXY_WORKER_IN_ERROR)
ap_rputs("Err", r);
else if (worker->s->status & PROXY_WORKER_INITIALIZED)
ap_rputs("Ok", r);
else
ap_rputs("-", r);
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 in %</td></tr>\n"
"<tr><th>Acc</th><td>Number of requests</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;
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);
/* Initialize worker's shared scoreboard data */
worker = (proxy_worker *)conf->workers->elts;
for (i = 0; i < conf->workers->nelts; i++) {
ap_proxy_initialize_worker_share(conf, worker, s);
ap_proxy_initialize_worker(worker, s);
worker++;
}
/* Initialize forward worker if defined */
if (conf->forward) {
ap_proxy_initialize_worker_share(conf, conf->forward, s);
ap_proxy_initialize_worker(conf->forward, s);
/* Do not disable worker in case of errors */
conf->forward->s->status |= PROXY_WORKER_IGNORE_ERRORS;
/* Disable address cache for generic forward worker */
conf->forward->is_address_reusable = 0;
}
if (!reverse) {
reverse = ap_proxy_create_worker(p);
reverse->name = "proxy:reverse";
reverse->hostname = "*";
reverse->scheme = "*";
ap_proxy_initialize_worker_share(conf, reverse, s);
ap_proxy_initialize_worker(reverse, s);
/* Do not disable worker in case of errors */
reverse->s->status |= PROXY_WORKER_IGNORE_ERRORS;
/* Disable address cache for generic reverse worker */
reverse->is_address_reusable = 0;
}
conf->reverse = reverse;
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", NULL};
APR_REGISTER_OPTIONAL_FN(ap_proxy_lb_workers);
/* 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);
}
module AP_MODULE_DECLARE_DATA proxy_module =
{
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)