2 /* Copyright 1999-2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "mod_proxy.h"
21 #include "apr_optional.h"
22 #include "mod_status.h"
24 #if (MODULE_MAGIC_NUMBER_MAJOR > 20020903)
27 APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
28 APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
32 #define MAX(x,y) ((x) >= (y) ? (x) : (y))
36 * A Web proxy module. Stages:
38 * translate_name: set filename to proxy:<URL>
39 * map_to_storage: run proxy_walk (rather than directory_walk/file_walk)
40 * can't trust directory_walk/file_walk since these are
41 * not in our filesystem. Prevents mod_http from serving
42 * the TRACE request we will set aside to handle later.
43 * type_checker: set type to PROXY_MAGIC_TYPE if filename begins proxy:
44 * fix_ups: convert the URL stored in the filename to the
46 * handler: handle proxy requests
49 /* -------------------------------------------------------------- */
50 /* Translate the URL into a 'filename' */
53 /* XXX: EBCDIC safe? --nd */
54 #define x2c(x) (((x >= '0') && (x <= '9')) \
56 : (((x >= 'a') && (x <= 'f')) \
58 : ((x >= 'A') && (x <='F')) \
64 static unsigned char hex2c(const char* p) {
66 const char c2 = p[1] ? p[2]: '\0';
67 int i1 = c1 ? x2c(c1) : 0;
68 int i2 = c2 ? x2c(c2) : 0;
69 unsigned char ret = (i1 << 4) | i2;
75 #define PROXY_COPY_CONF_PARAMS(w, c) \
77 (w)->timeout = (c)->timeout; \
78 (w)->timeout_set = (c)->timeout_set; \
79 (w)->recv_buffer_size = (c)->recv_buffer_size; \
80 (w)->recv_buffer_size_set = (c)->recv_buffer_size_set; \
81 (w)->io_buffer_size = (c)->io_buffer_size; \
82 (w)->io_buffer_size_set = (c)->io_buffer_size_set; \
85 static const char *set_worker_param(apr_pool_t *p,
92 if (!strcasecmp(key, "loadfactor")) {
93 worker->lbfactor = atoi(val);
94 if (worker->lbfactor < 1 || worker->lbfactor > 100)
95 return "LoadFactor must be number between 1..100";
97 else if (!strcasecmp(key, "retry")) {
100 return "Retry must be at least one second";
101 worker->retry = apr_time_from_sec(ival);
103 else if (!strcasecmp(key, "ttl")) {
106 return "TTL must be at least one second";
107 worker->ttl = apr_time_from_sec(ival);
109 else if (!strcasecmp(key, "min")) {
112 return "Min must be a positive number";
115 else if (!strcasecmp(key, "max")) {
118 return "Max must be a positive number";
121 /* XXX: More inteligent naming needed */
122 else if (!strcasecmp(key, "smax")) {
125 return "Smax must be a positive number";
128 else if (!strcasecmp(key, "acquire")) {
131 return "Acquire must be at least one mili second";
132 worker->acquire = apr_time_make(0, ival * 1000);
133 worker->acquire_set = 1;
135 else if (!strcasecmp(key, "timeout")) {
138 return "Timeout must be at least one second";
139 worker->timeout = apr_time_from_sec(ival);
140 worker->timeout_set = 1;
142 else if (!strcasecmp(key, "iobuffersize")) {
144 worker->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
145 worker->io_buffer_size_set = 1;
147 else if (!strcasecmp(key, "receivebuffersize")) {
149 if (ival < 512 && ival != 0) {
150 return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
152 worker->recv_buffer_size = ival;
153 worker->recv_buffer_size_set = 1;
155 else if (!strcasecmp(key, "keepalive")) {
156 if (!strcasecmp(val, "on"))
157 worker->keepalive = 1;
158 else if (!strcasecmp(val, "off"))
159 worker->keepalive = 0;
161 return "KeepAlive must be On|Off";
162 worker->keepalive_set = 1;
164 else if (!strcasecmp(key, "route")) {
165 worker->route = apr_pstrdup(p, val);
167 else if (!strcasecmp(key, "redirect")) {
168 worker->redirect = apr_pstrdup(p, val);
171 return "unknown Worker parameter";
176 static const char *set_balancer_param(apr_pool_t *p,
177 proxy_balancer *balancer,
183 if (!strcasecmp(key, "stickysession")) {
184 balancer->sticky = apr_pstrdup(p, val);
186 else if (!strcasecmp(key, "nofailover")) {
187 if (!strcasecmp(val, "on"))
188 balancer->sticky_force = 1;
189 else if (!strcasecmp(val, "off"))
190 balancer->sticky_force = 0;
192 return "failover must be On|Off";
194 else if (!strcasecmp(key, "timeout")) {
197 return "timeout must be at least one second";
198 balancer->timeout = apr_time_from_sec(ival);
201 return "unknown Balancer parameter";
206 static int alias_match(const char *uri, const char *alias_fakename)
208 const char *end_fakename = alias_fakename + strlen(alias_fakename);
209 const char *aliasp = alias_fakename, *urip = uri;
210 const char *end_uri = uri + strlen(uri);
211 unsigned char uric, aliasc;
213 while (aliasp < end_fakename && urip < end_uri) {
214 if (*aliasp == '/') {
215 /* any number of '/' in the alias matches any number in
216 * the supplied URI, but there must be at least one...
221 while (*aliasp == '/')
228 /* Other characters are compared literally */
229 if (*urip++ != *aliasp++)
232 /* Other characters are canonicalised and compared literally */
237 uric = (unsigned char)*urip++;
239 if (*aliasp == '%') {
240 aliasc = hex2c(aliasp);
243 aliasc = (unsigned char)*aliasp++;
245 if (uric != aliasc) {
252 /* fixup badly encoded stuff (e.g. % as last character) */
253 if (aliasp > end_fakename) {
254 aliasp = end_fakename;
256 if (urip > end_uri) {
260 /* We reach the end of the uri before the end of "alias_fakename"
261 * for example uri is "/" and alias_fakename "/examples"
263 if (urip == end_uri && aliasp!=end_fakename) {
267 /* Check last alias path component matched all the way */
268 if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
271 /* Return number of characters from URI which matched (may be
272 * greater than length of alias, since we may have matched
279 /* Detect if an absoluteURI should be proxied or not. Note that we
280 * have to do this during this phase because later phases are
281 * "short-circuiting"... i.e. translate_names will end when the first
282 * module returns OK. So for example, if the request is something like:
284 * GET http://othervhost/cgi-bin/printenv HTTP/1.0
286 * mod_alias will notice the /cgi-bin part and ScriptAlias it and
287 * short-circuit the proxy... just because of the ordering in the
288 * configuration file.
290 static int proxy_detect(request_rec *r)
292 void *sconf = r->server->module_config;
293 proxy_server_conf *conf =
294 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
297 struct proxy_alias *ent = (struct proxy_alias *)conf->aliases->elts;
300 /* Ick... msvc (perhaps others) promotes ternary short results to int */
302 if (conf->req && r->parsed_uri.scheme) {
303 /* but it might be something vhosted */
304 if (!(r->parsed_uri.hostname
305 && !strcasecmp(r->parsed_uri.scheme, ap_http_method(r))
306 && ap_matches_request_vhost(r, r->parsed_uri.hostname,
307 (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
308 : ap_default_port(r))))) {
309 r->proxyreq = PROXYREQ_PROXY;
310 r->uri = r->unparsed_uri;
311 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
312 r->handler = "proxy-server";
315 /* We need special treatment for CONNECT proxying: it has no scheme part */
316 else if (conf->req && r->method_number == M_CONNECT
317 && r->parsed_uri.hostname
318 && r->parsed_uri.port_str) {
319 r->proxyreq = PROXYREQ_PROXY;
320 r->uri = r->unparsed_uri;
321 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
322 r->handler = "proxy-server";
325 /* test for a ProxyPass */
326 for (i = 0; i < conf->aliases->nelts; i++) {
327 len = alias_match(r->unparsed_uri, ent[i].fake);
329 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
330 r->unparsed_uri + len, NULL);
331 r->handler = "proxy-server";
332 r->proxyreq = PROXYREQ_REVERSE;
333 r->uri = r->unparsed_uri;
342 static int proxy_trans(request_rec *r)
345 void *sconf = r->server->module_config;
346 proxy_server_conf *conf =
347 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
349 struct proxy_alias *ent = (struct proxy_alias *) conf->aliases->elts;
353 /* someone has already set up the proxy, it was possibly ourselves
360 /* XXX: since r->uri has been manipulated already we're not really
361 * compliant with RFC1945 at this point. But this probably isn't
362 * an issue because this is a hybrid proxy/origin server.
365 for (i = 0; i < conf->aliases->nelts; i++) {
366 len = alias_match(r->uri, ent[i].fake);
369 if ((ent[i].real[0] == '!') && (ent[i].real[1] == 0)) {
373 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
375 r->handler = "proxy-server";
376 r->proxyreq = PROXYREQ_REVERSE;
384 static int proxy_walk(request_rec *r)
386 proxy_server_conf *sconf = ap_get_module_config(r->server->module_config,
388 ap_conf_vector_t *per_dir_defaults = r->server->lookup_defaults;
389 ap_conf_vector_t **sec_proxy = (ap_conf_vector_t **) sconf->sec_proxy->elts;
390 ap_conf_vector_t *entry_config;
391 proxy_dir_conf *entry_proxy;
392 int num_sec = sconf->sec_proxy->nelts;
393 /* XXX: shouldn't we use URI here? Canonicalize it first?
394 * Pass over "proxy:" prefix
396 const char *proxyname = r->filename + 6;
399 for (j = 0; j < num_sec; ++j)
401 entry_config = sec_proxy[j];
402 entry_proxy = ap_get_module_config(entry_config, &proxy_module);
404 /* XXX: What about case insensitive matching ???
405 * Compare regex, fnmatch or string as appropriate
406 * If the entry doesn't relate, then continue
409 ? ap_regexec(entry_proxy->r, proxyname, 0, NULL, 0)
410 : (entry_proxy->p_is_fnmatch
411 ? apr_fnmatch(entry_proxy->p, proxyname, 0)
412 : strncmp(proxyname, entry_proxy->p,
413 strlen(entry_proxy->p)))) {
416 per_dir_defaults = ap_merge_per_dir_configs(r->pool, per_dir_defaults,
420 r->per_dir_config = per_dir_defaults;
425 static int proxy_map_location(request_rec *r)
429 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
432 /* Don't let the core or mod_http map_to_storage hooks handle this,
433 * We don't need directory/file_walk, and we want to TRACE on our own.
435 if ((access_status = proxy_walk(r))) {
436 ap_die(access_status, r);
437 return access_status;
443 /* -------------------------------------------------------------- */
444 /* Fixup the filename */
447 * Canonicalise the URL
449 static int proxy_fixup(request_rec *r)
454 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
458 /* We definitely shouldn't canonicalize a proxy_pass.
459 * But should we really canonicalize a STD_PROXY??? -- Fahree
461 if (r->proxyreq == PROXYREQ_REVERSE) {
466 /* XXX: Shouldn't we try this before we run the proxy_walk? */
467 url = &r->filename[6];
469 /* canonicalise each specific scheme */
470 if ((access_status = proxy_run_canon_handler(r, url))) {
471 return access_status;
474 p = strchr(url, ':');
475 if (p == NULL || p == url)
476 return HTTP_BAD_REQUEST;
478 return OK; /* otherwise; we've done the best we can */
481 /* Send a redirection if the request contains a hostname which is not */
482 /* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
483 /* servers like Netscape's allow this and access hosts from the local */
484 /* domain in this case. I think it is better to redirect to a FQDN, since */
485 /* these will later be found in the bookmarks files. */
486 /* The "ProxyDomain" directive determines what domain will be appended */
487 static int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
492 /* We only want to worry about GETs */
493 if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
496 /* If host does contain a dot already, or it is "localhost", decline */
497 if (strchr(r->parsed_uri.hostname, '.') != NULL
498 || strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
499 return DECLINED; /* host name has a dot already */
501 ref = apr_table_get(r->headers_in, "Referer");
503 /* Reassemble the request, but insert the domain after the host name */
504 /* Note that the domain name always starts with a dot */
505 r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
507 nuri = apr_uri_unparse(r->pool,
509 APR_URI_UNP_REVEALPASSWORD);
511 apr_table_set(r->headers_out, "Location", nuri);
512 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
513 "Domain missing: %s sent to %s%s%s", r->uri,
514 apr_uri_unparse(r->pool, &r->parsed_uri,
515 APR_URI_UNP_OMITUSERINFO),
516 ref ? " from " : "", ref ? ref : "");
518 return HTTP_MOVED_PERMANENTLY;
521 /* -------------------------------------------------------------- */
524 static int proxy_handler(request_rec *r)
526 char *url, *scheme, *p;
528 void *sconf = r->server->module_config;
529 proxy_server_conf *conf = (proxy_server_conf *)
530 ap_get_module_config(sconf, &proxy_module);
531 apr_array_header_t *proxies = conf->proxies;
532 struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
533 int i, rc, access_status;
534 int direct_connect = 0;
537 proxy_balancer *balancer = NULL;
538 proxy_worker *worker = NULL;
540 /* is this for us? */
541 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
544 /* handle max-forwards / OPTIONS / TRACE */
545 if ((str = apr_table_get(r->headers_in, "Max-Forwards"))) {
546 maxfwd = strtol(str, NULL, 10);
548 switch (r->method_number) {
551 r->proxyreq = PROXYREQ_NONE;
552 if ((access_status = ap_send_http_trace(r)))
553 ap_die(access_status, r);
555 ap_finalize_request_protocol(r);
560 r->proxyreq = PROXYREQ_NONE;
561 if ((access_status = ap_send_http_options(r)))
562 ap_die(access_status, r);
564 ap_finalize_request_protocol(r);
568 return ap_proxyerror(r, HTTP_BAD_GATEWAY,
569 "Max-Forwards has reached zero - proxy loop?");
573 maxfwd = (maxfwd > 0) ? maxfwd - 1 : 0;
576 /* set configured max-forwards */
577 maxfwd = conf->maxfwd;
579 apr_table_set(r->headers_in, "Max-Forwards",
580 apr_psprintf(r->pool, "%ld", (maxfwd > 0) ? maxfwd : 0));
582 url = r->filename + 6;
583 p = strchr(url, ':');
585 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
586 "proxy_handler no URL in %s", r->filename);
587 return HTTP_BAD_REQUEST;
590 /* If the host doesn't have a domain name, add one and redirect. */
591 if (conf->domain != NULL) {
592 rc = proxy_needsdomain(r, url, conf->domain);
593 if (ap_is_HTTP_REDIRECT(rc))
594 return HTTP_MOVED_PERMANENTLY;
598 scheme = apr_pstrdup(r->pool, url);
601 /* Check URI's destination host against NoProxy hosts */
602 /* Bypass ProxyRemote server lookup if configured as NoProxy */
603 /* we only know how to handle communication to a proxy via http */
604 /*if (strcasecmp(scheme, "http") == 0) */
607 struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
609 for (direct_connect = ii = 0; ii < conf->dirconn->nelts && !direct_connect; ii++) {
610 direct_connect = list[ii].matcher(&list[ii], r);
613 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
614 (direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
619 /* Try to obtain the most suitable worker */
620 access_status = ap_proxy_pre_request(&worker, &balancer, r, conf, &url);
621 if (access_status != OK)
622 return access_status;
624 /* firstly, try a proxy, unless a NoProxy directive is active */
625 if (!direct_connect) {
626 for (i = 0; i < proxies->nelts; i++) {
627 p2 = ap_strchr_c(ents[i].scheme, ':'); /* is it a partial URL? */
628 if (strcmp(ents[i].scheme, "*") == 0 ||
629 (ents[i].use_regex && ap_regexec(ents[i].regexp, url, 0,NULL, 0)) ||
630 (p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
632 strncasecmp(url, ents[i].scheme, strlen(ents[i].scheme)) == 0)) {
634 /* handle the scheme */
635 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
636 "Trying to run scheme_handler against proxy");
637 access_status = proxy_run_scheme_handler(r, worker, conf, url, ents[i].hostname, ents[i].port);
639 /* an error or success */
640 if (access_status != DECLINED && access_status != HTTP_BAD_GATEWAY) {
643 /* we failed to talk to the upstream proxy */
648 /* otherwise, try it direct */
649 /* N.B. what if we're behind a firewall, where we must use a proxy or
653 /* handle the scheme */
654 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
655 "Trying to run scheme_handler");
656 access_status = proxy_run_scheme_handler(r, worker, conf, url, NULL, 0);
657 if (DECLINED == access_status) {
658 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
659 "proxy: No protocol handler was valid for the URL %s. "
660 "If you are using a DSO version of mod_proxy, make sure "
661 "the proxy submodules are included in the configuration "
662 "using LoadModule.", r->uri);
663 access_status = HTTP_FORBIDDEN;
669 int post_status = proxy_run_post_request(worker, balancer, r, conf);
670 if (post_status == DECLINED) {
671 post_status = OK; /* no post_request handler available */
672 /* TODO: reclycle direct worker */
675 return access_status;
678 /* -------------------------------------------------------------- */
679 /* Setup configurable data */
681 static void * create_proxy_config(apr_pool_t *p, server_rec *s)
683 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
685 ps->sec_proxy = apr_array_make(p, 10, sizeof(ap_conf_vector_t *));
686 ps->proxies = apr_array_make(p, 10, sizeof(struct proxy_remote));
687 ps->aliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
688 ps->raliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
689 ps->cookie_paths = apr_array_make(p, 10, sizeof(struct proxy_alias));
690 ps->cookie_domains = apr_array_make(p, 10, sizeof(struct proxy_alias));
691 ps->cookie_path_str = apr_strmatch_precompile(p, "path=", 0);
692 ps->cookie_domain_str = apr_strmatch_precompile(p, "domain=", 0);
693 ps->noproxies = apr_array_make(p, 10, sizeof(struct noproxy_entry));
694 ps->dirconn = apr_array_make(p, 10, sizeof(struct dirconn_entry));
695 ps->allowed_connect_ports = apr_array_make(p, 10, sizeof(int));
696 ps->workers = apr_array_make(p, 10, sizeof(proxy_worker));
697 ps->balancers = apr_array_make(p, 10, sizeof(proxy_balancer));
699 ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
700 ps->viaopt_set = 0; /* 0 means default */
703 ps->recv_buffer_size = 0; /* this default was left unset for some reason */
704 ps->recv_buffer_size_set = 0;
705 ps->io_buffer_size = AP_IOBUFSIZE;
706 ps->io_buffer_size_set = 0;
707 ps->maxfwd = DEFAULT_MAX_FORWARDS;
709 ps->error_override = 0;
710 ps->error_override_set = 0;
711 ps->preserve_host_set = 0;
712 ps->preserve_host = 0;
715 ps->badopt = bad_error;
721 static void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
723 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
724 proxy_server_conf *base = (proxy_server_conf *) basev;
725 proxy_server_conf *overrides = (proxy_server_conf *) overridesv;
727 ps->proxies = apr_array_append(p, base->proxies, overrides->proxies);
728 ps->sec_proxy = apr_array_append(p, base->sec_proxy, overrides->sec_proxy);
729 ps->aliases = apr_array_append(p, base->aliases, overrides->aliases);
730 ps->raliases = apr_array_append(p, base->raliases, overrides->raliases);
732 = apr_array_append(p, base->cookie_paths, overrides->cookie_paths);
734 = apr_array_append(p, base->cookie_domains, overrides->cookie_domains);
735 ps->cookie_path_str = base->cookie_path_str;
736 ps->cookie_domain_str = base->cookie_domain_str;
737 ps->noproxies = apr_array_append(p, base->noproxies, overrides->noproxies);
738 ps->dirconn = apr_array_append(p, base->dirconn, overrides->dirconn);
739 ps->allowed_connect_ports = apr_array_append(p, base->allowed_connect_ports, overrides->allowed_connect_ports);
740 ps->workers = apr_array_append(p, base->workers, overrides->workers);
741 ps->balancers = apr_array_append(p, base->balancers, overrides->balancers);
743 ps->domain = (overrides->domain == NULL) ? base->domain : overrides->domain;
744 ps->viaopt = (overrides->viaopt_set == 0) ? base->viaopt : overrides->viaopt;
745 ps->req = (overrides->req_set == 0) ? base->req : overrides->req;
746 ps->recv_buffer_size = (overrides->recv_buffer_size_set == 0) ? base->recv_buffer_size : overrides->recv_buffer_size;
747 ps->io_buffer_size = (overrides->io_buffer_size_set == 0) ? base->io_buffer_size : overrides->io_buffer_size;
748 ps->maxfwd = (overrides->maxfwd_set == 0) ? base->maxfwd : overrides->maxfwd;
749 ps->error_override = (overrides->error_override_set == 0) ? base->error_override : overrides->error_override;
750 ps->preserve_host = (overrides->preserve_host_set == 0) ? base->preserve_host : overrides->preserve_host;
751 ps->timeout= (overrides->timeout_set == 0) ? base->timeout : overrides->timeout;
752 ps->badopt = (overrides->badopt_set == 0) ? base->badopt : overrides->badopt;
757 static void *create_proxy_dir_config(apr_pool_t *p, char *dummy)
759 proxy_dir_conf *new =
760 (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
762 /* Filled in by proxysection, when applicable */
767 static void *merge_proxy_dir_config(apr_pool_t *p, void *basev, void *addv)
769 proxy_dir_conf *new = (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
770 proxy_dir_conf *add = (proxy_dir_conf *) addv;
773 new->p_is_fnmatch = add->p_is_fnmatch;
780 add_proxy(cmd_parms *cmd, void *dummy, const char *f1, const char *r1, int regex)
782 server_rec *s = cmd->server;
783 proxy_server_conf *conf =
784 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
785 struct proxy_remote *new;
787 char *r, *f, *scheme;
791 r = apr_pstrdup(cmd->pool, r1);
792 scheme = apr_pstrdup(cmd->pool, r1);
793 f = apr_pstrdup(cmd->pool, f1);
795 if (p == NULL || p[1] != '/' || p[2] != '/' || p[3] == '\0') {
797 return "ProxyRemoteMatch: Bad syntax for a remote proxy server";
799 return "ProxyRemote: Bad syntax for a remote proxy server";
804 q = strchr(p + 3, ':');
806 if (sscanf(q + 1, "%u", &port) != 1 || port > 65535) {
808 return "ProxyRemoteMatch: Bad syntax for a remote proxy server (bad port number)";
810 return "ProxyRemote: Bad syntax for a remote proxy server (bad port number)";
818 reg = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
820 return "Regular expression for ProxyRemoteMatch could not be compiled.";
823 if (strchr(f, ':') == NULL)
824 ap_str_tolower(f); /* lowercase scheme */
825 ap_str_tolower(p + 3); /* lowercase hostname */
828 port = apr_uri_port_of_scheme(scheme);
831 new = apr_array_push(conf->proxies);
834 new->hostname = p + 3;
837 new->use_regex = regex;
842 add_proxy_noregex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
844 return add_proxy(cmd, dummy, f1, r1, 0);
848 add_proxy_regex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
850 return add_proxy(cmd, dummy, f1, r1, 1);
854 add_pass(cmd_parms *cmd, void *dummy, const char *arg)
856 server_rec *s = cmd->server;
857 proxy_server_conf *conf =
858 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
859 struct proxy_alias *new;
863 apr_table_t *params = apr_table_make(cmd->pool, 5);
864 const apr_array_header_t *arr;
865 const apr_table_entry_t *elts;
869 word = ap_getword_conf(cmd->pool, &arg);
875 char *val = strchr(word, '=');
878 return "Invalid ProxyPass parameter. Parameter must be "
879 "in the form 'key=value'";
881 return "ProxyPass can not have a path when defined in a location";
885 apr_table_setn(params, word, val);
890 return "ProxyPass needs a path when not defined in a location";
892 new = apr_array_push(conf->aliases);
893 new->fake = apr_pstrdup(cmd->pool, f);
894 new->real = apr_pstrdup(cmd->pool, r);
896 arr = apr_table_elts(params);
897 elts = (const apr_table_entry_t *)arr->elts;
898 /* Distinguish the balancer from woker */
899 if (strncasecmp(r, "balancer:", 9) == 0) {
900 proxy_balancer *balancer = ap_proxy_get_balancer(cmd->pool, conf, r);
902 const char *err = ap_proxy_add_balancer(&balancer,
906 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
908 for (i = 0; i < arr->nelts; i++) {
909 const char *err = set_balancer_param(cmd->pool, balancer, elts[i].key,
912 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
916 proxy_worker *worker = ap_proxy_get_worker(cmd->temp_pool, conf, r);
918 const char *err = ap_proxy_add_worker(&worker, cmd->pool, conf, r);
920 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
922 PROXY_COPY_CONF_PARAMS(worker, conf);
924 for (i = 0; i < arr->nelts; i++) {
925 const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
928 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
935 add_pass_reverse(cmd_parms *cmd, void *dummy, const char *f, const char *r)
937 server_rec *s = cmd->server;
938 proxy_server_conf *conf;
939 struct proxy_alias *new;
941 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
943 if (r!=NULL && cmd->path == NULL ) {
944 new = apr_array_push(conf->raliases);
947 } else if (r==NULL && cmd->path != NULL) {
948 new = apr_array_push(conf->raliases);
949 new->fake = cmd->path;
953 return "ProxyPassReverse needs a path when not defined in a location";
955 return "ProxyPassReverse can not have a path when defined in a location";
961 cookie_path(cmd_parms *cmd, void *dummy, const char *f, const char *r)
963 server_rec *s = cmd->server;
964 proxy_server_conf *conf;
965 struct proxy_alias *new;
967 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
969 new = apr_array_push(conf->cookie_paths);
976 cookie_domain(cmd_parms *cmd, void *dummy, const char *f, const char *r)
978 server_rec *s = cmd->server;
979 proxy_server_conf *conf;
980 struct proxy_alias *new;
982 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
984 new = apr_array_push(conf->cookie_domains);
992 set_proxy_exclude(cmd_parms *parms, void *dummy, const char *arg)
994 server_rec *s = parms->server;
995 proxy_server_conf *conf =
996 ap_get_module_config(s->module_config, &proxy_module);
997 struct noproxy_entry *new;
998 struct noproxy_entry *list = (struct noproxy_entry *) conf->noproxies->elts;
999 struct apr_sockaddr_t *addr;
1003 /* Don't duplicate entries */
1004 for (i = 0; i < conf->noproxies->nelts; i++) {
1005 if (apr_strnatcasecmp(arg, list[i].name) == 0) { /* ignore case for host names */
1011 new = apr_array_push(conf->noproxies);
1013 if (APR_SUCCESS == apr_sockaddr_info_get(&addr, new->name, APR_UNSPEC, 0, 0, parms->pool)) {
1024 * Set the ports CONNECT can use
1027 set_allowed_ports(cmd_parms *parms, void *dummy, const char *arg)
1029 server_rec *s = parms->server;
1030 proxy_server_conf *conf =
1031 ap_get_module_config(s->module_config, &proxy_module);
1034 if (!apr_isdigit(arg[0]))
1035 return "AllowCONNECT: port number must be numeric";
1037 New = apr_array_push(conf->allowed_connect_ports);
1042 /* Similar to set_proxy_exclude(), but defining directly connected hosts,
1043 * which should never be accessed via the configured ProxyRemote servers
1046 set_proxy_dirconn(cmd_parms *parms, void *dummy, const char *arg)
1048 server_rec *s = parms->server;
1049 proxy_server_conf *conf =
1050 ap_get_module_config(s->module_config, &proxy_module);
1051 struct dirconn_entry *New;
1052 struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
1056 /* Don't duplicate entries */
1057 for (i = 0; i < conf->dirconn->nelts; i++) {
1058 if (strcasecmp(arg, list[i].name) == 0)
1063 New = apr_array_push(conf->dirconn);
1064 New->name = apr_pstrdup(parms->pool, arg);
1065 New->hostaddr = NULL;
1067 if (ap_proxy_is_ipaddr(New, parms->pool)) {
1069 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1070 "Parsed addr %s", inet_ntoa(New->addr));
1071 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1072 "Parsed mask %s", inet_ntoa(New->mask));
1075 else if (ap_proxy_is_domainname(New, parms->pool)) {
1076 ap_str_tolower(New->name);
1078 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1079 "Parsed domain %s", New->name);
1082 else if (ap_proxy_is_hostname(New, parms->pool)) {
1083 ap_str_tolower(New->name);
1085 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1086 "Parsed host %s", New->name);
1090 ap_proxy_is_word(New, parms->pool);
1092 fprintf(stderr, "Parsed word %s\n", New->name);
1100 set_proxy_domain(cmd_parms *parms, void *dummy, const char *arg)
1102 proxy_server_conf *psf =
1103 ap_get_module_config(parms->server->module_config, &proxy_module);
1106 return "ProxyDomain: domain name must start with a dot.";
1113 set_proxy_req(cmd_parms *parms, void *dummy, int flag)
1115 proxy_server_conf *psf =
1116 ap_get_module_config(parms->server->module_config, &proxy_module);
1123 set_proxy_error_override(cmd_parms *parms, void *dummy, int flag)
1125 proxy_server_conf *psf =
1126 ap_get_module_config(parms->server->module_config, &proxy_module);
1128 psf->error_override = flag;
1129 psf->error_override_set = 1;
1133 set_preserve_host(cmd_parms *parms, void *dummy, int flag)
1135 proxy_server_conf *psf =
1136 ap_get_module_config(parms->server->module_config, &proxy_module);
1138 psf->preserve_host = flag;
1139 psf->preserve_host_set = 1;
1144 set_recv_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
1146 proxy_server_conf *psf =
1147 ap_get_module_config(parms->server->module_config, &proxy_module);
1149 if (s < 512 && s != 0) {
1150 return "ProxyReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
1153 psf->recv_buffer_size = s;
1154 psf->recv_buffer_size_set = 1;
1159 set_io_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
1161 proxy_server_conf *psf =
1162 ap_get_module_config(parms->server->module_config, &proxy_module);
1165 psf->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
1166 psf->io_buffer_size_set = 1;
1171 set_max_forwards(cmd_parms *parms, void *dummy, const char *arg)
1173 proxy_server_conf *psf =
1174 ap_get_module_config(parms->server->module_config, &proxy_module);
1177 return "ProxyMaxForwards must be greater or equal to zero..";
1181 psf->maxfwd_set = 1;
1185 set_proxy_timeout(cmd_parms *parms, void *dummy, const char *arg)
1187 proxy_server_conf *psf =
1188 ap_get_module_config(parms->server->module_config, &proxy_module);
1193 return "Proxy Timeout must be at least 1 second.";
1196 psf->timeout=apr_time_from_sec(timeout);
1202 set_via_opt(cmd_parms *parms, void *dummy, const char *arg)
1204 proxy_server_conf *psf =
1205 ap_get_module_config(parms->server->module_config, &proxy_module);
1207 if (strcasecmp(arg, "Off") == 0)
1208 psf->viaopt = via_off;
1209 else if (strcasecmp(arg, "On") == 0)
1210 psf->viaopt = via_on;
1211 else if (strcasecmp(arg, "Block") == 0)
1212 psf->viaopt = via_block;
1213 else if (strcasecmp(arg, "Full") == 0)
1214 psf->viaopt = via_full;
1216 return "ProxyVia must be one of: "
1217 "off | on | full | block";
1220 psf->viaopt_set = 1;
1225 set_bad_opt(cmd_parms *parms, void *dummy, const char *arg)
1227 proxy_server_conf *psf =
1228 ap_get_module_config(parms->server->module_config, &proxy_module);
1230 if (strcasecmp(arg, "IsError") == 0)
1231 psf->badopt = bad_error;
1232 else if (strcasecmp(arg, "Ignore") == 0)
1233 psf->badopt = bad_ignore;
1234 else if (strcasecmp(arg, "StartBody") == 0)
1235 psf->badopt = bad_body;
1237 return "ProxyBadHeader must be one of: "
1238 "IsError | Ignore | StartBody";
1241 psf->badopt_set = 1;
1246 set_status_opt(cmd_parms *parms, void *dummy, const char *arg)
1248 proxy_server_conf *psf =
1249 ap_get_module_config(parms->server->module_config, &proxy_module);
1251 if (strcasecmp(arg, "Off") == 0)
1252 psf->proxy_status = status_off;
1253 else if (strcasecmp(arg, "On") == 0)
1254 psf->proxy_status = status_on;
1255 else if (strcasecmp(arg, "Full") == 0)
1256 psf->proxy_status = status_full;
1258 return "ProxyStatus must be one of: "
1262 psf->proxy_status_set = 1;
1266 static const char *add_member(cmd_parms *cmd, void *dummy, const char *arg)
1268 server_rec *s = cmd->server;
1269 proxy_server_conf *conf =
1270 ap_get_module_config(s->module_config, &proxy_module);
1271 proxy_balancer *balancer;
1272 proxy_worker *worker;
1273 char *path = cmd->path;
1276 apr_table_t *params = apr_table_make(cmd->pool, 5);
1277 const apr_array_header_t *arr;
1278 const apr_table_entry_t *elts;
1282 path = apr_pstrdup(cmd->pool, cmd->path);
1284 word = ap_getword_conf(cmd->pool, &arg);
1290 char *val = strchr(word, '=');
1293 return "BalancerMember can not have a balancer name when defined in a location";
1295 return "Invalid BalancerMember parameter. Parameter must "
1296 "be in the form 'key=value'";
1299 apr_table_setn(params, word, val);
1303 return "BalancerMember must define balancer name when outside <Proxy > section";
1305 return "BalancerMember must define remote proxy server";
1307 ap_str_tolower(path); /* lowercase scheme://hostname */
1308 ap_str_tolower(name); /* lowercase scheme://hostname */
1310 /* Try to find existing worker */
1311 worker = ap_proxy_get_worker(cmd->temp_pool, conf, name);
1314 if ((err = ap_proxy_add_worker(&worker, cmd->pool, conf, name)) != NULL)
1315 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1317 PROXY_COPY_CONF_PARAMS(worker, conf);
1319 arr = apr_table_elts(params);
1320 elts = (const apr_table_entry_t *)arr->elts;
1321 for (i = 0; i < arr->nelts; i++) {
1322 const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
1325 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1327 /* Try to find the balancer */
1328 balancer = ap_proxy_get_balancer(cmd->temp_pool, conf, path);
1330 const char *err = ap_proxy_add_balancer(&balancer,
1334 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1336 /* Add the worker to the load balancer */
1337 ap_proxy_add_worker_to_balancer(cmd->pool, balancer, worker);
1343 set_proxy_param(cmd_parms *cmd, void *dummy, const char *arg)
1345 server_rec *s = cmd->server;
1346 proxy_server_conf *conf =
1347 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
1350 proxy_balancer *balancer = NULL;
1351 proxy_worker *worker = NULL;
1354 if (cmd->directive->parent &&
1355 strncasecmp(cmd->directive->parent->directive,
1356 "<Proxy", 6) == 0) {
1357 const char *pargs = cmd->directive->parent->args;
1358 /* Directive inside <Proxy section
1359 * Parent directive arg is the worker/balancer name.
1361 name = ap_getword_conf(cmd->temp_pool, &pargs);
1362 if ((word = ap_strchr_c(name, '>')))
1366 /* Standard set directive with worker/balancer
1367 * name as first param.
1369 name = ap_getword_conf(cmd->temp_pool, &arg);
1372 if (strncasecmp(name, "balancer:", 9) == 0) {
1373 balancer = ap_proxy_get_balancer(cmd->pool, conf, name);
1375 return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
1376 name, "' Balancer.", NULL);
1380 worker = ap_proxy_get_worker(cmd->temp_pool, conf, name);
1382 return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
1383 name, "' Worker.", NULL);
1388 word = ap_getword_conf(cmd->pool, &arg);
1389 val = strchr(word, '=');
1391 return "Invalid ProxySet parameter. Parameter must be "
1392 "in the form 'key=value'";
1397 err = set_worker_param(cmd->pool, worker, word, val);
1399 err = set_balancer_param(cmd->pool, balancer, word, val);
1402 return apr_pstrcat(cmd->temp_pool, "ProxySet ", err, " ", word, " ", name, NULL);
1408 static void ap_add_per_proxy_conf(server_rec *s, ap_conf_vector_t *dir_config)
1410 proxy_server_conf *sconf = ap_get_module_config(s->module_config,
1412 void **new_space = (void **)apr_array_push(sconf->sec_proxy);
1414 *new_space = dir_config;
1417 static const char *proxysection(cmd_parms *cmd, void *mconfig, const char *arg)
1420 const char *endp = ap_strrchr_c(arg, '>');
1421 int old_overrides = cmd->override;
1422 char *old_path = cmd->path;
1423 proxy_dir_conf *conf;
1424 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1426 const command_rec *thiscmd = cmd->cmd;
1428 const char *err = ap_check_cmd_context(cmd,
1429 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1435 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1436 "> directive missing closing '>'", NULL);
1439 arg=apr_pstrndup(cmd->pool, arg, endp-arg);
1442 if (thiscmd->cmd_data)
1443 return "<ProxyMatch > block must specify a path";
1445 return "<Proxy > block must specify a path";
1448 cmd->path = ap_getword_conf(cmd->pool, &arg);
1449 cmd->override = OR_ALL|ACCESS_CONF;
1451 if (!strncasecmp(cmd->path, "proxy:", 6))
1454 /* XXX Ignore case? What if we proxy a case-insensitive server?!?
1455 * While we are at it, shouldn't we also canonicalize the entire
1456 * scheme? See proxy_fixup()
1458 if (thiscmd->cmd_data) { /* <ProxyMatch> */
1459 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1461 return "Regex could not be compiled";
1464 else if (!strcmp(cmd->path, "~")) {
1465 cmd->path = ap_getword_conf(cmd->pool, &arg);
1467 return "<Proxy ~ > block must specify a path";
1468 if (strncasecmp(cmd->path, "proxy:", 6))
1470 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1472 return "Regex could not be compiled";
1476 /* initialize our config and fetch it */
1477 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1478 &proxy_module, cmd->pool);
1480 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1485 conf->p = cmd->path;
1486 conf->p_is_fnmatch = apr_fnmatch_test(conf->p);
1488 ap_add_per_proxy_conf(cmd->server, new_dir_conf);
1491 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1492 "> arguments not (yet) supported.", NULL);
1495 cmd->path = old_path;
1496 cmd->override = old_overrides;
1501 static const command_rec proxy_cmds[] =
1503 AP_INIT_RAW_ARGS("<Proxy", proxysection, NULL, RSRC_CONF,
1504 "Container for directives affecting resources located in the proxied "
1506 AP_INIT_RAW_ARGS("<ProxyMatch", proxysection, (void*)1, RSRC_CONF,
1507 "Container for directives affecting resources located in the proxied "
1508 "location, in regular expression syntax"),
1509 AP_INIT_FLAG("ProxyRequests", set_proxy_req, NULL, RSRC_CONF,
1510 "on if the true proxy requests should be accepted"),
1511 AP_INIT_TAKE2("ProxyRemote", add_proxy_noregex, NULL, RSRC_CONF,
1512 "a scheme, partial URL or '*' and a proxy server"),
1513 AP_INIT_TAKE2("ProxyRemoteMatch", add_proxy_regex, NULL, RSRC_CONF,
1514 "a regex pattern and a proxy server"),
1515 AP_INIT_RAW_ARGS("ProxyPass", add_pass, NULL, RSRC_CONF|ACCESS_CONF,
1516 "a virtual path and a URL"),
1517 AP_INIT_TAKE12("ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF|ACCESS_CONF,
1518 "a virtual path and a URL for reverse proxy behaviour"),
1519 AP_INIT_TAKE2("ProxyPassReverseCookiePath", cookie_path, NULL,
1520 RSRC_CONF|ACCESS_CONF, "Path rewrite rule for proxying cookies"),
1521 AP_INIT_TAKE2("ProxyPassReverseCookieDomain", cookie_domain, NULL,
1522 RSRC_CONF|ACCESS_CONF, "Domain rewrite rule for proxying cookies"),
1523 AP_INIT_ITERATE("ProxyBlock", set_proxy_exclude, NULL, RSRC_CONF,
1524 "A list of names, hosts or domains to which the proxy will not connect"),
1525 AP_INIT_TAKE1("ProxyReceiveBufferSize", set_recv_buffer_size, NULL, RSRC_CONF,
1526 "Receive buffer size for outgoing HTTP and FTP connections in bytes"),
1527 AP_INIT_TAKE1("ProxyIOBufferSize", set_io_buffer_size, NULL, RSRC_CONF,
1528 "IO buffer size for outgoing HTTP and FTP connections in bytes"),
1529 AP_INIT_TAKE1("ProxyMaxForwards", set_max_forwards, NULL, RSRC_CONF,
1530 "The maximum number of proxies a request may be forwarded through."),
1531 AP_INIT_ITERATE("NoProxy", set_proxy_dirconn, NULL, RSRC_CONF,
1532 "A list of domains, hosts, or subnets to which the proxy will connect directly"),
1533 AP_INIT_TAKE1("ProxyDomain", set_proxy_domain, NULL, RSRC_CONF,
1534 "The default intranet domain name (in absence of a domain in the URL)"),
1535 AP_INIT_ITERATE("AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF,
1536 "A list of ports which CONNECT may connect to"),
1537 AP_INIT_TAKE1("ProxyVia", set_via_opt, NULL, RSRC_CONF,
1538 "Configure Via: proxy header header to one of: on | off | block | full"),
1539 AP_INIT_FLAG("ProxyErrorOverride", set_proxy_error_override, NULL, RSRC_CONF,
1540 "use our error handling pages instead of the servers' we are proxying"),
1541 AP_INIT_FLAG("ProxyPreserveHost", set_preserve_host, NULL, RSRC_CONF,
1542 "on if we should preserve host header while proxying"),
1543 AP_INIT_TAKE1("ProxyTimeout", set_proxy_timeout, NULL, RSRC_CONF,
1544 "Set the timeout (in seconds) for a proxied connection. "
1545 "This overrides the server timeout"),
1546 AP_INIT_TAKE1("ProxyBadHeader", set_bad_opt, NULL, RSRC_CONF,
1547 "How to handle bad header line in response: IsError | Ignore | StartBody"),
1548 AP_INIT_RAW_ARGS("BalancerMember", add_member, NULL, RSRC_CONF|ACCESS_CONF,
1549 "A balancer name and scheme with list of params"),
1550 AP_INIT_TAKE1("ProxyStatus", set_status_opt, NULL, RSRC_CONF,
1551 "Configure Status: proxy status to one of: on | off | full"),
1552 AP_INIT_RAW_ARGS("ProxySet", set_proxy_param, NULL, RSRC_CONF|ACCESS_CONF,
1553 "A balancer or worker name with list of params"),
1557 static APR_OPTIONAL_FN_TYPE(ssl_proxy_enable) *proxy_ssl_enable = NULL;
1558 static APR_OPTIONAL_FN_TYPE(ssl_engine_disable) *proxy_ssl_disable = NULL;
1560 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c)
1563 * if c == NULL just check if the optional function was imported
1564 * else run the optional function so ssl filters are inserted
1566 if (proxy_ssl_enable) {
1567 return c ? proxy_ssl_enable(c) : 1;
1573 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c)
1575 if (proxy_ssl_disable) {
1576 return proxy_ssl_disable(c);
1582 static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1583 apr_pool_t *ptemp, server_rec *s)
1585 proxy_ssl_enable = APR_RETRIEVE_OPTIONAL_FN(ssl_proxy_enable);
1586 proxy_ssl_disable = APR_RETRIEVE_OPTIONAL_FN(ssl_engine_disable);
1593 #define MBYTE 1048576L
1594 #define GBYTE 1073741824L
1596 /* Format the number of bytes nicely */
1597 static void format_byte_out(request_rec *r, apr_off_t bytes)
1600 if (bytes < (5 * KBYTE))
1601 ap_rprintf(r, "%d B", (int) bytes);
1602 else if (bytes < (MBYTE / 2))
1603 ap_rprintf(r, "%.1f kB", (float) bytes / KBYTE);
1604 else if (bytes < (GBYTE / 2))
1605 ap_rprintf(r, "%.1f MB", (float) bytes / MBYTE);
1607 ap_rprintf(r, "%.1f GB", (float) bytes / GBYTE);
1611 * proxy Extension to mod_status
1613 static int proxy_status_hook(request_rec *r, int flags)
1616 void *sconf = r->server->module_config;
1617 proxy_server_conf *conf = (proxy_server_conf *)
1618 ap_get_module_config(sconf, &proxy_module);
1619 proxy_balancer *balancer = NULL;
1620 proxy_runtime_worker *worker = NULL;
1622 if (flags & AP_STATUS_SHORT || conf->balancers->nelts == 0 ||
1623 conf->proxy_status == status_off)
1626 balancer = (proxy_balancer *)conf->balancers->elts;
1627 for (i = 0; i < conf->balancers->nelts; i++) {
1628 ap_rputs("<hr />\n<h1>Proxy LoadBalancer Status for ", r);
1629 ap_rvputs(r, balancer->name, "</h1>\n\n", NULL);
1630 ap_rputs("\n\n<table border=\"0\"><tr>"
1631 "<th>SSes</th><th>Timeout</th>"
1633 ap_rvputs(r, "<td>", balancer->sticky, NULL);
1634 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>\n",
1635 apr_time_sec(balancer->timeout));
1636 ap_rputs("</table>\n", r);
1637 ap_rputs("\n\n<table border=\"0\"><tr>"
1638 "<th>Sch</th><th>Host</th>"
1639 "<th>Route</th><th>Redir</th>"
1640 "<th>F</th><th>Acc</th><th>Wr</th><th>Rd</th>"
1643 worker = (proxy_runtime_worker *)balancer->workers->elts;
1644 for (n = 0; n < balancer->workers->nelts; n++) {
1646 ap_rvputs(r, "<tr>\n<td>", worker->w->scheme, "</td>", NULL);
1647 ap_rvputs(r, "<td>", worker->w->hostname, "</td>", NULL);
1648 ap_rvputs(r, "<td>", worker->w->route, NULL);
1649 ap_rvputs(r, "</td><td>", worker->w->redirect, NULL);
1650 ap_rprintf(r, "</td><td>%.2f</td>", worker->s->lbfactor);
1651 ap_rprintf(r, "<td>%d</td><td>", (int)(worker->s->elected));
1652 format_byte_out(r, worker->s->transfered);
1653 ap_rputs("</td><td>", r);
1654 format_byte_out(r, worker->s->transfered);
1655 ap_rputs("</td>\n", r);
1657 /* TODO: Add the rest of dynamic worker data */
1658 ap_rputs("</tr>\n", r);
1662 ap_rputs("</table>\n", r);
1665 ap_rputs("<hr /><table>\n"
1666 "<tr><th>SSes</th><td>Sticky session name</td></tr>\n"
1667 "<tr><th>Timeout</th><td>Balancer Timeout</td></tr>\n"
1668 "<tr><th>Sch</th><td>Connection scheme</td></tr>\n"
1669 "<tr><th>Host</th><td>Backend Hostname</td></tr>\n"
1670 "<tr><th>Route</th><td>Session Route</td></tr>\n"
1671 "<tr><th>Redir</th><td>Session Route Redirection</td></tr>\n"
1672 "<tr><th>F</th><td>Load Balancer Factor in %</td></tr>\n"
1673 "<tr><th>Acc</th><td>Number of requests</td></tr>\n"
1674 "<tr><th>Wr</th><td>Number of bytes transfered</td></tr>\n"
1675 "<tr><th>Rd</th><td>Number of bytes readed</td></tr>\n"
1682 * This routine is called before the server processes the configuration
1683 * files. There is no return value.
1685 static int proxy_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
1688 APR_OPTIONAL_HOOK(ap, status_hook, proxy_status_hook, NULL, NULL,
1693 static void register_hooks(apr_pool_t *p)
1695 /* fixup before mod_rewrite, so that the proxied url will not
1696 * escaped accidentally by our fixup.
1699 static const char * const aszSucc[]={ "mod_rewrite.c", NULL };
1703 ap_hook_handler(proxy_handler, NULL, NULL, APR_HOOK_FIRST);
1704 /* filename-to-URI translation */
1705 ap_hook_translate_name(proxy_trans, NULL, NULL, APR_HOOK_FIRST);
1706 /* walk <Proxy > entries and suppress default TRACE behavior */
1707 ap_hook_map_to_storage(proxy_map_location, NULL,NULL, APR_HOOK_FIRST);
1710 ap_hook_fixups(proxy_fixup, NULL, aszSucc, APR_HOOK_FIRST);
1712 /* post read_request handling */
1713 ap_hook_post_read_request(proxy_detect, NULL, NULL, APR_HOOK_FIRST);
1714 /* pre config handling */
1715 ap_hook_pre_config(proxy_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1716 /* post config handling */
1717 ap_hook_post_config(proxy_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1720 module AP_MODULE_DECLARE_DATA proxy_module =
1722 STANDARD20_MODULE_STUFF,
1723 create_proxy_dir_config, /* create per-directory config structure */
1724 merge_proxy_dir_config, /* merge per-directory config structures */
1725 create_proxy_config, /* create per-server config structure */
1726 merge_proxy_config, /* merge per-server config structures */
1727 proxy_cmds, /* command table */
1732 APR_HOOK_LINK(scheme_handler)
1733 APR_HOOK_LINK(canon_handler)
1734 APR_HOOK_LINK(pre_request)
1735 APR_HOOK_LINK(post_request)
1738 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, scheme_handler,
1739 (request_rec *r, proxy_worker *worker,
1740 proxy_server_conf *conf,
1741 char *url, const char *proxyhost,
1742 apr_port_t proxyport),(r,worker,conf,
1743 url,proxyhost,proxyport),DECLINED)
1744 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, canon_handler,
1745 (request_rec *r, char *url),(r,
1747 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, pre_request, (
1748 proxy_worker **worker,
1749 proxy_balancer **balancer,
1751 proxy_server_conf *conf,
1752 char **url),(worker,balancer,
1753 r,conf,url),DECLINED)
1754 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, post_request,
1755 (proxy_worker *worker,
1756 proxy_balancer *balancer,
1758 proxy_server_conf *conf),(worker,
1759 balancer,r,conf),DECLINED)
1760 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, fixups,
1761 (request_rec *r), (r),