]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy_balancer.c
Add in BalancerNonce directive... useful for shared-secrets.
[apache] / modules / proxy / mod_proxy_balancer.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* Load balancer module for Apache proxy */
18
19 #include "mod_proxy.h"
20 #include "scoreboard.h"
21 #include "ap_mpm.h"
22 #include "apr_version.h"
23 #include "apr_hooks.h"
24 #include "apr_uuid.h"
25
26 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
27
28 static char balancer_nonce[APR_UUID_FORMATTED_LENGTH + 1];
29
30 static int proxy_balancer_canon(request_rec *r, char *url)
31 {
32     char *host, *path;
33     char *search = NULL;
34     const char *err;
35     apr_port_t port = 0;
36
37     if (strncasecmp(url, "balancer:", 9) == 0) {
38         url += 9;
39     }
40     else {
41         return DECLINED;
42     }
43
44     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
45              "proxy: BALANCER: canonicalising URL %s", url);
46
47     /* do syntatic check.
48      * We break the URL into host, port, path, search
49      */
50     err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
51     if (err) {
52         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
53                       "error parsing URL %s: %s",
54                       url, err);
55         return HTTP_BAD_REQUEST;
56     }
57     /*
58      * now parse path/search args, according to rfc1738:
59      * process the path. With proxy-noncanon set (by
60      * mod_proxy) we use the raw, unparsed uri
61      */
62     if (apr_table_get(r->notes, "proxy-nocanon")) {
63         path = url;   /* this is the raw path */
64     }
65     else {
66         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
67                                  r->proxyreq);
68         search = r->args;
69     }
70     if (path == NULL)
71         return HTTP_BAD_REQUEST;
72
73     r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
74             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
75
76     r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
77
78     return OK;
79 }
80
81 static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
82                                  proxy_balancer *balancer)
83 {
84     int i;
85     proxy_worker **workers;
86
87     workers = (proxy_worker **)balancer->workers->elts;
88
89     for (i = 0; i < balancer->workers->nelts; i++) {
90         int worker_is_initialized;
91         worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(*workers);
92         if (!worker_is_initialized) {
93             proxy_worker_stat *slot;
94             /*
95              * If the worker is not initialized check whether its scoreboard
96              * slot is already initialized.
97              */
98             slot = (proxy_worker_stat *) ap_get_scoreboard_lb((*workers)->id);
99             if (slot) {
100                 worker_is_initialized = slot->status & PROXY_WORKER_INITIALIZED;
101             }
102             else {
103                 worker_is_initialized = 0;
104             }
105         }
106         ap_proxy_initialize_worker_share(conf, *workers, s);
107         ap_proxy_initialize_worker(*workers, s, conf->pool);
108         if (!worker_is_initialized) {
109             /* Set to the original configuration */
110             (*workers)->s->lbstatus = (*workers)->s->lbfactor =
111             ((*workers)->lbfactor ? (*workers)->lbfactor : 1);
112             (*workers)->s->lbset = (*workers)->lbset;
113         }
114         ++workers;
115     }
116
117     /* Set default number of attempts to the number of
118      * workers.
119      */
120     if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
121         balancer->max_attempts = balancer->workers->nelts - 1;
122         balancer->max_attempts_set = 1;
123     }
124     return 0;
125 }
126
127 /* Retrieve the parameter with the given name
128  * Something like 'JSESSIONID=12345...N'
129  */
130 static char *get_path_param(apr_pool_t *pool, char *url,
131                             const char *name, int scolon_sep)
132 {
133     char *path = NULL;
134     char *pathdelims = "?&";
135
136     if (scolon_sep) {
137         pathdelims = ";?&";
138     }
139     for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
140         path += strlen(name);
141         if (*path == '=') {
142             /*
143              * Session path was found, get it's value
144              */
145             ++path;
146             if (strlen(path)) {
147                 char *q;
148                 path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
149                 return path;
150             }
151         }
152     }
153     return NULL;
154 }
155
156 static char *get_cookie_param(request_rec *r, const char *name)
157 {
158     const char *cookies;
159     const char *start_cookie;
160
161     if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
162         for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
163              start_cookie = ap_strstr_c(start_cookie + 1, name)) {
164             if (start_cookie == cookies ||
165                 start_cookie[-1] == ';' ||
166                 start_cookie[-1] == ',' ||
167                 isspace(start_cookie[-1])) {
168
169                 start_cookie += strlen(name);
170                 while(*start_cookie && isspace(*start_cookie))
171                     ++start_cookie;
172                 if (*start_cookie == '=' && start_cookie[1]) {
173                     /*
174                      * Session cookie was found, get it's value
175                      */
176                     char *end_cookie, *cookie;
177                     ++start_cookie;
178                     cookie = apr_pstrdup(r->pool, start_cookie);
179                     if ((end_cookie = strchr(cookie, ';')) != NULL)
180                         *end_cookie = '\0';
181                     if((end_cookie = strchr(cookie, ',')) != NULL)
182                         *end_cookie = '\0';
183                     return cookie;
184                 }
185             }
186         }
187     }
188     return NULL;
189 }
190
191 /* Find the worker that has the 'route' defined
192  */
193 static proxy_worker *find_route_worker(proxy_balancer *balancer,
194                                        const char *route, request_rec *r)
195 {
196     int i;
197     int checking_standby;
198     int checked_standby;
199     
200     proxy_worker **workers;
201     proxy_worker *worker;
202
203     checking_standby = checked_standby = 0;
204     while (!checked_standby) {
205         workers = (proxy_worker **)balancer->workers->elts;
206         for (i = 0; i < balancer->workers->nelts; i++, workers++) {
207             worker = *workers;
208             if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
209                 continue;
210             if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
211                 if (worker && PROXY_WORKER_IS_USABLE(worker)) {
212                     return worker;
213                 } else {
214                     /*
215                      * If the worker is in error state run
216                      * retry on that worker. It will be marked as
217                      * operational if the retry timeout is elapsed.
218                      * The worker might still be unusable, but we try
219                      * anyway.
220                      */
221                     ap_proxy_retry_worker("BALANCER", worker, r->server);
222                     if (PROXY_WORKER_IS_USABLE(worker)) {
223                             return worker;
224                     } else {
225                         /*
226                          * We have a worker that is unusable.
227                          * It can be in error or disabled, but in case
228                          * it has a redirection set use that redirection worker.
229                          * This enables to safely remove the member from the
230                          * balancer. Of course you will need some kind of
231                          * session replication between those two remote.
232                          */
233                         if (*worker->s->redirect) {
234                             proxy_worker *rworker = NULL;
235                             rworker = find_route_worker(balancer, worker->s->redirect, r);
236                             /* Check if the redirect worker is usable */
237                             if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
238                                 /*
239                                  * If the worker is in error state run
240                                  * retry on that worker. It will be marked as
241                                  * operational if the retry timeout is elapsed.
242                                  * The worker might still be unusable, but we try
243                                  * anyway.
244                                  */
245                                 ap_proxy_retry_worker("BALANCER", rworker, r->server);
246                             }
247                             if (rworker && PROXY_WORKER_IS_USABLE(rworker))
248                                 return rworker;
249                         }
250                     }
251                 }
252             }
253         }
254         checked_standby = checking_standby++;
255     }
256     return NULL;
257 }
258
259 static proxy_worker *find_session_route(proxy_balancer *balancer,
260                                         request_rec *r,
261                                         char **route,
262                                         const char **sticky_used,
263                                         char **url)
264 {
265     proxy_worker *worker = NULL;
266
267     if (!balancer->sticky)
268         return NULL;
269     /* Try to find the sticky route inside url */
270     *route = get_path_param(r->pool, *url, balancer->sticky_path, balancer->scolonsep);
271     if (*route) {
272         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
273                      "proxy: BALANCER: Found value %s for "
274                      "stickysession %s", *route, balancer->sticky_path);
275         *sticky_used =  balancer->sticky_path;
276     }
277     else {
278         *route = get_cookie_param(r, balancer->sticky);
279         if (*route) {
280             *sticky_used =  balancer->sticky;
281             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
282                          "proxy: BALANCER: Found value %s for "
283                          "stickysession %s", *route, balancer->sticky);
284         }
285     }
286     /*
287      * If we found a value for sticksession, find the first '.' within.
288      * Everything after '.' (if present) is our route.
289      */
290     if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
291         (*route)++;
292     if ((*route) && (**route)) {
293         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
294                                   "proxy: BALANCER: Found route %s", *route);
295         /* We have a route in path or in cookie
296          * Find the worker that has this route defined.
297          */
298         worker = find_route_worker(balancer, *route, r);
299         if (worker && strcmp(*route, worker->s->route)) {
300             /*
301              * Notice that the route of the worker chosen is different from
302              * the route supplied by the client.
303              */
304             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
305             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
306                          "proxy: BALANCER: Route changed from %s to %s",
307                          *route, worker->s->route);
308         }
309         return worker;
310     }
311     else
312         return NULL;
313 }
314
315 static proxy_worker *find_best_worker(proxy_balancer *balancer,
316                                       request_rec *r)
317 {
318     proxy_worker *candidate = NULL;
319     apr_status_t rv;
320
321     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
322         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
323         "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
324         return NULL;
325     }
326
327     candidate = (*balancer->lbmethod->finder)(balancer, r);
328
329     if (candidate)
330         candidate->s->elected++;
331
332 /*
333         PROXY_THREAD_UNLOCK(balancer);
334         return NULL;
335 */
336
337     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
338         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
339         "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
340     }
341
342     if (candidate == NULL) {
343         /* All the workers are in error state or disabled.
344          * If the balancer has a timeout sleep for a while
345          * and try again to find the worker. The chances are
346          * that some other thread will release a connection.
347          * By default the timeout is not set, and the server
348          * returns SERVER_BUSY.
349          */
350 #if APR_HAS_THREADS
351         if (balancer->timeout) {
352             /* XXX: This can perhaps be build using some
353              * smarter mechanism, like tread_cond.
354              * But since the statuses can came from
355              * different childs, use the provided algo.
356              */
357             apr_interval_time_t timeout = balancer->timeout;
358             apr_interval_time_t step, tval = 0;
359             /* Set the timeout to 0 so that we don't
360              * end in infinite loop
361              */
362             balancer->timeout = 0;
363             step = timeout / 100;
364             while (tval < timeout) {
365                 apr_sleep(step);
366                 /* Try again */
367                 if ((candidate = find_best_worker(balancer, r)))
368                     break;
369                 tval += step;
370             }
371             /* restore the timeout */
372             balancer->timeout = timeout;
373         }
374 #endif
375     }
376
377     return candidate;
378
379 }
380
381 static int rewrite_url(request_rec *r, proxy_worker *worker,
382                         char **url)
383 {
384     const char *scheme = strstr(*url, "://");
385     const char *path = NULL;
386
387     if (scheme)
388         path = ap_strchr_c(scheme + 3, '/');
389
390     /* we break the URL into host, port, uri */
391     if (!worker) {
392         return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
393                              "missing worker. URI cannot be parsed: ", *url,
394                              NULL));
395     }
396
397     *url = apr_pstrcat(r->pool, worker->name, path, NULL);
398
399     return OK;
400 }
401
402 static void force_recovery(proxy_balancer *balancer, server_rec *s)
403 {
404     int i;
405     int ok = 0;
406     proxy_worker **worker;
407
408     worker = (proxy_worker **)balancer->workers->elts;
409     for (i = 0; i < balancer->workers->nelts; i++, worker++) {
410         if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
411             ok = 1;
412             break;
413         }
414         else {
415             /* Try if we can recover */
416             ap_proxy_retry_worker("BALANCER", *worker, s);
417             if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
418                 ok = 1;
419                 break;
420             }
421         }
422     }
423     if (!ok) {
424         /* If all workers are in error state force the recovery.
425          */
426         worker = (proxy_worker **)balancer->workers->elts;
427         for (i = 0; i < balancer->workers->nelts; i++, worker++) {
428             ++(*worker)->s->retries;
429             (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
430             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
431                          "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
432                          balancer->name, (*worker)->hostname);
433         }
434     }
435 }
436
437 static int proxy_balancer_pre_request(proxy_worker **worker,
438                                       proxy_balancer **balancer,
439                                       request_rec *r,
440                                       proxy_server_conf *conf, char **url)
441 {
442     int access_status;
443     proxy_worker *runtime;
444     char *route = NULL;
445     const char *sticky = NULL;
446     apr_status_t rv;
447
448     *worker = NULL;
449     /* Step 1: check if the url is for us
450      * The url we can handle starts with 'balancer://'
451      * If balancer is already provided skip the search
452      * for balancer, because this is failover attempt.
453      */
454     if (!*balancer &&
455         !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
456         return DECLINED;
457
458     /* Step 2: Lock the LoadBalancer
459      * XXX: perhaps we need the process lock here
460      */
461     if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
462         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
463                      "proxy: BALANCER: (%s). Lock failed for pre_request",
464                      (*balancer)->name);
465         return DECLINED;
466     }
467
468     /* Step 3: force recovery */
469     force_recovery(*balancer, r->server);
470
471     /* Step 4: find the session route */
472     runtime = find_session_route(*balancer, r, &route, &sticky, url);
473     if (runtime) {
474         int i, total_factor = 0;
475         proxy_worker **workers;
476         /* We have a sticky load balancer
477          * Update the workers status
478          * so that even session routes get
479          * into account.
480          */
481         workers = (proxy_worker **)(*balancer)->workers->elts;
482         for (i = 0; i < (*balancer)->workers->nelts; i++) {
483             /* Take into calculation only the workers that are
484              * not in error state or not disabled.
485              *
486              * TODO: Abstract the below, since this is dependent
487              *       on the LB implementation
488              */
489             if (PROXY_WORKER_IS_USABLE(*workers)) {
490                 (*workers)->s->lbstatus += (*workers)->s->lbfactor;
491                 total_factor += (*workers)->s->lbfactor;
492             }
493             workers++;
494         }
495         runtime->s->lbstatus -= total_factor;
496         runtime->s->elected++;
497
498         *worker = runtime;
499     }
500     else if (route && (*balancer)->sticky_force) {
501         int i, member_of = 0;
502         proxy_worker **workers;
503         /*
504          * We have a route provided that doesn't match the
505          * balancer name. See if the provider route is the
506          * member of the same balancer in which case return 503
507          */
508         workers = (proxy_worker **)(*balancer)->workers->elts;
509         for (i = 0; i < (*balancer)->workers->nelts; i++) {
510             if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
511                 member_of = 1;
512                 break;
513             }
514             workers++;
515         }
516         if (member_of) {
517             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
518                          "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
519                          (*balancer)->name, route);
520             if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
521                 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
522                              "proxy: BALANCER: (%s). Unlock failed for pre_request",
523                              (*balancer)->name);
524             }
525             return HTTP_SERVICE_UNAVAILABLE;
526         }
527     }
528
529     if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
530         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
531                      "proxy: BALANCER: (%s). Unlock failed for pre_request",
532                      (*balancer)->name);
533     }
534     if (!*worker) {
535         runtime = find_best_worker(*balancer, r);
536         if (!runtime) {
537             if ((*balancer)->workers->nelts) {
538                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
539                             "proxy: BALANCER: (%s). All workers are in error state",
540                             (*balancer)->name);
541             } else {
542                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
543                             "proxy: BALANCER: (%s). No workers in balancer",
544                             (*balancer)->name);
545             }
546
547             return HTTP_SERVICE_UNAVAILABLE;
548         }
549         if ((*balancer)->sticky && runtime) {
550             /*
551              * This balancer has sticky sessions and the client either has not
552              * supplied any routing information or all workers for this route
553              * including possible redirect and hotstandby workers are in error
554              * state, but we have found another working worker for this
555              * balancer where we can send the request. Thus notice that we have
556              * changed the route to the backend.
557              */
558             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
559         }
560         *worker = runtime;
561     }
562
563     (*worker)->s->busy++;
564
565     /* Add balancer/worker info to env. */
566     apr_table_setn(r->subprocess_env,
567                    "BALANCER_NAME", (*balancer)->name);
568     apr_table_setn(r->subprocess_env,
569                    "BALANCER_WORKER_NAME", (*worker)->name);
570     apr_table_setn(r->subprocess_env,
571                    "BALANCER_WORKER_ROUTE", (*worker)->s->route);
572
573     /* Rewrite the url from 'balancer://url'
574      * to the 'worker_scheme://worker_hostname[:worker_port]/url'
575      * This replaces the balancers fictional name with the
576      * real hostname of the elected worker.
577      */
578     access_status = rewrite_url(r, *worker, url);
579     /* Add the session route to request notes if present */
580     if (route) {
581         apr_table_setn(r->notes, "session-sticky", sticky);
582         apr_table_setn(r->notes, "session-route", route);
583
584         /* Add session info to env. */
585         apr_table_setn(r->subprocess_env,
586                        "BALANCER_SESSION_STICKY", sticky);
587         apr_table_setn(r->subprocess_env,
588                        "BALANCER_SESSION_ROUTE", route);
589     }
590     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
591                  "proxy: BALANCER (%s) worker (%s) rewritten to %s",
592                  (*balancer)->name, (*worker)->name, *url);
593
594     return access_status;
595 }
596
597 static int proxy_balancer_post_request(proxy_worker *worker,
598                                        proxy_balancer *balancer,
599                                        request_rec *r,
600                                        proxy_server_conf *conf)
601 {
602
603     apr_status_t rv;
604
605     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
606         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
607             "proxy: BALANCER: (%s). Lock failed for post_request",
608             balancer->name);
609         return HTTP_INTERNAL_SERVER_ERROR;
610     }
611
612     if (!apr_is_empty_array(balancer->errstatuses)) {
613         int i;
614         for (i = 0; i < balancer->errstatuses->nelts; i++) {
615             int val = ((int *)balancer->errstatuses->elts)[i];
616             if (r->status == val) {
617                 ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, r->server,
618                     "Detected ErrorOnState (%d) for member (%s). Forcing worker into error state.", val, worker->name);
619                 worker->s->status |= PROXY_WORKER_IN_ERROR;
620                 worker->s->error_time = apr_time_now();
621                 break;
622             }
623         }
624     }
625
626     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
627         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
628             "proxy: BALANCER: (%s). Unlock failed for post_request",
629             balancer->name);
630     }
631     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
632                  "proxy_balancer_post_request for (%s)", balancer->name);
633
634     if (worker && worker->s->busy)
635         worker->s->busy--;
636
637     return OK;
638
639 }
640
641 static void recalc_factors(proxy_balancer *balancer)
642 {
643     int i;
644     proxy_worker **workers;
645
646
647     /* Recalculate lbfactors */
648     workers = (proxy_worker **)balancer->workers->elts;
649     /* Special case if there is only one worker it's
650      * load factor will always be 1
651      */
652     if (balancer->workers->nelts == 1) {
653         (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
654         return;
655     }
656     for (i = 0; i < balancer->workers->nelts; i++) {
657         /* Update the status entries */
658         workers[i]->s->lbstatus = workers[i]->s->lbfactor;
659     }
660 }
661
662 /* pre_config hook: */
663 static int balancer_init(apr_pool_t *pconf, apr_pool_t *plog,
664                          apr_pool_t *ptemp)
665 {
666     apr_uuid_t uuid;
667
668     /* Retrieve a UUID and store the nonce for the lifetime of
669      * the process. */
670     apr_uuid_get(&uuid);
671     apr_uuid_format(balancer_nonce, &uuid);
672
673     return OK;
674 }
675
676 /* Manages the loadfactors and member status
677  */
678 static int balancer_handler(request_rec *r)
679 {
680     void *sconf = r->server->module_config;
681     proxy_server_conf *conf = (proxy_server_conf *)
682         ap_get_module_config(sconf, &proxy_module);
683     proxy_balancer *balancer, *bsel = NULL;
684     proxy_worker *worker, *wsel = NULL;
685     proxy_worker **workers = NULL;
686     apr_table_t *params = apr_table_make(r->pool, 10);
687     int access_status;
688     int i, n;
689     const char *name;
690
691     /* is this for us? */
692     if (strcmp(r->handler, "balancer-manager"))
693         return DECLINED;
694     r->allowed = (AP_METHOD_BIT << M_GET);
695     if (r->method_number != M_GET)
696         return DECLINED;
697
698     if (r->args) {
699         char *args = apr_pstrdup(r->pool, r->args);
700         char *tok, *val;
701         while (args && *args) {
702             if ((val = ap_strchr(args, '='))) {
703                 *val++ = '\0';
704                 if ((tok = ap_strchr(val, '&')))
705                     *tok++ = '\0';
706                 /*
707                  * Special case: workers are allowed path information
708                  */
709                 if ((access_status = ap_unescape_url(val)) != OK)
710                     if (strcmp(args, "w") || (access_status !=  HTTP_NOT_FOUND))
711                         return access_status;
712                 apr_table_setn(params, args, val);
713                 args = tok;
714             }
715             else
716                 return HTTP_BAD_REQUEST;
717         }
718     }
719     
720     /* Check that the supplied nonce matches this server's nonce;
721      * otherwise ignore all parameters, to prevent a CSRF attack. */
722     if (*balancer_nonce &&
723         ((name = apr_table_get(params, "nonce")) == NULL 
724         || strcmp(balancer_nonce, name) != 0)) {
725         apr_table_clear(params);
726     }
727
728     if ((name = apr_table_get(params, "b")))
729         bsel = ap_proxy_get_balancer(r->pool, conf,
730             apr_pstrcat(r->pool, "balancer://", name, NULL));
731     if ((name = apr_table_get(params, "w"))) {
732         proxy_worker *ws;
733
734         ws = ap_proxy_get_worker(r->pool, conf, name);
735         if (bsel && ws) {
736             workers = (proxy_worker **)bsel->workers->elts;
737             for (n = 0; n < bsel->workers->nelts; n++) {
738                 worker = *workers;
739                 if (strcasecmp(worker->name, ws->name) == 0) {
740                     wsel = worker;
741                     break;
742                 }
743                 ++workers;
744             }
745         }
746     }
747     /* First set the params */
748     /*
749      * Note that it is not possible set the proxy_balancer because it is not
750      * in shared memory.
751      */
752     if (wsel) {
753         const char *val;
754         if ((val = apr_table_get(params, "lf"))) {
755             int ival = atoi(val);
756             if (ival >= 1 && ival <= 100) {
757                 wsel->s->lbfactor = ival;
758                 if (bsel)
759                     recalc_factors(bsel);
760             }
761         }
762         if ((val = apr_table_get(params, "wr"))) {
763             if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
764                 strcpy(wsel->s->route, val);
765             else
766                 *wsel->s->route = '\0';
767         }
768         if ((val = apr_table_get(params, "rr"))) {
769             if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
770                 strcpy(wsel->s->redirect, val);
771             else
772                 *wsel->s->redirect = '\0';
773         }
774         if ((val = apr_table_get(params, "dw"))) {
775             if (!strcasecmp(val, "Disable"))
776                 wsel->s->status |= PROXY_WORKER_DISABLED;
777             else if (!strcasecmp(val, "Enable"))
778                 wsel->s->status &= ~PROXY_WORKER_DISABLED;
779         }
780         if ((val = apr_table_get(params, "ls"))) {
781             int ival = atoi(val);
782             if (ival >= 0 && ival <= 99) {
783                 wsel->s->lbset = ival;
784              }
785         }
786
787     }
788     if (apr_table_get(params, "xml")) {
789         ap_set_content_type(r, "text/xml");
790         ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
791         ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
792         ap_rputs("  <httpd:balancers>\n", r);
793         balancer = (proxy_balancer *)conf->balancers->elts;
794         for (i = 0; i < conf->balancers->nelts; i++) {
795             ap_rputs("    <httpd:balancer>\n", r);
796             ap_rvputs(r, "      <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
797             ap_rputs("      <httpd:workers>\n", r);
798             workers = (proxy_worker **)balancer->workers->elts;
799             for (n = 0; n < balancer->workers->nelts; n++) {
800                 worker = *workers;
801                 ap_rputs("        <httpd:worker>\n", r);
802                 ap_rvputs(r, "          <httpd:scheme>", worker->scheme,
803                           "</httpd:scheme>\n", NULL);
804                 ap_rvputs(r, "          <httpd:hostname>", worker->hostname,
805                           "</httpd:hostname>\n", NULL);
806                ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
807                           worker->s->lbfactor);
808                 ap_rputs("        </httpd:worker>\n", r);
809                 ++workers;
810             }
811             ap_rputs("      </httpd:workers>\n", r);
812             ap_rputs("    </httpd:balancer>\n", r);
813             ++balancer;
814         }
815         ap_rputs("  </httpd:balancers>\n", r);
816         ap_rputs("</httpd:manager>", r);
817     }
818     else {
819         ap_set_content_type(r, "text/html; charset=ISO-8859-1");
820         ap_rputs(DOCTYPE_HTML_3_2
821                  "<html><head><title>Balancer Manager</title></head>\n", r);
822         ap_rputs("<body><h1>Load Balancer Manager for ", r);
823         ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
824         ap_rvputs(r, "<dl><dt>Server Version: ",
825                   ap_get_server_description(), "</dt>\n", NULL);
826         ap_rvputs(r, "<dt>Server Built: ",
827                   ap_get_server_built(), "\n</dt></dl>\n", NULL);
828         balancer = (proxy_balancer *)conf->balancers->elts;
829         for (i = 0; i < conf->balancers->nelts; i++) {
830
831             ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
832             ap_rvputs(r, balancer->name, "</h3>\n\n", NULL);
833             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
834                 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
835                 "</tr>\n<tr>", r);
836             if (balancer->sticky) {
837                 if (strcmp(balancer->sticky, balancer->sticky_path)) {
838                     ap_rvputs(r, "<td>", balancer->sticky, " | ",
839                               balancer->sticky_path, NULL);
840                 }
841                 else {
842                     ap_rvputs(r, "<td>", balancer->sticky, NULL);
843                 }
844             }
845             else {
846                 ap_rputs("<td> - ", r);
847             }
848             ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
849                 apr_time_sec(balancer->timeout));
850             ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
851             ap_rprintf(r, "<td>%s</td>\n",
852                        balancer->lbmethod->name);
853             ap_rputs("</table>\n<br />", r);
854             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
855                 "<th>Worker URL</th>"
856                 "<th>Route</th><th>RouteRedir</th>"
857                 "<th>Factor</th><th>Set</th><th>Status</th>"
858                 "<th>Elected</th><th>To</th><th>From</th>"
859                 "</tr>\n", r);
860
861             workers = (proxy_worker **)balancer->workers->elts;
862             for (n = 0; n < balancer->workers->nelts; n++) {
863                 char fbuf[50];
864                 worker = *workers;
865                 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
866                           balancer->name + sizeof("balancer://") - 1, "&w=",
867                           ap_escape_uri(r->pool, worker->name),
868                           "&nonce=", balancer_nonce, 
869                           "\">", NULL);
870                 ap_rvputs(r, worker->name, "</a></td>", NULL);
871                 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
872                           NULL);
873                 ap_rvputs(r, "</td><td>",
874                           ap_escape_html(r->pool, worker->s->redirect), NULL);
875                 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
876                 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
877                 if (worker->s->status & PROXY_WORKER_DISABLED)
878                    ap_rputs("Dis ", r);
879                 if (worker->s->status & PROXY_WORKER_IN_ERROR)
880                    ap_rputs("Err ", r);
881                 if (worker->s->status & PROXY_WORKER_STOPPED)
882                    ap_rputs("Stop ", r);
883                 if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
884                    ap_rputs("Stby ", r);
885                 if (PROXY_WORKER_IS_USABLE(worker))
886                     ap_rputs("Ok", r);
887                 if (!PROXY_WORKER_IS_INITIALIZED(worker))
888                     ap_rputs("-", r);
889                 ap_rputs("</td>", r);
890                 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
891                 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
892                 ap_rputs("</td><td>", r);
893                 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
894                 ap_rputs("</td></tr>\n", r);
895
896                 ++workers;
897             }
898             ap_rputs("</table>\n", r);
899             ++balancer;
900         }
901         ap_rputs("<hr />\n", r);
902         if (wsel && bsel) {
903             ap_rputs("<h3>Edit worker settings for ", r);
904             ap_rvputs(r, wsel->name, "</h3>\n", NULL);
905             ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
906             ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
907             ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
908             ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
909             ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
910             ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
911             ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
912             ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->route),
913                       NULL);
914             ap_rputs("\"></td></tr>\n", r);
915             ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
916             ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->redirect),
917                       NULL);
918             ap_rputs("\"></td></tr>\n", r);
919             ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
920             if (wsel->s->status & PROXY_WORKER_DISABLED)
921                 ap_rputs(" checked", r);
922             ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
923             if (!(wsel->s->status & PROXY_WORKER_DISABLED))
924                 ap_rputs(" checked", r);
925             ap_rputs("></td></tr>\n", r);
926             ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
927             ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ",  NULL);
928             ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
929             ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
930             ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
931                       "\">\n", NULL);
932             ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"", 
933                       balancer_nonce, "\">\n", NULL);
934             ap_rvputs(r, "</form>\n", NULL);
935             ap_rputs("<hr />\n", r);
936         }
937         ap_rputs(ap_psignature("",r), r);
938         ap_rputs("</body></html>\n", r);
939     }
940     return OK;
941 }
942
943 static void child_init(apr_pool_t *p, server_rec *s)
944 {
945     while (s) {
946         void *sconf = s->module_config;
947         proxy_server_conf *conf;
948         proxy_balancer *balancer;
949         int i;
950         conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
951
952         /* Initialize shared scoreboard data */
953         balancer = (proxy_balancer *)conf->balancers->elts;
954         for (i = 0; i < conf->balancers->nelts; i++) {
955             if (balancer->lbmethod && balancer->lbmethod->reset)
956                balancer->lbmethod->reset(balancer, s);
957             init_balancer_members(conf, s, balancer);
958             balancer++;
959         }
960         s = s->next;
961     }
962
963 }
964
965 static const char *set_balancer_nonce (cmd_parms *cmd, void *dummy, const char *arg,
966                                        const char *val)
967 {
968     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
969     if (err != NULL) {
970         return err;
971     }
972
973     if (!strcasecmp(arg, "None")) {
974         *balancer_nonce = '\0';
975     } else if (!strcasecmp(arg, "Set")) {
976         if (val) {
977             apr_cpystrn(balancer_nonce, val, sizeof(balancer_nonce));
978         } else {
979             return "BalancerNonce Set requires an argument";
980         }
981     } else if (strcasecmp(arg, "Default")) {
982         return "Bad argument for BalancerNonce: Must be 'Set', 'None' or 'Default'";
983     }
984     return NULL;
985 }
986
987 static const command_rec balancer_cmds[] =
988 {
989     AP_INIT_TAKE12("BalancerNonce", set_balancer_nonce, NULL,
990        RSRC_CONF, "Set value for balancer-manager nonce"),
991     {NULL}
992 };
993
994 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
995 {
996     /* Only the mpm_winnt has child init hook handler.
997      * make sure that we are called after the mpm
998      * initializes
999      */
1000     static const char *const aszPred[] = { "mpm_winnt.c", NULL};
1001      /* manager handler */
1002     ap_hook_pre_config(balancer_init, NULL, NULL, APR_HOOK_MIDDLE);
1003     ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1004     ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1005     proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1006     proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1007     proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1008 }
1009
1010 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
1011     STANDARD20_MODULE_STUFF,
1012     NULL,       /* create per-directory config structure */
1013     NULL,       /* merge per-directory config structures */
1014     NULL,       /* create per-server config structure */
1015     NULL,       /* merge per-server config structures */
1016     balancer_cmds,       /* command apr_table_t */
1017     ap_proxy_balancer_register_hook /* register hooks */
1018 };