http_request.c revision 81a0e29031b198417bd4c534d326d021c81861e8
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor/* ====================================================================
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * The Apache Software License, Version 1.1
dfcf800069c4a5afdd843c8f0ae454140df9f26dlgentis * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * reserved.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * Redistribution and use in source and binary forms, with or without
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * modification, are permitted provided that the following conditions
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * 1. Redistributions of source code must retain the above copyright
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * notice, this list of conditions and the following disclaimer.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * 2. Redistributions in binary form must reproduce the above copyright
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * notice, this list of conditions and the following disclaimer in
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * the documentation and/or other materials provided with the
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * distribution.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * 3. The end-user documentation included with the redistribution,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * if any, must include the following acknowledgment:
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * "This product includes software developed by the
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * Apache Software Foundation (http://www.apache.org/)."
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * Alternately, this acknowledgment may appear in the software itself,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * if and wherever such third-party acknowledgments normally appear.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * 4. The names "Apache" and "Apache Software Foundation" must
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * not be used to endorse or promote products derived from this
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * software without prior written permission. For written
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * permission, please contact apache@apache.org.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * 5. Products derived from this software may not be called "Apache",
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * nor may "Apache" appear in their name, without prior written
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * permission of the Apache Software Foundation.
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * SUCH DAMAGE.
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * ====================================================================
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * This software consists of voluntary contributions made by many
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * individuals on behalf of the Apache Software Foundation. For more
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * information on the Apache Software Foundation, please see
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * Portions of this software are based upon public domain software
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * originally written at the National Center for Supercomputing Applications,
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * University of Illinois, Urbana-Champaign.
5a884cb4f4b177e207554c26334ef853c5665e79lgentis * http_request.c: functions to get and process requests
5a884cb4f4b177e207554c26334ef853c5665e79lgentis * Rob McCool 3/21/93
5a884cb4f4b177e207554c26334ef853c5665e79lgentis * Thoroughly revamped by rst for Apache. NB this file reads
5a884cb4f4b177e207554c26334ef853c5665e79lgentis * best from the bottom up.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor/*****************************************************************
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * Mainline request processing...
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor char *custom_response = ap_response_code_string(r, error_index);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * The following takes care of Apache redirects to custom response URLs
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * Note that if we are already dealing with the response to some other
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * error condition, we just report on the original error, and give up on
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * any attempt to handle the other thing "intelligently"...
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor if (r->status != HTTP_OK && !ap_status_drops_connection(type)) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor custom_response = NULL; /* Do NOT retry the custom thing! */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * This test is done here so that none of the auth modules needs to know
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * about proxy authentication. They treat it like normal auth, and then
1f8ff58254f12b4a0756dc2bd173fcd4a401d3e2lgentis * we tweak the status.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor if (HTTP_UNAUTHORIZED == r->status && PROXYREQ_PROXY == r->proxyreq) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* If we don't want to keep the connection, make sure we mark that the
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * connection is not eligible for keepalive. If we want to keep the
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * connection, be sure that the request body (if any) has been read.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * Two types of custom redirects --- plain text, and URLs. Plain text has
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * a leading '"', so the URL code, here, is triggered on its absence
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * The URL isn't local, so lets drop through the rest of this
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * apache code, and continue with the usual REDIRECT handler.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * But note that the client will ultimately see the wrong
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * status...
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis apr_table_setn(r->headers_out, "Location", custom_response);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor r->no_local_copy = 1; /* Do NOT send HTTP_NOT_MODIFIED for
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * error documents! */
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * This redirect needs to be a GET no matter what the original
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * method was.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor apr_table_setn(r->subprocess_env, "REQUEST_METHOD", r->method);
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * Provide a special method for modules to communicate
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * more informative (than the plain canned) messages to us.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * Propagate them to ErrorDocuments via the ERROR_NOTES variable:
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor apr_table_setn(r->subprocess_env, "ERROR_NOTES", error_notes);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * Dumb user has given us a bad url to redirect to --- fake up
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * dying with a recursive server error...
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor "Invalid error redirection directive: %s",
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* ### if would be nice if we could PEEK without a brigade. that would
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor ### allow us to defer creation of the brigade to when we actually
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor ### need to send a FLUSH. */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor apr_bucket_brigade *bb = apr_brigade_create(r->pool, c->bucket_alloc);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* Flush the filter contents if:
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * 1) the connection will be closed
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * 2) there isn't a request ready to be read
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis /* ### shouldn't this read from the connection input filters? */
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis /* ### is zero correct? that means "read one line" */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor ap_get_brigade(r->input_filters, bb, AP_MODE_EATCRLF,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor apr_bucket *e = apr_bucket_flush_create(c->bucket_alloc);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* We just send directly to the connection based filters. At
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * this point, we know that we have seen all of the data
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * (request finalization sent an EOS bucket, which empties all
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * of the request filters). We just want to flush the buckets
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * if something hasn't been sent to the network yet.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* Give quick handlers a shot at serving the request on the fast
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * path, bypassing all of the other Apache hooks.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * This hook was added to enable serving files out of a URI keyed
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * content cache ( e.g., Mike Abbott's Quick Shortcut Cache,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * described here: http://oss.sgi.com/projects/apache/mod_qsc.html )
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * It may have other uses as well, such as routing requests directly to
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * content handlers that have the ability to grok HTTP and do their
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * own access checking, etc (e.g. servlet engines).
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * Use this hook with extreme care and only if you know what you are
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor access_status = ap_run_quick_handler(r, 0); /* Not a look-up request */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* e.g., something not in storage like TRACE */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * We want to flush the last packet if this isn't a pipelining connection
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * *before* we start into logging. Suppose that the logging causes a DNS
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * lookup to occur, which may have a high latency. If we hold off on
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * this packet, then it'll appear like the link is stalled when really
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * it's the application that's stalled.
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentisstatic apr_table_t *rename_original_env(apr_pool_t *p, apr_table_t *t)
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis const apr_array_header_t *env_arr = apr_table_elts(t);
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor apr_table_t *new = apr_table_make(p, env_arr->nalloc);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor apr_table_setn(new, apr_pstrcat(p, "REDIRECT_", elts[i].key, NULL),
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzorstatic request_rec *internal_internal_redirect(const char *new_uri,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor request_rec *new = (request_rec *) apr_pcalloc(r->pool,
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * A whole lot of this really ought to be shared with http_protocol.c...
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * another missing cleanup. It's particularly inappropriate to be
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * setting header_only, etc., here.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor new->allowed_methods = ap_make_method_list(new->pool, 2);
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis new->request_config = ap_create_request_config(r->pool);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* Must have prev and next pointers set before calling create_request
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* Inherit the rest of the protocol info... */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor new->subprocess_env = rename_original_env(r->pool, r->subprocess_env);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor new->allowed_methods = ap_make_method_list(new->pool, 2);
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis new->read_length = r->read_length; /* We can only read it once */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor while (f) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor if (f->r == r) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor while (f) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor if (f->r == r) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor ap_add_input_filter("HTTP_IN", NULL, new, new->connection);
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis apr_table_setn(new->subprocess_env, "REDIRECT_STATUS",
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * XXX: hmm. This is because mod_setenvif and mod_unique_id really need
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * to do their thing on internal redirects as well. Perhaps this is a
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * misnamed function.
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis if ((access_status = ap_run_post_read_request(new))) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor/* XXX: Is this function is so bogus and fragile that we deep-6 it? */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzorAP_DECLARE(void) ap_internal_fast_redirect(request_rec *rr, request_rec *r)
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* We need to tell POOL_DEBUG that we're guaranteeing that rr->pool
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * will exist as long as r->pool. Otherwise we run into troubles because
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * some values in this request will be allocated in r->pool, and others in
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * rr->pool.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor /* copy output headers from subrequest, but leave negotiation headers */
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor r->notes = apr_table_overlay(r->pool, rr->notes, r->notes);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor r->headers_out = apr_table_overlay(r->pool, rr->headers_out,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor r->err_headers_out = apr_table_overlay(r->pool, rr->err_headers_out,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor r->subprocess_env = apr_table_overlay(r->pool, rr->subprocess_env,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor ap_add_output_filter_handle(ap_subreq_core_filter_handle,
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor else if (r->output_filters->frec == ap_subreq_core_filter_handle) {
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis /* If any filters pointed at the now-defunct rr, we must point them
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * at our "new" instance of r. In particular, some of rr's structures
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * will now be bogus (say rr->headers_out). If a filter tried to modify
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * their f->r structure when it is pointing to rr, the real request_rec
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * will not get updated. Fix that here.
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentisAP_DECLARE(void) ap_internal_redirect(const char *new_uri, request_rec *r)
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis request_rec *new = internal_internal_redirect(new_uri, r);
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis int access_status = ap_process_request_internal(new);
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor/* This function is designed for things like actions or CGI scripts, when
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * using AddHandler, and you want to preserve the content type across
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * an internal redirect.
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzorAP_DECLARE(void) ap_internal_redirect_handler(const char *new_uri, request_rec *r)
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor request_rec *new = internal_internal_redirect(new_uri, r);
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis if ((access_status = ap_invoke_handler(new)) != 0) {
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzorAP_DECLARE(void) ap_allow_methods(request_rec *r, int reset, ...)
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor const char *method;
ee310fab1ff1ed24942136c5e7423bfae0ff01eagryzor * Get rid of any current settings if requested; not just the
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * well-known methods but any extensions as well.
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis while ((method = va_arg(methods, const char *)) != NULL) {
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentisAP_DECLARE(void) ap_allow_standard_methods(request_rec *r, int reset, ...)
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * Get rid of any current settings if requested; not just the
3016c72ae8173bcfc0989ff1b297d4f27f445108lgentis * well-known methods but any extensions as well.