]> granicus.if.org Git - apache/blob - modules/http/http_core.c
FINALLY Correct ap_http_method()! It is NOT a method, it's a SCHEME!
[apache] / modules / http / http_core.c
1 /* Copyright 1999-2004 The Apache Software Foundation
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include "apr_strings.h"
17 #include "apr_thread_proc.h"    /* for RLIMIT stuff */
18
19 #define APR_WANT_STRFUNC
20 #include "apr_want.h"
21
22 #define CORE_PRIVATE
23 #include "httpd.h"
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"
29
30 #include "util_filter.h"
31 #include "util_ebcdic.h"
32 #include "ap_mpm.h"
33 #include "scoreboard.h"
34
35 #include "mod_core.h"
36
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;
42
43 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
44                                           const char *arg)
45 {
46     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
47     if (err != NULL) {
48         return err;
49     }
50
51     cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
52     return NULL;
53 }
54
55 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
56                                   const char *arg) 
57 {
58     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
59     if (err != NULL) {
60         return err;
61     }
62
63     /* We've changed it to On/Off, but used to use numbers
64      * so we accept anything but "Off" or "0" as "On"
65      */
66     if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
67         cmd->server->keep_alive = 0;
68     }
69     else {
70         cmd->server->keep_alive = 1;
71     }
72     return NULL;
73 }
74
75 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
76                                       const char *arg)
77 {
78     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
79     if (err != NULL) {
80         return err;
81     }
82
83     cmd->server->keep_alive_max = atoi(arg);
84     return NULL;
85 }
86
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, "
92                   "or 0 for infinite"),
93     AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
94                   "Whether persistent connections should be On or Off"),
95     { NULL }
96 };
97
98 static const char *http_scheme(const request_rec *r)
99 {
100     return "http";
101 }
102
103 static apr_port_t http_port(const request_rec *r)
104 {
105     return DEFAULT_HTTP_PORT;
106 }
107
108 static int ap_process_http_async_connection(conn_rec *c)
109 {
110     request_rec *r;
111     conn_state_t *cs = c->cs;
112     
113     AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
114     
115     while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
116         ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
117             
118         if ((r = ap_read_request(c))) {
119
120             c->keepalive = AP_CONN_UNKNOWN;
121             /* process the request if it was read without error */
122                                                        
123             ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
124             if (r->status == HTTP_OK)
125                 ap_process_request(r);
126
127             if (ap_extended_status)
128                 ap_increment_counts(c->sbh, r);
129
130             if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted 
131                     || ap_graceful_stop_signalled()) {
132                 cs->state = CONN_STATE_LINGER;
133             }
134             else if (!c->data_in_input_filters) {
135                 cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
136             }
137
138             /* else we are pipelining.  Stay in READ_REQUEST_LINE state
139              *  and stay in the loop
140              */
141
142             apr_pool_destroy(r->pool);
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     int csd_set = 0;
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             ap_process_request(r);
172  
173         if (ap_extended_status)
174             ap_increment_counts(c->sbh, r);
175  
176         if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
177             break;
178  
179         ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, r);
180         apr_pool_destroy(r->pool);
181  
182         if (ap_graceful_stop_signalled())
183             break;
184         /* Go straight to select() to wait for the next request */
185         if (!csd_set) {
186             csd = ap_get_module_config(c->conn_config, &core_module);
187             csd_set = 1;
188         }
189         apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
190     }
191  
192     return OK;
193 }
194
195 static int http_create_request(request_rec *r)
196 {
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);
204     }
205
206     return OK;
207 }
208
209 static void register_hooks(apr_pool_t *p)
210 {
211     /**
212      * If we ae using an MPM That Supports Async Connections,
213      * use a different processing function
214      */
215     int async_mpm = 0;
216     if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) == APR_SUCCESS 
217         && async_mpm == 1) {
218         ap_hook_process_connection(ap_process_http_async_connection, NULL,
219                                    NULL, APR_HOOK_REALLY_LAST);
220     }
221     else {
222         ap_hook_process_connection(ap_process_http_connection, NULL, NULL, 
223                                    APR_HOOK_REALLY_LAST);
224     }
225
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);
243 }
244
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 */
253 };