1 /* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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
25 #include "http_config.h"
26 #include "http_connection.h"
27 #include "http_core.h"
28 #include "http_protocol.h" /* For index_of_response(). Grump. */
29 #include "http_request.h"
31 #include "util_filter.h"
32 #include "util_ebcdic.h"
34 #include "scoreboard.h"
38 /* Handles for core filters */
39 AP_DECLARE_DATA ap_filter_rec_t *ap_http_input_filter_handle;
40 AP_DECLARE_DATA ap_filter_rec_t *ap_http_header_filter_handle;
41 AP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle;
42 AP_DECLARE_DATA ap_filter_rec_t *ap_http_outerror_filter_handle;
43 AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
45 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
48 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
53 cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
57 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
60 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
65 /* We've changed it to On/Off, but used to use numbers
66 * so we accept anything but "Off" or "0" as "On"
68 if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
69 cmd->server->keep_alive = 0;
72 cmd->server->keep_alive = 1;
77 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
80 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
85 cmd->server->keep_alive_max = atoi(arg);
89 static const command_rec http_cmds[] = {
90 AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
91 "Keep-Alive timeout duration (sec)"),
92 AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
93 "Maximum number of Keep-Alive requests per connection, "
95 AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
96 "Whether persistent connections should be On or Off"),
100 static const char *http_scheme(const request_rec *r)
103 * The http module shouldn't return anything other than
104 * "http" (the default) or "https".
106 if (r->server->server_scheme &&
107 (strcmp(r->server->server_scheme, "https") == 0))
113 static apr_port_t http_port(const request_rec *r)
115 if (r->server->server_scheme &&
116 (strcmp(r->server->server_scheme, "https") == 0))
117 return DEFAULT_HTTPS_PORT;
119 return DEFAULT_HTTP_PORT;
122 static int ap_process_http_async_connection(conn_rec *c)
125 conn_state_t *cs = c->cs;
127 AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
129 while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
130 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
132 if ((r = ap_read_request(c))) {
134 c->keepalive = AP_CONN_UNKNOWN;
135 /* process the request if it was read without error */
137 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
138 if (r->status == HTTP_OK) {
139 cs->state = CONN_STATE_HANDLER;
140 ap_process_async_request(r);
141 /* After the call to ap_process_request, the
142 * request pool may have been deleted. We set
143 * r=NULL here to ensure that any dereference
144 * of r that might be added later in this function
145 * will result in a segfault immediately instead
146 * of nondeterministic failures later.
151 if (cs->state != CONN_STATE_WRITE_COMPLETION) {
152 /* Something went wrong; close the connection */
153 cs->state = CONN_STATE_LINGER;
156 else { /* ap_read_request failed - client may have closed */
157 cs->state = CONN_STATE_LINGER;
164 static int ap_process_http_connection(conn_rec *c)
167 conn_state_t *cs = c->cs;
168 apr_socket_t *csd = NULL;
171 * Read and process each request found on our connection
172 * until no requests are left or we decide to close.
175 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
176 while ((r = ap_read_request(c)) != NULL) {
178 c->keepalive = AP_CONN_UNKNOWN;
179 /* process the request if it was read without error */
181 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
182 if (r->status == HTTP_OK) {
183 cs->state = CONN_STATE_HANDLER;
184 ap_process_request(r);
185 /* After the call to ap_process_request, the
186 * request pool will have been deleted. We set
187 * r=NULL here to ensure that any dereference
188 * of r that might be added later in this function
189 * will result in a segfault immediately instead
190 * of nondeterministic failures later.
195 if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
198 ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
200 if (ap_graceful_stop_signalled())
204 csd = ap_get_module_config(c->conn_config, &core_module);
206 apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
207 apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
208 /* Go straight to select() to wait for the next request */
214 static int http_create_request(request_rec *r)
216 if (!r->main && !r->prev) {
217 ap_add_output_filter_handle(ap_byterange_filter_handle,
218 NULL, r, r->connection);
219 ap_add_output_filter_handle(ap_content_length_filter_handle,
220 NULL, r, r->connection);
221 ap_add_output_filter_handle(ap_http_header_filter_handle,
222 NULL, r, r->connection);
223 ap_add_output_filter_handle(ap_http_outerror_filter_handle,
224 NULL, r, r->connection);
230 static void register_hooks(apr_pool_t *p)
233 * If we ae using an MPM That Supports Async Connections,
234 * use a different processing function
237 if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS
239 ap_hook_process_connection(ap_process_http_async_connection, NULL,
240 NULL, APR_HOOK_REALLY_LAST);
243 ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
244 APR_HOOK_REALLY_LAST);
247 ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
248 ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
249 ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
250 ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
251 ap_http_input_filter_handle =
252 ap_register_input_filter("HTTP_IN", ap_http_filter,
253 NULL, AP_FTYPE_PROTOCOL);
254 ap_http_header_filter_handle =
255 ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
256 NULL, AP_FTYPE_PROTOCOL);
257 ap_chunk_filter_handle =
258 ap_register_output_filter("CHUNK", ap_http_chunk_filter,
259 NULL, AP_FTYPE_TRANSCODE);
260 ap_http_outerror_filter_handle =
261 ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
262 NULL, AP_FTYPE_PROTOCOL);
263 ap_byterange_filter_handle =
264 ap_register_output_filter("BYTERANGE", ap_byterange_filter,
265 NULL, AP_FTYPE_PROTOCOL);
266 ap_method_registry_init(p);
269 module AP_MODULE_DECLARE_DATA http_module = {
270 STANDARD20_MODULE_STUFF,
271 NULL, /* create per-directory config structure */
272 NULL, /* merge per-directory config structures */
273 NULL, /* create per-server config structure */
274 NULL, /* merge per-server config structures */
275 http_cmds, /* command apr_table_t */
276 register_hooks /* register hooks */