Blame server/protocol.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
 * protocol.c --- routines which directly communicate with the client.
Packit 90a5c9
 *
Packit 90a5c9
 * Code originally by Rob McCool; much redone by Robert S. Thau
Packit 90a5c9
 * and the Apache Software Foundation.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_buckets.h"
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
#include "apr_signal.h"
Packit 90a5c9
#include "apr_strmatch.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STDIO          /* for sscanf */
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#define APR_WANT_MEMFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_vhost.h"
Packit 90a5c9
#include "http_log.h"           /* For errors detected in basic auth common
Packit 90a5c9
                                 * support code... */
Packit 90a5c9
#include "mod_core.h"
Packit 90a5c9
#include "util_charset.h"
Packit 90a5c9
#include "util_ebcdic.h"
Packit 90a5c9
#include "scoreboard.h"
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_STDARG_H
Packit 90a5c9
#include <stdarg.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAVE_UNISTD_H
Packit 90a5c9
#include <unistd.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/* we know core's module_index is 0 */
Packit 90a5c9
#undef APLOG_MODULE_INDEX
Packit 90a5c9
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
Packit 90a5c9
Packit 90a5c9
APR_HOOK_STRUCT(
Packit 90a5c9
    APR_HOOK_LINK(pre_read_request)
Packit 90a5c9
    APR_HOOK_LINK(post_read_request)
Packit 90a5c9
    APR_HOOK_LINK(log_transaction)
Packit 90a5c9
    APR_HOOK_LINK(http_scheme)
Packit 90a5c9
    APR_HOOK_LINK(default_port)
Packit 90a5c9
    APR_HOOK_LINK(note_auth_failure)
Packit 90a5c9
    APR_HOOK_LINK(protocol_propose)
Packit 90a5c9
    APR_HOOK_LINK(protocol_switch)
Packit 90a5c9
    APR_HOOK_LINK(protocol_get)
Packit 90a5c9
)
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_DATA ap_filter_rec_t *ap_old_write_func = NULL;
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* Patterns to match in ap_make_content_type() */
Packit 90a5c9
static const char *needcset[] = {
Packit 90a5c9
    "text/plain",
Packit 90a5c9
    "text/html",
Packit 90a5c9
    NULL
Packit 90a5c9
};
Packit 90a5c9
static const apr_strmatch_pattern **needcset_patterns;
Packit 90a5c9
static const apr_strmatch_pattern *charset_pattern;
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool)
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    for (i = 0; needcset[i]; i++) {
Packit 90a5c9
        continue;
Packit 90a5c9
    }
Packit 90a5c9
    needcset_patterns = (const apr_strmatch_pattern **)
Packit 90a5c9
        apr_palloc(pool, (i + 1) * sizeof(apr_strmatch_pattern *));
Packit 90a5c9
    for (i = 0; needcset[i]; i++) {
Packit 90a5c9
        needcset_patterns[i] = apr_strmatch_precompile(pool, needcset[i], 0);
Packit 90a5c9
    }
Packit 90a5c9
    needcset_patterns[i] = NULL;
Packit 90a5c9
    charset_pattern = apr_strmatch_precompile(pool, "charset=", 0);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Builds the content-type that should be sent to the client from the
Packit 90a5c9
 * content-type specified.  The following rules are followed:
Packit 90a5c9
 *    - if type is NULL or "", return NULL (do not set content-type).
Packit 90a5c9
 *    - if charset adding is disabled, stop processing and return type.
Packit 90a5c9
 *    - then, if there are no parameters on type, add the default charset
