1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 /* HTTP routines for Apache proxy */
18 #include "mod_proxy.h"
20 module AP_MODULE_DECLARE_DATA proxy_http_module;
22 int ap_proxy_http_canon(request_rec *r, char *url);
23 int ap_proxy_http_handler(request_rec *r, proxy_worker *worker,
24 proxy_server_conf *conf,
25 char *url, const char *proxyname,
26 apr_port_t proxyport);
28 static apr_status_t ap_proxy_http_cleanup(const char *scheme,
30 proxy_conn_rec *backend);
33 * Canonicalise http-like URLs.
34 * scheme is the scheme for the URL
35 * url is the URL starting with the first '/'
36 * def_port is the default port for this scheme.
38 int ap_proxy_http_canon(request_rec *r, char *url)
40 char *host, *path, *search, sport[7];
43 apr_port_t port, def_port;
45 /* ap_port_of_scheme() */
46 if (strncasecmp(url, "http:", 5) == 0) {
50 else if (strncasecmp(url, "https:", 6) == 0) {
57 def_port = apr_uri_port_of_scheme(scheme);
59 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
60 "proxy: HTTP: canonicalising URL %s", url);
63 * We break the URL into host, port, path, search
66 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
68 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
69 "error parsing URL %s: %s",
71 return HTTP_BAD_REQUEST;
74 /* now parse path/search args, according to rfc1738 */
75 /* N.B. if this isn't a true proxy request, then the URL _path_
76 * has already been decoded. True proxy requests have r->uri
77 * == r->unparsed_uri, and no others have that property.
79 if (r->uri == r->unparsed_uri) {
80 search = strchr(url, '?');
88 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, r->proxyreq);
90 return HTTP_BAD_REQUEST;
93 apr_snprintf(sport, sizeof(sport), ":%d", port);
97 if (ap_strchr_c(host, ':')) { /* if literal IPv6 address */
98 host = apr_pstrcat(r->pool, "[", host, "]", NULL);
100 r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport,
101 "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
105 static const char *ap_proxy_location_reverse_map(request_rec *r, proxy_server_conf *conf, const char *url)
107 struct proxy_alias *ent;
111 /* XXX FIXME: Make sure this handled the ambiguous case of the :80
112 * after the hostname */
115 ent = (struct proxy_alias *)conf->raliases->elts;
116 for (i = 0; i < conf->raliases->nelts; i++) {
117 l2 = strlen(ent[i].real);
118 if (l1 >= l2 && strncasecmp(ent[i].real, url, l2) == 0) {
119 u = apr_pstrcat(r->pool, ent[i].fake, &url[l2], NULL);
120 return ap_construct_url(r->pool, u, r);
125 /* cookies are a bit trickier to match: we've got two substrings to worry
126 * about, and we can't just find them with strstr 'cos of case. Regexp
127 * matching would be an easy fix, but for better consistency with all the
128 * other matches we'll refrain and use apr_strmatch to find path=/domain=
129 * and stick to plain strings for the config values.
131 static const char *proxy_cookie_reverse_map(request_rec *r,
132 proxy_server_conf *conf, const char *str)
134 struct proxy_alias *ent;
135 size_t len = strlen(str);
136 const char* newpath = NULL ;
137 const char* newdomain = NULL ;
139 const char* domainp ;
140 const char* pathe = NULL;
141 const char* domaine = NULL;
142 size_t l1, l2, poffs = 0, doffs = 0 ;
148 /* find the match and replacement, but save replacing until we've done
149 both path and domain so we know the new strlen
151 if ( pathp = apr_strmatch(conf->cookie_path_str, str, len) , pathp ) {
153 poffs = pathp - str ;
154 pathe = ap_strchr_c(pathp, ';') ;
155 l1 = pathe ? (pathe-pathp) : strlen(pathp) ;
157 ent = (struct proxy_alias *)conf->cookie_paths->elts;
158 for (i = 0; i < conf->cookie_paths->nelts; i++) {
159 l2 = strlen(ent[i].fake);
160 if (l1 >= l2 && strncmp(ent[i].fake, pathp, l2) == 0) {
161 newpath = ent[i].real ;
162 pdiff = strlen(newpath) - l1 ;
167 if ( domainp = apr_strmatch(conf->cookie_domain_str, str, len) , domainp ) {
169 doffs = domainp - str ;
170 domaine = ap_strchr_c(domainp, ';') ;
171 l1 = domaine ? (domaine-domainp) : strlen(domainp) ;
172 domaine = domainp + l1 ;
173 ent = (struct proxy_alias *)conf->cookie_domains->elts;
174 for (i = 0; i < conf->cookie_domains->nelts; i++) {
175 l2 = strlen(ent[i].fake);
176 if (l1 >= l2 && strncasecmp(ent[i].fake, domainp, l2) == 0) {
177 newdomain = ent[i].real ;
178 ddiff = strlen(newdomain) - l1 ;
184 ret = apr_palloc(r->pool, len+pdiff+ddiff+1) ;
185 l1 = strlen(newpath) ;
187 l2 = strlen(newdomain) ;
188 if ( doffs > poffs ) {
189 memcpy(ret, str, poffs) ;
190 memcpy(ret+poffs, newpath, l1) ;
191 memcpy(ret+poffs+l1, pathe, domainp-pathe) ;
192 memcpy(ret+doffs+pdiff, newdomain, l2) ;
193 strcpy(ret+doffs+pdiff+l2, domaine) ;
195 memcpy(ret, str, doffs) ;
196 memcpy(ret+doffs, newdomain, l2) ;
197 memcpy(ret+doffs+l2, domaine, pathp-domaine) ;
198 memcpy(ret+poffs+ddiff, newpath, l1) ;
199 strcpy(ret+poffs+ddiff+l1, pathe) ;
202 memcpy(ret, str, poffs) ;
203 memcpy(ret+poffs, newpath, l1) ;
204 strcpy(ret+poffs+l1, pathe) ;
208 ret = apr_palloc(r->pool, len+pdiff+ddiff+1) ;
209 l2 = strlen(newdomain) ;
210 memcpy(ret, str, doffs) ;
211 memcpy(ret+doffs, newdomain, l2) ;
212 strcpy(ret+doffs+l2, domaine) ;
214 ret = (char*) str ; /* no change */
220 /* Clear all connection-based headers from the incoming headers table */
221 static void ap_proxy_clear_connection(apr_pool_t *p, apr_table_t *headers)
224 char *next = apr_pstrdup(p, apr_table_get(headers, "Connection"));
226 apr_table_unset(headers, "Proxy-Connection");
232 while (*next && !apr_isspace(*next) && (*next != ',')) {
235 while (*next && (apr_isspace(*next) || (*next == ','))) {
239 apr_table_unset(headers, name);
241 apr_table_unset(headers, "Connection");
245 apr_status_t ap_proxy_http_request(apr_pool_t *p, request_rec *r,
246 proxy_conn_rec *conn, conn_rec *origin,
247 proxy_server_conf *conf,
249 char *url, char *server_portstr)
251 conn_rec *c = r->connection;
253 apr_bucket *e, *last_header_bucket = NULL;
254 const apr_array_header_t *headers_in_array;
255 const apr_table_entry_t *headers_in;
256 int counter, seen_eos, send_chunks;
258 apr_bucket_brigade *header_brigade, *body_brigade, *input_brigade;
260 header_brigade = apr_brigade_create(p, origin->bucket_alloc);
261 body_brigade = apr_brigade_create(p, origin->bucket_alloc);
262 input_brigade = apr_brigade_create(p, origin->bucket_alloc);
265 * Send the HTTP/1.1 request to the remote server
268 /* strip connection listed hop-by-hop headers from the request */
269 /* even though in theory a connection: close coming from the client
270 * should not affect the connection to the server, it's unlikely
271 * that subsequent client requests will hit this thread/process, so
272 * we cancel server keepalive if the client does.
274 conn->close += ap_proxy_liststr(apr_table_get(r->headers_in,
275 "Connection"), "close");
277 /* sub-requests never use keepalives */
282 ap_proxy_clear_connection(p, r->headers_in);
284 apr_table_setn(r->headers_in, "Connection", "close");
285 origin->keepalive = AP_CONN_CLOSE;
288 /* By default, we can not send chunks. That means we must buffer
289 * the entire request before sending it along to ensure we have
290 * the correct Content-Length attached.
294 if (apr_table_get(r->subprocess_env, "force-proxy-request-1.0")) {
295 buf = apr_pstrcat(p, r->method, " ", url, " HTTP/1.0" CRLF, NULL);
297 buf = apr_pstrcat(p, r->method, " ", url, " HTTP/1.1" CRLF, NULL);
298 if (apr_table_get(r->subprocess_env, "proxy-sendchunks")) {
302 if (apr_table_get(r->subprocess_env, "proxy-nokeepalive")) {
303 apr_table_unset(r->headers_in, "Connection");
304 origin->keepalive = AP_CONN_CLOSE;
306 ap_xlate_proto_to_ascii(buf, strlen(buf));
307 e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
308 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
309 if (conf->preserve_host == 0) {
310 if (uri->port_str && uri->port != DEFAULT_HTTP_PORT) {
311 buf = apr_pstrcat(p, "Host: ", uri->hostname, ":", uri->port_str,
314 buf = apr_pstrcat(p, "Host: ", uri->hostname, CRLF, NULL);
318 /* don't want to use r->hostname, as the incoming header might have a
321 const char* hostname = apr_table_get(r->headers_in,"Host");
323 hostname = r->server->server_hostname;
324 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
325 "proxy: no HTTP 0.9 request (with no host line) "
326 "on incoming request and preserve host set "
327 "forcing hostname to be %s for uri %s",
331 buf = apr_pstrcat(p, "Host: ", hostname, CRLF, NULL);
333 ap_xlate_proto_to_ascii(buf, strlen(buf));
334 e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
335 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
338 if (conf->viaopt == via_block) {
339 /* Block all outgoing Via: headers */
340 apr_table_unset(r->headers_in, "Via");
341 } else if (conf->viaopt != via_off) {
342 const char *server_name = ap_get_server_name(r);
343 /* If USE_CANONICAL_NAME_OFF was configured for the proxy virtual host,
344 * then the server name returned by ap_get_server_name() is the
345 * origin server name (which does make too much sense with Via: headers)
346 * so we use the proxy vhost's name instead.
348 if (server_name == r->hostname)
349 server_name = r->server->server_hostname;
350 /* Create a "Via:" request header entry and merge it */
351 /* Generate outgoing Via: header with/without server comment: */
352 apr_table_mergen(r->headers_in, "Via",
353 (conf->viaopt == via_full)
354 ? apr_psprintf(p, "%d.%d %s%s (%s)",
355 HTTP_VERSION_MAJOR(r->proto_num),
356 HTTP_VERSION_MINOR(r->proto_num),
357 server_name, server_portstr,
358 AP_SERVER_BASEVERSION)
359 : apr_psprintf(p, "%d.%d %s%s",
360 HTTP_VERSION_MAJOR(r->proto_num),
361 HTTP_VERSION_MINOR(r->proto_num),
362 server_name, server_portstr)
366 /* X-Forwarded-*: handling
371 * These request headers are only really useful when the mod_proxy
372 * is used in a reverse proxy configuration, so that useful info
373 * about the client can be passed through the reverse proxy and on
374 * to the backend server, which may require the information to
377 * In a forward proxy situation, these options are a potential
378 * privacy violation, as information about clients behind the proxy
379 * are revealed to arbitrary servers out there on the internet.
381 * The HTTP/1.1 Via: header is designed for passing client
382 * information through proxies to a server, and should be used in
383 * a forward proxy configuation instead of X-Forwarded-*. See the
384 * ProxyVia option for details.
387 if (PROXYREQ_REVERSE == r->proxyreq) {
390 /* Add X-Forwarded-For: so that the upstream has a chance to
391 * determine, where the original request came from.
393 apr_table_mergen(r->headers_in, "X-Forwarded-For",
394 r->connection->remote_ip);
396 /* Add X-Forwarded-Host: so that upstream knows what the
397 * original request hostname was.
399 if ((buf = apr_table_get(r->headers_in, "Host"))) {
400 apr_table_mergen(r->headers_in, "X-Forwarded-Host", buf);
403 /* Add X-Forwarded-Server: so that upstream knows what the
404 * name of this proxy server is (if there are more than one)
405 * XXX: This duplicates Via: - do we strictly need it?
407 apr_table_mergen(r->headers_in, "X-Forwarded-Server",
408 r->server->server_hostname);
411 /* send request headers */
413 headers_in_array = apr_table_elts(r->headers_in);
414 headers_in = (const apr_table_entry_t *) headers_in_array->elts;
415 for (counter = 0; counter < headers_in_array->nelts; counter++) {
416 if (headers_in[counter].key == NULL || headers_in[counter].val == NULL
418 /* Clear out hop-by-hop request headers not to send
419 * RFC2616 13.5.1 says we should strip these headers
422 || !apr_strnatcasecmp(headers_in[counter].key, "Host")
424 || !apr_strnatcasecmp(headers_in[counter].key, "Keep-Alive")
425 || !apr_strnatcasecmp(headers_in[counter].key, "TE")
426 || !apr_strnatcasecmp(headers_in[counter].key, "Trailer")
427 || !apr_strnatcasecmp(headers_in[counter].key, "Transfer-Encoding")
428 || !apr_strnatcasecmp(headers_in[counter].key, "Upgrade")
430 /* We have no way of knowing whether this Content-Length will
431 * be accurate, so we must not include it.
433 || !apr_strnatcasecmp(headers_in[counter].key, "Content-Length")
434 /* XXX: @@@ FIXME: "Proxy-Authorization" should *only* be
435 * suppressed if THIS server requested the authentication,
436 * not when a frontend proxy requested it!
438 * The solution to this problem is probably to strip out
439 * the Proxy-Authorisation header in the authorisation
440 * code itself, not here. This saves us having to signal
441 * somehow whether this request was authenticated or not.
443 || !apr_strnatcasecmp(headers_in[counter].key,"Proxy-Authorization")
444 || !apr_strnatcasecmp(headers_in[counter].key,"Proxy-Authenticate")) {
447 /* for sub-requests, ignore freshness/expiry headers */
449 if (headers_in[counter].key == NULL || headers_in[counter].val == NULL
450 || !apr_strnatcasecmp(headers_in[counter].key, "If-Match")
451 || !apr_strnatcasecmp(headers_in[counter].key, "If-Modified-Since")
452 || !apr_strnatcasecmp(headers_in[counter].key, "If-Range")
453 || !apr_strnatcasecmp(headers_in[counter].key, "If-Unmodified-Since")
454 || !apr_strnatcasecmp(headers_in[counter].key, "If-None-Match")) {
460 buf = apr_pstrcat(p, headers_in[counter].key, ": ",
461 headers_in[counter].val, CRLF,
463 ap_xlate_proto_to_ascii(buf, strlen(buf));
464 e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
465 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
468 /* If we can send chunks, do so! */
470 const char te_hdr[] = "Transfer-Encoding: chunked" CRLF;
472 buf = apr_pmemdup(p, te_hdr, sizeof(te_hdr)-1);
473 ap_xlate_proto_to_ascii(buf, sizeof(te_hdr)-1);
475 e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
476 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
479 last_header_bucket = APR_BRIGADE_LAST(header_brigade);
482 /* add empty line at the end of the headers */
483 #if APR_CHARSET_EBCDIC
484 e = apr_bucket_immortal_create("\015\012", 2, c->bucket_alloc);
486 e = apr_bucket_immortal_create(CRLF, sizeof(CRLF)-1, c->bucket_alloc);
488 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
489 e = apr_bucket_flush_create(c->bucket_alloc);
490 APR_BRIGADE_INSERT_TAIL(header_brigade, e);
493 status = ap_pass_brigade(origin->output_filters, header_brigade);
495 if (status != APR_SUCCESS) {
496 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
497 "proxy: request failed to %pI (%s)",
498 conn->worker->cp->addr, conn->hostname);
503 /* send the request data, if any. */
506 char chunk_hdr[20]; /* must be here due to transient bucket. */
508 status = ap_get_brigade(r->input_filters, input_brigade,
509 AP_MODE_READBYTES, APR_BLOCK_READ,
512 if (status != APR_SUCCESS) {
516 /* If this brigade contain EOS, either stop or remove it. */
517 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
520 /* As a shortcut, if this brigade is simply an EOS bucket,
521 * don't send anything down the filter chain.
523 if (APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade))) {
527 /* We can't pass this EOS to the output_filters. */
528 e = APR_BRIGADE_LAST(input_brigade);
529 apr_bucket_delete(e);
533 #define ASCII_CRLF "\015\012"
534 #define ASCII_ZERO "\060"
538 apr_brigade_length(input_brigade, 1, &bytes);
540 hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
541 "%" APR_UINT64_T_HEX_FMT CRLF,
542 (apr_uint64_t)bytes);
544 ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
545 e = apr_bucket_transient_create(chunk_hdr, hdr_len,
546 body_brigade->bucket_alloc);
547 APR_BRIGADE_INSERT_HEAD(input_brigade, e);
550 * Append the end-of-chunk CRLF
552 e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
553 APR_BRIGADE_INSERT_TAIL(input_brigade, e);
556 /* The send_chunks case does not need to be setaside, but this
557 * case does because we may have transient buckets that may get
558 * overwritten in the next iteration of the loop.
560 e = APR_BRIGADE_FIRST(input_brigade);
561 while (e != APR_BRIGADE_SENTINEL(input_brigade)) {
562 apr_bucket_setaside(e, p);
563 e = APR_BUCKET_NEXT(e);
567 e = apr_bucket_flush_create(c->bucket_alloc);
568 APR_BRIGADE_INSERT_TAIL(input_brigade, e);
570 APR_BRIGADE_CONCAT(body_brigade, input_brigade);
573 status = ap_pass_brigade(origin->output_filters, body_brigade);
575 if (status != APR_SUCCESS) {
576 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
577 "proxy: pass request data failed to %pI (%s)",
578 conn->worker->cp->addr, conn->hostname);
582 apr_brigade_cleanup(body_brigade);
588 e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
590 ASCII_CRLF, 5, c->bucket_alloc);
591 APR_BRIGADE_INSERT_TAIL(body_brigade, e);
597 apr_brigade_length(body_brigade, 1, &bytes);
600 const char *cl_hdr = "Content-Length", *cl_val;
601 cl_val = apr_off_t_toa(p, bytes);
602 buf = apr_pstrcat(p, cl_hdr, ": ", cl_val, CRLF, NULL);
603 ap_xlate_proto_to_ascii(buf, strlen(buf));
604 e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
605 APR_BUCKET_INSERT_AFTER(last_header_bucket, e);
608 /* A client might really have sent a C-L of 0. Pass it on. */
609 const char *cl_hdr = "Content-Length", *cl_val;
611 cl_val = apr_table_get(r->headers_in, cl_hdr);
612 if (cl_val && cl_val[0] == '0' && cl_val[1] == 0) {
613 buf = apr_pstrcat(p, cl_hdr, ": ", cl_val, CRLF, NULL);
614 ap_xlate_proto_to_ascii(buf, strlen(buf));
615 e = apr_bucket_pool_create(buf, strlen(buf), p,
617 APR_BUCKET_INSERT_AFTER(last_header_bucket, e);
620 status = ap_pass_brigade(origin->output_filters, header_brigade);
621 if (status != APR_SUCCESS) {
622 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
623 "proxy: pass request data failed to %pI (%s)",
624 conn->worker->cp->addr, conn->hostname);
628 apr_brigade_cleanup(header_brigade);
631 status = ap_pass_brigade(origin->output_filters, body_brigade);
633 if (status != APR_SUCCESS) {
634 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
635 "proxy: pass request data failed to %pI (%s)",
636 conn->worker->cp->addr, conn->hostname);
640 apr_brigade_cleanup(body_brigade);
644 static void process_proxy_header(request_rec* r, proxy_server_conf* c,
645 const char* key, const char* value)
647 static const char* date_hdrs[]
648 = { "Date", "Expires", "Last-Modified", NULL } ;
649 static const struct {
651 const char* (*func)(request_rec*, proxy_server_conf*, const char*) ;
652 } transform_hdrs[] = {
653 { "Location", ap_proxy_location_reverse_map } ,
654 { "Content-Location", ap_proxy_location_reverse_map } ,
655 { "URI", ap_proxy_location_reverse_map } ,
656 { "Set-Cookie", proxy_cookie_reverse_map } ,
660 for ( i = 0 ; date_hdrs[i] ; ++i ) {
661 if ( !strcasecmp(date_hdrs[i], key) ) {
662 apr_table_add(r->headers_out, key,
663 ap_proxy_date_canon(r->pool, value)) ;
667 for ( i = 0 ; transform_hdrs[i].name ; ++i ) {
668 if ( !strcasecmp(transform_hdrs[i].name, key) ) {
669 apr_table_add(r->headers_out, key,
670 (*transform_hdrs[i].func)(r, c, value)) ;
674 apr_table_add(r->headers_out, key, value) ;
678 static void ap_proxy_read_headers(request_rec *r, request_rec *rr, char *buffer, int size, conn_rec *c)
682 char field[MAX_STRING_LEN];
684 void *sconf = r->server->module_config;
685 proxy_server_conf *psc;
687 psc = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
689 r->headers_out = apr_table_make(r->pool, 20);
692 * Read header lines until we get the empty separator line, a read error,
693 * the connection closes (EOF), or we timeout.
695 while ((len = ap_getline(buffer, size, rr, 1)) > 0) {
697 if (!(value = strchr(buffer, ':'))) { /* Find the colon separator */
699 /* We may encounter invalid headers, usually from buggy
700 * MS IIS servers, so we need to determine just how to handle
701 * them. We can either ignore them, assume that they mark the
702 * start-of-body (eg: a missing CRLF) or (the default) mark
703 * the headers as totally bogus and return a 500. The sole
704 * exception is an extra "HTTP/1.0 200, OK" line sprinkled
705 * in between the usual MIME headers, which is a favorite
708 /* XXX: The mask check is buggy if we ever see an HTTP/1.10 */
710 if (!apr_date_checkmask(buffer, "HTTP/#.# ###*")) {
711 if (psc->badopt == bad_error) {
712 /* Nope, it wasn't even an extra HTTP header. Give up. */
715 else if (psc->badopt == bad_body) {
716 /* if we've already started loading headers_out, then
717 * return what we've accumulated so far, in the hopes
718 * that they are useful. Otherwise, we completely bail.
720 /* FIXME: We've already scarfed the supposed 1st line of
721 * the body, so the actual content may end up being bogus
722 * as well. If the content is HTML, we may be lucky.
725 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
726 "proxy: Starting body due to bogus non-header in headers "
727 "returned by %s (%s)", r->uri, r->method);
730 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
731 "proxy: No HTTP headers "
732 "returned by %s (%s)", r->uri, r->method);
737 /* this is the psc->badopt == bad_ignore case */
738 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
739 "proxy: Ignoring bogus HTTP header "
740 "returned by %s (%s)", r->uri, r->method);
746 /* XXX: RFC2068 defines only SP and HT as whitespace, this test is
747 * wrong... and so are many others probably.
749 while (apr_isspace(*value))
750 ++value; /* Skip to start of value */
752 /* should strip trailing whitespace as well */
753 for (end = &value[strlen(value)-1]; end > value && apr_isspace(*end); --
757 /* make sure we add so as not to destroy duplicated headers
758 * Modify headers requiring canonicalisation and/or affected
759 * by ProxyPassReverse and family with process_proxy_header
761 process_proxy_header(r, psc, buffer, value) ;
764 /* the header was too long; at the least we should skip extra data */
765 if (len >= size - 1) {
766 while ((len = ap_getline(field, MAX_STRING_LEN, rr, 1))
767 >= MAX_STRING_LEN - 1) {
768 /* soak up the extra data */
770 if (len == 0) /* time to exit the larger loop as well */
778 static int addit_dammit(void *v, const char *key, const char *val)
780 apr_table_addn(v, key, val);
785 apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
786 proxy_conn_rec *backend,
788 proxy_server_conf *conf,
789 char *server_portstr) {
790 conn_rec *c = r->connection;
791 char buffer[HUGE_STRING_LEN];
795 apr_bucket_brigade *bb;
796 int len, backasswards;
797 int interim_response; /* non-zero whilst interim 1xx responses
799 apr_table_t *save_table;
801 bb = apr_brigade_create(p, c->bucket_alloc);
803 /* Get response from the remote server, and pass it up the
807 rp = ap_proxy_make_fake_req(origin, r);
808 /* In case anyone needs to know, this is a fake request that is really a
811 rp->proxyreq = PROXYREQ_RESPONSE;
814 apr_brigade_cleanup(bb);
816 len = ap_getline(buffer, sizeof(buffer), rp, 0);
818 /* handle one potential stray CRLF */
819 len = ap_getline(buffer, sizeof(buffer), rp, 0);
822 ap_proxy_http_cleanup(NULL, r, backend);
823 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
824 "proxy: error reading status line from remote "
825 "server %s", backend->hostname);
826 return ap_proxyerror(r, HTTP_BAD_GATEWAY,
827 "Error reading from remote server");
830 /* Is it an HTTP/1 response?
831 * This is buggy if we ever see an HTTP/1.10
833 if (apr_date_checkmask(buffer, "HTTP/#.# ###*")) {
836 if (2 != sscanf(buffer, "HTTP/%u.%u", &major, &minor)) {
840 /* If not an HTTP/1 message or
841 * if the status line was > 8192 bytes
843 else if ((buffer[5] != '1') || (len >= sizeof(buffer)-1)) {
844 ap_proxy_http_cleanup(NULL, r, backend);
845 return ap_proxyerror(r, HTTP_BAD_GATEWAY,
846 apr_pstrcat(p, "Corrupt status line returned by remote "
847 "server: ", buffer, NULL));
851 keepchar = buffer[12];
853 r->status = atoi(&buffer[9]);
855 if (keepchar != '\0') {
856 buffer[12] = keepchar;
858 /* 2616 requires the space in Status-Line; the origin
859 * server may have sent one but ap_rgetline_core will
860 * have stripped it. */
864 r->status_line = apr_pstrdup(p, &buffer[9]);
867 /* read the headers. */
868 /* N.B. for HTTP/1.0 clients, we have to fold line-wrapped headers*/
869 /* Also, take care with headers with multiple occurences. */
871 /* First, tuck away all already existing cookies */
872 save_table = apr_table_make(r->pool, 2);
873 apr_table_do(addit_dammit, save_table, r->headers_out,
876 /* shove the headers direct into r->headers_out */
877 ap_proxy_read_headers(r, rp, buffer, sizeof(buffer), origin);
879 if (r->headers_out == NULL) {
880 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
881 r->server, "proxy: bad HTTP/%d.%d header "
882 "returned by %s (%s)", major, minor, r->uri,
886 * ap_send_error relies on a headers_out to be present. we
887 * are in a bad position here.. so force everything we send out
888 * to have nothing to do with the incoming packet
890 r->headers_out = apr_table_make(r->pool,1);
891 r->status = HTTP_BAD_GATEWAY;
892 r->status_line = "bad gateway";
898 /* Now, add in the just read cookies */
899 apr_table_do(addit_dammit, save_table, r->headers_out,
902 /* and now load 'em all in */
903 if (!apr_is_empty_table(save_table)) {
904 apr_table_unset(r->headers_out, "Set-Cookie");
905 r->headers_out = apr_table_overlay(r->pool,
910 /* strip connection listed hop-by-hop headers from response */
911 backend->close += ap_proxy_liststr(apr_table_get(r->headers_out,
914 ap_proxy_clear_connection(p, r->headers_out);
915 if ((buf = apr_table_get(r->headers_out, "Content-Type"))) {
916 ap_set_content_type(r, apr_pstrdup(p, buf));
918 ap_proxy_pre_http_request(origin,rp);
921 /* handle Via header in response */
922 if (conf->viaopt != via_off && conf->viaopt != via_block) {
923 const char *server_name = ap_get_server_name(r);
924 /* If USE_CANONICAL_NAME_OFF was configured for the proxy virtual host,
925 * then the server name returned by ap_get_server_name() is the
926 * origin server name (which does make too much sense with Via: headers)
927 * so we use the proxy vhost's name instead.
929 if (server_name == r->hostname)
930 server_name = r->server->server_hostname;
931 /* create a "Via:" response header entry and merge it */
932 apr_table_mergen(r->headers_out, "Via",
933 (conf->viaopt == via_full)
934 ? apr_psprintf(p, "%d.%d %s%s (%s)",
935 HTTP_VERSION_MAJOR(r->proto_num),
936 HTTP_VERSION_MINOR(r->proto_num),
939 AP_SERVER_BASEVERSION)
940 : apr_psprintf(p, "%d.%d %s%s",
941 HTTP_VERSION_MAJOR(r->proto_num),
942 HTTP_VERSION_MINOR(r->proto_num),
948 /* cancel keepalive if HTTP/1.0 or less */
949 if ((major < 1) || (minor < 1)) {
951 origin->keepalive = AP_CONN_CLOSE;
954 /* an http/0.9 response */
957 r->status_line = "200 OK";
961 interim_response = ap_is_HTTP_INFO(r->status);
962 if (interim_response) {
963 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
964 "proxy: HTTP: received interim %d response",
967 /* Moved the fixups of Date headers and those affected by
968 * ProxyPassReverse/etc from here to ap_proxy_read_headers
971 if ((r->status == 401) && (conf->error_override != 0)) {
973 const char *wa = "WWW-Authenticate";
974 if ((buf = apr_table_get(r->headers_out, wa))) {
975 apr_table_set(r->err_headers_out, wa, buf);
977 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
978 "proxy: origin server sent 401 without WWW-Authenticate header");
983 /* Is it an HTTP/0.9 response? If so, send the extra data */
985 apr_ssize_t cntr = len;
987 * At this point in response processing of a 0.9 response,
988 * we don't know yet whether data is binary or not.
989 * mod_charset_lite will get control later on, so it cannot
990 * decide on the conversion of this buffer full of data.
991 * However, chances are that we are not really talking to an
992 * HTTP/0.9 server, but to some different protocol, therefore
993 * the best guess IMHO is to always treat the buffer as "text/x":
995 ap_xlate_proto_to_ascii(buffer, len);
996 e = apr_bucket_heap_create(buffer, cntr, NULL, c->bucket_alloc);
997 APR_BRIGADE_INSERT_TAIL(bb, e);
1000 /* send body - but only if a body is expected */
1001 if ((!r->header_only) && /* not HEAD request */
1002 !interim_response && /* not any 1xx response */
1003 (r->status != HTTP_NO_CONTENT) && /* not 204 */
1004 (r->status != HTTP_RESET_CONTENT) && /* not 205 */
1005 (r->status != HTTP_NOT_MODIFIED)) { /* not 304 */
1007 /* We need to copy the output headers and treat them as input
1008 * headers as well. BUT, we need to do this before we remove
1009 * TE, so that they are preserved accordingly for
1010 * ap_http_filter to know where to end.
1012 rp->headers_in = apr_table_copy(r->pool, r->headers_out);
1014 apr_table_unset(r->headers_out,"Transfer-Encoding");
1016 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1017 "proxy: start body send");
1020 * if we are overriding the errors, we can't put the content
1021 * of the page into the brigade
1023 if (conf->error_override == 0 || ap_is_HTTP_SUCCESS(r->status)) {
1025 /* read the body, pass it to the output filters */
1027 while (ap_get_brigade(rp->input_filters,
1031 conf->io_buffer_size) == APR_SUCCESS) {
1034 apr_off_t readbytes;
1035 apr_brigade_length(bb, 0, &readbytes);
1036 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
1037 r->server, "proxy (PID %d): readbytes: %#x",
1038 getpid(), readbytes);
1042 if (APR_BRIGADE_EMPTY(bb)) {
1043 apr_brigade_cleanup(bb);
1047 /* found the last brigade? */
1048 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
1049 /* if this is the last brigade, cleanup the
1050 * backend connection first to prevent the
1051 * backend server from hanging around waiting
1052 * for a slow client to eat these bytes
1055 /* signal that we must leave */
1059 /* try send what we read */
1060 if (ap_pass_brigade(r->output_filters, bb) != APR_SUCCESS) {
1061 /* Ack! Phbtt! Die! User aborted! */
1062 backend->close = 1; /* this causes socket close below */
1066 /* make sure we always clean up after ourselves */
1067 apr_brigade_cleanup(bb);
1069 /* if we are done, leave */
1070 if (TRUE == finish) {
1075 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1076 "proxy: end body send");
1078 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1079 "proxy: header only");
1081 } while (interim_response);
1083 if (conf->error_override) {
1084 /* the code above this checks for 'OK' which is what the hook expects */
1085 if (ap_is_HTTP_SUCCESS(r->status))
1088 /* clear r->status for override error, otherwise ErrorDocument
1089 * thinks that this is a recursive error, and doesn't find the
1092 int status = r->status;
1093 r->status = HTTP_OK;
1094 /* Discard body, if one is expected */
1095 if ((status != HTTP_NO_CONTENT) && /* not 204 */
1096 (status != HTTP_RESET_CONTENT) && /* not 205 */
1097 (status != HTTP_NOT_MODIFIED)) { /* not 304 */
1098 ap_discard_request_body(rp);
1107 apr_status_t ap_proxy_http_cleanup(const char *scheme, request_rec *r,
1108 proxy_conn_rec *backend)
1110 /* If there are no KeepAlives, or if the connection has been signalled
1111 * to close, close the socket and clean up
1114 /* if the connection is < HTTP/1.1, or Connection: close,
1115 * we close the socket, otherwise we leave it open for KeepAlive support
1117 if (backend->close || (r->proto_num < HTTP_VERSION(1,1))) {
1118 backend->close_on_recycle = 1;
1119 ap_set_module_config(r->connection->conn_config, &proxy_http_module, NULL);
1120 ap_proxy_release_connection(scheme, backend, r->server);
1126 * This handles http:// URLs, and other URLs using a remote proxy over http
1127 * If proxyhost is NULL, then contact the server directly, otherwise
1129 * Note that if a proxy is used, then URLs other than http: can be accessed,
1130 * also, if we have trouble which is clearly specific to the proxy, then
1131 * we return DECLINED so that we can try another proxy. (Or the direct
1134 int ap_proxy_http_handler(request_rec *r, proxy_worker *worker,
1135 proxy_server_conf *conf,
1136 char *url, const char *proxyname,
1137 apr_port_t proxyport)
1140 char server_portstr[32];
1142 const char *proxy_function;
1144 proxy_conn_rec *backend = NULL;
1147 /* Note: Memory pool allocation.
1148 * A downstream keepalive connection is always connected to the existence
1149 * (or not) of an upstream keepalive connection. If this is not done then
1150 * load balancing against multiple backend servers breaks (one backend
1151 * server ends up taking 100% of the load), and the risk is run of
1152 * downstream keepalive connections being kept open unnecessarily. This
1153 * keeps webservers busy and ties up resources.
1155 * As a result, we allocate all sockets out of the upstream connection
1156 * pool, and when we want to reuse a socket, we check first whether the
1157 * connection ID of the current upstream connection is the same as that
1158 * of the connection when the socket was opened.
1160 apr_pool_t *p = r->connection->pool;
1161 conn_rec *c = r->connection;
1162 apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
1164 /* find the scheme */
1165 u = strchr(url, ':');
1166 if (u == NULL || u[1] != '/' || u[2] != '/' || u[3] == '\0')
1169 return HTTP_BAD_REQUEST;
1170 scheme = apr_pstrndup(c->pool, url, u - url);
1171 /* scheme is lowercase */
1172 ap_str_tolower(scheme);
1174 if (strcmp(scheme, "https") == 0) {
1175 if (!ap_proxy_ssl_enable(NULL)) {
1176 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1177 "proxy: HTTPS: declining URL %s"
1178 " (mod_ssl not configured?)", url);
1182 proxy_function = "HTTPS";
1184 else if (!(strcmp(scheme, "http") == 0 || (strcmp(scheme, "ftp") == 0 && proxyname))) {
1185 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1186 "proxy: HTTP: declining URL %s", url);
1187 return DECLINED; /* only interested in HTTP, or FTP via proxy */
1191 proxy_function = "HTTP";
1193 proxy_function = "FTP";
1195 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1196 "proxy: HTTP: serving URL %s", url);
1199 /* only use stored info for top-level pages. Sub requests don't share
1203 backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config,
1204 &proxy_http_module);
1206 /* create space for state information */
1208 if ((status = ap_proxy_acquire_connection(proxy_function, &backend,
1209 worker, r->server)) != OK)
1213 ap_set_module_config(c->conn_config, &proxy_http_module, backend);
1217 backend->is_ssl = is_ssl;
1218 backend->close_on_recycle = 1;
1220 /* Step One: Determine Who To Connect To */
1221 if ((status = ap_proxy_determine_connection(p, r, conf, worker, backend,
1222 c->pool, uri, &url, proxyname,
1223 proxyport, server_portstr,
1224 sizeof(server_portstr))) != OK)
1227 /* Step Two: Make the Connection */
1228 if (ap_proxy_connect_backend(proxy_function, backend, worker, r->server)) {
1229 if (r->proxyreq == PROXYREQ_PROXY)
1230 status = HTTP_NOT_FOUND;
1232 status = HTTP_SERVICE_UNAVAILABLE;
1236 /* Step Three: Create conn_rec */
1237 if (!backend->connection) {
1238 if ((status = ap_proxy_connection_create(proxy_function, backend,
1239 c, r->server)) != OK)
1243 /* Step Four: Send the Request */
1244 if ((status = ap_proxy_http_request(p, r, backend, backend->connection,
1245 conf, uri, url, server_portstr)) != OK)
1248 /* Step Five: Receive the Response */
1249 if ((status = ap_proxy_http_process_response(p, r, backend,
1250 backend->connection,
1251 conf, server_portstr)) != OK)
1254 /* Step Six: Clean Up */
1260 backend->close_on_recycle = 1;
1262 ap_proxy_http_cleanup(proxy_function, r, backend);
1267 static void ap_proxy_http_register_hook(apr_pool_t *p)
1269 proxy_hook_scheme_handler(ap_proxy_http_handler, NULL, NULL, APR_HOOK_FIRST);
1270 proxy_hook_canon_handler(ap_proxy_http_canon, NULL, NULL, APR_HOOK_FIRST);
1273 module AP_MODULE_DECLARE_DATA proxy_http_module = {
1274 STANDARD20_MODULE_STUFF,
1275 NULL, /* create per-directory config structure */
1276 NULL, /* merge per-directory config structures */
1277 NULL, /* create per-server config structure */
1278 NULL, /* merge per-server config structures */
1279 NULL, /* command apr_table_t */
1280 ap_proxy_http_register_hook/* register hooks */