1 /* Copyright 1999-2005 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_byterange_filter_handle;
44 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
47 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
52 cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
56 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
59 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
64 /* We've changed it to On/Off, but used to use numbers
65 * so we accept anything but "Off" or "0" as "On"
67 if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
68 cmd->server->keep_alive = 0;
71 cmd->server->keep_alive = 1;
76 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
79 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
84 cmd->server->keep_alive_max = atoi(arg);
88 static const command_rec http_cmds[] = {
89 AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
90 "Keep-Alive timeout duration (sec)"),
91 AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
92 "Maximum number of Keep-Alive requests per connection, "
94 AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
95 "Whether persistent connections should be On or Off"),
99 static const char *http_scheme(const request_rec *r)
104 static apr_port_t http_port(const request_rec *r)
106 return DEFAULT_HTTP_PORT;
109 static int ap_process_http_async_connection(conn_rec *c)
112 conn_state_t *cs = c->cs;
114 AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
116 while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
117 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
119 if ((r = ap_read_request(c))) {
121 c->keepalive = AP_CONN_UNKNOWN;
122 /* process the request if it was read without error */
124 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
125 if (r->status == HTTP_OK) {
126 cs->state = CONN_STATE_HANDLER;
127 ap_process_async_request(r);
128 /* After the call to ap_process_request, the
129 * request pool may have been deleted. We set
130 * r=NULL here to ensure that any dereference
131 * of r that might be added later in this function
132 * will result in a segfault immediately instead
133 * of nondeterministic failures later.
138 if (cs->state != CONN_STATE_WRITE_COMPLETION) {
139 /* Something went wrong; close the connection */
140 cs->state = CONN_STATE_LINGER;
143 else { /* ap_read_request failed - client may have closed */
144 cs->state = CONN_STATE_LINGER;
151 static int ap_process_http_connection(conn_rec *c)
154 conn_state_t *cs = c->cs;
155 apr_socket_t *csd = NULL;
158 * Read and process each request found on our connection
159 * until no requests are left or we decide to close.
162 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
163 while ((r = ap_read_request(c)) != NULL) {
165 c->keepalive = AP_CONN_UNKNOWN;
166 /* process the request if it was read without error */
168 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
169 if (r->status == HTTP_OK) {
170 cs->state = CONN_STATE_HANDLER;
171 ap_process_request(r);
172 /* After the call to ap_process_request, the
173 * request pool will have been deleted. We set
174 * r=NULL here to ensure that any dereference
175 * of r that might be added later in this function
176 * will result in a segfault immediately instead
177 * of nondeterministic failures later.
182 if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
185 ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
187 if (ap_graceful_stop_signalled())
191 csd = ap_get_module_config(c->conn_config, &core_module);
193 apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
194 apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
195 /* Go straight to select() to wait for the next request */
201 static int http_create_request(request_rec *r)
203 if (!r->main && !r->prev) {
204 ap_add_output_filter_handle(ap_byterange_filter_handle,
205 NULL, r, r->connection);
206 ap_add_output_filter_handle(ap_content_length_filter_handle,
207 NULL, r, r->connection);
208 ap_add_output_filter_handle(ap_http_header_filter_handle,
209 NULL, r, r->connection);
215 static void register_hooks(apr_pool_t *p)
218 * If we ae using an MPM That Supports Async Connections,
219 * use a different processing function
222 if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS
224 ap_hook_process_connection(ap_process_http_async_connection, NULL,
225 NULL, APR_HOOK_REALLY_LAST);
228 ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
229 APR_HOOK_REALLY_LAST);
232 ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
233 ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
234 ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
235 ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
236 ap_http_input_filter_handle =
237 ap_register_input_filter("HTTP_IN", ap_http_filter,
238 NULL, AP_FTYPE_PROTOCOL);
239 ap_http_header_filter_handle =
240 ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
241 NULL, AP_FTYPE_PROTOCOL);
242 ap_chunk_filter_handle =
243 ap_register_output_filter("CHUNK", ap_http_chunk_filter,
244 NULL, AP_FTYPE_TRANSCODE);
245 ap_byterange_filter_handle =
246 ap_register_output_filter("BYTERANGE", ap_byterange_filter,
247 NULL, AP_FTYPE_PROTOCOL);
248 ap_method_registry_init(p);
251 module AP_MODULE_DECLARE_DATA http_module = {
252 STANDARD20_MODULE_STUFF,
253 NULL, /* create per-directory config structure */
254 NULL, /* merge per-directory config structures */
255 NULL, /* create per-server config structure */
256 NULL, /* merge per-server config structures */
257 http_cmds, /* command apr_table_t */
258 register_hooks /* register hooks */