]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy_balancer.c
Allow for setting of sticky session split char...
[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 "ap_hooks.h"
24 #include "apr_date.h"
25
26 static const char *balancer_mutex_type = "proxy-balancer-shm";
27 ap_slotmem_provider_t *storage = NULL;
28
29 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
30
31 static int (*ap_proxy_retry_worker_fn)(const char *proxy_function,
32         proxy_worker *worker, server_rec *s) = NULL;
33
34 /*
35  * Register our mutex type before the config is read so we
36  * can adjust the mutex settings using the Mutex directive.
37  */
38 static int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
39                                apr_pool_t *ptemp)
40 {
41
42     apr_status_t rv;
43
44     rv = ap_mutex_register(pconf, balancer_mutex_type, NULL,
45                                APR_LOCK_DEFAULT, 0);
46     if (rv != APR_SUCCESS) {
47         return rv;
48     }
49
50     return OK;
51 }
52
53 #if 0
54 extern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
55                                  proxy_server_conf *conf);
56 #endif
57
58 static int proxy_balancer_canon(request_rec *r, char *url)
59 {
60     char *host, *path;
61     char *search = NULL;
62     const char *err;
63     apr_port_t port = 0;
64
65     /* TODO: offset of BALANCER_PREFIX ?? */
66     if (strncasecmp(url, "balancer:", 9) == 0) {
67         url += 9;
68     }
69     else {
70         return DECLINED;
71     }
72
73     ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "canonicalising URL %s", url);
74
75     /* do syntatic check.
76      * We break the URL into host, port, path, search
77      */
78     err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
79     if (err) {
80         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01157)
81                       "error parsing URL %s: %s",
82                       url, err);
83         return HTTP_BAD_REQUEST;
84     }
85     /*
86      * now parse path/search args, according to rfc1738:
87      * process the path. With proxy-noncanon set (by
88      * mod_proxy) we use the raw, unparsed uri
89      */
90     if (apr_table_get(r->notes, "proxy-nocanon")) {
91         path = url;   /* this is the raw path */
92     }
93     else {
94         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
95                                  r->proxyreq);
96         search = r->args;
97     }
98     if (path == NULL)
99         return HTTP_BAD_REQUEST;
100
101     r->filename = apr_pstrcat(r->pool, "proxy:", BALANCER_PREFIX, host,
102             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
103
104     r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
105
106     return OK;
107 }
108
109 static void init_balancer_members(apr_pool_t *p, server_rec *s,
110                                  proxy_balancer *balancer)
111 {
112     int i;
113     proxy_worker **workers;
114
115     workers = (proxy_worker **)balancer->workers->elts;
116
117     for (i = 0; i < balancer->workers->nelts; i++) {
118         int worker_is_initialized;
119         proxy_worker *worker = *workers;
120         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01158)
121                      "Looking at %s -> %s initialized?", balancer->s->name, worker->s->name);
122         worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(worker);
123         if (!worker_is_initialized) {
124             ap_proxy_initialize_worker(worker, s, p);
125         }
126         ++workers;
127     }
128
129     /* Set default number of attempts to the number of
130      * workers.
131      */
132     if (!balancer->s->max_attempts_set && balancer->workers->nelts > 1) {
133         balancer->s->max_attempts = balancer->workers->nelts - 1;
134         balancer->s->max_attempts_set = 1;
135     }
136 }
137
138 /* Retrieve the parameter with the given name
139  * Something like 'JSESSIONID=12345...N'
140  */
141 static char *get_path_param(apr_pool_t *pool, char *url,
142                             const char *name, int scolon_sep)
143 {
144     char *path = NULL;
145     char *pathdelims = "?&";
146
147     if (scolon_sep) {
148         pathdelims = ";?&";
149     }
150     for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
151         path += strlen(name);
152         if (*path == '=') {
153             /*
154              * Session path was found, get its value
155              */
156             ++path;
157             if (*path) {
158                 char *q;
159                 path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
160                 return path;
161             }
162         }
163     }
164     return NULL;
165 }
166
167 static char *get_cookie_param(request_rec *r, const char *name)
168 {
169     const char *cookies;
170     const char *start_cookie;
171
172     if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
173         for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
174              start_cookie = ap_strstr_c(start_cookie + 1, name)) {
175             if (start_cookie == cookies ||
176                 start_cookie[-1] == ';' ||
177                 start_cookie[-1] == ',' ||
178                 isspace(start_cookie[-1])) {
179
180                 start_cookie += strlen(name);
181                 while(*start_cookie && isspace(*start_cookie))
182                     ++start_cookie;
183                 if (*start_cookie++ == '=' && *start_cookie) {
184                     /*
185                      * Session cookie was found, get its value
186                      */
187                     char *end_cookie, *cookie;
188                     cookie = apr_pstrdup(r->pool, start_cookie);
189                     if ((end_cookie = strchr(cookie, ';')) != NULL)
190                         *end_cookie = '\0';
191                     if((end_cookie = strchr(cookie, ',')) != NULL)
192                         *end_cookie = '\0';
193                     return cookie;
194                 }
195             }
196         }
197     }
198     return NULL;
199 }
200
201 /* Find the worker that has the 'route' defined
202  */
203 static proxy_worker *find_route_worker(proxy_balancer *balancer,
204                                        const char *route, request_rec *r)
205 {
206     int i;
207     int checking_standby;
208     int checked_standby;
209
210     proxy_worker **workers;
211
212     checking_standby = checked_standby = 0;
213     while (!checked_standby) {
214         workers = (proxy_worker **)balancer->workers->elts;
215         for (i = 0; i < balancer->workers->nelts; i++, workers++) {
216             proxy_worker *worker = *workers;
217             if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
218                 continue;
219             if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
220                 if (PROXY_WORKER_IS_USABLE(worker)) {
221                     return worker;
222                 } else {
223                     /*
224                      * If the worker is in error state run
225                      * retry on that worker. It will be marked as
226                      * operational if the retry timeout is elapsed.
227                      * The worker might still be unusable, but we try
228                      * anyway.
229                      */
230                     ap_proxy_retry_worker_fn("BALANCER", worker, r->server);
231                     if (PROXY_WORKER_IS_USABLE(worker)) {
232                             return worker;
233                     } else {
234                         /*
235                          * We have a worker that is unusable.
236                          * It can be in error or disabled, but in case
237                          * it has a redirection set use that redirection worker.
238                          * This enables to safely remove the member from the
239                          * balancer. Of course you will need some kind of
240                          * session replication between those two remote.
241                          */
242                         if (*worker->s->redirect) {
243                             proxy_worker *rworker = NULL;
244                             rworker = find_route_worker(balancer, worker->s->redirect, r);
245                             /* Check if the redirect worker is usable */
246                             if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
247                                 /*
248                                  * If the worker is in error state run
249                                  * retry on that worker. It will be marked as
250                                  * operational if the retry timeout is elapsed.
251                                  * The worker might still be unusable, but we try
252                                  * anyway.
253                                  */
254                                 ap_proxy_retry_worker_fn("BALANCER", rworker, r->server);
255                             }
256                             if (rworker && PROXY_WORKER_IS_USABLE(rworker))
257                                 return rworker;
258                         }
259                     }
260                 }
261             }
262         }
263         checked_standby = checking_standby++;
264     }
265     return NULL;
266 }
267
268 static proxy_worker *find_session_route(proxy_balancer *balancer,
269                                         request_rec *r,
270                                         char **route,
271                                         const char **sticky_used,
272                                         char **url)
273 {
274     proxy_worker *worker = NULL;
275
276     if (!*balancer->s->sticky)
277         return NULL;
278     /* Try to find the sticky route inside url */
279     *route = get_path_param(r->pool, *url, balancer->s->sticky_path, balancer->s->scolonsep);
280     if (*route) {
281         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01159)
282                      "Found value %s for stickysession %s",
283                      *route, balancer->s->sticky_path);
284         *sticky_used =  balancer->s->sticky_path;
285     }
286     else {
287         *route = get_cookie_param(r, balancer->s->sticky);
288         if (*route) {
289             *sticky_used =  balancer->s->sticky;
290             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01160)
291                          "Found value %s for stickysession %s",
292                          *route, balancer->s->sticky);
293         }
294     }
295     /*
296      * If we found a value for stickysession, find the first '.' (or whatever
297      * sticky_separator is set to) within. Everything after '.' (if present)
298      * is our route. 
299      */
300     if ((*route) && (balancer->s->sticky_separator != 0) && ((*route = strchr(*route, balancer->s->sticky_separator)) != NULL ))
301         (*route)++;
302     if ((*route) && (**route)) {
303         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01161) "Found route %s", *route);
304         /* We have a route in path or in cookie
305          * Find the worker that has this route defined.
306          */
307         worker = find_route_worker(balancer, *route, r);
308         if (worker && strcmp(*route, worker->s->route)) {
309             /*
310              * Notice that the route of the worker chosen is different from
311              * the route supplied by the client.
312              */
313             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
314             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01162)
315                           "Route changed from %s to %s",
316                           *route, worker->s->route);
317         }
318         return worker;
319     }
320     else
321         return NULL;
322 }
323
324 static proxy_worker *find_best_worker(proxy_balancer *balancer,
325                                       request_rec *r)
326 {
327     proxy_worker *candidate = NULL;
328     apr_status_t rv;
329
330     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
331         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01163)
332                       "%s: Lock failed for find_best_worker()",
333                       balancer->s->name);
334         return NULL;
335     }
336
337     candidate = (*balancer->lbmethod->finder)(balancer, r);
338
339     if (candidate)
340         candidate->s->elected++;
341
342     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
343         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01164)
344                       "%s: Unlock failed for find_best_worker()",
345                       balancer->s->name);
346     }
347
348     if (candidate == NULL) {
349         /* All the workers are in error state or disabled.
350          * If the balancer has a timeout sleep for a while
351          * and try again to find the worker. The chances are
352          * that some other thread will release a connection.
353          * By default the timeout is not set, and the server
354          * returns SERVER_BUSY.
355          */
356         if (balancer->s->timeout) {
357             /* XXX: This can perhaps be build using some
358              * smarter mechanism, like tread_cond.
359              * But since the statuses can came from
360              * different childs, use the provided algo.
361              */
362             apr_interval_time_t timeout = balancer->s->timeout;
363             apr_interval_time_t step, tval = 0;
364             /* Set the timeout to 0 so that we don't
365              * end in infinite loop
366              */
367             balancer->s->timeout = 0;
368             step = timeout / 100;
369             while (tval < timeout) {
370                 apr_sleep(step);
371                 /* Try again */
372                 if ((candidate = find_best_worker(balancer, r)))
373                     break;
374                 tval += step;
375             }
376             /* restore the timeout */
377             balancer->s->timeout = timeout;
378         }
379     }
380
381     return candidate;
382
383 }
384
385 static int rewrite_url(request_rec *r, proxy_worker *worker,
386                         char **url)
387 {
388     const char *scheme = strstr(*url, "://");
389     const char *path = NULL;
390
391     if (scheme)
392         path = ap_strchr_c(scheme + 3, '/');
393
394     /* we break the URL into host, port, uri */
395     if (!worker) {
396         return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
397                              "missing worker. URI cannot be parsed: ", *url,
398                              NULL));
399     }
400
401     *url = apr_pstrcat(r->pool, worker->s->name, path, NULL);
402
403     return OK;
404 }
405
406 static void force_recovery(proxy_balancer *balancer, server_rec *s)
407 {
408     int i;
409     int ok = 0;
410     proxy_worker **worker;
411
412     worker = (proxy_worker **)balancer->workers->elts;
413     for (i = 0; i < balancer->workers->nelts; i++, worker++) {
414         if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
415             ok = 1;
416             break;
417         }
418         else {
419             /* Try if we can recover */
420             ap_proxy_retry_worker_fn("BALANCER", *worker, s);
421             if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
422                 ok = 1;
423                 break;
424             }
425         }
426     }
427     if (!ok && balancer->s->forcerecovery) {
428         /* If all workers are in error state force the recovery.
429          */
430         worker = (proxy_worker **)balancer->workers->elts;
431         for (i = 0; i < balancer->workers->nelts; i++, worker++) {
432             ++(*worker)->s->retries;
433             (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
434             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01165)
435                          "%s: Forcing recovery for worker (%s)",
436                          balancer->s->name, (*worker)->s->hostname);
437         }
438     }
439 }
440
441 static apr_status_t decrement_busy_count(void *worker_)
442 {
443     proxy_worker *worker = worker_;
444     
445     if (worker->s->busy) {
446         worker->s->busy--;
447     }
448
449     return APR_SUCCESS;
450 }
451
452 static int proxy_balancer_pre_request(proxy_worker **worker,
453                                       proxy_balancer **balancer,
454                                       request_rec *r,
455                                       proxy_server_conf *conf, char **url)
456 {
457     int access_status;
458     proxy_worker *runtime;
459     char *route = NULL;
460     const char *sticky = NULL;
461     apr_status_t rv;
462
463     *worker = NULL;
464     /* Step 1: check if the url is for us
465      * The url we can handle starts with 'balancer://'
466      * If balancer is already provided skip the search
467      * for balancer, because this is failover attempt.
468      */
469     if (!*balancer &&
470         !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url, 1)))
471         return DECLINED;
472
473     /* Step 2: Lock the LoadBalancer
474      * XXX: perhaps we need the process lock here
475      */
476     if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
477         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01166)
478                       "%s: Lock failed for pre_request", (*balancer)->s->name);
479         return DECLINED;
480     }
481
482     /* Step 3: force recovery */
483     force_recovery(*balancer, r->server);
484
485     /* Step 3.5: Update member list for the balancer */
486     /* TODO: Implement as provider! */
487     ap_proxy_sync_balancer(*balancer, r->server, conf);
488
489     /* Step 4: find the session route */
490     runtime = find_session_route(*balancer, r, &route, &sticky, url);
491     if (runtime) {
492         if ((*balancer)->lbmethod && (*balancer)->lbmethod->updatelbstatus) {
493             /* Call the LB implementation */
494             (*balancer)->lbmethod->updatelbstatus(*balancer, runtime, r->server);
495         }
496         else { /* Use the default one */
497             int i, total_factor = 0;
498             proxy_worker **workers;
499             /* We have a sticky load balancer
500              * Update the workers status
501              * so that even session routes get
502              * into account.
503              */
504             workers = (proxy_worker **)(*balancer)->workers->elts;
505             for (i = 0; i < (*balancer)->workers->nelts; i++) {
506                 /* Take into calculation only the workers that are
507                  * not in error state or not disabled.
508                  */
509                 if (PROXY_WORKER_IS_USABLE(*workers)) {
510                     (*workers)->s->lbstatus += (*workers)->s->lbfactor;
511                     total_factor += (*workers)->s->lbfactor;
512                 }
513                 workers++;
514             }
515             runtime->s->lbstatus -= total_factor;
516         }
517         runtime->s->elected++;
518
519         *worker = runtime;
520     }
521     else if (route && (*balancer)->s->sticky_force) {
522         int i, member_of = 0;
523         proxy_worker **workers;
524         /*
525          * We have a route provided that doesn't match the
526          * balancer name. See if the provider route is the
527          * member of the same balancer in which case return 503
528          */
529         workers = (proxy_worker **)(*balancer)->workers->elts;
530         for (i = 0; i < (*balancer)->workers->nelts; i++) {
531             if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
532                 member_of = 1;
533                 break;
534             }
535             workers++;
536         }
537         if (member_of) {
538             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01167)
539                           "%s: All workers are in error state for route (%s)",
540                           (*balancer)->s->name, route);
541             if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
542                 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01168)
543                               "%s: Unlock failed for pre_request",
544                               (*balancer)->s->name);
545             }
546             return HTTP_SERVICE_UNAVAILABLE;
547         }
548     }
549
550     if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
551         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01169)
552                       "%s: Unlock failed for pre_request",
553                       (*balancer)->s->name);
554     }
555     if (!*worker) {
556         runtime = find_best_worker(*balancer, r);
557         if (!runtime) {
558             if ((*balancer)->workers->nelts) {
559                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01170)
560                               "%s: All workers are in error state",
561                               (*balancer)->s->name);
562             } else {
563                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01171)
564                               "%s: No workers in balancer",
565                               (*balancer)->s->name);
566             }
567
568             return HTTP_SERVICE_UNAVAILABLE;
569         }
570         if (*(*balancer)->s->sticky && runtime) {
571             /*
572              * This balancer has sticky sessions and the client either has not
573              * supplied any routing information or all workers for this route
574              * including possible redirect and hotstandby workers are in error
575              * state, but we have found another working worker for this
576              * balancer where we can send the request. Thus notice that we have
577              * changed the route to the backend.
578              */
579             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
580         }
581         *worker = runtime;
582     }
583
584     (*worker)->s->busy++;
585     apr_pool_cleanup_register(r->pool, *worker, decrement_busy_count,
586                               apr_pool_cleanup_null);
587
588     /* Add balancer/worker info to env. */
589     apr_table_setn(r->subprocess_env,
590                    "BALANCER_NAME", (*balancer)->s->name);
591     apr_table_setn(r->subprocess_env,
592                    "BALANCER_WORKER_NAME", (*worker)->s->name);
593     apr_table_setn(r->subprocess_env,
594                    "BALANCER_WORKER_ROUTE", (*worker)->s->route);
595
596     /* Rewrite the url from 'balancer://url'
597      * to the 'worker_scheme://worker_hostname[:worker_port]/url'
598      * This replaces the balancers fictional name with the
599      * real hostname of the elected worker.
600      */
601     access_status = rewrite_url(r, *worker, url);
602     /* Add the session route to request notes if present */
603     if (route) {
604         apr_table_setn(r->notes, "session-sticky", sticky);
605         apr_table_setn(r->notes, "session-route", route);
606
607         /* Add session info to env. */
608         apr_table_setn(r->subprocess_env,
609                        "BALANCER_SESSION_STICKY", sticky);
610         apr_table_setn(r->subprocess_env,
611                        "BALANCER_SESSION_ROUTE", route);
612     }
613     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01172)
614                   "%s: worker (%s) rewritten to %s",
615                   (*balancer)->s->name, (*worker)->s->name, *url);
616
617     return access_status;
618 }
619
620 static int proxy_balancer_post_request(proxy_worker *worker,
621                                        proxy_balancer *balancer,
622                                        request_rec *r,
623                                        proxy_server_conf *conf)
624 {
625
626     apr_status_t rv;
627
628     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
629         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01173)
630                       "%s: Lock failed for post_request",
631                       balancer->s->name);
632         return HTTP_INTERNAL_SERVER_ERROR;
633     }
634
635     if (!apr_is_empty_array(balancer->errstatuses)) {
636         int i;
637         for (i = 0; i < balancer->errstatuses->nelts; i++) {
638             int val = ((int *)balancer->errstatuses->elts)[i];
639             if (r->status == val) {
640                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01174)
641                               "%s: Forcing worker (%s) into error state " 
642                               "due to status code %d matching 'failonstatus' "
643                               "balancer parameter",
644                               balancer->s->name, worker->s->name, val);
645                 worker->s->status |= PROXY_WORKER_IN_ERROR;
646                 worker->s->error_time = apr_time_now();
647                 break;
648             }
649         }
650     }
651
652     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
653         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01175)
654                       "%s: Unlock failed for post_request", balancer->s->name);
655     }
656     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01176)
657                   "proxy_balancer_post_request for (%s)", balancer->s->name);
658
659     return OK;
660 }
661
662 static void recalc_factors(proxy_balancer *balancer)
663 {
664     int i;
665     proxy_worker **workers;
666
667
668     /* Recalculate lbfactors */
669     workers = (proxy_worker **)balancer->workers->elts;
670     /* Special case if there is only one worker its
671      * load factor will always be 1
672      */
673     if (balancer->workers->nelts == 1) {
674         (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
675         return;
676     }
677     for (i = 0; i < balancer->workers->nelts; i++) {
678         /* Update the status entries */
679         workers[i]->s->lbstatus = workers[i]->s->lbfactor;
680     }
681 }
682
683 static apr_status_t lock_remove(void *data)
684 {
685     int i;
686     proxy_balancer *balancer;
687     server_rec *s = data;
688     void *sconf = s->module_config;
689     proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
690
691     balancer = (proxy_balancer *)conf->balancers->elts;
692     for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
693         if (balancer->gmutex) {
694             apr_global_mutex_destroy(balancer->gmutex);
695             balancer->gmutex = NULL;
696         }
697     }
698     return(0);
699 }
700
701 /* post_config hook: */
702 static int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
703                          apr_pool_t *ptemp, server_rec *s)
704 {
705     apr_status_t rv;
706     proxy_server_conf *conf;
707     ap_slotmem_instance_t *new = NULL;
708     apr_time_t tstamp;
709
710     /* balancer_post_config() will be called twice during startup.  So, don't
711      * set up the static data the 1st time through. */
712     if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) {
713         return OK;
714     }
715
716     if (!ap_proxy_retry_worker_fn) {
717         ap_proxy_retry_worker_fn =
718                 APR_RETRIEVE_OPTIONAL_FN(ap_proxy_retry_worker);
719         if (!ap_proxy_retry_worker_fn) {
720             ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02230)
721                          "mod_proxy must be loaded for mod_proxy_balancer");
722             return !OK;
723         }
724     }
725
726     /*
727      * Get slotmem setups
728      */
729     storage = ap_lookup_provider(AP_SLOTMEM_PROVIDER_GROUP, "shm",
730                                  AP_SLOTMEM_PROVIDER_VERSION);
731     if (!storage) {
732         ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01177)
733                      "Failed to lookup provider 'shm' for '%s': is "
734                      "mod_slotmem_shm loaded??",
735                      AP_SLOTMEM_PROVIDER_GROUP);
736         return !OK;
737     }
738
739     tstamp = apr_time_now();
740     /*
741      * Go thru each Vhost and create the shared mem slotmem for
742      * each balancer's workers
743      */
744     while (s) {
745         int i,j;
746         char *id;
747         proxy_balancer *balancer;
748         ap_slotmem_type_t type;
749         void *sconf = s->module_config;
750         conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
751         /*
752          * During create_proxy_config() we created a dummy id. Now that
753          * we have identifying info, we can create the real id
754          */
755         id = apr_psprintf(pconf, "%s.%s.%d.%s.%s.%u.%s",
756                           (s->server_scheme ? s->server_scheme : "????"),
757                           (s->server_hostname ? s->server_hostname : "???"),
758                           (int)s->port,
759                           (s->server_admin ? s->server_admin : "??"),
760                           (s->defn_name ? s->defn_name : "?"),
761                           s->defn_line_number,
762                           (s->error_fname ? s->error_fname : DEFAULT_ERRORLOG));
763         conf->id = apr_psprintf(pconf, "p%x",
764                                 ap_proxy_hashfunc(id, PROXY_HASHFUNC_DEFAULT));
765         if (conf->bslot) {
766             /* Shared memory already created for this proxy_server_conf.
767              */
768             s = s->next;
769             continue;
770         }
771         if (conf->bal_persist) {
772             type = AP_SLOTMEM_TYPE_PREGRAB | AP_SLOTMEM_TYPE_PERSIST;
773         } else {
774             type = AP_SLOTMEM_TYPE_PREGRAB;
775         }
776         if (conf->balancers->nelts) {
777             conf->max_balancers = conf->balancers->nelts + conf->bgrowth;
778             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01178) "Doing balancers create: %d, %d (%d)",
779                          (int)ALIGNED_PROXY_BALANCER_SHARED_SIZE,
780                          (int)conf->balancers->nelts, conf->max_balancers);
781
782             rv = storage->create(&new, conf->id,
783                                  ALIGNED_PROXY_BALANCER_SHARED_SIZE,
784                                  conf->max_balancers, type, pconf);
785             if (rv != APR_SUCCESS) {
786                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01179) "balancer slotmem_create failed");
787                 return !OK;
788             }
789             conf->bslot = new;
790         }
791         conf->storage = storage;
792
793         /* Initialize shared scoreboard data */
794         balancer = (proxy_balancer *)conf->balancers->elts;
795         for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
796             proxy_worker **workers;
797             proxy_worker *worker;
798             proxy_balancer_shared *bshm;
799             const char *sname;
800
801             /* now that we have the right id, we need to redo the sname field */
802             ap_pstr2_alnum(pconf, balancer->s->name + sizeof(BALANCER_PREFIX) - 1,
803                            &sname);
804             sname = apr_pstrcat(pconf, conf->id, "_", sname, NULL);
805             PROXY_STRNCPY(balancer->s->sname, sname); /* We know this will succeed */
806
807             balancer->max_workers = balancer->workers->nelts + balancer->growth;
808
809             /* Create global mutex */
810             rv = ap_global_mutex_create(&(balancer->gmutex), NULL, balancer_mutex_type,
811                                         balancer->s->sname, s, pconf, 0);
812             if (rv != APR_SUCCESS || !balancer->gmutex) {
813                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01180)
814                              "mutex creation of %s : %s failed", balancer_mutex_type,
815                              balancer->s->sname);
816                 return HTTP_INTERNAL_SERVER_ERROR;
817             }
818
819             apr_pool_cleanup_register(pconf, (void *)s, lock_remove,
820                                       apr_pool_cleanup_null);
821
822             /* setup shm for balancers */
823             if ((rv = storage->fgrab(conf->bslot, i)) != APR_SUCCESS) {
824                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01181) "balancer slotmem_grab failed");
825                 return !OK;
826
827             }
828             if ((rv = storage->dptr(conf->bslot, i, (void *)&bshm)) != APR_SUCCESS) {
829                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01182) "balancer slotmem_dptr failed");
830                 return !OK;
831             }
832             if ((rv = ap_proxy_share_balancer(balancer, bshm, i)) != APR_SUCCESS) {
833                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01183) "Cannot share balancer");
834                 return !OK;
835             }
836
837             /* create slotmem slots for workers */
838             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01184) "Doing workers create: %s (%s), %d, %d [%u]",
839                          balancer->s->name, balancer->s->sname,
840                          (int)ALIGNED_PROXY_WORKER_SHARED_SIZE,
841                          (int)balancer->max_workers, i);
842
843             rv = storage->create(&new, balancer->s->sname,
844                                  ALIGNED_PROXY_WORKER_SHARED_SIZE,
845                                  balancer->max_workers, type, pconf);
846             if (rv != APR_SUCCESS) {
847                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01185) "worker slotmem_create failed");
848                 return !OK;
849             }
850             balancer->wslot = new;
851             balancer->storage = storage;
852
853             /* sync all timestamps */
854             balancer->wupdated = balancer->s->wupdated = tstamp;
855
856             /* now go thru each worker */
857             workers = (proxy_worker **)balancer->workers->elts;
858             for (j = 0; j < balancer->workers->nelts; j++, workers++) {
859                 proxy_worker_shared *shm;
860
861                 worker = *workers;
862                 if ((rv = storage->fgrab(balancer->wslot, j)) != APR_SUCCESS) {
863                     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01186) "worker slotmem_grab failed");
864                     return !OK;
865
866                 }
867                 if ((rv = storage->dptr(balancer->wslot, j, (void *)&shm)) != APR_SUCCESS) {
868                     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01187) "worker slotmem_dptr failed");
869                     return !OK;
870                 }
871                 if ((rv = ap_proxy_share_worker(worker, shm, j)) != APR_SUCCESS) {
872                     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01188) "Cannot share worker");
873                     return !OK;
874                 }
875                 worker->s->updated = tstamp;
876             }
877             if (conf->bal_persist) {
878                 /* We could have just read-in a persisted config. Force a sync. */
879                 balancer->wupdated--;
880                 ap_proxy_sync_balancer(balancer, s, conf);
881             }
882         }
883         s = s->next;
884     }
885
886     return OK;
887 }
888
889 static void create_radio(const char *name, unsigned int flag, request_rec *r)
890 {
891     ap_rvputs(r, "<td>On <input name='", name, "' id='", name, "' value='1' type=radio", NULL);
892     if (flag)
893         ap_rputs(" checked", r);
894     ap_rvputs(r, "> <br/> Off <input name='", name, "' id='", name, "' value='0' type=radio", NULL);
895     if (!flag)
896         ap_rputs(" checked", r);
897     ap_rputs("></td>\n", r);
898 }
899
900 static void push2table(const char *input, apr_table_t *params,
901                        const char *allowed[], apr_pool_t *p)
902 {
903     char *args;
904     char *tok, *val;
905     char *key;
906
907     if (input == NULL) {
908         return;
909     }
910     args = apr_pstrdup(p, input);
911
912     key = apr_strtok(args, "&", &tok);
913     while (key) {
914         val = strchr(key, '=');
915         if (val) {
916             *val++ = '\0';
917         }
918         else {
919             val = "";
920         }
921         ap_unescape_url(key);
922         ap_unescape_url(val);
923         if (allowed == NULL) { /* allow all */
924             apr_table_set(params, key, val);
925         }
926         else {
927             const char **ok = allowed;
928             while (*ok) {
929                 if (strcmp(*ok, key) == 0) {
930                     apr_table_set(params, key, val);
931                     break;
932                 }
933                 ok++;
934             }
935         }
936         key = apr_strtok(NULL, "&", &tok);
937     }
938 }
939
940 /* Manages the loadfactors and member status
941  *   The balancer, worker and nonce are obtained from
942  *   the request args (?b=...&w=...&nonce=....).
943  *   All other params are pulled from any POST
944  *   data that exists.
945  * TODO:
946  *   /.../<whatever>/balancer/worker/nonce
947  */
948 static int balancer_handler(request_rec *r)
949 {
950     void *sconf;
951     proxy_server_conf *conf;
952     proxy_balancer *balancer, *bsel = NULL;
953     proxy_worker *worker, *wsel = NULL;
954     proxy_worker **workers = NULL;
955     apr_table_t *params;
956     int i, n;
957     int ok2change = 1;
958     const char *name;
959     const char *action;
960     apr_status_t rv;
961
962     /* is this for us? */
963     if (strcmp(r->handler, "balancer-manager")) {
964         return DECLINED;
965     }
966
967     r->allowed = 0
968     | (AP_METHOD_BIT << M_GET)
969     | (AP_METHOD_BIT << M_POST);
970     if ((r->method_number != M_GET) && (r->method_number != M_POST)) {
971         return DECLINED;
972     }
973
974     sconf = r->server->module_config;
975     conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
976     params = apr_table_make(r->pool, 10);
977
978     balancer = (proxy_balancer *)conf->balancers->elts;
979     for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
980         if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
981             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01189)
982                           "%s: Lock failed for balancer_handler",
983                           balancer->s->name);
984         }
985         ap_proxy_sync_balancer(balancer, r->server, conf);
986         if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
987             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01190)
988                           "%s: Unlock failed for balancer_handler",
989                           balancer->s->name);
990         }
991     }
992
993     if (r->args && (r->method_number == M_GET)) {
994         const char *allowed[] = { "w", "b", "nonce", "xml", NULL };
995         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01191) "parsing r->args");
996
997         push2table(r->args, params, allowed, r->pool);
998     }
999     if (r->method_number == M_POST) {
1000         apr_bucket_brigade *ib;
1001         apr_size_t len = 1024;
1002         char *buf = apr_pcalloc(r->pool, len+1);
1003
1004         ib = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
1005         rv = ap_get_brigade(r->input_filters, ib, AP_MODE_READBYTES,
1006                                 APR_BLOCK_READ, len);
1007         if (rv != APR_SUCCESS) {
1008             return HTTP_INTERNAL_SERVER_ERROR;
1009         }
1010         apr_brigade_flatten(ib, buf, &len);
1011         buf[len] = '\0';
1012         push2table(buf, params, NULL, r->pool);
1013     }
1014     if ((name = apr_table_get(params, "b")))
1015         bsel = ap_proxy_get_balancer(r->pool, conf,
1016             apr_pstrcat(r->pool, BALANCER_PREFIX, name, NULL), 0);
1017
1018     if ((name = apr_table_get(params, "w"))) {
1019         wsel = ap_proxy_get_worker(r->pool, bsel, conf, name);
1020     }
1021
1022
1023     /* Check that the supplied nonce matches this server's nonce;
1024      * otherwise ignore all parameters, to prevent a CSRF attack. */
1025     if (!bsel ||
1026         (*bsel->s->nonce &&
1027          (
1028           (name = apr_table_get(params, "nonce")) == NULL ||
1029           strcmp(bsel->s->nonce, name) != 0
1030          )
1031         )
1032        ) {
1033         apr_table_clear(params);
1034         ok2change = 0;
1035     }
1036
1037     /* First set the params */
1038     if (wsel && ok2change) {
1039         const char *val;
1040         int was_usable = PROXY_WORKER_IS_USABLE(wsel);
1041
1042         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01192) "settings worker params");
1043
1044         if ((val = apr_table_get(params, "w_lf"))) {
1045             int ival = atoi(val);
1046             if (ival >= 1 && ival <= 100) {
1047                 wsel->s->lbfactor = ival;
1048                 if (bsel)
1049                     recalc_factors(bsel);
1050             }
1051         }
1052         if ((val = apr_table_get(params, "w_wr"))) {
1053             if (strlen(val) && strlen(val) < sizeof(wsel->s->route))
1054                 strcpy(wsel->s->route, val);
1055             else
1056                 *wsel->s->route = '\0';
1057         }
1058         if ((val = apr_table_get(params, "w_rr"))) {
1059             if (strlen(val) && strlen(val) < sizeof(wsel->s->redirect))
1060                 strcpy(wsel->s->redirect, val);
1061             else
1062                 *wsel->s->redirect = '\0';
1063         }
1064         if ((val = apr_table_get(params, "w_status_I"))) {
1065             ap_proxy_set_wstatus('I', atoi(val), wsel);
1066         }
1067         if ((val = apr_table_get(params, "w_status_N"))) {
1068             ap_proxy_set_wstatus('N', atoi(val), wsel);
1069         }
1070         if ((val = apr_table_get(params, "w_status_D"))) {
1071             ap_proxy_set_wstatus('D', atoi(val), wsel);
1072         }
1073         if ((val = apr_table_get(params, "w_status_H"))) {
1074             ap_proxy_set_wstatus('H', atoi(val), wsel);
1075         }
1076         if ((val = apr_table_get(params, "w_ls"))) {
1077             int ival = atoi(val);
1078             if (ival >= 0 && ival <= 99) {
1079                 wsel->s->lbset = ival;
1080              }
1081         }
1082         /* if enabling, we need to reset all lb params */
1083         if (bsel && !was_usable && PROXY_WORKER_IS_USABLE(wsel)) {
1084             bsel->s->need_reset = 1;
1085         }
1086
1087     }
1088
1089     if (bsel && ok2change) {
1090         const char *val;
1091         int ival;
1092         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01193)
1093                       "settings balancer params");
1094         if ((val = apr_table_get(params, "b_lbm"))) {
1095             if ((strlen(val) < (sizeof(bsel->s->lbpname)-1)) &&
1096                 strcmp(val, bsel->s->lbpname)) {
1097                 proxy_balancer_method *lbmethod;
1098                 lbmethod = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
1099                 if (lbmethod) {
1100                     PROXY_STRNCPY(bsel->s->lbpname, val);
1101                     bsel->lbmethod = lbmethod;
1102                     bsel->s->wupdated = apr_time_now();
1103                     bsel->s->need_reset = 1;
1104                 }
1105             }
1106         }
1107         if ((val = apr_table_get(params, "b_tmo"))) {
1108             ival = atoi(val);
1109             if (ival >= 0 && ival <= 7200) { /* 2 hrs enuff? */
1110                 bsel->s->timeout = apr_time_from_sec(ival);
1111             }
1112         }
1113         if ((val = apr_table_get(params, "b_max"))) {
1114             ival = atoi(val);
1115             if (ival >= 0 && ival <= 99) {
1116                 bsel->s->max_attempts = ival;
1117             }
1118         }
1119         if ((val = apr_table_get(params, "b_sforce"))) {
1120             ival = atoi(val);
1121             bsel->s->sticky_force = (ival != 0);
1122         }
1123         if ((val = apr_table_get(params, "b_ss")) && *val) {
1124             if (strlen(val) < (sizeof(bsel->s->sticky_path)-1)) {
1125                 if (*val == '-' && *(val+1) == '\0')
1126                     *bsel->s->sticky_path = *bsel->s->sticky = '\0';
1127                 else {
1128                     char *path;
1129                     PROXY_STRNCPY(bsel->s->sticky_path, val);
1130                     PROXY_STRNCPY(bsel->s->sticky, val);
1131
1132                     if ((path = strchr((char *)bsel->s->sticky, '|'))) {
1133                         *path++ = '\0';
1134                         PROXY_STRNCPY(bsel->s->sticky_path, path);
1135                     }
1136                 }
1137             }
1138         }
1139         if ((val = apr_table_get(params, "b_wyes")) &&
1140             (*val == '1' && *(val+1) == '\0') &&
1141             (val = apr_table_get(params, "b_nwrkr"))) {
1142             char *ret;
1143             proxy_worker *nworker;
1144             nworker = ap_proxy_get_worker(conf->pool, bsel, conf, val);
1145             if (!nworker && storage->num_free_slots(bsel->wslot)) {
1146                 if ((rv = PROXY_GLOBAL_LOCK(bsel)) != APR_SUCCESS) {
1147                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01194)
1148                                   "%s: Lock failed for adding worker",
1149                                   bsel->s->name);
1150                 }
1151                 ret = ap_proxy_define_worker(conf->pool, &nworker, bsel, conf, val, 0);
1152                 if (!ret) {
1153                     unsigned int index;
1154                     proxy_worker_shared *shm;
1155                     PROXY_COPY_CONF_PARAMS(nworker, conf);
1156                     if ((rv = storage->grab(bsel->wslot, &index)) != APR_SUCCESS) {
1157                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01195)
1158                                       "worker slotmem_grab failed");
1159                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1160                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01196)
1161                                           "%s: Unlock failed for adding worker",
1162                                           bsel->s->name);
1163                         }
1164                         return HTTP_BAD_REQUEST;
1165                     }
1166                     if ((rv = storage->dptr(bsel->wslot, index, (void *)&shm)) != APR_SUCCESS) {
1167                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01197)
1168                                       "worker slotmem_dptr failed");
1169                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1170                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01198)
1171                                           "%s: Unlock failed for adding worker",
1172                                           bsel->s->name);
1173                         }
1174                         return HTTP_BAD_REQUEST;
1175                     }
1176                     if ((rv = ap_proxy_share_worker(nworker, shm, index)) != APR_SUCCESS) {
1177                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01199)
1178                                       "Cannot share worker");
1179                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1180                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01200)
1181                                           "%s: Unlock failed for adding worker",
1182                                           bsel->s->name);
1183                         }
1184                         return HTTP_BAD_REQUEST;
1185                     }
1186                     if ((rv = ap_proxy_initialize_worker(nworker, r->server, conf->pool)) != APR_SUCCESS) {
1187                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01201)
1188                                       "Cannot init worker");
1189                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1190                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01202)
1191                                           "%s: Unlock failed for adding worker",
1192                                           bsel->s->name);
1193                         }
1194                         return HTTP_BAD_REQUEST;
1195                     }
1196                     /* sync all timestamps */
1197                     bsel->wupdated = bsel->s->wupdated = nworker->s->updated = apr_time_now();
1198                     /* by default, all new workers are disabled */
1199                     ap_proxy_set_wstatus('D', 1, nworker);
1200                 }
1201                 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1202                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01203)
1203                                   "%s: Unlock failed for adding worker",
1204                                   bsel->s->name);
1205                 }
1206             }
1207
1208         }
1209
1210     }
1211
1212     action = ap_construct_url(r->pool, r->uri, r);
1213     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01204) "genning page");
1214
1215     if (apr_table_get(params, "xml")) {
1216         char date[APR_RFC822_DATE_LEN];
1217         ap_set_content_type(r, "text/xml");
1218         ap_rputs("<?xml version='1.0' encoding='UTF-8' ?>\n", r);
1219         ap_rputs("<httpd:manager xmlns:httpd='http://httpd.apache.org'>\n", r);
1220         ap_rputs("  <httpd:balancers>\n", r);
1221         balancer = (proxy_balancer *)conf->balancers->elts;
1222         for (i = 0; i < conf->balancers->nelts; i++) {
1223             ap_rputs("    <httpd:balancer>\n", r);
1224             /* Start proxy_balancer */
1225             ap_rvputs(r, "      <httpd:name>", balancer->s->name, "</httpd:name>\n", NULL);
1226             if (balancer->s->sticky) {
1227                 ap_rvputs(r, "      <httpd:stickysession>", balancer->s->sticky,
1228                           "</httpd:stickysession>\n", NULL);
1229                 ap_rprintf(r,
1230                            "      <httpd:nofailover>%s</httpd:nofailover>\n",
1231                            (balancer->s->sticky_force ? "On" : "Off"));
1232             }
1233             ap_rprintf(r,
1234                        "      <httpd:timeout>%" APR_TIME_T_FMT "</httpd:timeout>",
1235                        apr_time_sec(balancer->s->timeout));
1236             if (balancer->s->max_attempts_set) {
1237                 ap_rprintf(r,
1238                            "      <httpd:maxattempts>%d</httpd:maxattempts>\n",
1239                            balancer->s->max_attempts);
1240             }
1241             ap_rvputs(r, "      <httpd:lbmethod>", balancer->lbmethod->name,
1242                       "</httpd:lbmethod>\n", NULL);
1243             if (balancer->s->sticky) {
1244                 ap_rprintf(r,
1245                            "      <httpd:scolonpathdelim>%s</httpd:scolonpathdelim>\n",
1246                            (balancer->s->scolonsep ? "On" : "Off"));
1247             }
1248             /* End proxy_balancer */
1249             ap_rputs("      <httpd:workers>\n", r);
1250             workers = (proxy_worker **)balancer->workers->elts;
1251             for (n = 0; n < balancer->workers->nelts; n++) {
1252                 worker = *workers;
1253                 /* Start proxy_worker */
1254                 ap_rputs("        <httpd:worker>\n", r);
1255                 ap_rvputs(r, "          <httpd:name>", worker->s->name,
1256                           "</httpd:name>\n", NULL);
1257                 ap_rvputs(r, "          <httpd:scheme>", worker->s->scheme,
1258                           "</httpd:scheme>\n", NULL);
1259                 ap_rvputs(r, "          <httpd:hostname>", worker->s->hostname,
1260                           "</httpd:hostname>\n", NULL);
1261                 ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
1262                           worker->s->lbfactor);
1263                 ap_rprintf(r,
1264                            "          <httpd:port>%d</httpd:port>\n",
1265                            worker->s->port);
1266                 ap_rprintf(r, "          <httpd:min>%d</httpd:min>\n",
1267                            worker->s->min);
1268                 ap_rprintf(r, "          <httpd:smax>%d</httpd:smax>\n",
1269                            worker->s->smax);
1270                 ap_rprintf(r, "          <httpd:max>%d</httpd:max>\n",
1271                            worker->s->hmax);
1272                 ap_rprintf(r,
1273                            "          <httpd:ttl>%" APR_TIME_T_FMT "</httpd:ttl>\n",
1274                            apr_time_sec(worker->s->ttl));
1275                 if (worker->s->timeout_set) {
1276                     ap_rprintf(r,
1277                                "          <httpd:timeout>%" APR_TIME_T_FMT "</httpd:timeout>\n",
1278                                apr_time_sec(worker->s->timeout));
1279                 }
1280                 if (worker->s->acquire_set) {
1281                     ap_rprintf(r,
1282                                "          <httpd:acquire>%" APR_TIME_T_FMT "</httpd:acquire>\n",
1283                                apr_time_msec(worker->s->acquire));
1284                 }
1285                 if (worker->s->recv_buffer_size_set) {
1286                     ap_rprintf(r,
1287                                "          <httpd:recv_buffer_size>%" APR_SIZE_T_FMT "</httpd:recv_buffer_size>\n",
1288                                worker->s->recv_buffer_size);
1289                 }
1290                 if (worker->s->io_buffer_size_set) {
1291                     ap_rprintf(r,
1292                                "          <httpd:io_buffer_size>%" APR_SIZE_T_FMT "</httpd:io_buffer_size>\n",
1293                                worker->s->io_buffer_size);
1294                 }
1295                 if (worker->s->keepalive_set) {
1296                     ap_rprintf(r,
1297                                "          <httpd:keepalive>%s</httpd:keepalive>\n",
1298                                (worker->s->keepalive ? "On" : "Off"));
1299                 }
1300                 /* Begin proxy_worker_stat */
1301                 ap_rputs("          <httpd:status>", r);
1302                 if (worker->s->status & PROXY_WORKER_DISABLED)
1303                     ap_rputs("Disabled", r);
1304                 else if (worker->s->status & PROXY_WORKER_IN_ERROR)
1305                     ap_rputs("Error", r);
1306                 else if (worker->s->status & PROXY_WORKER_STOPPED)
1307                     ap_rputs("Stopped", r);
1308                 else if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
1309                     ap_rputs("Standby", r);
1310                 else if (PROXY_WORKER_IS_USABLE(worker))
1311                     ap_rputs("OK", r);
1312                 else if (!PROXY_WORKER_IS_INITIALIZED(worker))
1313                     ap_rputs("Uninitialized", r);
1314                 ap_rputs("</httpd:status>\n", r);
1315                 if ((worker->s->error_time > 0) && apr_rfc822_date(date, worker->s->error_time) == APR_SUCCESS) {
1316                     ap_rvputs(r, "          <httpd:error_time>", date,
1317                               "</httpd:error_time>\n", NULL);
1318                 }
1319                 ap_rprintf(r,
1320                            "          <httpd:retries>%d</httpd:retries>\n",
1321                            worker->s->retries);
1322                 ap_rprintf(r,
1323                            "          <httpd:lbstatus>%d</httpd:lbstatus>\n",
1324                            worker->s->lbstatus);
1325                 ap_rprintf(r,
1326                            "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
1327                            worker->s->lbfactor);
1328                 ap_rprintf(r,
1329                            "          <httpd:transferred>%" APR_OFF_T_FMT "</httpd:transferred>\n",
1330                            worker->s->transferred);
1331                 ap_rprintf(r,
1332                            "          <httpd:read>%" APR_OFF_T_FMT "</httpd:read>\n",
1333                            worker->s->read);
1334                 ap_rprintf(r,
1335                            "          <httpd:elected>%" APR_SIZE_T_FMT "</httpd:elected>\n",
1336                            worker->s->elected);
1337                 ap_rvputs(r, "          <httpd:route>",
1338                           ap_escape_html(r->pool, worker->s->route),
1339                           "</httpd:route>\n", NULL);
1340                 ap_rvputs(r, "          <httpd:redirect>",
1341                           ap_escape_html(r->pool, worker->s->redirect),
1342                           "</httpd:redirect>\n", NULL);
1343                 ap_rprintf(r,
1344                            "          <httpd:busy>%" APR_SIZE_T_FMT "</httpd:busy>\n",
1345                            worker->s->busy);
1346                 ap_rprintf(r, "          <httpd:lbset>%d</httpd:lbset>\n",
1347                            worker->s->lbset);
1348                 /* End proxy_worker_stat */
1349                 if (!strcasecmp(worker->s->scheme, "ajp")) {
1350                     ap_rputs("          <httpd:flushpackets>", r);
1351                     switch (worker->s->flush_packets) {
1352                         case flush_off:
1353                             ap_rputs("Off", r);
1354                             break;
1355                         case flush_on:
1356                             ap_rputs("On", r);
1357                             break;
1358                         case flush_auto:
1359                             ap_rputs("Auto", r);
1360                             break;
1361                     }
1362                     ap_rputs("</httpd:flushpackets>\n", r);
1363                     if (worker->s->flush_packets == flush_auto) {
1364                         ap_rprintf(r,
1365                                    "          <httpd:flushwait>%d</httpd:flushwait>\n",
1366                                    worker->s->flush_wait);
1367                     }
1368                     if (worker->s->ping_timeout_set) {
1369                         ap_rprintf(r,
1370                                    "          <httpd:ping>%" APR_TIME_T_FMT "</httpd:ping>",
1371                                    apr_time_msec(worker->s->ping_timeout));
1372                     }
1373                 }
1374                 if (worker->s->disablereuse_set) {
1375                     ap_rprintf(r,
1376                                "      <httpd:disablereuse>%s</httpd:disablereuse>\n",
1377                                (worker->s->disablereuse ? "On" : "Off"));
1378                 }
1379                 if (worker->s->conn_timeout_set) {
1380                     ap_rprintf(r,
1381                                "          <httpd:connectiontimeout>%" APR_TIME_T_FMT "</httpd:connectiontimeout>\n",
1382                                apr_time_msec(worker->s->conn_timeout));
1383                 }
1384                 if (worker->s->retry_set) {
1385                     ap_rprintf(r,
1386                                "          <httpd:retry>%" APR_TIME_T_FMT "</httpd:retry>\n",
1387                                apr_time_sec(worker->s->retry));
1388                 }
1389                 ap_rputs("        </httpd:worker>\n", r);
1390                 ++workers;
1391             }
1392             ap_rputs("      </httpd:workers>\n", r);
1393             ap_rputs("    </httpd:balancer>\n", r);
1394             ++balancer;
1395         }
1396         ap_rputs("  </httpd:balancers>\n", r);
1397         ap_rputs("</httpd:manager>", r);
1398     }
1399     else {
1400         ap_set_content_type(r, "text/html; charset=ISO-8859-1");
1401         ap_rputs(DOCTYPE_HTML_3_2
1402                  "<html><head><title>Balancer Manager</title>\n", r);
1403         ap_rputs("<style type='text/css'>\n"
1404                  "table {\n"
1405                  " border-width: 1px;\n"
1406                  " border-spacing: 3px;\n"
1407                  " border-style: solid;\n"
1408                  " border-color: gray;\n"
1409                  " border-collapse: collapse;\n"
1410                  " background-color: white;\n"
1411                  " text-align: center;\n"
1412                  "}\n"
1413                  "th {\n"
1414                  " border-width: 1px;\n"
1415                  " padding: 2px;\n"
1416                  " border-style: dotted;\n"
1417                  " border-color: gray;\n"
1418                  " background-color: white;\n"
1419                  " text-align: center;\n"
1420                  "}\n"
1421                  "td {\n"
1422                  " border-width: 1px;\n"
1423                  " padding: 2px;\n"
1424                  " border-style: dotted;\n"
1425                  " border-color: gray;\n"
1426                  " background-color: white;\n"
1427                  " text-align: center;\n"
1428                  "}\n"
1429                  "</style>\n</head>\n", r);
1430         ap_rputs("<body><h1>Load Balancer Manager for ", r);
1431         ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
1432         ap_rvputs(r, "<dl><dt>Server Version: ",
1433                   ap_get_server_description(), "</dt>\n", NULL);
1434         ap_rvputs(r, "<dt>Server Built: ",
1435                   ap_get_server_built(), "\n</dt></dl>\n", NULL);
1436         balancer = (proxy_balancer *)conf->balancers->elts;
1437         for (i = 0; i < conf->balancers->nelts; i++) {
1438
1439             ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
1440             ap_rvputs(r, "<a href='", r->uri, "?b=",
1441                       balancer->s->name + sizeof(BALANCER_PREFIX) - 1,
1442                       "&nonce=", balancer->s->nonce,
1443                       "'>", NULL);
1444             ap_rvputs(r, balancer->s->name, "</a> [",balancer->s->sname, "]</h3>\n", NULL);
1445             ap_rputs("\n\n<table><tr>"
1446                 "<th>MaxMembers</th><th>StickySession</th><th>DisableFailover</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
1447                 "<th>Path</th><th>Active</th></tr>\n<tr>", r);
1448             /* the below is a safe cast, since the number of slots total will
1449              * never be more than max_workers, which is restricted to int */
1450             ap_rprintf(r, "<td>%d [%d Used]</td>\n", balancer->max_workers,
1451                        balancer->max_workers - (int)storage->num_free_slots(balancer->wslot));
1452             if (*balancer->s->sticky) {
1453                 if (strcmp(balancer->s->sticky, balancer->s->sticky_path)) {
1454                     ap_rvputs(r, "<td>", balancer->s->sticky, " | ",
1455                               balancer->s->sticky_path, NULL);
1456                 }
1457                 else {
1458                     ap_rvputs(r, "<td>", balancer->s->sticky, NULL);
1459                 }
1460             }
1461             else {
1462                 ap_rputs("<td> (None) ", r);
1463             }
1464             ap_rprintf(r, "<td>%s</td>\n",
1465                        balancer->s->sticky_force ? "On" : "Off");
1466             ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
1467                 apr_time_sec(balancer->s->timeout));
1468             ap_rprintf(r, "<td>%d</td>\n", balancer->s->max_attempts);
1469             ap_rprintf(r, "<td>%s</td>\n",
1470                        balancer->s->lbpname);
1471             ap_rputs("<td>", r);
1472             if (balancer->s->vhost && *(balancer->s->vhost)) {
1473                 ap_rvputs(r, balancer->s->vhost, " -> ", NULL);
1474             }
1475             ap_rvputs(r, balancer->s->vpath, "</td>\n", NULL);
1476             ap_rprintf(r, "<td>%s</td>\n",
1477                        !balancer->s->inactive ? "Yes" : "No");
1478             ap_rputs("</table>\n<br />", r);
1479             ap_rputs("\n\n<table><tr>"
1480                 "<th>Worker URL</th>"
1481                 "<th>Route</th><th>RouteRedir</th>"
1482                 "<th>Factor</th><th>Set</th><th>Status</th>"
1483                 "<th>Elected</th><th>Busy</th><th>Load</th><th>To</th><th>From</th>"
1484                 "</tr>\n", r);
1485
1486             workers = (proxy_worker **)balancer->workers->elts;
1487             for (n = 0; n < balancer->workers->nelts; n++) {
1488                 char fbuf[50];
1489                 worker = *workers;
1490                 ap_rvputs(r, "<tr>\n<td><a href='", r->uri, "?b=",
1491                           balancer->s->name + sizeof(BALANCER_PREFIX) - 1, "&w=",
1492                           ap_escape_uri(r->pool, worker->s->name),
1493                           "&nonce=", balancer->s->nonce,
1494                           "'>", NULL);
1495                 ap_rvputs(r, worker->s->name, "</a></td>", NULL);
1496                 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
1497                           NULL);
1498                 ap_rvputs(r, "</td><td>",
1499                           ap_escape_html(r->pool, worker->s->redirect), NULL);
1500                 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
1501                 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
1502                 ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, worker), NULL);
1503                 ap_rputs("</td>", r);
1504                 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->elected);
1505                 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->busy);
1506                 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbstatus);
1507                 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
1508                 ap_rputs("</td><td>", r);
1509                 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
1510                 ap_rputs("</td></tr>\n", r);
1511
1512                 ++workers;
1513             }
1514             ap_rputs("</table>\n", r);
1515             ++balancer;
1516         }
1517         ap_rputs("<hr />\n", r);
1518         if (wsel && bsel) {
1519             ap_rputs("<h3>Edit worker settings for ", r);
1520             ap_rvputs(r, wsel->s->name, "</h3>\n", NULL);
1521             ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1522             ap_rvputs(r, action, "'>\n", NULL);
1523             ap_rputs("<dl>\n<table><tr><td>Load factor:</td><td><input name='w_lf' id='w_lf' type=text ", r);
1524             ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbfactor);
1525             ap_rputs("<tr><td>LB Set:</td><td><input name='w_ls' id='w_ls' type=text ", r);
1526             ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbset);
1527             ap_rputs("<tr><td>Route:</td><td><input name='w_wr' id='w_wr' type=text ", r);
1528             ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->route),
1529                       NULL);
1530             ap_rputs("'></td></tr>\n", r);
1531             ap_rputs("<tr><td>Route Redirect:</td><td><input name='w_rr' id='w_rr' type=text ", r);
1532             ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->redirect),
1533                       NULL);
1534             ap_rputs("'></td></tr>\n", r);
1535             ap_rputs("<tr><td>Status:</td>", r);
1536             ap_rputs("<td><table><tr><th>Ign</th><th>Drn</th><th>Dis</th><th>Stby</th></tr>\n<tr>", r);
1537             create_radio("w_status_I", (PROXY_WORKER_IGNORE_ERRORS & wsel->s->status), r);
1538             create_radio("w_status_N", (PROXY_WORKER_DRAIN & wsel->s->status), r);
1539             create_radio("w_status_D", (PROXY_WORKER_DISABLED & wsel->s->status), r);
1540             create_radio("w_status_H", (PROXY_WORKER_HOT_STANDBY & wsel->s->status), r);
1541             ap_rputs("</tr></table>\n", r);
1542             ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1543             ap_rvputs(r, "</table>\n<input type=hidden name='w' id='w' ",  NULL);
1544             ap_rvputs(r, "value='", ap_escape_uri(r->pool, wsel->s->name), "'>\n", NULL);
1545             ap_rvputs(r, "<input type=hidden name='b' id='b' ", NULL);
1546             ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1547                       "'>\n", NULL);
1548             ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1549                       bsel->s->nonce, "'>\n", NULL);
1550             ap_rvputs(r, "</form>\n", NULL);
1551             ap_rputs("<hr />\n", r);
1552         } else if (bsel) {
1553             const apr_array_header_t *provs;
1554             const ap_list_provider_names_t *pname;
1555             int i;
1556             ap_rputs("<h3>Edit balancer settings for ", r);
1557             ap_rvputs(r, bsel->s->name, "</h3>\n", NULL);
1558             ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1559             ap_rvputs(r, action, "'>\n", NULL);
1560             ap_rputs("<dl>\n<table>\n", r);
1561             provs = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
1562             if (provs) {
1563                 ap_rputs("<tr><td>LBmethod:</td>", r);
1564                 ap_rputs("<td>\n<select name='b_lbm' id='b_lbm'>", r);
1565                 pname = (ap_list_provider_names_t *)provs->elts;
1566                 for (i = 0; i < provs->nelts; i++, pname++) {
1567                     ap_rvputs(r,"<option value='", pname->provider_name, "'", NULL);
1568                     if (strcmp(pname->provider_name, bsel->s->lbpname) == 0)
1569                         ap_rputs(" selected ", r);
1570                     ap_rvputs(r, ">", pname->provider_name, "\n", NULL);
1571                 }
1572                 ap_rputs("</select>\n</td></tr>\n", r);
1573             }
1574             ap_rputs("<tr><td>Timeout:</td><td><input name='b_tmo' id='b_tmo' type=text ", r);
1575             ap_rprintf(r, "value='%" APR_TIME_T_FMT "'></td></tr>\n", apr_time_sec(bsel->s->timeout));
1576             ap_rputs("<tr><td>Failover Attempts:</td><td><input name='b_max' id='b_max' type=text ", r);
1577             ap_rprintf(r, "value='%d'></td></tr>\n", bsel->s->max_attempts);
1578             ap_rputs("<tr><td>Disable Failover:</td>", r);
1579             create_radio("b_sforce", bsel->s->sticky_force, r);
1580             ap_rputs("<tr><td>Sticky Session:</td><td><input name='b_ss' id='b_ss' size=64 type=text ", r);
1581             if (strcmp(bsel->s->sticky, bsel->s->sticky_path)) {
1582                 ap_rvputs(r, "value ='", bsel->s->sticky, " | ",
1583                           bsel->s->sticky_path, NULL);
1584             }
1585             else {
1586                 ap_rvputs(r, "value ='", bsel->s->sticky, NULL);
1587             }
1588             ap_rputs("'>&nbsp;&nbsp;&nbsp;&nbsp;(Use '-' to delete)</td></tr>\n", r);
1589             if (storage->num_free_slots(bsel->wslot) != 0) {
1590                 ap_rputs("<tr><td>Add New Worker:</td><td><input name='b_nwrkr' id='b_nwrkr' size=32 type=text>"
1591                          "&nbsp;&nbsp;&nbsp;&nbsp;Are you sure? <input name='b_wyes' id='b_wyes' type=checkbox value='1'>"
1592                          "</td></tr>", r);
1593             }
1594             ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1595             ap_rvputs(r, "</table>\n<input type=hidden name='b' id='b' ", NULL);
1596             ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1597                       "'>\n", NULL);
1598             ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1599                       bsel->s->nonce, "'>\n", NULL);
1600             ap_rvputs(r, "</form>\n", NULL);
1601             ap_rputs("<hr />\n", r);
1602         }
1603         ap_rputs(ap_psignature("",r), r);
1604         ap_rputs("</body></html>\n", r);
1605         ap_rflush(r);
1606     }
1607     return DONE;
1608 }
1609
1610 static void balancer_child_init(apr_pool_t *p, server_rec *s)
1611 {
1612     while (s) {
1613         proxy_balancer *balancer;
1614         int i;
1615         void *sconf = s->module_config;
1616         proxy_server_conf *conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
1617         apr_status_t rv;
1618
1619         if (conf->balancers->nelts) {
1620             apr_size_t size;
1621             unsigned int num;
1622             storage->attach(&(conf->bslot), conf->id, &size, &num, p);
1623             if (!conf->bslot) {
1624                 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01205) "slotmem_attach failed");
1625                 exit(1); /* Ugly, but what else? */
1626             }
1627         }
1628
1629         balancer = (proxy_balancer *)conf->balancers->elts;
1630         for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
1631             rv = ap_proxy_initialize_balancer(balancer, s, p);
1632
1633             if (rv != APR_SUCCESS) {
1634                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(01206)
1635                              "Failed to init balancer %s in child",
1636                              balancer->s->name);
1637                 exit(1); /* Ugly, but what else? */
1638             }
1639             init_balancer_members(conf->pool, s, balancer);
1640         }
1641         s = s->next;
1642     }
1643
1644 }
1645
1646 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
1647 {
1648     /* Only the mpm_winnt has child init hook handler.
1649      * make sure that we are called after the mpm
1650      * initializes
1651      */
1652     static const char *const aszPred[] = { "mpm_winnt.c", "mod_slotmem_shm.c", NULL};
1653      /* manager handler */
1654     ap_hook_post_config(balancer_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1655     ap_hook_pre_config(balancer_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1656     ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1657     ap_hook_child_init(balancer_child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1658     proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1659     proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1660     proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1661 }
1662
1663 AP_DECLARE_MODULE(proxy_balancer) = {
1664     STANDARD20_MODULE_STUFF,
1665     NULL,       /* create per-directory config structure */
1666     NULL,       /* merge per-directory config structures */
1667     NULL,       /* create per-server config structure */
1668     NULL,       /* merge per-server config structures */
1669     NULL,       /* command apr_table_t */
1670     ap_proxy_balancer_register_hook /* register hooks */
1671 };