proxy_http.c revision e351a4349a3dcc2e8d9c27bcdf72414bdde0942f
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* ====================================================================
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The Apache Software License, Version 1.1
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Copyright (c) 2000 The Apache Software Foundation. All rights
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * reserved.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Redistribution and use in source and binary forms, with or without
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * modification, are permitted provided that the following conditions
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * are met:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 1. Redistributions of source code must retain the above copyright
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * notice, this list of conditions and the following disclaimer.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * 2. Redistributions in binary form must reproduce the above copyright
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * notice, this list of conditions and the following disclaimer in
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * the documentation and/or other materials provided with the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * distribution.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 3. The end-user documentation included with the redistribution,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * if any, must include the following acknowledgment:
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * "This product includes software developed by the
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Apache Software Foundation (http://www.apache.org/)."
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Alternately, this acknowledgment may appear in the software itself,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * if and wherever such third-party acknowledgments normally appear.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 4. The names "Apache" and "Apache Software Foundation" must
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * not be used to endorse or promote products derived from this
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * software without prior written permission. For written
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * permission, please contact apache@apache.org.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * 5. Products derived from this software may not be called "Apache",
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * nor may "Apache" appear in their name, without prior written
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * permission of the Apache Software Foundation.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * SUCH DAMAGE.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * ====================================================================
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * This software consists of voluntary contributions made by many
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * individuals on behalf of the Apache Software Foundation. For more
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * information on the Apache Software Foundation, please see
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * Portions of this software are based upon public domain software
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * originally written at the National Center for Supercomputing Applications,
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * University of Illinois, Urbana-Champaign.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* HTTP routines for Apache proxy */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Canonicalise http-like URLs.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * scheme is the scheme for the URL
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * url is the URL starting with the first '/'
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * def_port is the default port for this scheme.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseint ap_proxy_http_canon(request_rec *r, char *url, const char *scheme, int def_port)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *err;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* do syntatic check.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * We break the URL into host, port, path, search
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* now parse path/search args, according to rfc1738 */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* N.B. if this isn't a true proxy request, then the URL _path_
05413593151a238718198cc04ca849b2426be106rse * has already been decoded. True proxy requests have r->uri
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * == r->unparsed_uri, and no others have that property.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* process path */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, r->proxyreq);
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport, "/",
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron path, (search) ? "?" : "", (search) ? search : "", NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic const char *proxy_location_reverse_map(request_rec *r, const char *url)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Clear all connection-based headers from the incoming headers table */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic void clear_connection(apr_pool_t *p, apr_table_t *headers)
87a1c79b7b37702a254920ca5214fb282a4fb085dougm const char *name;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *next = apr_pstrdup(p, apr_table_get(headers, "Connection"));
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse while (*next) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse while (*next && (apr_isspace(*next) || (*next == ','))) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * This handles http:// URLs, and other URLs using a remote proxy over http
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse * If proxyhost is NULL, then contact the server directly, otherwise
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * go via the proxy.
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse * Note that if a proxy is used, then URLs other than http: can be accessed,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * also, if we have trouble which is clearly specific to the proxy, then
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * we return DECLINED so that we can try another proxy. (Or the direct
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseint ap_proxy_http_handler(request_rec *r, ap_cache_el *c, char *url,
87a1c79b7b37702a254920ca5214fb282a4fb085dougm const char *strp;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse struct noproxy_entry *npent = (struct noproxy_entry *) conf->noproxies->elts;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse struct nocache_entry *ncent = (struct nocache_entry *) conf->nocaches->elts;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse/* We break the URL into host, port, path-search */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* check if ProxyBlock directive on this host */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "Connect to remote machine blocked");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((apr_create_tcp_socket(&sock, r->pool)) != APR_SUCCESS) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "proxy: error creating socket");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (conf->recv_buffer_size > 0 && apr_setsocketopt(sock, APR_SO_RCVBUF,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse err = ap_proxy_doconnect(sock, (char *)proxyhost, proxyport, r);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse err = ap_proxy_doconnect(sock, (char *)desthost, destport, r);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(r->pool,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "Could not connect to remote machine: ",
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse origin = ap_new_apr_connection(r->pool, r->server, sock, 0);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse clear_connection(r->pool, r->headers_in); /* Strip connection-based headers */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse buf = apr_pstrcat(r->pool, r->method, " ", proxyhost ? url : urlptr,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (destportstr != NULL && destport != DEFAULT_HTTP_PORT) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse buf = apr_pstrcat(r->pool, "Host: ", desthost, ":", destportstr, CRLF, NULL);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse buf = apr_pstrcat(r->pool, "Host: ", desthost, CRLF, NULL);
184f5da95d14895f7f33c90b8b8f70653afb0d92wrowe /* Block all outgoing Via: headers */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Create a "Via:" request header entry and merge it */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Generate outgoing Via: header with/without server comment: */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Clear out headers not to send */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm || !strcasecmp(reqhdrs[i].key, "Host") /* Already sent */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* XXX: @@@ FIXME: "Proxy-Authorization" should *only* be
a0e0d20b666cfc453ac76506079eb50e03997eefdougm * suppressed if THIS server requested the authentication,
a0e0d20b666cfc453ac76506079eb50e03997eefdougm * not when a frontend proxy requested it!
a0e0d20b666cfc453ac76506079eb50e03997eefdougm || !strcasecmp(reqhdrs[i].key, "Proxy-Authorization"))
a0e0d20b666cfc453ac76506079eb50e03997eefdougm buf = apr_pstrcat(r->pool, reqhdrs[i].key, ": ", reqhdrs[i].val, CRLF, NULL);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm e = ap_bucket_create_pool(CRLF, strlen(CRLF), r->pool);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm/* send the request data, if any. */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm while ((i = ap_get_client_block(r, buffer, sizeof buffer)) > 0) {
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Flush the data to the origin server */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Tell http_filter to grab the data one line at a time. */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_get_brigade(origin->input_filters, bb, AP_MODE_BLOCKING);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_bucket_read(AP_BRIGADE_FIRST(bb), (const char **)&buffer2, &len, AP_BLOCK_READ);
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm "ap_get_brigade() - proxy receive - Error reading from remote server %s (length %d)",
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm "Error reading from remote server");
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm } else if (len == 0) {
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm "Document contains no data");
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm/* Is it an HTTP/1 response? This is buggy if we ever see an HTTP/1.10 */
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm if (2 != sscanf(buffer2, "HTTP/%u.%u", &major, &minor)) {
0ccb8cf8cbc291aca5e8da62cde6cc6948e2081eben/* If not an HTTP/1 message or if the status line was > 8192 bytes */
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm/* read the headers. */
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm/* N.B. for HTTP/1.0 clients, we have to fold line-wrapped headers */
dd9940ba9b4d9c09f034b910d1569db4a5111c75dougm/* Also, take care with headers with multiple occurences. */
e62985c7a1b46a5036a247f35bddac1308985758dougm resp_hdrs = ap_proxy_read_headers(r, buffer, HUGE_STRING_LEN, origin);
e62985c7a1b46a5036a247f35bddac1308985758dougm ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r->server,
8464a9c46b967001e38fe3c8afff51a649e9de51dougm "proxy: Bad HTTP/%d.%d header returned by %s (%s)",
8464a9c46b967001e38fe3c8afff51a649e9de51dougm clear_connection(p, resp_hdrs); /* Strip Connection hdrs */
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm if (conf->viaopt != via_off && conf->viaopt != via_block) {
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm /* Create a "Via:" response header entry and merge it */
e18e68b42830409bf48de0df9eed3fe363664aa7aaron ap_cache_el_header_add(c, "Via", (conf->viaopt == via_full)
8464a9c46b967001e38fe3c8afff51a649e9de51dougm ap_get_server_name(r), portstr, AP_SERVER_BASEVERSION)
e18e68b42830409bf48de0df9eed3fe363664aa7aaron/* an http/0.9 response */
8464a9c46b967001e38fe3c8afff51a649e9de51dougm * HTTP/1.0 requires us to accept 3 types of dates, but only generate
e18e68b42830409bf48de0df9eed3fe363664aa7aaron if (ap_cache_el_header(c, "Date", &datestr) == APR_SUCCESS)
e18e68b42830409bf48de0df9eed3fe363664aa7aaron ap_cache_el_header_set(c, "Date", ap_proxy_date_canon(p, datestr));
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm if (ap_cache_el_header(c, "Last-Modified", &datestr) == APR_SUCCESS)
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm ap_cache_el_header_set(c, "Last-Modified", ap_proxy_date_canon(p, datestr));
e62985c7a1b46a5036a247f35bddac1308985758dougm if (ap_cache_el_header(c, "Expires", &datestr) == APR_SUCCESS)
9e530d1e49062250c345bfd45810e145b4f435eddougm ap_cache_el_header_set(c, "Expires", ap_proxy_date_canon(p, datestr));
8464a9c46b967001e38fe3c8afff51a649e9de51dougm if (ap_cache_el_header(c, "Location", &datestr) == APR_SUCCESS)
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm ap_cache_el_header_set(c, "Location", proxy_location_reverse_map(r, datestr));
8464a9c46b967001e38fe3c8afff51a649e9de51dougm if (ap_cache_el_header(c, "URI", &datestr) == APR_SUCCESS)
8464a9c46b967001e38fe3c8afff51a649e9de51dougm ap_cache_el_header_set(c, "URI", proxy_location_reverse_map(r, datestr));
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm/* check if NoCache directive on this host */
6a26d195dfba3a91f8352cabd4547afa77675bb1aaron if (ap_cache_el_header(c, "Content-Length", &clen) == APR_SUCCESS)
ap_proxy_cache_error(&c);
if (!r->assbackwards)
ap_proxy_cache_error(&c);
if (!r->assbackwards)
if (backasswards) {
ap_proxy_cache_error(&c);
if (!r->header_only) {
if(c) ap_proxy_cache_update(c);
return OK;