Blame modules/http/http_request.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * http_request.c: functions to get and process requests
Packit 90a5c9
 *
Packit 90a5c9
 * Rob McCool 3/21/93
Packit 90a5c9
 *
Packit 90a5c9
 * Thoroughly revamped by rst for Apache.  NB this file reads
Packit 90a5c9
 * best from the bottom up.
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_file_io.h"
Packit 90a5c9
#include "apr_fnmatch.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "util_charset.h"
Packit 90a5c9
#include "scoreboard.h"
Packit 90a5c9
Packit 90a5c9
#include "mod_core.h"
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_STDARG_H
Packit 90a5c9
#include <stdarg.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
APLOG_USE_MODULE(http);
Packit 90a5c9
Packit 90a5c9
/*****************************************************************
Packit 90a5c9
 *
Packit 90a5c9
 * Mainline request processing...
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/* XXX A cleaner and faster way to do this might be to pass the request_rec
Packit 90a5c9
 * down the filter chain as a parameter.  It would need to change for
Packit 90a5c9
 * subrequest vs. main request filters; perhaps the subrequest filter could
Packit 90a5c9
 * make the switch.
Packit 90a5c9
 */
Packit 90a5c9
static void update_r_in_filters(ap_filter_t *f,
Packit 90a5c9
                                request_rec *from,
Packit 90a5c9
                                request_rec *to)
