mod_proxy_balancer.c revision 2f1146e06a1bfa371573a3f3fb0379448e18aaed
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu/* Licensed to the Apache Software Foundation (ASF) under one or more
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * contributor license agreements. See the NOTICE file distributed with
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * this work for additional information regarding copyright ownership.
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * The ASF licenses this file to You under the Apache License, Version 2.0
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu * (the "License"); you may not use this file except in compliance with
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * the License. You may obtain a copy of the License at
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * http://www.apache.org/licenses/LICENSE-2.0
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * Unless required by applicable law or agreed to in writing, software
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * distributed under the License is distributed on an "AS IS" BASIS,
b87efd3db0d2dc41615ea28669faf80fc1b48d56Corneliu-Claudiu Prodescu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81c09542d31779a407196fbd710aa8e69a815522Georgel Calin * See the License for the specific language governing permissions and
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder * limitations under the License.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin/* Load balancer module for Apache proxy */
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic const char *balancer_mutex_type = "proxy-balancer-shm";
81c09542d31779a407196fbd710aa8e69a815522Georgel Calinmodule AP_MODULE_DECLARE_DATA proxy_balancer_module;
81c09542d31779a407196fbd710aa8e69a815522Georgel Calinstatic char balancer_nonce[APR_UUID_FORMATTED_LENGTH + 1];
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * Register our mutex type before the config is read so we
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * can adjust the mutex settings using the Mutex directive.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin ap_mutex_register(pconf, balancer_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
81c09542d31779a407196fbd710aa8e69a815522Georgel Calinextern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic int proxy_balancer_canon(request_rec *r, char *url)
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin const char *err;
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin "proxy: BALANCER: canonicalising URL %s", url);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin /* do syntatic check.
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * We break the URL into host, port, path, search
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin "error parsing URL %s: %s",
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * now parse path/search args, according to rfc1738:
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * process the path. With proxy-noncanon set (by
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * mod_proxy) we use the raw, unparsed uri
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin if (apr_table_get(r->notes, "proxy-nocanon")) {
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic int init_balancer_members(proxy_server_conf *conf, server_rec *s,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin workers = (proxy_worker **)balancer->workers->elts;
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin for (i = 0; i < balancer->workers->nelts; i++) {
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(*workers);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * If the worker is not initialized check whether its scoreboard
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * slot is already initialized.
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder slot = (proxy_worker_shared *) XXXXXap_get_scoreboard_lb((*workers)->id);
902bfaac7e88afebb6684fe1f2414ae2efbc7edfChristian Maeder worker_is_initialized = slot->status & PROXY_WORKER_INITIALIZED;
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin ap_proxy_initialize_worker_share(conf, *workers, s);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin ap_proxy_initialize_worker(*workers, s, conf->pool);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin /* Set to the original configuration */
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin (*workers)->s->lbstatus = (*workers)->s->lbfactor =
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin ((*workers)->lbfactor ? (*workers)->lbfactor : 1);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin /* Set default number of attempts to the number of
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin balancer->max_attempts = balancer->workers->nelts - 1;
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin/* Retrieve the parameter with the given name
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * Something like 'JSESSIONID=12345...N'
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic char *get_path_param(apr_pool_t *pool, char *url,
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin * Session path was found, get it's value
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calinstatic char *get_cookie_param(request_rec *r, const char *name)
05d4bb01e11ad19f2efb41a3efec59a7784c9942Christian Maeder if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
3d13c5135e7bab987599acf23afe9c59df49b06fGeorgel Calin for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
05d4bb01e11ad19f2efb41a3efec59a7784c9942Christian Maeder start_cookie = ap_strstr_c(start_cookie + 1, name)) {
++start_cookie;
return cookie;
return NULL;
int checking_standby;
int checked_standby;
while (!checked_standby) {
return worker;
return worker;
return rworker;
return NULL;
request_rec *r,
char **route,
const char **sticky_used,
char **url)
return NULL;
if (*route) {
if (*route) {
(*route)++;
return worker;
return NULL;
request_rec *r)
return NULL;
if (candidate)
return candidate;
char **url)
if (scheme)
if (!worker) {
NULL));
return OK;
int ok = 0;
if (!ok) {
request_rec *r,
int access_status;
if (!*balancer &&
return DECLINED;
return DECLINED;
if (runtime) {
int i, total_factor = 0;
workers++;
int i, member_of = 0;
workers++;
if (member_of) {
return HTTP_SERVICE_UNAVAILABLE;
if (!*worker) {
if (!runtime) {
return HTTP_SERVICE_UNAVAILABLE;
if (route) {
return access_status;
request_rec *r,
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
void *data;
if (!data) {
s, pconf, 0);
return HTTP_INTERNAL_SERVER_ERROR;
if (!storage) {
return !OK;
return !OK;
unsigned int index;
return !OK;
s = s->next;
return OK;
void *sconf;
int access_status;
const char *name;
return DECLINED;
return DECLINED;
if (r->args) {
return access_status;
return HTTP_BAD_REQUEST;
if (*balancer_nonce &&
++workers;
if (wsel) {
const char *val;
if (bsel)
++workers;
++balancer;
NULL);
++workers;
++balancer;
NULL);
NULL);
return OK;
unsigned int num;
return !OK;
balancer++;
s = s->next;
const char *val)
return err;
if (val) {
return NULL;
{NULL}