]> granicus.if.org Git - apache/blobdiff - modules/proxy/mod_proxy.c
Close PR 32459, 15207. API change for PROXY_DECLARE ap_proxy_canonenc()
[apache] / modules / proxy / mod_proxy.c
index 73fce3c1b635d0cb498c66edaa1343e4319a6920..15372beb7b80ffe9cacd5c87508d61030dd7d99c 100644 (file)
@@ -1,4 +1,3 @@
-#define FIX_15207
 /* Copyright 1999-2004 The Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,8 +17,9 @@
 
 #include "mod_proxy.h"
 #include "mod_core.h"
-
 #include "apr_optional.h"
+#include "scoreboard.h"
+#include "mod_status.h"
 
 #if (MODULE_MAGIC_NUMBER_MAJOR > 20020903)
 #include "mod_ssl.h"
@@ -72,64 +72,158 @@ static unsigned char hex2c(const char* p) {
 }
 #endif
 
-static const char *set_worker_param(proxy_worker *worker,
+#define PROXY_COPY_CONF_PARAMS(w, c) \
+    do {                             \
+        (w)->timeout              = (c)->timeout;               \
+        (w)->timeout_set          = (c)->timeout_set;           \
+        (w)->recv_buffer_size     = (c)->recv_buffer_size;      \
+        (w)->recv_buffer_size_set = (c)->recv_buffer_size_set;  \
+        (w)->io_buffer_size       = (c)->io_buffer_size;        \
+        (w)->io_buffer_size_set   = (c)->io_buffer_size_set;    \
+    } while (0)
+
+static const char *set_worker_param(apr_pool_t *p,
+                                    proxy_worker *worker,
                                     const char *key,
                                     const char *val)
 {
 
     int ival;
     if (!strcasecmp(key, "loadfactor")) {
+        /* Normalized load factor. Used with BalancerMamber,
+         * it is a number between 1 and 100.
+         */
         worker->lbfactor = atoi(val);
         if (worker->lbfactor < 1 || worker->lbfactor > 100)
-            return "loadfactor must be number between 1..100";
+            return "LoadFactor must be number between 1..100";
     }
     else if (!strcasecmp(key, "retry")) {
+        /* If set it will give the retry timeout for the worker
+         * The default value is 60 seconds, meaning that if
+         * in error state, it will be retried after that timeout.
+         */
         ival = atoi(val);
         if (ival < 1)
-            return "retry must be al least one second";
+            return "Retry must be at least one second";
         worker->retry = apr_time_from_sec(ival);
     }
     else if (!strcasecmp(key, "ttl")) {
+        /* Time in seconds that will destroy all the connections
+         * that exced the smax 
+         */
         ival = atoi(val);
         if (ival < 1)
-            return "ttl must be al least one second";
+            return "TTL must be at least one second";
         worker->ttl = apr_time_from_sec(ival);
     }
     else if (!strcasecmp(key, "min")) {
+        /* Initial number of connections to remote
+         */
         ival = atoi(val);
         if (ival < 0)
-            return "min must be a positive number";
+            return "Min must be a positive number";
         worker->min = ival;
     }
     else if (!strcasecmp(key, "max")) {
+        /* Maximum number of connections to remote
+         */
         ival = atoi(val);
         if (ival < 0)
-            return "max must be a positive number";
+            return "Max must be a positive number";
         worker->hmax = ival;
     }
     /* XXX: More inteligent naming needed */
     else if (!strcasecmp(key, "smax")) {
+        /* Maximum number of connections to remote that
+         * will not be destroyed
+         */
         ival = atoi(val);
         if (ival < 0)
-            return "smax must be a positive number";
+            return "Smax must be a positive number";
         worker->smax = ival;
     }
