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 /* Normalized load factor. Used with BalancerMamber,
95 * it is a number between 1 and 100.
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 if (strlen(val) > PROXY_WORKER_MAX_ROUTE_SIZ)
193 return "Route length must be < 64 characters";
194 worker->route = apr_pstrdup(p, val);
196 else if (!strcasecmp(key, "redirect")) {
197 /* Worker redirection route.
199 if (strlen(val) > PROXY_WORKER_MAX_ROUTE_SIZ)
200 return "Redirect length must be < 64 characters";
201 worker->redirect = apr_pstrdup(p, val);
204 return "unknown Worker parameter";
209 static const char *set_balancer_param(apr_pool_t *p,
210 proxy_balancer *balancer,
216 if (!strcasecmp(key, "stickysession")) {
217 /* Balancer sticky session name.
218 * Set to something like JSESSIONID or
219 * PHPSESSIONID, etc..,
221 balancer->sticky = apr_pstrdup(p, val);
223 else if (!strcasecmp(key, "nofailover")) {
224 /* If set to 'on' the session will break
225 * if the worker is in error state or
228 if (!strcasecmp(val, "on"))
229 balancer->sticky_force = 1;
230 else if (!strcasecmp(val, "off"))
231 balancer->sticky_force = 0;
233 return "failover must be On|Off";
235 else if (!strcasecmp(key, "timeout")) {
236 /* Balancer timeout in seconds.
237 * If set this will be the maximum time to
238 * wait for a free worker.
239 * Default is not to wait.
243 return "timeout must be at least one second";
244 balancer->timeout = apr_time_from_sec(ival);
246 else if (!strcasecmp(key, "maxattempts")) {
247 /* Maximum number of failover attempts before
252 return "maximum number of attempts must be a positive number";
253 balancer->max_attempts = ival;
254 balancer->max_attempts_set = 1;
256 else if (!strcasecmp(key, "lbmethod")) {
257 /* Which LB scheduler method */
258 if (!strcasecmp(val, "traffic"))
259 balancer->lbmethod = lbmethod_traffic;
260 else if (!strcasecmp(val, "requests"))
261 balancer->lbmethod = lbmethod_requests;
263 return "lbmethod must be Traffic|Requests";
266 return "unknown Balancer parameter";
271 static int alias_match(const char *uri, const char *alias_fakename)
273 const char *end_fakename = alias_fakename + strlen(alias_fakename);
274 const char *aliasp = alias_fakename, *urip = uri;
275 const char *end_uri = uri + strlen(uri);
276 unsigned char uric, aliasc;
278 while (aliasp < end_fakename && urip < end_uri) {
279 if (*aliasp == '/') {
280 /* any number of '/' in the alias matches any number in
281 * the supplied URI, but there must be at least one...
286 while (*aliasp == '/')
293 /* Other characters are compared literally */
294 if (*urip++ != *aliasp++)
297 /* Other characters are canonicalised and compared literally */
302 uric = (unsigned char)*urip++;
304 if (*aliasp == '%') {
305 aliasc = hex2c(aliasp);
308 aliasc = (unsigned char)*aliasp++;
310 if (uric != aliasc) {
317 /* fixup badly encoded stuff (e.g. % as last character) */
318 if (aliasp > end_fakename) {
319 aliasp = end_fakename;
321 if (urip > end_uri) {
325 /* We reach the end of the uri before the end of "alias_fakename"
326 * for example uri is "/" and alias_fakename "/examples"
328 if (urip == end_uri && aliasp!=end_fakename) {
332 /* Check last alias path component matched all the way */
333 if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
336 /* Return number of characters from URI which matched (may be
337 * greater than length of alias, since we may have matched
344 /* Detect if an absoluteURI should be proxied or not. Note that we
345 * have to do this during this phase because later phases are
346 * "short-circuiting"... i.e. translate_names will end when the first
347 * module returns OK. So for example, if the request is something like:
349 * GET http://othervhost/cgi-bin/printenv HTTP/1.0
351 * mod_alias will notice the /cgi-bin part and ScriptAlias it and
352 * short-circuit the proxy... just because of the ordering in the
353 * configuration file.
355 static int proxy_detect(request_rec *r)
357 void *sconf = r->server->module_config;
358 proxy_server_conf *conf =
359 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
362 struct proxy_alias *ent = (struct proxy_alias *)conf->aliases->elts;
365 /* Ick... msvc (perhaps others) promotes ternary short results to int */
367 if (conf->req && r->parsed_uri.scheme) {
368 /* but it might be something vhosted */
369 if (!(r->parsed_uri.hostname
370 && !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))
371 && ap_matches_request_vhost(r, r->parsed_uri.hostname,
372 (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
373 : ap_default_port(r))))) {
374 r->proxyreq = PROXYREQ_PROXY;
375 r->uri = r->unparsed_uri;
376 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
377 r->handler = "proxy-server";
380 /* We need special treatment for CONNECT proxying: it has no scheme part */
381 else if (conf->req && r->method_number == M_CONNECT
382 && r->parsed_uri.hostname
383 && r->parsed_uri.port_str) {
384 r->proxyreq = PROXYREQ_PROXY;
385 r->uri = r->unparsed_uri;
386 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
387 r->handler = "proxy-server";
390 /* test for a ProxyPass */
391 for (i = 0; i < conf->aliases->nelts; i++) {
392 len = alias_match(r->unparsed_uri, ent[i].fake);
394 if ((ent[i].real[0] == '!') && (ent[i].real[1] == 0)) {
397 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
398 r->unparsed_uri + len, NULL);
399 r->handler = "proxy-server";
400 r->proxyreq = PROXYREQ_REVERSE;
401 r->uri = r->unparsed_uri;
410 static int proxy_trans(request_rec *r)
413 void *sconf = r->server->module_config;
414 proxy_server_conf *conf =
415 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
417 struct proxy_alias *ent = (struct proxy_alias *) conf->aliases->elts;
421 /* someone has already set up the proxy, it was possibly ourselves
428 /* XXX: since r->uri has been manipulated already we're not really
429 * compliant with RFC1945 at this point. But this probably isn't
430 * an issue because this is a hybrid proxy/origin server.
433 for (i = 0; i < conf->aliases->nelts; i++) {
434 len = alias_match(r->uri, ent[i].fake);
437 if ((ent[i].real[0] == '!') && (ent[i].real[1] == 0)) {
441 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
443 r->handler = "proxy-server";
444 r->proxyreq = PROXYREQ_REVERSE;
452 static int proxy_walk(request_rec *r)
454 proxy_server_conf *sconf = ap_get_module_config(r->server->module_config,
456 ap_conf_vector_t *per_dir_defaults = r->server->lookup_defaults;
457 ap_conf_vector_t **sec_proxy = (ap_conf_vector_t **) sconf->sec_proxy->elts;
458 ap_conf_vector_t *entry_config;
459 proxy_dir_conf *entry_proxy;
460 int num_sec = sconf->sec_proxy->nelts;
461 /* XXX: shouldn't we use URI here? Canonicalize it first?
462 * Pass over "proxy:" prefix
464 const char *proxyname = r->filename + 6;
467 for (j = 0; j < num_sec; ++j)
469 entry_config = sec_proxy[j];
470 entry_proxy = ap_get_module_config(entry_config, &proxy_module);
472 /* XXX: What about case insensitive matching ???
473 * Compare regex, fnmatch or string as appropriate
474 * If the entry doesn't relate, then continue
477 ? ap_regexec(entry_proxy->r, proxyname, 0, NULL, 0)
478 : (entry_proxy->p_is_fnmatch
479 ? apr_fnmatch(entry_proxy->p, proxyname, 0)
480 : strncmp(proxyname, entry_proxy->p,
481 strlen(entry_proxy->p)))) {
484 per_dir_defaults = ap_merge_per_dir_configs(r->pool, per_dir_defaults,
488 r->per_dir_config = per_dir_defaults;
493 static int proxy_map_location(request_rec *r)
497 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
500 /* Don't let the core or mod_http map_to_storage hooks handle this,
501 * We don't need directory/file_walk, and we want to TRACE on our own.
503 if ((access_status = proxy_walk(r))) {
504 ap_die(access_status, r);
505 return access_status;
511 /* -------------------------------------------------------------- */
512 /* Fixup the filename */
515 * Canonicalise the URL
517 static int proxy_fixup(request_rec *r)
522 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
526 /* We definitely shouldn't canonicalize a proxy_pass.
527 * But should we really canonicalize a STD_PROXY??? -- Fahree
529 if (r->proxyreq == PROXYREQ_REVERSE) {
534 /* XXX: Shouldn't we try this before we run the proxy_walk? */
535 url = &r->filename[6];
537 /* canonicalise each specific scheme */
538 if ((access_status = proxy_run_canon_handler(r, url))) {
539 return access_status;
542 p = strchr(url, ':');
543 if (p == NULL || p == url)
544 return HTTP_BAD_REQUEST;
546 return OK; /* otherwise; we've done the best we can */
549 /* Send a redirection if the request contains a hostname which is not */
550 /* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
551 /* servers like Netscape's allow this and access hosts from the local */
552 /* domain in this case. I think it is better to redirect to a FQDN, since */
553 /* these will later be found in the bookmarks files. */
554 /* The "ProxyDomain" directive determines what domain will be appended */
555 static int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
560 /* We only want to worry about GETs */
561 if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
564 /* If host does contain a dot already, or it is "localhost", decline */
565 if (strchr(r->parsed_uri.hostname, '.') != NULL
566 || strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
567 return DECLINED; /* host name has a dot already */
569 ref = apr_table_get(r->headers_in, "Referer");
571 /* Reassemble the request, but insert the domain after the host name */
572 /* Note that the domain name always starts with a dot */
573 r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
575 nuri = apr_uri_unparse(r->pool,
577 APR_URI_UNP_REVEALPASSWORD);
579 apr_table_set(r->headers_out, "Location", nuri);
580 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
581 "Domain missing: %s sent to %s%s%s", r->uri,
582 apr_uri_unparse(r->pool, &r->parsed_uri,
583 APR_URI_UNP_OMITUSERINFO),
584 ref ? " from " : "", ref ? ref : "");
586 return HTTP_MOVED_PERMANENTLY;
589 /* -------------------------------------------------------------- */
592 static int proxy_handler(request_rec *r)
594 char *url, *scheme, *p;
596 void *sconf = r->server->module_config;
597 proxy_server_conf *conf = (proxy_server_conf *)
598 ap_get_module_config(sconf, &proxy_module);
599 apr_array_header_t *proxies = conf->proxies;
600 struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
601 int i, rc, access_status;
602 int direct_connect = 0;
605 proxy_balancer *balancer = NULL;
606 proxy_worker *worker = NULL;
607 int attempts = 0, max_attempts = 0;
609 /* is this for us? */
610 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
613 /* handle max-forwards / OPTIONS / TRACE */
614 if ((str = apr_table_get(r->headers_in, "Max-Forwards"))) {
615 maxfwd = strtol(str, NULL, 10);
617 switch (r->method_number) {
620 r->proxyreq = PROXYREQ_NONE;
621 if ((access_status = ap_send_http_trace(r)))
622 ap_die(access_status, r);
624 ap_finalize_request_protocol(r);
629 r->proxyreq = PROXYREQ_NONE;
630 if ((access_status = ap_send_http_options(r)))
631 ap_die(access_status, r);
633 ap_finalize_request_protocol(r);
637 return ap_proxyerror(r, HTTP_BAD_GATEWAY,
638 "Max-Forwards has reached zero - proxy loop?");
642 maxfwd = (maxfwd > 0) ? maxfwd - 1 : 0;
645 /* set configured max-forwards */
646 maxfwd = conf->maxfwd;
648 apr_table_set(r->headers_in, "Max-Forwards",
649 apr_psprintf(r->pool, "%ld", (maxfwd > 0) ? maxfwd : 0));
652 url = r->filename + 6;
653 p = strchr(url, ':');
655 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
656 "proxy_handler no URL in %s", r->filename);
657 return HTTP_BAD_REQUEST;
660 /* If the host doesn't have a domain name, add one and redirect. */
661 if (conf->domain != NULL) {
662 rc = proxy_needsdomain(r, url, conf->domain);
663 if (ap_is_HTTP_REDIRECT(rc))
664 return HTTP_MOVED_PERMANENTLY;
668 scheme = apr_pstrdup(r->pool, url);
671 /* Check URI's destination host against NoProxy hosts */
672 /* Bypass ProxyRemote server lookup if configured as NoProxy */
673 /* we only know how to handle communication to a proxy via http */
674 /*if (strcasecmp(scheme, "http") == 0) */
677 struct dirconn_entry *list = (struct dirconn_entry *)
680 for (direct_connect = ii = 0; ii < conf->dirconn->nelts &&
681 !direct_connect; ii++) {
682 direct_connect = list[ii].matcher(&list[ii], r);
685 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
686 (direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
691 /* Try to obtain the most suitable worker */
692 access_status = ap_proxy_pre_request(&worker, &balancer, r, conf, &url);
693 if (access_status != OK)
694 return access_status;
695 if (balancer && balancer->max_attempts_set && !max_attempts)
696 max_attempts = balancer->max_attempts;
697 /* firstly, try a proxy, unless a NoProxy directive is active */
698 if (!direct_connect) {
699 for (i = 0; i < proxies->nelts; i++) {
700 p2 = ap_strchr_c(ents[i].scheme, ':'); /* is it a partial URL? */
701 if (strcmp(ents[i].scheme, "*") == 0 ||
702 (ents[i].use_regex && ap_regexec(ents[i].regexp, url,
704 (p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
706 strncasecmp(url, ents[i].scheme,
707 strlen(ents[i].scheme)) == 0)) {
709 /* handle the scheme */
710 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
711 "Trying to run scheme_handler against proxy");
712 access_status = proxy_run_scheme_handler(r, worker,
717 /* an error or success */
718 if (access_status != DECLINED &&
719 access_status != HTTP_BAD_GATEWAY) {
722 /* we failed to talk to the upstream proxy */
727 /* otherwise, try it direct */
728 /* N.B. what if we're behind a firewall, where we must use a proxy or
732 /* handle the scheme */
733 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
734 "Running scheme %s handler (attempt %d)",
736 if ((access_status = proxy_run_scheme_handler(r, worker, conf,
737 url, NULL, 0)) == OK)
740 } while (!PROXY_WORKER_IS_USABLE(worker) &&
741 max_attempts > attempts++);
743 if (DECLINED == access_status) {
744 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
745 "proxy: No protocol handler was valid for the URL %s. "
746 "If you are using a DSO version of mod_proxy, make sure "
747 "the proxy submodules are included in the configuration "
748 "using LoadModule.", r->uri);
749 access_status = HTTP_FORBIDDEN;
754 int post_status = proxy_run_post_request(worker, balancer, r, conf);
755 if (post_status == DECLINED) {
756 post_status = OK; /* no post_request handler available */
757 /* TODO: reclycle direct worker */
760 return access_status;
763 /* -------------------------------------------------------------- */
764 /* Setup configurable data */
766 static void * create_proxy_config(apr_pool_t *p, server_rec *s)
768 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
770 ps->sec_proxy = apr_array_make(p, 10, sizeof(ap_conf_vector_t *));
771 ps->proxies = apr_array_make(p, 10, sizeof(struct proxy_remote));
772 ps->aliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
773 ps->raliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
774 ps->cookie_paths = apr_array_make(p, 10, sizeof(struct proxy_alias));
775 ps->cookie_domains = apr_array_make(p, 10, sizeof(struct proxy_alias));
776 ps->cookie_path_str = apr_strmatch_precompile(p, "path=", 0);
777 ps->cookie_domain_str = apr_strmatch_precompile(p, "domain=", 0);
778 ps->noproxies = apr_array_make(p, 10, sizeof(struct noproxy_entry));
779 ps->dirconn = apr_array_make(p, 10, sizeof(struct dirconn_entry));
780 ps->allowed_connect_ports = apr_array_make(p, 10, sizeof(int));
781 ps->workers = apr_array_make(p, 10, sizeof(proxy_worker));
782 ps->balancers = apr_array_make(p, 10, sizeof(proxy_balancer));
786 ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
787 ps->viaopt_set = 0; /* 0 means default */
790 ps->recv_buffer_size = 0; /* this default was left unset for some reason */
791 ps->recv_buffer_size_set = 0;
792 ps->io_buffer_size = AP_IOBUFSIZE;
793 ps->io_buffer_size_set = 0;
794 ps->maxfwd = DEFAULT_MAX_FORWARDS;
796 ps->error_override = 0;
797 ps->error_override_set = 0;
798 ps->preserve_host_set = 0;
799 ps->preserve_host = 0;
802 ps->badopt = bad_error;
808 static void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
810 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
811 proxy_server_conf *base = (proxy_server_conf *) basev;
812 proxy_server_conf *overrides = (proxy_server_conf *) overridesv;
814 ps->proxies = apr_array_append(p, base->proxies, overrides->proxies);
815 ps->sec_proxy = apr_array_append(p, base->sec_proxy, overrides->sec_proxy);
816 ps->aliases = apr_array_append(p, base->aliases, overrides->aliases);
817 ps->raliases = apr_array_append(p, base->raliases, overrides->raliases);
819 = apr_array_append(p, base->cookie_paths, overrides->cookie_paths);
821 = apr_array_append(p, base->cookie_domains, overrides->cookie_domains);
822 ps->cookie_path_str = base->cookie_path_str;
823 ps->cookie_domain_str = base->cookie_domain_str;
824 ps->noproxies = apr_array_append(p, base->noproxies, overrides->noproxies);
825 ps->dirconn = apr_array_append(p, base->dirconn, overrides->dirconn);
826 ps->allowed_connect_ports = apr_array_append(p, base->allowed_connect_ports, overrides->allowed_connect_ports);
827 ps->workers = apr_array_append(p, base->workers, overrides->workers);
828 ps->balancers = apr_array_append(p, base->balancers, overrides->balancers);
829 ps->forward = overrides->forward ? overrides->forward : base->forward;
830 ps->reverse = overrides->reverse ? overrides->reverse : base->reverse;
832 ps->domain = (overrides->domain == NULL) ? base->domain : overrides->domain;
833 ps->viaopt = (overrides->viaopt_set == 0) ? base->viaopt : overrides->viaopt;
834 ps->req = (overrides->req_set == 0) ? base->req : overrides->req;
835 ps->recv_buffer_size = (overrides->recv_buffer_size_set == 0) ? base->recv_buffer_size : overrides->recv_buffer_size;
836 ps->io_buffer_size = (overrides->io_buffer_size_set == 0) ? base->io_buffer_size : overrides->io_buffer_size;
837 ps->maxfwd = (overrides->maxfwd_set == 0) ? base->maxfwd : overrides->maxfwd;
838 ps->error_override = (overrides->error_override_set == 0) ? base->error_override : overrides->error_override;
839 ps->preserve_host = (overrides->preserve_host_set == 0) ? base->preserve_host : overrides->preserve_host;
840 ps->timeout= (overrides->timeout_set == 0) ? base->timeout : overrides->timeout;
841 ps->badopt = (overrides->badopt_set == 0) ? base->badopt : overrides->badopt;
842 ps->proxy_status = (overrides->proxy_status_set == 0) ? base->proxy_status : overrides->proxy_status;
847 static void *create_proxy_dir_config(apr_pool_t *p, char *dummy)
849 proxy_dir_conf *new =
850 (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
852 /* Filled in by proxysection, when applicable */
857 static void *merge_proxy_dir_config(apr_pool_t *p, void *basev, void *addv)
859 proxy_dir_conf *new = (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
860 proxy_dir_conf *add = (proxy_dir_conf *) addv;
863 new->p_is_fnmatch = add->p_is_fnmatch;
870 add_proxy(cmd_parms *cmd, void *dummy, const char *f1, const char *r1, int regex)
872 server_rec *s = cmd->server;
873 proxy_server_conf *conf =
874 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
875 struct proxy_remote *new;
877 char *r, *f, *scheme;
881 r = apr_pstrdup(cmd->pool, r1);
882 scheme = apr_pstrdup(cmd->pool, r1);
883 f = apr_pstrdup(cmd->pool, f1);
885 if (p == NULL || p[1] != '/' || p[2] != '/' || p[3] == '\0') {
887 return "ProxyRemoteMatch: Bad syntax for a remote proxy server";
889 return "ProxyRemote: Bad syntax for a remote proxy server";
894 q = strchr(p + 3, ':');
896 if (sscanf(q + 1, "%u", &port) != 1 || port > 65535) {
898 return "ProxyRemoteMatch: Bad syntax for a remote proxy server (bad port number)";
900 return "ProxyRemote: Bad syntax for a remote proxy server (bad port number)";
908 reg = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
910 return "Regular expression for ProxyRemoteMatch could not be compiled.";
913 if (strchr(f, ':') == NULL)
914 ap_str_tolower(f); /* lowercase scheme */
915 ap_str_tolower(p + 3); /* lowercase hostname */
918 port = apr_uri_port_of_scheme(scheme);
921 new = apr_array_push(conf->proxies);
924 new->hostname = p + 3;
927 new->use_regex = regex;
932 add_proxy_noregex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
934 return add_proxy(cmd, dummy, f1, r1, 0);
938 add_proxy_regex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
940 return add_proxy(cmd, dummy, f1, r1, 1);
944 add_pass(cmd_parms *cmd, void *dummy, const char *arg)
946 server_rec *s = cmd->server;
947 proxy_server_conf *conf =
948 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
949 struct proxy_alias *new;
953 apr_table_t *params = apr_table_make(cmd->pool, 5);
954 const apr_array_header_t *arr;
955 const apr_table_entry_t *elts;
959 word = ap_getword_conf(cmd->pool, &arg);
965 char *val = strchr(word, '=');
968 return "Invalid ProxyPass parameter. Parameter must be "
969 "in the form 'key=value'";
971 return "ProxyPass can not have a path when defined in a location";
975 apr_table_setn(params, word, val);
980 return "ProxyPass needs a path when not defined in a location";
982 new = apr_array_push(conf->aliases);
983 new->fake = apr_pstrdup(cmd->pool, f);
984 new->real = apr_pstrdup(cmd->pool, r);
985 if (r[0] == '!' && r[1] == '\0')
988 arr = apr_table_elts(params);
989 elts = (const apr_table_entry_t *)arr->elts;
990 /* Distinguish the balancer from woker */
991 if (strncasecmp(r, "balancer:", 9) == 0) {
992 proxy_balancer *balancer = ap_proxy_get_balancer(cmd->pool, conf, r);
994 const char *err = ap_proxy_add_balancer(&balancer,
998 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
1000 for (i = 0; i < arr->nelts; i++) {
1001 const char *err = set_balancer_param(cmd->pool, balancer, elts[i].key,
1004 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
1008 proxy_worker *worker = ap_proxy_get_worker(cmd->temp_pool, conf, r);
1010 const char *err = ap_proxy_add_worker(&worker, cmd->pool, conf, r);
1012 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
1014 PROXY_COPY_CONF_PARAMS(worker, conf);
1016 for (i = 0; i < arr->nelts; i++) {
1017 const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
1020 return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
1027 add_pass_reverse(cmd_parms *cmd, void *dummy, const char *f, const char *r)
1029 server_rec *s = cmd->server;
1030 proxy_server_conf *conf;
1031 struct proxy_alias *new;
1033 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
1035 if (r!=NULL && cmd->path == NULL ) {
1036 new = apr_array_push(conf->raliases);
1039 } else if (r==NULL && cmd->path != NULL) {
1040 new = apr_array_push(conf->raliases);
1041 new->fake = cmd->path;
1045 return "ProxyPassReverse needs a path when not defined in a location";
1047 return "ProxyPassReverse can not have a path when defined in a location";
1053 cookie_path(cmd_parms *cmd, void *dummy, const char *f, const char *r)
1055 server_rec *s = cmd->server;
1056 proxy_server_conf *conf;
1057 struct proxy_alias *new;
1059 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
1061 new = apr_array_push(conf->cookie_paths);
1068 cookie_domain(cmd_parms *cmd, void *dummy, const char *f, const char *r)
1070 server_rec *s = cmd->server;
1071 proxy_server_conf *conf;
1072 struct proxy_alias *new;
1074 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
1076 new = apr_array_push(conf->cookie_domains);
1084 set_proxy_exclude(cmd_parms *parms, void *dummy, const char *arg)
1086 server_rec *s = parms->server;
1087 proxy_server_conf *conf =
1088 ap_get_module_config(s->module_config, &proxy_module);
1089 struct noproxy_entry *new;
1090 struct noproxy_entry *list = (struct noproxy_entry *) conf->noproxies->elts;
1091 struct apr_sockaddr_t *addr;
1095 /* Don't duplicate entries */
1096 for (i = 0; i < conf->noproxies->nelts; i++) {
1097 if (apr_strnatcasecmp(arg, list[i].name) == 0) { /* ignore case for host names */
1103 new = apr_array_push(conf->noproxies);
1105 if (APR_SUCCESS == apr_sockaddr_info_get(&addr, new->name, APR_UNSPEC, 0, 0, parms->pool)) {
1116 * Set the ports CONNECT can use
1119 set_allowed_ports(cmd_parms *parms, void *dummy, const char *arg)
1121 server_rec *s = parms->server;
1122 proxy_server_conf *conf =
1123 ap_get_module_config(s->module_config, &proxy_module);
1126 if (!apr_isdigit(arg[0]))
1127 return "AllowCONNECT: port number must be numeric";
1129 New = apr_array_push(conf->allowed_connect_ports);
1134 /* Similar to set_proxy_exclude(), but defining directly connected hosts,
1135 * which should never be accessed via the configured ProxyRemote servers
1138 set_proxy_dirconn(cmd_parms *parms, void *dummy, const char *arg)
1140 server_rec *s = parms->server;
1141 proxy_server_conf *conf =
1142 ap_get_module_config(s->module_config, &proxy_module);
1143 struct dirconn_entry *New;
1144 struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
1148 /* Don't duplicate entries */
1149 for (i = 0; i < conf->dirconn->nelts; i++) {
1150 if (strcasecmp(arg, list[i].name) == 0)
1155 New = apr_array_push(conf->dirconn);
1156 New->name = apr_pstrdup(parms->pool, arg);
1157 New->hostaddr = NULL;
1159 if (ap_proxy_is_ipaddr(New, parms->pool)) {
1161 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1162 "Parsed addr %s", inet_ntoa(New->addr));
1163 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1164 "Parsed mask %s", inet_ntoa(New->mask));
1167 else if (ap_proxy_is_domainname(New, parms->pool)) {
1168 ap_str_tolower(New->name);
1170 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1171 "Parsed domain %s", New->name);
1174 else if (ap_proxy_is_hostname(New, parms->pool)) {
1175 ap_str_tolower(New->name);
1177 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1178 "Parsed host %s", New->name);
1182 ap_proxy_is_word(New, parms->pool);
1184 fprintf(stderr, "Parsed word %s\n", New->name);
1192 set_proxy_domain(cmd_parms *parms, void *dummy, const char *arg)
1194 proxy_server_conf *psf =
1195 ap_get_module_config(parms->server->module_config, &proxy_module);
1198 return "ProxyDomain: domain name must start with a dot.";
1205 set_proxy_req(cmd_parms *parms, void *dummy, int flag)
1207 proxy_server_conf *psf =
1208 ap_get_module_config(parms->server->module_config, &proxy_module);
1213 if (flag && !psf->forward) {
1214 psf->forward = ap_proxy_create_worker(parms->pool);
1215 psf->forward->name = "proxy:forward";
1216 psf->forward->hostname = "*";
1217 psf->forward->scheme = "*";
1223 set_proxy_error_override(cmd_parms *parms, void *dummy, int flag)
1225 proxy_server_conf *psf =
1226 ap_get_module_config(parms->server->module_config, &proxy_module);
1228 psf->error_override = flag;
1229 psf->error_override_set = 1;
1233 set_preserve_host(cmd_parms *parms, void *dummy, int flag)
1235 proxy_server_conf *psf =
1236 ap_get_module_config(parms->server->module_config, &proxy_module);
1238 psf->preserve_host = flag;
1239 psf->preserve_host_set = 1;
1244 set_recv_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
1246 proxy_server_conf *psf =
1247 ap_get_module_config(parms->server->module_config, &proxy_module);
1249 if (s < 512 && s != 0) {
1250 return "ProxyReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
1253 psf->recv_buffer_size = s;
1254 psf->recv_buffer_size_set = 1;
1259 set_io_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
1261 proxy_server_conf *psf =
1262 ap_get_module_config(parms->server->module_config, &proxy_module);
1265 psf->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
1266 psf->io_buffer_size_set = 1;
1271 set_max_forwards(cmd_parms *parms, void *dummy, const char *arg)
1273 proxy_server_conf *psf =
1274 ap_get_module_config(parms->server->module_config, &proxy_module);
1277 return "ProxyMaxForwards must be greater or equal to zero..";
1281 psf->maxfwd_set = 1;
1285 set_proxy_timeout(cmd_parms *parms, void *dummy, const char *arg)
1287 proxy_server_conf *psf =
1288 ap_get_module_config(parms->server->module_config, &proxy_module);
1293 return "Proxy Timeout must be at least 1 second.";
1296 psf->timeout=apr_time_from_sec(timeout);
1302 set_via_opt(cmd_parms *parms, void *dummy, const char *arg)
1304 proxy_server_conf *psf =
1305 ap_get_module_config(parms->server->module_config, &proxy_module);
1307 if (strcasecmp(arg, "Off") == 0)
1308 psf->viaopt = via_off;
1309 else if (strcasecmp(arg, "On") == 0)
1310 psf->viaopt = via_on;
1311 else if (strcasecmp(arg, "Block") == 0)
1312 psf->viaopt = via_block;
1313 else if (strcasecmp(arg, "Full") == 0)
1314 psf->viaopt = via_full;
1316 return "ProxyVia must be one of: "
1317 "off | on | full | block";
1320 psf->viaopt_set = 1;
1325 set_bad_opt(cmd_parms *parms, void *dummy, const char *arg)
1327 proxy_server_conf *psf =
1328 ap_get_module_config(parms->server->module_config, &proxy_module);
1330 if (strcasecmp(arg, "IsError") == 0)
1331 psf->badopt = bad_error;
1332 else if (strcasecmp(arg, "Ignore") == 0)
1333 psf->badopt = bad_ignore;
1334 else if (strcasecmp(arg, "StartBody") == 0)
1335 psf->badopt = bad_body;
1337 return "ProxyBadHeader must be one of: "
1338 "IsError | Ignore | StartBody";
1341 psf->badopt_set = 1;
1346 set_status_opt(cmd_parms *parms, void *dummy, const char *arg)
1348 proxy_server_conf *psf =
1349 ap_get_module_config(parms->server->module_config, &proxy_module);
1351 if (strcasecmp(arg, "Off") == 0)
1352 psf->proxy_status = status_off;
1353 else if (strcasecmp(arg, "On") == 0)
1354 psf->proxy_status = status_on;
1355 else if (strcasecmp(arg, "Full") == 0)
1356 psf->proxy_status = status_full;
1358 return "ProxyStatus must be one of: "
1362 psf->proxy_status_set = 1;
1366 static const char *add_member(cmd_parms *cmd, void *dummy, const char *arg)
1368 server_rec *s = cmd->server;
1369 proxy_server_conf *conf =
1370 ap_get_module_config(s->module_config, &proxy_module);
1371 proxy_balancer *balancer;
1372 proxy_worker *worker;
1373 char *path = cmd->path;
1376 apr_table_t *params = apr_table_make(cmd->pool, 5);
1377 const apr_array_header_t *arr;
1378 const apr_table_entry_t *elts;
1382 path = apr_pstrdup(cmd->pool, cmd->path);
1384 word = ap_getword_conf(cmd->pool, &arg);
1390 char *val = strchr(word, '=');
1393 return "BalancerMember can not have a balancer name when defined in a location";
1395 return "Invalid BalancerMember parameter. Parameter must "
1396 "be in the form 'key=value'";
1399 apr_table_setn(params, word, val);
1403 return "BalancerMember must define balancer name when outside <Proxy > section";
1405 return "BalancerMember must define remote proxy server";
1407 ap_str_tolower(path); /* lowercase scheme://hostname */
1408 ap_str_tolower(name); /* lowercase scheme://hostname */
1410 /* Try to find existing worker */
1411 worker = ap_proxy_get_worker(cmd->temp_pool, conf, name);
1414 if ((err = ap_proxy_add_worker(&worker, cmd->pool, conf, name)) != NULL)
1415 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1417 PROXY_COPY_CONF_PARAMS(worker, conf);
1419 arr = apr_table_elts(params);
1420 elts = (const apr_table_entry_t *)arr->elts;
1421 for (i = 0; i < arr->nelts; i++) {
1422 const char *err = set_worker_param(cmd->pool, worker, elts[i].key,
1425 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1427 /* Try to find the balancer */
1428 balancer = ap_proxy_get_balancer(cmd->temp_pool, conf, path);
1430 const char *err = ap_proxy_add_balancer(&balancer,
1434 return apr_pstrcat(cmd->temp_pool, "BalancerMember ", err, NULL);
1436 /* Add the worker to the load balancer */
1437 ap_proxy_add_worker_to_balancer(cmd->pool, balancer, worker);
1442 set_proxy_param(cmd_parms *cmd, void *dummy, const char *arg)
1444 server_rec *s = cmd->server;
1445 proxy_server_conf *conf =
1446 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
1449 proxy_balancer *balancer = NULL;
1450 proxy_worker *worker = NULL;
1453 if (cmd->directive->parent &&
1454 strncasecmp(cmd->directive->parent->directive,
1455 "<Proxy", 6) == 0) {
1456 const char *pargs = cmd->directive->parent->args;
1457 /* Directive inside <Proxy section
1458 * Parent directive arg is the worker/balancer name.
1460 name = ap_getword_conf(cmd->temp_pool, &pargs);
1461 if ((word = ap_strchr(name, '>')))
1465 /* Standard set directive with worker/balancer
1466 * name as first param.
1468 name = ap_getword_conf(cmd->temp_pool, &arg);
1471 if (strncasecmp(name, "balancer:", 9) == 0) {
1472 balancer = ap_proxy_get_balancer(cmd->pool, conf, name);
1474 return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
1475 name, "' Balancer.", NULL);
1479 worker = ap_proxy_get_worker(cmd->temp_pool, conf, name);
1481 return apr_pstrcat(cmd->temp_pool, "ProxySet can not find '",
1482 name, "' Worker.", NULL);
1487 word = ap_getword_conf(cmd->pool, &arg);
1488 val = strchr(word, '=');
1490 return "Invalid ProxySet parameter. Parameter must be "
1491 "in the form 'key=value'";
1496 err = set_worker_param(cmd->pool, worker, word, val);
1498 err = set_balancer_param(cmd->pool, balancer, word, val);
1501 return apr_pstrcat(cmd->temp_pool, "ProxySet ", err, " ", word, " ", name, NULL);
1507 static void ap_add_per_proxy_conf(server_rec *s, ap_conf_vector_t *dir_config)
1509 proxy_server_conf *sconf = ap_get_module_config(s->module_config,
1511 void **new_space = (void **)apr_array_push(sconf->sec_proxy);
1513 *new_space = dir_config;
1516 static const char *proxysection(cmd_parms *cmd, void *mconfig, const char *arg)
1519 const char *endp = ap_strrchr_c(arg, '>');
1520 int old_overrides = cmd->override;
1521 char *old_path = cmd->path;
1522 proxy_dir_conf *conf;
1523 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1525 const command_rec *thiscmd = cmd->cmd;
1527 const char *err = ap_check_cmd_context(cmd,
1528 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1534 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1535 "> directive missing closing '>'", NULL);
1538 arg=apr_pstrndup(cmd->pool, arg, endp-arg);
1541 if (thiscmd->cmd_data)
1542 return "<ProxyMatch > block must specify a path";
1544 return "<Proxy > block must specify a path";
1547 cmd->path = ap_getword_conf(cmd->pool, &arg);
1548 cmd->override = OR_ALL|ACCESS_CONF;
1550 if (!strncasecmp(cmd->path, "proxy:", 6))
1553 /* XXX Ignore case? What if we proxy a case-insensitive server?!?
1554 * While we are at it, shouldn't we also canonicalize the entire
1555 * scheme? See proxy_fixup()
1557 if (thiscmd->cmd_data) { /* <ProxyMatch> */
1558 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1560 return "Regex could not be compiled";
1563 else if (!strcmp(cmd->path, "~")) {
1564 cmd->path = ap_getword_conf(cmd->pool, &arg);
1566 return "<Proxy ~ > block must specify a path";
1567 if (strncasecmp(cmd->path, "proxy:", 6))
1569 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1571 return "Regex could not be compiled";
1575 /* initialize our config and fetch it */
1576 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1577 &proxy_module, cmd->pool);
1579 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1584 conf->p = cmd->path;
1585 conf->p_is_fnmatch = apr_fnmatch_test(conf->p);
1587 ap_add_per_proxy_conf(cmd->server, new_dir_conf);
1590 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1591 "> arguments not (yet) supported.", NULL);
1594 cmd->path = old_path;
1595 cmd->override = old_overrides;
1600 static const command_rec proxy_cmds[] =
1602 AP_INIT_RAW_ARGS("<Proxy", proxysection, NULL, RSRC_CONF,
1603 "Container for directives affecting resources located in the proxied "
1605 AP_INIT_RAW_ARGS("<ProxyMatch", proxysection, (void*)1, RSRC_CONF,
1606 "Container for directives affecting resources located in the proxied "
1607 "location, in regular expression syntax"),
1608 AP_INIT_FLAG("ProxyRequests", set_proxy_req, NULL, RSRC_CONF,
1609 "on if the true proxy requests should be accepted"),
1610 AP_INIT_TAKE2("ProxyRemote", add_proxy_noregex, NULL, RSRC_CONF,
1611 "a scheme, partial URL or '*' and a proxy server"),
1612 AP_INIT_TAKE2("ProxyRemoteMatch", add_proxy_regex, NULL, RSRC_CONF,
1613 "a regex pattern and a proxy server"),
1614 AP_INIT_RAW_ARGS("ProxyPass", add_pass, NULL, RSRC_CONF|ACCESS_CONF,
1615 "a virtual path and a URL"),
1616 AP_INIT_TAKE12("ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF|ACCESS_CONF,
1617 "a virtual path and a URL for reverse proxy behaviour"),
1618 AP_INIT_TAKE2("ProxyPassReverseCookiePath", cookie_path, NULL,
1619 RSRC_CONF|ACCESS_CONF, "Path rewrite rule for proxying cookies"),
1620 AP_INIT_TAKE2("ProxyPassReverseCookieDomain", cookie_domain, NULL,
1621 RSRC_CONF|ACCESS_CONF, "Domain rewrite rule for proxying cookies"),
1622 AP_INIT_ITERATE("ProxyBlock", set_proxy_exclude, NULL, RSRC_CONF,
1623 "A list of names, hosts or domains to which the proxy will not connect"),
1624 AP_INIT_TAKE1("ProxyReceiveBufferSize", set_recv_buffer_size, NULL, RSRC_CONF,
1625 "Receive buffer size for outgoing HTTP and FTP connections in bytes"),
1626 AP_INIT_TAKE1("ProxyIOBufferSize", set_io_buffer_size, NULL, RSRC_CONF,
1627 "IO buffer size for outgoing HTTP and FTP connections in bytes"),
1628 AP_INIT_TAKE1("ProxyMaxForwards", set_max_forwards, NULL, RSRC_CONF,
1629 "The maximum number of proxies a request may be forwarded through."),
1630 AP_INIT_ITERATE("NoProxy", set_proxy_dirconn, NULL, RSRC_CONF,
1631 "A list of domains, hosts, or subnets to which the proxy will connect directly"),
1632 AP_INIT_TAKE1("ProxyDomain", set_proxy_domain, NULL, RSRC_CONF,
1633 "The default intranet domain name (in absence of a domain in the URL)"),
1634 AP_INIT_ITERATE("AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF,
1635 "A list of ports which CONNECT may connect to"),
1636 AP_INIT_TAKE1("ProxyVia", set_via_opt, NULL, RSRC_CONF,
1637 "Configure Via: proxy header header to one of: on | off | block | full"),
1638 AP_INIT_FLAG("ProxyErrorOverride", set_proxy_error_override, NULL, RSRC_CONF,
1639 "use our error handling pages instead of the servers' we are proxying"),
1640 AP_INIT_FLAG("ProxyPreserveHost", set_preserve_host, NULL, RSRC_CONF,
1641 "on if we should preserve host header while proxying"),
1642 AP_INIT_TAKE1("ProxyTimeout", set_proxy_timeout, NULL, RSRC_CONF,
1643 "Set the timeout (in seconds) for a proxied connection. "
1644 "This overrides the server timeout"),
1645 AP_INIT_TAKE1("ProxyBadHeader", set_bad_opt, NULL, RSRC_CONF,
1646 "How to handle bad header line in response: IsError | Ignore | StartBody"),
1647 AP_INIT_RAW_ARGS("BalancerMember", add_member, NULL, RSRC_CONF|ACCESS_CONF,
1648 "A balancer name and scheme with list of params"),
1649 AP_INIT_TAKE1("ProxyStatus", set_status_opt, NULL, RSRC_CONF,
1650 "Configure Status: proxy status to one of: on | off | full"),
1651 AP_INIT_RAW_ARGS("ProxySet", set_proxy_param, NULL, RSRC_CONF|ACCESS_CONF,
1652 "A balancer or worker name with list of params"),
1656 static APR_OPTIONAL_FN_TYPE(ssl_proxy_enable) *proxy_ssl_enable = NULL;
1657 static APR_OPTIONAL_FN_TYPE(ssl_engine_disable) *proxy_ssl_disable = NULL;
1659 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c)
1662 * if c == NULL just check if the optional function was imported
1663 * else run the optional function so ssl filters are inserted
1665 if (proxy_ssl_enable) {
1666 return c ? proxy_ssl_enable(c) : 1;
1672 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c)
1674 if (proxy_ssl_disable) {
1675 return proxy_ssl_disable(c);
1681 static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1682 apr_pool_t *ptemp, server_rec *s)
1685 proxy_ssl_enable = APR_RETRIEVE_OPTIONAL_FN(ssl_proxy_enable);
1686 proxy_ssl_disable = APR_RETRIEVE_OPTIONAL_FN(ssl_engine_disable);
1692 * proxy Extension to mod_status
1694 static int proxy_status_hook(request_rec *r, int flags)
1697 void *sconf = r->server->module_config;
1698 proxy_server_conf *conf = (proxy_server_conf *)
1699 ap_get_module_config(sconf, &proxy_module);
1700 proxy_balancer *balancer = NULL;
1701 proxy_worker *worker = NULL;
1703 if (flags & AP_STATUS_SHORT || conf->balancers->nelts == 0 ||
1704 conf->proxy_status == status_off)
1707 balancer = (proxy_balancer *)conf->balancers->elts;
1708 for (i = 0; i < conf->balancers->nelts; i++) {
1709 ap_rputs("<hr />\n<h1>Proxy LoadBalancer Status for ", r);
1710 ap_rvputs(r, balancer->name, "</h1>\n\n", NULL);
1711 ap_rputs("\n\n<table border=\"0\"><tr>"
1712 "<th>SSes</th><th>Timeout</th><th>Method</th>"
1714 ap_rvputs(r, "<td>", balancer->sticky, NULL);
1715 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
1716 apr_time_sec(balancer->timeout));
1717 ap_rprintf(r, "<td>%s</td>\n",
1718 balancer->lbmethod == lbmethod_requests ? "Requests" :
1719 balancer->lbmethod == lbmethod_traffic ? "Traffic" :
1721 ap_rputs("</table>\n", r);
1722 ap_rputs("\n\n<table border=\"0\"><tr>"
1723 "<th>Sch</th><th>Host</th><th>Stat</th>"
1724 "<th>Route</th><th>Redir</th>"
1725 "<th>F</th><th>Acc</th><th>Wr</th><th>Rd</th>"
1728 worker = (proxy_worker *)balancer->workers->elts;
1729 for (n = 0; n < balancer->workers->nelts; n++) {
1731 ap_rvputs(r, "<tr>\n<td>", worker->scheme, "</td>", NULL);
1732 ap_rvputs(r, "<td>", worker->hostname, "</td><td>", NULL);
1733 if (worker->s->status & PROXY_WORKER_DISABLED)
1735 else if (worker->s->status & PROXY_WORKER_IN_ERROR)
1737 else if (worker->s->status & PROXY_WORKER_INITIALIZED)
1741 ap_rvputs(r, "</td><td>", worker->s->route, NULL);
1742 ap_rvputs(r, "</td><td>", worker->s->redirect, NULL);
1743 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
1744 ap_rprintf(r, "<td>%d</td><td>", (int)(worker->s->elected));
1745 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
1746 ap_rputs("</td><td>", r);
1747 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
1748 ap_rputs("</td>\n", r);
1750 /* TODO: Add the rest of dynamic worker data */
1751 ap_rputs("</tr>\n", r);
1755 ap_rputs("</table>\n", r);
1758 ap_rputs("<hr /><table>\n"
1759 "<tr><th>SSes</th><td>Sticky session name</td></tr>\n"
1760 "<tr><th>Timeout</th><td>Balancer Timeout</td></tr>\n"
1761 "<tr><th>Sch</th><td>Connection scheme</td></tr>\n"
1762 "<tr><th>Host</th><td>Backend Hostname</td></tr>\n"
1763 "<tr><th>Stat</th><td>Worker status</td></tr>\n"
1764 "<tr><th>Route</th><td>Session Route</td></tr>\n"
1765 "<tr><th>Redir</th><td>Session Route Redirection</td></tr>\n"
1766 "<tr><th>F</th><td>Load Balancer Factor in %</td></tr>\n"
1767 "<tr><th>Acc</th><td>Number of requests</td></tr>\n"
1768 "<tr><th>Wr</th><td>Number of bytes transferred</td></tr>\n"
1769 "<tr><th>Rd</th><td>Number of bytes read</td></tr>\n"
1775 static void child_init(apr_pool_t *p, server_rec *s)
1777 proxy_worker *reverse = NULL;
1780 void *sconf = s->module_config;
1781 proxy_server_conf *conf;
1782 proxy_worker *worker;
1785 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
1786 /* Initialize worker's shared scoreboard data */
1787 worker = (proxy_worker *)conf->workers->elts;
1788 for (i = 0; i < conf->workers->nelts; i++) {
1789 ap_proxy_initialize_worker_share(conf, worker, s);
1790 ap_proxy_initialize_worker(worker, s);
1793 /* Initialize forward worker if defined */
1794 if (conf->forward) {
1795 ap_proxy_initialize_worker_share(conf, conf->forward, s);
1796 ap_proxy_initialize_worker(conf->forward, s);
1797 /* Do not disable worker in case of errors */
1798 conf->forward->s->status |= PROXY_WORKER_IGNORE_ERRORS;
1799 /* Disable address cache for generic forward worker */
1800 conf->forward->is_address_reusable = 0;
1803 reverse = ap_proxy_create_worker(p);
1804 reverse->name = "proxy:reverse";
1805 reverse->hostname = "*";
1806 reverse->scheme = "*";
1807 ap_proxy_initialize_worker_share(conf, reverse, s);
1808 ap_proxy_initialize_worker(reverse, s);
1809 /* Do not disable worker in case of errors */
1810 reverse->s->status |= PROXY_WORKER_IGNORE_ERRORS;
1811 /* Disable address cache for generic reverse worker */
1812 reverse->is_address_reusable = 0;
1814 conf->reverse = reverse;
1820 * This routine is called before the server processes the configuration
1821 * files. There is no return value.
1823 static int proxy_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
1826 APR_OPTIONAL_HOOK(ap, status_hook, proxy_status_hook, NULL, NULL,
1828 /* Reset workers count on gracefull restart */
1829 proxy_lb_workers = 0;
1833 static void register_hooks(apr_pool_t *p)
1835 /* fixup before mod_rewrite, so that the proxied url will not
1836 * escaped accidentally by our fixup.
1839 static const char * const aszSucc[]={ "mod_rewrite.c", NULL };
1841 /* Only the mpm_winnt has child init hook handler.
1842 * make sure that we are called after the mpm
1845 static const char *const aszPred[] = { "mpm_winnt.c", NULL};
1847 APR_REGISTER_OPTIONAL_FN(ap_proxy_lb_workers);
1849 ap_hook_handler(proxy_handler, NULL, NULL, APR_HOOK_FIRST);
1850 /* filename-to-URI translation */
1851 ap_hook_translate_name(proxy_trans, NULL, NULL, APR_HOOK_FIRST);
1852 /* walk <Proxy > entries and suppress default TRACE behavior */
1853 ap_hook_map_to_storage(proxy_map_location, NULL,NULL, APR_HOOK_FIRST);
1856 ap_hook_fixups(proxy_fixup, NULL, aszSucc, APR_HOOK_FIRST);
1858 /* post read_request handling */
1859 ap_hook_post_read_request(proxy_detect, NULL, NULL, APR_HOOK_FIRST);
1860 /* pre config handling */
1861 ap_hook_pre_config(proxy_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1862 /* post config handling */
1863 ap_hook_post_config(proxy_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1864 /* child init handling */
1865 ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1869 module AP_MODULE_DECLARE_DATA proxy_module =
1871 STANDARD20_MODULE_STUFF,
1872 create_proxy_dir_config, /* create per-directory config structure */
1873 merge_proxy_dir_config, /* merge per-directory config structures */
1874 create_proxy_config, /* create per-server config structure */
1875 merge_proxy_config, /* merge per-server config structures */
1876 proxy_cmds, /* command table */
1881 APR_HOOK_LINK(scheme_handler)
1882 APR_HOOK_LINK(canon_handler)
1883 APR_HOOK_LINK(pre_request)
1884 APR_HOOK_LINK(post_request)
1887 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, scheme_handler,
1888 (request_rec *r, proxy_worker *worker,
1889 proxy_server_conf *conf,
1890 char *url, const char *proxyhost,
1891 apr_port_t proxyport),(r,worker,conf,
1892 url,proxyhost,proxyport),DECLINED)
1893 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, canon_handler,
1894 (request_rec *r, char *url),(r,
1896 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, pre_request, (
1897 proxy_worker **worker,
1898 proxy_balancer **balancer,
1900 proxy_server_conf *conf,
1901 char **url),(worker,balancer,
1902 r,conf,url),DECLINED)
1903 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, post_request,
1904 (proxy_worker *worker,
1905 proxy_balancer *balancer,
1907 proxy_server_conf *conf),(worker,
1908 balancer,r,conf),DECLINED)
1909 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, fixups,
1910 (request_rec *r), (r),