proxy_http.c revision b8a843847aae3d9a1838fb8f1de84cd66212f48a
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/* HTTP routines for Apache proxy */
#include "mod_proxy.h"
typedef struct {
const char *name;
int close;
/*
* Canonicalise http-like URLs.
* scheme is the scheme for the URL
* url is the URL starting with the first '/'
* def_port is the default port for this scheme.
*/
{
const char *err;
const char *scheme;
/* ap_default_port_for_scheme() */
url += 5;
scheme = "http";
}
url += 6;
scheme = "https";
}
else {
return DECLINED;
}
"proxy: HTTP: canonicalising URL %s", url);
/* do syntatic check.
* We break the URL into host, port, path, search
*/
if (err)
return HTTP_BAD_REQUEST;
/* N.B. if this isn't a true proxy request, then the URL _path_
* has already been decoded. True proxy requests have r->uri
* == r->unparsed_uri, and no others have that property.
*/
if (r->uri == r->unparsed_uri) {
*(search++) = '\0';
}
else
/* process path */
return HTTP_BAD_REQUEST;
else
sport[0] = '\0';
return OK;
}
static const char *ap_proxy_location_reverse_map(request_rec *r, proxy_server_conf *conf, const char *url)
{
struct proxy_alias *ent;
char *u;
/* XXX FIXME: Make sure this handled the ambiguous case of the :80
* after the hostname */
return ap_construct_url(r->pool, u, r);
}
}
return url;
}
/* Clear all connection-based headers from the incoming headers table */
{
const char *name;
if (!next)
return;
while (*next) {
++next;
}
*next = '\0';
++next;
}
}
}
static
conn_rec *c,
char **url,
const char *proxyname,
char *server_portstr,
int server_portstr_size) {
int server_port;
/*
* Break up the URL to determine the host to connect to
*/
/* we break the URL into host, port, uri */
return ap_proxyerror(r, HTTP_BAD_REQUEST,
NULL));
}
}
/* do a DNS lookup for the destination host */
/* see memory note above */
/* allocate these out of the connection pool - the check on
* r->connection->id makes sure that this string does not get accessed
* past the connection lifetime */
/* are we connecting directly, or via a proxy? */
if (proxyname) {
/* see memory note above */
} else {
}
if (err != APR_SUCCESS) {
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
apr_pstrcat(p, "DNS lookup failure for: ",
}
/* Get the server port for the Via headers */
{
if (ap_is_default_port(server_port, r)) {
} else {
}
}
/* check if ProxyBlock directive on this host */
return ap_proxyerror(r, HTTP_FORBIDDEN,
"Connect to remote machine blocked");
}
return OK;
}
static
const char *proxyname) {
/* We have determined who to connect to. Now make the connection, supporting
* a KeepAlive connection.
*/
/* get all the possible IP addresses for the destname and loop through them
* until we get a successful connection
*/
/* if a keepalive socket is already open, check whether it must stay
* open, or whether it should be closed and a new socket created.
*/
/* see memory note above */
if (backend->connection) {
"proxy: keepalive address match (keep original socket)");
} else {
"proxy: keepalive address mismatch / connection has"
" changed (close old socket (%s/%s, %d/%d))",
}
}
/* get a socket - either a keepalive one, or a new one */
new = 1;
char test_buffer[1];
/* use previous keepalive socket */
new = 0;
/* save timeout */
/* set no timeout */
/* put back old timeout */
if ( APR_STATUS_IS_EOF(socket_status) ) {
"proxy: HTTP: previous connection is closed");
new = 1;
}
}
if (new) {
/* create a new socket */
/*
* At this point we have a list of one or more IP addresses of
* the machine to connect to. If configured, reorder this
* list so that the "best candidate" is first try. "best
* candidate" could mean the least loaded server, the fastest
* responding server, whatever.
*
* For now we do nothing, ie we get DNS round robin.
* XXX FIXME
*/
/* handle a permanent error on the connect */
if (failed) {
if (proxyname) {
return DECLINED;
} else {
return HTTP_BAD_GATEWAY;
}
}
"proxy: socket is connected");
/* the socket is now open, create a new backend server connection */
r->connection->id,
if (!origin) {
/* the peer reset the connection already; ap_run_create_connection()
* closed the socket
*/
r->server, "proxy: an error occurred creating a "
return HTTP_INTERNAL_SERVER_ERROR;
}
r->server, "proxy: failed to enable ssl support "
return HTTP_INTERNAL_SERVER_ERROR;
}
}
else {
}
"proxy: connection complete to %pI (%s)",
/* set up the connection filters */
}
return OK;
}
static
char *server_portstr) {
conn_rec *c = r->connection;
char buffer[HUGE_STRING_LEN];
char *buf;
apr_bucket *e;
const apr_array_header_t *headers_in_array;
const apr_table_entry_t *headers_in;
int counter;
/*
* Send the HTTP/1.1 request to the remote server
*/
/* strip connection listed hop-by-hop headers from the request */
/* even though in theory a connection: close coming from the client
* should not affect the connection to the server, it's unlikely
* we cancel server keepalive if the client does.
*/
"Connection"), "close");
/* sub-requests never use keepalives */
if (r->main) {
}
ap_proxy_clear_connection(p, r->headers_in);
}
if ( conf->preserve_host == 0 ) {
NULL);
} else {
}
}
else {
/* don't want to use r->hostname, as the incoming header might have a
* port attached
*/
if (!hostname) {
"proxy: no HTTP 0.9 request (with no host line) "
"on incoming request and preserve host set "
"forcing hostname to be %s for uri %s",
r->uri );
}
}
/* handle Via */
/* Block all outgoing Via: headers */
/* Create a "Via:" request header entry and merge it */
? apr_psprintf(p, "%d.%d %s%s (%s)",
: apr_psprintf(p, "%d.%d %s%s",
);
}
/* X-Forwarded-*: handling
*
* XXX Privacy Note:
* -----------------
*
* These request headers are only really useful when the mod_proxy
* is used in a reverse proxy configuration, so that useful info
* about the client can be passed through the reverse proxy and on
* to the backend server, which may require the information to
* function properly.
*
* In a forward proxy situation, these options are a potential
* privacy violation, as information about clients behind the proxy
* are revealed to arbitrary servers out there on the internet.
*
* The HTTP/1.1 Via: header is designed for passing client
* information through proxies to a server, and should be used in
* a forward proxy configuation instead of X-Forwarded-*. See the
* ProxyVia option for details.
*/
if (PROXYREQ_REVERSE == r->proxyreq) {
const char *buf;
/* Add X-Forwarded-For: so that the upstream has a chance to
* determine, where the original request came from.
*/
r->connection->remote_ip);
/* Add X-Forwarded-Host: so that upstream knows what the
* original request hostname was.
*/
}
/* Add X-Forwarded-Server: so that upstream knows what the
* name of this proxy server is (if there are more than one)
* XXX: This duplicates Via: - do we strictly need it?
*/
r->server->server_hostname);
}
/* send request headers */
proxy_run_fixups(r);
/* Clear out hop-by-hop request headers not to send
* RFC2616 13.5.1 says we should strip these headers
*/
/* Already sent */
/* XXX: @@@ FIXME: "Proxy-Authorization" should *only* be
* suppressed if THIS server requested the authentication,
* not when a frontend proxy requested it!
*
* The solution to this problem is probably to strip out
* the Proxy-Authorisation header in the authorisation
* code itself, not here. This saves us having to signal
* somehow whether this request was authenticated or not.
*/
continue;
}
if (r->main) {
continue;
}
}
NULL);
}
/* add empty line at the end of the headers */
#else
#endif
e = apr_bucket_flush_create(c->bucket_alloc);
if (status != APR_SUCCESS) {
"proxy: request failed to %pI (%s)",
return status;
}
/* send the request data, if any. */
if (ap_should_client_block(r)) {
e = apr_bucket_flush_create(c->bucket_alloc);
if (status != APR_SUCCESS) {
"proxy: pass request data failed to %pI (%s)",
return status;
}
}
}
return APR_SUCCESS;
}
static
char *server_portstr) {
conn_rec *c = r->connection;
char buffer[HUGE_STRING_LEN];
apr_bucket *e;
int len, backasswards;
* loop over response parsing logic
* in the case that the origin told us
* to HTTP_CONTINUE
*/
/* Get response from the remote server, and pass it up the
* filter chain
*/
while (received_continue) {
if (len == 0) {
/* handle one potential stray CRLF */
}
if (len <= 0) {
"proxy: error reading status line from remote "
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
"Error reading from remote server");
}
/* Is it an HTTP/1 response?
* This is buggy if we ever see an HTTP/1.10
*/
major = 1;
minor = 1;
}
/* If not an HTTP/1 message or
* if the status line was > 8192 bytes
*/
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
apr_pstrcat(p, "Corrupt status line returned by remote "
}
backasswards = 0;
/* read the headers. */
/* N.B. for HTTP/1.0 clients, we have to fold line-wrapped headers*/
/* Also, take care with headers with multiple occurences. */
if (r->headers_out == NULL) {
r->server, "proxy: bad HTTP/%d.%d header "
r->method);
/*
* ap_send_error relies on a headers_out to be present. we
* are in a bad position here.. so force everything we send out
* to have nothing to do with the incoming packet
*/
r->status = HTTP_BAD_GATEWAY;
r->status_line = "bad gateway";
return r->status;
} else {
/* strip connection listed hop-by-hop headers from response */
const char *buf;
"Connection"),
"close");
ap_proxy_clear_connection(p, r->headers_out);
}
}
/* handle Via header in response */
/* create a "Via:" response header entry and merge it */
? apr_psprintf(p, "%d.%d %s%s (%s)",
: apr_psprintf(p, "%d.%d %s%s",
);
}
/* cancel keepalive if HTTP/1.0 or less */
}
} else {
/* an http/0.9 response */
backasswards = 1;
r->status = 200;
r->status_line = "200 OK";
}
if ( r->status != HTTP_CONTINUE ) {
received_continue = 0;
} else {
"proxy: HTTP: received 100 CONTINUE");
}
/* we must accept 3 kinds of date, but generate only 1 kind of date */
{
const char *buf;
ap_proxy_date_canon(p, buf));
}
ap_proxy_date_canon(p, buf));
}
ap_proxy_date_canon(p, buf));
}
}
/* munge the Location and URI response headers according to
* ProxyPassReverse
*/
{
const char *buf;
}
}
}
}
r->sent_bodyct = 1;
/* Is it an HTTP/0.9 response? If so, send the extra data */
if (backasswards) {
}
/* send body - but only if a body is expected */
if ((!r->header_only) && /* not HEAD request */
/* We need to copy the output headers and treat them as input
* headers as well. BUT, we need to do this before we remove
* TE and C-L, so that they are preserved accordingly for
* ap_http_filter to know where to end.
*/
/* In order for ap_set_keepalive to work properly, we can NOT
* have any length information stored in the output headers.
*/
"proxy: start body send");
/*
* if we are overriding the errors, we can't put the content
* of the page into the brigade
*/
/* read the body, pass it to the output filters */
bb,
#if DEBUGGING
{
r->server, "proxy (PID %d): readbytes: %#x",
}
#endif
/* sanity check */
if (APR_BRIGADE_EMPTY(bb)) {
break;
}
/* found the last brigade? */
/* if this is the last brigade, cleanup the
* backend connection first to prevent the
* backend server from hanging around waiting
* for a slow client to eat these bytes
*/
/* signal that we must leave */
}
/* try send what we read */
/* Ack! Phbtt! Die! User aborted! */
}
/* make sure we always clean up after ourselves */
/* if we are done, leave */
break;
}
}
}
"proxy: end body send");
} else {
"proxy: header only");
}
}
if ( conf->error_override ) {
/* the code above this checks for 'OK' which is what the hook expects */
return OK;
else {
/* clear r->status for override error, otherwise ErrorDocument
* thinks that this is a recursive error, and doesn't find the
* custom error page
*/
return status;
}
} else
return OK;
}
static
/* If there are no KeepAlives, or if the connection has been signalled
* to close, close the socket and clean up
*/
/* if the connection is < HTTP/1.1, or Connection: close,
* we close the socket, otherwise we leave it open for KeepAlive support
*/
}
}
return OK;
}
/*
* This handles http:// URLs, and other URLs using a remote proxy over http
* If proxyhost is NULL, then contact the server directly, otherwise
* go via the proxy.
* Note that if a proxy is used, then URLs other than http: can be accessed,
* also, if we have trouble which is clearly specific to the proxy, then
* we return DECLINED so that we can try another proxy. (Or the direct
* route.)
*/
{
int status;
char server_portstr[32];
int is_ssl = 0;
/* Note: Memory pool allocation.
* A downstream keepalive connection is always connected to the existence
* (or not) of an upstream keepalive connection. If this is not done then
* load balancing against multiple backend servers breaks (one backend
* server ends up taking 100% of the load), and the risk is run of
* downstream keepalive connections being kept open unnecessarily. This
* keeps webservers busy and ties up resources.
*
* As a result, we allocate all sockets out of the upstream connection
* pool, and when we want to reuse a socket, we check first whether the
* connection ID of the current upstream connection is the same as that
* of the connection when the socket was opened.
*/
conn_rec *c = r->connection;
sizeof(*p_conn));
/* is it for us? */
if (!ap_proxy_ssl_enable(NULL)) {
"proxy: HTTPS: declining URL %s"
" (mod_ssl not configured?)", url);
return DECLINED;
}
is_ssl = 1;
}
"proxy: HTTP: declining URL %s", url);
return DECLINED; /* only interested in HTTP */
}
"proxy: HTTP: serving URL %s", url);
/* only use stored info for top-level pages. Sub requests don't share
* in keepalives
*/
if (!r->main) {
}
/* create space for state information */
if (!backend) {
if (!r->main) {
}
}
/* Step One: Determine Who To Connect To */
sizeof(server_portstr));
return status;
}
/* Step Two: Make the Connection */
return status;
}
/* Step Three: Send the Request */
return status;
}
/* Step Four: Receive the Response */
bb, server_portstr);
/* clean up even if there is an error */
return status;
}
/* Step Five: Clean Up */
return status;
}
return OK;
}
static void ap_proxy_http_register_hook(apr_pool_t *p)
{
}
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
ap_proxy_http_register_hook/* register hooks */
};