]> granicus.if.org Git - apache/blob - modules/http/http_core.c
Update copyright year to 2005 and standardize on current copyright owner line.
[apache] / modules / http / http_core.c
1 /* Copyright 1999-2005 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_byterange_filter_handle;
43
44 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
45                                           const char *arg)
46 {
47     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
48     if (err != NULL) {
49         return err;
50     }
51
52     cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
53     return NULL;
54 }
55
56 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
57                                   const char *arg) 
58 {
59     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
60     if (err != NULL) {
61         return err;
62     }
63
64     /* We've changed it to On/Off, but used to use numbers
65      * so we accept anything but "Off" or "0" as "On"
66      */
67     if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
68         cmd->server->keep_alive = 0;
69     }
70     else {
71         cmd->server->keep_alive = 1;
72     }
73     return NULL;
74 }
75
76 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
77                                       const char *arg)
78 {
79     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
80     if (err != NULL) {
81         return err;
82     }
83
84     cmd->server->keep_alive_max = atoi(arg);
85     return NULL;
86 }
87
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, "
93                   "or 0 for infinite"),
94     AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
95                   "Whether persistent connections should be On or Off"),
96     { NULL }
97 };
98
99 static const char *http_scheme(const request_rec *r)
100 {
101     return "http";
102 }
103
104 static apr_port_t http_port(const request_rec *r)
105 {
106     return DEFAULT_HTTP_PORT;
107 }
108
109 static int ap_process_http_async_connection(conn_rec *c)
110 {
111     request_rec *r;
112     conn_state_t *cs = c->cs;
113     
114     AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
115     
116     while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
117         ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
118             
119         if ((r = ap_read_request(c))) {
120
121             c->keepalive = AP_CONN_UNKNOWN;
122             /* process the request if it was read without error */
123                                                        
124             ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
125             if (r->status == HTTP_OK)
126                 ap_process_request(r);
127
128             if (ap_extended_status)
129                 ap_increment_counts(c->sbh, r);
130
131             if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted 
132                     || ap_graceful_stop_signalled()) {
133                 cs->state = CONN_STATE_LINGER;
134             }
135             else if (!c->data_in_input_filters) {
136                 cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
137             }
138
139             /* else we are pipelining.  Stay in READ_REQUEST_LINE state
140              *  and stay in the loop
141              */
142
143             apr_pool_destroy(r->pool);
144         }
145         else {   /* ap_read_request failed - client may have closed */
146             cs->state = CONN_STATE_LINGER;
147         }
148     }
149
150     return OK;
151 }
152
153 static int ap_process_http_connection(conn_rec *c)
154 {
155     request_rec *r;
156     int csd_set = 0;
157     apr_socket_t *csd = NULL;
158
159     /*
160      * Read and process each request found on our connection
161      * until no requests are left or we decide to close.
162      */
163  
164     ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
165     while ((r = ap_read_request(c)) != NULL) {
166
167         c->keepalive = AP_CONN_UNKNOWN;
168         /* process the request if it was read without error */
169  
170         ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
171         if (r->status == HTTP_OK)
172             ap_process_request(r);
173  
174         if (ap_extended_status)
175             ap_increment_counts(c->sbh, r);
176  
177         if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
178             break;
179  
180         ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, r);
181         apr_pool_destroy(r->pool);
182  
183         if (ap_graceful_stop_signalled())
184             break;
185         /* Go straight to select() to wait for the next request */
186         if (!csd_set) {
187             csd = ap_get_module_config(c->conn_config, &core_module);
188             csd_set = 1;
189         }
190         apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
191     }
192  
193     return OK;
194 }
195
196 static int http_create_request(request_rec *r)
197 {
198     if (!r->main && !r->prev) {
199         ap_add_output_filter_handle(ap_byterange_filter_handle,
200                                     NULL, r, r->connection);
201         ap_add_output_filter_handle(ap_content_length_filter_handle,
202                                     NULL, r, r->connection);
203         ap_add_output_filter_handle(ap_http_header_filter_handle,
204                                     NULL, r, r->connection);
205     }
206
207     return OK;
208 }
209
210 static void register_hooks(apr_pool_t *p)
211 {
212     /**
213      * If we ae using an MPM That Supports Async Connections,
214      * use a different processing function
215      */
216     int async_mpm = 0;
217     if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS 
218         && async_mpm == 1) {
219         ap_hook_process_connection(ap_process_http_async_connection, NULL,
220                                    NULL, APR_HOOK_REALLY_LAST);
221     }
222     else {
223         ap_hook_process_connection(ap_process_http_connection, NULL, NULL, 
224                                    APR_HOOK_REALLY_LAST);
225     }
226
227     ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
228     ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
229     ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
230     ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
231     ap_http_input_filter_handle =
232         ap_register_input_filter("HTTP_IN", ap_http_filter,
233                                  NULL, AP_FTYPE_PROTOCOL);
234     ap_http_header_filter_handle =
235         ap_register_output_filter("HTTP_HEADER", ap_http_header_filter, 
236                                   NULL, AP_FTYPE_PROTOCOL);
237     ap_chunk_filter_handle =
238         ap_register_output_filter("CHUNK", ap_http_chunk_filter,
239                                   NULL, AP_FTYPE_TRANSCODE);
240     ap_byterange_filter_handle =
241         ap_register_output_filter("BYTERANGE", ap_byterange_filter,
242                                   NULL, AP_FTYPE_PROTOCOL);
243     ap_method_registry_init(p);
244 }
245
246 module AP_MODULE_DECLARE_DATA http_module = {
247     STANDARD20_MODULE_STUFF,
248     NULL,                       /* create per-directory config structure */
249     NULL,                       /* merge per-directory config structures */
250     NULL,                       /* create per-server config structure */
251     NULL,                       /* merge per-server config structures */
252     http_cmds,                  /* command apr_table_t */
253     register_hooks              /* register hooks */
254 };