1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
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"
22 #include "apr_optional.h"
25 #define MAX(x,y) ((x) >= (y) ? (x) : (y))
29 * A Web proxy module. Stages:
31 * translate_name: set filename to proxy:<URL>
32 * map_to_storage: run proxy_walk (rather than directory_walk/file_walk)
33 * can't trust directory_walk/file_walk since these are
34 * not in our filesystem. Prevents mod_http from serving
35 * the TRACE request we will set aside to handle later.
36 * type_checker: set type to PROXY_MAGIC_TYPE if filename begins proxy:
37 * fix_ups: convert the URL stored in the filename to the
39 * handler: handle proxy requests
42 /* -------------------------------------------------------------- */
43 /* Translate the URL into a 'filename' */
45 static int alias_match(const char *uri, const char *alias_fakename)
47 const char *end_fakename = alias_fakename + strlen(alias_fakename);
48 const char *aliasp = alias_fakename, *urip = uri;
50 while (aliasp < end_fakename) {
52 /* any number of '/' in the alias matches any number in
53 * the supplied URI, but there must be at least one...
58 while (*aliasp == '/')
64 /* Other characters are compared literally */
65 if (*urip++ != *aliasp++)
70 /* Check last alias path component matched all the way */
72 if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
75 /* Return number of characters from URI which matched (may be
76 * greater than length of alias, since we may have matched
83 /* Detect if an absoluteURI should be proxied or not. Note that we
84 * have to do this during this phase because later phases are
85 * "short-circuiting"... i.e. translate_names will end when the first
86 * module returns OK. So for example, if the request is something like:
88 * GET http://othervhost/cgi-bin/printenv HTTP/1.0
90 * mod_alias will notice the /cgi-bin part and ScriptAlias it and
91 * short-circuit the proxy... just because of the ordering in the
94 static int proxy_detect(request_rec *r)
96 void *sconf = r->server->module_config;
97 proxy_server_conf *conf;
99 conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
101 /* Ick... msvc (perhaps others) promotes ternary short results to int */
103 if (conf->req && r->parsed_uri.scheme) {
104 /* but it might be something vhosted */
105 if (!(r->parsed_uri.hostname
106 && !strcasecmp(r->parsed_uri.scheme, ap_http_method(r))
107 && ap_matches_request_vhost(r, r->parsed_uri.hostname,
108 (apr_port_t)(r->parsed_uri.port_str ? r->parsed_uri.port
109 : ap_default_port(r))))) {
110 r->proxyreq = PROXYREQ_PROXY;
111 r->uri = r->unparsed_uri;
112 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
113 r->handler = "proxy-server";
116 /* We need special treatment for CONNECT proxying: it has no scheme part */
117 else if (conf->req && r->method_number == M_CONNECT
118 && r->parsed_uri.hostname
119 && r->parsed_uri.port_str) {
120 r->proxyreq = PROXYREQ_PROXY;
121 r->uri = r->unparsed_uri;
122 r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
123 r->handler = "proxy-server";
128 static int proxy_trans(request_rec *r)
130 void *sconf = r->server->module_config;
131 proxy_server_conf *conf =
132 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
134 struct proxy_alias *ent = (struct proxy_alias *) conf->aliases->elts;
137 /* someone has already set up the proxy, it was possibly ourselves
143 /* XXX: since r->uri has been manipulated already we're not really
144 * compliant with RFC1945 at this point. But this probably isn't
145 * an issue because this is a hybrid proxy/origin server.
148 for (i = 0; i < conf->aliases->nelts; i++) {
149 len = alias_match(r->uri, ent[i].fake);
152 if ((ent[i].real[0] == '!' ) && ( ent[i].real[1] == 0 )) {
156 r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
157 (r->uri + len ), NULL);
158 r->handler = "proxy-server";
159 r->proxyreq = PROXYREQ_REVERSE;
166 static int proxy_walk(request_rec *r)
168 proxy_server_conf *sconf = ap_get_module_config(r->server->module_config,
170 ap_conf_vector_t *per_dir_defaults = r->server->lookup_defaults;
171 ap_conf_vector_t **sec_proxy = (ap_conf_vector_t **) sconf->sec_proxy->elts;
172 ap_conf_vector_t *entry_config;
173 proxy_dir_conf *entry_proxy;
174 int num_sec = sconf->sec_proxy->nelts;
175 /* XXX: shouldn't we use URI here? Canonicalize it first?
176 * Pass over "proxy:" prefix
178 const char *proxyname = r->filename + 6;
181 for (j = 0; j < num_sec; ++j)
183 entry_config = sec_proxy[j];
184 entry_proxy = ap_get_module_config(entry_config, &proxy_module);
186 /* XXX: What about case insensitive matching ???
187 * Compare regex, fnmatch or string as appropriate
188 * If the entry doesn't relate, then continue
191 ? ap_regexec(entry_proxy->r, proxyname, 0, NULL, 0)
192 : (entry_proxy->p_is_fnmatch
193 ? apr_fnmatch(entry_proxy->p, proxyname, 0)
194 : strncmp(proxyname, entry_proxy->p,
195 strlen(entry_proxy->p)))) {
198 per_dir_defaults = ap_merge_per_dir_configs(r->pool, per_dir_defaults,
202 r->per_dir_config = per_dir_defaults;
207 static int proxy_map_location(request_rec *r)
211 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
214 /* Don't let the core or mod_http map_to_storage hooks handle this,
215 * We don't need directory/file_walk, and we want to TRACE on our own.
217 if ((access_status = proxy_walk(r))) {
218 ap_die(access_status, r);
219 return access_status;
225 /* -------------------------------------------------------------- */
226 /* Fixup the filename */
229 * Canonicalise the URL
231 static int proxy_fixup(request_rec *r)
236 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
239 /* XXX: Shouldn't we try this before we run the proxy_walk? */
240 url = &r->filename[6];
242 /* canonicalise each specific scheme */
243 if ((access_status = proxy_run_canon_handler(r, url))) {
244 return access_status;
247 p = strchr(url, ':');
248 if (p == NULL || p == url)
249 return HTTP_BAD_REQUEST;
251 return OK; /* otherwise; we've done the best we can */
254 /* Send a redirection if the request contains a hostname which is not */
255 /* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
256 /* servers like Netscape's allow this and access hosts from the local */
257 /* domain in this case. I think it is better to redirect to a FQDN, since */
258 /* these will later be found in the bookmarks files. */
259 /* The "ProxyDomain" directive determines what domain will be appended */
260 static int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
265 /* We only want to worry about GETs */
266 if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
269 /* If host does contain a dot already, or it is "localhost", decline */
270 if (strchr(r->parsed_uri.hostname, '.') != NULL
271 || strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
272 return DECLINED; /* host name has a dot already */
274 ref = apr_table_get(r->headers_in, "Referer");
276 /* Reassemble the request, but insert the domain after the host name */
277 /* Note that the domain name always starts with a dot */
278 r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
280 nuri = apr_uri_unparse(r->pool,
282 APR_URI_UNP_REVEALPASSWORD);
284 apr_table_set(r->headers_out, "Location", nuri);
285 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
286 "Domain missing: %s sent to %s%s%s", r->uri,
287 apr_uri_unparse(r->pool, &r->parsed_uri,
288 APR_URI_UNP_OMITUSERINFO),
289 ref ? " from " : "", ref ? ref : "");
291 return HTTP_MOVED_PERMANENTLY;
294 /* -------------------------------------------------------------- */
297 static int proxy_handler(request_rec *r)
299 char *url, *scheme, *p;
301 void *sconf = r->server->module_config;
302 proxy_server_conf *conf = (proxy_server_conf *)
303 ap_get_module_config(sconf, &proxy_module);
304 apr_array_header_t *proxies = conf->proxies;
305 struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
306 int i, rc, access_status;
307 int direct_connect = 0;
311 /* is this for us? */
312 if (!r->proxyreq || !r->filename || strncmp(r->filename, "proxy:", 6) != 0)
315 /* handle max-forwards / OPTIONS / TRACE */
316 if ((str = apr_table_get(r->headers_in, "Max-Forwards"))) {
317 maxfwd = strtol(str, NULL, 10);
319 switch (r->method_number) {
322 r->proxyreq = PROXYREQ_NONE;
323 if ((access_status = ap_send_http_trace(r)))
324 ap_die(access_status, r);
326 ap_finalize_request_protocol(r);
331 r->proxyreq = PROXYREQ_NONE;
332 if ((access_status = ap_send_http_options(r)))
333 ap_die(access_status, r);
335 ap_finalize_request_protocol(r);
339 return ap_proxyerror(r, HTTP_BAD_GATEWAY,
340 "Max-Forwards has reached zero - proxy loop?");
344 maxfwd = (maxfwd > 0) ? maxfwd - 1 : 0;
347 /* set configured max-forwards */
348 maxfwd = conf->maxfwd;
350 apr_table_set(r->headers_in, "Max-Forwards",
351 apr_psprintf(r->pool, "%ld", (maxfwd > 0) ? maxfwd : 0));
353 if (r->method_number == M_TRACE) {
354 core_server_config *coreconf = (core_server_config *)
355 ap_get_module_config(sconf, &core_module);
357 if (coreconf->trace_enable == AP_TRACE_DISABLE)
359 /* Allow "error-notes" string to be printed by ap_send_error_response()
360 * Note; this goes nowhere, canned error response need an overhaul.
362 apr_table_setn(r->notes, "error-notes",
363 "TRACE forbidden by server configuration");
364 apr_table_setn(r->notes, "verbose-error-to", "*");
365 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
366 "proxy: TRACE forbidden by server configuration");
367 return HTTP_FORBIDDEN;
370 /* Can't test ap_should_client_block, we aren't ready to send
371 * the client a 100 Continue response till the connection has
374 if (coreconf->trace_enable != AP_TRACE_EXTENDED
375 && (r->read_length || r->read_chunked || r->remaining))
377 /* Allow "error-notes" string to be printed by ap_send_error_response()
378 * Note; this goes nowhere, canned error response need an overhaul.
380 apr_table_setn(r->notes, "error-notes",
381 "TRACE with request body is not allowed");
382 apr_table_setn(r->notes, "verbose-error-to", "*");
383 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
384 "proxy: TRACE with request body is not allowed");
385 return HTTP_REQUEST_ENTITY_TOO_LARGE;
389 url = r->filename + 6;
390 p = strchr(url, ':');
392 return HTTP_BAD_REQUEST;
394 /* If the host doesn't have a domain name, add one and redirect. */
395 if (conf->domain != NULL) {
396 rc = proxy_needsdomain(r, url, conf->domain);
397 if (ap_is_HTTP_REDIRECT(rc))
398 return HTTP_MOVED_PERMANENTLY;
402 scheme = apr_pstrdup(r->pool, url);
405 /* Check URI's destination host against NoProxy hosts */
406 /* Bypass ProxyRemote server lookup if configured as NoProxy */
407 /* we only know how to handle communication to a proxy via http */
408 /*if (strcasecmp(scheme, "http") == 0) */
411 struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
413 for (direct_connect = ii = 0; ii < conf->dirconn->nelts && !direct_connect; ii++) {
414 direct_connect = list[ii].matcher(&list[ii], r);
417 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
418 (direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
423 /* firstly, try a proxy, unless a NoProxy directive is active */
424 if (!direct_connect) {
425 for (i = 0; i < proxies->nelts; i++) {
426 p2 = ap_strchr_c(ents[i].scheme, ':'); /* is it a partial URL? */
427 if (strcmp(ents[i].scheme, "*") == 0 ||
428 (ents[i].use_regex &&
429 ap_regexec(ents[i].regexp, url, 0,NULL, 0) == 0) ||
430 (p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
432 strncasecmp(url, ents[i].scheme, strlen(ents[i].scheme)) == 0)) {
434 /* handle the scheme */
435 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
436 "Trying to run scheme_handler against proxy");
437 access_status = proxy_run_scheme_handler(r, conf, url, ents[i].hostname, ents[i].port);
439 /* an error or success */
440 if (access_status != DECLINED && access_status != HTTP_BAD_GATEWAY) {
441 return access_status;
443 /* we failed to talk to the upstream proxy */
448 /* otherwise, try it direct */
449 /* N.B. what if we're behind a firewall, where we must use a proxy or
453 /* handle the scheme */
454 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
455 "Trying to run scheme_handler");
456 access_status = proxy_run_scheme_handler(r, conf, url, NULL, 0);
457 if (DECLINED == access_status) {
458 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
459 "proxy: No protocol handler was valid for the URL %s. "
460 "If you are using a DSO version of mod_proxy, make sure "
461 "the proxy submodules are included in the configuration "
462 "using LoadModule.", r->uri);
463 return HTTP_FORBIDDEN;
465 return access_status;
468 /* -------------------------------------------------------------- */
469 /* Setup configurable data */
471 static void * create_proxy_config(apr_pool_t *p, server_rec *s)
473 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
475 ps->sec_proxy = apr_array_make(p, 10, sizeof(ap_conf_vector_t *));
476 ps->proxies = apr_array_make(p, 10, sizeof(struct proxy_remote));
477 ps->aliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
478 ps->raliases = apr_array_make(p, 10, sizeof(struct proxy_alias));
479 ps->noproxies = apr_array_make(p, 10, sizeof(struct noproxy_entry));
480 ps->dirconn = apr_array_make(p, 10, sizeof(struct dirconn_entry));
481 ps->allowed_connect_ports = apr_array_make(p, 10, sizeof(int));
483 ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
484 ps->viaopt_set = 0; /* 0 means default */
487 ps->recv_buffer_size = 0; /* this default was left unset for some reason */
488 ps->recv_buffer_size_set = 0;
489 ps->io_buffer_size = AP_IOBUFSIZE;
490 ps->io_buffer_size_set = 0;
491 ps->maxfwd = DEFAULT_MAX_FORWARDS;
493 ps->error_override = 0;
494 ps->error_override_set = 0;
495 ps->preserve_host_set = 0;
496 ps->preserve_host = 0;
499 ps->badopt = bad_error;
504 static void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
506 proxy_server_conf *ps = apr_pcalloc(p, sizeof(proxy_server_conf));
507 proxy_server_conf *base = (proxy_server_conf *) basev;
508 proxy_server_conf *overrides = (proxy_server_conf *) overridesv;
510 ps->proxies = apr_array_append(p, base->proxies, overrides->proxies);
511 ps->sec_proxy = apr_array_append(p, base->sec_proxy, overrides->sec_proxy);
512 ps->aliases = apr_array_append(p, base->aliases, overrides->aliases);
513 ps->raliases = apr_array_append(p, base->raliases, overrides->raliases);
514 ps->noproxies = apr_array_append(p, base->noproxies, overrides->noproxies);
515 ps->dirconn = apr_array_append(p, base->dirconn, overrides->dirconn);
516 ps->allowed_connect_ports = apr_array_append(p, base->allowed_connect_ports, overrides->allowed_connect_ports);
518 ps->domain = (overrides->domain == NULL) ? base->domain : overrides->domain;
519 ps->viaopt = (overrides->viaopt_set == 0) ? base->viaopt : overrides->viaopt;
520 ps->viaopt_set = overrides->viaopt_set || base->viaopt_set;
521 ps->req = (overrides->req_set == 0) ? base->req : overrides->req;
522 ps->req_set = overrides->req_set || base->req_set;
523 ps->recv_buffer_size = (overrides->recv_buffer_size_set == 0) ? base->recv_buffer_size : overrides->recv_buffer_size;
524 ps->recv_buffer_size_set = overrides->recv_buffer_size_set || base->recv_buffer_size_set;
525 ps->io_buffer_size = (overrides->io_buffer_size_set == 0) ? base->io_buffer_size : overrides->io_buffer_size;
526 ps->io_buffer_size_set = overrides->io_buffer_size_set || base->io_buffer_size_set;
527 ps->maxfwd = (overrides->maxfwd_set == 0) ? base->maxfwd : overrides->maxfwd;
528 ps->maxfwd_set = overrides->maxfwd_set || base->maxfwd_set;
529 ps->error_override = (overrides->error_override_set == 0) ? base->error_override : overrides->error_override;
530 ps->error_override_set = overrides->error_override_set || base->error_override_set;
531 ps->preserve_host = (overrides->preserve_host_set == 0) ? base->preserve_host : overrides->preserve_host;
532 ps->preserve_host_set = overrides->preserve_host_set || base->preserve_host_set;
533 ps->timeout= (overrides->timeout_set == 0) ? base->timeout : overrides->timeout;
534 ps->timeout_set = overrides->timeout_set || base->timeout_set;
535 ps->badopt = (overrides->badopt_set == 0) ? base->badopt : overrides->badopt;
536 ps->badopt_set = overrides->badopt_set || base->badopt_set;
541 static void *create_proxy_dir_config(apr_pool_t *p, char *dummy)
543 proxy_dir_conf *new =
544 (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
546 /* Filled in by proxysection, when applicable */
551 static void *merge_proxy_dir_config(apr_pool_t *p, void *basev, void *addv)
553 proxy_dir_conf *new = (proxy_dir_conf *) apr_pcalloc(p, sizeof(proxy_dir_conf));
554 proxy_dir_conf *add = (proxy_dir_conf *) addv;
555 proxy_dir_conf *base = (proxy_dir_conf *) basev;
558 new->p_is_fnmatch = add->p_is_fnmatch;
560 new->ftp_directory_charset = add->ftp_directory_charset ?
561 add->ftp_directory_charset :
562 base->ftp_directory_charset;
568 add_proxy(cmd_parms *cmd, void *dummy, const char *f1, const char *r1, int regex)
570 server_rec *s = cmd->server;
571 proxy_server_conf *conf =
572 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
573 struct proxy_remote *new;
575 char *r, *f, *scheme;
579 r = apr_pstrdup(cmd->pool, r1);
580 scheme = apr_pstrdup(cmd->pool, r1);
581 f = apr_pstrdup(cmd->pool, f1);
583 if (p == NULL || p[1] != '/' || p[2] != '/' || p[3] == '\0') {
585 return "ProxyRemoteMatch: Bad syntax for a remote proxy server";
587 return "ProxyRemote: Bad syntax for a remote proxy server";
592 q = strchr(p + 3, ':');
594 if (sscanf(q + 1, "%u", &port) != 1 || port > 65535) {
596 return "ProxyRemoteMatch: Bad syntax for a remote proxy server (bad port number)";
598 return "ProxyRemote: Bad syntax for a remote proxy server (bad port number)";
606 reg = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
608 return "Regular expression for ProxyRemoteMatch could not be compiled.";
611 if (strchr(f, ':') == NULL)
612 ap_str_tolower(f); /* lowercase scheme */
613 ap_str_tolower(p + 3); /* lowercase hostname */
616 port = apr_uri_port_of_scheme(scheme);
619 new = apr_array_push(conf->proxies);
622 new->hostname = p + 3;
625 new->use_regex = regex;
630 add_proxy_noregex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
632 return add_proxy(cmd, dummy, f1, r1, 0);
636 add_proxy_regex(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
638 return add_proxy(cmd, dummy, f1, r1, 1);
642 add_pass(cmd_parms *cmd, void *dummy, const char *f, const char *r)
644 server_rec *s = cmd->server;
645 proxy_server_conf *conf =
646 (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
647 struct proxy_alias *new;
648 if (r!=NULL && cmd->path == NULL ) {
649 new = apr_array_push(conf->aliases);
652 } else if (r==NULL && cmd->path != NULL) {
653 new = apr_array_push(conf->aliases);
654 new->fake = cmd->path;
658 return "ProxyPass needs a path when not defined in a location";
660 return "ProxyPass can not have a path when defined in a location";
667 add_pass_reverse(cmd_parms *cmd, void *dummy, const char *f, const char *r)
669 server_rec *s = cmd->server;
670 proxy_server_conf *conf;
671 struct proxy_alias *new;
673 conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
675 if (r!=NULL && cmd->path == NULL ) {
676 new = apr_array_push(conf->raliases);
679 } else if (r==NULL && cmd->path != NULL) {
680 new = apr_array_push(conf->raliases);
681 new->fake = cmd->path;
685 return "ProxyPassReverse needs a path when not defined in a location";
687 return "ProxyPassReverse can not have a path when defined in a location";
694 set_proxy_exclude(cmd_parms *parms, void *dummy, const char *arg)
696 server_rec *s = parms->server;
697 proxy_server_conf *conf =
698 ap_get_module_config(s->module_config, &proxy_module);
699 struct noproxy_entry *new;
700 struct noproxy_entry *list = (struct noproxy_entry *) conf->noproxies->elts;
701 struct apr_sockaddr_t *addr;
705 /* Don't duplicate entries */
706 for (i = 0; i < conf->noproxies->nelts; i++) {
707 if (apr_strnatcasecmp(arg, list[i].name) == 0) { /* ignore case for host names */
713 new = apr_array_push(conf->noproxies);
715 if (APR_SUCCESS == apr_sockaddr_info_get(&addr, new->name, APR_UNSPEC, 0, 0, parms->pool)) {
726 * Set the ports CONNECT can use
729 set_allowed_ports(cmd_parms *parms, void *dummy, const char *arg)
731 server_rec *s = parms->server;
732 proxy_server_conf *conf =
733 ap_get_module_config(s->module_config, &proxy_module);
736 if (!apr_isdigit(arg[0]))
737 return "AllowCONNECT: port number must be numeric";
739 New = apr_array_push(conf->allowed_connect_ports);
744 /* Similar to set_proxy_exclude(), but defining directly connected hosts,
745 * which should never be accessed via the configured ProxyRemote servers
748 set_proxy_dirconn(cmd_parms *parms, void *dummy, const char *arg)
750 server_rec *s = parms->server;
751 proxy_server_conf *conf =
752 ap_get_module_config(s->module_config, &proxy_module);
753 struct dirconn_entry *New;
754 struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
758 /* Don't duplicate entries */
759 for (i = 0; i < conf->dirconn->nelts; i++) {
760 if (strcasecmp(arg, list[i].name) == 0)
765 New = apr_array_push(conf->dirconn);
766 New->name = apr_pstrdup(parms->pool, arg);
767 New->hostaddr = NULL;
769 if (ap_proxy_is_ipaddr(New, parms->pool)) {
771 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
772 "Parsed addr %s", inet_ntoa(New->addr));
773 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
774 "Parsed mask %s", inet_ntoa(New->mask));
777 else if (ap_proxy_is_domainname(New, parms->pool)) {
778 ap_str_tolower(New->name);
780 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
781 "Parsed domain %s", New->name);
784 else if (ap_proxy_is_hostname(New, parms->pool)) {
785 ap_str_tolower(New->name);
787 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
788 "Parsed host %s", New->name);
792 ap_proxy_is_word(New, parms->pool);
794 fprintf(stderr, "Parsed word %s\n", New->name);
802 set_proxy_domain(cmd_parms *parms, void *dummy, const char *arg)
804 proxy_server_conf *psf =
805 ap_get_module_config(parms->server->module_config, &proxy_module);
808 return "ProxyDomain: domain name must start with a dot.";
815 set_proxy_req(cmd_parms *parms, void *dummy, int flag)
817 proxy_server_conf *psf =
818 ap_get_module_config(parms->server->module_config, &proxy_module);
825 set_proxy_error_override(cmd_parms *parms, void *dummy, int flag)
827 proxy_server_conf *psf =
828 ap_get_module_config(parms->server->module_config, &proxy_module);
830 psf->error_override = flag;
831 psf->error_override_set = 1;
835 set_preserve_host(cmd_parms *parms, void *dummy, int flag)
837 proxy_server_conf *psf =
838 ap_get_module_config(parms->server->module_config, &proxy_module);
840 psf->preserve_host = flag;
841 psf->preserve_host_set = 1;
846 set_recv_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
848 proxy_server_conf *psf =
849 ap_get_module_config(parms->server->module_config, &proxy_module);
851 if (s < 512 && s != 0) {
852 return "ProxyReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
855 psf->recv_buffer_size = s;
856 psf->recv_buffer_size_set = 1;
861 set_io_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
863 proxy_server_conf *psf =
864 ap_get_module_config(parms->server->module_config, &proxy_module);
867 psf->io_buffer_size = ((s > AP_IOBUFSIZE) ? s : AP_IOBUFSIZE);
868 psf->io_buffer_size_set = 1;
873 set_max_forwards(cmd_parms *parms, void *dummy, const char *arg)
875 proxy_server_conf *psf =
876 ap_get_module_config(parms->server->module_config, &proxy_module);
879 return "ProxyMaxForwards must be greater or equal to zero..";
887 set_proxy_timeout(cmd_parms *parms, void *dummy, const char *arg)
889 proxy_server_conf *psf =
890 ap_get_module_config(parms->server->module_config, &proxy_module);
895 return "Proxy Timeout must be at least 1 second.";
898 psf->timeout=apr_time_from_sec(timeout);
904 set_via_opt(cmd_parms *parms, void *dummy, const char *arg)
906 proxy_server_conf *psf =
907 ap_get_module_config(parms->server->module_config, &proxy_module);
909 if (strcasecmp(arg, "Off") == 0)
910 psf->viaopt = via_off;
911 else if (strcasecmp(arg, "On") == 0)
912 psf->viaopt = via_on;
913 else if (strcasecmp(arg, "Block") == 0)
914 psf->viaopt = via_block;
915 else if (strcasecmp(arg, "Full") == 0)
916 psf->viaopt = via_full;
918 return "ProxyVia must be one of: "
919 "off | on | full | block";
927 set_bad_opt(cmd_parms *parms, void *dummy, const char *arg)
929 proxy_server_conf *psf =
930 ap_get_module_config(parms->server->module_config, &proxy_module);
932 if (strcasecmp(arg, "IsError") == 0)
933 psf->badopt = bad_error;
934 else if (strcasecmp(arg, "Ignore") == 0)
935 psf->badopt = bad_ignore;
936 else if (strcasecmp(arg, "StartBody") == 0)
937 psf->badopt = bad_body;
939 return "ProxyBadHeader must be one of: "
940 "IsError | Ignore | StartBody";
947 static const char* set_ftp_directory_charset(cmd_parms *cmd, void *dconf,
950 proxy_dir_conf *conf = dconf;
952 conf->ftp_directory_charset = arg;
956 static void ap_add_per_proxy_conf(server_rec *s, ap_conf_vector_t *dir_config)
958 proxy_server_conf *sconf = ap_get_module_config(s->module_config,
960 void **new_space = (void **)apr_array_push(sconf->sec_proxy);
962 *new_space = dir_config;
965 static const char *proxysection(cmd_parms *cmd, void *mconfig, const char *arg)
968 const char *endp = ap_strrchr_c(arg, '>');
969 int old_overrides = cmd->override;
970 char *old_path = cmd->path;
971 proxy_dir_conf *conf;
972 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
974 const command_rec *thiscmd = cmd->cmd;
976 const char *err = ap_check_cmd_context(cmd,
977 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
983 return apr_pstrcat(cmd->pool, cmd->cmd->name,
984 "> directive missing closing '>'", NULL);
987 arg=apr_pstrndup(cmd->pool, arg, endp-arg);
990 if (thiscmd->cmd_data)
991 return "<ProxyMatch > block must specify a path";
993 return "<Proxy > block must specify a path";
996 cmd->path = ap_getword_conf(cmd->pool, &arg);
997 cmd->override = OR_ALL|ACCESS_CONF;
999 if (!strncasecmp(cmd->path, "proxy:", 6))
1002 /* XXX Ignore case? What if we proxy a case-insensitive server?!?
1003 * While we are at it, shouldn't we also canonicalize the entire
1004 * scheme? See proxy_fixup()
1006 if (thiscmd->cmd_data) { /* <ProxyMatch> */
1007 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1009 return "Regex could not be compiled";
1012 else if (!strcmp(cmd->path, "~")) {
1013 cmd->path = ap_getword_conf(cmd->pool, &arg);
1015 return "<Proxy ~ > block must specify a path";
1016 if (strncasecmp(cmd->path, "proxy:", 6))
1018 r = ap_pregcomp(cmd->pool, cmd->path, REG_EXTENDED);
1020 return "Regex could not be compiled";
1024 /* initialize our config and fetch it */
1025 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1026 &proxy_module, cmd->pool);
1028 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1033 conf->p = cmd->path;
1034 conf->p_is_fnmatch = apr_fnmatch_test(conf->p);
1036 ap_add_per_proxy_conf(cmd->server, new_dir_conf);
1039 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1040 "> arguments not (yet) supported.", NULL);
1043 cmd->path = old_path;
1044 cmd->override = old_overrides;
1049 static const command_rec proxy_cmds[] =
1051 AP_INIT_RAW_ARGS("<Proxy", proxysection, NULL, RSRC_CONF,
1052 "Container for directives affecting resources located in the proxied "
1054 AP_INIT_RAW_ARGS("<ProxyMatch", proxysection, (void*)1, RSRC_CONF,
1055 "Container for directives affecting resources located in the proxied "
1056 "location, in regular expression syntax"),
1057 AP_INIT_FLAG("ProxyRequests", set_proxy_req, NULL, RSRC_CONF,
1058 "on if the true proxy requests should be accepted"),
1059 AP_INIT_TAKE2("ProxyRemote", add_proxy_noregex, NULL, RSRC_CONF,
1060 "a scheme, partial URL or '*' and a proxy server"),
1061 AP_INIT_TAKE2("ProxyRemoteMatch", add_proxy_regex, NULL, RSRC_CONF,
1062 "a regex pattern and a proxy server"),
1063 AP_INIT_TAKE12("ProxyPass", add_pass, NULL, RSRC_CONF|ACCESS_CONF,
1064 "a virtual path and a URL"),
1065 AP_INIT_TAKE12("ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF|ACCESS_CONF,
1066 "a virtual path and a URL for reverse proxy behaviour"),
1067 AP_INIT_ITERATE("ProxyBlock", set_proxy_exclude, NULL, RSRC_CONF,
1068 "A list of names, hosts or domains to which the proxy will not connect"),
1069 AP_INIT_TAKE1("ProxyReceiveBufferSize", set_recv_buffer_size, NULL, RSRC_CONF,
1070 "Receive buffer size for outgoing HTTP and FTP connections in bytes"),
1071 AP_INIT_TAKE1("ProxyIOBufferSize", set_io_buffer_size, NULL, RSRC_CONF,
1072 "IO buffer size for outgoing HTTP and FTP connections in bytes"),
1073 AP_INIT_TAKE1("ProxyMaxForwards", set_max_forwards, NULL, RSRC_CONF,
1074 "The maximum number of proxies a request may be forwarded through."),
1075 AP_INIT_ITERATE("NoProxy", set_proxy_dirconn, NULL, RSRC_CONF,
1076 "A list of domains, hosts, or subnets to which the proxy will connect directly"),
1077 AP_INIT_TAKE1("ProxyDomain", set_proxy_domain, NULL, RSRC_CONF,
1078 "The default intranet domain name (in absence of a domain in the URL)"),
1079 AP_INIT_ITERATE("AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF,
1080 "A list of ports which CONNECT may connect to"),
1081 AP_INIT_TAKE1("ProxyVia", set_via_opt, NULL, RSRC_CONF,
1082 "Configure Via: proxy header header to one of: on | off | block | full"),
1083 AP_INIT_FLAG("ProxyErrorOverride", set_proxy_error_override, NULL, RSRC_CONF,
1084 "use our error handling pages instead of the servers' we are proxying"),
1085 AP_INIT_FLAG("ProxyPreserveHost", set_preserve_host, NULL, RSRC_CONF,
1086 "on if we should preserve host header while proxying"),
1087 AP_INIT_TAKE1("ProxyTimeout", set_proxy_timeout, NULL, RSRC_CONF,
1088 "Set the timeout (in seconds) for a proxied connection. "
1089 "This overrides the server timeout"),
1090 AP_INIT_TAKE1("ProxyBadHeader", set_bad_opt, NULL, RSRC_CONF,
1091 "How to handle bad header line in response: IsError | Ignore | StartBody"),
1092 AP_INIT_TAKE1("ProxyFtpDirCharset", set_ftp_directory_charset, NULL,
1093 RSRC_CONF|ACCESS_CONF, "Define the character set for proxied FTP listings"),
1097 APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
1098 APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
1100 static APR_OPTIONAL_FN_TYPE(ssl_proxy_enable) *proxy_ssl_enable = NULL;
1101 static APR_OPTIONAL_FN_TYPE(ssl_engine_disable) *proxy_ssl_disable = NULL;
1103 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c)
1106 * if c == NULL just check if the optional function was imported
1107 * else run the optional function so ssl filters are inserted
1109 if (proxy_ssl_enable) {
1110 return c ? proxy_ssl_enable(c) : 1;
1116 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c)
1118 if (proxy_ssl_disable) {
1119 return proxy_ssl_disable(c);
1125 static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
1126 apr_pool_t *ptemp, server_rec *s)
1128 proxy_ssl_enable = APR_RETRIEVE_OPTIONAL_FN(ssl_proxy_enable);
1129 proxy_ssl_disable = APR_RETRIEVE_OPTIONAL_FN(ssl_engine_disable);
1134 static void register_hooks(apr_pool_t *p)
1136 /* fixup before mod_rewrite, so that the proxied url will not
1137 * escaped accidentally by our fixup.
1139 static const char * const aszSucc[]={ "mod_rewrite.c", NULL };
1142 ap_hook_handler(proxy_handler, NULL, NULL, APR_HOOK_FIRST);
1143 /* filename-to-URI translation */
1144 ap_hook_translate_name(proxy_trans, NULL, NULL, APR_HOOK_FIRST);
1145 /* walk <Proxy > entries and suppress default TRACE behavior */
1146 ap_hook_map_to_storage(proxy_map_location, NULL,NULL, APR_HOOK_FIRST);
1148 ap_hook_fixups(proxy_fixup, NULL, aszSucc, APR_HOOK_FIRST);
1149 /* post read_request handling */
1150 ap_hook_post_read_request(proxy_detect, NULL, NULL, APR_HOOK_FIRST);
1151 /* post config handling */
1152 ap_hook_post_config(proxy_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1155 module AP_MODULE_DECLARE_DATA proxy_module =
1157 STANDARD20_MODULE_STUFF,
1158 create_proxy_dir_config, /* create per-directory config structure */
1159 merge_proxy_dir_config, /* merge per-directory config structures */
1160 create_proxy_config, /* create per-server config structure */
1161 merge_proxy_config, /* merge per-server config structures */
1162 proxy_cmds, /* command table */
1167 APR_HOOK_LINK(scheme_handler)
1168 APR_HOOK_LINK(canon_handler)
1171 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, scheme_handler,
1172 (request_rec *r, proxy_server_conf *conf,
1173 char *url, const char *proxyhost,
1174 apr_port_t proxyport),(r,conf,url,
1175 proxyhost,proxyport),DECLINED)
1176 APR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(proxy, PROXY, int, canon_handler,
1177 (request_rec *r, char *url),(r,
1179 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, fixups,
1180 (request_rec *r), (r),