Packit 90a5c9
 *    - return type
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(const char *)ap_make_content_type(request_rec *r, const char *type)
Packit 90a5c9
{
Packit 90a5c9
    const apr_strmatch_pattern **pcset;
Packit 90a5c9
    core_dir_config *conf =
Packit 90a5c9
        (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
    core_request_config *request_conf;
Packit 90a5c9
    apr_size_t type_len;
Packit 90a5c9
Packit 90a5c9
    if (!type || *type == '\0') {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (conf->add_default_charset != ADD_DEFAULT_CHARSET_ON) {
Packit 90a5c9
        return type;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    request_conf = ap_get_core_module_config(r->request_config);
Packit 90a5c9
    if (request_conf->suppress_charset) {
Packit 90a5c9
        return type;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    type_len = strlen(type);
Packit 90a5c9
Packit 90a5c9
    if (apr_strmatch(charset_pattern, type, type_len) != NULL) {
Packit 90a5c9
        /* already has parameter, do nothing */
Packit 90a5c9
        /* XXX we don't check the validity */
Packit 90a5c9
        ;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* see if it makes sense to add the charset. At present,
Packit 90a5c9
         * we only add it if the Content-type is one of needcset[]
Packit 90a5c9
         */
Packit 90a5c9
        for (pcset = needcset_patterns; *pcset ; pcset++) {
Packit 90a5c9
            if (apr_strmatch(*pcset, type, type_len) != NULL) {
Packit 90a5c9
                struct iovec concat[3];
Packit 90a5c9
                concat[0].iov_base = (void *)type;
Packit 90a5c9
                concat[0].iov_len = type_len;
Packit 90a5c9
                concat[1].iov_base = (void *)"; charset=";
Packit 90a5c9
                concat[1].iov_len = sizeof("; charset=") - 1;
Packit 90a5c9
                concat[2].iov_base = (void *)(conf->add_default_charset_name);
Packit 90a5c9
                concat[2].iov_len = strlen(conf->add_default_charset_name);
Packit 90a5c9
                type = apr_pstrcatv(r->pool, concat, 3, NULL);
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return type;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t clength)
Packit 90a5c9
{
Packit 90a5c9
    r->clength = clength;
Packit 90a5c9
    apr_table_setn(r->headers_out, "Content-Length",
Packit 90a5c9
                   apr_off_t_toa(r->pool, clength));
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Return the latest rational time from a request/mtime (modification time)
Packit 90a5c9
 * pair.  We return the mtime unless it's in the future, in which case we
Packit 90a5c9
 * return the current time.  We use the request time as a reference in order
Packit 90a5c9
 * to limit the number of calls to time().  We don't check for futurosity
Packit 90a5c9
 * unless the mtime is at least as new as the reference.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
Packit 90a5c9
{
Packit 90a5c9
    apr_time_t now;
Packit 90a5c9
Packit 90a5c9
    /* For all static responses, it's almost certain that the file was
Packit 90a5c9
     * last modified before the beginning of the request.  So there's
Packit 90a5c9
     * no reason to call time(NULL) again.  But if the response has been
Packit 90a5c9
     * created on demand, then it might be newer than the time the request
Packit 90a5c9
     * started.  In this event we really have to call time(NULL) again
Packit 90a5c9
     * so that we can give the clients the most accurate Last-Modified.  If we
Packit 90a5c9
     * were given a time in the future, we return the current time - the
Packit 90a5c9
     * Last-Modified can't be in the future.
Packit 90a5c9
     */
Packit 90a5c9
    now = (mtime < r->request_time) ? r->request_time : apr_time_now();
Packit 90a5c9
    return (mtime > now) ? now : mtime;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* Get a line of protocol input, including any continuation lines
Packit 90a5c9
 * caused by MIME folding (or broken clients) if fold != 0, and place it
Packit 90a5c9
 * in the buffer s, of size n bytes, without the ending newline.
Packit 90a5c9
 * 
Packit 90a5c9
 * Pulls from r->proto_input_filters instead of r->input_filters for
Packit 90a5c9
 * stricter protocol adherence and better input filter behavior during
Packit 90a5c9
 * chunked trailer processing (for http).
Packit 90a5c9
 *
Packit 90a5c9
 * If s is NULL, ap_rgetline_core will allocate necessary memory from r->pool.
Packit 90a5c9
 *
Packit 90a5c9
 * Returns APR_SUCCESS if there are no problems and sets *read to be
Packit 90a5c9
 * the full length of s.
Packit 90a5c9
 *
Packit 90a5c9
 * APR_ENOSPC is returned if there is not enough buffer space.
Packit 90a5c9
 * Other errors may be returned on other errors.
Packit 90a5c9
 *
Packit 90a5c9
 * The [CR]LF are *not* returned in the buffer.  Therefore, a *read of 0
Packit 90a5c9
 * indicates that an empty line was read.
Packit 90a5c9
 *
Packit 90a5c9
 * Notes: Because the buffer uses 1 char for NUL, the most we can return is
Packit 90a5c9
 *        (n - 1) actual characters.
Packit 90a5c9
 *
Packit 90a5c9
 *        If no LF is detected on the last line due to a dropped connection
Packit 90a5c9
 *        or a full buffer, that's considered an error.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
Packit 90a5c9
                                          apr_size_t *read, request_rec *r,
Packit 90a5c9
                                          int flags, apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    apr_bucket *e;
Packit 90a5c9
    apr_size_t bytes_handled = 0, current_alloc = 0;
Packit 90a5c9
    char *pos, *last_char = *s;
Packit 90a5c9
    int do_alloc = (*s == NULL), saw_eos = 0;
Packit 90a5c9
    int fold = flags & AP_GETLINE_FOLD;
Packit 90a5c9
    int crlf = flags & AP_GETLINE_CRLF;
Packit 90a5c9
    int nospc_eol = flags & AP_GETLINE_NOSPC_EOL;
Packit 90a5c9
    int saw_eol = 0, saw_nospc = 0;
Packit 90a5c9
Packit 90a5c9
    if (!n) {
Packit 90a5c9
        /* Needs room for NUL byte at least */
Packit 90a5c9
        *read = 0;
Packit 90a5c9
        return APR_BADARG;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Initialize last_char as otherwise a random value will be compared
Packit 90a5c9
     * against APR_ASCII_LF at the end of the loop if bb only contains
Packit 90a5c9
     * zero-length buckets.
Packit 90a5c9
     */
Packit 90a5c9
    if (last_char)
Packit 90a5c9
        *last_char = '\0';
Packit 90a5c9
Packit 90a5c9
    do {
Packit 90a5c9
        apr_brigade_cleanup(bb);
Packit 90a5c9
        rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_GETLINE,
Packit 90a5c9
                            APR_BLOCK_READ, 0);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            goto cleanup;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Something horribly wrong happened.  Someone didn't block! 
Packit 90a5c9
         * (this also happens at the end of each keepalive connection)
Packit 90a5c9
         */
Packit 90a5c9
        if (APR_BRIGADE_EMPTY(bb)) {
Packit 90a5c9
            rv = APR_EGENERAL;
Packit 90a5c9
            goto cleanup;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        for (e = APR_BRIGADE_FIRST(bb);
Packit 90a5c9
             e != APR_BRIGADE_SENTINEL(bb);
Packit 90a5c9
             e = APR_BUCKET_NEXT(e))
Packit 90a5c9
        {
Packit 90a5c9
            const char *str;
Packit 90a5c9
            apr_size_t len;
Packit 90a5c9
Packit 90a5c9
            /* If we see an EOS, don't bother doing anything more. */
Packit 90a5c9
            if (APR_BUCKET_IS_EOS(e)) {
Packit 90a5c9
                saw_eos = 1;
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                goto cleanup;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (len == 0) {
Packit 90a5c9
                /* no use attempting a zero-byte alloc (hurts when
Packit 90a5c9
                 * using --with-efence --enable-pool-debug) or
Packit 90a5c9
                 * doing any of the other logic either
Packit 90a5c9
                 */
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Would this overrun our buffer?  If so, we'll die. */
Packit 90a5c9
            if (n < bytes_handled + len) {
Packit 90a5c9
                /* Before we die, let's fill the buffer up to its limit (i.e.
Packit 90a5c9
                 * fall through with the remaining length, if any), setting
Packit 90a5c9
                 * saw_eol on LF to stop the outer loop appropriately; we may
Packit 90a5c9
                 * come back here once the buffer is filled (no LF seen), and
Packit 90a5c9
                 * either be done at that time or continue to wait for LF here
Packit 90a5c9
                 * if nospc_eol is set.
Packit 90a5c9
                 *
Packit 90a5c9
                 * But there is also a corner cases which we want to address,
Packit 90a5c9
                 * namely if the buffer is overrun by the final LF only (i.e.
Packit 90a5c9
                 * the CR fits in); this is not really an overrun since we'll
Packit 90a5c9
                 * strip the CR finally (and use it for NUL byte), but anyway
Packit 90a5c9
                 * we have to handle the case so that it's not returned to the
Packit 90a5c9
                 * caller as part of the truncated line (it's not!). This is
Packit 90a5c9
                 * easier to consider that LF is out of counting and thus fall
Packit 90a5c9
                 * through with no error (saw_eol is set to 2 so that we later
Packit 90a5c9
                 * ignore LF handling already done here), while folding and
Packit 90a5c9
                 * nospc_eol logics continue to work (or fail) appropriately.
Packit 90a5c9
                 */
Packit 90a5c9
                saw_eol = (str[len - 1] == APR_ASCII_LF);
Packit 90a5c9
                if (/* First time around */
Packit 90a5c9
                    saw_eol && !saw_nospc
Packit 90a5c9
                    /*  Single LF completing the buffered CR, */
Packit 90a5c9
                    && ((len == 1 && ((*s)[bytes_handled - 1] == APR_ASCII_CR))
Packit 90a5c9
                    /*  or trailing CRLF overuns by LF only */
Packit 90a5c9
                        || (len > 1 && str[len - 2] == APR_ASCII_CR
Packit 90a5c9
                            && n - bytes_handled + 1 == len))) {
Packit 90a5c9
                    /* In both cases *last_char is (to be) the CR stripped by
Packit 90a5c9
                     * later 'bytes_handled = last_char - *s'.
Packit 90a5c9
                     */
Packit 90a5c9
                    saw_eol = 2;
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    /* In any other case we'd lose data. */
Packit 90a5c9
                    rv = APR_ENOSPC;
Packit 90a5c9
                    saw_nospc = 1;
Packit 90a5c9
                }
Packit 90a5c9
                len = n - bytes_handled;
Packit 90a5c9
                if (!len) {
Packit 90a5c9
                    if (saw_eol) {
Packit 90a5c9
                        break;
Packit 90a5c9
                    }
Packit 90a5c9
                    if (nospc_eol) {
Packit 90a5c9
                        continue;
Packit 90a5c9
                    }
Packit 90a5c9
                    goto cleanup;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Do we have to handle the allocation ourselves? */
Packit 90a5c9
            if (do_alloc) {
Packit 90a5c9
                /* We'll assume the common case where one bucket is enough. */
Packit 90a5c9
                if (!*s) {
Packit 90a5c9
                    current_alloc = len;
Packit 90a5c9
                    *s = apr_palloc(r->pool, current_alloc + 1);
Packit 90a5c9
                }
Packit 90a5c9
                else if (bytes_handled + len > current_alloc) {
Packit 90a5c9
                    /* Increase the buffer size */
Packit 90a5c9
                    apr_size_t new_size = current_alloc * 2;
Packit 90a5c9
                    char *new_buffer;
Packit 90a5c9
Packit 90a5c9
                    if (bytes_handled + len > new_size) {
Packit 90a5c9
                        new_size = (bytes_handled + len) * 2;
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    new_buffer = apr_palloc(r->pool, new_size + 1);
Packit 90a5c9
Packit 90a5c9
                    /* Copy what we already had. */
Packit 90a5c9
                    memcpy(new_buffer, *s, bytes_handled);
Packit 90a5c9
                    current_alloc = new_size;
Packit 90a5c9
                    *s = new_buffer;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Just copy the rest of the data to the end of the old buffer. */
Packit 90a5c9
            pos = *s + bytes_handled;
Packit 90a5c9
            memcpy(pos, str, len);
Packit 90a5c9
            last_char = pos + len - 1;
Packit 90a5c9
Packit 90a5c9
            /* We've now processed that new data - update accordingly. */
Packit 90a5c9
            bytes_handled += len;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* If we got a full line of input, stop reading */
Packit 90a5c9
        if (last_char && (*last_char == APR_ASCII_LF)) {
Packit 90a5c9
            saw_eol = 1;
Packit 90a5c9
        }
Packit 90a5c9
    } while (!saw_eol);
Packit 90a5c9
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        /* End of line after APR_ENOSPC above */
Packit 90a5c9
        goto cleanup;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Now terminate the string at the end of the line;
Packit 90a5c9
     * if the last-but-one character is a CR, terminate there.
Packit 90a5c9
     * LF is handled above (not accounted) when saw_eol == 2,
Packit 90a5c9
     * the last char is CR to terminate at still.
Packit 90a5c9
     */
Packit 90a5c9
    if (saw_eol < 2) {
Packit 90a5c9
        if (last_char > *s && last_char[-1] == APR_ASCII_CR) {
Packit 90a5c9
            last_char--;
Packit 90a5c9
        }
Packit 90a5c9
        else if (crlf) {
Packit 90a5c9
            rv = APR_EINVAL;
Packit 90a5c9
            goto cleanup;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    bytes_handled = last_char - *s;
Packit 90a5c9
Packit 90a5c9
    /* If we're folding, we have more work to do.
Packit 90a5c9
     *
Packit 90a5c9
     * Note that if an EOS was seen, we know we can't have another line.
Packit 90a5c9
     */
Packit 90a5c9
    if (fold && bytes_handled && !saw_eos) {
Packit 90a5c9
        for (;;) {
Packit 90a5c9
            const char *str;
Packit 90a5c9
            apr_size_t len;
Packit 90a5c9
            char c;
Packit 90a5c9
Packit 90a5c9
            /* Clear the temp brigade for this filter read. */
Packit 90a5c9
            apr_brigade_cleanup(bb);
Packit 90a5c9
Packit 90a5c9
            /* We only care about the first byte. */
Packit 90a5c9
            rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_SPECULATIVE,
Packit 90a5c9
                                APR_BLOCK_READ, 1);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                goto cleanup;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (APR_BRIGADE_EMPTY(bb)) {
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            e = APR_BRIGADE_FIRST(bb);
Packit 90a5c9
Packit 90a5c9
            /* If we see an EOS, don't bother doing anything more. */
Packit 90a5c9
            if (APR_BUCKET_IS_EOS(e)) {
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
Packit 90a5c9
            if (rv != APR_SUCCESS) {
Packit 90a5c9
                apr_brigade_cleanup(bb);
Packit 90a5c9
                goto cleanup;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Found one, so call ourselves again to get the next line.
Packit 90a5c9
             *
Packit 90a5c9
             * FIXME: If the folding line is completely blank, should we
Packit 90a5c9
             * stop folding?  Does that require also looking at the next
Packit 90a5c9
             * char?
Packit 90a5c9
             */
Packit 90a5c9
            /* When we call destroy, the buckets are deleted, so save that
Packit 90a5c9
             * one character we need.  This simplifies our execution paths
Packit 90a5c9
             * at the cost of one character read.
Packit 90a5c9
             */
Packit 90a5c9
            c = *str;
Packit 90a5c9
            if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {
Packit 90a5c9
                /* Do we have enough space? We may be full now. */
Packit 90a5c9
                if (bytes_handled >= n) {
Packit 90a5c9
                    rv = APR_ENOSPC;
Packit 90a5c9
                    goto cleanup;
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    apr_size_t next_size, next_len;
Packit 90a5c9
                    char *tmp;
Packit 90a5c9
Packit 90a5c9
                    /* If we're doing the allocations for them, we have to
Packit 90a5c9
                     * give ourselves a NULL and copy it on return.
Packit 90a5c9
                     */
Packit 90a5c9
                    if (do_alloc) {
Packit 90a5c9
                        tmp = NULL;
Packit 90a5c9
                    }
Packit 90a5c9
                    else {
Packit 90a5c9
                        tmp = last_char;
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    next_size = n - bytes_handled;
Packit 90a5c9
Packit 90a5c9
                    rv = ap_rgetline_core(&tmp, next_size, &next_len, r,
Packit 90a5c9
                                          flags & ~AP_GETLINE_FOLD, bb);
Packit 90a5c9
                    if (rv != APR_SUCCESS) {
Packit 90a5c9
                        goto cleanup;
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    if (do_alloc && next_len > 0) {
Packit 90a5c9
                        char *new_buffer;
Packit 90a5c9
                        apr_size_t new_size = bytes_handled + next_len + 1;
Packit 90a5c9
Packit 90a5c9
                        /* we need to alloc an extra byte for a null */
Packit 90a5c9
                        new_buffer = apr_palloc(r->pool, new_size);
Packit 90a5c9
Packit 90a5c9
                        /* Copy what we already had. */
Packit 90a5c9
                        memcpy(new_buffer, *s, bytes_handled);
Packit 90a5c9
Packit 90a5c9
                        /* copy the new line, including the trailing null */
Packit 90a5c9
                        memcpy(new_buffer + bytes_handled, tmp, next_len);
Packit 90a5c9
                        *s = new_buffer;
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    last_char += next_len;
Packit 90a5c9
                    bytes_handled += next_len;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else { /* next character is not tab or space */
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
cleanup:
Packit 90a5c9
    if (bytes_handled >= n) {
Packit 90a5c9
        bytes_handled = n - 1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    *read = bytes_handled;
Packit 90a5c9
    if (*s) {
Packit 90a5c9
        /* ensure the string is NUL terminated */
Packit 90a5c9
        (*s)[*read] = '\0';
Packit 90a5c9
Packit 90a5c9
        /* PR#43039: We shouldn't accept NULL bytes within the line */
Packit 90a5c9
        bytes_handled = strlen(*s);
Packit 90a5c9
        if (bytes_handled < *read) {
Packit 90a5c9
            *read = bytes_handled;
Packit 90a5c9
            if (rv == APR_SUCCESS) {
Packit 90a5c9
                rv = APR_EINVAL;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#if APR_CHARSET_EBCDIC
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
Packit 90a5c9
                                     apr_size_t *read, request_rec *r,
Packit 90a5c9
                                     int fold, apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    /* on ASCII boxes, ap_rgetline is a macro which simply invokes
Packit 90a5c9
     * ap_rgetline_core with the same parms
Packit 90a5c9
     *
Packit 90a5c9
     * on EBCDIC boxes, each complete http protocol input line needs to be
Packit 90a5c9
     * translated into the code page used by the compiler.  Since
Packit 90a5c9
     * ap_rgetline_core uses recursion, we do the translation in a wrapper
Packit 90a5c9
     * function to ensure that each input character gets translated only once.
Packit 90a5c9
     */
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    rv = ap_rgetline_core(s, n, read, r, fold, bb);
Packit 90a5c9
    if (rv == APR_SUCCESS || APR_STATUS_IS_ENOSPC(rv)) {
Packit 90a5c9
        ap_xlate_proto_from_ascii(*s, *read);
Packit 90a5c9
    }
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int flags)
Packit 90a5c9
{
Packit 90a5c9
    char *tmp_s = s;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    apr_size_t len;
Packit 90a5c9
    apr_bucket_brigade *tmp_bb;
Packit 90a5c9
Packit 90a5c9
    if (n < 1) {
Packit 90a5c9
        /* Can't work since we always NUL terminate */
Packit 90a5c9
        return -1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
Packit 90a5c9
    rv = ap_rgetline(&tmp_s, n, &len, r, flags, tmp_bb);
Packit 90a5c9
    apr_brigade_destroy(tmp_bb);
Packit 90a5c9
Packit 90a5c9
    /* Map the out-of-space condition to the old API. */
Packit 90a5c9
    if (rv == APR_ENOSPC) {
Packit 90a5c9
        return n;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Anything else is just bad. */
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return -1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return (int)len;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* parse_uri: break apart the uri
Packit 90a5c9
 * Side Effects:
Packit 90a5c9
 * - sets r->args to rest after '?' (or NULL if no '?')
Packit 90a5c9
 * - sets r->uri to request uri (without r->args part)
Packit 90a5c9
 * - sets r->hostname (if not set already) from request (scheme://host:port)
Packit 90a5c9
 */
Packit 90a5c9
AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri)
Packit 90a5c9
{
Packit 90a5c9
    int status = HTTP_OK;
Packit 90a5c9
Packit 90a5c9
    r->unparsed_uri = apr_pstrdup(r->pool, uri);
Packit 90a5c9
Packit 90a5c9
    /* http://issues.apache.org/bugzilla/show_bug.cgi?id=31875
Packit 90a5c9
     * http://issues.apache.org/bugzilla/show_bug.cgi?id=28450
Packit 90a5c9
     *
Packit 90a5c9
     * This is not in fact a URI, it's a path.  That matters in the
Packit 90a5c9
     * case of a leading double-slash.  We need to resolve the issue
Packit 90a5c9
     * by normalizing that out before treating it as a URI.
Packit 90a5c9
     */
Packit 90a5c9
    while ((uri[0] == '/') && (uri[1] == '/')) {
Packit 90a5c9
        ++uri ;
Packit 90a5c9
    }
Packit 90a5c9
    if (r->method_number == M_CONNECT) {
Packit 90a5c9
        status = apr_uri_parse_hostinfo(r->pool, uri, &r->parsed_uri);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        status = apr_uri_parse(r->pool, uri, &r->parsed_uri);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (status == APR_SUCCESS) {
Packit 90a5c9
        /* if it has a scheme we may need to do absoluteURI vhost stuff */
Packit 90a5c9
        if (r->parsed_uri.scheme
Packit 90a5c9
            && !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))) {
Packit 90a5c9
            r->hostname = r->parsed_uri.hostname;
Packit 90a5c9
        }
Packit 90a5c9
        else if (r->method_number == M_CONNECT) {
Packit 90a5c9
            r->hostname = r->parsed_uri.hostname;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        r->args = r->parsed_uri.query;
Packit 90a5c9
        r->uri = r->parsed_uri.path ? r->parsed_uri.path
Packit 90a5c9
                 : apr_pstrdup(r->pool, "/");
Packit 90a5c9
Packit 90a5c9
#if defined(OS2) || defined(WIN32)
Packit 90a5c9
        /* Handle path translations for OS/2 and plug security hole.
Packit 90a5c9
         * This will prevent "http://www.wherever.com/..\..\/" from
Packit 90a5c9
         * returning a directory for the root drive.
Packit 90a5c9
         */
Packit 90a5c9
        {
Packit 90a5c9
            char *x;
Packit 90a5c9
Packit 90a5c9
            for (x = r->uri; (x = strchr(x, '\\')) != NULL; )
Packit 90a5c9
                *x = '/';
Packit 90a5c9
        }
Packit 90a5c9
#endif /* OS2 || WIN32 */
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        r->args = NULL;
Packit 90a5c9
        r->hostname = NULL;
Packit 90a5c9
        r->status = HTTP_BAD_REQUEST;             /* set error status */
Packit 90a5c9
        r->uri = apr_pstrdup(r->pool, uri);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* get the length of the field name for logging, but no more than 80 bytes */
Packit 90a5c9
#define LOG_NAME_MAX_LEN 80
Packit 90a5c9
static int field_name_len(const char *field)
Packit 90a5c9
{
Packit 90a5c9
    const char *end = ap_strchr_c(field, ':');
Packit 90a5c9
    if (end == NULL || end - field > LOG_NAME_MAX_LEN)
Packit 90a5c9
        return LOG_NAME_MAX_LEN;
Packit 90a5c9
    return end - field;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    enum {
Packit 90a5c9
        rrl_none, rrl_badmethod, rrl_badwhitespace, rrl_excesswhitespace,
Packit 90a5c9
        rrl_missinguri, rrl_baduri, rrl_badprotocol, rrl_trailingtext,
Packit 90a5c9
        rrl_badmethod09, rrl_reject09
Packit 90a5c9
    } deferred_error = rrl_none;
Packit 90a5c9
    char *ll;
Packit 90a5c9
    char *uri;
Packit 90a5c9
    apr_size_t len;
Packit 90a5c9
    int num_blank_lines = DEFAULT_LIMIT_BLANK_LINES;
Packit 90a5c9
    core_server_config *conf = ap_get_core_module_config(r->server->module_config);
Packit 90a5c9
    int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
Packit 90a5c9
Packit 90a5c9
    /* Read past empty lines until we get a real request line,
Packit 90a5c9
     * a read error, the connection closes (EOF), or we timeout.
Packit 90a5c9
     *
Packit 90a5c9
     * We skip empty lines because browsers have to tack a CRLF on to the end
Packit 90a5c9
     * of POSTs to support old CERN webservers.  But note that we may not
Packit 90a5c9
     * have flushed any previous response completely to the client yet.
Packit 90a5c9
     * We delay the flush as long as possible so that we can improve
Packit 90a5c9
     * performance for clients that are pipelining requests.  If a request
Packit 90a5c9
     * is pipelined then we won't block during the (implicit) read() below.
Packit 90a5c9
     * If the requests aren't pipelined, then the client is still waiting
Packit 90a5c9
     * for the final buffer flush from us, and we will block in the implicit
Packit 90a5c9
     * read().  B_SAFEREAD ensures that the BUFF layer flushes if it will
Packit 90a5c9
     * have to block during a read.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    do {
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
        /* ensure ap_rgetline allocates memory each time thru the loop
Packit 90a5c9
         * if there are empty lines
Packit 90a5c9
         */
Packit 90a5c9
        r->the_request = NULL;
Packit 90a5c9
        rv = ap_rgetline(&(r->the_request), (apr_size_t)(r->server->limit_req_line + 2),
Packit 90a5c9
                         &len, r, strict ? AP_GETLINE_CRLF : 0, bb);
Packit 90a5c9
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            r->request_time = apr_time_now();
Packit 90a5c9
Packit 90a5c9
            /* ap_rgetline returns APR_ENOSPC if it fills up the
Packit 90a5c9
             * buffer before finding the end-of-line.  This is only going to
Packit 90a5c9
             * happen if it exceeds the configured limit for a request-line.
Packit 90a5c9
             */
Packit 90a5c9
            if (APR_STATUS_IS_ENOSPC(rv)) {
Packit 90a5c9
                r->status = HTTP_REQUEST_URI_TOO_LARGE;
Packit 90a5c9
            }
Packit 90a5c9
            else if (APR_STATUS_IS_TIMEUP(rv)) {
Packit 90a5c9
                r->status = HTTP_REQUEST_TIME_OUT;
Packit 90a5c9
            }
Packit 90a5c9
            else if (APR_STATUS_IS_EINVAL(rv)) {
Packit 90a5c9
                r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
            }
Packit 90a5c9
            r->proto_num = HTTP_VERSION(1,0);
Packit 90a5c9
            r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
Packit 90a5c9
            return 0;
Packit 90a5c9
        }
Packit 90a5c9
    } while ((len <= 0) && (--num_blank_lines >= 0));
Packit 90a5c9
Packit 90a5c9
    if (APLOGrtrace5(r)) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r,
Packit 90a5c9
                      "Request received from client: %s",
Packit 90a5c9
                      ap_escape_logitem(r->pool, r->the_request));
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    r->request_time = apr_time_now();
Packit 90a5c9
Packit 90a5c9
    r->method = r->the_request;
Packit 90a5c9
Packit 90a5c9
    /* If there is whitespace before a method, skip it and mark in error */
Packit 90a5c9
    if (apr_isspace(*r->method)) {
Packit 90a5c9
        deferred_error = rrl_badwhitespace; 
Packit 90a5c9
        for ( ; apr_isspace(*r->method); ++r->method)
Packit 90a5c9
            ; 
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Scan the method up to the next whitespace, ensure it contains only
Packit 90a5c9
     * valid http-token characters, otherwise mark in error
Packit 90a5c9
     */
Packit 90a5c9
    if (strict) {
Packit 90a5c9
        ll = (char*) ap_scan_http_token(r->method);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ll = (char*) ap_scan_vchar_obstext(r->method);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (((ll == r->method) || (*ll && !apr_isspace(*ll)))
Packit 90a5c9
            && deferred_error == rrl_none) {
Packit 90a5c9
        deferred_error = rrl_badmethod;
Packit 90a5c9
        ll = strpbrk(ll, "\t\n\v\f\r ");
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Verify method terminated with a single SP, or mark as specific error */
Packit 90a5c9
    if (!ll) {
Packit 90a5c9
        if (deferred_error == rrl_none)
Packit 90a5c9
            deferred_error = rrl_missinguri;
Packit 90a5c9
        r->protocol = uri = "";
Packit 90a5c9
        len = 0;
Packit 90a5c9
        goto rrl_done;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strict && ll[0] && apr_isspace(ll[1])
Packit 90a5c9
             && deferred_error == rrl_none) {
Packit 90a5c9
        deferred_error = rrl_excesswhitespace; 
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Advance uri pointer over leading whitespace, NUL terminate the method
Packit 90a5c9
     * If non-SP whitespace is encountered, mark as specific error
Packit 90a5c9
     */
Packit 90a5c9
    for (uri = ll; apr_isspace(*uri); ++uri) 
Packit 90a5c9
        if (*uri != ' ' && deferred_error == rrl_none)
Packit 90a5c9
            deferred_error = rrl_badwhitespace; 
Packit 90a5c9
    *ll = '\0';
Packit 90a5c9
Packit 90a5c9
    if (!*uri && deferred_error == rrl_none)
Packit 90a5c9
        deferred_error = rrl_missinguri;
Packit 90a5c9
Packit 90a5c9
    /* Scan the URI up to the next whitespace, ensure it contains no raw
Packit 90a5c9
     * control characters, otherwise mark in error
Packit 90a5c9
     */
Packit 90a5c9
    ll = (char*) ap_scan_vchar_obstext(uri);
Packit 90a5c9
    if (ll == uri || (*ll && !apr_isspace(*ll))) {
Packit 90a5c9
        deferred_error = rrl_baduri;
Packit 90a5c9
        ll = strpbrk(ll, "\t\n\v\f\r ");
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Verify URI terminated with a single SP, or mark as specific error */
Packit 90a5c9
    if (!ll) {
Packit 90a5c9
        r->protocol = "";
Packit 90a5c9
        len = 0;
Packit 90a5c9
        goto rrl_done;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strict && ll[0] && apr_isspace(ll[1])
Packit 90a5c9
             && deferred_error == rrl_none) {
Packit 90a5c9
        deferred_error = rrl_excesswhitespace; 
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Advance protocol pointer over leading whitespace, NUL terminate the uri
Packit 90a5c9
     * If non-SP whitespace is encountered, mark as specific error
Packit 90a5c9
     */
Packit 90a5c9
    for (r->protocol = ll; apr_isspace(*r->protocol); ++r->protocol) 
Packit 90a5c9
        if (*r->protocol != ' ' && deferred_error == rrl_none)
Packit 90a5c9
            deferred_error = rrl_badwhitespace; 
Packit 90a5c9
    *ll = '\0';
Packit 90a5c9
Packit 90a5c9
    /* Scan the protocol up to the next whitespace, validation comes later */
Packit 90a5c9
    if (!(ll = (char*) ap_scan_vchar_obstext(r->protocol))) {
Packit 90a5c9
        len = strlen(r->protocol);
Packit 90a5c9
        goto rrl_done;
Packit 90a5c9
    }
Packit 90a5c9
    len = ll - r->protocol;
Packit 90a5c9
Packit 90a5c9
    /* Advance over trailing whitespace, if found mark in error,
Packit 90a5c9
     * determine if trailing text is found, unconditionally mark in error,
Packit 90a5c9
     * finally NUL terminate the protocol string
Packit 90a5c9
     */
Packit 90a5c9
    if (*ll && !apr_isspace(*ll)) {
Packit 90a5c9
        deferred_error = rrl_badprotocol;
Packit 90a5c9
    }
Packit 90a5c9
    else if (strict && *ll) {
Packit 90a5c9
        deferred_error = rrl_excesswhitespace;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        for ( ; apr_isspace(*ll); ++ll)
Packit 90a5c9
            if (*ll != ' ' && deferred_error == rrl_none)
Packit 90a5c9
                deferred_error = rrl_badwhitespace; 
Packit 90a5c9
        if (*ll && deferred_error == rrl_none)
Packit 90a5c9
            deferred_error = rrl_trailingtext;
Packit 90a5c9
    }
Packit 90a5c9
    *((char *)r->protocol + len) = '\0';
Packit 90a5c9
Packit 90a5c9
rrl_done:
Packit 90a5c9
    /* For internal integrity and palloc efficiency, reconstruct the_request
Packit 90a5c9
     * in one palloc, using only single SP characters, per spec.
Packit 90a5c9
     */
Packit 90a5c9
    r->the_request = apr_pstrcat(r->pool, r->method, *uri ? " " : NULL, uri,
Packit 90a5c9
                                 *r->protocol ? " " : NULL, r->protocol, NULL);
Packit 90a5c9
Packit 90a5c9
    if (len == 8
Packit 90a5c9
            && r->protocol[0] == 'H' && r->protocol[1] == 'T'
Packit 90a5c9
            && r->protocol[2] == 'T' && r->protocol[3] == 'P'
Packit 90a5c9
            && r->protocol[4] == '/' && apr_isdigit(r->protocol[5])
Packit 90a5c9
            && r->protocol[6] == '.' && apr_isdigit(r->protocol[7])
Packit 90a5c9
            && r->protocol[5] != '0') {
Packit 90a5c9
        r->assbackwards = 0;
Packit 90a5c9
        r->proto_num = HTTP_VERSION(r->protocol[5] - '0', r->protocol[7] - '0');
Packit 90a5c9
    }
Packit 90a5c9
    else if (len == 8
Packit 90a5c9
                 && (r->protocol[0] == 'H' || r->protocol[0] == 'h')
Packit 90a5c9
                 && (r->protocol[1] == 'T' || r->protocol[1] == 't')
Packit 90a5c9
                 && (r->protocol[2] == 'T' || r->protocol[2] == 't')
Packit 90a5c9
                 && (r->protocol[3] == 'P' || r->protocol[3] == 'p')
Packit 90a5c9
                 && r->protocol[4] == '/' && apr_isdigit(r->protocol[5])
Packit 90a5c9
                 && r->protocol[6] == '.' && apr_isdigit(r->protocol[7])
Packit 90a5c9
                 && r->protocol[5] != '0') {
Packit 90a5c9
        r->assbackwards = 0;
Packit 90a5c9
        r->proto_num = HTTP_VERSION(r->protocol[5] - '0', r->protocol[7] - '0');
Packit 90a5c9
        if (strict && deferred_error == rrl_none)
Packit 90a5c9
            deferred_error = rrl_badprotocol;
Packit 90a5c9
        else
Packit 90a5c9
            memcpy((char*)r->protocol, "HTTP", 4);
Packit 90a5c9
    }
Packit 90a5c9
    else if (r->protocol[0]) {
Packit 90a5c9
        r->proto_num = HTTP_VERSION(0, 9);
Packit 90a5c9
        /* Defer setting the r->protocol string till error msg is composed */
Packit 90a5c9
        if (deferred_error == rrl_none)
Packit 90a5c9
            deferred_error = rrl_badprotocol;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        r->assbackwards = 1;
Packit 90a5c9
        r->protocol  = apr_pstrdup(r->pool, "HTTP/0.9");
Packit 90a5c9
        r->proto_num = HTTP_VERSION(0, 9);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Determine the method_number and parse the uri prior to invoking error
Packit 90a5c9
     * handling, such that these fields are available for substitution
Packit 90a5c9
     */
Packit 90a5c9
    r->method_number = ap_method_number_of(r->method);
Packit 90a5c9
    if (r->method_number == M_GET && r->method[0] == 'H')
Packit 90a5c9
        r->header_only = 1;
Packit 90a5c9
Packit 90a5c9
    ap_parse_uri(r, uri);
Packit 90a5c9
Packit 90a5c9
    /* With the request understood, we can consider HTTP/0.9 specific errors */
Packit 90a5c9
    if (r->proto_num == HTTP_VERSION(0, 9) && deferred_error == rrl_none) {
Packit 90a5c9
        if (conf->http09_enable == AP_HTTP09_DISABLE)
Packit 90a5c9
            deferred_error = rrl_reject09;
Packit 90a5c9
        else if (strict && (r->method_number != M_GET || r->header_only))
Packit 90a5c9
            deferred_error = rrl_badmethod09;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Now that the method, uri and protocol are all processed,
Packit 90a5c9
     * we can safely resume any deferred error reporting
Packit 90a5c9
     */
Packit 90a5c9
    if (deferred_error != rrl_none) {
Packit 90a5c9
        if (deferred_error == rrl_badmethod)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03445)
Packit 90a5c9
                          "HTTP Request Line; Invalid method token: '%.*s'",
Packit 90a5c9
                          field_name_len(r->method), r->method);
Packit 90a5c9
        else if (deferred_error == rrl_badmethod09)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03444)
Packit 90a5c9
                          "HTTP Request Line; Invalid method token: '%.*s'"
Packit 90a5c9
                          " (only GET is allowed for HTTP/0.9 requests)",
Packit 90a5c9
                          field_name_len(r->method), r->method);
Packit 90a5c9
        else if (deferred_error == rrl_missinguri)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03446)
Packit 90a5c9
                          "HTTP Request Line; Missing URI");
Packit 90a5c9
        else if (deferred_error == rrl_baduri)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03454)
Packit 90a5c9
                          "HTTP Request Line; URI incorrectly encoded: '%.*s'",
Packit 90a5c9
                          field_name_len(r->unparsed_uri), r->unparsed_uri);
Packit 90a5c9
        else if (deferred_error == rrl_badwhitespace)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03447)
Packit 90a5c9
                          "HTTP Request Line; Invalid whitespace");
Packit 90a5c9
        else if (deferred_error == rrl_excesswhitespace)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03448)
Packit 90a5c9
                          "HTTP Request Line; Excess whitespace "
Packit 90a5c9
                          "(disallowed by HttpProtocolOptions Strict");
Packit 90a5c9
        else if (deferred_error == rrl_trailingtext)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03449)
Packit 90a5c9
                          "HTTP Request Line; Extraneous text found '%.*s' "
Packit 90a5c9
                          "(perhaps whitespace was injected?)",
Packit 90a5c9
                          field_name_len(ll), ll);
Packit 90a5c9
        else if (deferred_error == rrl_reject09)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02401)
Packit 90a5c9
                          "HTTP Request Line; Rejected HTTP/0.9 request");
Packit 90a5c9
        else if (deferred_error == rrl_badprotocol)
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02418)
Packit 90a5c9
                          "HTTP Request Line; Unrecognized protocol '%.*s' "
Packit 90a5c9
                          "(perhaps whitespace was injected?)",
Packit 90a5c9
                          field_name_len(r->protocol), r->protocol);
Packit 90a5c9
        r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
        goto rrl_failed;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (conf->http_methods == AP_HTTP_METHODS_REGISTERED
Packit 90a5c9
            && r->method_number == M_INVALID) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02423)
Packit 90a5c9
                      "HTTP Request Line; Unrecognized HTTP method: '%.*s' "
Packit 90a5c9
                      "(disallowed by RegisteredMethods)",
Packit 90a5c9
                      field_name_len(r->method), r->method);
Packit 90a5c9
        r->status = HTTP_NOT_IMPLEMENTED;
Packit 90a5c9
        /* This can't happen in an HTTP/0.9 request, we verified GET above */
Packit 90a5c9
        return 0;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (r->status != HTTP_OK) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03450)
Packit 90a5c9
                      "HTTP Request Line; Unable to parse URI: '%.*s'",
Packit 90a5c9
                      field_name_len(r->uri), r->uri);
Packit 90a5c9
        goto rrl_failed;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (strict) {
Packit 90a5c9
        if (r->parsed_uri.fragment) {
Packit 90a5c9
            /* RFC3986 3.5: no fragment */
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02421)
Packit 90a5c9
                          "HTTP Request Line; URI must not contain a fragment");
Packit 90a5c9
            r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
            goto rrl_failed;
Packit 90a5c9
        }
Packit 90a5c9
        if (r->parsed_uri.user || r->parsed_uri.password) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02422)
Packit 90a5c9
                          "HTTP Request Line; URI must not contain a "
Packit 90a5c9
                          "username/password");
Packit 90a5c9
            r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
            goto rrl_failed;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return 1;
Packit 90a5c9
Packit 90a5c9
rrl_failed:
Packit 90a5c9
    if (r->proto_num == HTTP_VERSION(0, 9)) {
Packit 90a5c9
        /* Send all parsing and protocol error response with 1.x behavior,
Packit 90a5c9
         * and reserve 505 errors for actual HTTP protocols presented.
Packit 90a5c9
         * As called out in RFC7230 3.5, any errors parsing the protocol
Packit 90a5c9
         * from the request line are nearly always misencoded HTTP/1.x
Packit 90a5c9
         * requests. Only a valid 0.9 request with no parsing errors
Packit 90a5c9
         * at all may be treated as a simple request, if allowed.
Packit 90a5c9
         */
Packit 90a5c9
        r->assbackwards = 0;
Packit 90a5c9
        r->connection->keepalive = AP_CONN_CLOSE;
Packit 90a5c9
        r->proto_num = HTTP_VERSION(1, 0);
Packit 90a5c9
        r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
Packit 90a5c9
    }
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int table_do_fn_check_lengths(void *r_, const char *key,
Packit 90a5c9
                                     const char *value)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r = r_;
Packit 90a5c9
    if (value == NULL || r->server->limit_req_fieldsize >= strlen(value) )
Packit 90a5c9
        return 1;
Packit 90a5c9
Packit 90a5c9
    r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
    apr_table_setn(r->notes, "error-notes",
Packit 90a5c9
                   "Size of a request header field exceeds server limit.");
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00560) "Request "
Packit 90a5c9
                  "header exceeds LimitRequestFieldSize after merging: %.*s",
Packit 90a5c9
                  field_name_len(key), key);
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    char *last_field = NULL;
Packit 90a5c9
    apr_size_t last_len = 0;
Packit 90a5c9
    apr_size_t alloc_len = 0;
Packit 90a5c9
    char *field;
Packit 90a5c9
    char *value;
Packit 90a5c9
    apr_size_t len;
Packit 90a5c9
    int fields_read = 0;
Packit 90a5c9
    char *tmp_field;
Packit 90a5c9
    core_server_config *conf = ap_get_core_module_config(r->server->module_config);
Packit 90a5c9
    int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Read header lines until we get the empty separator line, a read error,
Packit 90a5c9
     * the connection closes (EOF), reach the server limit, or we timeout.
Packit 90a5c9
     */
Packit 90a5c9
    while(1) {
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
        field = NULL;
Packit 90a5c9
        rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2,
Packit 90a5c9
                         &len, r, strict ? AP_GETLINE_CRLF : 0, bb);
Packit 90a5c9
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            if (APR_STATUS_IS_TIMEUP(rv)) {
Packit 90a5c9
                r->status = HTTP_REQUEST_TIME_OUT;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, 
Packit 90a5c9
                              "Failed to read request header line %s", field);
Packit 90a5c9
                r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before
Packit 90a5c9
             * finding the end-of-line.  This is only going to happen if it
Packit 90a5c9
             * exceeds the configured limit for a field size.
Packit 90a5c9
             */
Packit 90a5c9
            if (rv == APR_ENOSPC) {
Packit 90a5c9
                apr_table_setn(r->notes, "error-notes",
Packit 90a5c9
                               "Size of a request header field "
Packit 90a5c9
                               "exceeds server limit.");
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00561)
Packit 90a5c9
                              "Request header exceeds LimitRequestFieldSize%s"
Packit 90a5c9
                              "%.*s",
Packit 90a5c9
                              (field && *field) ? ": " : "",
Packit 90a5c9
                              (field) ? field_name_len(field) : 0,
Packit 90a5c9
                              (field) ? field : "");
Packit 90a5c9
            }
Packit 90a5c9
            return;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* For all header values, and all obs-fold lines, the presence of
Packit 90a5c9
         * additional whitespace is a no-op, so collapse trailing whitespace
Packit 90a5c9
         * to save buffer allocation and optimize copy operations.
Packit 90a5c9
         * Do not remove the last single whitespace under any condition.
Packit 90a5c9
         */
Packit 90a5c9
        while (len > 1 && (field[len-1] == '\t' || field[len-1] == ' ')) {
Packit 90a5c9
            field[--len] = '\0';
Packit 90a5c9
        } 
Packit 90a5c9
Packit 90a5c9
        if (*field == '\t' || *field == ' ') {
Packit 90a5c9
Packit 90a5c9
            /* Append any newly-read obs-fold line onto the preceding
Packit 90a5c9
             * last_field line we are processing
Packit 90a5c9
             */
Packit 90a5c9
            apr_size_t fold_len;
Packit 90a5c9
Packit 90a5c9
            if (last_field == NULL) {
Packit 90a5c9
                r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03442)
Packit 90a5c9
                              "Line folding encountered before first"
Packit 90a5c9
                              " header line");
Packit 90a5c9
                return;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (field[1] == '\0') {
Packit 90a5c9
                r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03443)
Packit 90a5c9
                              "Empty folded line encountered");
Packit 90a5c9
                return;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Leading whitespace on an obs-fold line can be
Packit 90a5c9
             * similarly discarded */
Packit 90a5c9
            while (field[1] == '\t' || field[1] == ' ') {
Packit 90a5c9
                ++field; --len;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* This line is a continuation of the preceding line(s),
Packit 90a5c9
             * so append it to the line that we've set aside.
Packit 90a5c9
             * Note: this uses a power-of-two allocator to avoid
Packit 90a5c9
             * doing O(n) allocs and using O(n^2) space for
Packit 90a5c9
             * continuations that span many many lines.
Packit 90a5c9
             */
Packit 90a5c9
            fold_len = last_len + len + 1; /* trailing null */
Packit 90a5c9
Packit 90a5c9
            if (fold_len >= (apr_size_t)(r->server->limit_req_fieldsize)) {
Packit 90a5c9
                r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                /* report what we have accumulated so far before the
Packit 90a5c9
                 * overflow (last_field) as the field with the problem
Packit 90a5c9
                 */
Packit 90a5c9
                apr_table_setn(r->notes, "error-notes",
Packit 90a5c9
                               "Size of a request header field "
Packit 90a5c9
                               "exceeds server limit.");
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00562)
Packit 90a5c9
                              "Request header exceeds LimitRequestFieldSize "
Packit 90a5c9
                              "after folding: %.*s",
Packit 90a5c9
                              field_name_len(last_field), last_field);
Packit 90a5c9
                return;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (fold_len > alloc_len) {
Packit 90a5c9
                char *fold_buf;
Packit 90a5c9
                alloc_len += alloc_len;
Packit 90a5c9
                if (fold_len > alloc_len) {
Packit 90a5c9
                    alloc_len = fold_len;
Packit 90a5c9
                }
Packit 90a5c9
                fold_buf = (char *)apr_palloc(r->pool, alloc_len);
Packit 90a5c9
                memcpy(fold_buf, last_field, last_len);
Packit 90a5c9
                last_field = fold_buf;
Packit 90a5c9
            }
Packit 90a5c9
            memcpy(last_field + last_len, field, len +1); /* +1 for nul */
Packit 90a5c9
            /* Replace obs-fold w/ SP per RFC 7230 3.2.4 */
Packit 90a5c9
            last_field[last_len] = ' ';
Packit 90a5c9
            last_len += len;
Packit 90a5c9
Packit 90a5c9
            /* We've appended this obs-fold line to last_len, proceed to
Packit 90a5c9
             * read the next input line
Packit 90a5c9
             */
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        else if (last_field != NULL) {
Packit 90a5c9
Packit 90a5c9
            /* Process the previous last_field header line with all obs-folded
Packit 90a5c9
             * segments already concatenated (this is not operating on the
Packit 90a5c9
             * most recently read input line).
Packit 90a5c9
             */
Packit 90a5c9
Packit 90a5c9
            if (r->server->limit_req_fields
Packit 90a5c9
                    && (++fields_read > r->server->limit_req_fields)) {
Packit 90a5c9
                r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                apr_table_setn(r->notes, "error-notes",
Packit 90a5c9
                               "The number of request header fields "
Packit 90a5c9
                               "exceeds this server's limit.");
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00563)
Packit 90a5c9
                              "Number of request headers exceeds "
Packit 90a5c9
                              "LimitRequestFields");
Packit 90a5c9
                return;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (!strict)
Packit 90a5c9
            {
Packit 90a5c9
                /* Not Strict ('Unsafe' mode), using the legacy parser */
Packit 90a5c9
Packit 90a5c9
                if (!(value = strchr(last_field, ':'))) { /* Find ':' or */
Packit 90a5c9
                    r->status = HTTP_BAD_REQUEST;   /* abort bad request */
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00564)
Packit 90a5c9
                                  "Request header field is missing ':' "
Packit 90a5c9
                                  "separator: %.*s", (int)LOG_NAME_MAX_LEN,
Packit 90a5c9
                                  last_field);
Packit 90a5c9
                    return;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                if (value == last_field) {
Packit 90a5c9
                    r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03453)
Packit 90a5c9
                                  "Request header field name was empty");
Packit 90a5c9
                    return;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                *value++ = '\0'; /* NUL-terminate at colon */
Packit 90a5c9
Packit 90a5c9
                if (strpbrk(last_field, "\t\n\v\f\r ")) {
Packit 90a5c9
                    r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03452)
Packit 90a5c9
                                  "Request header field name presented"
Packit 90a5c9
                                  " invalid whitespace");
Packit 90a5c9
                    return;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                while (*value == ' ' || *value == '\t') {
Packit 90a5c9
                     ++value;            /* Skip to start of value   */
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                if (strpbrk(value, "\n\v\f\r")) {
Packit 90a5c9
                    r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03451)
Packit 90a5c9
                                  "Request header field value presented"
Packit 90a5c9
                                  " bad whitespace");
Packit 90a5c9
                    return;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else /* Using strict RFC7230 parsing */
Packit 90a5c9
            {
Packit 90a5c9
                /* Ensure valid token chars before ':' per RFC 7230 3.2.4 */
Packit 90a5c9
                value = (char *)ap_scan_http_token(last_field);
Packit 90a5c9
                if ((value == last_field) || *value != ':') {
Packit 90a5c9
                    r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02426)
Packit 90a5c9
                                  "Request header field name is malformed: "
Packit 90a5c9
                                  "%.*s", (int)LOG_NAME_MAX_LEN, last_field);
Packit 90a5c9
                    return;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                *value++ = '\0'; /* NUL-terminate last_field name at ':' */
Packit 90a5c9
Packit 90a5c9
                while (*value == ' ' || *value == '\t') {
Packit 90a5c9
                    ++value;     /* Skip LWS of value */
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* Find invalid, non-HT ctrl char, or the trailing NULL */
Packit 90a5c9
                tmp_field = (char *)ap_scan_http_field_content(value);
Packit 90a5c9
Packit 90a5c9
                /* Reject value for all garbage input (CTRLs excluding HT)
Packit 90a5c9
                 * e.g. only VCHAR / SP / HT / obs-text are allowed per
Packit 90a5c9
                 * RFC7230 3.2.6 - leave all more explicit rule enforcement
Packit 90a5c9
                 * for specific header handler logic later in the cycle
Packit 90a5c9
                 */
Packit 90a5c9
                if (*tmp_field != '\0') {
Packit 90a5c9
                    r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02427)
Packit 90a5c9
                                  "Request header value is malformed: "
Packit 90a5c9
                                  "%.*s", (int)LOG_NAME_MAX_LEN, value);
Packit 90a5c9
                    return;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            apr_table_addn(r->headers_in, last_field, value);
Packit 90a5c9
Packit 90a5c9
            /* This last_field header is now stored in headers_in,
Packit 90a5c9
             * resume processing of the current input line.
Packit 90a5c9
             */
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Found the terminating empty end-of-headers line, stop. */
Packit 90a5c9
        if (len == 0) {
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Keep track of this new header line so that we can extend it across
Packit 90a5c9
         * any obs-fold or parse it on the next loop iteration. We referenced
Packit 90a5c9
         * our previously allocated buffer in r->headers_in,
Packit 90a5c9
         * so allocate a fresh buffer if required.
Packit 90a5c9
         */
Packit 90a5c9
        alloc_len = 0;
Packit 90a5c9
        last_field = field;
Packit 90a5c9
        last_len = len;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Combine multiple message-header fields with the same
Packit 90a5c9
     * field-name, following RFC 2616, 4.2.
Packit 90a5c9
     */
Packit 90a5c9
    apr_table_compress(r->headers_in, APR_OVERLAP_TABLES_MERGE);
Packit 90a5c9
Packit 90a5c9
    /* enforce LimitRequestFieldSize for merged headers */
Packit 90a5c9
    apr_table_do(table_do_fn_check_lengths, r, r->headers_in, NULL);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_get_mime_headers(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    apr_bucket_brigade *tmp_bb;
Packit 90a5c9
    tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
Packit 90a5c9
    ap_get_mime_headers_core(r, tmp_bb);
Packit 90a5c9
    apr_brigade_destroy(tmp_bb);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
request_rec *ap_read_request(conn_rec *conn)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r;
Packit 90a5c9
    apr_pool_t *p;
Packit 90a5c9
    const char *expect;
Packit 90a5c9
    int access_status;
Packit 90a5c9
    apr_bucket_brigade *tmp_bb;
Packit 90a5c9
    apr_socket_t *csd;
Packit 90a5c9
    apr_interval_time_t cur_timeout;
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
    apr_pool_create(&p, conn->pool);
Packit 90a5c9
    apr_pool_tag(p, "request");
Packit 90a5c9
    r = apr_pcalloc(p, sizeof(request_rec));
Packit 90a5c9
    AP_READ_REQUEST_ENTRY((intptr_t)r, (uintptr_t)conn);
Packit 90a5c9
    r->pool            = p;
Packit 90a5c9
    r->connection      = conn;
Packit 90a5c9
    r->server          = conn->base_server;
Packit 90a5c9
Packit 90a5c9
    r->user            = NULL;
Packit 90a5c9
    r->ap_auth_type    = NULL;
Packit 90a5c9
Packit 90a5c9
    r->allowed_methods = ap_make_method_list(p, 2);
Packit 90a5c9
Packit 90a5c9
    r->headers_in      = apr_table_make(r->pool, 25);
Packit 90a5c9
    r->trailers_in     = apr_table_make(r->pool, 5);
Packit 90a5c9
    r->subprocess_env  = apr_table_make(r->pool, 25);
Packit 90a5c9
    r->headers_out     = apr_table_make(r->pool, 12);
Packit 90a5c9
    r->err_headers_out = apr_table_make(r->pool, 5);
Packit 90a5c9
    r->trailers_out    = apr_table_make(r->pool, 5);
Packit 90a5c9
    r->notes           = apr_table_make(r->pool, 5);
Packit 90a5c9
Packit 90a5c9
    r->request_config  = ap_create_request_config(r->pool);
Packit 90a5c9
    /* Must be set before we run create request hook */
Packit 90a5c9
Packit 90a5c9
    r->proto_output_filters = conn->output_filters;
Packit 90a5c9
    r->output_filters  = r->proto_output_filters;
Packit 90a5c9
    r->proto_input_filters = conn->input_filters;
Packit 90a5c9
    r->input_filters   = r->proto_input_filters;
Packit 90a5c9
    ap_run_create_request(r);
Packit 90a5c9
    r->per_dir_config  = r->server->lookup_defaults;
Packit 90a5c9
Packit 90a5c9
    r->sent_bodyct     = 0;                      /* bytect isn't for body */
Packit 90a5c9
Packit 90a5c9
    r->read_length     = 0;
Packit 90a5c9
    r->read_body       = REQUEST_NO_BODY;
Packit 90a5c9
Packit 90a5c9
    r->status          = HTTP_OK;  /* Until further notice */
Packit 90a5c9
    r->the_request     = NULL;
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
    r->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
Packit 90a5c9
Packit 90a5c9
    r->useragent_addr = conn->client_addr;
Packit 90a5c9
    r->useragent_ip = conn->client_ip;
Packit 90a5c9
Packit 90a5c9
    tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
Packit 90a5c9
Packit 90a5c9
    ap_run_pre_read_request(r, conn);
Packit 90a5c9
Packit 90a5c9
    /* Get the request... */
Packit 90a5c9
    if (!read_request_line(r, tmp_bb)) {
Packit 90a5c9
        switch (r->status) {
Packit 90a5c9
        case HTTP_REQUEST_URI_TOO_LARGE:
Packit 90a5c9
        case HTTP_BAD_REQUEST:
Packit 90a5c9
        case HTTP_VERSION_NOT_SUPPORTED:
Packit 90a5c9
        case HTTP_NOT_IMPLEMENTED:
Packit 90a5c9
            if (r->status == HTTP_REQUEST_URI_TOO_LARGE) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00565)
Packit 90a5c9
                              "request failed: client's request-line exceeds LimitRequestLine (longer than %d)",
Packit 90a5c9
                              r->server->limit_req_line);
Packit 90a5c9
            }
Packit 90a5c9
            else if (r->method == NULL) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00566)
Packit 90a5c9
                              "request failed: malformed request line");
Packit 90a5c9
            }
Packit 90a5c9
            access_status = r->status;
Packit 90a5c9
            r->status = HTTP_OK;
Packit 90a5c9
            ap_die(access_status, r);
Packit 90a5c9
            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
Packit 90a5c9
            ap_run_log_transaction(r);
Packit 90a5c9
            r = NULL;
Packit 90a5c9
            apr_brigade_destroy(tmp_bb);
Packit 90a5c9
            goto traceout;
Packit 90a5c9
        case HTTP_REQUEST_TIME_OUT:
Packit 90a5c9
            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, NULL);
Packit 90a5c9
            if (!r->connection->keepalives)
Packit 90a5c9
                ap_run_log_transaction(r);
Packit 90a5c9
            apr_brigade_destroy(tmp_bb);
Packit 90a5c9
            goto traceout;
Packit 90a5c9
        default:
Packit 90a5c9
            apr_brigade_destroy(tmp_bb);
Packit 90a5c9
            r = NULL;
Packit 90a5c9
            goto traceout;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* We may have been in keep_alive_timeout mode, so toggle back
Packit 90a5c9
     * to the normal timeout mode as we fetch the header lines,
Packit 90a5c9
     * as necessary.
Packit 90a5c9
     */
Packit 90a5c9
    csd = ap_get_conn_socket(conn);
Packit 90a5c9
    apr_socket_timeout_get(csd, &cur_timeout);
Packit 90a5c9
    if (cur_timeout != conn->base_server->timeout) {
Packit 90a5c9
        apr_socket_timeout_set(csd, conn->base_server->timeout);
Packit 90a5c9
        cur_timeout = conn->base_server->timeout;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!r->assbackwards) {
Packit 90a5c9
        const char *tenc;
Packit 90a5c9
Packit 90a5c9
        ap_get_mime_headers_core(r, tmp_bb);
Packit 90a5c9
        if (r->status != HTTP_OK) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00567)
Packit 90a5c9
                          "request failed: error reading the headers");
Packit 90a5c9
            ap_send_error_response(r, 0);
Packit 90a5c9
            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
Packit 90a5c9
            ap_run_log_transaction(r);
Packit 90a5c9
            apr_brigade_destroy(tmp_bb);
Packit 90a5c9
            goto traceout;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
Packit 90a5c9
        if (tenc) {
Packit 90a5c9
            /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
Packit 90a5c9
             * Section 3.3.3.3: "If a Transfer-Encoding header field is
Packit 90a5c9
             * present in a request and the chunked transfer coding is not
Packit 90a5c9
             * the final encoding ...; the server MUST respond with the 400
Packit 90a5c9
             * (Bad Request) status code and then close the connection".
Packit 90a5c9
             */
Packit 90a5c9
            if (!(strcasecmp(tenc, "chunked") == 0 /* fast path */
Packit 90a5c9
                    || ap_find_last_token(r->pool, tenc, "chunked"))) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02539)
Packit 90a5c9
                              "client sent unknown Transfer-Encoding "
Packit 90a5c9
                              "(%s): %s", tenc, r->uri);
Packit 90a5c9
                r->status = HTTP_BAD_REQUEST;
Packit 90a5c9
                conn->keepalive = AP_CONN_CLOSE;
Packit 90a5c9
                ap_send_error_response(r, 0);
Packit 90a5c9
                ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
Packit 90a5c9
                ap_run_log_transaction(r);
Packit 90a5c9
                apr_brigade_destroy(tmp_bb);
Packit 90a5c9
                goto traceout;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
Packit 90a5c9
             * Section 3.3.3.3: "If a message is received with both a
Packit 90a5c9
             * Transfer-Encoding and a Content-Length header field, the
Packit 90a5c9
             * Transfer-Encoding overrides the Content-Length. ... A sender
Packit 90a5c9
             * MUST remove the received Content-Length field".
Packit 90a5c9
             */
Packit 90a5c9
            apr_table_unset(r->headers_in, "Content-Length");
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    apr_brigade_destroy(tmp_bb);
Packit 90a5c9
Packit 90a5c9
    /* update what we think the virtual host is based on the headers we've
Packit 90a5c9
     * now read. may update status.
Packit 90a5c9
     */
Packit 90a5c9
    ap_update_vhost_from_headers(r);
Packit 90a5c9
    access_status = r->status;
Packit 90a5c9
Packit 90a5c9
    /* Toggle to the Host:-based vhost's timeout mode to fetch the
Packit 90a5c9
     * request body and send the response body, if needed.
Packit 90a5c9
     */
Packit 90a5c9
    if (cur_timeout != r->server->timeout) {
Packit 90a5c9
        apr_socket_timeout_set(csd, r->server->timeout);
Packit 90a5c9
        cur_timeout = r->server->timeout;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* we may have switched to another server */
Packit 90a5c9
    r->per_dir_config = r->server->lookup_defaults;
Packit 90a5c9
Packit 90a5c9
    if ((!r->hostname && (r->proto_num >= HTTP_VERSION(1, 1)))
Packit 90a5c9
        || ((r->proto_num == HTTP_VERSION(1, 1))
Packit 90a5c9
            && !apr_table_get(r->headers_in, "Host"))) {
Packit 90a5c9
        /*
Packit 90a5c9
         * Client sent us an HTTP/1.1 or later request without telling us the
Packit 90a5c9
         * hostname, either with a full URL or a Host: header. We therefore
Packit 90a5c9
         * need to (as per the 1.1 spec) send an error.  As a special case,
Packit 90a5c9
         * HTTP/1.1 mentions twice (S9, S14.23) that a request MUST contain
Packit 90a5c9
         * a Host: header, and the server MUST respond with 400 if it doesn't.
Packit 90a5c9
         */
Packit 90a5c9
        access_status = HTTP_BAD_REQUEST;
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00569)
Packit 90a5c9
                      "client sent HTTP/1.1 request without hostname "
Packit 90a5c9
                      "(see RFC2616 section 14.23): %s", r->uri);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Add the HTTP_IN filter here to ensure that ap_discard_request_body
Packit 90a5c9
     * called by ap_die and by ap_send_error_response works correctly on
Packit 90a5c9
     * status codes that do not cause the connection to be dropped and
Packit 90a5c9
     * in situations where the connection should be kept alive.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    ap_add_input_filter_handle(ap_http_input_filter_handle,
Packit 90a5c9
                               NULL, r, r->connection);
Packit 90a5c9
Packit 90a5c9
    if (access_status != HTTP_OK
Packit 90a5c9
        || (access_status = ap_run_post_read_request(r))) {
Packit 90a5c9
        ap_die(access_status, r);
Packit 90a5c9
        ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
Packit 90a5c9
        ap_run_log_transaction(r);
Packit 90a5c9
        r = NULL;
Packit 90a5c9
        goto traceout;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (((expect = apr_table_get(r->headers_in, "Expect")) != NULL)
Packit 90a5c9
        && (expect[0] != '\0')) {
Packit 90a5c9
        /*
Packit 90a5c9
         * The Expect header field was added to HTTP/1.1 after RFC 2068
Packit 90a5c9
         * as a means to signal when a 100 response is desired and,
Packit 90a5c9
         * unfortunately, to signal a poor man's mandatory extension that
Packit 90a5c9
         * the server must understand or return 417 Expectation Failed.
Packit 90a5c9
         */
Packit 90a5c9
        if (strcasecmp(expect, "100-continue") == 0) {
Packit 90a5c9
            r->expecting_100 = 1;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            r->status = HTTP_EXPECTATION_FAILED;
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00570)
Packit 90a5c9
                          "client sent an unrecognized expectation value of "
Packit 90a5c9
                          "Expect: %s", expect);
Packit 90a5c9
            ap_send_error_response(r, 0);
Packit 90a5c9
            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
Packit 90a5c9
            ap_run_log_transaction(r);
Packit 90a5c9
            goto traceout;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    AP_READ_REQUEST_SUCCESS((uintptr_t)r, (char *)r->method, (char *)r->uri, (char *)r->server->defn_name, r->status);
Packit 90a5c9
    return r;
Packit 90a5c9
    traceout:
Packit 90a5c9
    AP_READ_REQUEST_FAILURE((uintptr_t)r);
Packit 90a5c9
    return r;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* if a request with a body creates a subrequest, remove original request's
Packit 90a5c9
 * input headers which pertain to the body which has already been read.
Packit 90a5c9
 * out-of-line helper function for ap_set_sub_req_protocol.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
static void strip_headers_request_body(request_rec *rnew)
Packit 90a5c9
{
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Content-Encoding");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Content-Language");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Content-Length");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Content-Location");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Content-MD5");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Content-Range");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Content-Type");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Expires");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Last-Modified");
Packit 90a5c9
    apr_table_unset(rnew->headers_in, "Transfer-Encoding");
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * A couple of other functions which initialize some of the fields of
Packit 90a5c9
 * a request structure, as appropriate for adjuncts of one kind or another
Packit 90a5c9
 * to a request in progress.  Best here, rather than elsewhere, since
Packit 90a5c9
 * *someone* has to set the protocol-specific fields...
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
Packit 90a5c9
                                         const request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    rnew->the_request     = r->the_request;  /* Keep original request-line */
Packit 90a5c9
Packit 90a5c9
    rnew->assbackwards    = 1;   /* Don't send headers from this. */
Packit 90a5c9
    rnew->no_local_copy   = 1;   /* Don't try to send HTTP_NOT_MODIFIED for a
Packit 90a5c9
                                  * fragment. */
Packit 90a5c9
    rnew->method          = "GET";
Packit 90a5c9
    rnew->method_number   = M_GET;
Packit 90a5c9
    rnew->protocol        = "INCLUDED";
Packit 90a5c9
Packit 90a5c9
    rnew->status          = HTTP_OK;
Packit 90a5c9
Packit 90a5c9
    rnew->headers_in      = apr_table_copy(rnew->pool, r->headers_in);
Packit 90a5c9
    rnew->trailers_in     = apr_table_copy(rnew->pool, r->trailers_in);
Packit 90a5c9
Packit 90a5c9
    /* did the original request have a body?  (e.g. POST w/SSI tags)
Packit 90a5c9
     * if so, make sure the subrequest doesn't inherit body headers
Packit 90a5c9
     */
Packit 90a5c9
    if (!r->kept_body && (apr_table_get(r->headers_in, "Content-Length")
Packit 90a5c9
        || apr_table_get(r->headers_in, "Transfer-Encoding"))) {
Packit 90a5c9
        strip_headers_request_body(rnew);
Packit 90a5c9
    }
Packit 90a5c9
    rnew->subprocess_env  = apr_table_copy(rnew->pool, r->subprocess_env);
Packit 90a5c9
    rnew->headers_out     = apr_table_make(rnew->pool, 5);
Packit 90a5c9
    rnew->err_headers_out = apr_table_make(rnew->pool, 5);
Packit 90a5c9
    rnew->trailers_out    = apr_table_make(rnew->pool, 5);
Packit 90a5c9
    rnew->notes           = apr_table_make(rnew->pool, 5);
Packit 90a5c9
Packit 90a5c9
    rnew->expecting_100   = r->expecting_100;
Packit 90a5c9
    rnew->read_length     = r->read_length;
Packit 90a5c9
    rnew->read_body       = REQUEST_NO_BODY;
Packit 90a5c9
Packit 90a5c9
    rnew->main = (request_rec *) r;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void end_output_stream(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_bucket_brigade *bb;
Packit 90a5c9
    apr_bucket *b;
Packit 90a5c9
Packit 90a5c9
    bb = apr_brigade_create(r->pool, c->bucket_alloc);
Packit 90a5c9
    b = apr_bucket_eos_create(c->bucket_alloc);
Packit 90a5c9
    APR_BRIGADE_INSERT_TAIL(bb, b);
Packit 90a5c9
    ap_pass_brigade(r->output_filters, bb);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
Packit 90a5c9
{
Packit 90a5c9
    /* tell the filter chain there is no more content coming */
Packit 90a5c9
    if (!sub->eos_sent) {
Packit 90a5c9
        end_output_stream(sub);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* finalize_request_protocol is called at completion of sending the
Packit 90a5c9
 * response.  Its sole purpose is to send the terminating protocol
Packit 90a5c9
 * information for any wrappers around the response message body
Packit 90a5c9
 * (i.e., transfer encodings).  It should have been named finalize_response.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    (void) ap_discard_request_body(r);
Packit 90a5c9
Packit 90a5c9
    /* tell the filter chain there is no more content coming */
Packit 90a5c9
    if (!r->eos_sent) {
Packit 90a5c9
        end_output_stream(r);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Support for the Basic authentication protocol, and a bit for Digest.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_note_auth_failure(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    const char *type = ap_auth_type(r);
Packit 90a5c9
    if (type) {
Packit 90a5c9
        ap_run_note_auth_failure(r, type);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00571)
Packit 90a5c9
                      "need AuthType to note auth failure: %s", r->uri);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_note_auth_failure(r);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_note_auth_failure(r);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw)
Packit 90a5c9
{
Packit 90a5c9
    const char *auth_line = apr_table_get(r->headers_in,
Packit 90a5c9
                                          (PROXYREQ_PROXY == r->proxyreq)
Packit 90a5c9
                                              ? "Proxy-Authorization"
Packit 90a5c9
                                              : "Authorization");
Packit 90a5c9
    const char *t;
Packit 90a5c9
Packit 90a5c9
    if (!(t = ap_auth_type(r)) || strcasecmp(t, "Basic"))
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
Packit 90a5c9
    if (!ap_auth_name(r)) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00572) 
Packit 90a5c9
                      "need AuthName: %s", r->uri);
Packit 90a5c9
        return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!auth_line) {
Packit 90a5c9
        ap_note_auth_failure(r);
Packit 90a5c9
        return HTTP_UNAUTHORIZED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (strcasecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
Packit 90a5c9
        /* Client tried to authenticate using wrong auth scheme */
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00573)
Packit 90a5c9
                      "client used wrong authentication scheme: %s", r->uri);
Packit 90a5c9
        ap_note_auth_failure(r);
Packit 90a5c9
        return HTTP_UNAUTHORIZED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    while (*auth_line == ' ' || *auth_line == '\t') {
Packit 90a5c9
        auth_line++;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    t = ap_pbase64decode(r->pool, auth_line);
Packit 90a5c9
    r->user = ap_getword_nulls (r->pool, &t, ':');
Packit 90a5c9
    apr_table_setn(r->notes, AP_GET_BASIC_AUTH_PW_NOTE, "1");
Packit 90a5c9
    r->ap_auth_type = "Basic";
Packit 90a5c9
Packit 90a5c9
    *pw = t;
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_get_basic_auth_components(const request_rec *r,
Packit 90a5c9
                                                      const char **username,
Packit 90a5c9
                                                      const char **password)
Packit 90a5c9
{
Packit 90a5c9
    const char *auth_header;
Packit 90a5c9
    const char *credentials;
Packit 90a5c9
    const char *decoded;
Packit 90a5c9
    const char *user;
Packit 90a5c9
Packit 90a5c9
    auth_header = (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authorization"
Packit 90a5c9
                                                  : "Authorization";
Packit 90a5c9
    credentials = apr_table_get(r->headers_in, auth_header);
Packit 90a5c9
Packit 90a5c9
    if (!credentials) {
Packit 90a5c9
        /* No auth header. */
Packit 90a5c9
        return APR_EINVAL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (ap_cstr_casecmp(ap_getword(r->pool, &credentials, ' '), "Basic")) {
Packit 90a5c9
        /* These aren't Basic credentials. */
Packit 90a5c9
        return APR_EINVAL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    while (*credentials == ' ' || *credentials == '\t') {
Packit 90a5c9
        credentials++;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* XXX Our base64 decoding functions don't actually error out if the string
Packit 90a5c9
     * we give it isn't base64; they'll just silently stop and hand us whatever
Packit 90a5c9
     * they've parsed up to that point.
Packit 90a5c9
     *
Packit 90a5c9
     * Since this function is supposed to be a drop-in replacement for the
Packit 90a5c9
     * deprecated ap_get_basic_auth_pw(), don't fix this for 2.4.x.
Packit 90a5c9
     */
Packit 90a5c9
    decoded = ap_pbase64decode(r->pool, credentials);
Packit 90a5c9
    user = ap_getword_nulls(r->pool, &decoded, ':');
Packit 90a5c9
Packit 90a5c9
    if (username) {
Packit 90a5c9
        *username = user;
Packit 90a5c9
    }
Packit 90a5c9
    if (password) {
Packit 90a5c9
        *password = decoded;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
struct content_length_ctx {
Packit 90a5c9
    int data_sent;  /* true if the C-L filter has already sent at
Packit 90a5c9
                     * least one bucket on to the next output filter
Packit 90a5c9
                     * for this request
Packit 90a5c9
                     */
Packit 90a5c9
    apr_bucket_brigade *tmpbb;
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* This filter computes the content length, but it also computes the number
Packit 90a5c9
 * of bytes sent to the client.  This means that this filter will always run
Packit 90a5c9
 * through all of the buckets in all brigades
Packit 90a5c9
 */
Packit 90a5c9
AP_CORE_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(
Packit 90a5c9
    ap_filter_t *f,
Packit 90a5c9
    apr_bucket_brigade *b)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r = f->r;
Packit 90a5c9
    struct content_length_ctx *ctx;
Packit 90a5c9
    apr_bucket *e;
Packit 90a5c9
    int eos = 0;
Packit 90a5c9
    apr_read_type_e eblock = APR_NONBLOCK_READ;
Packit 90a5c9
Packit 90a5c9
    ctx = f->ctx;
Packit 90a5c9
    if (!ctx) {
Packit 90a5c9
        f->ctx = ctx = apr_palloc(r->pool, sizeof(*ctx));
Packit 90a5c9
        ctx->data_sent = 0;
Packit 90a5c9
        ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Loop through the brigade to count the length. To avoid
Packit 90a5c9
     * arbitrary memory consumption with morphing bucket types, this
Packit 90a5c9
     * loop will stop and pass on the brigade when necessary. */
Packit 90a5c9
    e = APR_BRIGADE_FIRST(b);
Packit 90a5c9
    while (e != APR_BRIGADE_SENTINEL(b)) {
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
        if (APR_BUCKET_IS_EOS(e)) {
Packit 90a5c9
            eos = 1;
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
        /* For a flush bucket, fall through to pass the brigade and
Packit 90a5c9
         * flush now. */
Packit 90a5c9
        else if (APR_BUCKET_IS_FLUSH(e)) {
Packit 90a5c9
            e = APR_BUCKET_NEXT(e);
Packit 90a5c9
        }
Packit 90a5c9
        /* For metadata bucket types other than FLUSH, loop. */
Packit 90a5c9
        else if (APR_BUCKET_IS_METADATA(e)) {
Packit 90a5c9
            e = APR_BUCKET_NEXT(e);
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        /* For determinate length data buckets, count the length and
Packit 90a5c9
         * continue. */
Packit 90a5c9
        else if (e->length != (apr_size_t)-1) {
Packit 90a5c9
            r->bytes_sent += e->length;
Packit 90a5c9
            e = APR_BUCKET_NEXT(e);
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
        /* For indeterminate length data buckets, perform one read. */
Packit 90a5c9
        else /* e->length == (apr_size_t)-1 */ {
Packit 90a5c9
            apr_size_t len;
Packit 90a5c9
            const char *ignored;
Packit 90a5c9
        
Packit 90a5c9
            rv = apr_bucket_read(e, &ignored, &len, eblock);
Packit 90a5c9
            if ((rv != APR_SUCCESS) && !APR_STATUS_IS_EAGAIN(rv)) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00574)
Packit 90a5c9
                              "ap_content_length_filter: "
Packit 90a5c9
                              "apr_bucket_read() failed");
Packit 90a5c9
                return rv;
Packit 90a5c9
            }
Packit 90a5c9
            if (rv == APR_SUCCESS) {
Packit 90a5c9
                eblock = APR_NONBLOCK_READ;
Packit 90a5c9
                e = APR_BUCKET_NEXT(e);
Packit 90a5c9
                r->bytes_sent += len;
Packit 90a5c9
            }
Packit 90a5c9
            else if (APR_STATUS_IS_EAGAIN(rv)) {
Packit 90a5c9
                apr_bucket *flush;
Packit 90a5c9
Packit 90a5c9
                /* Next read must block. */
Packit 90a5c9
                eblock = APR_BLOCK_READ;
Packit 90a5c9
Packit 90a5c9
                /* Ensure the last bucket to pass down is a flush if
Packit 90a5c9
                 * the next read will block. */
Packit 90a5c9
                flush = apr_bucket_flush_create(f->c->bucket_alloc);
Packit 90a5c9
                APR_BUCKET_INSERT_BEFORE(e, flush);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Optimization: if the next bucket is EOS (directly after a
Packit 90a5c9
         * bucket morphed to the heap, or a flush), short-cut to
Packit 90a5c9
         * handle EOS straight away - allowing C-L to be determined
Packit 90a5c9
         * for content which is already entirely in memory. */
Packit 90a5c9
        if (e != APR_BRIGADE_SENTINEL(b) && APR_BUCKET_IS_EOS(e)) {
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* On reaching here, pass on everything in the brigade up to
Packit 90a5c9
         * this point. */
Packit 90a5c9
        apr_brigade_split_ex(b, e, ctx->tmpbb);
Packit 90a5c9
        
Packit 90a5c9
        rv = ap_pass_brigade(f->next, b);
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
        else if (f->c->aborted) {
Packit 90a5c9
            return APR_ECONNABORTED;
Packit 90a5c9
        }
Packit 90a5c9
        apr_brigade_cleanup(b);
Packit 90a5c9
        APR_BRIGADE_CONCAT(b, ctx->tmpbb);
Packit 90a5c9
        e = APR_BRIGADE_FIRST(b);
Packit 90a5c9
        
Packit 90a5c9
        ctx->data_sent = 1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* If we've now seen the entire response and it's otherwise
Packit 90a5c9
     * okay to set the C-L in the response header, then do so now.
Packit 90a5c9
     *
Packit 90a5c9
     * We can only set a C-L in the response header if we haven't already
Packit 90a5c9
     * sent any buckets on to the next output filter for this request.
Packit 90a5c9
     */
Packit 90a5c9
    if (ctx->data_sent == 0 && eos &&
Packit 90a5c9
        /* don't whack the C-L if it has already been set for a HEAD
Packit 90a5c9
         * by something like proxy.  the brigade only has an EOS bucket
Packit 90a5c9
         * in this case, making r->bytes_sent zero.
Packit 90a5c9
         *
Packit 90a5c9
         * if r->bytes_sent > 0 we have a (temporary) body whose length may
Packit 90a5c9
         * have been changed by a filter.  the C-L header might not have been
Packit 90a5c9
         * updated so we do it here.  long term it would be cleaner to have
Packit 90a5c9
         * such filters update or remove the C-L header, and just use it
Packit 90a5c9
         * if present.
Packit 90a5c9
         */
Packit 90a5c9
        !((r->header_only || AP_STATUS_IS_HEADER_ONLY(r->status)) && r->bytes_sent == 0 &&
Packit 90a5c9
            apr_table_get(r->headers_out, "Content-Length"))) {
Packit 90a5c9
        ap_set_content_length(r, r->bytes_sent);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ctx->data_sent = 1;
Packit 90a5c9
    return ap_pass_brigade(f->next, b);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Send the body of a response to the client.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r,
Packit 90a5c9
                                    apr_off_t offset, apr_size_t len,
Packit 90a5c9
                                    apr_size_t *nbytes)
Packit 90a5c9
{
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_bucket_brigade *bb = NULL;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    bb = apr_brigade_create(r->pool, c->bucket_alloc);
Packit 90a5c9
Packit 90a5c9
    apr_brigade_insert_file(bb, fd, offset, len, r->pool);
Packit 90a5c9
Packit 90a5c9
    rv = ap_pass_brigade(r->output_filters, bb);
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        *nbytes = 0; /* no way to tell how many were actually sent */
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        *nbytes = len;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#if APR_HAS_MMAP
Packit 90a5c9
/* send data from an in-memory buffer */
Packit 90a5c9
AP_DECLARE(apr_size_t) ap_send_mmap(apr_mmap_t *mm,
Packit 90a5c9
                                    request_rec *r,
Packit 90a5c9
                                    apr_size_t offset,
Packit 90a5c9
                                    apr_size_t length)
Packit 90a5c9
{
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_bucket_brigade *bb = NULL;
Packit 90a5c9
    apr_bucket *b;
Packit 90a5c9
Packit 90a5c9
    bb = apr_brigade_create(r->pool, c->bucket_alloc);
Packit 90a5c9
    b = apr_bucket_mmap_create(mm, offset, length, c->bucket_alloc);
Packit 90a5c9
    APR_BRIGADE_INSERT_TAIL(bb, b);
Packit 90a5c9
    ap_pass_brigade(r->output_filters, bb);
Packit 90a5c9
Packit 90a5c9
    return mm->size; /* XXX - change API to report apr_status_t? */
Packit 90a5c9
}
Packit 90a5c9
#endif /* APR_HAS_MMAP */
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    apr_bucket_brigade *bb;
Packit 90a5c9
    apr_bucket_brigade *tmpbb;
Packit 90a5c9
} old_write_filter_ctx;
Packit 90a5c9
Packit 90a5c9
AP_CORE_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(
Packit 90a5c9
    ap_filter_t *f, apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    old_write_filter_ctx *ctx = f->ctx;
Packit 90a5c9
Packit 90a5c9
    AP_DEBUG_ASSERT(ctx);
Packit 90a5c9
Packit 90a5c9
    if (ctx->bb != NULL) {
Packit 90a5c9
        /* whatever is coming down the pipe (we don't care), we
Packit 90a5c9
         * can simply insert our buffered data at the front and
Packit 90a5c9
         * pass the whole bundle down the chain.
Packit 90a5c9
         */
Packit 90a5c9
        APR_BRIGADE_PREPEND(bb, ctx->bb);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return ap_pass_brigade(f->next, bb);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static ap_filter_t *insert_old_write_filter(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_filter_t *f;
Packit 90a5c9
    old_write_filter_ctx *ctx;
Packit 90a5c9
Packit 90a5c9
    /* future optimization: record some flags in the request_rec to
Packit 90a5c9
     * say whether we've added our filter, and whether it is first.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    /* this will typically exit on the first test */
Packit 90a5c9
    for (f = r->output_filters; f != NULL; f = f->next) {
Packit 90a5c9
        if (ap_old_write_func == f->frec)
Packit 90a5c9
            break;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (f == NULL) {
Packit 90a5c9
        /* our filter hasn't been added yet */
Packit 90a5c9
        ctx = apr_pcalloc(r->pool, sizeof(*ctx));
Packit 90a5c9
        ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
Packit 90a5c9
Packit 90a5c9
        ap_add_output_filter("OLD_WRITE", ctx, r, r->connection);
Packit 90a5c9
        f = r->output_filters;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return f;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t buffer_output(request_rec *r,
Packit 90a5c9
                                  const char *str, apr_size_t len)
Packit 90a5c9
{
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    ap_filter_t *f;
Packit 90a5c9
    old_write_filter_ctx *ctx;
Packit 90a5c9
Packit 90a5c9
    if (len == 0)
Packit 90a5c9
        return APR_SUCCESS;
Packit 90a5c9
Packit 90a5c9
    f = insert_old_write_filter(r);
Packit 90a5c9
    ctx = f->ctx;
Packit 90a5c9
Packit 90a5c9
    /* if the first filter is not our buffering filter, then we have to
Packit 90a5c9
     * deliver the content through the normal filter chain
Packit 90a5c9
     */
Packit 90a5c9
    if (f != r->output_filters) {
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
        apr_bucket *b = apr_bucket_transient_create(str, len, c->bucket_alloc);
Packit 90a5c9
        APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b);
Packit 90a5c9
Packit 90a5c9
        rv = ap_pass_brigade(r->output_filters, ctx->tmpbb);
Packit 90a5c9
        apr_brigade_cleanup(ctx->tmpbb);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (ctx->bb == NULL) {
Packit 90a5c9
        ctx->bb = apr_brigade_create(r->pool, c->bucket_alloc);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return ap_fwrite(f->next, ctx->bb, str, len);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_rputc(int c, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    char c2 = (char)c;
Packit 90a5c9
Packit 90a5c9
    if (r->connection->aborted) {
Packit 90a5c9
        return -1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (buffer_output(r, &c2, 1) != APR_SUCCESS)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    return c;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if (r->connection->aborted)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    if (buffer_output(r, buf, nbyte) != APR_SUCCESS)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    return nbyte;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
struct ap_vrprintf_data {
Packit 90a5c9
    apr_vformatter_buff_t vbuff;
Packit 90a5c9
    request_rec *r;
Packit 90a5c9
    char *buff;
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* Flush callback for apr_vformatter; returns -1 on error. */
Packit 90a5c9
static int r_flush(apr_vformatter_buff_t *buff)
Packit 90a5c9
{
Packit 90a5c9
    /* callback function passed to ap_vformatter to be called when
Packit 90a5c9
     * vformatter needs to write into buff and buff.curpos > buff.endpos */
Packit 90a5c9
Packit 90a5c9
    /* ap_vrprintf_data passed as a apr_vformatter_buff_t, which is then
Packit 90a5c9
     * "downcast" to an ap_vrprintf_data */
Packit 90a5c9
    struct ap_vrprintf_data *vd = (struct ap_vrprintf_data*)buff;
Packit 90a5c9
Packit 90a5c9
    if (vd->r->connection->aborted)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    /* r_flush is called when vbuff is completely full */
Packit 90a5c9
    if (buffer_output(vd->r, vd->buff, AP_IOBUFSIZE)) {
Packit 90a5c9
        return -1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* reset the buffer position */
Packit 90a5c9
    vd->vbuff.curpos = vd->buff;
Packit 90a5c9
    vd->vbuff.endpos = vd->buff + AP_IOBUFSIZE;
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
Packit 90a5c9
{
Packit 90a5c9
    apr_size_t written;
Packit 90a5c9
    struct ap_vrprintf_data vd;
Packit 90a5c9
    char vrprintf_buf[AP_IOBUFSIZE];
Packit 90a5c9
Packit 90a5c9
    vd.vbuff.curpos = vrprintf_buf;
Packit 90a5c9
    vd.vbuff.endpos = vrprintf_buf + AP_IOBUFSIZE;
Packit 90a5c9
    vd.r = r;
Packit 90a5c9
    vd.buff = vrprintf_buf;
Packit 90a5c9
Packit 90a5c9
    if (r->connection->aborted)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    written = apr_vformatter(r_flush, &vd.vbuff, fmt, va);
Packit 90a5c9
Packit 90a5c9
    if (written != -1) {
Packit 90a5c9
        int n = vd.vbuff.curpos - vrprintf_buf;
Packit 90a5c9
Packit 90a5c9
        /* last call to buffer_output, to finish clearing the buffer */
Packit 90a5c9
        if (buffer_output(r, vrprintf_buf,n) != APR_SUCCESS)
Packit 90a5c9
            return -1;
Packit 90a5c9
Packit 90a5c9
        written += n;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return written;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list va;
Packit 90a5c9
    int n;
Packit 90a5c9
Packit 90a5c9
    if (r->connection->aborted)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    va_start(va, fmt);
Packit 90a5c9
    n = ap_vrprintf(r, fmt, va);
Packit 90a5c9
    va_end(va);
Packit 90a5c9
Packit 90a5c9
    return n;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r, ...)
Packit 90a5c9
{
Packit 90a5c9
    va_list va;
Packit 90a5c9
    const char *s;
Packit 90a5c9
    apr_size_t len;
Packit 90a5c9
    apr_size_t written = 0;
Packit 90a5c9
Packit 90a5c9
    if (r->connection->aborted)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    /* ### TODO: if the total output is large, put all the strings
Packit 90a5c9
     * ### into a single brigade, rather than flushing each time we
Packit 90a5c9
     * ### fill the buffer
Packit 90a5c9
     */
Packit 90a5c9
    va_start(va, r);
Packit 90a5c9
    while (1) {
Packit 90a5c9
        s = va_arg(va, const char *);
Packit 90a5c9
        if (s == NULL)
Packit 90a5c9
            break;
Packit 90a5c9
Packit 90a5c9
        len = strlen(s);
Packit 90a5c9
        if (buffer_output(r, s, len) != APR_SUCCESS) {
Packit 90a5c9
            return -1;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        written += len;
Packit 90a5c9
    }
Packit 90a5c9
    va_end(va);
Packit 90a5c9
Packit 90a5c9
    return written;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_rflush(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_bucket *b;
Packit 90a5c9
    ap_filter_t *f;
Packit 90a5c9
    old_write_filter_ctx *ctx;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    f = insert_old_write_filter(r);
Packit 90a5c9
    ctx = f->ctx;
Packit 90a5c9
Packit 90a5c9
    b = apr_bucket_flush_create(c->bucket_alloc);
Packit 90a5c9
    APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b);
Packit 90a5c9
Packit 90a5c9
    rv = ap_pass_brigade(r->output_filters, ctx->tmpbb);
Packit 90a5c9
    apr_brigade_cleanup(ctx->tmpbb);
Packit 90a5c9
    if (rv != APR_SUCCESS)
Packit 90a5c9
        return -1;
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * This function sets the Last-Modified output header field to the value
Packit 90a5c9
 * of the mtime field in the request structure - rationalized to keep it from
Packit 90a5c9
 * being in the future.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_set_last_modified(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if (!r->assbackwards) {
Packit 90a5c9
        apr_time_t mod_time = ap_rationalize_mtime(r, r->mtime);
Packit 90a5c9
        char *datestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
Packit 90a5c9
Packit 90a5c9
        apr_rfc822_date(datestr, mod_time);
Packit 90a5c9
        apr_table_setn(r->headers_out, "Last-Modified", datestr);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
typedef struct hdr_ptr {
Packit 90a5c9
    ap_filter_t *f;
Packit 90a5c9
    apr_bucket_brigade *bb;
Packit 90a5c9
} hdr_ptr;
Packit 90a5c9
 
Packit 90a5c9
#if APR_CHARSET_EBCDIC
Packit 90a5c9
static int send_header(void *data, const char *key, const char *val)
Packit 90a5c9
{
Packit 90a5c9
    char *header_line = NULL;
Packit 90a5c9
    hdr_ptr *hdr = (hdr_ptr*)data;
Packit 90a5c9
Packit 90a5c9
    header_line = apr_pstrcat(hdr->bb->p, key, ": ", val, CRLF, NULL);
Packit 90a5c9
    ap_xlate_proto_to_ascii(header_line, strlen(header_line));
Packit 90a5c9
    ap_fputs(hdr->f, hdr->bb, header_line);
Packit 90a5c9
    return 1;
Packit 90a5c9
}
Packit 90a5c9
#else
Packit 90a5c9
static int send_header(void *data, const char *key, const char *val)
Packit 90a5c9
{
Packit 90a5c9
     ap_fputstrs(((hdr_ptr*)data)->f, ((hdr_ptr*)data)->bb,
Packit 90a5c9
                 key, ": ", val, CRLF, NULL);
Packit 90a5c9
     return 1;
Packit 90a5c9
 }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers)
Packit 90a5c9
{
Packit 90a5c9
    hdr_ptr x;
Packit 90a5c9
    char *status_line = NULL;
Packit 90a5c9
    request_rec *rr;
Packit 90a5c9
Packit 90a5c9
    if (r->proto_num < HTTP_VERSION(1,1)) {
Packit 90a5c9
        /* don't send interim response to HTTP/1.0 Client */
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
    if (!ap_is_HTTP_INFO(r->status)) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00575)
Packit 90a5c9
                      "Status is %d - not sending interim response", r->status);
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
    if ((r->status == HTTP_CONTINUE) && !r->expecting_100) {
Packit 90a5c9
        /*
Packit 90a5c9
         * Don't send 100-Continue when there was no Expect: 100-continue
Packit 90a5c9
         * in the request headers. For origin servers this is a SHOULD NOT
Packit 90a5c9
         * for proxies it is a MUST NOT according to RFC 2616 8.2.3
Packit 90a5c9
         */
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* if we send an interim response, we're no longer in a state of
Packit 90a5c9
     * expecting one.  Also, this could feasibly be in a subrequest,
Packit 90a5c9
     * so we need to propagate the fact that we responded.
Packit 90a5c9
     */
Packit 90a5c9
    for (rr = r; rr != NULL; rr = rr->main) {
Packit 90a5c9
        rr->expecting_100 = 0;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    status_line = apr_pstrcat(r->pool, AP_SERVER_PROTOCOL, " ", r->status_line, CRLF, NULL);
Packit 90a5c9
    ap_xlate_proto_to_ascii(status_line, strlen(status_line));
Packit 90a5c9
Packit 90a5c9
    x.f = r->connection->output_filters;
Packit 90a5c9
    x.bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
Packit 90a5c9
Packit 90a5c9
    ap_fputs(x.f, x.bb, status_line);
Packit 90a5c9
    if (send_headers) {
Packit 90a5c9
        apr_table_do(send_header, &x, r->headers_out, NULL);
Packit 90a5c9
        apr_table_clear(r->headers_out);
Packit 90a5c9
    }
Packit 90a5c9
    ap_fputs(x.f, x.bb, CRLF_ASCII);
Packit 90a5c9
    ap_fflush(x.f, x.bb);
Packit 90a5c9
    apr_brigade_destroy(x.bb);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Compare two protocol identifier. Result is similar to strcmp():
Packit 90a5c9
 * 0 gives same precedence, >0 means proto1 is preferred.
Packit 90a5c9
 */
Packit 90a5c9
static int protocol_cmp(const apr_array_header_t *preferences,
Packit 90a5c9
                        const char *proto1,
Packit 90a5c9
                        const char *proto2)
Packit 90a5c9
{
Packit 90a5c9
    if (preferences && preferences->nelts > 0) {
Packit 90a5c9
        int index1 = ap_array_str_index(preferences, proto1, 0);
Packit 90a5c9
        int index2 = ap_array_str_index(preferences, proto2, 0);
Packit 90a5c9
        if (index2 > index1) {
Packit 90a5c9
            return (index1 >= 0) ? 1 : -1;
Packit 90a5c9
        }
Packit 90a5c9
        else if (index1 > index2) {
Packit 90a5c9
            return (index2 >= 0) ? -1 : 1;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    /* both have the same index (mabye -1 or no pref configured) and we compare
Packit 90a5c9
     * the names so that spdy3 gets precedence over spdy2. That makes
Packit 90a5c9
     * the outcome at least deterministic. */
Packit 90a5c9
    return strcmp(proto1, proto2);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(const char *) ap_get_protocol(conn_rec *c)
Packit 90a5c9
{
Packit 90a5c9
    const char *protocol = ap_run_protocol_get(c);
Packit 90a5c9
    return protocol? protocol : AP_PROTOCOL_HTTP1;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_get_protocol_upgrades(conn_rec *c, request_rec *r, 
Packit 90a5c9
                                                  server_rec *s, int report_all, 
Packit 90a5c9
                                                  const apr_array_header_t **pupgrades)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t *pool = r? r->pool : c->pool;
Packit 90a5c9
    core_server_config *conf;
Packit 90a5c9
    const char *existing;
Packit 90a5c9
    apr_array_header_t *upgrades = NULL;
Packit 90a5c9
Packit 90a5c9
    if (!s) {
Packit 90a5c9
        s = (r? r->server : c->base_server);
Packit 90a5c9
    }
Packit 90a5c9
    conf = ap_get_core_module_config(s->module_config);
Packit 90a5c9
    
Packit 90a5c9
    if (conf->protocols->nelts > 0) {
Packit 90a5c9
        existing = ap_get_protocol(c);
Packit 90a5c9
        if (conf->protocols->nelts > 1 
Packit 90a5c9
            || !ap_array_str_contains(conf->protocols, existing)) {
Packit 90a5c9
            int i;
Packit 90a5c9
            
Packit 90a5c9
            /* possibly more than one choice or one, but not the
Packit 90a5c9
             * existing. (TODO: maybe 426 and Upgrade then?) */
Packit 90a5c9
            upgrades = apr_array_make(pool, conf->protocols->nelts + 1, 
Packit 90a5c9
                                      sizeof(char *));
Packit 90a5c9
            for (i = 0; i < conf->protocols->nelts; i++) {
Packit 90a5c9
                const char *p = APR_ARRAY_IDX(conf->protocols, i, char *);
Packit 90a5c9
                if (strcmp(existing, p)) {
Packit 90a5c9
                    /* not the one we have and possible, add in this order */
Packit 90a5c9
                    APR_ARRAY_PUSH(upgrades, const char*) = p;
Packit 90a5c9
                }
Packit 90a5c9
                else if (!report_all) {
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    
Packit 90a5c9
    *pupgrades = upgrades;
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(const char *) ap_select_protocol(conn_rec *c, request_rec *r, 
Packit 90a5c9
                                            server_rec *s,
Packit 90a5c9
                                            const apr_array_header_t *choices)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t *pool = r? r->pool : c->pool;
Packit 90a5c9
    core_server_config *conf;
Packit 90a5c9
    const char *protocol = NULL, *existing;
Packit 90a5c9
    apr_array_header_t *proposals;
Packit 90a5c9
Packit 90a5c9
    if (!s) {
Packit 90a5c9
        s = (r? r->server : c->base_server);
Packit 90a5c9
    }
Packit 90a5c9
    conf = ap_get_core_module_config(s->module_config);
Packit 90a5c9
    
Packit 90a5c9
    if (APLOGcdebug(c)) {
Packit 90a5c9
        const char *p = apr_array_pstrcat(pool, conf->protocols, ',');
Packit 90a5c9
        ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03155) 
Packit 90a5c9
                      "select protocol from %s, choices=%s for server %s", 
Packit 90a5c9
                      p, apr_array_pstrcat(pool, choices, ','),
Packit 90a5c9
                      s->server_hostname);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (conf->protocols->nelts <= 0) {
Packit 90a5c9
        /* nothing configured, by default, we only allow http/1.1 here.
Packit 90a5c9
         * For now...
Packit 90a5c9
         */
Packit 90a5c9
        if (ap_array_str_contains(choices, AP_PROTOCOL_HTTP1)) {
Packit 90a5c9
            return AP_PROTOCOL_HTTP1;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return NULL;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    proposals = apr_array_make(pool, choices->nelts + 1, sizeof(char *));
Packit 90a5c9
    ap_run_protocol_propose(c, r, s, choices, proposals);
Packit 90a5c9
Packit 90a5c9
    /* If the existing protocol has not been proposed, but is a choice,
Packit 90a5c9
     * add it to the proposals implicitly.
Packit 90a5c9
     */
Packit 90a5c9
    existing = ap_get_protocol(c);
Packit 90a5c9
    if (!ap_array_str_contains(proposals, existing)
Packit 90a5c9
        && ap_array_str_contains(choices, existing)) {
Packit 90a5c9
        APR_ARRAY_PUSH(proposals, const char*) = existing;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (proposals->nelts > 0) {
Packit 90a5c9
        int i;
Packit 90a5c9
        const apr_array_header_t *prefs = NULL;
Packit 90a5c9
Packit 90a5c9
        /* Default for protocols_honor_order is 'on' or != 0 */
Packit 90a5c9
        if (conf->protocols_honor_order == 0 && choices->nelts > 0) {
Packit 90a5c9
            prefs = choices;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            prefs = conf->protocols;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Select the most preferred protocol */
Packit 90a5c9
        if (APLOGcdebug(c)) {
Packit 90a5c9
            ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03156) 
Packit 90a5c9
                          "select protocol, proposals=%s preferences=%s configured=%s", 
Packit 90a5c9
                          apr_array_pstrcat(pool, proposals, ','),
Packit 90a5c9
                          apr_array_pstrcat(pool, prefs, ','),
Packit 90a5c9
                          apr_array_pstrcat(pool, conf->protocols, ','));
Packit 90a5c9
        }
Packit 90a5c9
        for (i = 0; i < proposals->nelts; ++i) {
Packit 90a5c9
            const char *p = APR_ARRAY_IDX(proposals, i, const char *);
Packit 90a5c9
            if (!ap_array_str_contains(conf->protocols, p)) {
Packit 90a5c9
                /* not a configured protocol here */
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
            else if (!protocol 
Packit 90a5c9
                     || (protocol_cmp(prefs, protocol, p) < 0)) {
Packit 90a5c9
                /* none selected yet or this one has preference */
Packit 90a5c9
                protocol = p;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    if (APLOGcdebug(c)) {
Packit 90a5c9
        ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03157)
Packit 90a5c9
                      "selected protocol=%s", 
Packit 90a5c9
                      protocol? protocol : "(none)");
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return protocol;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_switch_protocol(conn_rec *c, request_rec *r, 
Packit 90a5c9
                                            server_rec *s,
Packit 90a5c9
                                            const char *protocol)
Packit 90a5c9
{
Packit 90a5c9
    const char *current = ap_get_protocol(c);
Packit 90a5c9
    int rc;
Packit 90a5c9
    
Packit 90a5c9
    if (!strcmp(current, protocol)) {
Packit 90a5c9
        ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(02906)
Packit 90a5c9
                      "already at it, protocol_switch to %s", 
Packit 90a5c9
                      protocol);
Packit 90a5c9
        return APR_SUCCESS;
Packit 90a5c9
    }
Packit 90a5c9
    
Packit 90a5c9
    rc = ap_run_protocol_switch(c, r, s, protocol);
Packit 90a5c9
    switch (rc) {
Packit 90a5c9
        case DECLINED:
Packit 90a5c9
            ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02907)
Packit 90a5c9
                          "no implementation for protocol_switch to %s", 
Packit 90a5c9
                          protocol);
Packit 90a5c9
            return APR_ENOTIMPL;
Packit 90a5c9
        case OK:
Packit 90a5c9
        case DONE:
Packit 90a5c9
            return APR_SUCCESS;
Packit 90a5c9
        default:
Packit 90a5c9
            ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02905)
Packit 90a5c9
                          "unexpected return code %d from protocol_switch to %s"
Packit 90a5c9
                          , rc, protocol);
Packit 90a5c9
            return APR_EOF;
Packit 90a5c9
    }    
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_is_allowed_protocol(conn_rec *c, request_rec *r,
Packit 90a5c9
                                       server_rec *s, const char *protocol)
Packit 90a5c9
{
Packit 90a5c9
    core_server_config *conf;
Packit 90a5c9
Packit 90a5c9
    if (!s) {
Packit 90a5c9
        s = (r? r->server : c->base_server);
Packit 90a5c9
    }
Packit 90a5c9
    conf = ap_get_core_module_config(s->module_config);
Packit 90a5c9
    
Packit 90a5c9
    if (conf->protocols->nelts > 0) {
Packit 90a5c9
        return ap_array_str_contains(conf->protocols, protocol);
Packit 90a5c9
    }
Packit 90a5c9
    return !strcmp(AP_PROTOCOL_HTTP1, protocol);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
AP_IMPLEMENT_HOOK_VOID(pre_read_request,
Packit 90a5c9
                       (request_rec *r, conn_rec *c),
Packit 90a5c9
                       (r, c))
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_ALL(int,post_read_request,
Packit 90a5c9
                          (request_rec *r), (r), OK, DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_ALL(int,log_transaction,
Packit 90a5c9
                          (request_rec *r), (r), OK, DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,http_scheme,
Packit 90a5c9
                            (const request_rec *r), (r), NULL)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(unsigned short,default_port,
Packit 90a5c9
                            (const request_rec *r), (r), 0)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int, note_auth_failure,
Packit 90a5c9
                            (request_rec *r, const char *auth_type),
Packit 90a5c9
                            (r, auth_type), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_ALL(int,protocol_propose,
Packit 90a5c9
                          (conn_rec *c, request_rec *r, server_rec *s,
Packit 90a5c9
                           const apr_array_header_t *offers,
Packit 90a5c9
                           apr_array_header_t *proposals), 
Packit 90a5c9
                          (c, r, s, offers, proposals), OK, DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,protocol_switch,
Packit 90a5c9
                            (conn_rec *c, request_rec *r, server_rec *s,
Packit 90a5c9
                             const char *protocol), 
Packit 90a5c9
                            (c, r, s, protocol), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,protocol_get,
Packit 90a5c9
                            (const conn_rec *c), (c), NULL)