Packit 90a5c9
{
Packit 90a5c9
    while (f) {
Packit 90a5c9
        if (f->r == from) {
Packit 90a5c9
            f->r = to;
Packit 90a5c9
        }
Packit 90a5c9
        f = f->next;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void ap_die_r(int type, request_rec *r, int recursive_error)
Packit 90a5c9
{
Packit 90a5c9
    char *custom_response;
Packit 90a5c9
    request_rec *r_1st_err = r;
Packit 90a5c9
Packit 90a5c9
    if (type == OK || type == DONE) {
Packit 90a5c9
        ap_finalize_request_protocol(r);
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!ap_is_HTTP_VALID_RESPONSE(type)) {
Packit 90a5c9
        ap_filter_t *next;
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * Check if we still have the ap_http_header_filter in place. If
Packit 90a5c9
         * this is the case we should not ignore the error here because
Packit 90a5c9
         * it means that we have not sent any response at all and never
Packit 90a5c9
         * will. This is bad. Sent an internal server error instead.
Packit 90a5c9
         */
Packit 90a5c9
        next = r->output_filters;
Packit 90a5c9
        while (next && (next->frec != ap_http_header_filter_handle)) {
Packit 90a5c9
               next = next->next;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * If next != NULL then we left the while above because of
Packit 90a5c9
         * next->frec == ap_http_header_filter
Packit 90a5c9
         */
Packit 90a5c9
        if (next) {
Packit 90a5c9
            if (type != AP_FILTER_ERROR) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01579)
Packit 90a5c9
                              "Invalid response status %i", type);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02831)
Packit 90a5c9
                              "Response from AP_FILTER_ERROR");
Packit 90a5c9
            }
Packit 90a5c9
            type = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * The following takes care of Apache redirects to custom response URLs
Packit 90a5c9
     * Note that if we are already dealing with the response to some other
Packit 90a5c9
     * error condition, we just report on the original error, and give up on
Packit 90a5c9
     * any attempt to handle the other thing "intelligently"...
Packit 90a5c9
     */
Packit 90a5c9
    if (recursive_error != HTTP_OK) {
Packit 90a5c9
        while (r_1st_err->prev && (r_1st_err->prev->status != HTTP_OK))
Packit 90a5c9
            r_1st_err = r_1st_err->prev;  /* Get back to original error */
Packit 90a5c9
Packit 90a5c9
        if (r_1st_err != r) {
Packit 90a5c9
            /* The recursive error was caused by an ErrorDocument specifying
Packit 90a5c9
             * an internal redirect to a bad URI.  ap_internal_redirect has
Packit 90a5c9
             * changed the filter chains to point to the ErrorDocument's
Packit 90a5c9
             * request_rec.  Back out those changes so we can safely use the
Packit 90a5c9
             * original failing request_rec to send the canned error message.
Packit 90a5c9
             *
Packit 90a5c9
             * ap_send_error_response gets rid of existing resource filters
Packit 90a5c9
             * on the output side, so we can skip those.
Packit 90a5c9
             */
Packit 90a5c9
            update_r_in_filters(r_1st_err->proto_output_filters, r, r_1st_err);
Packit 90a5c9
            update_r_in_filters(r_1st_err->input_filters, r, r_1st_err);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        custom_response = NULL; /* Do NOT retry the custom thing! */
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        int error_index = ap_index_of_response(type);
Packit 90a5c9
        custom_response = ap_response_code_string(r, error_index);
Packit 90a5c9
        recursive_error = 0;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    r->status = type;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * This test is done here so that none of the auth modules needs to know
Packit 90a5c9
     * about proxy authentication.  They treat it like normal auth, and then
Packit 90a5c9
     * we tweak the status.
Packit 90a5c9
     */
Packit 90a5c9
    if (HTTP_UNAUTHORIZED == r->status && PROXYREQ_PROXY == r->proxyreq) {
Packit 90a5c9
        r->status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* If we don't want to keep the connection, make sure we mark that the
Packit 90a5c9
     * connection is not eligible for keepalive.  If we want to keep the
Packit 90a5c9
     * connection, be sure that the request body (if any) has been read.
Packit 90a5c9
     */
Packit 90a5c9
    if (ap_status_drops_connection(r->status)) {
Packit 90a5c9
        r->connection->keepalive = AP_CONN_CLOSE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Two types of custom redirects --- plain text, and URLs. Plain text has
Packit 90a5c9
     * a leading '"', so the URL code, here, is triggered on its absence
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    if (custom_response && custom_response[0] != '"') {
Packit 90a5c9
Packit 90a5c9
        if (ap_is_url(custom_response)) {
Packit 90a5c9
            /*
Packit 90a5c9
             * The URL isn't local, so lets drop through the rest of this
Packit 90a5c9
             * apache code, and continue with the usual REDIRECT handler.
Packit 90a5c9
             * But note that the client will ultimately see the wrong
Packit 90a5c9
             * status...
Packit 90a5c9
             */
Packit 90a5c9
            r->status = HTTP_MOVED_TEMPORARILY;
Packit 90a5c9
            apr_table_setn(r->headers_out, "Location", custom_response);
Packit 90a5c9
        }
Packit 90a5c9
        else if (custom_response[0] == '/') {
Packit 90a5c9
            const char *error_notes, *original_method;
Packit 90a5c9
            int original_method_number;
Packit 90a5c9
            r->no_local_copy = 1;       /* Do NOT send HTTP_NOT_MODIFIED for
Packit 90a5c9
                                         * error documents! */
Packit 90a5c9
            /*
Packit 90a5c9
             * This redirect needs to be a GET no matter what the original
Packit 90a5c9
             * method was.
Packit 90a5c9
             */
Packit 90a5c9
            apr_table_setn(r->subprocess_env, "REQUEST_METHOD", r->method);
Packit 90a5c9
Packit 90a5c9
            /*
Packit 90a5c9
             * Provide a special method for modules to communicate
Packit 90a5c9
             * more informative (than the plain canned) messages to us.
Packit 90a5c9
             * Propagate them to ErrorDocuments via the ERROR_NOTES variable:
Packit 90a5c9
             */
Packit 90a5c9
            if ((error_notes = apr_table_get(r->notes,
Packit 90a5c9
                                             "error-notes")) != NULL) {
Packit 90a5c9
                apr_table_setn(r->subprocess_env, "ERROR_NOTES", error_notes);
Packit 90a5c9
            }
Packit 90a5c9
            original_method = r->method;
Packit 90a5c9
            original_method_number = r->method_number;
Packit 90a5c9
            r->method = "GET";
Packit 90a5c9
            r->method_number = M_GET;
Packit 90a5c9
            ap_internal_redirect(custom_response, r);
Packit 90a5c9
            /* preserve ability to see %
Packit 90a5c9
            r->method = original_method;
Packit 90a5c9
            r->method_number = original_method_number;
Packit 90a5c9
            return;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /*
Packit 90a5c9
             * Dumb user has given us a bad url to redirect to --- fake up
Packit 90a5c9
             * dying with a recursive server error...
Packit 90a5c9
             */
Packit 90a5c9
            recursive_error = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01580)
Packit 90a5c9
                        "Invalid error redirection directive: %s",
Packit 90a5c9
                        custom_response);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    ap_send_error_response(r_1st_err, recursive_error);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_die(int type, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_die_r(type, r, r->status);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_check_pipeline(conn_rec *c, apr_bucket_brigade *bb,
Packit 90a5c9
                                           unsigned int max_blank_lines)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv = APR_EOF;
Packit 90a5c9
    ap_input_mode_t mode = AP_MODE_SPECULATIVE;
Packit 90a5c9
    unsigned int num_blank_lines = 0;
Packit 90a5c9
    apr_size_t cr = 0;
Packit 90a5c9
    char buf[2];
Packit 90a5c9
Packit 90a5c9
    while (c->keepalive != AP_CONN_CLOSE && !c->aborted) {
Packit 90a5c9
        apr_size_t len = cr + 1;
Packit 90a5c9
Packit 90a5c9
        apr_brigade_cleanup(bb);
Packit 90a5c9
        rv = ap_get_brigade(c->input_filters, bb, mode,
Packit 90a5c9
                            APR_NONBLOCK_READ, len);
Packit 90a5c9
        if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(bb) || !max_blank_lines) {
Packit 90a5c9
            if (mode == AP_MODE_READBYTES) {
Packit 90a5c9
                /* Unexpected error, stop with this connection */
Packit 90a5c9
                ap_log_cerror(APLOG_MARK, APLOG_ERR, rv, c, APLOGNO(02967)
Packit 90a5c9
                              "Can't consume pipelined empty lines");
Packit 90a5c9
                c->keepalive = AP_CONN_CLOSE;
Packit 90a5c9
                rv = APR_EGENERAL;
Packit 90a5c9
            }
Packit 90a5c9
            else if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(bb)) {
Packit 90a5c9
                if (rv != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(rv)) {
Packit 90a5c9
                    /* Pipe is dead */
Packit 90a5c9
                    c->keepalive = AP_CONN_CLOSE;
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    /* Pipe is up and empty */
Packit 90a5c9
                    rv = APR_EAGAIN;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                apr_off_t n = 0;
Packit 90a5c9
                /* Single read asked, (non-meta-)data available? */
Packit 90a5c9
                rv = apr_brigade_length(bb, 0, &n);
Packit 90a5c9
                if (rv == APR_SUCCESS && n <= 0) {
Packit 90a5c9
                    rv = APR_EAGAIN;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Lookup and consume blank lines */
Packit 90a5c9
        rv = apr_brigade_flatten(bb, buf, &len;;
Packit 90a5c9
        if (rv != APR_SUCCESS || len != cr + 1) {
Packit 90a5c9
            int log_level;
Packit 90a5c9
            if (mode == AP_MODE_READBYTES) {
Packit 90a5c9
                /* Unexpected error, stop with this connection */
Packit 90a5c9
                c->keepalive = AP_CONN_CLOSE;
Packit 90a5c9
                log_level = APLOG_ERR;
Packit 90a5c9
                rv = APR_EGENERAL;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                /* Let outside (non-speculative/blocking) read determine
Packit 90a5c9
                 * where this possible failure comes from (metadata,
Packit 90a5c9
                 * morphed EOF socket, ...). Debug only here.
Packit 90a5c9
                 */
Packit 90a5c9
                log_level = APLOG_DEBUG;
Packit 90a5c9
                rv = APR_SUCCESS;
Packit 90a5c9
            }
Packit 90a5c9
            ap_log_cerror(APLOG_MARK, log_level, rv, c, APLOGNO(02968)
Packit 90a5c9
                          "Can't check pipelined data");
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (mode == AP_MODE_READBYTES) {
Packit 90a5c9
            /* [CR]LF consumed, try next */
Packit 90a5c9
            mode = AP_MODE_SPECULATIVE;
Packit 90a5c9
            cr = 0;
Packit 90a5c9
        }
Packit 90a5c9
        else if (cr) {
Packit 90a5c9
            AP_DEBUG_ASSERT(len == 2 && buf[0] == APR_ASCII_CR);
Packit 90a5c9
            if (buf[1] == APR_ASCII_LF) {
Packit 90a5c9
                /* consume this CRLF */
Packit 90a5c9
                mode = AP_MODE_READBYTES;
Packit 90a5c9
                num_blank_lines++;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                /* CR(?!LF) is data */
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            if (buf[0] == APR_ASCII_LF) {
Packit 90a5c9
                /* consume this LF */
Packit 90a5c9
                mode = AP_MODE_READBYTES;
Packit 90a5c9
                num_blank_lines++;
Packit 90a5c9
            }
Packit 90a5c9
            else if (buf[0] == APR_ASCII_CR) {
Packit 90a5c9
                cr = 1;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                /* Not [CR]LF, some data */
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        if (num_blank_lines > max_blank_lines) {
Packit 90a5c9
            /* Enough blank lines with this connection,
Packit 90a5c9
             * stop and don't recycle it.
Packit 90a5c9
             */
Packit 90a5c9
            c->keepalive = AP_CONN_CLOSE;
Packit 90a5c9
            rv = APR_NOTFOUND;
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#define RETRIEVE_BRIGADE_FROM_POOL(bb, key, pool, allocator) do {       \
Packit 90a5c9
    apr_pool_userdata_get((void **)&bb, key, pool);                     \
Packit 90a5c9
    if (bb == NULL) {                                                   \
Packit 90a5c9
        bb = apr_brigade_create(pool, allocator);                       \
Packit 90a5c9
        apr_pool_userdata_setn((const void *)bb, key, NULL, pool);      \
Packit 90a5c9
    }                                                                   \
Packit 90a5c9
    else {                                                              \
Packit 90a5c9
        apr_brigade_cleanup(bb);                                        \
Packit 90a5c9
    }                                                                   \
Packit 90a5c9
} while(0)
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_process_request_after_handler(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    apr_bucket_brigade *bb;
Packit 90a5c9
    apr_bucket *b;
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    /* Send an EOR bucket through the output filter chain.  When
Packit 90a5c9
     * this bucket is destroyed, the request will be logged and
Packit 90a5c9
     * its pool will be freed
Packit 90a5c9
     */
Packit 90a5c9
    RETRIEVE_BRIGADE_FROM_POOL(bb, "ap_process_request_after_handler_brigade",
Packit 90a5c9
                               c->pool, c->bucket_alloc);
Packit 90a5c9
    b = ap_bucket_eor_create(c->bucket_alloc, r);
Packit 90a5c9
    APR_BRIGADE_INSERT_HEAD(bb, b);
Packit 90a5c9
Packit 90a5c9
    ap_pass_brigade(c->output_filters, bb);
Packit 90a5c9
    
Packit 90a5c9
    /* The EOR bucket has either been handled by an output filter (eg.
Packit 90a5c9
     * deleted or moved to a buffered_bb => no more in bb), or an error
Packit 90a5c9
     * occured before that (eg. c->aborted => still in bb) and we ought
Packit 90a5c9
     * to destroy it now. So cleanup any remaining bucket along with
Packit 90a5c9
     * the orphan request (if any).
Packit 90a5c9
     */
Packit 90a5c9
    apr_brigade_cleanup(bb);
Packit 90a5c9
Packit 90a5c9
    /* From here onward, it is no longer safe to reference r
Packit 90a5c9
     * or r->pool, because r->pool may have been destroyed
Packit 90a5c9
     * already by the EOR bucket's cleanup function.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    /* Check pipeline consuming blank lines, they must not be interpreted as
Packit 90a5c9
     * the next pipelined request, otherwise we would block on the next read
Packit 90a5c9
     * without flushing data, and hence possibly delay pending response(s)
Packit 90a5c9
     * until the next/real request comes in or the keepalive timeout expires.
Packit 90a5c9
     */
Packit 90a5c9
    rv = ap_check_pipeline(c, bb, DEFAULT_LIMIT_BLANK_LINES);
Packit 90a5c9
    c->data_in_input_filters = (rv == APR_SUCCESS);
Packit 90a5c9
    apr_brigade_cleanup(bb);
Packit 90a5c9
Packit 90a5c9
    if (c->cs)
Packit 90a5c9
        c->cs->state = (c->aborted) ? CONN_STATE_LINGER
Packit 90a5c9
                                    : CONN_STATE_WRITE_COMPLETION;
Packit 90a5c9
    AP_PROCESS_REQUEST_RETURN((uintptr_t)r, r->uri, r->status);
Packit 90a5c9
    if (ap_extended_status) {
Packit 90a5c9
        ap_time_process_request(c->sbh, STOP_PREQUEST);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
void ap_process_async_request(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    int access_status;
Packit 90a5c9
Packit 90a5c9
    /* Give quick handlers a shot at serving the request on the fast
Packit 90a5c9
     * path, bypassing all of the other Apache hooks.
Packit 90a5c9
     *
Packit 90a5c9
     * This hook was added to enable serving files out of a URI keyed
Packit 90a5c9
     * content cache ( e.g., Mike Abbott's Quick Shortcut Cache,
Packit 90a5c9
     * described here: http://oss.sgi.com/projects/apache/mod_qsc.html )
Packit 90a5c9
     *
Packit 90a5c9
     * It may have other uses as well, such as routing requests directly to
Packit 90a5c9
     * content handlers that have the ability to grok HTTP and do their
Packit 90a5c9
     * own access checking, etc (e.g. servlet engines).
Packit 90a5c9
     *
Packit 90a5c9
     * Use this hook with extreme care and only if you know what you are
Packit 90a5c9
     * doing.
Packit 90a5c9
     */
Packit 90a5c9
    AP_PROCESS_REQUEST_ENTRY((uintptr_t)r, r->uri);
Packit 90a5c9
    if (ap_extended_status) {
Packit 90a5c9
        ap_time_process_request(r->connection->sbh, START_PREQUEST);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (APLOGrtrace4(r)) {
Packit 90a5c9
        int i;
Packit 90a5c9
        const apr_array_header_t *t_h = apr_table_elts(r->headers_in);
Packit 90a5c9
        const apr_table_entry_t *t_elt = (apr_table_entry_t *)t_h->elts;
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r,
Packit 90a5c9
                      "Headers received from client:");
Packit 90a5c9
        for (i = 0; i < t_h->nelts; i++, t_elt++) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, "  %s: %s",
Packit 90a5c9
                          ap_escape_logitem(r->pool, t_elt->key),
Packit 90a5c9
                          ap_escape_logitem(r->pool, t_elt->val));
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    apr_thread_mutex_create(&r->invoke_mtx, APR_THREAD_MUTEX_DEFAULT, r->pool);
Packit 90a5c9
    apr_thread_mutex_lock(r->invoke_mtx);
Packit 90a5c9
#endif
Packit 90a5c9
    access_status = ap_run_quick_handler(r, 0);  /* Not a look-up request */
Packit 90a5c9
    if (access_status == DECLINED) {
Packit 90a5c9
        access_status = ap_process_request_internal(r);
Packit 90a5c9
        if (access_status == OK) {
Packit 90a5c9
            access_status = ap_invoke_handler(r);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (access_status == SUSPENDED) {
Packit 90a5c9
        /* TODO: Should move these steps into a generic function, so modules
Packit 90a5c9
         * working on a suspended request can also call _ENTRY again.
Packit 90a5c9
         */
Packit 90a5c9
        AP_PROCESS_REQUEST_RETURN((uintptr_t)r, r->uri, access_status);
Packit 90a5c9
        if (ap_extended_status) {
Packit 90a5c9
            ap_time_process_request(c->sbh, STOP_PREQUEST);
Packit 90a5c9
        }
Packit 90a5c9
        if (c->cs)
Packit 90a5c9
            c->cs->state = CONN_STATE_SUSPENDED;
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
        apr_thread_mutex_unlock(r->invoke_mtx);
Packit 90a5c9
#endif
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    apr_thread_mutex_unlock(r->invoke_mtx);
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    ap_die_r(access_status, r, HTTP_OK);
Packit 90a5c9
Packit 90a5c9
    ap_process_request_after_handler(r);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_process_request(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    apr_bucket_brigade *bb;
Packit 90a5c9
    apr_bucket *b;
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    ap_process_async_request(r);
Packit 90a5c9
Packit 90a5c9
    if (!c->data_in_input_filters) {
Packit 90a5c9
        RETRIEVE_BRIGADE_FROM_POOL(bb, "ap_process_request_brigade", 
Packit 90a5c9
                                   c->pool, c->bucket_alloc);
Packit 90a5c9
        b = apr_bucket_flush_create(c->bucket_alloc);
Packit 90a5c9
        APR_BRIGADE_INSERT_HEAD(bb, b);
Packit 90a5c9
        rv = ap_pass_brigade(c->output_filters, bb);
Packit 90a5c9
        if (APR_STATUS_IS_TIMEUP(rv)) {
Packit 90a5c9
            /*
Packit 90a5c9
             * Notice a timeout as an error message. This might be
Packit 90a5c9
             * valuable for detecting clients with broken network
Packit 90a5c9
             * connections or possible DoS attacks.
Packit 90a5c9
             */
Packit 90a5c9
            ap_log_cerror(APLOG_MARK, APLOG_INFO, rv, c, APLOGNO(01581)
Packit 90a5c9
                          "flushing data to the client");
Packit 90a5c9
        }
Packit 90a5c9
        apr_brigade_cleanup(bb);
Packit 90a5c9
    }
Packit 90a5c9
    if (ap_extended_status) {
Packit 90a5c9
        ap_time_process_request(c->sbh, STOP_PREQUEST);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_table_t *rename_original_env(apr_pool_t *p, apr_table_t *t)
Packit 90a5c9
{
Packit 90a5c9
    const apr_array_header_t *env_arr = apr_table_elts(t);
Packit 90a5c9
    const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
Packit 90a5c9
    apr_table_t *new = apr_table_make(p, env_arr->nalloc);
Packit 90a5c9
    int i;
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < env_arr->nelts; ++i) {
Packit 90a5c9
        if (!elts[i].key)
Packit 90a5c9
            continue;
Packit 90a5c9
        apr_table_setn(new, apr_pstrcat(p, "REDIRECT_", elts[i].key, NULL),
Packit 90a5c9
                  elts[i].val);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static request_rec *internal_internal_redirect(const char *new_uri,
Packit 90a5c9
                                               request_rec *r) {
Packit 90a5c9
    int access_status;
Packit 90a5c9
    request_rec *new;
Packit 90a5c9
    const char *vary_header;
Packit 90a5c9
Packit 90a5c9
    if (ap_is_recursion_limit_exceeded(r)) {
Packit 90a5c9
        ap_die(HTTP_INTERNAL_SERVER_ERROR, r);
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    new = (request_rec *) apr_pcalloc(r->pool, sizeof(request_rec));
Packit 90a5c9
Packit 90a5c9
    new->connection = r->connection;
Packit 90a5c9
    new->server     = r->server;
Packit 90a5c9
    new->pool       = r->pool;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * A whole lot of this really ought to be shared with http_protocol.c...
Packit 90a5c9
     * another missing cleanup.  It's particularly inappropriate to be
Packit 90a5c9
     * setting header_only, etc., here.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    new->method          = r->method;
Packit 90a5c9
    new->method_number   = r->method_number;
Packit 90a5c9
    new->allowed_methods = ap_make_method_list(new->pool, 2);
Packit 90a5c9
    ap_parse_uri(new, new_uri);
Packit 90a5c9
    new->parsed_uri.port_str = r->parsed_uri.port_str;
Packit 90a5c9
    new->parsed_uri.port = r->parsed_uri.port;
Packit 90a5c9
Packit 90a5c9
    new->request_config = ap_create_request_config(r->pool);
Packit 90a5c9
Packit 90a5c9
    new->per_dir_config = r->server->lookup_defaults;
Packit 90a5c9
Packit 90a5c9
    new->prev = r;
Packit 90a5c9
    r->next   = new;
Packit 90a5c9
Packit 90a5c9
    new->useragent_addr = r->useragent_addr;
Packit 90a5c9
    new->useragent_ip = r->useragent_ip;
Packit 90a5c9
Packit 90a5c9
    /* Must have prev and next pointers set before calling create_request
Packit 90a5c9
     * hook.
Packit 90a5c9
     */
Packit 90a5c9
    ap_run_create_request(new);
Packit 90a5c9
Packit 90a5c9
    /* Inherit the rest of the protocol info... */
Packit 90a5c9
Packit 90a5c9
    new->the_request = r->the_request;
Packit 90a5c9
Packit 90a5c9
    new->allowed         = r->allowed;
Packit 90a5c9
Packit 90a5c9
    new->status          = r->status;
Packit 90a5c9
    new->assbackwards    = r->assbackwards;
Packit 90a5c9
    new->header_only     = r->header_only;
Packit 90a5c9
    new->protocol        = r->protocol;
Packit 90a5c9
    new->proto_num       = r->proto_num;
Packit 90a5c9
    new->hostname        = r->hostname;
Packit 90a5c9
    new->request_time    = r->request_time;
Packit 90a5c9
    new->main            = r->main;
Packit 90a5c9
Packit 90a5c9
    new->headers_in      = r->headers_in;
Packit 90a5c9
    new->trailers_in     = r->trailers_in;
Packit 90a5c9
    new->headers_out     = apr_table_make(r->pool, 12);
Packit 90a5c9
    if (ap_is_HTTP_REDIRECT(new->status)) {
Packit 90a5c9
        const char *location = apr_table_get(r->headers_out, "Location");
Packit 90a5c9
        if (location)
Packit 90a5c9
            apr_table_setn(new->headers_out, "Location", location);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* A module (like mod_rewrite) can force an internal redirect
Packit 90a5c9
     * to carry over the Vary header (if present).
Packit 90a5c9
     */
Packit 90a5c9
    if (apr_table_get(r->notes, "redirect-keeps-vary")) {
Packit 90a5c9
        if((vary_header = apr_table_get(r->headers_out, "Vary"))) {
Packit 90a5c9
            apr_table_setn(new->headers_out, "Vary", vary_header);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    new->err_headers_out = r->err_headers_out;
Packit 90a5c9
    new->trailers_out    = apr_table_make(r->pool, 5);
Packit 90a5c9
    new->subprocess_env  = rename_original_env(r->pool, r->subprocess_env);
Packit 90a5c9
    new->notes           = apr_table_make(r->pool, 5);
Packit 90a5c9
Packit 90a5c9
    new->htaccess        = r->htaccess;
Packit 90a5c9
    new->no_cache        = r->no_cache;
Packit 90a5c9
    new->expecting_100   = r->expecting_100;
Packit 90a5c9
    new->no_local_copy   = r->no_local_copy;
Packit 90a5c9
    new->read_length     = r->read_length;     /* We can only read it once */
Packit 90a5c9
    new->vlist_validator = r->vlist_validator;
Packit 90a5c9
Packit 90a5c9
    new->proto_output_filters  = r->proto_output_filters;
Packit 90a5c9
    new->proto_input_filters   = r->proto_input_filters;
Packit 90a5c9
Packit 90a5c9
    new->input_filters   = new->proto_input_filters;
Packit 90a5c9
Packit 90a5c9
    if (new->main) {
Packit 90a5c9
        ap_filter_t *f, *nextf;
Packit 90a5c9
Packit 90a5c9
        /* If this is a subrequest, the filter chain may contain a
Packit 90a5c9
         * mixture of filters specific to the old request (r), and
Packit 90a5c9
         * some inherited from r->main.  Here, inherit that filter
Packit 90a5c9
         * chain, and remove all those which are specific to the old
Packit 90a5c9
         * request; ensuring the subreq filter is left in place. */
Packit 90a5c9
        new->output_filters = r->output_filters;
Packit 90a5c9
Packit 90a5c9
        f = new->output_filters;
Packit 90a5c9
        do {
Packit 90a5c9
            nextf = f->next;
Packit 90a5c9
Packit 90a5c9
            if (f->r == r && f->frec != ap_subreq_core_filter_handle) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01582)
Packit 90a5c9
                              "dropping filter '%s' in internal redirect from %s to %s",
Packit 90a5c9
                              f->frec->name, r->unparsed_uri, new_uri);
Packit 90a5c9
Packit 90a5c9
                /* To remove the filter, first set f->r to the *new*
Packit 90a5c9
                 * request_rec, so that ->output_filters on 'new' is
Packit 90a5c9
                 * changed (if necessary) when removing the filter. */
Packit 90a5c9
                f->r = new;
Packit 90a5c9
                ap_remove_output_filter(f);
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            f = nextf;
Packit 90a5c9
Packit 90a5c9
            /* Stop at the protocol filters.  If a protocol filter has
Packit 90a5c9
             * been newly installed for this resource, better leave it
Packit 90a5c9
             * in place, though it's probably a misconfiguration or
Packit 90a5c9
             * filter bug to get into this state. */
Packit 90a5c9
        } while (f && f != new->proto_output_filters);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* If this is not a subrequest, clear out all
Packit 90a5c9
         * resource-specific filters. */
Packit 90a5c9
        new->output_filters  = new->proto_output_filters;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    update_r_in_filters(new->input_filters, r, new);
Packit 90a5c9
    update_r_in_filters(new->output_filters, r, new);
Packit 90a5c9
Packit 90a5c9
    apr_table_setn(new->subprocess_env, "REDIRECT_STATUS",
Packit 90a5c9
                   apr_itoa(r->pool, r->status));
Packit 90a5c9
Packit 90a5c9
    /* Begin by presuming any module can make its own path_info assumptions,
Packit 90a5c9
     * until some module interjects and changes the value.
Packit 90a5c9
     */
Packit 90a5c9
    new->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
Packit 90a5c9
Packit 90a5c9
#if APR_HAS_THREADS
Packit 90a5c9
    new->invoke_mtx = r->invoke_mtx;
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * XXX: hmm.  This is because mod_setenvif and mod_unique_id really need
Packit 90a5c9
     * to do their thing on internal redirects as well.  Perhaps this is a
Packit 90a5c9
     * misnamed function.
Packit 90a5c9
     */
Packit 90a5c9
    if ((access_status = ap_run_post_read_request(new))) {
Packit 90a5c9
        ap_die(access_status, new);
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* XXX: Is this function is so bogus and fragile that we deep-6 it? */
Packit 90a5c9
AP_DECLARE(void) ap_internal_fast_redirect(request_rec *rr, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    /* We need to tell POOL_DEBUG that we're guaranteeing that rr->pool
Packit 90a5c9
     * will exist as long as r->pool.  Otherwise we run into troubles because
Packit 90a5c9
     * some values in this request will be allocated in r->pool, and others in
Packit 90a5c9
     * rr->pool.
Packit 90a5c9
     */
Packit 90a5c9
    apr_pool_join(r->pool, rr->pool);
Packit 90a5c9
    r->proxyreq = rr->proxyreq;
Packit 90a5c9
    r->no_cache = (r->no_cache && rr->no_cache);
Packit 90a5c9
    r->no_local_copy = (r->no_local_copy && rr->no_local_copy);
Packit 90a5c9
    r->mtime = rr->mtime;
Packit 90a5c9
    r->uri = rr->uri;
Packit 90a5c9
    r->filename = rr->filename;
Packit 90a5c9
    r->canonical_filename = rr->canonical_filename;
Packit 90a5c9
    r->path_info = rr->path_info;
Packit 90a5c9
    r->args = rr->args;
Packit 90a5c9
    r->finfo = rr->finfo;
Packit 90a5c9
    r->handler = rr->handler;
Packit 90a5c9
    ap_set_content_type(r, rr->content_type);
Packit 90a5c9
    r->content_encoding = rr->content_encoding;
Packit 90a5c9
    r->content_languages = rr->content_languages;
Packit 90a5c9
    r->per_dir_config = rr->per_dir_config;
Packit 90a5c9
    /* copy output headers from subrequest, but leave negotiation headers */
Packit 90a5c9
    r->notes = apr_table_overlay(r->pool, rr->notes, r->notes);
Packit 90a5c9
    r->headers_out = apr_table_overlay(r->pool, rr->headers_out,
Packit 90a5c9
                                       r->headers_out);
Packit 90a5c9
    r->err_headers_out = apr_table_overlay(r->pool, rr->err_headers_out,
Packit 90a5c9
                                           r->err_headers_out);
Packit 90a5c9
    r->trailers_out = apr_table_overlay(r->pool, rr->trailers_out,
Packit 90a5c9
                                           r->trailers_out);
Packit 90a5c9
    r->subprocess_env = apr_table_overlay(r->pool, rr->subprocess_env,
Packit 90a5c9
                                          r->subprocess_env);
Packit 90a5c9
Packit 90a5c9
    r->output_filters = rr->output_filters;
Packit 90a5c9
    r->input_filters = rr->input_filters;
Packit 90a5c9
Packit 90a5c9
    /* If any filters pointed at the now-defunct rr, we must point them
Packit 90a5c9
     * at our "new" instance of r.  In particular, some of rr's structures
Packit 90a5c9
     * will now be bogus (say rr->headers_out).  If a filter tried to modify
Packit 90a5c9
     * their f->r structure when it is pointing to rr, the real request_rec
Packit 90a5c9
     * will not get updated.  Fix that here.
Packit 90a5c9
     */
Packit 90a5c9
    update_r_in_filters(r->input_filters, rr, r);
Packit 90a5c9
    update_r_in_filters(r->output_filters, rr, r);
Packit 90a5c9
Packit 90a5c9
    if (r->main) {
Packit 90a5c9
        ap_filter_t *next = r->output_filters;
Packit 90a5c9
        while (next && (next != r->proto_output_filters)) {
Packit 90a5c9
            if (next->frec == ap_subreq_core_filter_handle) {
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
            next = next->next;
Packit 90a5c9
        }
Packit 90a5c9
        if (!next || next == r->proto_output_filters) {
Packit 90a5c9
            ap_add_output_filter_handle(ap_subreq_core_filter_handle,
Packit 90a5c9
                                        NULL, r, r->connection);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /*
Packit 90a5c9
         * We need to check if we now have the SUBREQ_CORE filter in our filter
Packit 90a5c9
         * chain. If this is the case we need to remove it since we are NO
Packit 90a5c9
         * subrequest. But we need to keep in mind that the SUBREQ_CORE filter
Packit 90a5c9
         * does not necessarily need to be the first filter in our chain. So we
Packit 90a5c9
         * need to go through the chain. But we only need to walk up the chain
Packit 90a5c9
         * until the proto_output_filters as the SUBREQ_CORE filter is below the
Packit 90a5c9
         * protocol filters.
Packit 90a5c9
         */
Packit 90a5c9
        ap_filter_t *next;
Packit 90a5c9
Packit 90a5c9
        next = r->output_filters;
Packit 90a5c9
        while (next && (next->frec != ap_subreq_core_filter_handle)
Packit 90a5c9
               && (next != r->proto_output_filters)) {
Packit 90a5c9
                next = next->next;
Packit 90a5c9
        }
Packit 90a5c9
        if (next && (next->frec == ap_subreq_core_filter_handle)) {
Packit 90a5c9
            ap_remove_output_filter(next);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_internal_redirect(const char *new_uri, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int access_status;
Packit 90a5c9
    request_rec *new = internal_internal_redirect(new_uri, r);
Packit 90a5c9
Packit 90a5c9
    AP_INTERNAL_REDIRECT(r->uri, new_uri);
Packit 90a5c9
Packit 90a5c9
    /* ap_die was already called, if an error occured */
Packit 90a5c9
    if (!new) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    access_status = ap_run_quick_handler(new, 0);  /* Not a look-up request */
Packit 90a5c9
    if (access_status == DECLINED) {
Packit 90a5c9
        access_status = ap_process_request_internal(new);
Packit 90a5c9
        if (access_status == OK) {
Packit 90a5c9
            access_status = ap_invoke_handler(new);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    ap_die(access_status, new);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* This function is designed for things like actions or CGI scripts, when
Packit 90a5c9
 * using AddHandler, and you want to preserve the content type across
Packit 90a5c9
 * an internal redirect.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_internal_redirect_handler(const char *new_uri, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int access_status;
Packit 90a5c9
    request_rec *new = internal_internal_redirect(new_uri, r);
Packit 90a5c9
Packit 90a5c9
    /* ap_die was already called, if an error occured */
Packit 90a5c9
    if (!new) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (r->handler)
Packit 90a5c9
        ap_set_content_type(new, r->content_type);
Packit 90a5c9
    access_status = ap_process_request_internal(new);
Packit 90a5c9
    if (access_status == OK) {
Packit 90a5c9
        access_status = ap_invoke_handler(new);
Packit 90a5c9
    }
Packit 90a5c9
    ap_die(access_status, new);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_allow_methods(request_rec *r, int reset, ...)
Packit 90a5c9
{
Packit 90a5c9
    const char *method;
Packit 90a5c9
    va_list methods;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Get rid of any current settings if requested; not just the
Packit 90a5c9
     * well-known methods but any extensions as well.
Packit 90a5c9
     */
Packit 90a5c9
    if (reset) {
Packit 90a5c9
        ap_clear_method_list(r->allowed_methods);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    va_start(methods, reset);
Packit 90a5c9
    while ((method = va_arg(methods, const char *)) != NULL) {
Packit 90a5c9
        ap_method_list_add(r->allowed_methods, method);
Packit 90a5c9
    }
Packit 90a5c9
    va_end(methods);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_allow_standard_methods(request_rec *r, int reset, ...)
Packit 90a5c9
{
Packit 90a5c9
    int method;
Packit 90a5c9
    va_list methods;
Packit 90a5c9
    apr_int64_t mask;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Get rid of any current settings if requested; not just the
Packit 90a5c9
     * well-known methods but any extensions as well.
Packit 90a5c9
     */
Packit 90a5c9
    if (reset) {
Packit 90a5c9
        ap_clear_method_list(r->allowed_methods);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    mask = 0;
Packit 90a5c9
    va_start(methods, reset);
Packit 90a5c9
    while ((method = va_arg(methods, int)) != -1) {
Packit 90a5c9
        mask |= (AP_METHOD_BIT << method);
Packit 90a5c9
    }
Packit 90a5c9
    va_end(methods);
Packit 90a5c9
Packit 90a5c9
    r->allowed_methods->method_mask |= mask;
Packit 90a5c9
}