]> granicus.if.org Git - apache/blob - modules/http/http_core.c
As hinted on dev@httpd, change filter naming schemes to match our
[apache] / modules / http / http_core.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 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     apr_bucket_brigade *more;
148     apr_bucket *e;
149     apr_status_t rv;
150
151     for (more = NULL; b; b = more, more = NULL) {
152         apr_off_t bytes = 0;
153         apr_bucket *eos = NULL;
154         apr_bucket *flush = NULL;
155         /* XXX: chunk_hdr must remain at this scope since it is used in a 
156          *      transient bucket.
157          */
158         char chunk_hdr[20]; /* enough space for the snprintf below */
159
160         APR_BRIGADE_FOREACH(e, b) {
161             if (APR_BUCKET_IS_EOS(e)) {
162                 /* there shouldn't be anything after the eos */
163                 eos = e;
164                 break;
165             }
166             if (APR_BUCKET_IS_FLUSH(e)) {
167                 flush = e;
168             }
169             else if (e->length == -1) {
170                 /* unknown amount of data (e.g. a pipe) */
171                 const char *data;
172                 apr_size_t len;
173
174                 rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
175                 if (rv != APR_SUCCESS) {
176                     return rv;
177                 }
178                 if (len > 0) {
179                     /*
180                      * There may be a new next bucket representing the
181                      * rest of the data stream on which a read() may
182                      * block so we pass down what we have so far.
183                      */
184                     bytes += len;
185                     more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
186                     break;
187                 }
188                 else {
189                     /* If there was nothing in this bucket then we can
190                      * safely move on to the next one without pausing
191                      * to pass down what we have counted up so far.
192                      */
193                     continue;
194                 }
195             }
196             else {
197                 bytes += e->length;
198             }
199         }
200
201         /*
202          * XXX: if there aren't very many bytes at this point it may
203          * be a good idea to set them aside and return for more,
204          * unless we haven't finished counting this brigade yet.
205          */
206         /* if there are content bytes, then wrap them in a chunk */
207         if (bytes > 0) {
208             apr_size_t hdr_len;
209             /*
210              * Insert the chunk header, specifying the number of bytes in
211              * the chunk.
212              */
213             /* XXX might be nice to have APR_OFF_T_FMT_HEX */
214             hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
215                                    "%qx" 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);
218             APR_BRIGADE_INSERT_HEAD(b, e);
219
220             /*
221              * Insert the end-of-chunk CRLF before an EOS or
222              * FLUSH bucket, or appended to the brigade
223              */
224             e = apr_bucket_immortal_create(ASCII_CRLF, 2);
225             if (eos != NULL) {
226                 APR_BUCKET_INSERT_BEFORE(eos, e);
227             }
228             else if (flush != NULL) {
229                 APR_BUCKET_INSERT_BEFORE(flush, e);
230             }
231             else {
232                 APR_BRIGADE_INSERT_TAIL(b, e);
233             }
234         }
235
236         /* RFC 2616, Section 3.6.1
237          *
238          * If there is an EOS bucket, then prefix it with:
239          *   1) the last-chunk marker ("0" CRLF)
240          *   2) the trailer
241          *   3) the end-of-chunked body CRLF
242          *
243          * If there is no EOS bucket, then do nothing.
244          *
245          * XXX: it would be nice to combine this with the end-of-chunk
246          * marker above, but this is a bit more straight-forward for
247          * now.
248          */
249         if (eos != NULL) {
250             /* XXX: (2) trailers ... does not yet exist */
251             e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF /* <trailers> */ ASCII_CRLF, 5);
252             APR_BUCKET_INSERT_BEFORE(eos, e);
253         }
254
255         /* pass the brigade to the next filter. */
256         rv = ap_pass_brigade(f->next, b);
257         if (rv != APR_SUCCESS || eos != NULL) {
258             return rv;
259         }
260     }
261     return APR_SUCCESS;
262 }
263
264 static const char *http_method(const request_rec *r)
265     { return "http"; }
266
267 static apr_port_t http_port(const request_rec *r)
268     { return DEFAULT_HTTP_PORT; }
269
270 static int ap_process_http_connection(conn_rec *c)
271 {
272     request_rec *r;
273  
274     /*
275      * Read and process each request found on our connection
276      * until no requests are left or we decide to close.
277      */
278  
279     ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
280     while ((r = ap_read_request(c)) != NULL) {
281  
282         c->keepalive = 0;
283         /* process the request if it was read without error */
284  
285         ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
286         if (r->status == HTTP_OK)
287             ap_process_request(r);
288  
289         if (ap_extended_status)
290             ap_increment_counts(c->sbh, r);
291  
292         if (!c->keepalive || c->aborted)
293             break;
294  
295         ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, r);
296         apr_pool_destroy(r->pool);
297  
298         if (ap_graceful_stop_signalled())
299             break;
300     }
301  
302     return OK;
303 }
304
305 static int http_create_request(request_rec *r)
306 {
307     if (!r->main && !r->prev) {
308         ap_add_output_filter_handle(ap_byterange_filter_handle,
309                                     NULL, r, r->connection);
310         ap_add_output_filter_handle(ap_content_length_filter_handle,
311                                     NULL, r, r->connection);
312         ap_add_output_filter_handle(ap_http_header_filter_handle,
313                                     NULL, r, r->connection);
314     }
315
316     return OK;
317 }
318
319 static void register_hooks(apr_pool_t *p)
320 {
321     ap_hook_process_connection(ap_process_http_connection,NULL,NULL,
322                                APR_HOOK_REALLY_LAST);
323     ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
324     ap_hook_http_method(http_method,NULL,NULL,APR_HOOK_REALLY_LAST);
325     ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
326     ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
327     ap_http_input_filter_handle =
328         ap_register_input_filter("HTTP_IN", ap_http_filter,
329                                  AP_FTYPE_PROTOCOL);
330     ap_http_header_filter_handle =
331         ap_register_output_filter("HTTP_HEADER", ap_http_header_filter, 
332                                   AP_FTYPE_PROTOCOL);
333     ap_chunk_filter_handle =
334         ap_register_output_filter("CHUNK", chunk_filter, AP_FTYPE_TRANSCODE);
335     ap_byterange_filter_handle =
336         ap_register_output_filter("BYTERANGE", ap_byterange_filter,
337                                   AP_FTYPE_PROTOCOL);
338 }
339
340 module AP_MODULE_DECLARE_DATA http_module = {
341     STANDARD20_MODULE_STUFF,
342     NULL,                       /* create per-directory config structure */
343     NULL,                       /* merge per-directory config structures */
344     NULL,                       /* create per-server config structure */
345     NULL,                       /* merge per-server config structures */
346     http_cmds,                  /* command apr_table_t */
347     register_hooks              /* register hooks */
348 };