+    else if (!strcasecmp(key, "acquire")) {
+        /* Acquire timeout in milliseconds.
+         * If set this will be the maximum time to
+         * wait for a free connection.
+         */
+        ival = atoi(val);
+        if (ival < 1)
+            return "Acquire must be at least one mili second";
+        worker->acquire = apr_time_make(0, ival * 1000);
+        worker->acquire_set = 1;
+    }
+    else if (!strcasecmp(key, "timeout")) {
+        /* Connection timeout in seconds.
+         * Defaults to server timeout.
+         */
+        ival = atoi(val);
+        if (ival < 1)
+            return "Timeout must be at least one second";
+        worker->timeout = apr_time_from_sec(ival);
+        worker->timeout_set = 1;
+    }
+    else if (!strcasecmp(key, "iobuffersize")) {
+        long s = atol(val);
+        worker->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
+        worker->io_buffer_size_set = 1;
+    }
+    else if (!strcasecmp(key, "receivebuffersize")) {
+        ival = atoi(val);
+        if (ival < 512 && ival != 0) {
+            return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
+        }
+        worker->recv_buffer_size = ival;
+        worker->recv_buffer_size_set = 1;
+    }
+    else if (!strcasecmp(key, "keepalive")) {
+        if (!strcasecmp(val, "on"))
+            worker->keepalive = 1;
+        else if (!strcasecmp(val, "off"))
+            worker->keepalive = 0;
+        else
+            return "KeepAlive must be On|Off";
+        worker->keepalive_set = 1;
+    }    
+    else if (!strcasecmp(key, "route")) {
+        /* Worker route.
+         */
+        if (strlen(val) > PROXY_WORKER_MAX_ROUTE_SIZ)
+            return "Route length must be < 64 characters";
+        worker->route = apr_pstrdup(p, val);
+    }
+    else if (!strcasecmp(key, "redirect")) {
+        /* Worker redirection route.
+         */
+        if (strlen(val) > PROXY_WORKER_MAX_ROUTE_SIZ)
+            return "Redirect length must be < 64 characters";
+        worker->redirect = apr_pstrdup(p, val);
+    }
     else {
-        return "unknown parameter";
+        return "unknown Worker parameter";
     }
     return NULL;
 }
 
