Blame modules/http/http_core.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
#include "apr_strings.h"
Packit 90a5c9
#include "apr_thread_proc.h"    /* for RLIMIT stuff */
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_connection.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_protocol.h"   /* For index_of_response().  Grump. */
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "util_ebcdic.h"
Packit 90a5c9
#include "ap_mpm.h"
Packit 90a5c9
#include "scoreboard.h"
Packit 90a5c9
Packit 90a5c9
#include "mod_core.h"
Packit 90a5c9
Packit 90a5c9
/* Handles for core filters */
Packit 90a5c9
AP_DECLARE_DATA ap_filter_rec_t *ap_http_input_filter_handle;
Packit 90a5c9
AP_DECLARE_DATA ap_filter_rec_t *ap_http_header_filter_handle;
Packit 90a5c9
AP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle;
Packit 90a5c9
AP_DECLARE_DATA ap_filter_rec_t *ap_http_outerror_filter_handle;
Packit 90a5c9
AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_DATA const char *ap_multipart_boundary;
Packit 90a5c9
Packit 90a5c9
/* If we are using an MPM That Supports Async Connections,
Packit 90a5c9
 * use a different processing function
Packit 90a5c9
 */
Packit 90a5c9
static int async_mpm = 0;
Packit 90a5c9
Packit 90a5c9
static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
Packit 90a5c9
                                          const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    apr_interval_time_t timeout;
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Stolen from mod_proxy.c */
Packit 90a5c9
    if (ap_timeout_parameter_parse(arg, &timeout, "s") != APR_SUCCESS)
Packit 90a5c9
        return "KeepAliveTimeout has wrong format";
Packit 90a5c9
    cmd->server->keep_alive_timeout = timeout;
Packit 90a5c9
Packit 90a5c9
    /* We don't want to take into account whether or not KeepAliveTimeout is
Packit 90a5c9
     * set for the main server, because if no http_module directive is used
Packit 90a5c9
     * for a vhost, it will inherit the http_srv_cfg from the main server.
Packit 90a5c9
     * However keep_alive_timeout_set helps determine whether the vhost should
Packit 90a5c9
     * use its own configured timeout or the one from the vhost declared first
Packit 90a5c9
     * on the same IP:port (ie. c->base_server, and the legacy behaviour).
Packit 90a5c9
     */
Packit 90a5c9
    if (cmd->server->is_virtual) {
Packit 90a5c9
        cmd->server->keep_alive_timeout_set = 1;
Packit 90a5c9
    }
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
Packit 90a5c9
                                  int arg)
Packit 90a5c9
{
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cmd->server->keep_alive = arg;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
Packit 90a5c9
                                      const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cmd->server->keep_alive_max = atoi(arg);
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec http_cmds[] = {
Packit 90a5c9
    AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
Packit 90a5c9
                  "Keep-Alive timeout duration (sec)"),
Packit 90a5c9
    AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
Packit 90a5c9
                  "Maximum number of Keep-Alive requests per connection, "
Packit 90a5c9
                  "or 0 for infinite"),
Packit 90a5c9
    AP_INIT_FLAG("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
Packit 90a5c9
                  "Whether persistent connections should be On or Off"),
