1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_strings.h"
18 #include "apr_thread_proc.h" /* for RLIMIT stuff */
20 #define APR_WANT_STRFUNC
24 #include "http_config.h"
25 #include "http_connection.h"
26 #include "http_core.h"
27 #include "http_protocol.h" /* For index_of_response(). Grump. */
28 #include "http_request.h"
30 #include "util_filter.h"
31 #include "util_ebcdic.h"
33 #include "scoreboard.h"
37 /* Handles for core filters */
38 AP_DECLARE_DATA ap_filter_rec_t *ap_http_input_filter_handle;
39 AP_DECLARE_DATA ap_filter_rec_t *ap_http_header_filter_handle;
40 AP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle;
41 AP_DECLARE_DATA ap_filter_rec_t *ap_http_outerror_filter_handle;
42 AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
44 static int ap_process_http_connection(conn_rec *c);
47 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
50 apr_interval_time_t timeout;
51 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
56 /* Stolen from mod_proxy.c */
57 if (ap_timeout_parameter_parse(arg, &timeout, "s") != APR_SUCCESS)
58 return "KeepAliveTimeout has wrong format";
59 cmd->server->keep_alive_timeout = timeout;
63 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
66 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
71 /* We've changed it to On/Off, but used to use numbers
72 * so we accept anything but "Off" or "0" as "On"
74 if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
75 cmd->server->keep_alive = 0;
78 cmd->server->keep_alive = 1;
83 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
86 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
91 cmd->server->keep_alive_max = atoi(arg);
95 static const command_rec http_cmds[] = {
96 AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
97 "Keep-Alive timeout duration (sec)"),
98 AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
99 "Maximum number of Keep-Alive requests per connection, "
100 "or 0 for infinite"),
101 AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
102 "Whether persistent connections should be On or Off"),
106 static const char *http_scheme(const request_rec *r)
109 * The http module shouldn't return anything other than
110 * "http" (the default) or "https".
112 if (r->server->server_scheme &&
113 (strcmp(r->server->server_scheme, "https") == 0))
119 static apr_port_t http_port(const request_rec *r)
121 if (r->server->server_scheme &&
122 (strcmp(r->server->server_scheme, "https") == 0))
123 return DEFAULT_HTTPS_PORT;
125 return DEFAULT_HTTP_PORT;
128 static int ap_process_http_async_connection(conn_rec *c)
131 conn_state_t *cs = c->cs;
133 if (c->clogging_input_filters) {
134 return ap_process_http_connection(c);
137 AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
139 while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
140 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
142 if ((r = ap_read_request(c))) {
144 c->keepalive = AP_CONN_UNKNOWN;
145 /* process the request if it was read without error */
147 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
148 if (r->status == HTTP_OK) {
149 cs->state = CONN_STATE_HANDLER;
150 ap_process_async_request(r);
151 /* After the call to ap_process_request, the
152 * request pool may have been deleted. We set
153 * r=NULL here to ensure that any dereference
154 * of r that might be added later in this function
155 * will result in a segfault immediately instead
156 * of nondeterministic failures later.
161 if (cs->state != CONN_STATE_WRITE_COMPLETION &&
162 cs->state != CONN_STATE_SUSPENDED) {
163 /* Something went wrong; close the connection */
164 cs->state = CONN_STATE_LINGER;
167 else { /* ap_read_request failed - client may have closed */
168 cs->state = CONN_STATE_LINGER;
175 static int ap_process_http_connection(conn_rec *c)
178 conn_state_t *cs = c->cs;
179 apr_socket_t *csd = NULL;
183 * Read and process each request found on our connection
184 * until no requests are left or we decide to close.
187 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
188 while ((r = ap_read_request(c)) != NULL) {
190 c->keepalive = AP_CONN_UNKNOWN;
191 /* process the request if it was read without error */
193 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
194 if (r->status == HTTP_OK) {
195 cs->state = CONN_STATE_HANDLER;
196 ap_process_request(r);
197 /* After the call to ap_process_request, the
198 * request pool will have been deleted. We set
199 * r=NULL here to ensure that any dereference
200 * of r that might be added later in this function
201 * will result in a segfault immediately instead
202 * of nondeterministic failures later.
207 if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
210 ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
212 if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state)) {
216 if (mpm_state == AP_MPMQ_STOPPING) {
221 csd = ap_get_module_config(c->conn_config, &core_module);
223 apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
224 apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
225 /* Go straight to select() to wait for the next request */
231 static int http_create_request(request_rec *r)
233 if (!r->main && !r->prev) {
234 ap_add_output_filter_handle(ap_byterange_filter_handle,
235 NULL, r, r->connection);
236 ap_add_output_filter_handle(ap_content_length_filter_handle,
237 NULL, r, r->connection);
238 ap_add_output_filter_handle(ap_http_header_filter_handle,
239 NULL, r, r->connection);
240 ap_add_output_filter_handle(ap_http_outerror_filter_handle,
241 NULL, r, r->connection);
247 static int http_send_options(request_rec *r)
249 if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
250 (r->uri[1] == '\0')) {
251 return DONE; /* Send HTTP pong, without Allow header */
256 static void register_hooks(apr_pool_t *p)
259 * If we ae using an MPM That Supports Async Connections,
260 * use a different processing function
263 if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS
265 ap_hook_process_connection(ap_process_http_async_connection, NULL,
266 NULL, APR_HOOK_REALLY_LAST);
269 ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
270 APR_HOOK_REALLY_LAST);
273 ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
274 ap_hook_map_to_storage(http_send_options,NULL,NULL,APR_HOOK_MIDDLE);
275 ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
276 ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
277 ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
278 ap_http_input_filter_handle =
279 ap_register_input_filter("HTTP_IN", ap_http_filter,
280 NULL, AP_FTYPE_PROTOCOL);
281 ap_http_header_filter_handle =
282 ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
283 NULL, AP_FTYPE_PROTOCOL);
284 ap_chunk_filter_handle =
285 ap_register_output_filter("CHUNK", ap_http_chunk_filter,
286 NULL, AP_FTYPE_TRANSCODE);
287 ap_http_outerror_filter_handle =
288 ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
289 NULL, AP_FTYPE_PROTOCOL);
290 ap_byterange_filter_handle =
291 ap_register_output_filter("BYTERANGE", ap_byterange_filter,
292 NULL, AP_FTYPE_PROTOCOL);
293 ap_method_registry_init(p);
296 module AP_MODULE_DECLARE_DATA http_module = {
297 STANDARD20_MODULE_STUFF,
298 NULL, /* create per-directory config structure */
299 NULL, /* merge per-directory config structures */
300 NULL, /* create per-server config structure */
301 NULL, /* merge per-server config structures */
302 http_cmds, /* command apr_table_t */
303 register_hooks /* register hooks */