1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
59 #include "apr_strings.h"
60 #include "apr_thread_proc.h" /* for RLIMIT stuff */
62 #define APR_WANT_STRFUNC
67 #include "http_config.h"
68 #include "http_connection.h"
69 #include "http_core.h"
70 #include "http_protocol.h" /* For index_of_response(). Grump. */
71 #include "http_request.h"
73 #include "util_filter.h"
74 #include "util_ebcdic.h"
76 #include "scoreboard.h"
80 /* Handles for core filters */
81 AP_DECLARE_DATA ap_filter_rec_t *ap_http_input_filter_handle;
82 AP_DECLARE_DATA ap_filter_rec_t *ap_http_header_filter_handle;
83 AP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle;
84 AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
86 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
89 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
94 cmd->server->keep_alive_timeout = apr_time_from_sec(atoi(arg));
98 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
101 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
106 /* We've changed it to On/Off, but used to use numbers
107 * so we accept anything but "Off" or "0" as "On"
109 if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
110 cmd->server->keep_alive = 0;
113 cmd->server->keep_alive = 1;
118 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
121 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
126 cmd->server->keep_alive_max = atoi(arg);
130 static const command_rec http_cmds[] = {
131 AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
132 "Keep-Alive timeout duration (sec)"),
133 AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
134 "Maximum number of Keep-Alive requests per connection, or 0 for infinite"),
135 AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
136 "Whether persistent connections should be On or Off"),
141 * HTTP/1.1 chunked transfer encoding filter.
143 static apr_status_t chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
145 #define ASCII_CRLF "\015\012"
146 #define ASCII_ZERO "\060"
147 conn_rec *c = f->r->connection;
148 apr_bucket_brigade *more;
152 for (more = NULL; b; b = more, more = NULL) {
154 apr_bucket *eos = NULL;
155 apr_bucket *flush = NULL;
156 /* XXX: chunk_hdr must remain at this scope since it is used in a
159 char chunk_hdr[20]; /* enough space for the snprintf below */
161 APR_BRIGADE_FOREACH(e, b) {
162 if (APR_BUCKET_IS_EOS(e)) {
163 /* there shouldn't be anything after the eos */
167 if (APR_BUCKET_IS_FLUSH(e)) {
170 else if (e->length == (apr_size_t)-1) {
171 /* unknown amount of data (e.g. a pipe) */
175 rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
176 if (rv != APR_SUCCESS) {
181 * There may be a new next bucket representing the
182 * rest of the data stream on which a read() may
183 * block so we pass down what we have so far.
186 more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
190 /* If there was nothing in this bucket then we can
191 * safely move on to the next one without pausing
192 * to pass down what we have counted up so far.
203 * XXX: if there aren't very many bytes at this point it may
204 * be a good idea to set them aside and return for more,
205 * unless we haven't finished counting this brigade yet.
207 /* if there are content bytes, then wrap them in a chunk */
211 * Insert the chunk header, specifying the number of bytes in
214 hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
215 "%" APR_UINT64_T_HEX_FMT CRLF, (apr_uint64_t)bytes);
216 ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
217 e = apr_bucket_transient_create(chunk_hdr, hdr_len,
219 APR_BRIGADE_INSERT_HEAD(b, e);
222 * Insert the end-of-chunk CRLF before an EOS or
223 * FLUSH bucket, or appended to the brigade
225 e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
227 APR_BUCKET_INSERT_BEFORE(eos, e);
229 else if (flush != NULL) {
230 APR_BUCKET_INSERT_BEFORE(flush, e);
233 APR_BRIGADE_INSERT_TAIL(b, e);
237 /* RFC 2616, Section 3.6.1
239 * If there is an EOS bucket, then prefix it with:
240 * 1) the last-chunk marker ("0" CRLF)
242 * 3) the end-of-chunked body CRLF
244 * If there is no EOS bucket, then do nothing.
246 * XXX: it would be nice to combine this with the end-of-chunk
247 * marker above, but this is a bit more straight-forward for
251 /* XXX: (2) trailers ... does not yet exist */
252 e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
254 ASCII_CRLF, 5, c->bucket_alloc);
255 APR_BUCKET_INSERT_BEFORE(eos, e);
258 /* pass the brigade to the next filter. */
259 rv = ap_pass_brigade(f->next, b);
260 if (rv != APR_SUCCESS || eos != NULL) {
267 static const char *http_method(const request_rec *r)
270 static apr_port_t http_port(const request_rec *r)
271 { return DEFAULT_HTTP_PORT; }
273 static int ap_process_http_connection(conn_rec *c)
277 apr_socket_t *csd = NULL;
280 * Read and process each request found on our connection
281 * until no requests are left or we decide to close.
284 ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
285 while ((r = ap_read_request(c)) != NULL) {
287 c->keepalive = AP_CONN_UNKNOWN;
288 /* process the request if it was read without error */
290 ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
291 if (r->status == HTTP_OK)
292 ap_process_request(r);
294 if (ap_extended_status)
295 ap_increment_counts(c->sbh, r);
297 if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
300 ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, r);
301 apr_pool_destroy(r->pool);
303 if (ap_graceful_stop_signalled())
305 /* Go straight to select() to wait for the next request */
307 csd = ap_get_module_config(c->conn_config, &core_module);
310 apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
316 static int http_create_request(request_rec *r)
318 if (!r->main && !r->prev) {
319 ap_add_output_filter_handle(ap_byterange_filter_handle,
320 NULL, r, r->connection);
321 ap_add_output_filter_handle(ap_content_length_filter_handle,
322 NULL, r, r->connection);
323 ap_add_output_filter_handle(ap_http_header_filter_handle,
324 NULL, r, r->connection);
330 static void register_hooks(apr_pool_t *p)
332 ap_hook_process_connection(ap_process_http_connection,NULL,NULL,
333 APR_HOOK_REALLY_LAST);
334 ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
335 ap_hook_http_method(http_method,NULL,NULL,APR_HOOK_REALLY_LAST);
336 ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
337 ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
338 ap_http_input_filter_handle =
339 ap_register_input_filter("HTTP_IN", ap_http_filter,
340 NULL, AP_FTYPE_PROTOCOL);
341 ap_http_header_filter_handle =
342 ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
343 NULL, AP_FTYPE_PROTOCOL);
344 ap_chunk_filter_handle =
345 ap_register_output_filter("CHUNK", chunk_filter,
346 NULL, AP_FTYPE_TRANSCODE);
347 ap_byterange_filter_handle =
348 ap_register_output_filter("BYTERANGE", ap_byterange_filter,
349 NULL, AP_FTYPE_PROTOCOL);
350 ap_method_registry_init(p);
353 module AP_MODULE_DECLARE_DATA http_module = {
354 STANDARD20_MODULE_STUFF,
355 NULL, /* create per-directory config structure */
356 NULL, /* merge per-directory config structures */
357 NULL, /* create per-server config structure */
358 NULL, /* merge per-server config structures */
359 http_cmds, /* command apr_table_t */
360 register_hooks /* register hooks */