1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr_strings.h"
#include "apr_thread_proc.h" /* for RLIMIT stuff */
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "httpd.h"
#include "http_config.h"
#include "http_connection.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h" /* For index_of_response(). Grump. */
#include "http_request.h"
#include "util_filter.h"
#include "util_ebcdic.h"
#include "ap_mpm.h"
#include "scoreboard.h"
#include "mod_core.h"
/* Handles for core filters */
AP_DECLARE_DATA ap_filter_rec_t *ap_http_input_filter_handle;
AP_DECLARE_DATA ap_filter_rec_t *ap_h1_request_in_filter_handle;
AP_DECLARE_DATA ap_filter_rec_t *ap_h1_body_in_filter_handle;
AP_DECLARE_DATA ap_filter_rec_t *ap_http_header_filter_handle;
AP_DECLARE_DATA ap_filter_rec_t *ap_h1_response_out_filter_handle;
AP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle;
AP_DECLARE_DATA ap_filter_rec_t *ap_http_outerror_filter_handle;
AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
AP_DECLARE_DATA const char *ap_multipart_boundary;
/* If we are using an MPM That Supports Async Connections,
* use a different processing function
*/
static int async_mpm = 0;
static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
const char *arg)
{
apr_interval_time_t timeout;
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
if (err != NULL) {
return err;
}
/* Stolen from mod_proxy.c */
if (ap_timeout_parameter_parse(arg, &timeout, "s") != APR_SUCCESS)
return "KeepAliveTimeout has wrong format";
cmd->server->keep_alive_timeout = timeout;
/* We don't want to take into account whether or not KeepAliveTimeout is
* set for the main server, because if no http_module directive is used
* for a vhost, it will inherit the http_srv_cfg from the main server.
* However keep_alive_timeout_set helps determine whether the vhost should
* use its own configured timeout or the one from the vhost declared first
* on the same IP:port (ie. c->base_server, and the legacy behaviour).
*/
if (cmd->server->is_virtual) {
cmd->server->keep_alive_timeout_set = 1;
}
return NULL;
}
static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
int arg)
{
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
if (err != NULL) {
return err;
}
cmd->server->keep_alive = arg;
return NULL;
}
static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
const char *arg)
{
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
if (err != NULL) {
return err;
}
cmd->server->keep_alive_max = atoi(arg);
return NULL;
}
static const command_rec http_cmds[] = {
AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
"Keep-Alive timeout duration (sec)"),
AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
"Maximum number of Keep-Alive requests per connection, "
"or 0 for infinite"),
AP_INIT_FLAG("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
"Whether persistent connections should be On or Off"),
{ NULL }
};
static const char *http_scheme(const request_rec *r)
{
/*
* The http module shouldn't return anything other than
* "http" (the default) or "https".
*/
if (r->server->server_scheme &&
(strcmp(r->server->server_scheme, "https") == 0))
return "https";
return "http";
}
static apr_port_t http_port(const request_rec *r)
{
if (r->server->server_scheme &&
(strcmp(r->server->server_scheme, "https") == 0))
return DEFAULT_HTTPS_PORT;
return DEFAULT_HTTP_PORT;
}
static int ap_process_http_async_connection(conn_rec *c)
{
request_rec *r = NULL;
conn_state_t *cs = c->cs;
AP_DEBUG_ASSERT(cs != NULL);
AP_DEBUG_ASSERT(cs->state == CONN_STATE_PROCESSING);
if (cs->state == CONN_STATE_PROCESSING) {
ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
if (ap_extended_status) {
ap_set_conn_count(c->sbh, r, c->keepalives);
}
if ((r = ap_read_request(c))) {
if (r->status == HTTP_OK) {
cs->state = CONN_STATE_HANDLER;
if (ap_extended_status) {
ap_set_conn_count(c->sbh, r, c->keepalives + 1);
}
ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
ap_process_async_request(r);
/* After the call to ap_process_request, the
* request pool may have been deleted. We set
* r=NULL here to ensure that any dereference
* of r that might be added later in this function
* will result in a segfault immediately instead
* of nondeterministic failures later.
*/
r = NULL;
}
if (cs->state != CONN_STATE_WRITE_COMPLETION &&
cs->state != CONN_STATE_SUSPENDED) {
/* Something went wrong; close the connection */
cs->state = CONN_STATE_LINGER;
}
}
else { /* ap_read_request failed - client may have closed */
cs->state = CONN_STATE_LINGER;
}
}
return OK;
}
static int ap_process_http_sync_connection(conn_rec *c)
{
request_rec *r;
conn_state_t *cs = c->cs;
apr_socket_t *csd = NULL;
int mpm_state = 0;
/*
* Read and process each request found on our connection
* until no requests are left or we decide to close.
*/
ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
while ((r = ap_read_request(c)) != NULL) {
apr_interval_time_t keep_alive_timeout = r->server->keep_alive_timeout;
/* To preserve legacy behaviour, use the keepalive timeout from the
* base server (first on this IP:port) when none is explicitly
* configured on this server.
*/
if (!r->server->keep_alive_timeout_set) {
keep_alive_timeout = c->base_server->keep_alive_timeout;
}
if (r->status == HTTP_OK) {
if (cs)
cs->state = CONN_STATE_HANDLER;
ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
ap_process_request(r);
/* After the call to ap_process_request, the
* request pool will have been deleted. We set
* r=NULL here to ensure that any dereference
* of r that might be added later in this function
* will result in a segfault immediately instead
* of nondeterministic failures later.
*/
r = NULL;
}
if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
break;
ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state)) {
break;
}
if (mpm_state == AP_MPMQ_STOPPING) {
break;
}
if (!csd) {
csd = ap_get_conn_socket(c);
}
apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
apr_socket_timeout_set(csd, keep_alive_timeout);
/* Go straight to select() to wait for the next request */
}
return OK;
}
static int ap_process_http_connection(conn_rec *c)
{
if (async_mpm && !c->clogging_input_filters) {
return ap_process_http_async_connection(c);
}
else {
return ap_process_http_sync_connection(c);
}
}
static int http_create_request(request_rec *r)
{
/* FIXME: we must only add these filters if we are an HTTP request */
if (!r->main && !r->prev) {
ap_add_output_filter_handle(ap_byterange_filter_handle,
NULL, r, r->connection);
ap_add_output_filter_handle(ap_content_length_filter_handle,
NULL, r, r->connection);
ap_add_output_filter_handle(ap_http_header_filter_handle,
NULL, r, r->connection);
ap_add_output_filter_handle(ap_http_outerror_filter_handle,
NULL, r, r->connection);
}
return OK;
}
static void h1_pre_read_request(request_rec *r, conn_rec *c)
{
if (!r->main && !r->prev
&& !strcmp(AP_PROTOCOL_HTTP1, ap_get_protocol(c))) {
if (r->proxyreq == PROXYREQ_NONE) {
ap_add_input_filter_handle(ap_h1_request_in_filter_handle,
NULL, r, r->connection);
}
ap_add_output_filter_handle(ap_h1_response_out_filter_handle,
NULL, r, r->connection);
}
}
static int h1_post_read_request(request_rec *r)
{
const char *tenc;
if (!r->main && !r->prev && r->proto_num <= HTTP_VERSION(1,1)) {
if (r->proto_num >= HTTP_VERSION(1,0)) {
tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
if (tenc) {
r->body_indeterminate = 1;
/* https://tools.ietf.org/html/rfc7230
* Section 3.3.3.3: "If a Transfer-Encoding header field is
* present in a request and the chunked transfer coding is not
* the final encoding ...; the server MUST respond with the 400
* (Bad Request) status code and then close the connection".
*/
if (!ap_is_chunked(r->pool, tenc)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02539)
"client sent unknown Transfer-Encoding "
"(%s): %s", tenc, r->uri);
return HTTP_BAD_REQUEST;
}
/* https://tools.ietf.org/html/rfc7230
* Section 3.3.3.3: "If a message is received with both a
* Transfer-Encoding and a Content-Length header field, the
* Transfer-Encoding overrides the Content-Length. ... A sender
* MUST remove the received Content-Length field".
*/
if (apr_table_get(r->headers_in, "Content-Length")) {
apr_table_unset(r->headers_in, "Content-Length");
/* Don't reuse this connection anyway to avoid confusion with
* intermediaries and request/reponse spltting.
*/
r->connection->keepalive = AP_CONN_CLOSE;
}
}
}
/* HTTP1_BODY_IN takes care of chunked encoding and content-length.
*/
ap_add_input_filter_handle(ap_h1_body_in_filter_handle,
NULL, r, r->connection);
}
return OK;
}
static int http_send_options(request_rec *r)
{
if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
(r->uri[1] == '\0')) {
return DONE; /* Send HTTP pong, without Allow header */
}
return DECLINED;
}
static int http_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
{
apr_uint64_t val;
if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) != APR_SUCCESS) {
async_mpm = 0;
}
ap_random_insecure_bytes(&val, sizeof(val));
ap_multipart_boundary = apr_psprintf(p, "%0" APR_UINT64_T_HEX_FMT, val);
return OK;
}
static void register_hooks(apr_pool_t *p)
{
ap_hook_post_config(http_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
APR_HOOK_REALLY_LAST);
ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
ap_hook_map_to_storage(http_send_options,NULL,NULL,APR_HOOK_MIDDLE);
ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
ap_hook_pre_read_request(h1_pre_read_request, NULL, NULL, APR_HOOK_REALLY_LAST);
ap_hook_post_read_request(h1_post_read_request, NULL, NULL, APR_HOOK_REALLY_FIRST);
ap_http_input_filter_handle =
ap_register_input_filter("HTTP_IN", ap_http_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_h1_request_in_filter_handle =
ap_register_input_filter("HTTP1_REQUEST_IN", ap_h1_request_in_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_h1_body_in_filter_handle =
ap_register_input_filter("HTTP1_BODY_IN", ap_h1_body_in_filter,
NULL, AP_FTYPE_TRANSCODE);
ap_http_header_filter_handle =
ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_h1_response_out_filter_handle =
ap_register_output_filter("HTTP1_RESPONSE_OUT", ap_h1_response_out_filter,
NULL, AP_FTYPE_TRANSCODE);
ap_chunk_filter_handle =
ap_register_output_filter("CHUNK", ap_http_chunk_filter,
NULL, AP_FTYPE_TRANSCODE);
ap_http_outerror_filter_handle =
ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_byterange_filter_handle =
ap_register_output_filter("BYTERANGE", ap_byterange_filter,
NULL, AP_FTYPE_PROTOCOL);
ap_method_registry_init(p);
}
AP_DECLARE_MODULE(http) = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
http_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};
|