-static const char *set_balancer_param(struct proxy_balancer *balancer,
+static const char *set_balancer_param(apr_pool_t *p,
+                                      proxy_balancer *balancer,
                                       const char *key,
                                       const char *val)
 {
 
     int ival;
     if (!strcasecmp(key, "stickysession")) {
-        balancer->sticky = val;
+        /* Balancer sticky session name.
+         * Set to something like JSESSIONID or
+         * PHPSESSIONID, etc..,
+         */
+        balancer->sticky = apr_pstrdup(p, val);
     }
     else if (!strcasecmp(key, "nofailover")) {
+        /* If set to 'on' the session will break
+         * if the worker is in error state or
+         * disabled.
+         */
         if (!strcasecmp(val, "on"))
             balancer->sticky_force = 1;
         else if (!strcasecmp(val, "off"))
@@ -138,13 +232,37 @@ static const char *set_balancer_param(struct proxy_balancer *balancer,
             return "failover must be On|Off";
     }
     else if (!strcasecmp(key, "timeout")) {
+        /* Balancer timeout in seconds.
+         * If set this will be the maximum time to
+         * wait for a free worker.
+         * Default is not to wait.
+         */
         ival = atoi(val);
         if (ival < 1)
-            return "timeout must be al least one second";
+            return "timeout must be at least one second";
         balancer->timeout = apr_time_from_sec(ival);
     }
+    else if (!strcasecmp(key, "maxattempts")) {
+        /* Maximum number of failover attempts before
+         * giving up.
+         */
+        ival = atoi(val);
+        if (ival < 0)
+            return "maximum number of attempts must be a positive number";
+        balancer->max_attempts = ival;
+        balancer->max_attempts_set = 1;
+    }
+    else if (!strcasecmp(key, "lbmethod")) {
+        /* Which LB scheduler method */
+        if (!strcasecmp(val, "traffic"))
+            balancer->lbmethod = lbmethod_traffic;
+        else if (!strcasecmp(val, "requests"))
+            balancer->lbmethod = lbmethod_requests;
+        else
+            return "lbmethod must be Traffic|Requests";
+    }
     else {
-        return "unknown parameter";
+        return "unknown Balancer parameter";
     }
     return NULL;
 }
@@ -203,6 +321,13 @@ static int alias_match(const char *uri, const char *alias_fakename)
         urip = end_uri;
     }
 
+   /* We reach the end of the uri before the end of "alias_fakename"
+    * for example uri is "/" and alias_fakename "/examples"
+    */
+   if (urip == end_uri && aliasp!=end_fakename) {
+       return 0;
+   }
+
     /* Check last alias path component matched all the way */
     if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
         return 0;
@@ -241,7 +366,7 @@ static int proxy_detect(request_rec *r)
     if (conf->req && r->parsed_uri.scheme) {
         /* but it might be something vhosted */
         if (!(r->parsed_uri.hostname
-              && !strcasecmp(r->parsed_uri.scheme, ap_http_method(r))
+              && !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))
               && ap_matches_request_vhost(r, r->parsed_uri.hostname,
                                           (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port 
                                                        : ap_default_port(r))))) {
@@ -265,6 +390,9 @@ static int proxy_detect(request_rec *r)
         for (i = 0; i < conf->aliases->nelts; i++) {
             len = alias_match(r->unparsed_uri, ent[i].fake);
             if (len > 0) {
+                if ((ent[i].real[0] == '!') && (ent[i].real[1] == 0)) {
+                    return DECLINED;
+                }
                 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
                                           r->unparsed_uri + len, NULL);
                 r->handler = "proxy-server";
@@ -414,7 +542,7 @@ static int proxy_fixup(request_rec *r)
     if (p == NULL || p == url)
         return HTTP_BAD_REQUEST;
 
-    return OK;         /* otherwise; we've done the best we can */
+    return OK;      /* otherwise; we've done the best we can */
 }
 #endif
 /* Send a redirection if the request contains a hostname which is not */
@@ -435,7 +563,7 @@ static int proxy_needsdomain(request_rec *r, const char *url, const char *domain
     /* 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 */
+        return DECLINED;    /* host name has a dot already */
 
     ref = apr_table_get(r->headers_in, "Referer");
 
@@ -473,9 +601,9 @@ static int proxy_handler(request_rec *r)
     int direct_connect = 0;
     const char *str;
     long maxfwd;
-    struct proxy_balancer *balancer = NULL;
+    proxy_balancer *balancer = NULL;
     proxy_worker *worker = NULL;
-    proxy_module_conf *mconf;
+    int attempts = 0, max_attempts = 0;
 
     /* is this for us? */
     if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
@@ -519,116 +647,112 @@ static int proxy_handler(request_rec *r)
     apr_table_set(r->headers_in, "Max-Forwards", 
                   apr_psprintf(r->pool, "%ld", (maxfwd > 0) ? maxfwd : 0));
 
-    url = r->filename + 6;
-    p = strchr(url, ':');
-    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, url, conf->domain);
-        if (ap_is_HTTP_REDIRECT(rc))
-            return HTTP_MOVED_PERMANENTLY;
-    }
-
-    *p = '\0';
-    scheme = apr_pstrdup(r->pool, url);
-    *p = ':';
-
-    /* Check URI's destination host against NoProxy hosts */
-    /* Bypass ProxyRemote server lookup if configured as NoProxy */
-    /* we only know how to handle communication to a proxy via http */
-    /*if (strcasecmp(scheme, "http") == 0) */
-    {
-        int ii;
-        struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
+    do {
+        url = r->filename + 6;
+        p = strchr(url, ':');
+        if (p == NULL) {
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                          "proxy_handler no URL in %s", r->filename);
+            return HTTP_BAD_REQUEST;
+        }
 
-        for (direct_connect = ii = 0; ii < conf->dirconn->nelts && !direct_connect; ii++) {
-            direct_connect = list[ii].matcher(&list[ii], r);
+        /* If the host doesn't have a domain name, add one and redirect. */
+        if (conf->domain != NULL) {
+            rc = proxy_needsdomain(r, url, conf->domain);
+            if (ap_is_HTTP_REDIRECT(rc))
+                return HTTP_MOVED_PERMANENTLY;
         }
+
+        *p = '\0';
+        scheme = apr_pstrdup(r->pool, url);
+        *p = ':';
+
+        /* Check URI's destination host against NoProxy hosts */
+        /* Bypass ProxyRemote server lookup if configured as NoProxy */
+        /* we only know how to handle communication to a proxy via http */
+        /*if (strcasecmp(scheme, "http") == 0) */
+        {
+            int ii;
+            struct dirconn_entry *list = (struct dirconn_entry *)
+                                                conf->dirconn->elts;
+
+            for (direct_connect = ii = 0; ii < conf->dirconn->nelts &&
+                                               !direct_connect; ii++) {
+                direct_connect = list[ii].matcher(&list[ii], r);
+            }
 #if DEBUGGING
-        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
                       (direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
                       r->uri);
 #endif
-    }
-    
-    /* Try to obtain the most suitable worker */
-    access_status = ap_proxy_pre_request(&worker, &balancer, r, conf, &url);
-    if (access_status != OK)
-        return access_status;
-    
-    /* only use stored info for top-level pages. Sub requests don't share 
-     * in keepalives
-     */
-    if (!r->main) {
-        mconf = (proxy_module_conf *)ap_get_module_config(r->connection->conn_config,
-                                                          &proxy_module);
-    }
-    /* create space for state information */
-    if (!mconf) {
-        mconf = apr_pcalloc(r->connection->pool, sizeof(proxy_module_conf));
-        if (!r->main) {
-            ap_set_module_config(r->connection->conn_config,
-                                 &proxy_module, mconf);
         }
-    }
-    /* use the current balancer and worker. 
-     * the proxy_conn will be set in particular scheme handler
-     * if not already set.
-     */ 
-    mconf->balancer = balancer;
-    mconf->worker   = worker;
-    mconf->url      = url;
     
-    /* 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)) ||
-                (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, conf, url, ents[i].hostname, ents[i].port);
-
-                /* an error or success */
-                if (access_status != DECLINED && access_status != HTTP_BAD_GATEWAY) {
-                    return access_status;
+        /* Try to obtain the most suitable worker */
+        access_status = ap_proxy_pre_request(&worker, &balancer, r, conf, &url);
+        if (access_status != OK)
+            return access_status;
+        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)) ||
+                    (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 */
                 }
-                /* 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??
-     */
+        /* 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);
+        if ((access_status = proxy_run_scheme_handler(r, worker, conf,
+                                                      url, NULL, 0)) == OK)
+            break;
+        
+    } while (!PROXY_WORKER_IS_USABLE(worker) && 
+             max_attempts > attempts++);
 
-    /* handle the scheme */
-    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
-                 "Trying to run scheme_handler");
-    access_status = proxy_run_scheme_handler(r, conf, url, NULL, 0);
     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);
