1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "apr_strings.h"
17 #include "apr_thread_proc.h" /* for RLIMIT stuff */
19 #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_byterange_filter_handle;
43 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
46 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
51 cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
55 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
58 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
63 /* We've changed it to On/Off, but used to use numbers
64 * so we accept anything but "Off" or "0" as "On"
66 if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
67 cmd->server->keep_alive = 0;
70 cmd->server->keep_alive = 1;
75 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
78 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
83 cmd->server->keep_alive_max = atoi(arg);
87 static const command_rec http_cmds[] = {
88 AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
89 "Keep-Alive timeout duration (sec)"),
90 AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
91 "Maximum number of Keep-Alive requests per connection, "
93 AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
94 "Whether persistent connections should be On or Off"),
98 static const char *http_scheme(const request_rec *r)
103 static apr_port_t http_port(const request_rec *r)
105 return DEFAULT_HTTP_PORT;
108 static int ap_process_http_async_connection(conn_rec *c)
111 conn_state_t *cs = c->cs;
113 AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
115 while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
116 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
118 if ((r = ap_read_request(c))) {
120 c->keepalive = AP_CONN_UNKNOWN;
121 /* process the request if it was read without error */
123 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
124 if (r->status == HTTP_OK)
125 ap_process_request(r);
127 if (ap_extended_status)
128 ap_increment_counts(c->sbh, r);
130 if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted
131 || ap_graceful_stop_signalled()) {
132 cs->state = CONN_STATE_LINGER;
134 else if (!c->data_in_input_filters) {
135 cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
138 /* else we are pipelining. Stay in READ_REQUEST_LINE state
139 * and stay in the loop
142 apr_pool_destroy(r->pool);
144 else { /* ap_read_request failed - client may have closed */
145 cs->state = CONN_STATE_LINGER;
152 static int ap_process_http_connection(conn_rec *c)
156 apr_socket_t *csd = NULL;
159 * Read and process each request found on our connection
160 * until no requests are left or we decide to close.
163 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
164 while ((r = ap_read_request(c)) != NULL) {
166 c->keepalive = AP_CONN_UNKNOWN;
167 /* process the request if it was read without error */
169 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
170 if (r->status == HTTP_OK)
171 ap_process_request(r);
173 if (ap_extended_status)
174 ap_increment_counts(c->sbh, r);
176 if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
179 ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, r);
180 apr_pool_destroy(r->pool);
182 if (ap_graceful_stop_signalled())
184 /* Go straight to select() to wait for the next request */
186 csd = ap_get_module_config(c->conn_config, &core_module);
189 apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
195 static int http_create_request(request_rec *r)
197 if (!r->main && !r->prev) {
198 ap_add_output_filter_handle(ap_byterange_filter_handle,
199 NULL, r, r->connection);
200 ap_add_output_filter_handle(ap_content_length_filter_handle,
201 NULL, r, r->connection);
202 ap_add_output_filter_handle(ap_http_header_filter_handle,
203 NULL, r, r->connection);
209 static void register_hooks(apr_pool_t *p)
212 * If we ae using an MPM That Supports Async Connections,
213 * use a different processing function
216 if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS
218 ap_hook_process_connection(ap_process_http_async_connection, NULL,
219 NULL, APR_HOOK_REALLY_LAST);
222 ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
223 APR_HOOK_REALLY_LAST);
226 ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
227 ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
228 ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
229 ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
230 ap_http_input_filter_handle =
231 ap_register_input_filter("HTTP_IN", ap_http_filter,
232 NULL, AP_FTYPE_PROTOCOL);
233 ap_http_header_filter_handle =
234 ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
235 NULL, AP_FTYPE_PROTOCOL);
236 ap_chunk_filter_handle =
237 ap_register_output_filter("CHUNK", ap_http_chunk_filter,
238 NULL, AP_FTYPE_TRANSCODE);
239 ap_byterange_filter_handle =
240 ap_register_output_filter("BYTERANGE", ap_byterange_filter,
241 NULL, AP_FTYPE_PROTOCOL);
242 ap_method_registry_init(p);
245 module AP_MODULE_DECLARE_DATA http_module = {
246 STANDARD20_MODULE_STUFF,
247 NULL, /* create per-directory config structure */
248 NULL, /* merge per-directory config structures */
249 NULL, /* create per-server config structure */
250 NULL, /* merge per-server config structures */
251 http_cmds, /* command apr_table_t */
252 register_hooks /* register hooks */