]> granicus.if.org Git - apache/blob - modules/http/http_core.c
BUCKET FREELISTS
[apache] / modules / http / http_core.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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.
25  *
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.
30  *
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.
34  *
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
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
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/>.
53  *
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.
57  */
58
59 #include "apr_strings.h"
60 #include "apr_thread_proc.h"    /* for RLIMIT stuff */
61
62 #define APR_WANT_STRFUNC
63 #include "apr_want.h"
64
65 #define CORE_PRIVATE
66 #include "httpd.h"
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"
72
73 #include "util_filter.h"
74 #include "util_ebcdic.h"
75 #include "ap_mpm.h"
76 #include "scoreboard.h"
77
78 #include "mod_core.h"
79
80 /* Handles for core filters */
81 ap_filter_rec_t *ap_http_input_filter_handle;
82 ap_filter_rec_t *ap_http_header_filter_handle;
83 ap_filter_rec_t *ap_chunk_filter_handle;
84 ap_filter_rec_t *ap_byterange_filter_handle;
85
86 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
87                                           const char *arg)
88 {
89     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
90     if (err != NULL) {
91         return err;
92     }
93
94     cmd->server->keep_alive_timeout = atoi(arg);
95     return NULL;
96 }
97
98 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
99                                   const char *arg) 
100 {
101     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
102     if (err != NULL) {
103         return err;
104     }
105
106     /* We've changed it to On/Off, but used to use numbers
107      * so we accept anything but "Off" or "0" as "On"
108      */
109     if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
110         cmd->server->keep_alive = 0;
111     }
112     else {
113         cmd->server->keep_alive = 1;
114     }
115     return NULL;
116 }
117
118 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
119                                       const char *arg)
120 {
121     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
122     if (err != NULL) {
123         return err;
124     }
125
126     cmd->server->keep_alive_max = atoi(arg);
127     return NULL;
128 }
129
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"),
137     { NULL }
138 };
139
140 /*
141  * HTTP/1.1 chunked transfer encoding filter.
142  */
143 static apr_status_t chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
144 {
145 #define ASCII_CRLF  "\015\012"
146 #define ASCII_ZERO  "\060"
147     conn_rec *c = f->r->connection;
148     apr_bucket_brigade *more;
149     apr_bucket *e;
150     apr_status_t rv;
151
152     for (more = NULL; b; b = more, more = NULL) {
153         apr_off_t bytes = 0;
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 
157          *      transient bucket.
158          */
159         char chunk_hdr[20]; /* enough space for the snprintf below */
160
161         APR_BRIGADE_FOREACH(e, b) {
162             if (APR_BUCKET_IS_EOS(e)) {
163                 /* there shouldn't be anything after the eos */
164                 eos = e;
165                 break;
166             }
167             if (APR_BUCKET_IS_FLUSH(e)) {
168                 flush = e;
169             }
170             else if (e->length == -1) {
171                 /* unknown amount of data (e.g. a pipe) */
172                 const char *data;
173                 apr_size_t len;
174
175                 rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
176                 if (rv != APR_SUCCESS) {
177                     return rv;
178                 }
179                 if (len > 0) {
180                     /*
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.
184                      */
185                     bytes += len;
186                     more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
187                     break;
188                 }
189                 else {
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.
193                      */
194                     continue;
195                 }
196             }
197             else {
198                 bytes += e->length;
199             }
200         }
201
202         /*
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.
206          */
207         /* if there are content bytes, then wrap them in a chunk */
208         if (bytes > 0) {
209             apr_size_t hdr_len;
210             /*
211              * Insert the chunk header, specifying the number of bytes in
212              * the chunk.
213              */
214             /* XXX might be nice to have APR_OFF_T_FMT_HEX */
215             hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
216                                    "%qx" CRLF, (apr_uint64_t)bytes);
217             ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
218             e = apr_bucket_transient_create(chunk_hdr, hdr_len,
219                                             c->bucket_alloc);
220             APR_BRIGADE_INSERT_HEAD(b, e);
221
222             /*
223              * Insert the end-of-chunk CRLF before an EOS or
224              * FLUSH bucket, or appended to the brigade
225              */
226             e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
227             if (eos != NULL) {
228                 APR_BUCKET_INSERT_BEFORE(eos, e);
229             }
230             else if (flush != NULL) {
231                 APR_BUCKET_INSERT_BEFORE(flush, e);
232             }
233             else {
234                 APR_BRIGADE_INSERT_TAIL(b, e);
235             }
236         }
237
238         /* RFC 2616, Section 3.6.1
239          *
240          * If there is an EOS bucket, then prefix it with:
241          *   1) the last-chunk marker ("0" CRLF)
242          *   2) the trailer
243          *   3) the end-of-chunked body CRLF
244          *
245          * If there is no EOS bucket, then do nothing.
246          *
247          * XXX: it would be nice to combine this with the end-of-chunk
248          * marker above, but this is a bit more straight-forward for
249          * now.
250          */
251         if (eos != NULL) {
252             /* XXX: (2) trailers ... does not yet exist */
253             e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
254                                            /* <trailers> */
255                                            ASCII_CRLF, 5, c->bucket_alloc);
256             APR_BUCKET_INSERT_BEFORE(eos, e);
257         }
258
259         /* pass the brigade to the next filter. */
260         rv = ap_pass_brigade(f->next, b);
261         if (rv != APR_SUCCESS || eos != NULL) {
262             return rv;
263         }
264     }
265     return APR_SUCCESS;
266 }
267
268 static const char *http_method(const request_rec *r)
269     { return "http"; }
270
271 static apr_port_t http_port(const request_rec *r)
272     { return DEFAULT_HTTP_PORT; }
273
274 static int ap_process_http_connection(conn_rec *c)
275 {
276     request_rec *r;
277  
278     /*
279      * Read and process each request found on our connection
280      * until no requests are left or we decide to close.
281      */
282  
283     ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
284     while ((r = ap_read_request(c)) != NULL) {
285  
286         c->keepalive = 0;
287         /* process the request if it was read without error */
288  
289         ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
290         if (r->status == HTTP_OK)
291             ap_process_request(r);
292  
293         if (ap_extended_status)
294             ap_increment_counts(c->sbh, r);
295  
296         if (!c->keepalive || c->aborted)
297             break;
298  
299         ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, r);
300         apr_pool_destroy(r->pool);
301  
302         if (ap_graceful_stop_signalled())
303             break;
304     }
305  
306     return OK;
307 }
308
309 static int http_create_request(request_rec *r)
310 {
311     if (!r->main && !r->prev) {
312         ap_add_output_filter_handle(ap_byterange_filter_handle,
313                                     NULL, r, r->connection);
314         ap_add_output_filter_handle(ap_content_length_filter_handle,
315                                     NULL, r, r->connection);
316         ap_add_output_filter_handle(ap_http_header_filter_handle,
317                                     NULL, r, r->connection);
318     }
319
320     return OK;
321 }
322
323 static void register_hooks(apr_pool_t *p)
324 {
325     ap_hook_process_connection(ap_process_http_connection,NULL,NULL,
326                                APR_HOOK_REALLY_LAST);
327     ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
328     ap_hook_http_method(http_method,NULL,NULL,APR_HOOK_REALLY_LAST);
329     ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
330     ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
331     ap_http_input_filter_handle =
332         ap_register_input_filter("HTTP_IN", ap_http_filter,
333                                  AP_FTYPE_PROTOCOL);
334     ap_http_header_filter_handle =
335         ap_register_output_filter("HTTP_HEADER", ap_http_header_filter, 
336                                   AP_FTYPE_PROTOCOL);
337     ap_chunk_filter_handle =
338         ap_register_output_filter("CHUNK", chunk_filter, AP_FTYPE_TRANSCODE);
339     ap_byterange_filter_handle =
340         ap_register_output_filter("BYTERANGE", ap_byterange_filter,
341                                   AP_FTYPE_PROTOCOL);
342     ap_method_registry_init(p);
343 }
344
345 module AP_MODULE_DECLARE_DATA http_module = {
346     STANDARD20_MODULE_STUFF,
347     NULL,                       /* create per-directory config structure */
348     NULL,                       /* merge per-directory config structures */
349     NULL,                       /* create per-server config structure */
350     NULL,                       /* merge per-server config structures */
351     http_cmds,                  /* command apr_table_t */
352     register_hooks              /* register hooks */
353 };