-        return HTTP_FORBIDDEN;
+        access_status = HTTP_FORBIDDEN;
+        goto cleanup;
     }
+cleanup:
     if (balancer) {
-        access_status = proxy_run_post_request(worker, balancer, r, conf);
-        if (access_status == DECLINED) {
-            access_status = OK; /* no post_request handler available */
+        int post_status = proxy_run_post_request(worker, balancer, r, conf);
+        if (post_status == DECLINED) {
+            post_status = OK; /* no post_request handler available */
             /* TODO: reclycle direct worker */
         }
     }
@@ -654,7 +778,9 @@ static void * create_proxy_config(apr_pool_t *p, server_rec *s)
     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(struct proxy_balancer));
+    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 */
@@ -674,6 +800,7 @@ static void * create_proxy_config(apr_pool_t *p, server_rec *s)
     ps->timeout_set = 0;
     ps->badopt = bad_error;
     ps->badopt_set = 0;
+    ps->pool = p;
     return ps;
 }
 
@@ -698,6 +825,8 @@ static void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
     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;
@@ -709,7 +838,8 @@ static void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
     ps->preserve_host = (overrides->preserve_host_set == 0) ? base->preserve_host : overrides->preserve_host;
     ps->timeout= (overrides->timeout_set == 0) ? base->timeout : overrides->timeout;
     ps->badopt = (overrides->badopt_set == 0) ? base->badopt : overrides->badopt;
