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.
17 /* Utility routines for Apache proxy */
18 #include "mod_proxy.h"
20 #include "scoreboard.h"
21 #include "apr_version.h"
24 #include <unistd.h> /* for getpid() */
27 #if (APR_MAJOR_VERSION < 1)
28 #undef apr_socket_create
29 #define apr_socket_create apr_socket_create_ex
32 /* Global balancer counter */
33 int PROXY_DECLARE_DATA proxy_lb_workers = 0;
34 static int lb_workers_limit = 0;
36 static int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
37 static int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
38 static int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
39 static int proxy_match_word(struct dirconn_entry *This, request_rec *r);
41 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, create_req,
42 (request_rec *r, request_rec *pr), (r, pr),
45 /* already called in the knowledge that the characters are hex digits */
46 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x)
50 #if !APR_CHARSET_EBCDIC
52 if (apr_isdigit(ch)) {
55 else if (apr_isupper(ch)) {
64 if (apr_isdigit(ch)) {
67 else if (apr_isupper(ch)) {
74 #else /*APR_CHARSET_EBCDIC*/
76 * we assume that the hex value refers to an ASCII character
77 * so convert to EBCDIC so that it makes sense locally;
81 * client specifies %20 in URL to refer to a space char;
82 * at this point we're called with EBCDIC "20"; after turning
83 * EBCDIC "20" into binary 0x20, we then need to assume that 0x20
84 * represents an ASCII char and convert 0x20 to EBCDIC, yielding
89 if (1 == sscanf(x, "%2x", &i)) {
91 ap_xlate_proto_from_ascii(buf, 1);
97 #endif /*APR_CHARSET_EBCDIC*/
100 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x)
102 #if !APR_CHARSET_EBCDIC
106 i = (ch & 0xF0) >> 4;
108 x[1] = ('A' - 10) + i;
116 x[2] = ('A' - 10) + i;
121 #else /*APR_CHARSET_EBCDIC*/
122 static const char ntoa[] = { "0123456789ABCDEF" };
128 ap_xlate_proto_to_ascii(buf, 1);
131 x[1] = ntoa[(buf[0] >> 4) & 0x0F];
132 x[2] = ntoa[buf[0] & 0x0F];
134 #endif /*APR_CHARSET_EBCDIC*/
138 * canonicalise a URL-encoded string
142 * Convert a URL-encoded string to canonical form.
143 * It decodes characters which need not be encoded,
144 * and encodes those which must be encoded, and does not touch
145 * those which must not be touched.
147 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len,
148 enum enctype t, int forcedec,
153 char *allowed; /* characters which should not be encoded */
154 char *reserved; /* characters which much not be en/de-coded */
157 * N.B. in addition to :@&=, this allows ';' in an http path
158 * and '?' in an ftp path -- this may be revised
160 * Also, it makes a '+' character in a search string reserved, as
161 * it may be form-encoded. (Although RFC 1738 doesn't allow this -
162 * it only permits ; / ? : @ = & as reserved chars.)
165 allowed = "~$-_.+!*'(),;:@&=";
167 else if (t == enc_search) {
168 allowed = "$-_.!*'(),;:@&=";
170 else if (t == enc_user) {
171 allowed = "$-_.+!*'(),;@&=";
173 else if (t == enc_fpath) {
174 allowed = "$-_.+!*'(),?:@&=";
176 else { /* if (t == enc_parm) */
177 allowed = "$-_.+!*'(),?/:@&=";
183 else if (t == enc_search) {
190 y = apr_palloc(p, 3 * len + 1);
192 for (i = 0, j = 0; i < len; i++, j++) {
193 /* always handle '/' first */
195 if (strchr(reserved, ch)) {
200 * decode it if not already done. do not decode reverse proxied URLs
201 * unless specifically forced
203 if ((forcedec || (proxyreq && proxyreq != PROXYREQ_REVERSE)) && ch == '%') {
204 if (!apr_isxdigit(x[i + 1]) || !apr_isxdigit(x[i + 2])) {
207 ch = ap_proxy_hex2c(&x[i + 1]);
209 if (ch != 0 && strchr(reserved, ch)) { /* keep it encoded */
210 ap_proxy_c2hex(ch, &y[j]);
215 /* recode it, if necessary */
216 if (!apr_isalnum(ch) && !strchr(allowed, ch)) {
217 ap_proxy_c2hex(ch, &y[j]);
229 * Parses network-location.
230 * urlp on input the URL; on output the path, after the leading /
231 * user NULL if no user/password permitted
232 * password holder for password
233 * host holder for host
234 * port port number; only set if one is supplied.
236 * Returns an error string.
238 PROXY_DECLARE(char *)
239 ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
240 char **passwordp, char **hostp, apr_port_t *port)
242 char *addr, *scope_id, *strp, *host, *url = *urlp;
243 char *user = NULL, *password = NULL;
247 if (url[0] != '/' || url[1] != '/') {
248 return "Malformed URL";
251 url = strchr(host, '/');
256 *(url++) = '\0'; /* skip seperating '/' */
259 /* find _last_ '@' since it might occur in user/password part */
260 strp = strrchr(host, '@');
268 strp = strchr(user, ':');
271 password = ap_proxy_canonenc(p, strp + 1, strlen(strp + 1), enc_user, 1, 0);
272 if (password == NULL) {
273 return "Bad %-escape in URL (password)";
277 user = ap_proxy_canonenc(p, user, strlen(user), enc_user, 1, 0);
279 return "Bad %-escape in URL (username)";
285 if (passwordp != NULL) {
286 *passwordp = password;
290 * Parse the host string to separate host portion from optional port.
291 * Perform range checking on port.
293 rv = apr_parse_addr_port(&addr, &scope_id, &tmp_port, host, p);
294 if (rv != APR_SUCCESS || addr == NULL || scope_id != NULL) {
295 return "Invalid host/port";
297 if (tmp_port != 0) { /* only update caller's port if port was specified */
301 ap_str_tolower(addr); /* DNS names are case-insensitive */
310 * If the date is a valid RFC 850 date or asctime() date, then it
311 * is converted to the RFC 1123 format.
313 PROXY_DECLARE(const char *)
314 ap_proxy_date_canon(apr_pool_t *p, const char *date)
319 apr_time_t time = apr_date_parse_http(date);
324 ndate = apr_palloc(p, APR_RFC822_DATE_LEN);
325 rv = apr_rfc822_date(ndate, time);
326 if (rv != APR_SUCCESS) {
333 PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r)
335 request_rec *rp = apr_pcalloc(r->pool, sizeof(*r));
338 rp->status = HTTP_OK;
340 rp->headers_in = apr_table_make(r->pool, 50);
341 rp->subprocess_env = apr_table_make(r->pool, 50);
342 rp->headers_out = apr_table_make(r->pool, 12);
343 rp->err_headers_out = apr_table_make(r->pool, 5);
344 rp->notes = apr_table_make(r->pool, 5);
346 rp->server = r->server;
347 rp->proxyreq = r->proxyreq;
348 rp->request_time = r->request_time;
350 rp->output_filters = c->output_filters;
351 rp->input_filters = c->input_filters;
352 rp->proto_output_filters = c->output_filters;
353 rp->proto_input_filters = c->input_filters;
355 rp->request_config = ap_create_request_config(r->pool);
356 proxy_run_create_req(r, rp);
363 * list is a comma-separated list of case-insensitive tokens, with
364 * optional whitespace around the tokens.
365 * The return returns 1 if the token val is found in the list, or 0
368 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val)
375 while (list != NULL) {
376 p = ap_strchr_c(list, ',');
381 } while (apr_isspace(*p));
387 while (i > 0 && apr_isspace(list[i - 1])) {
390 if (i == len && strncasecmp(list, val, len) == 0) {
399 * list is a comma-separated list of case-insensitive tokens, with
400 * optional whitespace around the tokens.
401 * if val appears on the list of tokens, it is removed from the list,
402 * and the new list is returned.
404 PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val)
412 while (list != NULL) {
413 p = ap_strchr_c(list, ',');
418 } while (apr_isspace(*p));
424 while (i > 0 && apr_isspace(list[i - 1])) {
427 if (i == len && strncasecmp(list, val, len) == 0) {
432 new = apr_pstrcat(pool, new, ",", apr_pstrndup(pool, list, i), NULL);
435 new = apr_pstrndup(pool, list, i);
444 * Converts 8 hex digits to a time integer
446 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x)
451 for (i = 0, j = 0; i < 8; i++) {
454 if (apr_isdigit(ch)) {
457 else if (apr_isupper(ch)) {
458 j |= ch - ('A' - 10);
461 j |= ch - ('a' - 10);
464 if (j == 0xffffffff) {
465 return -1; /* so that it works with 8-byte ints */
473 * Converts a time integer to 8 hex digits
475 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y)
480 for (i = 7; i >= 0; i--) {
484 y[i] = ch + ('A' - 10);
493 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message)
495 apr_table_setn(r->notes, "error-notes",
497 "The proxy server could not handle the request "
498 "<em><a href=\"", ap_escape_html(r->pool, r->uri),
499 "\">", ap_escape_html(r->pool, r->method),
501 ap_escape_html(r->pool, r->uri), "</a></em>.<p>\n"
503 ap_escape_html(r->pool, message),
504 "</strong></p>", NULL));
506 /* Allow "error-notes" string to be printed by ap_send_error_response() */
507 apr_table_setn(r->notes, "verbose-error-to", apr_pstrdup(r->pool, "*"));
509 r->status_line = apr_psprintf(r->pool, "%3.3u Proxy Error", statuscode);
510 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
511 "proxy: %s returned by %s", message, r->uri);
516 proxy_get_host_of_request(request_rec *r)
518 char *url, *user = NULL, *password = NULL, *err, *host;
521 if (r->hostname != NULL) {
525 /* Set url to the first char after "scheme://" */
526 if ((url = strchr(r->uri, ':')) == NULL || url[1] != '/' || url[2] != '/') {
530 url = apr_pstrdup(r->pool, &url[1]); /* make it point to "//", which is what proxy_canon_netloc expects */
532 err = ap_proxy_canon_netloc(r->pool, &url, &user, &password, &host, &port);
535 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "%s", err);
540 return host; /* ought to return the port, too */
543 /* Return TRUE if addr represents an IP address (or an IP network address) */
544 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p)
546 const char *addr = This->name;
552 * if the address is given with an explicit netmask, use that
553 * Due to a deficiency in apr_inet_addr(), it is impossible to parse
554 * "partial" addresses (with less than 4 quads) correctly, i.e.
555 * 192.168.123 is parsed as 192.168.0.123, which is not what I want.
556 * I therefore have to parse the IP address manually:
557 * if (proxy_readmask(This->name, &This->addr.s_addr, &This->mask.s_addr) == 0)
558 * addr and mask were set by proxy_readmask()
563 * Parse IP addr manually, optionally allowing
564 * abbreviated net addresses like 192.168.
567 /* Iterate over up to 4 (dotted) quads. */
568 for (quads = 0; quads < 4 && *addr != '\0'; ++quads) {
571 if (*addr == '/' && quads > 0) { /* netmask starts here. */
575 if (!apr_isdigit(*addr)) {
576 return 0; /* no digit at start of quad */
579 ip_addr[quads] = strtol(addr, &tmp, 0);
581 if (tmp == addr) { /* expected a digit, found something else */
585 if (ip_addr[quads] < 0 || ip_addr[quads] > 255) {
592 if (*addr == '.' && quads != 3) {
593 ++addr; /* after the 4th quad, a dot would be illegal */
597 for (This->addr.s_addr = 0, i = 0; i < quads; ++i) {
598 This->addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
601 if (addr[0] == '/' && apr_isdigit(addr[1])) { /* net mask follows: */
606 bits = strtol(addr, &tmp, 0);
608 if (tmp == addr) { /* expected a digit, found something else */
614 if (bits < 0 || bits > 32) { /* netmask must be between 0 and 32 */
621 * Determine (i.e., "guess") netmask by counting the
622 * number of trailing .0's; reduce #quads appropriately
623 * (so that 192.168.0.0 is equivalent to 192.168.)
625 while (quads > 0 && ip_addr[quads - 1] == 0) {
629 /* "IP Address should be given in dotted-quad form, optionally followed by a netmask (e.g., 192.168.111.0/24)"; */
634 /* every zero-byte counts as 8 zero-bits */
637 if (bits != 32) { /* no warning for fully qualified IP address */
638 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
639 "Warning: NetMask not supplied with IP-Addr; guessing: %s/%ld",
640 inet_ntoa(This->addr), bits);
644 This->mask.s_addr = htonl(APR_INADDR_NONE << (32 - bits));
646 if (*addr == '\0' && (This->addr.s_addr & ~This->mask.s_addr) != 0) {
647 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
648 "Warning: NetMask and IP-Addr disagree in %s/%ld",
649 inet_ntoa(This->addr), bits);
650 This->addr.s_addr &= This->mask.s_addr;
651 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
652 " Set to %s/%ld", inet_ntoa(This->addr), bits);
656 This->matcher = proxy_match_ipaddr;
660 return (*addr == '\0'); /* okay iff we've parsed the whole string */
664 /* Return TRUE if addr represents an IP address (or an IP network address) */
665 static int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r)
668 struct in_addr addr, *ip;
669 const char *host = proxy_get_host_of_request(r);
671 if (host == NULL) { /* oops! */
675 memset(&addr, '\0', sizeof addr);
676 memset(ip_addr, '\0', sizeof ip_addr);
678 if (4 == sscanf(host, "%d.%d.%d.%d", &ip_addr[0], &ip_addr[1], &ip_addr[2], &ip_addr[3])) {
679 for (addr.s_addr = 0, i = 0; i < 4; ++i) {
680 addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
683 if (This->addr.s_addr == (addr.s_addr & This->mask.s_addr)) {
685 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
686 "1)IP-Match: %s[%s] <-> ", host, inet_ntoa(addr));
687 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
688 "%s/", inet_ntoa(This->addr));
689 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
690 "%s", inet_ntoa(This->mask));
696 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
697 "1)IP-NoMatch: %s[%s] <-> ", host, inet_ntoa(addr));
698 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
699 "%s/", inet_ntoa(This->addr));
700 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
701 "%s", inet_ntoa(This->mask));
706 struct apr_sockaddr_t *reqaddr;
708 if (apr_sockaddr_info_get(&reqaddr, host, APR_UNSPEC, 0, 0, r->pool)
711 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
712 "2)IP-NoMatch: hostname=%s msg=Host not found", host);
717 /* Try to deal with multiple IP addr's for a host */
718 /* FIXME: This needs to be able to deal with IPv6 */
720 ip = (struct in_addr *) reqaddr->ipaddr_ptr;
721 if (This->addr.s_addr == (ip->s_addr & This->mask.s_addr)) {
723 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
724 "3)IP-Match: %s[%s] <-> ", host, inet_ntoa(*ip));
725 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
726 "%s/", inet_ntoa(This->addr));
727 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
728 "%s", inet_ntoa(This->mask));
734 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
735 "3)IP-NoMatch: %s[%s] <-> ", host, inet_ntoa(*ip));
736 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
737 "%s/", inet_ntoa(This->addr));
738 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
739 "%s", inet_ntoa(This->mask));
742 reqaddr = reqaddr->next;
749 /* Return TRUE if addr represents a domain name */
750 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p)
752 char *addr = This->name;
755 /* Domain name must start with a '.' */
756 if (addr[0] != '.') {
760 /* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
761 for (i = 0; apr_isalnum(addr[i]) || addr[i] == '-' || addr[i] == '.'; ++i) {
766 if (addr[i] == ':') {
767 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
768 "@@@@ handle optional port in proxy_is_domainname()");
769 /* @@@@ handle optional port */
773 if (addr[i] != '\0') {
777 /* Strip trailing dots */
778 for (i = strlen(addr) - 1; i > 0 && addr[i] == '.'; --i) {
782 This->matcher = proxy_match_domainname;
786 /* Return TRUE if host "host" is in domain "domain" */
787 static int proxy_match_domainname(struct dirconn_entry *This, request_rec *r)
789 const char *host = proxy_get_host_of_request(r);
790 int d_len = strlen(This->name), h_len;
792 if (host == NULL) { /* some error was logged already */
796 h_len = strlen(host);
798 /* @@@ do this within the setup? */
799 /* Ignore trailing dots in domain comparison: */
800 while (d_len > 0 && This->name[d_len - 1] == '.') {
803 while (h_len > 0 && host[h_len - 1] == '.') {
807 && strncasecmp(&host[h_len - d_len], This->name, d_len) == 0;
810 /* Return TRUE if host represents a host name */
811 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p)
813 struct apr_sockaddr_t *addr;
814 char *host = This->name;
817 /* Host names must not start with a '.' */
818 if (host[0] == '.') {
821 /* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
822 for (i = 0; apr_isalnum(host[i]) || host[i] == '-' || host[i] == '.'; ++i);
824 if (host[i] != '\0' || apr_sockaddr_info_get(&addr, host, APR_UNSPEC, 0, 0, p) != APR_SUCCESS) {
828 This->hostaddr = addr;
830 /* Strip trailing dots */
831 for (i = strlen(host) - 1; i > 0 && host[i] == '.'; --i) {
835 This->matcher = proxy_match_hostname;
839 /* Return TRUE if host "host" is equal to host2 "host2" */
840 static int proxy_match_hostname(struct dirconn_entry *This, request_rec *r)
842 char *host = This->name;
843 const char *host2 = proxy_get_host_of_request(r);
847 if (host == NULL || host2 == NULL) {
848 return 0; /* oops! */
851 h2_len = strlen(host2);
852 h1_len = strlen(host);
855 struct apr_sockaddr_t *addr = *This->hostaddr;
857 /* Try to deal with multiple IP addr's for a host */
859 if (addr->ipaddr_ptr == ? ? ? ? ? ? ? ? ? ? ? ? ?)
865 /* Ignore trailing dots in host2 comparison: */
866 while (h2_len > 0 && host2[h2_len - 1] == '.') {
869 while (h1_len > 0 && host[h1_len - 1] == '.') {
872 return h1_len == h2_len
873 && strncasecmp(host, host2, h1_len) == 0;
876 /* Return TRUE if addr is to be matched as a word */
877 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p)
879 This->matcher = proxy_match_word;
883 /* Return TRUE if string "str2" occurs literally in "str1" */
884 static int proxy_match_word(struct dirconn_entry *This, request_rec *r)
886 const char *host = proxy_get_host_of_request(r);
887 return host != NULL && ap_strstr_c(host, This->name) != NULL;
890 /* checks whether a host in uri_addr matches proxyblock */
891 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf,
892 apr_sockaddr_t *uri_addr)
895 apr_sockaddr_t * src_uri_addr = uri_addr;
896 /* XXX FIXME: conf->noproxies->elts is part of an opaque structure */
897 for (j = 0; j < conf->noproxies->nelts; j++) {
898 struct noproxy_entry *npent = (struct noproxy_entry *) conf->noproxies->elts;
899 struct apr_sockaddr_t *conf_addr = npent[j].addr;
900 uri_addr = src_uri_addr;
901 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
902 "proxy: checking remote machine [%s] against [%s]", uri_addr->hostname, npent[j].name);
903 if ((npent[j].name && ap_strstr_c(uri_addr->hostname, npent[j].name))
904 || npent[j].name[0] == '*') {
905 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
906 "proxy: connect to remote machine %s blocked: name %s matched", uri_addr->hostname, npent[j].name);
907 return HTTP_FORBIDDEN;
910 uri_addr = src_uri_addr;
914 apr_sockaddr_ip_get(&conf_ip, conf_addr);
915 apr_sockaddr_ip_get(&uri_ip, uri_addr);
916 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
917 "proxy: ProxyBlock comparing %s and %s", conf_ip, uri_ip);
918 if (!apr_strnatcasecmp(conf_ip, uri_ip)) {
919 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
920 "proxy: connect to remote machine %s blocked: IP %s matched", uri_addr->hostname, conf_ip);
921 return HTTP_FORBIDDEN;
923 uri_addr = uri_addr->next;
925 conf_addr = conf_addr->next;
931 /* set up the minimal filter set */
932 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r)
934 ap_add_input_filter("HTTP_IN", NULL, r, c);
939 * converts a series of buckets into a string
940 * XXX: BillS says this function performs essentially the same function as
941 * ap_rgetline() in protocol.c. Deprecate this function and use ap_rgetline()
942 * instead? I think ap_proxy_string_read() will not work properly on non ASCII
943 * (EBCDIC) machines either.
945 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb,
946 char *buff, apr_size_t bufflen, int *eos)
955 /* start with an empty string */
959 /* loop through each brigade */
961 /* get brigade from network one line at a time */
962 if (APR_SUCCESS != (rv = ap_get_brigade(c->input_filters, bb,
968 /* loop through each bucket */
970 if (*eos || APR_BRIGADE_EMPTY(bb)) {
971 /* The connection aborted or timed out */
972 return APR_ECONNABORTED;
974 e = APR_BRIGADE_FIRST(bb);
975 if (APR_BUCKET_IS_EOS(e)) {
979 if (APR_SUCCESS != (rv = apr_bucket_read(e,
980 (const char **)&response,
986 * is string LF terminated?
987 * XXX: This check can be made more efficient by simply checking
988 * if the last character in the 'response' buffer is an ASCII_LF.
989 * See ap_rgetline() for an example.
991 if (memchr(response, APR_ASCII_LF, len)) {
994 /* concat strings until buff is full - then throw the data away */
995 if (len > ((bufflen-1)-(pos-buff))) {
996 len = (bufflen-1)-(pos-buff);
999 memcpy(pos, response, len);
1003 APR_BUCKET_REMOVE(e);
1004 apr_bucket_destroy(e);
1012 /* unmerge an element in the table */
1013 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key)
1015 apr_off_t offset = 0;
1016 apr_off_t count = 0;
1019 /* get the value to unmerge */
1020 const char *initial = apr_table_get(t, key);
1024 value = apr_pstrdup(p, initial);
1026 /* remove the value from the headers */
1027 apr_table_unset(t, key);
1029 /* find each comma */
1030 while (value[count]) {
1031 if (value[count] == ',') {
1033 apr_table_add(t, key, value + offset);
1038 apr_table_add(t, key, value + offset);
1041 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r,
1042 proxy_dir_conf *conf, const char *url)
1044 proxy_req_conf *rconf;
1045 struct proxy_alias *ent;
1050 * XXX FIXME: Make sure this handled the ambiguous case of the :<PORT>
1051 * after the hostname
1053 if (r->proxyreq != PROXYREQ_REVERSE) {
1058 if (conf->interpolate_env == 1) {
1059 rconf = ap_get_module_config(r->request_config, &proxy_module);
1060 ent = (struct proxy_alias *)rconf->raliases->elts;
1063 ent = (struct proxy_alias *)conf->raliases->elts;
1065 for (i = 0; i < conf->raliases->nelts; i++) {
1066 proxy_server_conf *sconf = (proxy_server_conf *)
1067 ap_get_module_config(r->server->module_config, &proxy_module);
1068 proxy_balancer *balancer;
1071 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1072 "ppr: real: %s", real);
1074 * First check if mapping against a balancer and see
1075 * if we have such a entity. If so, then we need to
1076 * find the particulars of the actual worker which may
1077 * or may not be the right one... basically, we need
1078 * to find which member actually handled this request.
1080 if ((strncasecmp(real, "balancer:", 9) == 0) &&
1081 (balancer = ap_proxy_get_balancer(r->pool, sconf, real))) {
1083 proxy_worker *worker;
1084 worker = (proxy_worker *)balancer->workers->elts;
1085 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1086 "ppr: checking balancer: %s",
1088 for (n = 0; n < balancer->workers->nelts; n++) {
1090 u = apr_psprintf(r->pool, "%s://%s:%d/", worker->scheme,
1091 worker->hostname, worker->port);
1094 u = apr_psprintf(r->pool, "%s://%s/", worker->scheme,
1097 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1098 "ppr: matching member (%s) and URL (%s)",
1102 if (l1 >= l2 && strncasecmp(u, url, l2) == 0) {
1103 u = apr_pstrcat(r->pool, ent[i].fake, &url[l2], NULL);
1104 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1105 "ppr: matched member (%s)", u);
1106 return ap_construct_url(r->pool, u, r);
1113 if (l1 >= l2 && strncasecmp(real, url, l2) == 0) {
1114 u = apr_pstrcat(r->pool, ent[i].fake, &url[l2], NULL);
1115 return ap_construct_url(r->pool, u, r);
1123 * Cookies are a bit trickier to match: we've got two substrings to worry
1124 * about, and we can't just find them with strstr 'cos of case. Regexp
1125 * matching would be an easy fix, but for better consistency with all the
1126 * other matches we'll refrain and use apr_strmatch to find path=/domain=
1127 * and stick to plain strings for the config values.
1129 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r,
1130 proxy_dir_conf *conf, const char *str)
1132 proxy_req_conf *rconf = ap_get_module_config(r->request_config,
1134 struct proxy_alias *ent;
1135 size_t len = strlen(str);
1136 const char *newpath = NULL;
1137 const char *newdomain = NULL;
1139 const char *domainp;
1140 const char *pathe = NULL;
1141 const char *domaine = NULL;
1142 size_t l1, l2, poffs = 0, doffs = 0;
1148 if (r->proxyreq != PROXYREQ_REVERSE) {
1153 * Find the match and replacement, but save replacing until we've done
1154 * both path and domain so we know the new strlen
1156 if ((pathp = apr_strmatch(conf->cookie_path_str, str, len)) != NULL) {
1158 poffs = pathp - str;
1159 pathe = ap_strchr_c(pathp, ';');
1160 l1 = pathe ? (pathe - pathp) : strlen(pathp);
1161 pathe = pathp + l1 ;
1162 if (conf->interpolate_env == 1) {
1163 ent = (struct proxy_alias *)rconf->cookie_paths->elts;
1166 ent = (struct proxy_alias *)conf->cookie_paths->elts;
1168 for (i = 0; i < conf->cookie_paths->nelts; i++) {
1169 l2 = strlen(ent[i].fake);
1170 if (l1 >= l2 && strncmp(ent[i].fake, pathp, l2) == 0) {
1171 newpath = ent[i].real;
1172 pdiff = strlen(newpath) - l1;
1178 if ((domainp = apr_strmatch(conf->cookie_domain_str, str, len)) != NULL) {
1180 doffs = domainp - str;
1181 domaine = ap_strchr_c(domainp, ';');
1182 l1 = domaine ? (domaine - domainp) : strlen(domainp);
1183 domaine = domainp + l1;
1184 if (conf->interpolate_env == 1) {
1185 ent = (struct proxy_alias *)rconf->cookie_domains->elts;
1188 ent = (struct proxy_alias *)conf->cookie_domains->elts;
1190 for (i = 0; i < conf->cookie_domains->nelts; i++) {
1191 l2 = strlen(ent[i].fake);
1192 if (l1 >= l2 && strncasecmp(ent[i].fake, domainp, l2) == 0) {
1193 newdomain = ent[i].real;
1194 ddiff = strlen(newdomain) - l1;
1201 ret = apr_palloc(r->pool, len + pdiff + ddiff + 1);
1202 l1 = strlen(newpath);
1204 l2 = strlen(newdomain);
1205 if (doffs > poffs) {
1206 memcpy(ret, str, poffs);
1207 memcpy(ret + poffs, newpath, l1);
1208 memcpy(ret + poffs + l1, pathe, domainp - pathe);
1209 memcpy(ret + doffs + pdiff, newdomain, l2);
1210 strcpy(ret + doffs + pdiff + l2, domaine);
1213 memcpy(ret, str, doffs) ;
1214 memcpy(ret + doffs, newdomain, l2);
1215 memcpy(ret + doffs + l2, domaine, pathp - domaine);
1216 memcpy(ret + poffs + ddiff, newpath, l1);
1217 strcpy(ret + poffs + ddiff + l1, pathe);
1221 memcpy(ret, str, poffs);
1222 memcpy(ret + poffs, newpath, l1);
1223 strcpy(ret + poffs + l1, pathe);
1228 ret = apr_palloc(r->pool, len + pdiff + ddiff + 1);
1229 l2 = strlen(newdomain);
1230 memcpy(ret, str, doffs);
1231 memcpy(ret + doffs, newdomain, l2);
1232 strcpy(ret + doffs+l2, domaine);
1235 ret = (char *)str; /* no change */
1242 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
1243 proxy_server_conf *conf,
1246 proxy_balancer *balancer;
1247 char *c, *uri = apr_pstrdup(p, url);
1250 c = strchr(uri, ':');
1251 if (c == NULL || c[1] != '/' || c[2] != '/' || c[3] == '\0') {
1254 /* remove path from uri */
1255 if ((c = strchr(c + 3, '/'))) {
1258 balancer = (proxy_balancer *)conf->balancers->elts;
1259 for (i = 0; i < conf->balancers->nelts; i++) {
1260 if (strcasecmp(balancer->name, uri) == 0) {
1268 PROXY_DECLARE(const char *) ap_proxy_add_balancer(proxy_balancer **balancer,
1270 proxy_server_conf *conf,
1273 char *c, *q, *uri = apr_pstrdup(p, url);
1274 proxy_balancer_method *lbmethod;
1276 c = strchr(uri, ':');
1277 if (c == NULL || c[1] != '/' || c[2] != '/' || c[3] == '\0')
1278 return "Bad syntax for a balancer name";
1279 /* remove path from uri */
1280 if ((q = strchr(c + 3, '/')))
1283 ap_str_tolower(uri);
1284 *balancer = apr_array_push(conf->balancers);
1285 memset(*balancer, 0, sizeof(proxy_balancer));
1288 * NOTE: The default method is byrequests, which we assume
1291 lbmethod = ap_lookup_provider(PROXY_LBMETHOD, "byrequests", "0");
1293 return "Can't find 'byrequests' lb method";
1296 (*balancer)->name = uri;
1297 (*balancer)->lbmethod = lbmethod;
1298 (*balancer)->workers = apr_array_make(p, 5, sizeof(proxy_worker));
1299 /* XXX Is this a right place to create mutex */
1301 if (apr_thread_mutex_create(&((*balancer)->mutex),
1302 APR_THREAD_MUTEX_DEFAULT, p) != APR_SUCCESS) {
1303 /* XXX: Do we need to log something here */
1304 return "can not create thread mutex";
1311 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
1312 proxy_server_conf *conf,
1315 proxy_worker *worker;
1316 proxy_worker *max_worker = NULL;
1320 int worker_name_length;
1325 c = ap_strchr_c(url, ':');
1326 if (c == NULL || c[1] != '/' || c[2] != '/' || c[3] == '\0') {
1330 url_copy = apr_pstrdup(p, url);
1331 url_length = strlen(url);
1334 * We need to find the start of the path and
1335 * therefore we know the length of the scheme://hostname/
1336 * part to we can force-lowercase everything up to
1337 * the start of the path.
1339 c = ap_strchr_c(c+3, '/');
1342 pathstart = url_copy + (c - url);
1344 ap_str_tolower(url_copy);
1345 min_match = strlen(url_copy);
1349 ap_str_tolower(url_copy);
1350 min_match = strlen(url_copy);
1353 worker = (proxy_worker *)conf->workers->elts;
1356 * Do a "longest match" on the worker name to find the worker that
1357 * fits best to the URL, but keep in mind that we must have at least
1358 * a minimum matching of length min_match such that
1359 * scheme://hostname[:port] matches between worker and url.
1361 for (i = 0; i < conf->workers->nelts; i++) {
1362 if ( ((worker_name_length = strlen(worker->name)) <= url_length)
1363 && (worker_name_length >= min_match)
1364 && (worker_name_length > max_match)
1365 && (strncmp(url_copy, worker->name, worker_name_length) == 0) ) {
1366 max_worker = worker;
1367 max_match = worker_name_length;
1375 static apr_status_t conn_pool_cleanup(void *theworker)
1377 proxy_worker *worker = (proxy_worker *)theworker;
1378 if (worker->cp->res) {
1379 worker->cp->pool = NULL;
1380 apr_reslist_destroy(worker->cp->res);
1386 static void init_conn_pool(apr_pool_t *p, proxy_worker *worker)
1389 proxy_conn_pool *cp;
1392 * Create a connection pool's subpool.
1393 * This pool is used for connection recycling.
1394 * Once the worker is added it is never removed but
1395 * it can be disabled.
1397 apr_pool_create(&pool, p);
1398 apr_pool_tag(pool, "proxy_worker_cp");
1400 * Alloc from the same pool as worker.
1401 * proxy_conn_pool is permanently attached to the worker.
1403 cp = (proxy_conn_pool *)apr_pcalloc(p, sizeof(proxy_conn_pool));
1408 PROXY_DECLARE(const char *) ap_proxy_add_worker(proxy_worker **worker,
1410 proxy_server_conf *conf,
1416 rv = apr_uri_parse(p, url, &uri);
1418 if (rv != APR_SUCCESS) {
1419 return "Unable to parse URL";
1421 if (!uri.hostname || !uri.scheme) {
1422 return "URL must be absolute!";
1425 ap_str_tolower(uri.hostname);
1426 ap_str_tolower(uri.scheme);
1427 *worker = apr_array_push(conf->workers);
1428 memset(*worker, 0, sizeof(proxy_worker));
1429 (*worker)->name = apr_uri_unparse(p, &uri, APR_URI_UNP_REVEALPASSWORD);
1430 (*worker)->scheme = uri.scheme;
1431 (*worker)->hostname = uri.hostname;
1432 (*worker)->port = uri.port;
1433 (*worker)->id = proxy_lb_workers;
1434 (*worker)->flush_packets = flush_off;
1435 (*worker)->flush_wait = PROXY_FLUSH_WAIT;
1436 /* Increase the total worker count */
1438 init_conn_pool(p, *worker);
1440 if (apr_thread_mutex_create(&((*worker)->mutex),
1441 APR_THREAD_MUTEX_DEFAULT, p) != APR_SUCCESS) {
1442 /* XXX: Do we need to log something here */
1443 return "can not create thread mutex";
1450 PROXY_DECLARE(proxy_worker *) ap_proxy_create_worker(apr_pool_t *p)
1453 proxy_worker *worker;
1454 worker = (proxy_worker *)apr_pcalloc(p, sizeof(proxy_worker));
1455 worker->id = proxy_lb_workers;
1456 /* Increase the total worker count */
1458 init_conn_pool(p, worker);
1464 ap_proxy_add_worker_to_balancer(apr_pool_t *pool, proxy_balancer *balancer,
1465 proxy_worker *worker)
1467 proxy_worker *runtime;
1469 runtime = apr_array_push(balancer->workers);
1470 memcpy(runtime, worker, sizeof(proxy_worker));
1471 runtime->id = proxy_lb_workers;
1472 /* Increase the total runtime count */
1477 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
1478 proxy_balancer **balancer,
1480 proxy_server_conf *conf, char **url)
1484 access_status = proxy_run_pre_request(worker, balancer, r, conf, url);
1485 if (access_status == DECLINED && *balancer == NULL) {
1486 *worker = ap_proxy_get_worker(r->pool, conf, *url);
1488 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1489 "proxy: %s: found worker %s for %s",
1490 (*worker)->scheme, (*worker)->name, *url);
1495 else if (r->proxyreq == PROXYREQ_PROXY) {
1496 if (conf->forward) {
1497 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1498 "proxy: *: found forward proxy worker for %s",
1501 *worker = conf->forward;
1505 else if (r->proxyreq == PROXYREQ_REVERSE) {
1506 if (conf->reverse) {
1507 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1508 "proxy: *: found reverse proxy worker for %s",
1511 *worker = conf->reverse;
1516 else if (access_status == DECLINED && *balancer != NULL) {
1517 /* All the workers are busy */
1518 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1519 "proxy: all workers are busy. Unable to serve %s",
1521 access_status = HTTP_SERVICE_UNAVAILABLE;
1523 return access_status;
1526 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
1527 proxy_balancer *balancer,
1529 proxy_server_conf *conf)
1533 access_status = proxy_run_post_request(worker, balancer, r, conf);
1539 return access_status;
1543 PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **newsock,
1544 const char *proxy_function,
1545 apr_sockaddr_t *backend_addr,
1546 const char *backend_name,
1547 proxy_server_conf *conf,
1555 while (backend_addr && !connected) {
1556 if ((rv = apr_socket_create(newsock, backend_addr->family,
1557 SOCK_STREAM, 0, p)) != APR_SUCCESS) {
1558 loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
1559 ap_log_error(APLOG_MARK, loglevel, rv, s,
1560 "proxy: %s: error creating fam %d socket for target %s",
1562 backend_addr->family,
1565 * this could be an IPv6 address from the DNS but the
1566 * local machine won't give us an IPv6 socket; hopefully the
1567 * DNS returned an additional address to try
1569 backend_addr = backend_addr->next;
1573 #if !defined(TPF) && !defined(BEOS)
1574 if (conf->recv_buffer_size > 0 &&
1575 (rv = apr_socket_opt_set(*newsock, APR_SO_RCVBUF,
1576 conf->recv_buffer_size))) {
1577 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
1578 "apr_socket_opt_set(SO_RCVBUF): Failed to set "
1579 "ProxyReceiveBufferSize, using default");
1583 rv = apr_socket_opt_set(*newsock, APR_TCP_NODELAY, 1);
1584 if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
1585 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
1586 "apr_socket_opt_set(APR_TCP_NODELAY): "
1590 /* Set a timeout on the socket */
1591 if (conf->timeout_set == 1) {
1592 apr_socket_timeout_set(*newsock, conf->timeout);
1595 apr_socket_timeout_set(*newsock, s->timeout);
1598 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1599 "proxy: %s: fam %d socket created to connect to %s",
1600 proxy_function, backend_addr->family, backend_name);
1602 /* make the connection out of the socket */
1603 rv = apr_socket_connect(*newsock, backend_addr);
1605 /* if an error occurred, loop round and try again */
1606 if (rv != APR_SUCCESS) {
1607 apr_socket_close(*newsock);
1608 loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
1609 ap_log_error(APLOG_MARK, loglevel, rv, s,
1610 "proxy: %s: attempt to connect to %pI (%s) failed",
1614 backend_addr = backend_addr->next;
1619 return connected ? 0 : 1;
1622 static apr_status_t connection_cleanup(void *theconn)
1624 proxy_conn_rec *conn = (proxy_conn_rec *)theconn;
1625 proxy_worker *worker = conn->worker;
1626 apr_bucket_brigade *bb;
1631 * If the connection pool is NULL the worker
1632 * cleanup has been run. Just return.
1639 /* Sanity check: Did we already return the pooled connection? */
1640 if (conn->inreslist) {
1641 ap_log_perror(APLOG_MARK, APLOG_ERR, 0, conn->pool,
1642 "proxy: Pooled connection 0x%pp for worker %s has been"
1643 " already returned to the connection pool.", conn,
1650 if (conn->need_flush && r && (r->bytes_sent || r->eos_sent)) {
1652 * We need to ensure that buckets that may have been buffered in the
1653 * network filters get flushed to the network. This is needed since
1654 * these buckets have been created with the bucket allocator of the
1655 * backend connection. This allocator either gets destroyed if
1656 * conn->close is set or the worker address is not reusable which
1657 * causes the connection to the backend to be closed or it will be used
1658 * again by another frontend connection that wants to recycle the
1659 * backend connection.
1660 * In this case we could run into nasty race conditions (e.g. if the
1661 * next user of the backend connection destroys the allocator before we
1662 * sent the buckets to the network).
1664 * Remark 1: Only do this if buckets where sent down the chain before
1665 * that could still be buffered in the network filter. This is the case
1666 * if we have sent an EOS bucket or if we actually sent buckets with
1667 * data down the chain. In all other cases we either have not sent any
1668 * buckets at all down the chain or we only sent meta buckets that are
1669 * not EOS buckets down the chain. The only meta bucket that remains in
1670 * this case is the flush bucket which would have removed all possibly
1671 * buffered buckets in the network filter.
1672 * If we sent a flush bucket in the case where not ANY buckets were
1673 * sent down the chain, we break error handling which happens AFTER us.
1675 * Remark 2: Doing a setaside does not help here as the buckets remain
1676 * created by the wrong allocator in this case.
1678 * Remark 3: Yes, this creates a possible performance penalty in the case
1679 * of pipelined requests as we may send only a small amount of data over
1683 bb = apr_brigade_create(r->pool, c->bucket_alloc);
1686 * If we have already sent an EOS bucket send directly to the
1687 * connection based filters. We just want to flush the buckets
1688 * if something hasn't been sent to the network yet.
1690 ap_fflush(c->output_filters, bb);
1693 ap_fflush(r->output_filters, bb);
1695 apr_brigade_destroy(bb);
1697 conn->need_flush = 0;
1700 /* determine if the connection need to be closed */
1701 if (conn->close || !worker->is_address_reusable) {
1702 apr_pool_t *p = conn->pool;
1704 conn = apr_pcalloc(p, sizeof(proxy_conn_rec));
1706 conn->worker = worker;
1707 apr_pool_create(&(conn->scpool), p);
1708 apr_pool_tag(conn->scpool, "proxy_conn_scpool");
1711 if (worker->hmax && worker->cp->res) {
1712 conn->inreslist = 1;
1713 apr_reslist_release(worker->cp->res, (void *)conn);
1718 worker->cp->conn = conn;
1721 /* Always return the SUCCESS */
1725 static void socket_cleanup(proxy_conn_rec *conn)
1728 conn->connection = NULL;
1729 apr_pool_clear(conn->scpool);
1732 PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
1735 apr_bucket_brigade *bb;
1739 * If we have an existing SSL connection it might be possible that the
1740 * server sent some SSL message we have not read so far (e.g. a SSL
1741 * shutdown message if the server closed the keepalive connection while
1742 * the connection was held unused in our pool).
1743 * So ensure that if present (=> APR_NONBLOCK_READ) it is read and
1744 * processed. We don't expect any data to be in the returned brigade.
1746 if (conn->sock && conn->connection) {
1747 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1748 rv = ap_get_brigade(conn->connection->input_filters, bb,
1749 AP_MODE_READBYTES, APR_NONBLOCK_READ,
1751 if ((rv != APR_SUCCESS) && !APR_STATUS_IS_EAGAIN(rv)) {
1752 socket_cleanup(conn);
1754 if (!APR_BRIGADE_EMPTY(bb)) {
1757 rv = apr_brigade_length(bb, 0, &len);
1758 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
1759 "proxy: SSL cleanup brigade contained %"
1760 APR_OFF_T_FMT " bytes of data.", len);
1762 apr_brigade_destroy(bb);
1767 /* reslist constructor */
1768 static apr_status_t connection_constructor(void **resource, void *params,
1773 proxy_conn_rec *conn;
1774 proxy_worker *worker = (proxy_worker *)params;
1777 * Create the subpool for each connection
1778 * This keeps the memory consumption constant
1779 * when disconnecting from backend.
1781 apr_pool_create(&ctx, pool);
1782 apr_pool_tag(ctx, "proxy_conn_pool");
1784 * Create another subpool that manages the data for the
1785 * socket and the connection member of the proxy_conn_rec struct as we
1786 * destroy this data more frequently than other data in the proxy_conn_rec
1787 * struct like hostname and addr (at least in the case where we have
1788 * keepalive connections that timed out).
1790 apr_pool_create(&scpool, ctx);
1791 apr_pool_tag(scpool, "proxy_conn_scpool");
1792 conn = apr_pcalloc(ctx, sizeof(proxy_conn_rec));
1795 conn->scpool = scpool;
1796 conn->worker = worker;
1798 conn->inreslist = 1;
1805 #if APR_HAS_THREADS /* only needed when threads are used */
1806 /* reslist destructor */
1807 static apr_status_t connection_destructor(void *resource, void *params,
1810 proxy_conn_rec *conn = (proxy_conn_rec *)resource;
1812 /* Destroy the pool only if not called from reslist_destroy */
1813 if (conn->worker->cp->pool) {
1814 apr_pool_destroy(conn->pool);
1822 * ap_proxy_initialize_worker_share() concerns itself
1823 * with initializing those parts of worker which
1824 * are, or could be, shared. Basically worker->s
1826 PROXY_DECLARE(void) ap_proxy_initialize_worker_share(proxy_server_conf *conf,
1827 proxy_worker *worker,
1830 proxy_worker_stat *score = NULL;
1832 if (PROXY_WORKER_IS_INITIALIZED(worker)) {
1833 /* The worker share is already initialized */
1834 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1835 "proxy: worker %s already initialized",
1839 /* Get scoreboard slot */
1840 if (ap_scoreboard_image) {
1841 score = (proxy_worker_stat *) ap_get_scoreboard_lb(worker->id);
1843 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
1844 "proxy: ap_get_scoreboard_lb(%d) failed in child %" APR_PID_T_FMT " for worker %s",
1845 worker->id, getpid(), worker->name);
1848 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1849 "proxy: grabbed scoreboard slot %d in child %" APR_PID_T_FMT " for worker %s",
1850 worker->id, getpid(), worker->name);
1854 score = (proxy_worker_stat *) apr_pcalloc(conf->pool, sizeof(proxy_worker_stat));
1855 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1856 "proxy: initialized plain memory in child %" APR_PID_T_FMT " for worker %s",
1857 getpid(), worker->name);
1861 * recheck to see if we've already been here. Possible
1862 * if proxy is using scoreboard to hold shared stats
1864 if (PROXY_WORKER_IS_INITIALIZED(worker)) {
1865 /* The worker share is already initialized */
1866 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1867 "proxy: worker %s already initialized",
1871 if (worker->route) {
1872 strcpy(worker->s->route, worker->route);
1875 *worker->s->route = '\0';
1877 if (worker->redirect) {
1878 strcpy(worker->s->redirect, worker->redirect);
1881 *worker->s->redirect = '\0';
1884 worker->s->status |= (worker->status | PROXY_WORKER_INITIALIZED);
1888 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, server_rec *s)
1896 if (worker->status & PROXY_WORKER_INITIALIZED) {
1897 /* The worker is already initialized */
1901 /* Set default parameters */
1902 if (!worker->retry_set) {
1903 worker->retry = apr_time_from_sec(PROXY_WORKER_DEFAULT_RETRY);
1905 /* By default address is reusable */
1906 worker->is_address_reusable = 1;
1909 ap_mpm_query(AP_MPMQ_MAX_THREADS, &mpm_threads);
1910 if (mpm_threads > 1) {
1911 /* Set hard max to no more then mpm_threads */
1912 if (worker->hmax == 0 || worker->hmax > mpm_threads) {
1913 worker->hmax = mpm_threads;
1915 if (worker->smax == 0 || worker->smax > worker->hmax) {
1916 worker->smax = worker->hmax;
1918 /* Set min to be lower then smax */
1919 if (worker->min > worker->smax) {
1920 worker->min = worker->smax;
1924 /* This will supress the apr_reslist creation */
1925 worker->min = worker->smax = worker->hmax = 0;
1928 rv = apr_reslist_create(&(worker->cp->res),
1929 worker->min, worker->smax,
1930 worker->hmax, worker->ttl,
1931 connection_constructor, connection_destructor,
1932 worker, worker->cp->pool);
1934 apr_pool_cleanup_register(worker->cp->pool, (void *)worker,
1936 apr_pool_cleanup_null);
1938 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1939 "proxy: initialized worker %d in child %" APR_PID_T_FMT " for (%s) min=%d max=%d smax=%d",
1940 worker->id, getpid(), worker->hostname, worker->min,
1941 worker->hmax, worker->smax);
1943 #if (APR_MAJOR_VERSION > 0)
1944 /* Set the acquire timeout */
1945 if (rv == APR_SUCCESS && worker->acquire_set) {
1946 apr_reslist_timeout_set(worker->cp->res, worker->acquire);
1955 rv = connection_constructor(&conn, worker, worker->cp->pool);
1956 worker->cp->conn = conn;
1958 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1959 "proxy: initialized single connection worker %d in child %" APR_PID_T_FMT " for (%s)",
1960 worker->id, getpid(), worker->hostname);
1962 if (rv == APR_SUCCESS) {
1963 worker->status |= (PROXY_WORKER_INITIALIZED);
1968 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
1969 proxy_worker *worker,
1972 if (worker->s->status & PROXY_WORKER_IN_ERROR) {
1973 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1974 "proxy: %s: retrying the worker for (%s)",
1975 proxy_function, worker->hostname);
1976 if (apr_time_now() > worker->s->error_time + worker->retry) {
1977 ++worker->s->retries;
1978 worker->s->status &= ~PROXY_WORKER_IN_ERROR;
1979 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1980 "proxy: %s: worker for (%s) has been marked for retry",
1981 proxy_function, worker->hostname);
1993 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
1994 proxy_conn_rec **conn,
1995 proxy_worker *worker,
2000 if (!PROXY_WORKER_IS_USABLE(worker)) {
2001 /* Retry the worker */
2002 ap_proxy_retry_worker(proxy_function, worker, s);
2004 if (!PROXY_WORKER_IS_USABLE(worker)) {
2005 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
2006 "proxy: %s: disabled connection for (%s)",
2007 proxy_function, worker->hostname);
2008 return HTTP_SERVICE_UNAVAILABLE;
2012 if (worker->hmax && worker->cp->res) {
2013 rv = apr_reslist_acquire(worker->cp->res, (void **)conn);
2018 /* create the new connection if the previous was destroyed */
2019 if (!worker->cp->conn) {
2020 connection_constructor((void **)conn, worker, worker->cp->pool);
2023 *conn = worker->cp->conn;
2024 worker->cp->conn = NULL;
2029 if (rv != APR_SUCCESS) {
2030 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
2031 "proxy: %s: failed to acquire connection for (%s)",
2032 proxy_function, worker->hostname);
2033 return HTTP_SERVICE_UNAVAILABLE;
2035 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2036 "proxy: %s: has acquired connection for (%s)",
2037 proxy_function, worker->hostname);
2039 (*conn)->worker = worker;
2042 (*conn)->inreslist = 0;
2048 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
2049 proxy_conn_rec *conn,
2052 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2053 "proxy: %s: has released connection for (%s)",
2054 proxy_function, conn->worker->hostname);
2055 connection_cleanup(conn);
2061 ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
2062 proxy_server_conf *conf,
2063 proxy_worker *worker,
2064 proxy_conn_rec *conn,
2067 const char *proxyname,
2068 apr_port_t proxyport,
2069 char *server_portstr,
2070 int server_portstr_size)
2073 apr_status_t err = APR_SUCCESS;
2074 apr_status_t uerr = APR_SUCCESS;
2079 * Break up the URL to determine the host to connect to
2082 /* we break the URL into host, port, uri */
2083 if (APR_SUCCESS != apr_uri_parse(p, *url, uri)) {
2084 return ap_proxyerror(r, HTTP_BAD_REQUEST,
2085 apr_pstrcat(p,"URI cannot be parsed: ", *url,
2089 uri->port = apr_uri_port_of_scheme(uri->scheme);
2092 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
2093 "proxy: connecting %s to %s:%d", *url, uri->hostname,
2097 * allocate these out of the specified connection pool
2098 * The scheme handler decides if this is permanent or
2099 * short living pool.
2101 /* are we connecting directly, or via a proxy? */
2103 *url = apr_pstrcat(p, uri->path, uri->query ? "?" : "",
2104 uri->query ? uri->query : "",
2105 uri->fragment ? "#" : "",
2106 uri->fragment ? uri->fragment : "", NULL);
2109 * Make sure that we pick the the correct and valid worker.
2110 * If a single keepalive connection triggers different workers,
2111 * then we have a problem (we don't select the correct one).
2112 * Do an expensive check in this case, where we compare the
2113 * the hostnames associated between the two.
2115 * TODO: Handle this much better...
2117 if (!conn->hostname || !worker->is_address_reusable ||
2118 (r->connection->keepalives &&
2119 (r->proxyreq == PROXYREQ_PROXY || r->proxyreq == PROXYREQ_REVERSE) &&
2120 (strcasecmp(conn->hostname, uri->hostname) != 0) ) ) {
2122 conn->hostname = apr_pstrdup(conn->pool, proxyname);
2123 conn->port = proxyport;
2126 conn->hostname = apr_pstrdup(conn->pool, uri->hostname);
2127 conn->port = uri->port;
2129 socket_cleanup(conn);
2130 err = apr_sockaddr_info_get(&(conn->addr),
2131 conn->hostname, APR_UNSPEC,
2135 else if (!worker->cp->addr) {
2136 if ((err = PROXY_THREAD_LOCK(worker)) != APR_SUCCESS) {
2137 ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server,
2139 return HTTP_INTERNAL_SERVER_ERROR;
2143 * Worker can have the single constant backend adress.
2144 * The single DNS lookup is used once per worker.
2145 * If dynamic change is needed then set the addr to NULL
2146 * inside dynamic config to force the lookup.
2148 err = apr_sockaddr_info_get(&(worker->cp->addr),
2149 conn->hostname, APR_UNSPEC,
2152 conn->addr = worker->cp->addr;
2153 if ((uerr = PROXY_THREAD_UNLOCK(worker)) != APR_SUCCESS) {
2154 ap_log_error(APLOG_MARK, APLOG_ERR, uerr, r->server,
2159 conn->addr = worker->cp->addr;
2162 if (err != APR_SUCCESS) {
2163 return ap_proxyerror(r, HTTP_BAD_GATEWAY,
2164 apr_pstrcat(p, "DNS lookup failure for: ",
2165 conn->hostname, NULL));
2168 /* Get the server port for the Via headers */
2170 server_port = ap_get_server_port(r);
2171 if (ap_is_default_port(server_port, r)) {
2172 strcpy(server_portstr,"");
2175 apr_snprintf(server_portstr, server_portstr_size, ":%d",
2179 /* check if ProxyBlock directive on this host */
2180 if (OK != ap_proxy_checkproxyblock(r, conf, conn->addr)) {
2181 return ap_proxyerror(r, HTTP_FORBIDDEN,
2182 "Connect to remote machine blocked");
2184 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
2185 "proxy: connected %s to %s:%d", *url, conn->hostname,
2190 #define USE_ALTERNATE_IS_CONNECTED 1
2192 #if !defined(APR_MSG_PEEK) && defined(MSG_PEEK)
2193 #define APR_MSG_PEEK MSG_PEEK
2196 #if USE_ALTERNATE_IS_CONNECTED && defined(APR_MSG_PEEK)
2197 static int is_socket_connected(apr_socket_t *socket)
2199 apr_pollfd_t pfds[1];
2200 apr_status_t status;
2203 pfds[0].reqevents = APR_POLLIN;
2204 pfds[0].desc_type = APR_POLL_SOCKET;
2205 pfds[0].desc.s = socket;
2208 status = apr_poll(&pfds[0], 1, &nfds, 0);
2209 } while (APR_STATUS_IS_EINTR(status));
2211 if (status == APR_SUCCESS && nfds == 1 &&
2212 pfds[0].rtnevents == APR_POLLIN) {
2213 apr_sockaddr_t unused;
2216 /* The socket might be closed in which case
2217 * the poll will return POLLIN.
2218 * If there is no data available the socket
2221 status = apr_socket_recvfrom(&unused, socket, APR_MSG_PEEK,
2223 if (status == APR_SUCCESS && len)
2228 else if (APR_STATUS_IS_EAGAIN(status) || APR_STATUS_IS_TIMEUP(status)) {
2235 static int is_socket_connected(apr_socket_t *sock)
2238 apr_size_t buffer_len = 1;
2239 char test_buffer[1];
2240 apr_status_t socket_status;
2241 apr_interval_time_t current_timeout;
2244 apr_socket_timeout_get(sock, ¤t_timeout);
2245 /* set no timeout */
2246 apr_socket_timeout_set(sock, 0);
2247 socket_status = apr_socket_recv(sock, test_buffer, &buffer_len);
2248 /* put back old timeout */
2249 apr_socket_timeout_set(sock, current_timeout);
2250 if (APR_STATUS_IS_EOF(socket_status)
2251 || APR_STATUS_IS_ECONNRESET(socket_status)) {
2258 #endif /* USE_ALTERNATE_IS_CONNECTED */
2260 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
2261 proxy_conn_rec *conn,
2262 proxy_worker *worker,
2268 apr_sockaddr_t *backend_addr = conn->addr;
2269 apr_socket_t *newsock;
2270 void *sconf = s->module_config;
2271 proxy_server_conf *conf =
2272 (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
2275 if (!(connected = is_socket_connected(conn->sock))) {
2276 socket_cleanup(conn);
2277 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2278 "proxy: %s: backend socket is disconnected.",
2282 while (backend_addr && !connected) {
2283 if ((rv = apr_socket_create(&newsock, backend_addr->family,
2284 SOCK_STREAM, APR_PROTO_TCP,
2285 conn->scpool)) != APR_SUCCESS) {
2286 loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
2287 ap_log_error(APLOG_MARK, loglevel, rv, s,
2288 "proxy: %s: error creating fam %d socket for target %s",
2290 backend_addr->family,
2293 * this could be an IPv6 address from the DNS but the
2294 * local machine won't give us an IPv6 socket; hopefully the
2295 * DNS returned an additional address to try
2297 backend_addr = backend_addr->next;
2300 conn->connection = NULL;
2302 #if !defined(TPF) && !defined(BEOS)
2303 if (worker->recv_buffer_size > 0 &&
2304 (rv = apr_socket_opt_set(newsock, APR_SO_RCVBUF,
2305 worker->recv_buffer_size))) {
2306 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
2307 "apr_socket_opt_set(SO_RCVBUF): Failed to set "
2308 "ProxyReceiveBufferSize, using default");
2312 rv = apr_socket_opt_set(newsock, APR_TCP_NODELAY, 1);
2313 if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
2314 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
2315 "apr_socket_opt_set(APR_TCP_NODELAY): "
2319 /* Set a timeout on the socket */
2320 if (worker->timeout_set == 1) {
2321 apr_socket_timeout_set(newsock, worker->timeout);
2323 else if (conf->timeout_set == 1) {
2324 apr_socket_timeout_set(newsock, conf->timeout);
2327 apr_socket_timeout_set(newsock, s->timeout);
2329 /* Set a keepalive option */
2330 if (worker->keepalive) {
2331 if ((rv = apr_socket_opt_set(newsock,
2332 APR_SO_KEEPALIVE, 1)) != APR_SUCCESS) {
2333 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
2334 "apr_socket_opt_set(SO_KEEPALIVE): Failed to set"
2338 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2339 "proxy: %s: fam %d socket created to connect to %s",
2340 proxy_function, backend_addr->family, worker->hostname);
2342 /* make the connection out of the socket */
2343 rv = apr_socket_connect(newsock, backend_addr);
2345 /* if an error occurred, loop round and try again */
2346 if (rv != APR_SUCCESS) {
2347 apr_socket_close(newsock);
2348 loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
2349 ap_log_error(APLOG_MARK, loglevel, rv, s,
2350 "proxy: %s: attempt to connect to %pI (%s) failed",
2354 backend_addr = backend_addr->next;
2358 conn->sock = newsock;
2362 * Put the entire worker to error state if
2363 * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
2364 * Altrough some connections may be alive
2365 * no further connections to the worker could be made
2367 if (!connected && PROXY_WORKER_IS_USABLE(worker) &&
2368 !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
2369 worker->s->status |= PROXY_WORKER_IN_ERROR;
2370 worker->s->error_time = apr_time_now();
2371 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
2372 "ap_proxy_connect_backend disabling worker for (%s)",
2376 worker->s->error_time = 0;
2377 worker->s->retries = 0;
2379 return connected ? OK : DECLINED;
2382 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
2383 proxy_conn_rec *conn,
2387 apr_sockaddr_t *backend_addr = conn->addr;
2389 apr_interval_time_t current_timeout;
2390 apr_bucket_alloc_t *bucket_alloc;
2392 if (conn->connection) {
2397 * We need to flush the buckets before we return the connection to the
2398 * connection pool. See comment in connection_cleanup for why this is
2401 conn->need_flush = 1;
2402 bucket_alloc = apr_bucket_alloc_create(conn->scpool);
2404 * The socket is now open, create a new backend server connection
2406 conn->connection = ap_run_create_connection(conn->scpool, s, conn->sock,
2410 if (!conn->connection) {
2412 * the peer reset the connection already; ap_run_create_connection()
2415 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
2416 s, "proxy: %s: an error occurred creating a "
2417 "new connection to %pI (%s)", proxy_function,
2418 backend_addr, conn->hostname);
2419 /* XXX: Will be closed when proxy_conn is closed */
2420 socket_cleanup(conn);
2421 return HTTP_INTERNAL_SERVER_ERROR;
2424 /* For ssl connection to backend */
2426 if (!ap_proxy_ssl_enable(conn->connection)) {
2427 ap_log_error(APLOG_MARK, APLOG_ERR, 0,
2428 s, "proxy: %s: failed to enable ssl support "
2429 "for %pI (%s)", proxy_function,
2430 backend_addr, conn->hostname);
2431 return HTTP_INTERNAL_SERVER_ERROR;
2435 /* TODO: See if this will break FTP */
2436 ap_proxy_ssl_disable(conn->connection);
2439 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2440 "proxy: %s: connection complete to %pI (%s)",
2441 proxy_function, backend_addr, conn->hostname);
2444 * save the timeout of the socket because core_pre_connection
2445 * will set it to base_server->timeout
2446 * (core TimeOut directive).
2448 apr_socket_timeout_get(conn->sock, ¤t_timeout);
2449 /* set up the connection filters */
2450 rc = ap_run_pre_connection(conn->connection, conn->sock);
2451 if (rc != OK && rc != DONE) {
2452 conn->connection->aborted = 1;
2453 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2454 "proxy: %s: pre_connection setup failed (%d)",
2455 proxy_function, rc);
2458 apr_socket_timeout_set(conn->sock, current_timeout);
2463 int ap_proxy_lb_workers(void)
2466 * Since we can't resize the scoreboard when reconfiguring, we
2467 * have to impose a limit on the number of workers, we are
2468 * able to reconfigure to.
2470 if (!lb_workers_limit)
2471 lb_workers_limit = proxy_lb_workers + PROXY_DYNAMIC_BALANCER_LIMIT;
2472 return lb_workers_limit;
2475 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
2476 apr_bucket_brigade *brigade)
2479 conn_rec *c = r->connection;
2483 * If this is a subrequest, then prevent also caching of the main
2487 r->main->no_cache = 1;
2488 e = ap_bucket_error_create(HTTP_BAD_GATEWAY, NULL, c->pool,
2490 APR_BRIGADE_INSERT_TAIL(brigade, e);
2491 e = apr_bucket_eos_create(c->bucket_alloc);
2492 APR_BRIGADE_INSERT_TAIL(brigade, e);