1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  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 #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_http_outerror_filter_handle;
42 AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
43 
44 AP_DECLARE_DATA const char *ap_multipart_boundary;
45 
46 /* If we are using an MPM That Supports Async Connections,
47  * use a different processing function
48  */
49 static int async_mpm = 0;
50 
set_keep_alive_timeout(cmd_parms * cmd,void * dummy,const char * arg)51 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
52                                           const char *arg)
53 {
54     apr_interval_time_t timeout;
55     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
56     if (err != NULL) {
57         return err;
58     }
59 
60     /* Stolen from mod_proxy.c */
61     if (ap_timeout_parameter_parse(arg, &timeout, "s") != APR_SUCCESS)
62         return "KeepAliveTimeout has wrong format";
63     cmd->server->keep_alive_timeout = timeout;
64 
65     /* We don't want to take into account whether or not KeepAliveTimeout is
66      * set for the main server, because if no http_module directive is used
67      * for a vhost, it will inherit the http_srv_cfg from the main server.
68      * However keep_alive_timeout_set helps determine whether the vhost should
69      * use its own configured timeout or the one from the vhost declared first
70      * on the same IP:port (ie. c->base_server, and the legacy behaviour).
71      */
72     if (cmd->server->is_virtual) {
73         cmd->server->keep_alive_timeout_set = 1;
74     }
75     return NULL;
76 }
77 
set_keep_alive(cmd_parms * cmd,void * dummy,int arg)78 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
79                                   int arg)
80 {
81     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
82     if (err != NULL) {
83         return err;
84     }
85 
86     cmd->server->keep_alive = arg;
87     return NULL;
88 }
89 
set_keep_alive_max(cmd_parms * cmd,void * dummy,const char * arg)90 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
91                                       const char *arg)
92 {
93     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
94     if (err != NULL) {
95         return err;
96     }
97 
98     cmd->server->keep_alive_max = atoi(arg);
99     return NULL;
100 }
101 
102 static const command_rec http_cmds[] = {
103     AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
104                   "Keep-Alive timeout duration (sec)"),
105     AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
106                   "Maximum number of Keep-Alive requests per connection, "
107                   "or 0 for infinite"),
108     AP_INIT_FLAG("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
109                   "Whether persistent connections should be On or Off"),
110     { NULL }
111 };
112 
http_scheme(const request_rec * r)113 static const char *http_scheme(const request_rec *r)
114 {
115     /*
116      * The http module shouldn't return anything other than
117      * "http" (the default) or "https".
118      */
119     if (r->server->server_scheme &&
120         (strcmp(r->server->server_scheme, "https") == 0))
121         return "https";
122 
123     return "http";
124 }
125 
http_port(const request_rec * r)126 static apr_port_t http_port(const request_rec *r)
127 {
128     if (r->server->server_scheme &&
129         (strcmp(r->server->server_scheme, "https") == 0))
130         return DEFAULT_HTTPS_PORT;
131 
132     return DEFAULT_HTTP_PORT;
133 }
134 
ap_process_http_async_connection(conn_rec * c)135 static int ap_process_http_async_connection(conn_rec *c)
136 {
137     request_rec *r = NULL;
138     conn_state_t *cs = c->cs;
139 
140     AP_DEBUG_ASSERT(cs != NULL);
141     AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
142 
143     if (cs->state == CONN_STATE_READ_REQUEST_LINE) {
144         ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
145         if (ap_extended_status) {
146             ap_set_conn_count(c->sbh, r, c->keepalives);
147         }
148         if ((r = ap_read_request(c))) {
149             if (r->status == HTTP_OK) {
150                 cs->state = CONN_STATE_HANDLER;
151                 if (ap_extended_status) {
152                     ap_set_conn_count(c->sbh, r, c->keepalives + 1);
153                 }
154                 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
155                 ap_process_async_request(r);
156                 /* After the call to ap_process_request, the
157                  * request pool may have been deleted.  We set
158                  * r=NULL here to ensure that any dereference
159                  * of r that might be added later in this function
160                  * will result in a segfault immediately instead
161                  * of nondeterministic failures later.
162                  */
163                 r = NULL;
164             }
165 
166             if (cs->state != CONN_STATE_WRITE_COMPLETION &&
167                 cs->state != CONN_STATE_SUSPENDED) {
168                 /* Something went wrong; close the connection */
169                 cs->state = CONN_STATE_LINGER;
170             }
171         }
172         else {   /* ap_read_request failed - client may have closed */
173             cs->state = CONN_STATE_LINGER;
174         }
175     }
176 
177     return OK;
178 }
179 
ap_process_http_sync_connection(conn_rec * c)180 static int ap_process_http_sync_connection(conn_rec *c)
181 {
182     request_rec *r;
183     conn_state_t *cs = c->cs;
184     apr_socket_t *csd = NULL;
185     int mpm_state = 0;
186 
187     /*
188      * Read and process each request found on our connection
189      * until no requests are left or we decide to close.
190      */
191 
192     ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
193     while ((r = ap_read_request(c)) != NULL) {
194         apr_interval_time_t keep_alive_timeout = r->server->keep_alive_timeout;
195 
196         /* To preserve legacy behaviour, use the keepalive timeout from the
197          * base server (first on this IP:port) when none is explicitly
198          * configured on this server.
199          */
200         if (!r->server->keep_alive_timeout_set) {
201             keep_alive_timeout = c->base_server->keep_alive_timeout;
202         }
203 
204         if (r->status == HTTP_OK) {
205             if (cs)
206                 cs->state = CONN_STATE_HANDLER;
207             ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
208             ap_process_request(r);
209             /* After the call to ap_process_request, the
210              * request pool will have been deleted.  We set
211              * r=NULL here to ensure that any dereference
212              * of r that might be added later in this function
213              * will result in a segfault immediately instead
214              * of nondeterministic failures later.
215              */
216             r = NULL;
217         }
218 
219         if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
220             break;
221 
222         ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
223 
224         if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state)) {
225             break;
226         }
227 
228         if (mpm_state == AP_MPMQ_STOPPING) {
229           break;
230         }
231 
232         if (!csd) {
233             csd = ap_get_conn_socket(c);
234         }
235         apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
236         apr_socket_timeout_set(csd, keep_alive_timeout);
237         /* Go straight to select() to wait for the next request */
238     }
239 
240     return OK;
241 }
242 
ap_process_http_connection(conn_rec * c)243 static int ap_process_http_connection(conn_rec *c)
244 {
245     if (async_mpm && !c->clogging_input_filters) {
246         return ap_process_http_async_connection(c);
247     }
248     else {
249         return ap_process_http_sync_connection(c);
250     }
251 }
252 
http_create_request(request_rec * r)253 static int http_create_request(request_rec *r)
254 {
255     if (!r->main && !r->prev) {
256         ap_add_output_filter_handle(ap_byterange_filter_handle,
257                                     NULL, r, r->connection);
258         ap_add_output_filter_handle(ap_content_length_filter_handle,
259                                     NULL, r, r->connection);
260         ap_add_output_filter_handle(ap_http_header_filter_handle,
261                                     NULL, r, r->connection);
262         ap_add_output_filter_handle(ap_http_outerror_filter_handle,
263                                     NULL, r, r->connection);
264     }
265 
266     return OK;
267 }
268 
http_send_options(request_rec * r)269 static int http_send_options(request_rec *r)
270 {
271     if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
272          (r->uri[1] == '\0')) {
273         return DONE;           /* Send HTTP pong, without Allow header */
274     }
275     return DECLINED;
276 }
277 
http_post_config(apr_pool_t * p,apr_pool_t * plog,apr_pool_t * ptemp,server_rec * s)278 static int http_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
279 {
280     apr_uint64_t val;
281     if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) != APR_SUCCESS) {
282         async_mpm = 0;
283     }
284     ap_random_insecure_bytes(&val, sizeof(val));
285     ap_multipart_boundary = apr_psprintf(p, "%0" APR_UINT64_T_HEX_FMT, val);
286 
287     return OK;
288 }
289 
register_hooks(apr_pool_t * p)290 static void register_hooks(apr_pool_t *p)
291 {
292     ap_hook_post_config(http_post_config, NULL, NULL, APR_HOOK_MIDDLE);
293     ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
294                                APR_HOOK_REALLY_LAST);
295     ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
296     ap_hook_map_to_storage(http_send_options,NULL,NULL,APR_HOOK_MIDDLE);
297     ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
298     ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
299     ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
300     ap_http_input_filter_handle =
301         ap_register_input_filter("HTTP_IN", ap_http_filter,
302                                  NULL, AP_FTYPE_PROTOCOL);
303     ap_http_header_filter_handle =
304         ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
305                                   NULL, AP_FTYPE_PROTOCOL);
306     ap_chunk_filter_handle =
307         ap_register_output_filter("CHUNK", ap_http_chunk_filter,
308                                   NULL, AP_FTYPE_TRANSCODE);
309     ap_http_outerror_filter_handle =
310         ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
311                                   NULL, AP_FTYPE_PROTOCOL);
312     ap_byterange_filter_handle =
313         ap_register_output_filter("BYTERANGE", ap_byterange_filter,
314                                   NULL, AP_FTYPE_PROTOCOL);
315     ap_method_registry_init(p);
316 }
317 
318 AP_DECLARE_MODULE(http) = {
319     STANDARD20_MODULE_STUFF,
320     NULL,              /* create per-directory config structure */
321     NULL,              /* merge per-directory config structures */
322     NULL,              /* create per-server config structure */
323     NULL,              /* merge per-server config structures */
324     http_cmds,         /* command apr_table_t */
325     register_hooks     /* register hooks */
326 };
327