-
+    ps->proxy_status = (overrides->proxy_status_set == 0) ? base->proxy_status : overrides->proxy_status;
+    ps->pool = p;
     return ps;
 }
 
@@ -780,8 +910,8 @@ static const char *
     }
     else
         if (strchr(f, ':') == NULL)
-            ap_str_tolower(f);         /* lowercase scheme */
-    ap_str_tolower(p + 3);             /* lowercase hostname */
+            ap_str_tolower(f);      /* lowercase scheme */
+    ap_str_tolower(p + 3);      /* lowercase hostname */
 
     if (port == -1) {
         port = apr_uri_port_of_scheme(scheme);
@@ -834,7 +964,8 @@ static const char *
             char *val = strchr(word, '=');
             if (!val) {
                 if (cmd->path)
-                    return "Invalid ProxyPass parameter. Paramet must be in the form key=value";
+                    return "Invalid ProxyPass parameter.  Parameter must be "
+                           "in the form 'key=value'";
                 else
                     return "ProxyPass can not have a path when defined in a location"; 
             }
@@ -850,38 +981,42 @@ static const char *
     new = apr_array_push(conf->aliases);
     new->fake = apr_pstrdup(cmd->pool, f);
     new->real = apr_pstrdup(cmd->pool, r);
+    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 woker */
     if (strncasecmp(r, "balancer:", 9) == 0) {
-        struct proxy_balancer *balancer = ap_proxy_get_balancer(cmd->pool, conf, r);
+        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, "BalancerMember: ", err, NULL);
+                return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
         }        
         for (i = 0; i < arr->nelts; i++) {
-            const char *err = set_balancer_param(balancer, elts[i].key, elts[i].val);
+            const char *err = set_balancer_param(cmd->pool, balancer, elts[i].key,
+                                                 elts[i].val);
             if (err)
-                return apr_pstrcat(cmd->temp_pool, "ProxyPass: ", err, NULL);
+                return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
         }
     }
     else {
-        proxy_worker *worker = ap_proxy_get_worker(cmd->pool, conf, r);
+        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);
+                return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
         }
-        if (conf->timeout_set)
-            worker->timeout = conf->timeout;
+        PROXY_COPY_CONF_PARAMS(worker, conf);
+
         for (i = 0; i < arr->nelts; i++) {
-            const char *err = set_worker_param(worker, elts[i].key, elts[i].val);
+            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 apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
         }
     }
     return NULL;
@@ -1020,19 +1155,19 @@ static const char *
         New->name = apr_pstrdup(parms->pool, arg);
         New->hostaddr = NULL;
 
-       if (ap_proxy_is_ipaddr(New, parms->pool)) {
+    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));
+        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);
+    }
+    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);
+        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+                     "Parsed domain %s", New->name);
 #endif
         }
         else if (ap_proxy_is_hostname(New, parms->pool)) {
@@ -1073,8 +1208,16 @@ static const char *
 
     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)
 {
@@ -1198,12 +1341,33 @@ static const char*
     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 | block";
+    }
+
+    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);
-    struct proxy_balancer *balancer;
+    proxy_balancer *balancer;
     proxy_worker *worker;
     char *path = cmd->path;
     char *name = NULL;
@@ -1227,7 +1391,8 @@ static const char *add_member(cmd_parms *cmd, void *dummy, const char *arg)
                 if (cmd->path)
                     return "BalancerMember can not have a balancer name when defined in a location";
                 else
-                    return "Invalid BalancerMember parameter. Paramet must be in the form key=value";
+                    return "Invalid BalancerMember parameter. Parameter must "
+                           "be in the form 'key=value'";
             else
                 *val++ = '\0';
             apr_table_setn(params, word, val);
@@ -1238,78 +1403,110 @@ static const char *add_member(cmd_parms *cmd, void *dummy, const char *arg)
     if (!name)
         return "BalancerMember must define remote proxy server";
     
