proxy_http.c revision e351a4349a3dcc2e8d9c27bcdf72414bdde0942f
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* ====================================================================
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * The Apache Software License, Version 1.1
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * Copyright (c) 2000 The Apache Software Foundation. All rights
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * reserved.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
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 *
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * 1. Redistributions of source code must retain the above copyright
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse * notice, this list of conditions and the following disclaimer.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *
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 *
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 *
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 *
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 *
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 *
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 * <http://www.apache.org/>.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse *
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 */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* HTTP routines for Apache proxy */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#define CORE_PRIVATE
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "mod_proxy.h"
e18e68b42830409bf48de0df9eed3fe363664aa7aaron#include "http_log.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "http_main.h"
8464a9c46b967001e38fe3c8afff51a649e9de51dougm#include "http_core.h"
579fd9e90990eee18b5e504eb4c0d2ce18f76208aaron#include "http_connection.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#include "util_date.h"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/*
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.
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseint ap_proxy_http_canon(request_rec *r, char *url, const char *scheme, int def_port)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *host, *path, *search, sport[7];
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *err;
05413593151a238718198cc04ca849b2426be106rse int port;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* do syntatic check.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * We break the URL into host, port, path, search
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse port = def_port;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (err)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_BAD_REQUEST;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
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 */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (r->uri == r->unparsed_uri) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse search = strchr(url, '?');
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (search != NULL)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *(search++) = '\0';
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse search = r->args;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* process path */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, r->proxyreq);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (path == NULL)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_BAD_REQUEST;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (port != def_port)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_snprintf(sport, sizeof(sport), ":%d", port);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse sport[0] = '\0';
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport, "/",
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron path, (search) ? "?" : "", (search) ? search : "", NULL);
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron return OK;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic const char *proxy_location_reverse_map(request_rec *r, const char *url)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse{
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse void *sconf;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse proxy_server_conf *conf;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse struct proxy_alias *ent;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int i, l1, l2;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *u;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse sconf = r->server->module_config;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse l1 = strlen(url);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ent = (struct proxy_alias *)conf->raliases->elts;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (i = 0; i < conf->raliases->nelts; i++) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse l2 = strlen(ent[i].real);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (l1 >= l2 && strncmp(ent[i].real, url, l2) == 0) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse u = apr_pstrcat(r->pool, ent[i].fake, &url[l2], NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return ap_construct_url(r->pool, u, r);
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron return url;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse}
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* Clear all connection-based headers from the incoming headers table */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsestatic void clear_connection(apr_pool_t *p, apr_table_t *headers)
87a1c79b7b37702a254920ca5214fb282a4fb085dougm{
87a1c79b7b37702a254920ca5214fb282a4fb085dougm const char *name;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *next = apr_pstrdup(p, apr_table_get(headers, "Connection"));
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_table_unset(headers, "Proxy-Connection");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (!next)
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse return;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse while (*next) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse name = next;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse while (*next && !apr_isspace(*next) && (*next != ','))
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse ++next;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse while (*next && (apr_isspace(*next) || (*next == ','))) {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse *next = '\0';
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse ++next;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse }
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_table_unset(headers, name);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse }
87a1c79b7b37702a254920ca5214fb282a4fb085dougm apr_table_unset(headers, "Connection");
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse}
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/*
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
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse * route.)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseint ap_proxy_http_handler(request_rec *r, ap_cache_el *c, char *url,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *proxyhost, int proxyport)
87a1c79b7b37702a254920ca5214fb282a4fb085dougm{
87a1c79b7b37702a254920ca5214fb282a4fb085dougm const char *strp;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *strp2;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *desthost;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse apr_socket_t *sock;
87a1c79b7b37702a254920ca5214fb282a4fb085dougm int i, len, backasswards, content_length = -1;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_status_t err;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_array_header_t *reqhdrs_arr;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_table_t *resp_hdrs = NULL;
87a1c79b7b37702a254920ca5214fb282a4fb085dougm apr_table_entry_t *reqhdrs;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse struct sockaddr_in server;
87a1c79b7b37702a254920ca5214fb282a4fb085dougm struct in_addr destaddr;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse char buffer[HUGE_STRING_LEN];
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse char *buffer2;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char portstr[32];
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_pool_t *p = r->pool;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int destport = 0;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *destportstr = NULL;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse const char *urlptr = NULL;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *datestr, *clen;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_ssize_t cntr;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_file_t *cachefp = NULL;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse char *buf;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse conn_rec *origin;
03181bdde77be8e10ed297a02db5d8f98ecb703ewrowe ap_bucket *e;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_bucket_brigade *bb = ap_brigade_create(r->pool);
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse void *sconf = r->server->module_config;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse proxy_server_conf *conf =
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;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse int nocache = 0;
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse memset(&server, '\0', sizeof(server));
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse server.sin_family = AF_INET;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse/* We break the URL into host, port, path-search */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse urlptr = strstr(url, "://");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (urlptr == NULL)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_BAD_REQUEST;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse urlptr += 3;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse destport = DEFAULT_HTTP_PORT;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse strp = ap_strchr_c(urlptr, '/');
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (strp == NULL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse desthost = apr_pstrdup(p, urlptr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse urlptr = "/";
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else {
7f683bb300df767164724ebc664f339ac396b434dougm char *q = apr_palloc(p, strp - urlptr + 1);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse memcpy(q, urlptr, strp - urlptr);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse q[strp - urlptr] = '\0';
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse urlptr = strp;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse desthost = q;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse strp2 = ap_strchr(desthost, ':');
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (strp2 != NULL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse *(strp2++) = '\0';
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (apr_isdigit(*strp2)) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse destport = atoi(strp2);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse destportstr = strp2;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse/* check if ProxyBlock directive on this host */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse destaddr.s_addr = apr_inet_addr(desthost);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse for (i = 0; i < conf->noproxies->nelts; i++) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((npent[i].name != NULL
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse && ap_strstr_c(desthost, npent[i].name) != NULL)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse || destaddr.s_addr == npent[i].addr.s_addr
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse || npent[i].name[0] == '*')
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return ap_proxyerror(r, HTTP_FORBIDDEN,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "Connect to remote machine blocked");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if ((apr_create_tcp_socket(&sock, r->pool)) != APR_SUCCESS) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "proxy: error creating socket");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return HTTP_INTERNAL_SERVER_ERROR;
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron#if !defined(TPF) && !defined(BEOS)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (conf->recv_buffer_size > 0 && apr_setsocketopt(sock, APR_SO_RCVBUF,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse conf->recv_buffer_size)) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse#endif
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (proxyhost != NULL) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse err = ap_proxy_doconnect(sock, (char *)proxyhost, proxyport, r);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse err = ap_proxy_doconnect(sock, (char *)desthost, destport, r);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (err != APR_SUCCESS) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (proxyhost != NULL)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return DECLINED; /* try again another way */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(r->pool,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse "Could not connect to remote machine: ",
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse desthost, NULL));
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse origin = ap_new_apr_connection(r->pool, r->server, sock, 0);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ap_add_output_filter("CORE", NULL, NULL, origin);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse clear_connection(r->pool, r->headers_in); /* Strip connection-based headers */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse buf = apr_pstrcat(r->pool, r->method, " ", proxyhost ? url : urlptr,
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse " HTTP/1.0" CRLF, NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse e = ap_bucket_create_pool(buf, strlen(buf), r->pool);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse AP_BRIGADE_INSERT_TAIL(bb, e);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (destportstr != NULL && destport != DEFAULT_HTTP_PORT) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse buf = apr_pstrcat(r->pool, "Host: ", desthost, ":", destportstr, CRLF, NULL);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse e = ap_bucket_create_pool(buf, strlen(buf), r->pool);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse AP_BRIGADE_INSERT_TAIL(bb, e);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse }
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse else {
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse buf = apr_pstrcat(r->pool, "Host: ", desthost, CRLF, NULL);
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron e = ap_bucket_create_pool(buf, strlen(buf), r->pool);
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron AP_BRIGADE_INSERT_TAIL(bb, e);
421d9002d73db52972bcca8f4497fe5d603b6b8eaaron }
184f5da95d14895f7f33c90b8b8f70653afb0d92wrowe
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (conf->viaopt == via_block) {
184f5da95d14895f7f33c90b8b8f70653afb0d92wrowe /* Block all outgoing Via: headers */
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_table_unset(r->headers_in, "Via");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse } else if (conf->viaopt != via_off) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse /* Create a "Via:" request header entry and merge it */
bb0b94431dc9a1591a0a38a6c48925c6d9213c83rse i = ap_get_server_port(r);
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse if (ap_is_default_port(i,r)) {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse strcpy(portstr,"");
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse } else {
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse apr_snprintf(portstr, sizeof portstr, ":%d", i);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm }
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Generate outgoing Via: header with/without server comment: */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_table_mergen(r->headers_in, "Via",
a0e0d20b666cfc453ac76506079eb50e03997eefdougm (conf->viaopt == via_full)
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ? apr_psprintf(p, "%d.%d %s%s (%s)",
a0e0d20b666cfc453ac76506079eb50e03997eefdougm HTTP_VERSION_MAJOR(r->proto_num),
a0e0d20b666cfc453ac76506079eb50e03997eefdougm HTTP_VERSION_MINOR(r->proto_num),
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_get_server_name(r), portstr,
a0e0d20b666cfc453ac76506079eb50e03997eefdougm AP_SERVER_BASEVERSION)
a0e0d20b666cfc453ac76506079eb50e03997eefdougm : apr_psprintf(p, "%d.%d %s%s",
a0e0d20b666cfc453ac76506079eb50e03997eefdougm HTTP_VERSION_MAJOR(r->proto_num),
a0e0d20b666cfc453ac76506079eb50e03997eefdougm HTTP_VERSION_MINOR(r->proto_num),
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_get_server_name(r), portstr)
a0e0d20b666cfc453ac76506079eb50e03997eefdougm );
a0e0d20b666cfc453ac76506079eb50e03997eefdougm }
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm reqhdrs_arr = apr_table_elts(r->headers_in);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm reqhdrs = (apr_table_entry_t *) reqhdrs_arr->elts;
a0e0d20b666cfc453ac76506079eb50e03997eefdougm for (i = 0; i < reqhdrs_arr->nelts; i++) {
a0e0d20b666cfc453ac76506079eb50e03997eefdougm if (reqhdrs[i].key == NULL || reqhdrs[i].val == NULL
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 */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm || !strcasecmp(reqhdrs[i].key, "Proxy-Authorization"))
a0e0d20b666cfc453ac76506079eb50e03997eefdougm continue;
a0e0d20b666cfc453ac76506079eb50e03997eefdougm buf = apr_pstrcat(r->pool, reqhdrs[i].key, ": ", reqhdrs[i].val, CRLF, NULL);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm e = ap_bucket_create_pool(buf, strlen(buf), r->pool);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm AP_BRIGADE_INSERT_TAIL(bb, e);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm }
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm e = ap_bucket_create_pool(CRLF, strlen(CRLF), r->pool);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm AP_BRIGADE_INSERT_TAIL(bb, e);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm e = ap_bucket_create_flush();
a0e0d20b666cfc453ac76506079eb50e03997eefdougm AP_BRIGADE_INSERT_TAIL(bb, e);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_pass_brigade(origin->output_filters, bb);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm/* send the request data, if any. */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm if (ap_should_client_block(r)) {
a0e0d20b666cfc453ac76506079eb50e03997eefdougm while ((i = ap_get_client_block(r, buffer, sizeof buffer)) > 0) {
a0e0d20b666cfc453ac76506079eb50e03997eefdougm e = ap_bucket_create_pool(buffer, i, r->pool);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm AP_BRIGADE_INSERT_TAIL(bb, e);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm }
a0e0d20b666cfc453ac76506079eb50e03997eefdougm }
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Flush the data to the origin server */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm e = ap_bucket_create_flush();
a0e0d20b666cfc453ac76506079eb50e03997eefdougm AP_BRIGADE_INSERT_TAIL(bb, e);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_pass_brigade(origin->output_filters, bb);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_add_input_filter("HTTP", NULL, NULL, origin);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_add_input_filter("CORE_IN", NULL, NULL, origin);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm ap_brigade_destroy(bb);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm bb = ap_brigade_create(r->pool);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
a0e0d20b666cfc453ac76506079eb50e03997eefdougm /* Tell http_filter to grab the data one line at a time. */
a0e0d20b666cfc453ac76506079eb50e03997eefdougm origin->remain = 0;
a0e0d20b666cfc453ac76506079eb50e03997eefdougm
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);
a0e0d20b666cfc453ac76506079eb50e03997eefdougm if (len == -1) {
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm apr_close_socket(sock);
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm "ap_get_brigade() - proxy receive - Error reading from remote server %s (length %d)",
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm proxyhost ? proxyhost : desthost, len);
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm return ap_proxyerror(r, HTTP_BAD_GATEWAY,
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm "Error reading from remote server");
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm } else if (len == 0) {
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm apr_close_socket(sock);
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm return ap_proxyerror(r, HTTP_BAD_GATEWAY,
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm "Document contains no data");
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm }
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm/* Is it an HTTP/1 response? This is buggy if we ever see an HTTP/1.10 */
22357f10585a847ebf7b084cbe1db07ba071aeb6dougm if (ap_checkmask(buffer2, "HTTP/#.# ###*")) {
6a26d195dfba3a91f8352cabd4547afa77675bb1aaron int major, minor;
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm if (2 != sscanf(buffer2, "HTTP/%u.%u", &major, &minor)) {
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm major = 1;
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm minor = 0;
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm }
e18e68b42830409bf48de0df9eed3fe363664aa7aaron
0ccb8cf8cbc291aca5e8da62cde6cc6948e2081eben/* If not an HTTP/1 message or if the status line was > 8192 bytes */
0ccb8cf8cbc291aca5e8da62cde6cc6948e2081eben if (buffer2[5] != '1' || buffer2[len - 1] != '\n') {
e18e68b42830409bf48de0df9eed3fe363664aa7aaron apr_close_socket(sock);
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm return HTTP_BAD_GATEWAY;
e62985c7a1b46a5036a247f35bddac1308985758dougm }
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm backasswards = 0;
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm buffer2[--len] = '\0';
e18e68b42830409bf48de0df9eed3fe363664aa7aaron
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm buffer2[12] = '\0';
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm r->status = atoi(&buffer2[9]);
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm buffer2[12] = ' ';
e18e68b42830409bf48de0df9eed3fe363664aa7aaron r->status_line = apr_pstrdup(p, &buffer2[9]);
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm
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
e62985c7a1b46a5036a247f35bddac1308985758dougm resp_hdrs = ap_proxy_read_headers(r, buffer, HUGE_STRING_LEN, origin);
e62985c7a1b46a5036a247f35bddac1308985758dougm if (resp_hdrs == NULL) {
e62985c7a1b46a5036a247f35bddac1308985758dougm ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r->server,
8464a9c46b967001e38fe3c8afff51a649e9de51dougm "proxy: Bad HTTP/%d.%d header returned by %s (%s)",
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm major, minor, r->uri, r->method);
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm nocache = 1; /* do not cache this broken file */
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm }
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm else
8464a9c46b967001e38fe3c8afff51a649e9de51dougm {
8464a9c46b967001e38fe3c8afff51a649e9de51dougm clear_connection(p, resp_hdrs); /* Strip Connection hdrs */
e18e68b42830409bf48de0df9eed3fe363664aa7aaron ap_cache_el_header_merge(c, resp_hdrs);
8464a9c46b967001e38fe3c8afff51a649e9de51dougm }
8464a9c46b967001e38fe3c8afff51a649e9de51dougm
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm if (conf->viaopt != via_off && conf->viaopt != via_block) {
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm /* Create a "Via:" response header entry and merge it */
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm i = ap_get_server_port(r);
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm if (ap_is_default_port(i,r)) {
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm strcpy(portstr,"");
8464a9c46b967001e38fe3c8afff51a649e9de51dougm } else {
e18e68b42830409bf48de0df9eed3fe363664aa7aaron apr_snprintf(portstr, sizeof portstr, ":%d", i);
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm }
e18e68b42830409bf48de0df9eed3fe363664aa7aaron ap_cache_el_header_add(c, "Via", (conf->viaopt == via_full)
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm ? apr_psprintf(p, "%d.%d %s%s (%s)", major, minor,
8464a9c46b967001e38fe3c8afff51a649e9de51dougm ap_get_server_name(r), portstr, AP_SERVER_BASEVERSION)
8464a9c46b967001e38fe3c8afff51a649e9de51dougm : apr_psprintf(p, "%d.%d %s%s", major, minor,
8464a9c46b967001e38fe3c8afff51a649e9de51dougm ap_get_server_name(r), portstr));
8464a9c46b967001e38fe3c8afff51a649e9de51dougm }
8464a9c46b967001e38fe3c8afff51a649e9de51dougm }
8464a9c46b967001e38fe3c8afff51a649e9de51dougm else {
e18e68b42830409bf48de0df9eed3fe363664aa7aaron/* an http/0.9 response */
8464a9c46b967001e38fe3c8afff51a649e9de51dougm backasswards = 1;
8464a9c46b967001e38fe3c8afff51a649e9de51dougm r->status = 200;
8464a9c46b967001e38fe3c8afff51a649e9de51dougm r->status_line = "200 OK";
8464a9c46b967001e38fe3c8afff51a649e9de51dougm }
8464a9c46b967001e38fe3c8afff51a649e9de51dougm
8464a9c46b967001e38fe3c8afff51a649e9de51dougm/*
8464a9c46b967001e38fe3c8afff51a649e9de51dougm * HTTP/1.0 requires us to accept 3 types of dates, but only generate
e18e68b42830409bf48de0df9eed3fe363664aa7aaron * one type
e18e68b42830409bf48de0df9eed3fe363664aa7aaron */
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));
e62985c7a1b46a5036a247f35bddac1308985758dougm
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
d94fd18ee21dc9b8c1f422144a881e941687d41fdougm/* check if NoCache directive on this host */
6a26d195dfba3a91f8352cabd4547afa77675bb1aaron if (ap_cache_el_header(c, "Content-Length", &clen) == APR_SUCCESS)
content_length = atoi(clen ? clen : "-1");
for (i = 0; i < conf->nocaches->nelts; i++) {
if ((ncent[i].name != NULL && ap_strstr_c(desthost, ncent[i].name) != NULL)
|| destaddr.s_addr == ncent[i].addr.s_addr || ncent[i].name[0] == '*')
nocache = 1;
}
if(nocache || !ap_proxy_cache_should_cache(r, resp_hdrs, !backasswards))
ap_proxy_cache_error(&c);
else
ap_cache_el_data(c, &cachefp);
/* write status line */
#if 0
if (!r->assbackwards)
ap_rvputs(r, "HTTP/1.0 ", r->status_line, CRLF, NULL);
#endif
if (cachefp && apr_puts(apr_pstrcat(r->pool, "HTTP/1.0 ",
r->status_line, CRLF, NULL), cachefp) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"proxy: error writing status line to cache");
ap_proxy_cache_error(&c);
cachefp = NULL;
}
/* send headers */
ap_cache_el_header_walk(c, ap_proxy_send_hdr_line, r, NULL);
if (!r->assbackwards)
ap_rputs(CRLF, r);
r->sent_bodyct = 1;
/* Is it an HTTP/0.9 response? If so, send the extra data */
if (backasswards) {
cntr = len;
apr_send(r->connection->client_socket, buffer, &cntr);
cntr = len;
e = ap_bucket_create_heap(buffer, cntr, 0, NULL);
AP_BRIGADE_INSERT_TAIL(bb, e);
if (cachefp && apr_write(cachefp, buffer, &cntr) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"proxy: error writing extra data to cache");
ap_proxy_cache_error(&c);
}
}
/* send body */
/* if header only, then cache will be NULL */
/* HTTP/1.0 tells us to read to EOF, rather than content-length bytes */
if (!r->header_only) {
proxy_completion pc;
pc.content_length = content_length;
pc.cache_completion = conf->cache_completion;
origin->remain = content_length;
while (ap_get_brigade(origin->input_filters, bb, AP_MODE_BLOCKING) == APR_SUCCESS) {
if (AP_BUCKET_IS_EOS(AP_BRIGADE_LAST(bb))) {
ap_pass_brigade(r->output_filters, bb);
break;
}
ap_pass_brigade(r->output_filters, bb);
}
}
apr_close_socket(sock);
if(c) ap_proxy_cache_update(c);
return OK;
}