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 "scoreboard.h"
23 #include "mod_status.h"
25 #if (MODULE_MAGIC_NUMBER_MAJOR > 20020903)
28 APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
29 APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
33 #define MAX(x,y) ((x) >= (y) ? (x) : (y))
37 * A Web proxy module. Stages:
39 * translate_name: set filename to proxy:<URL>
40 * map_to_storage: run proxy_walk (rather than directory_walk/file_walk)
41 * can't trust directory_walk/file_walk since these are
42 * not in our filesystem. Prevents mod_http from serving
43 * the TRACE request we will set aside to handle later.
44 * type_checker: set type to PROXY_MAGIC_TYPE if filename begins proxy:
45 * fix_ups: convert the URL stored in the filename to the
47 * handler: handle proxy requests
50 /* -------------------------------------------------------------- */
51 /* Translate the URL into a 'filename' */
54 /* XXX: EBCDIC safe? --nd */
55 #define x2c(x) (((x >= '0') && (x <= '9')) \
57 : (((x >= 'a') && (x <= 'f')) \
59 : ((x >= 'A') && (x <='F')) \
65 static unsigned char hex2c(const char* p) {
67 const char c2 = p[1] ? p[2]: '\0';
68 int i1 = c1 ? x2c(c1) : 0;
69 int i2 = c2 ? x2c(c2) : 0;
70 unsigned char ret = (i1 << 4) | i2;
76 #define PROXY_COPY_CONF_PARAMS(w, c) \
78 (w)->timeout = (c)->timeout; \
79 (w)->timeout_set = (c)->timeout_set; \
80 (w)->recv_buffer_size = (c)->recv_buffer_size; \
81 (w)->recv_buffer_size_set = (c)->recv_buffer_size_set; \
82 (w)->io_buffer_size = (c)->io_buffer_size; \
83 (w)->io_buffer_size_set = (c)->io_buffer_size_set; \
86 static const char *set_worker_param(apr_pool_t *p,
93 if (!strcasecmp(key, "loadfactor")) {
94 /* Worker load factor. Used with BalancerMamber
95 * It is a number between 1 and 100 in percents.
97 worker->lbfactor = atoi(val);
98 if (worker->lbfactor < 1 || worker->lbfactor > 100)
99 return "LoadFactor must be number between 1..100";
101 else if (!strcasecmp(key, "retry")) {
102 /* If set it will give the retry timeout for the worker
103 * The default value is 60 seconds, meaning that if
104 * in error state, it will be retried after that timeout.
108 return "Retry must be at least one second";
109 worker->retry = apr_time_from_sec(ival);
111 else if (!strcasecmp(key, "ttl")) {
112 /* Time in seconds that will destroy all the connections
113 * that exced the smax
117 return "TTL must be at least one second";
118 worker->ttl = apr_time_from_sec(ival);
120 else if (!strcasecmp(key, "min")) {
121 /* Initial number of connections to remote
125 return "Min must be a positive number";
128 else if (!strcasecmp(key, "max")) {
129 /* Maximum number of connections to remote
133 return "Max must be a positive number";
136 /* XXX: More inteligent naming needed */
137 else if (!strcasecmp(key, "smax")) {
138 /* Maximum number of connections to remote that
139 * will not be destroyed
143 return "Smax must be a positive number";
146 else if (!strcasecmp(key, "acquire")) {
147 /* Acquire timeout in milliseconds.
148 * If set this will be the maximum time to
149 * wait for a free connection.
153 return "Acquire must be at least one mili second";
154 worker->acquire = apr_time_make(0, ival * 1000);
155 worker->acquire_set = 1;
157 else if (!strcasecmp(key, "timeout")) {
158 /* Connection timeout in seconds.
159 * Defaults to server timeout.
163 return "Timeout must be at least one second";
164 worker->timeout = apr_time_from_sec(ival);
165 worker->timeout_set = 1;
167 else if (!strcasecmp(key, "iobuffersize")) {
169 worker->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
170 worker->io_buffer_size_set = 1;
172 else if (!strcasecmp(key, "receivebuffersize")) {
174 if (ival < 512 && ival != 0) {
175 return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
177 worker->recv_buffer_size = ival;
178 worker->recv_buffer_size_set = 1;
180 else if (!strcasecmp(key, "keepalive")) {
181 if (!strcasecmp(val, "on"))
182 worker->keepalive = 1;
183 else if (!strcasecmp(val, "off"))
184 worker->keepalive = 0;
186 return "KeepAlive must be On|Off";
187 worker->keepalive_set = 1;
189 else if (!strcasecmp(key, "route")) {
192 worker->route = apr_pstrdup(p, val);
194 else if (!strcasecmp(key, "redirect")) {
195 /* Worker redirection route.
197 worker->redirect = apr_pstrdup(p, val);
200 return "unknown Worker parameter";
205 static const char *set_balancer_param(apr_pool_t *p,
206 proxy_balancer *balancer,
212 if (!strcasecmp(key, "stickysession")) {
213 /* Balancer sticky session name.
214 * Set to something like JSESSIONID or
215 * PHPSESSIONID, etc..,
217 balancer->sticky = apr_pstrdup(p, val);
219 else if (!strcasecmp(key, "nofailover")) {
220 /* If set to 'on' the session will break
221 * if the worker is in error state or
224 if (!strcasecmp(val, "on"))
225 balancer->sticky_force = 1;
226 else if (!strcasecmp(val, "off"))
227 balancer->sticky_force = 0;
229 return "failover must be On|Off";
231 else if (!strcasecmp(key, "timeout")) {
232 /* Balancer timeout in seconds.
233 * If set this will be the maximum time to
234 * wait for a free worker.
235 * Default is not to wait.
239 return "timeout must be at least one second";
240 balancer->timeout = apr_time_from_sec(ival);
242 else if (!strcasecmp(key, "maxattempts")) {
243 /* Maximum number of failover attempts before
248 return "maximum number of attempts must be a positive number";
249 balancer->max_attempts = ival;
250 balancer->max_attempts_set = 1;
253 return "unknown Balancer parameter";
258 static int alias_match(const char *uri, const char *alias_fakename)
260 const char *end_fakename = alias_fakename + strlen(alias_fakename);
261 const char *aliasp = alias_fakename, *urip = uri;
262 const char *end_uri = uri + strlen(uri);
263 unsigned char uric, aliasc;
265 while (aliasp < end_fakename && urip < end_uri) {
266 if (*aliasp == '/') {
267 /* any number of '/' in the alias matches any number in
268 * the supplied URI, but there must be at least one...
273 while (*aliasp == '/')
280 /* Other characters are compared literally */
281 if (*urip++ != *aliasp++)
284 /* Other characters are canonicalised and compared literally */
289 uric = (unsigned char)*urip++;
291 if (*aliasp == '%') {
292 aliasc = hex2c(aliasp);
295 aliasc = (unsigned char)*aliasp++;
297 if (uric != aliasc) {
304 /* fixup badly encoded stuff (e.g. % as last character) */
305 if (aliasp > end_fakename) {
306 aliasp = end_fakename;
308 if (urip > end_uri) {
312 /* We reach the end of the uri before the end of "alias_fakename"
313 * for example uri is "/" and alias_fakename "/examples"
315 if (urip == end_uri && aliasp!=end_fakename) {
319 /* Check last alias path component matched all the way */
320 if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
323 /* Return number of characters from URI which matched (may be
324 * greater than length of alias, since we may have matched
331 /* Detect if an absoluteURI should be proxied or not. Note that we
332 * have to do this during this phase because later phases are
333 * "short-circuiting"... i.e. translate_names will end when the first
334 * module returns OK. So for example, if the request is something like:
336 * GET http://othervhost/cgi-bin/printenv HTTP/1.0
338 * mod_alias will notice the /cgi-bin part and ScriptAlias it and
339 * short-circuit the proxy... just because of the ordering in the
340 * configuration file.
342 static int proxy_detect(request_rec *r)
344 void *sconf = r->server->module_config;
345 proxy_server_conf *conf =
346 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
349 struct proxy_alias *ent = (struct proxy_alias *)conf->aliases->elts;
352 /* Ick... msvc (perhaps others) promotes ternary short results to int */
354 if (conf->req && r->parsed_uri.scheme) {
355 /* but it might be something vhosted */
356 if (!(r->parsed_uri.hostname
357 && !strcasecmp(r->parsed_uri.scheme, ap_http_method(r))
358 && ap_matches_request_vhost(r, r->parsed_uri.hostname,
359 (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
360 : ap_default_port(r))))) {
361 r->proxyreq = PROXYREQ_PROXY;
362 r->uri = r->unparsed_uri;
363 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
364 r->handler = "proxy-server";
367 /* We need special treatment for CONNECT proxying: it has no scheme part */
368 else if (conf->req && r->method_number == M_CONNECT
369 && r->parsed_uri.hostname
370 && r->parsed_uri.port_str) {
371 r->proxyreq = PROXYREQ_PROXY;
372 r->uri = r->unparsed_uri;
373 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
374 r->handler = "proxy-server";
377 /* test for a ProxyPass */
378 for (i = 0; i < conf->aliases->nelts; i++) {
379 len = alias_match(r->unparsed_uri, ent[i].fake);
381 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
382 r->unparsed_uri + len, NULL);
383 r->handler = "proxy-server";
384 r->proxyreq = PROXYREQ_REVERSE;
385 r->uri = r->unparsed_uri;
394 static int proxy_trans(request_rec *r)
397 void *sconf = r->server->module_config;
398 proxy_server_conf *conf =
399 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
401 struct proxy_alias *ent = (struct proxy_alias *) conf->aliases->elts;
405 /* someone has already set up the proxy, it was possibly ourselves
412 /* XXX: since r->uri has been manipulated already we're not really
413 * compliant with RFC1945 at this point. But this probably isn't
414 * an issue because this is a hybrid proxy/origin server.
417 for (i = 0; i < conf->aliases->nelts; i++) {
418 len = alias_match(r->uri, ent[i].fake);
421 if ((ent[i].real[0] == '!') && (ent[i].real[1] == 0)) {
425 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
427 r->handler = "proxy-server";
428 r->proxyreq = PROXYREQ_REVERSE;
436 static int proxy_walk(request_rec *r)
438 proxy_server_conf *sconf = ap_get_module_config(r->server->module_config,
440 ap_conf_vector_t *per_dir_defaults = r->server->lookup_defaults;
441 ap_conf_vector_t **sec_proxy = (ap_conf_vector_t **) sconf->sec_proxy->elts;
442 ap_conf_vector_t *entry_config;
443 proxy_dir_conf *entry_proxy;
444 int num_sec = sconf->sec_proxy->nelts;
445 /* XXX: shouldn't we use URI here? Canonicalize it first?
446 * Pass over "proxy:" prefix
448 const char *proxyname = r->filename + 6;
451 for (j = 0; j < num_sec; ++j)
453 entry_config = sec_proxy[j];
454 entry_proxy = ap_get_module_config(entry_config, &proxy_module);
456 /* XXX: What about case insensitive matching ???
457 * Compare regex, fnmatch or string as appropriate
458 * If the entry doesn't relate, then continue
461 ? ap_regexec(entry_proxy->r, proxyname, 0, NULL, 0)
462 : (entry_proxy->p_is_fnmatch
463 ? apr_fnmatch(entry_proxy->p, proxyname, 0)
464 : strncmp(proxyname, entry_proxy->p,
465 strlen(entry_proxy->p)))) {
468 per_dir_defaults = ap_merge_per_dir_configs(r->pool, per_dir_defaults,
472 r->per_dir_config = per_dir_defaults;
477 static int proxy_map_location(request_rec *r)
481 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
484 /* Don't let the core or mod_http map_to_storage hooks handle this,
485 * We don't need directory/file_walk, and we want to TRACE on our own.
487 if ((access_status = proxy_walk(r))) {
488 ap_die(access_status, r);
489 return access_status;
495 /* -------------------------------------------------------------- */
496 /* Fixup the filename */
499 * Canonicalise the URL
501 static int proxy_fixup(request_rec *r)
506 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
510 /* We definitely shouldn't canonicalize a proxy_pass.
511 * But should we really canonicalize a STD_PROXY??? -- Fahree
513 if (r->proxyreq == PROXYREQ_REVERSE) {
518 /* XXX: Shouldn't we try this before we run the proxy_walk? */
519 url = &r->filename[6];
521 /* canonicalise each specific scheme */
522 if ((access_status = proxy_run_canon_handler(r, url))) {
523 return access_status;
526 p = strchr(url, ':');
527 if (p == NULL || p == url)
528 return HTTP_BAD_REQUEST;
530 return OK; /* otherwise; we've done the best we can */
533 /* Send a redirection if the request contains a hostname which is not */
534 /* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
535 /* servers like Netscape's allow this and access hosts from the local */
536 /* domain in this case. I think it is better to redirect to a FQDN, since */
537 /* these will later be found in the bookmarks files. */
538 /* The "ProxyDomain" directive determines what domain will be appended */
539 static int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
544 /* We only want to worry about GETs */
545 if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
548 /* If host does contain a dot already, or it is "localhost", decline */
549 if (strchr(r->parsed_uri.hostname, '.') != NULL
550 || strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
551 return DECLINED; /* host name has a dot already */
553 ref = apr_table_get(r->headers_in, "Referer");
555 /* Reassemble the request, but insert the domain after the host name */
556 /* Note that the domain name always starts with a dot */
557 r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
559 nuri = apr_uri_unparse(r->pool,
561 APR_URI_UNP_REVEALPASSWORD);
563 apr_table_set(r->headers_out, "Location", nuri);
564 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
565 "Domain missing: %s sent to %s%s%s", r->uri,
566 apr_uri_unparse(r->pool, &r->parsed_uri,
567 APR_URI_UNP_OMITUSERINFO),
568 ref ? " from " : "", ref ? ref : "");
570 return HTTP_MOVED_PERMANENTLY;
573 /* -------------------------------------------------------------- */
576 static int proxy_handler(request_rec *r)
578 char *url, *scheme, *p;
580 void *sconf = r->server->module_config;
581 proxy_server_conf *conf = (proxy_server_conf *)
582 ap_get_module_config(sconf, &proxy_module);
583 apr_array_header_t *proxies = conf->proxies;
584 struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
585 int i, rc, access_status;
586 int direct_connect = 0;
589 proxy_balancer *balancer = NULL;
590 proxy_worker *worker = NULL;
591 int attempts = 0, max_attempts = 0;
593 /* is this for us? */
594 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
597 /* handle max-forwards / OPTIONS / TRACE */
598 if ((str = apr_table_get(r->headers_in, "Max-Forwards"))) {
599 maxfwd = strtol(str, NULL, 10);
601 switch (r->method_number) {
604 r->proxyreq = PROXYREQ_NONE;
605 if ((access_status = ap_send_http_trace(r)))
606 ap_die(access_status, r);
608 ap_finalize_request_protocol(r);
613 r->proxyreq = PROXYREQ_NONE;
614 if ((access_status = ap_send_http_options(r)))
615 ap_die(access_status, r);
617 ap_finalize_request_protocol(r);
621 return ap_proxyerror(r, HTTP_BAD_GATEWAY,
622 "Max-Forwards has reached zero - proxy loop?");
626 maxfwd = (maxfwd > 0) ? maxfwd - 1 : 0;
629 /* set configured max-forwards */
630 maxfwd = conf->maxfwd;
632 apr_table_set(r->headers_in, "Max-Forwards",
633 apr_psprintf(r->pool, "%ld", (maxfwd > 0) ? maxfwd : 0));
636 url = r->filename + 6;
637 p = strchr(url, ':');
639 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
640 "proxy_handler no URL in %s", r->filename);
641 return HTTP_BAD_REQUEST;
644 /* If the host doesn't have a domain name, add one and redirect. */
645 if (conf->domain != NULL) {
646 rc = proxy_needsdomain(r, url, conf->domain);
647 if (ap_is_HTTP_REDIRECT(rc))
648 return HTTP_MOVED_PERMANENTLY;
652 scheme = apr_pstrdup(r->pool, url);
655 /* Check URI's destination host against NoProxy hosts */
656 /* Bypass ProxyRemote server lookup if configured as NoProxy */
657 /* we only know how to handle communication to a proxy via http */
658 /*if (strcasecmp(scheme, "http") == 0) */
661 struct dirconn_entry *list = (struct dirconn_entry *)
664 for (direct_connect = ii = 0; ii < conf->dirconn->nelts &&
665 !direct_connect; ii++) {
666 direct_connect = list[ii].matcher(&list[ii], r);
669 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
670 (direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
675 /* Try to obtain the most suitable worker */
676 access_status = ap_proxy_pre_request(&worker, &balancer, r, conf, &url);
677 if (access_status != OK)
678 return access_status;
679 if (balancer && balancer->max_attempts_set && !max_attempts)
680 max_attempts = balancer->max_attempts;
681 /* firstly, try a proxy, unless a NoProxy directive is active */
682 if (!direct_connect) {
683 for (i = 0; i < proxies->nelts; i++) {
684 p2 = ap_strchr_c(ents[i].scheme, ':'); /* is it a partial URL? */
685 if (strcmp(ents[i].scheme, "*") == 0 ||
686 (ents[i].use_regex && ap_regexec(ents[i].regexp, url,
688 (p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
690 strncasecmp(url, ents[i].scheme,
691 strlen(ents[i].scheme)) == 0)) {
693 /* handle the scheme */
694 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
695 "Trying to run scheme_handler against proxy");
696 access_status = proxy_run_scheme_handler(r, worker,
701 /* an error or success */
702 if (access_status != DECLINED &&
703 access_status != HTTP_BAD_GATEWAY) {
706 /* we failed to talk to the upstream proxy */
711 /* otherwise, try it direct */
712 /* N.B. what if we're behind a firewall, where we must use a proxy or
716 /* handle the scheme */
717 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
718 "Running scheme %s handler (attempt %d)",
720 if ((access_status = proxy_run_scheme_handler(r, worker, conf,
721 url, NULL, 0)) == OK)
724 } while (!PROXY_WORKER_IS_USABLE(worker) &&
725 max_attempts > attempts++);
727 if (DECLINED == access_status) {
728 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
729 "proxy: No protocol handler was valid for the URL %s. "
730 "If you are using a DSO version of mod_proxy, make sure "
731 "the proxy submodules are included in the configuration "
732 "using LoadModule.", r->uri);
733 access_status = HTTP_FORBIDDEN;
738 int post_status = proxy_run_post_request(worker, balancer, r, conf);
739 if (post_status == DECLINED) {
740 post_status = OK; /* no post_request handler available */
741 /* TODO: reclycle direct worker */
744 return access_status;
747 /* -------------------------------------------------------------- */
748 /* Setup configurable data */
750 static void * create_proxy_config(apr_pool_t *p, server_rec *s)
752 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
754 ps->sec_proxy = apr_array_make(p, 10, sizeof(ap_conf_vector_t *));
755 ps->proxies = apr_array_make(p, 10, sizeof(struct proxy_remote));
756 ps->aliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
757 ps->raliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
758 ps->cookie_paths = apr_array_make(p, 10, sizeof(struct proxy_alias));
759 ps->cookie_domains = apr_array_make(p, 10, sizeof(struct proxy_alias));
760 ps->cookie_path_str = apr_strmatch_precompile(p, "path=", 0);
761 ps->cookie_domain_str = apr_strmatch_precompile(p, "domain=", 0);
762 ps->noproxies = apr_array_make(p, 10, sizeof(struct noproxy_entry));
763 ps->dirconn = apr_array_make(p, 10, sizeof(struct dirconn_entry));
764 ps->allowed_connect_ports = apr_array_make(p, 10, sizeof(int));
765 ps->workers = apr_array_make(p, 10, sizeof(proxy_worker));
766 ps->balancers = apr_array_make(p, 10, sizeof(proxy_balancer));
769 ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
770 ps->viaopt_set = 0; /* 0 means default */
773 ps->recv_buffer_size = 0; /* this default was left unset for some reason */
774 ps->recv_buffer_size_set = 0;
775 ps->io_buffer_size = AP_IOBUFSIZE;
776 ps->io_buffer_size_set = 0;
777 ps->maxfwd = DEFAULT_MAX_FORWARDS;
779 ps->error_override = 0;
780 ps->error_override_set = 0;
781 ps->preserve_host_set = 0;
782 ps->preserve_host = 0;
785 ps->badopt = bad_error;
791 static void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
793 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
794 proxy_server_conf *base = (proxy_server_conf *) basev;
795 proxy_server_conf *overrides = (proxy_server_conf *) overridesv;
797 ps->proxies = apr_array_append(p, base->proxies, overrides->proxies);
798 ps->sec_proxy = apr_array_append(p, base->sec_proxy, overrides->sec_proxy);
799 ps->aliases = apr_array_append(p, base->aliases, overrides->aliases);
800 ps->raliases = apr_array_append(p, base->raliases, overrides->raliases);
802 = apr_array_append(p, base->cookie_paths, overrides->cookie_paths);
804 = apr_array_append(p, base->cookie_domains, overrides->cookie_domains);
805 ps->cookie_path_str = base->cookie_path_str;
806 ps->cookie_domain_str = base->cookie_domain_str;
807 ps->noproxies = apr_array_append(p, base->noproxies, overrides->noproxies);
808 ps->dirconn = apr_array_append(p, base->dirconn, overrides->dirconn);
809 ps->allowed_connect_ports = apr_array_append(p, base->allowed_connect_ports, overrides->allowed_connect_ports);
810 ps->workers = apr_array_append(p, base->workers, overrides->workers);
811 ps->balancers = apr_array_append(p, base->balancers, overrides->balancers);
812 ps->forward = overrides->forward ? overrides->forward : base->forward;
814 ps->domain = (overrides->domain == NULL) ? base->domain : overrides->domain;
815 ps->viaopt = (overrides->viaopt_set == 0) ? base->viaopt : overrides->viaopt;
816 ps->req = (overrides->req_set == 0) ? base->req : overrides->req;
817 ps->recv_buffer_size = (overrides->recv_buffer_size_set == 0) ? base->recv_buffer_size : overrides->recv_buffer_size;
818 ps->io_buffer_size = (overrides->io_buffer_size_set == 0) ? base->io_buffer_size : overrides->io_buffer_size;
819 ps->maxfwd = (overrides->maxfwd_set == 0) ? base->maxfwd : overrides->maxfwd;
820 ps->error_override = (overrides->error_override_set == 0) ? base->error_override : overrides->error_override;
821 ps->preserve_host = (overrides->preserve_host_set == 0) ? base->preserve_host : overrides->preserve_host;
822 ps->timeout= (overrides->timeout_set == 0) ? base->timeout : overrides->timeout;
823 ps->badopt = (overrides->badopt_set == 0) ? base->badopt : overrides->badopt;
828 static void *create_proxy_dir_config(apr_pool_t *p, char *dummy)
830 proxy_dir_conf *new =
831 (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
833 /* Filled in by proxysection, when applicable */
838 static void *merge_proxy_dir_config(apr_pool_t *p, void *basev, void *addv)
840 proxy_dir_conf *new = (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
841 proxy_dir_conf *add = (proxy_dir_conf *) addv;
844 new->p_is_fnmatch = add->p_is_fnmatch;
851 add_proxy(cmd_parms *cmd, void *dummy, const char *f1, const char *r1, int regex)
853 server_rec *s = cmd->server;
854 proxy_server_conf *conf =
855 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
856 struct proxy_remote *new;
858 char *r, *f, *scheme;
862 r = apr_pstrdup(cmd->pool, r1);
863 scheme = apr_pstrdup(cmd->pool, r1);
864 f = apr_pstrdup(cmd->pool, f1);
866 if (p == NULL || p[1] != '/' || p[2] != '/' || p[3] == '\0') {
868 return "ProxyRemoteMatch: Bad syntax for a remote proxy server";
870 return "ProxyRemote: Bad syntax for a remote proxy server";
875 q = strchr(p + 3, ':');
877 if (sscanf(q + 1, "%u", &port) != 1 || port > 65535) {
879 return "ProxyRemoteMatch: Bad syntax for a remote proxy server (bad port number)";
881 return "ProxyRemote: Bad syntax for a remote proxy server (bad port number)";
889 reg = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
891 return "Regular expression for ProxyRemoteMatch could not be compiled.";
894 if (strchr(f, ':') == NULL)
895 ap_str_tolower(f); /* lowercase scheme */
896 ap_str_tolower(p + 3); /* lowercase hostname */
899 port = apr_uri_port_of_scheme(scheme);
902 new = apr_array_push(conf->proxies);
905 new->hostname = p + 3;
908 new->use_regex = regex;
913 add_proxy_noregex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
915 return add_proxy(cmd, dummy, f1, r1, 0);
919 add_proxy_regex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
921 return add_proxy(cmd, dummy, f1, r1, 1);
925 add_pass(cmd_parms *cmd, void *dummy, const char *arg)
927 server_rec *s = cmd->server;
928 proxy_server_conf *conf =
929 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
930 struct proxy_alias *new;
934 apr_table_t *params = apr_table_make(cmd->pool, 5);
935 const apr_array_header_t *arr;
936 const apr_table_entry_t *elts;
940 word = ap_getword_conf(cmd->pool, &arg);
946 char *val = strchr(word, '=');
949 return "Invalid ProxyPass parameter. Parameter must be "
950 "in the form 'key=value'";
952 return "ProxyPass can not have a path when defined in a location";
956 apr_table_setn(params, word, val);
961 return "ProxyPass needs a path when not defined in a location";
963 new = apr_array_push(conf->aliases);
964 new->fake = apr_pstrdup(cmd->pool, f);
965 new->real = apr_pstrdup(cmd->pool, r);
967 arr = apr_table_elts(params);
968 elts = (const apr_table_entry_t *)arr->elts;
969 /* Distinguish the balancer from woker */
970 if (strncasecmp(r, "balancer:", 9) == 0) {
971 proxy_balancer *balancer = ap_proxy_get_balancer(cmd->pool, conf, r);
973 const char *err = ap_proxy_add_balancer(&balancer,
977 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
979 for (i = 0; i < arr->nelts; i++) {
980 const char *err = set_balancer_param(cmd->pool, balancer, elts[i].key,
983 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
987 proxy_worker *worker = ap_proxy_get_worker(cmd->temp_pool, conf, r);
989 const char *err = ap_proxy_add_worker(&worker, cmd->pool, conf, r);
991 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
993 PROXY_COPY_CONF_PARAMS(worker, conf);
995 for (i = 0; i < arr->nelts; i++) {
996 const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
999 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
1006 add_pass_reverse(cmd_parms *cmd, void *dummy, const char *f, const char *r)
1008 server_rec *s = cmd->server;
1009 proxy_server_conf *conf;
1010 struct proxy_alias *new;
1012 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
1014 if (r!=NULL && cmd->path == NULL ) {
1015 new = apr_array_push(conf->raliases);
1018 } else if (r==NULL && cmd->path != NULL) {
1019 new = apr_array_push(conf->raliases);
1020 new->fake = cmd->path;
1024 return "ProxyPassReverse needs a path when not defined in a location";
1026 return "ProxyPassReverse can not have a path when defined in a location";
1032 cookie_path(cmd_parms *cmd, void *dummy, const char *f, const char *r)
1034 server_rec *s = cmd->server;
1035 proxy_server_conf *conf;
1036 struct proxy_alias *new;
1038 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
1040 new = apr_array_push(conf->cookie_paths);
1047 cookie_domain(cmd_parms *cmd, void *dummy, const char *f, const char *r)
1049 server_rec *s = cmd->server;
1050 proxy_server_conf *conf;
1051 struct proxy_alias *new;
1053 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
1055 new = apr_array_push(conf->cookie_domains);
1063 set_proxy_exclude(cmd_parms *parms, void *dummy, const char *arg)
1065 server_rec *s = parms->server;
1066 proxy_server_conf *conf =
1067 ap_get_module_config(s->module_config, &proxy_module);
1068 struct noproxy_entry *new;
1069 struct noproxy_entry *list = (struct noproxy_entry *) conf->noproxies->elts;
1070 struct apr_sockaddr_t *addr;
1074 /* Don't duplicate entries */
1075 for (i = 0; i < conf->noproxies->nelts; i++) {
1076 if (apr_strnatcasecmp(arg, list[i].name) == 0) { /* ignore case for host names */
1082 new = apr_array_push(conf->noproxies);
1084 if (APR_SUCCESS == apr_sockaddr_info_get(&addr, new->name, APR_UNSPEC, 0, 0, parms->pool)) {
1095 * Set the ports CONNECT can use
1098 set_allowed_ports(cmd_parms *parms, void *dummy, const char *arg)
1100 server_rec *s = parms->server;
1101 proxy_server_conf *conf =
1102 ap_get_module_config(s->module_config, &proxy_module);
1105 if (!apr_isdigit(arg[0]))
1106 return "AllowCONNECT: port number must be numeric";
1108 New = apr_array_push(conf->allowed_connect_ports);
1113 /* Similar to set_proxy_exclude(), but defining directly connected hosts,
1114 * which should never be accessed via the configured ProxyRemote servers
1117 set_proxy_dirconn(cmd_parms *parms, void *dummy, const char *arg)
1119 server_rec *s = parms->server;
1120 proxy_server_conf *conf =
1121 ap_get_module_config(s->module_config, &proxy_module);
1122 struct dirconn_entry *New;
1123 struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
1127 /* Don't duplicate entries */
1128 for (i = 0; i < conf->dirconn->nelts; i++) {
1129 if (strcasecmp(arg, list[i].name) == 0)
1134 New = apr_array_push(conf->dirconn);
1135 New->name = apr_pstrdup(parms->pool, arg);
1136 New->hostaddr = NULL;
1138 if (ap_proxy_is_ipaddr(New, parms->pool)) {
1140 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1141 "Parsed addr %s", inet_ntoa(New->addr));
1142 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1143 "Parsed mask %s", inet_ntoa(New->mask));
1146 else if (ap_proxy_is_domainname(New, parms->pool)) {
1147 ap_str_tolower(New->name);
1149 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1150 "Parsed domain %s", New->name);
1153 else if (ap_proxy_is_hostname(New, parms->pool)) {
1154 ap_str_tolower(New->name);
1156 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1157 "Parsed host %s", New->name);
1161 ap_proxy_is_word(New, parms->pool);
1163 fprintf(stderr, "Parsed word %s\n", New->name);
1171 set_proxy_domain(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 "ProxyDomain: domain name must start with a dot.";
1184 set_proxy_req(cmd_parms *parms, void *dummy, int flag)
1186 proxy_server_conf *psf =
1187 ap_get_module_config(parms->server->module_config, &proxy_module);
1192 if (flag && !psf->forward) {
1193 psf->forward = ap_proxy_create_worker(parms->pool);
1194 psf->forward->name = "proxy:forward";
1195 psf->forward->hostname = "*";
1196 psf->forward->scheme = "*";
1198 /* Do not disable worker in case of errors */
1199 psf->forward->status = PROXY_WORKER_IGNORE_ERRORS;
1205 set_proxy_error_override(cmd_parms *parms, void *dummy, int flag)
1207 proxy_server_conf *psf =
1208 ap_get_module_config(parms->server->module_config, &proxy_module);
1210 psf->error_override = flag;
1211 psf->error_override_set = 1;
1215 set_preserve_host(cmd_parms *parms, void *dummy, int flag)
1217 proxy_server_conf *psf =
1218 ap_get_module_config(parms->server->module_config, &proxy_module);
1220 psf->preserve_host = flag;
1221 psf->preserve_host_set = 1;
1226 set_recv_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
1228 proxy_server_conf *psf =
1229 ap_get_module_config(parms->server->module_config, &proxy_module);
1231 if (s < 512 && s != 0) {
1232 return "ProxyReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
1235 psf->recv_buffer_size = s;
1236 psf->recv_buffer_size_set = 1;
1241 set_io_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
1243 proxy_server_conf *psf =
1244 ap_get_module_config(parms->server->module_config, &proxy_module);
1247 psf->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
1248 psf->io_buffer_size_set = 1;
1253 set_max_forwards(cmd_parms *parms, void *dummy, const char *arg)
1255 proxy_server_conf *psf =
1256 ap_get_module_config(parms->server->module_config, &proxy_module);
1259 return "ProxyMaxForwards must be greater or equal to zero..";
1263 psf->maxfwd_set = 1;
1267 set_proxy_timeout(cmd_parms *parms, void *dummy, const char *arg)
1269 proxy_server_conf *psf =
1270 ap_get_module_config(parms->server->module_config, &proxy_module);
1275 return "Proxy Timeout must be at least 1 second.";
1278 psf->timeout=apr_time_from_sec(timeout);
1284 set_via_opt(cmd_parms *parms, void *dummy, const char *arg)
1286 proxy_server_conf *psf =
1287 ap_get_module_config(parms->server->module_config, &proxy_module);
1289 if (strcasecmp(arg, "Off") == 0)
1290 psf->viaopt = via_off;
1291 else if (strcasecmp(arg, "On") == 0)
1292 psf->viaopt = via_on;
1293 else if (strcasecmp(arg, "Block") == 0)
1294 psf->viaopt = via_block;
1295 else if (strcasecmp(arg, "Full") == 0)
1296 psf->viaopt = via_full;
1298 return "ProxyVia must be one of: "
1299 "off | on | full | block";
1302 psf->viaopt_set = 1;
1307 set_bad_opt(cmd_parms *parms, void *dummy, const char *arg)
1309 proxy_server_conf *psf =
1310 ap_get_module_config(parms->server->module_config, &proxy_module);
1312 if (strcasecmp(arg, "IsError") == 0)
1313 psf->badopt = bad_error;
1314 else if (strcasecmp(arg, "Ignore") == 0)
1315 psf->badopt = bad_ignore;
1316 else if (strcasecmp(arg, "StartBody") == 0)
1317 psf->badopt = bad_body;
1319 return "ProxyBadHeader must be one of: "
1320 "IsError | Ignore | StartBody";
1323 psf->badopt_set = 1;
1328 set_status_opt(cmd_parms *parms, void *dummy, const char *arg)
1330 proxy_server_conf *psf =
1331 ap_get_module_config(parms->server->module_config, &proxy_module);
1333 if (strcasecmp(arg, "Off") == 0)
1334 psf->proxy_status = status_off;
1335 else if (strcasecmp(arg, "On") == 0)
1336 psf->proxy_status = status_on;
1337 else if (strcasecmp(arg, "Full") == 0)
1338 psf->proxy_status = status_full;
1340 return "ProxyStatus must be one of: "
1344 psf->proxy_status_set = 1;
1348 static const char *add_member(cmd_parms *cmd, void *dummy, const char *arg)
1350 server_rec *s = cmd->server;
1351 proxy_server_conf *conf =
1352 ap_get_module_config(s->module_config, &proxy_module);
1353 proxy_balancer *balancer;
1354 proxy_worker *worker;
1355 char *path = cmd->path;
1358 apr_table_t *params = apr_table_make(cmd->pool, 5);
1359 const apr_array_header_t *arr;
1360 const apr_table_entry_t *elts;
1364 path = apr_pstrdup(cmd->pool, cmd->path);
1366 word = ap_getword_conf(cmd->pool, &arg);
1372 char *val = strchr(word, '=');
1375 return "BalancerMember can not have a balancer name when defined in a location";
1377 return "Invalid BalancerMember parameter. Parameter must "
1378 "be in the form 'key=value'";
1381 apr_table_setn(params, word, val);
1385 return "BalancerMember must define balancer name when outside <Proxy > section";
1387 return "BalancerMember must define remote proxy server";
1389 ap_str_tolower(path); /* lowercase scheme://hostname */
1390 ap_str_tolower(name); /* lowercase scheme://hostname */
1392 /* Try to find existing worker */
1393 worker = ap_proxy_get_worker(cmd->temp_pool, conf, name);
1396 if ((err = ap_proxy_add_worker(&worker, cmd->pool, conf, name)) != NULL)
1397 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1399 PROXY_COPY_CONF_PARAMS(worker, conf);
1401 arr = apr_table_elts(params);
1402 elts = (const apr_table_entry_t *)arr->elts;
1403 for (i = 0; i < arr->nelts; i++) {
1404 const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
1407 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1409 /* Try to find the balancer */
1410 balancer = ap_proxy_get_balancer(cmd->temp_pool, conf, path);
1412 const char *err = ap_proxy_add_balancer(&balancer,
1416 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1418 /* Add the worker to the load balancer */
1419 ap_proxy_add_worker_to_balancer(cmd->pool, balancer, worker);
1424 set_proxy_param(cmd_parms *cmd, void *dummy, const char *arg)
1426 server_rec *s = cmd->server;
1427 proxy_server_conf *conf =
1428 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
1431 proxy_balancer *balancer = NULL;
1432 proxy_worker *worker = NULL;
1435 if (cmd->directive->parent &&
1436 strncasecmp(cmd->directive->parent->directive,
1437 "<Proxy", 6) == 0) {
1438 const char *pargs = cmd->directive->parent->args;
1439 /* Directive inside <Proxy section
1440 * Parent directive arg is the worker/balancer name.
1442 name = ap_getword_conf(cmd->temp_pool, &pargs);
1443 if ((word = ap_strchr(name, '>')))
1447 /* Standard set directive with worker/balancer
1448 * name as first param.
1450 name = ap_getword_conf(cmd->temp_pool, &arg);
1453 if (strncasecmp(name, "balancer:", 9) == 0) {
1454 balancer = ap_proxy_get_balancer(cmd->pool, conf, name);
1456 return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
1457 name, "' Balancer.", NULL);
1461 worker = ap_proxy_get_worker(cmd->temp_pool, conf, name);
1463 return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
1464 name, "' Worker.", NULL);
1469 word = ap_getword_conf(cmd->pool, &arg);
1470 val = strchr(word, '=');
1472 return "Invalid ProxySet parameter. Parameter must be "
1473 "in the form 'key=value'";
1478 err = set_worker_param(cmd->pool, worker, word, val);
1480 err = set_balancer_param(cmd->pool, balancer, word, val);
1483 return apr_pstrcat(cmd->temp_pool, "ProxySet ", err, " ", word, " ", name, NULL);
1489 static void ap_add_per_proxy_conf(server_rec *s, ap_conf_vector_t *dir_config)
1491 proxy_server_conf *sconf = ap_get_module_config(s->module_config,
1493 void **new_space = (void **)apr_array_push(sconf->sec_proxy);
1495 *new_space = dir_config;
1498 static const char *proxysection(cmd_parms *cmd, void *mconfig, const char *arg)
1501 const char *endp = ap_strrchr_c(arg, '>');
1502 int old_overrides = cmd->override;
1503 char *old_path = cmd->path;
1504 proxy_dir_conf *conf;
1505 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1507 const command_rec *thiscmd = cmd->cmd;
1509 const char *err = ap_check_cmd_context(cmd,
1510 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1516 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1517 "> directive missing closing '>'", NULL);
1520 arg=apr_pstrndup(cmd->pool, arg, endp-arg);
1523 if (thiscmd->cmd_data)
1524 return "<ProxyMatch > block must specify a path";
1526 return "<Proxy > block must specify a path";
1529 cmd->path = ap_getword_conf(cmd->pool, &arg);
1530 cmd->override = OR_ALL|ACCESS_CONF;
1532 if (!strncasecmp(cmd->path, "proxy:", 6))
1535 /* XXX Ignore case? What if we proxy a case-insensitive server?!?
1536 * While we are at it, shouldn't we also canonicalize the entire
1537 * scheme? See proxy_fixup()
1539 if (thiscmd->cmd_data) { /* <ProxyMatch> */
1540 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1542 return "Regex could not be compiled";
1545 else if (!strcmp(cmd->path, "~")) {
1546 cmd->path = ap_getword_conf(cmd->pool, &arg);
1548 return "<Proxy ~ > block must specify a path";
1549 if (strncasecmp(cmd->path, "proxy:", 6))
1551 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1553 return "Regex could not be compiled";
1557 /* initialize our config and fetch it */
1558 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1559 &proxy_module, cmd->pool);
1561 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1566 conf->p = cmd->path;
1567 conf->p_is_fnmatch = apr_fnmatch_test(conf->p);
1569 ap_add_per_proxy_conf(cmd->server, new_dir_conf);
1572 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1573 "> arguments not (yet) supported.", NULL);
1576 cmd->path = old_path;
1577 cmd->override = old_overrides;
1582 static const command_rec proxy_cmds[] =
1584 AP_INIT_RAW_ARGS("<Proxy", proxysection, NULL, RSRC_CONF,
1585 "Container for directives affecting resources located in the proxied "
1587 AP_INIT_RAW_ARGS("<ProxyMatch", proxysection, (void*)1, RSRC_CONF,
1588 "Container for directives affecting resources located in the proxied "
1589 "location, in regular expression syntax"),
1590 AP_INIT_FLAG("ProxyRequests", set_proxy_req, NULL, RSRC_CONF,
1591 "on if the true proxy requests should be accepted"),
1592 AP_INIT_TAKE2("ProxyRemote", add_proxy_noregex, NULL, RSRC_CONF,
1593 "a scheme, partial URL or '*' and a proxy server"),
1594 AP_INIT_TAKE2("ProxyRemoteMatch", add_proxy_regex, NULL, RSRC_CONF,
1595 "a regex pattern and a proxy server"),
1596 AP_INIT_RAW_ARGS("ProxyPass", add_pass, NULL, RSRC_CONF|ACCESS_CONF,
1597 "a virtual path and a URL"),
1598 AP_INIT_TAKE12("ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF|ACCESS_CONF,
1599 "a virtual path and a URL for reverse proxy behaviour"),
1600 AP_INIT_TAKE2("ProxyPassReverseCookiePath", cookie_path, NULL,
1601 RSRC_CONF|ACCESS_CONF, "Path rewrite rule for proxying cookies"),
1602 AP_INIT_TAKE2("ProxyPassReverseCookieDomain", cookie_domain, NULL,
1603 RSRC_CONF|ACCESS_CONF, "Domain rewrite rule for proxying cookies"),
1604 AP_INIT_ITERATE("ProxyBlock", set_proxy_exclude, NULL, RSRC_CONF,
1605 "A list of names, hosts or domains to which the proxy will not connect"),
1606 AP_INIT_TAKE1("ProxyReceiveBufferSize", set_recv_buffer_size, NULL, RSRC_CONF,
1607 "Receive buffer size for outgoing HTTP and FTP connections in bytes"),
1608 AP_INIT_TAKE1("ProxyIOBufferSize", set_io_buffer_size, NULL, RSRC_CONF,
1609 "IO buffer size for outgoing HTTP and FTP connections in bytes"),
1610 AP_INIT_TAKE1("ProxyMaxForwards", set_max_forwards, NULL, RSRC_CONF,
1611 "The maximum number of proxies a request may be forwarded through."),
1612 AP_INIT_ITERATE("NoProxy", set_proxy_dirconn, NULL, RSRC_CONF,
1613 "A list of domains, hosts, or subnets to which the proxy will connect directly"),
1614 AP_INIT_TAKE1("ProxyDomain", set_proxy_domain, NULL, RSRC_CONF,
1615 "The default intranet domain name (in absence of a domain in the URL)"),
1616 AP_INIT_ITERATE("AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF,
1617 "A list of ports which CONNECT may connect to"),
1618 AP_INIT_TAKE1("ProxyVia", set_via_opt, NULL, RSRC_CONF,
1619 "Configure Via: proxy header header to one of: on | off | block | full"),
1620 AP_INIT_FLAG("ProxyErrorOverride", set_proxy_error_override, NULL, RSRC_CONF,
1621 "use our error handling pages instead of the servers' we are proxying"),
1622 AP_INIT_FLAG("ProxyPreserveHost", set_preserve_host, NULL, RSRC_CONF,
1623 "on if we should preserve host header while proxying"),
1624 AP_INIT_TAKE1("ProxyTimeout", set_proxy_timeout, NULL, RSRC_CONF,
1625 "Set the timeout (in seconds) for a proxied connection. "
1626 "This overrides the server timeout"),
1627 AP_INIT_TAKE1("ProxyBadHeader", set_bad_opt, NULL, RSRC_CONF,
1628 "How to handle bad header line in response: IsError | Ignore | StartBody"),
1629 AP_INIT_RAW_ARGS("BalancerMember", add_member, NULL, RSRC_CONF|ACCESS_CONF,
1630 "A balancer name and scheme with list of params"),
1631 AP_INIT_TAKE1("ProxyStatus", set_status_opt, NULL, RSRC_CONF,
1632 "Configure Status: proxy status to one of: on | off | full"),
1633 AP_INIT_RAW_ARGS("ProxySet", set_proxy_param, NULL, RSRC_CONF|ACCESS_CONF,
1634 "A balancer or worker name with list of params"),
1638 static APR_OPTIONAL_FN_TYPE(ssl_proxy_enable) *proxy_ssl_enable = NULL;
1639 static APR_OPTIONAL_FN_TYPE(ssl_engine_disable) *proxy_ssl_disable = NULL;
1641 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c)
1644 * if c == NULL just check if the optional function was imported
1645 * else run the optional function so ssl filters are inserted
1647 if (proxy_ssl_enable) {
1648 return c ? proxy_ssl_enable(c) : 1;
1654 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c)
1656 if (proxy_ssl_disable) {
1657 return proxy_ssl_disable(c);
1663 static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1664 apr_pool_t *ptemp, server_rec *s)
1666 proxy_server_conf *conf =
1667 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
1668 proxy_worker *worker;
1671 proxy_ssl_enable = APR_RETRIEVE_OPTIONAL_FN(ssl_proxy_enable);
1672 proxy_ssl_disable = APR_RETRIEVE_OPTIONAL_FN(ssl_engine_disable);
1674 /* Initialize workers */
1675 worker = (proxy_worker *)conf->workers->elts;
1676 for (i = 0; i < conf->workers->nelts; i++) {
1677 ap_proxy_initialize_worker(worker, s);
1680 /* Initialize forward worker if defined */
1682 ap_proxy_initialize_worker(conf->forward, s);
1689 #define MBYTE 1048576L
1690 #define GBYTE 1073741824L
1692 /* Format the number of bytes nicely */
1693 static void format_byte_out(request_rec *r, apr_off_t bytes)
1696 if (bytes < (5 * KBYTE))
1697 ap_rprintf(r, "%d B", (int) bytes);
1698 else if (bytes < (MBYTE / 2))
1699 ap_rprintf(r, "%.1f kB", (float) bytes / KBYTE);
1700 else if (bytes < (GBYTE / 2))
1701 ap_rprintf(r, "%.1f MB", (float) bytes / MBYTE);
1703 ap_rprintf(r, "%.1f GB", (float) bytes / GBYTE);
1707 * proxy Extension to mod_status
1709 static int proxy_status_hook(request_rec *r, int flags)
1712 void *sconf = r->server->module_config;
1713 proxy_server_conf *conf = (proxy_server_conf *)
1714 ap_get_module_config(sconf, &proxy_module);
1715 proxy_balancer *balancer = NULL;
1716 proxy_runtime_worker *worker = NULL;
1718 if (flags & AP_STATUS_SHORT || conf->balancers->nelts == 0 ||
1719 conf->proxy_status == status_off)
1722 balancer = (proxy_balancer *)conf->balancers->elts;
1723 for (i = 0; i < conf->balancers->nelts; i++) {
1724 ap_rputs("<hr />\n<h1>Proxy LoadBalancer Status for ", r);
1725 ap_rvputs(r, balancer->name, "</h1>\n\n", NULL);
1726 ap_rputs("\n\n<table border=\"0\"><tr>"
1727 "<th>SSes</th><th>Timeout</th>"
1729 ap_rvputs(r, "<td>", balancer->sticky, NULL);
1730 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>\n",
1731 apr_time_sec(balancer->timeout));
1732 ap_rputs("</table>\n", r);
1733 ap_rputs("\n\n<table border=\"0\"><tr>"
1734 "<th>Sch</th><th>Host</th><th>Stat</th>"
1735 "<th>Route</th><th>Redir</th>"
1736 "<th>F</th><th>Acc</th><th>Wr</th><th>Rd</th>"
1739 worker = (proxy_runtime_worker *)balancer->workers->elts;
1740 for (n = 0; n < balancer->workers->nelts; n++) {
1742 ap_rvputs(r, "<tr>\n<td>", worker->w->scheme, "</td>", NULL);
1743 ap_rvputs(r, "<td>", worker->w->hostname, "</td><td>", NULL);
1744 if (worker->w->status & PROXY_WORKER_DISABLED)
1746 else if (worker->w->status & PROXY_WORKER_IN_ERROR)
1748 else if (worker->w->status & PROXY_WORKER_INITIALIZED)
1752 ap_rvputs(r, "</td><td>", worker->w->route, NULL);
1753 ap_rvputs(r, "</td><td>", worker->w->redirect, NULL);
1754 ap_rprintf(r, "</td><td>%.2f</td>", worker->s->lbfactor);
1755 ap_rprintf(r, "<td>%d</td><td>", (int)(worker->s->elected));
1756 format_byte_out(r, worker->s->transfered);
1757 ap_rputs("</td><td>", r);
1758 format_byte_out(r, worker->s->transfered);
1759 ap_rputs("</td>\n", r);
1761 /* TODO: Add the rest of dynamic worker data */
1762 ap_rputs("</tr>\n", r);
1766 ap_rputs("</table>\n", r);
1769 ap_rputs("<hr /><table>\n"
1770 "<tr><th>SSes</th><td>Sticky session name</td></tr>\n"
1771 "<tr><th>Timeout</th><td>Balancer Timeout</td></tr>\n"
1772 "<tr><th>Sch</th><td>Connection scheme</td></tr>\n"
1773 "<tr><th>Host</th><td>Backend Hostname</td></tr>\n"
1774 "<tr><th>Stat</th><td>Worker status</td></tr>\n"
1775 "<tr><th>Route</th><td>Session Route</td></tr>\n"
1776 "<tr><th>Redir</th><td>Session Route Redirection</td></tr>\n"
1777 "<tr><th>F</th><td>Load Balancer Factor in %</td></tr>\n"
1778 "<tr><th>Acc</th><td>Number of requests</td></tr>\n"
1779 "<tr><th>Wr</th><td>Number of bytes transfered</td></tr>\n"
1780 "<tr><th>Rd</th><td>Number of bytes readed</td></tr>\n"
1787 * This routine is called before the server processes the configuration
1788 * files. There is no return value.
1790 static int proxy_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
1793 APR_OPTIONAL_HOOK(ap, status_hook, proxy_status_hook, NULL, NULL,
1798 static void register_hooks(apr_pool_t *p)
1800 /* fixup before mod_rewrite, so that the proxied url will not
1801 * escaped accidentally by our fixup.
1804 static const char * const aszSucc[]={ "mod_rewrite.c", NULL };
1807 APR_REGISTER_OPTIONAL_FN(ap_proxy_lb_workers);
1809 ap_hook_handler(proxy_handler, NULL, NULL, APR_HOOK_FIRST);
1810 /* filename-to-URI translation */
1811 ap_hook_translate_name(proxy_trans, NULL, NULL, APR_HOOK_FIRST);
1812 /* walk <Proxy > entries and suppress default TRACE behavior */
1813 ap_hook_map_to_storage(proxy_map_location, NULL,NULL, APR_HOOK_FIRST);
1816 ap_hook_fixups(proxy_fixup, NULL, aszSucc, APR_HOOK_FIRST);
1818 /* post read_request handling */
1819 ap_hook_post_read_request(proxy_detect, NULL, NULL, APR_HOOK_FIRST);
1820 /* pre config handling */
1821 ap_hook_pre_config(proxy_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1822 /* post config handling */
1823 ap_hook_post_config(proxy_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1826 module AP_MODULE_DECLARE_DATA proxy_module =
1828 STANDARD20_MODULE_STUFF,
1829 create_proxy_dir_config, /* create per-directory config structure */
1830 merge_proxy_dir_config, /* merge per-directory config structures */
1831 create_proxy_config, /* create per-server config structure */
1832 merge_proxy_config, /* merge per-server config structures */
1833 proxy_cmds, /* command table */
1838 APR_HOOK_LINK(scheme_handler)
1839 APR_HOOK_LINK(canon_handler)
1840 APR_HOOK_LINK(pre_request)
1841 APR_HOOK_LINK(post_request)
1844 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, scheme_handler,
1845 (request_rec *r, proxy_worker *worker,
1846 proxy_server_conf *conf,
1847 char *url, const char *proxyhost,
1848 apr_port_t proxyport),(r,worker,conf,
1849 url,proxyhost,proxyport),DECLINED)
1850 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, canon_handler,
1851 (request_rec *r, char *url),(r,
1853 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, pre_request, (
1854 proxy_worker **worker,
1855 proxy_balancer **balancer,
1857 proxy_server_conf *conf,
1858 char **url),(worker,balancer,
1859 r,conf,url),DECLINED)
1860 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, post_request,
1861 (proxy_worker *worker,
1862 proxy_balancer *balancer,
1864 proxy_server_conf *conf),(worker,
1865 balancer,r,conf),DECLINED)
1866 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, fixups,
1867 (request_rec *r), (r),