-    ap_str_tolower(path);      /* lowercase scheme://hostname */
-    ap_str_tolower(name);      /* lowercase scheme://hostname */
+    ap_str_tolower(path);   /* lowercase scheme://hostname */
+    ap_str_tolower(name);   /* 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); 
+            return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL); 
     }
-    if (conf->timeout_set)
-        worker->timeout = conf->timeout;
-
+    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(worker, elts[i].key, elts[i].val);
+        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);
+            return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
     }
     /* Try to find the balancer */
-    balancer = ap_proxy_get_balancer(cmd->temp_pool, conf, name); 
+    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);
+            return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
     }
     /* Add the worker to the load balancer */
-    ap_proxy_add_worker_to_balancer(balancer, worker);
-
+    ap_proxy_add_worker_to_balancer(cmd->pool, balancer, worker);
     return NULL;
 }
 
 static const char *
-    set_sticky_session(cmd_parms *cmd, void *dummy, const char *f, const char *r)
+    set_proxy_param(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);
-    struct proxy_balancer *balancer;
-    const char *name, *sticky;
-
-    if (r != NULL && cmd->path == NULL ) {
-        name = f;
-        sticky = r;
-    } else if (r == NULL && cmd->path != NULL) {
-        name = cmd->path;
-        sticky = f;
-    } else {
-        if (r == NULL)
-            return "BalancerStickySession needs a path when not defined in a location";
-        else 
-            return "BalancerStickySession can not have a path when defined in a location";
+    (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;
+
+    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';
     }
-    /* Try to find the balancer */
-    balancer = ap_proxy_get_balancer(cmd->temp_pool, conf, name);
-    if (!balancer)
-        return apr_pstrcat(cmd->temp_pool, "BalancerStickySession: can not find a load balancer '",
-                           name, "'", NULL);
-    if (!strcasecmp(sticky, "nofailover"))
-        balancer->sticky_force = 1;   
-    else
-        balancer->sticky = sticky;
+    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) {
+            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) {
+            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(cmd->pool, balancer, word, val);
+
+        if (err)
+            return apr_pstrcat(cmd->temp_pool, "ProxySet ", err, " ", word, " ", 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);
+                                                    &proxy_module);
     void **new_space = (void **)apr_array_push(sconf->sec_proxy);
     
     *new_space = dir_config;
@@ -1448,8 +1645,10 @@ static const command_rec proxy_cmds[] =
      "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_TAKE12("BalancerStickySession", set_sticky_session, NULL, RSRC_CONF|ACCESS_CONF,
-     "A balancer and sticky session name"),
+    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}
 };
 
@@ -1481,12 +1680,155 @@ PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c)
 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);
 
     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);                
+        ap_rvputs(r, "<td>", balancer->sticky, NULL);
+        ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
+                   apr_time_sec(balancer->timeout));
+        ap_rprintf(r, "<td>%s</td>\n",
+                   balancer->lbmethod == lbmethod_requests ? "Requests" :
+                   balancer->lbmethod == lbmethod_traffic ? "Traffic" :
+                   "Unknown");
+        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>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><td>", (int)(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
@@ -1495,7 +1837,13 @@ static void register_hooks(apr_pool_t *p)
 #ifndef FIX_15207
     static const char * const aszSucc[]={ "mod_rewrite.c", NULL };
 #endif
-
+    /* 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 */
@@ -1508,8 +1856,13 @@ static void register_hooks(apr_pool_t *p)
 #endif
     /* 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 =
@@ -1517,40 +1870,41 @@ 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 */
+    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(scheme_handler)
+    APR_HOOK_LINK(canon_handler)
+    APR_HOOK_LINK(pre_request)
+    APR_HOOK_LINK(post_request)
 )
 
 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, scheme_handler, 
-                                     (request_rec *r, proxy_server_conf *conf, 
-                                     char *url, const char *proxyhost, 
-                                     apr_port_t proxyport),(r,conf,url,
-                                     proxyhost,proxyport),DECLINED)
+                                     (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)
+                                      (request_rec *r, char *url),(r,
+                                      url),DECLINED)
 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, pre_request, (
                                       proxy_worker **worker,
-                                      struct proxy_balancer **balancer,
+                                      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,
-                                       struct proxy_balancer *balancer,
+                                       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)
+                                    (request_rec *r), (r),
+                                    OK, DECLINED)