proxy_http.c revision f22051090565b48d837694b9bd5b742c4c65caaf
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 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 */
#define CORE_PRIVATE
#include "mod_proxy.h"
#include "apr_strings.h"
#include "apr_buckets.h"
#include "util_filter.h"
#include "ap_config.h"
#include "http_log.h"
#include "http_main.h"
#include "http_core.h"
#include "http_connection.h"
#include "util_date.h"
/*
* 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;
int port;
/* 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;
}
{
void *sconf;
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;
}
}
}
/*
* 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.)
*/
{
const char *connectname;
int connectport = 0;
char server_portstr[32];
char buffer[HUGE_STRING_LEN];
char *response;
char *buf;
apr_bucket *e;
/*
* Step One: Determine Who To Connect To
*
* 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,
}
/* do a DNS lookup for the destination host */
/* are we connecting directly, or via a proxy? */
if (proxyname) {
}
else {
}
/* Get the server port for the Via headers */
{
i = ap_get_server_port(r);
if (ap_is_default_port(i,r)) {
} else {
}
}
/* check if ProxyBlock directive on this host */
/* XXX FIXME: conf->noproxies->elts is part of an opaque structure */
return ap_proxyerror(r, HTTP_FORBIDDEN,
"Connect to remote machine blocked (by name)");
}
while (conf_addr) {
while (uri_addr) {
char *conf_ip;
char *uri_ip;
/* ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
"Testing %s and %s", conf_ip, uri_ip); */
return ap_proxyerror(r, HTTP_FORBIDDEN,
"Connect to remote machine blocked (by IP address)");
}
}
}
}
/*
* Step Two: Make the Connection
*
* 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 (APR_SUCCESS != err) {
"DNS lookup failure for: ",
connectname, NULL));
}
/* if a KeepAlive socket is already open, check whether it must stay
* open, or whether it should be closed and a new socket created.
*/
struct apr_sockaddr_t *remote_addr;
(port == connectport) &&
"proxy: keepalive address match (keep original socket)");
}
else {
"proxy: keepalive address mismatch (close old socket (%s/%s, %d/%d))", connectname, conf->origin->remote_addr->hostname, connectport, port);
}
}
/* get a socket - either a keepalive one, or a new one */
new = 1;
/* use previous keepalive socket */
origin->keepalives++;
new = 0;
/* XXX FIXME: If the socket has since closed, change new to 1 so
* a new socket is opened */
}
if (new) {
/* create a new socket */
"proxy: error creating socket");
return HTTP_INTERNAL_SERVER_ERROR;
}
conf->recv_buffer_size)) {
"setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
}
#endif
/*
* 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
*/
/* try each IP address until we connect successfully */
failed = 1;
while (connect_addr) {
/* make the connection out of the socket */
/* if an error occurred, loop round and try again */
if (err != APR_SUCCESS) {
continue;
}
/* the socket is now open, create a new connection */
if (!origin) {
/* the peer reset the connection already; ap_new_connection()
* closed the socket */
continue;
}
/* we use keepalives unless later specified */
/* if we get here, all is well */
failed = 0;
break;
}
/* handle a permanent error from the above loop */
if (failed) {
if (proxyname) {
return DECLINED;
}
else {
return HTTP_BAD_GATEWAY;
}
}
}
/*
* Step Three: Send the Request
*
* Send the HTTP/1.1 request to the remote server
*/
/* set up the connection filters */
/* 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.
*/
ap_proxy_clear_connection(p, r->headers_in);
if (close) {
}
}
else {
}
/* 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.
*/
/* 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 */
for (i = 0; i < headers_in_array->nelts; i++) {
/* Clear out hop-by-hop request headers not to send
* RFC2616 13.5.1 says we should strip these headers
*/
/* 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;
}
/* add empty line at the end of the headers */
e = apr_bucket_flush_create();
/* send the request data, if any. */
if (ap_should_client_block(r)) {
e = apr_bucket_pool_create(buffer, i, p);
}
}
/* Flush the data to the origin server */
e = apr_bucket_flush_create();
/*
* Step Four: Receive the Response
*
* Get response from the remote server, and pass it up the
* filter chain
*/
bb = apr_brigade_create(p);
/* Tell http_filter to grab the data one line at a time. */
e = APR_BRIGADE_FIRST(bb);
/* XXX FIXME: a bug exists where apr_bucket_read() is returning
* len=0 when the response line is expected... we try it up to
* 5 times - this has not fixed the problem though.
*/
i = 5;
len = 0;
while (!len && i--) {
}
if (len == -1) {
"proxy: error reading from remote server %s (length %d) using ap_get_brigade()",
connectname, len);
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
"Error reading from remote server");
} else if (len == 0) {
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
"No response data from 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,
}
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) {
"proxy: bad HTTP/%d.%d header returned by %s (%s)",
close += 1;
}
else
{
/* strip connection listed hop-by-hop headers from response */
const char *buf;
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 */
close += 1;
}
}
else {
/* an http/0.9 response */
backasswards = 1;
r->status = 200;
r->status_line = "200 OK";
close += 1;
}
/* 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) {
/* FIXME: what is buffer used for here? It is of limited size */
}
/* XXX FIXME - what about 304 et al responses that have no body and no content-length? */
/* send body */
if (!r->header_only) {
const char *buf;
/* if chunked - insert DECHUNK filter */
}
}
/* if content length - set the length to read */
}
/* no chunked / no length therefore read till EOF */
else {
}
/* if keepalive cancelled, read to EOF */
if (close) {
}
"proxy: start body send");
/* read the body, pass it to the output filters */
break;
}
bb = apr_brigade_create(p);
}
"proxy: end body send");
}
else {
"proxy: header only");
}
/*
* Step Five: Clean Up
*
* 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
*/
if (close) {
}
else {
}
return OK;
}