]> granicus.if.org Git - apache/blob - modules/http/http_core.c
Update the copyright year in all .c, .h and .xml files
[apache] / modules / http / http_core.c
1 /* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "apr_strings.h"
18 #include "apr_thread_proc.h"    /* for RLIMIT stuff */
19
20 #define APR_WANT_STRFUNC
21 #include "apr_want.h"
22
23 #define CORE_PRIVATE
24 #include "httpd.h"
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"
30
31 #include "util_filter.h"
32 #include "util_ebcdic.h"
33 #include "ap_mpm.h"
34 #include "scoreboard.h"
35
36 #include "mod_core.h"
37
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;
44
45 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
46                                           const char *arg)
47 {
48     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
49     if (err != NULL) {
50         return err;
51     }
52
53     cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
54     return NULL;
55 }
56
57 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
58                                   const char *arg)
59 {
60     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
61     if (err != NULL) {
62         return err;
63     }
64
65     /* We've changed it to On/Off, but used to use numbers
66      * so we accept anything but "Off" or "0" as "On"
67      */
68     if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
69         cmd->server->keep_alive = 0;
70     }
71     else {
72         cmd->server->keep_alive = 1;
73     }
74     return NULL;
75 }
76
77 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
78                                       const char *arg)
79 {
80     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
81     if (err != NULL) {
82         return err;
83     }
84
85     cmd->server->keep_alive_max = atoi(arg);
86     return NULL;
87 }
88
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, "
94                   "or 0 for infinite"),
95     AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
96                   "Whether persistent connections should be On or Off"),
97     { NULL }
98 };
99
100 static const char *http_scheme(const request_rec *r)
101 {
102     return "http";
103 }
104
105 static apr_port_t http_port(const request_rec *r)
106 {
107     return DEFAULT_HTTP_PORT;
108 }
109
110 static int ap_process_http_async_connection(conn_rec *c)
111 {
112     request_rec *r;
113     conn_state_t *cs = c->cs;
114
115     AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
116
117     while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
118         ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
119
120         if ((r = ap_read_request(c))) {
121
122             c->keepalive = AP_CONN_UNKNOWN;
123             /* process the request if it was read without error */
124
125             ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
126             if (r->status == HTTP_OK) {
127                 cs->state = CONN_STATE_HANDLER;
128                 ap_process_async_request(r);
129                 /* After the call to ap_process_request, the
130                  * request pool may have been deleted.  We set
131                  * r=NULL here to ensure that any dereference
132                  * of r that might be added later in this function
133                  * will result in a segfault immediately instead
134                  * of nondeterministic failures later.
135                  */
136                 r = NULL;
137             }
138
139             if (cs->state != CONN_STATE_WRITE_COMPLETION) {
140                 /* Something went wrong; close the connection */
141                 cs->state = CONN_STATE_LINGER;
142             }
143         }
144         else {   /* ap_read_request failed - client may have closed */
145             cs->state = CONN_STATE_LINGER;
146         }
147     }
148
149     return OK;
150 }
151
152 static int ap_process_http_connection(conn_rec *c)
153 {
154     request_rec *r;
155     conn_state_t *cs = c->cs;
156     apr_socket_t *csd = NULL;
157
158     /*
159      * Read and process each request found on our connection
160      * until no requests are left or we decide to close.
161      */
162
163     ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
164     while ((r = ap_read_request(c)) != NULL) {
165
166         c->keepalive = AP_CONN_UNKNOWN;
167         /* process the request if it was read without error */
168
169         ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
170         if (r->status == HTTP_OK) {
171             cs->state = CONN_STATE_HANDLER;
172             ap_process_request(r);
173             /* After the call to ap_process_request, the
174              * request pool will have been deleted.  We set
175              * r=NULL here to ensure that any dereference
176              * of r that might be added later in this function
177              * will result in a segfault immediately instead
178              * of nondeterministic failures later.
179              */
180             r = NULL;
181         }
182
183         if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
184             break;
185
186         ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
187
188         if (ap_graceful_stop_signalled())
189             break;
190
191         if (!csd) {
192             csd = ap_get_module_config(c->conn_config, &core_module);
193         }
194         apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
195         apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
196         /* Go straight to select() to wait for the next request */
197     }
198
199     return OK;
200 }
201
202 static int http_create_request(request_rec *r)
203 {
204     if (!r->main && !r->prev) {
205         ap_add_output_filter_handle(ap_byterange_filter_handle,
206                                     NULL, r, r->connection);
207         ap_add_output_filter_handle(ap_content_length_filter_handle,
208                                     NULL, r, r->connection);
209         ap_add_output_filter_handle(ap_http_header_filter_handle,
210                                     NULL, r, r->connection);
211         ap_add_output_filter_handle(ap_http_outerror_filter_handle,
212                                     NULL, r, r->connection);
213     }
214
215     return OK;
216 }
217
218 static void register_hooks(apr_pool_t *p)
219 {
220     /**
221      * If we ae using an MPM That Supports Async Connections,
222      * use a different processing function
223      */
224     int async_mpm = 0;
225     if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS
226         && async_mpm == 1) {
227         ap_hook_process_connection(ap_process_http_async_connection, NULL,
228                                    NULL, APR_HOOK_REALLY_LAST);
229     }
230     else {
231         ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
232                                    APR_HOOK_REALLY_LAST);
233     }
234
235     ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
236     ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
237     ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
238     ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
239     ap_http_input_filter_handle =
240         ap_register_input_filter("HTTP_IN", ap_http_filter,
241                                  NULL, AP_FTYPE_PROTOCOL);
242     ap_http_header_filter_handle =
243         ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
244                                   NULL, AP_FTYPE_PROTOCOL);
245     ap_chunk_filter_handle =
246         ap_register_output_filter("CHUNK", ap_http_chunk_filter,
247                                   NULL, AP_FTYPE_TRANSCODE);
248     ap_http_outerror_filter_handle =
249         ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
250                                   NULL, AP_FTYPE_PROTOCOL);
251     ap_byterange_filter_handle =
252         ap_register_output_filter("BYTERANGE", ap_byterange_filter,
253                                   NULL, AP_FTYPE_PROTOCOL);
254     ap_method_registry_init(p);
255 }
256
257 module AP_MODULE_DECLARE_DATA http_module = {
258     STANDARD20_MODULE_STUFF,
259     NULL,              /* create per-directory config structure */
260     NULL,              /* merge per-directory config structures */
261     NULL,              /* create per-server config structure */
262     NULL,              /* merge per-server config structures */
263     http_cmds,         /* command apr_table_t */
264     register_hooks     /* register hooks */
265 };