Packit 90a5c9
    { NULL }
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static const char *http_scheme(const request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    /*
Packit 90a5c9
     * The http module shouldn't return anything other than
Packit 90a5c9
     * "http" (the default) or "https".
Packit 90a5c9
     */
Packit 90a5c9
    if (r->server->server_scheme &&
Packit 90a5c9
        (strcmp(r->server->server_scheme, "https") == 0))
Packit 90a5c9
        return "https";
Packit 90a5c9
Packit 90a5c9
    return "http";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_port_t http_port(const request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if (r->server->server_scheme &&
Packit 90a5c9
        (strcmp(r->server->server_scheme, "https") == 0))
Packit 90a5c9
        return DEFAULT_HTTPS_PORT;
Packit 90a5c9
Packit 90a5c9
    return DEFAULT_HTTP_PORT;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int ap_process_http_async_connection(conn_rec *c)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r = NULL;
Packit 90a5c9
    conn_state_t *cs = c->cs;
Packit 90a5c9
Packit 90a5c9
    AP_DEBUG_ASSERT(cs != NULL);
Packit 90a5c9
    AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
Packit 90a5c9
Packit 90a5c9
    while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
Packit 90a5c9
        ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
Packit 90a5c9
Packit 90a5c9
        if ((r = ap_read_request(c))) {
Packit 90a5c9
Packit 90a5c9
            c->keepalive = AP_CONN_UNKNOWN;
Packit 90a5c9
            /* process the request if it was read without error */
Packit 90a5c9
Packit 90a5c9
            if (r->status == HTTP_OK) {
Packit 90a5c9
                cs->state = CONN_STATE_HANDLER;
Packit 90a5c9
                ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
Packit 90a5c9
                ap_process_async_request(r);
Packit 90a5c9
                /* After the call to ap_process_request, the
Packit 90a5c9
                 * request pool may have been deleted.  We set
Packit 90a5c9
                 * r=NULL here to ensure that any dereference
Packit 90a5c9
                 * of r that might be added later in this function
Packit 90a5c9
                 * will result in a segfault immediately instead
Packit 90a5c9
                 * of nondeterministic failures later.
Packit 90a5c9
                 */
Packit 90a5c9
                r = NULL;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (cs->state != CONN_STATE_WRITE_COMPLETION &&
Packit 90a5c9
                cs->state != CONN_STATE_SUSPENDED) {
Packit 90a5c9
                /* Something went wrong; close the connection */
Packit 90a5c9
                cs->state = CONN_STATE_LINGER;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {   /* ap_read_request failed - client may have closed */
Packit 90a5c9
            cs->state = CONN_STATE_LINGER;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int ap_process_http_sync_connection(conn_rec *c)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r;
Packit 90a5c9
    conn_state_t *cs = c->cs;
Packit 90a5c9
    apr_socket_t *csd = NULL;
Packit 90a5c9
    int mpm_state = 0;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Read and process each request found on our connection
Packit 90a5c9
     * until no requests are left or we decide to close.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
Packit 90a5c9
    while ((r = ap_read_request(c)) != NULL) {
Packit 90a5c9
        apr_interval_time_t keep_alive_timeout = r->server->keep_alive_timeout;
Packit 90a5c9
Packit 90a5c9
        /* To preserve legacy behaviour, use the keepalive timeout from the
Packit 90a5c9
         * base server (first on this IP:port) when none is explicitly
Packit 90a5c9
         * configured on this server.
Packit 90a5c9
         */
Packit 90a5c9
        if (!r->server->keep_alive_timeout_set) {
Packit 90a5c9
            keep_alive_timeout = c->base_server->keep_alive_timeout;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        c->keepalive = AP_CONN_UNKNOWN;
Packit 90a5c9
        /* process the request if it was read without error */
Packit 90a5c9
Packit 90a5c9
        if (r->status == HTTP_OK) {
Packit 90a5c9
            if (cs)
Packit 90a5c9
                cs->state = CONN_STATE_HANDLER;
Packit 90a5c9
            ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
Packit 90a5c9
            ap_process_request(r);
Packit 90a5c9
            /* After the call to ap_process_request, the
Packit 90a5c9
             * request pool will have been deleted.  We set
Packit 90a5c9
             * r=NULL here to ensure that any dereference
Packit 90a5c9
             * of r that might be added later in this function
Packit 90a5c9
             * will result in a segfault immediately instead
Packit 90a5c9
             * of nondeterministic failures later.
Packit 90a5c9
             */
Packit 90a5c9
            r = NULL;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
Packit 90a5c9
            break;
Packit 90a5c9
Packit 90a5c9
        ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
Packit 90a5c9
Packit 90a5c9
        if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state)) {
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (mpm_state == AP_MPMQ_STOPPING) {
Packit 90a5c9
          break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (!csd) {
Packit 90a5c9
            csd = ap_get_conn_socket(c);
Packit 90a5c9
        }
Packit 90a5c9
        apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
Packit 90a5c9
        apr_socket_timeout_set(csd, keep_alive_timeout);
Packit 90a5c9
        /* Go straight to select() to wait for the next request */
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int ap_process_http_connection(conn_rec *c)
Packit 90a5c9
{
Packit 90a5c9
    if (async_mpm && !c->clogging_input_filters) {
Packit 90a5c9
        return ap_process_http_async_connection(c);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        return ap_process_http_sync_connection(c);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int http_create_request(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if (!r->main && !r->prev) {
Packit 90a5c9
        ap_add_output_filter_handle(ap_byterange_filter_handle,
Packit 90a5c9
                                    NULL, r, r->connection);
Packit 90a5c9
        ap_add_output_filter_handle(ap_content_length_filter_handle,
Packit 90a5c9
                                    NULL, r, r->connection);
Packit 90a5c9
        ap_add_output_filter_handle(ap_http_header_filter_handle,
Packit 90a5c9
                                    NULL, r, r->connection);
Packit 90a5c9
        ap_add_output_filter_handle(ap_http_outerror_filter_handle,
Packit 90a5c9
                                    NULL, r, r->connection);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int http_send_options(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
Packit 90a5c9
         (r->uri[1] == '\0')) {
Packit 90a5c9
        return DONE;           /* Send HTTP pong, without Allow header */
Packit 90a5c9
    }
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int http_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    apr_uint64_t val;
Packit 90a5c9
    if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) != APR_SUCCESS) {
Packit 90a5c9
        async_mpm = 0;
Packit 90a5c9
    }
Packit 90a5c9
    ap_random_insecure_bytes(&val, sizeof(val));
Packit 90a5c9
    ap_multipart_boundary = apr_psprintf(p, "%0" APR_UINT64_T_HEX_FMT, val);
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_post_config(http_post_config, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
Packit 90a5c9
                               APR_HOOK_REALLY_LAST);
Packit 90a5c9
    ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_map_to_storage(http_send_options,NULL,NULL,APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
Packit 90a5c9
    ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
Packit 90a5c9
    ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
Packit 90a5c9
    ap_http_input_filter_handle =
Packit 90a5c9
        ap_register_input_filter("HTTP_IN", ap_http_filter,
Packit 90a5c9
                                 NULL, AP_FTYPE_PROTOCOL);
Packit 90a5c9
    ap_http_header_filter_handle =
Packit 90a5c9
        ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
Packit 90a5c9
                                  NULL, AP_FTYPE_PROTOCOL);
Packit 90a5c9
    ap_chunk_filter_handle =
Packit 90a5c9
        ap_register_output_filter("CHUNK", ap_http_chunk_filter,
Packit 90a5c9
                                  NULL, AP_FTYPE_TRANSCODE);
Packit 90a5c9
    ap_http_outerror_filter_handle =
Packit 90a5c9
        ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
Packit 90a5c9
                                  NULL, AP_FTYPE_PROTOCOL);
Packit 90a5c9
    ap_byterange_filter_handle =
Packit 90a5c9
        ap_register_output_filter("BYTERANGE", ap_byterange_filter,
Packit 90a5c9
                                  NULL, AP_FTYPE_PROTOCOL);
Packit 90a5c9
    ap_method_registry_init(p);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(http) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    NULL,              /* create per-directory config structure */
Packit 90a5c9
    NULL,              /* merge per-directory config structures */
Packit 90a5c9
    NULL,              /* create per-server config structure */
Packit 90a5c9
    NULL,              /* merge per-server config structures */
Packit 90a5c9
    http_cmds,         /* command apr_table_t */
Packit 90a5c9
    register_hooks     /* register hooks */
Packit 90a5c9
};