http_core.c revision bdf8801c51d958b1d3ff40f60ab16ccfd999b0fa
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff/* Licensed to the Apache Software Foundation (ASF) under one or more
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * contributor license agreements. See the NOTICE file distributed with
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * this work for additional information regarding copyright ownership.
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * The ASF licenses this file to You under the Apache License, Version 2.0
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * (the "License"); you may not use this file except in compliance with
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * the License. You may obtain a copy of the License at
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff *
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * http://www.apache.org/licenses/LICENSE-2.0
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff *
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * Unless required by applicable law or agreed to in writing, software
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * distributed under the License is distributed on an "AS IS" BASIS,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * See the License for the specific language governing permissions and
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * limitations under the License.
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff */
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "apr_strings.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "apr_thread_proc.h" /* for RLIMIT stuff */
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#define APR_WANT_STRFUNC
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "apr_want.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#define CORE_PRIVATE
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "httpd.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "http_config.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "http_connection.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "http_core.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "http_protocol.h" /* For index_of_response(). Grump. */
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "http_request.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "util_filter.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "util_ebcdic.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "ap_mpm.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "scoreboard.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff#include "mod_core.h"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff/* Handles for core filters */
599c6d44f4d41aab5d3da98214492eb26e674b65Michael GraffAP_DECLARE_DATA ap_filter_rec_t *ap_http_input_filter_handle;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael GraffAP_DECLARE_DATA ap_filter_rec_t *ap_kept_body_input_filter_handle;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael GraffAP_DECLARE_DATA ap_filter_rec_t *ap_http_header_filter_handle;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael GraffAP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael GraffAP_DECLARE_DATA ap_filter_rec_t *ap_http_outerror_filter_handle;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael GraffAP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic int ap_process_http_connection(conn_rec *c);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff const char *arg)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff{
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (err != NULL) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return err;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return NULL;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff}
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic const char *set_keep_alive(cmd_parms *cmd, void *dummy,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff const char *arg)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff{
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (err != NULL) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return err;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff /* We've changed it to On/Off, but used to use numbers
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * so we accept anything but "Off" or "0" as "On"
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff */
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff cmd->server->keep_alive = 0;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff else {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff cmd->server->keep_alive = 1;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return NULL;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff}
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff const char *arg)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff{
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (err != NULL) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return err;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff cmd->server->keep_alive_max = atoi(arg);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return NULL;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff}
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic const char *set_kept_body_size(cmd_parms *cmd, void *dconf,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff const char *arg)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff{
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff core_dir_conf *conf = dconf;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (APR_SUCCESS != apr_strtoff(&(conf->keep_body), arg, NULL, 0)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff || conf->keep_body < 0) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return "KeptBodySize must be a size in bytes, or zero.";
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff conf->keep_body_set = 1;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return NULL;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff}
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic const command_rec http_cmds[] = {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff "Keep-Alive timeout duration (sec)"),
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff "Maximum number of Keep-Alive requests per connection, "
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff "or 0 for infinite"),
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff "Whether persistent connections should be On or Off"),
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff AP_INIT_TAKE1("KeptBodySize", set_kept_body_size, NULL, ACCESS_CONF,
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff "Maximum size of request bodies kept aside for use by filters"),
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff { NULL }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff};
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic const char *http_scheme(const request_rec *r)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff{
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff /*
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * The http module shouldn't return anything other than
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff * "http" (the default) or "https".
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff */
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (r->server->server_scheme &&
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff (strcmp(r->server->server_scheme, "https") == 0))
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return "https";
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return "http";
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff}
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic apr_port_t http_port(const request_rec *r)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff{
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (r->server->server_scheme &&
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff (strcmp(r->server->server_scheme, "https") == 0))
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return DEFAULT_HTTPS_PORT;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return DEFAULT_HTTP_PORT;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff}
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graffstatic int ap_process_http_async_connection(conn_rec *c)
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff{
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff request_rec *r;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff conn_state_t *cs = c->cs;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (c->clogging_input_filters) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff return ap_process_http_connection(c);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff }
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if ((r = ap_read_request(c))) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff c->keepalive = AP_CONN_UNKNOWN;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff /* process the request if it was read without error */
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff if (r->status == HTTP_OK) {
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff cs->state = CONN_STATE_HANDLER;
599c6d44f4d41aab5d3da98214492eb26e674b65Michael Graff ap_process_async_request(r);
/* After the call to ap_process_request, the
* request pool may have been deleted. We set
* r=NULL here to ensure that any dereference
* of r that might be added later in this function
* will result in a segfault immediately instead
* of nondeterministic failures later.
*/
r = NULL;
}
if (cs->state != CONN_STATE_WRITE_COMPLETION) {
/* Something went wrong; close the connection */
cs->state = CONN_STATE_LINGER;
}
}
else { /* ap_read_request failed - client may have closed */
cs->state = CONN_STATE_LINGER;
}
}
return OK;
}
static int ap_process_http_connection(conn_rec *c)
{
request_rec *r;
conn_state_t *cs = c->cs;
apr_socket_t *csd = NULL;
/*
* Read and process each request found on our connection
* until no requests are left or we decide to close.
*/
ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
while ((r = ap_read_request(c)) != NULL) {
c->keepalive = AP_CONN_UNKNOWN;
/* process the request if it was read without error */
ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
if (r->status == HTTP_OK) {
cs->state = CONN_STATE_HANDLER;
ap_process_request(r);
/* After the call to ap_process_request, the
* request pool will have been deleted. We set
* r=NULL here to ensure that any dereference
* of r that might be added later in this function
* will result in a segfault immediately instead
* of nondeterministic failures later.
*/
r = NULL;
}
if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
break;
ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
if (ap_graceful_stop_signalled())
break;
if (!csd) {
csd = ap_get_module_config(c->conn_config, &core_module);
}
apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
/* Go straight to select() to wait for the next request */
}
return OK;
}
static int http_create_request(request_rec *r)
{
if (!r->main && !r->prev) {
ap_add_output_filter_handle(ap_byterange_filter_handle,
NULL, r, r->connection);
ap_add_output_filter_handle(ap_content_length_filter_handle,
NULL, r, r->connection);
ap_add_output_filter_handle(ap_http_header_filter_handle,
NULL, r, r->connection);
ap_add_output_filter_handle(ap_http_outerror_filter_handle,
NULL, r, r->connection);
}
return OK;
}
static int http_send_options(request_rec *r)
{
if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
(r->uri[1] == '\0')) {
return DONE; /* Send HTTP pong, without Allow header */
}
return DECLINED;
}
static void register_hooks(apr_pool_t *p)
{
/**
* If we ae using an MPM That Supports Async Connections,
* use a different processing function
*/
int async_mpm = 0;
if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS
&& async_mpm == 1) {
ap_hook_process_connection(ap_process_http_async_connection, NULL,
NULL, APR_HOOK_REALLY_LAST);
}
else {
ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
APR_HOOK_REALLY_LAST);
}
ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
ap_hook_map_to_storage(http_send_options,NULL,NULL,APR_HOOK_MIDDLE);
ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
ap_http_input_filter_handle =
ap_register_input_filter("HTTP_IN", ap_http_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_kept_body_input_filter_handle =
ap_register_input_filter("KEPT_BODY", ap_kept_body_filter,
ap_kept_body_filter_init, AP_FTYPE_RESOURCE);
ap_http_header_filter_handle =
ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_chunk_filter_handle =
ap_register_output_filter("CHUNK", ap_http_chunk_filter,
NULL, AP_FTYPE_TRANSCODE);
ap_http_outerror_filter_handle =
ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_byterange_filter_handle =
ap_register_output_filter("BYTERANGE", ap_byterange_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_method_registry_init(p);
}
static void *create_core_dir_config(apr_pool_t *p, char *dummy)
{
core_dir_conf *new =
(core_dir_conf *) apr_pcalloc(p, sizeof(core_dir_conf));
new->keep_body_set = 0; /* unset */
new->keep_body = 0; /* don't by default */
return (void *) new;
}
static void *merge_core_dir_config(apr_pool_t *p, void *basev, void *addv)
{
core_dir_conf *new = (core_dir_conf *) apr_pcalloc(p, sizeof(core_dir_conf));
core_dir_conf *add = (core_dir_conf *) addv;
core_dir_conf *base = (core_dir_conf *) basev;
new->keep_body = (add->keep_body_set == 0) ? base->keep_body : add->keep_body;
new->keep_body_set = add->keep_body_set || base->keep_body_set;
return new;
}
module AP_MODULE_DECLARE_DATA http_module = {
STANDARD20_MODULE_STUFF,
create_core_dir_config, /* create per-directory config structure */
merge_core_dir_config, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
http_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};