]> granicus.if.org Git - apache/blob - modules/http/http_core.c
Move the addition of default AP_HTTP_HTTP_HEADER filters to the
[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_protocol.h"      /* For index_of_response().  Grump. */
70 #include "http_request.h"
71
72 #include "util_filter.h"
73 #include "util_ebcdic.h"
74 #include "ap_mpm.h"
75 #include "scoreboard.h"
76
77 #include "mod_core.h"
78
79 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
80                                           const char *arg)
81 {
82     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
83     if (err != NULL) {
84         return err;
85     }
86
87     cmd->server->keep_alive_timeout = atoi(arg);
88     return NULL;
89 }
90
91 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
92                                   const char *arg) 
93 {
94     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
95     if (err != NULL) {
96         return err;
97     }
98
99     /* We've changed it to On/Off, but used to use numbers
100      * so we accept anything but "Off" or "0" as "On"
101      */
102     if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
103         cmd->server->keep_alive = 0;
104     }
105     else {
106         cmd->server->keep_alive = 1;
107     }
108     return NULL;
109 }
110
111 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
112                                       const char *arg)
113 {
114     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
115     if (err != NULL) {
116         return err;
117     }
118
119     cmd->server->keep_alive_max = atoi(arg);
120     return NULL;
121 }
122
123 static const command_rec http_cmds[] = {
124     AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
125                   "Keep-Alive timeout duration (sec)"),
126     AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
127      "Maximum number of Keep-Alive requests per connection, or 0 for infinite"),
128     AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
129                   "Whether persistent connections should be On or Off"),
130     { NULL }
131 };
132
133 /*
134  * HTTP/1.1 chunked transfer encoding filter.
135  */
136 static apr_status_t chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
137 {
138 #define ASCII_CRLF  "\015\012"
139 #define ASCII_ZERO  "\060"
140     apr_bucket_brigade *more = NULL;
141     apr_bucket *e;
142     apr_status_t rv;
143
144     for (more = NULL; b; b = more, more = NULL) {
145         apr_off_t bytes = 0;
146         apr_bucket *eos = NULL;
147         char chunk_hdr[20]; /* enough space for the snprintf below */
148
149         APR_BRIGADE_FOREACH(e, b) {
150             if (APR_BUCKET_IS_EOS(e)) {
151                 /* there shouldn't be anything after the eos */
152                 eos = e;
153                 break;
154             }
155             else if (e->length == -1) {
156                 /* unknown amount of data (e.g. a pipe) */
157                 const char *data;
158                 apr_size_t len;
159
160                 rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
161                 if (rv != APR_SUCCESS) {
162                     return rv;
163                 }
164                 if (len > 0) {
165                     /*
166                      * There may be a new next bucket representing the
167                      * rest of the data stream on which a read() may
168                      * block so we pass down what we have so far.
169                      */
170                     bytes += len;
171                     more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
172                     break;
173                 }
174                 else {
175                     /* If there was nothing in this bucket then we can
176                      * safely move on to the next one without pausing
177                      * to pass down what we have counted up so far.
178                      */
179                     continue;
180                 }
181             }
182             else {
183                 bytes += e->length;
184             }
185         }
186
187         /*
188          * XXX: if there aren't very many bytes at this point it may
189          * be a good idea to set them aside and return for more,
190          * unless we haven't finished counting this brigade yet.
191          */
192
193         /* if there are content bytes, then wrap them in a chunk */
194         if (bytes > 0) {
195             apr_size_t hdr_len;
196
197             /*
198              * Insert the chunk header, specifying the number of bytes in
199              * the chunk.
200              */
201             /* XXX might be nice to have APR_OFF_T_FMT_HEX */
202             hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
203                                    "%qx" CRLF, (apr_uint64_t)bytes);
204             ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
205             e = apr_bucket_transient_create(chunk_hdr, hdr_len);
206             APR_BRIGADE_INSERT_HEAD(b, e);
207
208             /*
209              * Insert the end-of-chunk CRLF before the EOS bucket, or
210              * appended to the brigade
211              */
212             e = apr_bucket_immortal_create(ASCII_CRLF, 2);
213             if (eos != NULL) {
214                 APR_BUCKET_INSERT_BEFORE(eos, e);
215             }
216             else {
217                 APR_BRIGADE_INSERT_TAIL(b, e);
218             }
219         }
220
221         /* RFC 2616, Section 3.6.1
222          *
223          * If there is an EOS bucket, then prefix it with:
224          *   1) the last-chunk marker ("0" CRLF)
225          *   2) the trailer
226          *   3) the end-of-chunked body CRLF
227          *
228          * If there is no EOS bucket, then do nothing.
229          *
230          * XXX: it would be nice to combine this with the end-of-chunk
231          * marker above, but this is a bit more straight-forward for
232          * now.
233          */
234         if (eos != NULL) {
235             /* XXX: (2) trailers ... does not yet exist */
236             e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF /* <trailers> */ ASCII_CRLF, 5);
237             APR_BUCKET_INSERT_BEFORE(eos, e);
238         }
239
240         /* pass the brigade to the next filter. */
241         rv = ap_pass_brigade(f->next, b);
242         if (rv != APR_SUCCESS || eos != NULL) {
243             return rv;
244         }
245     }
246
247     return APR_SUCCESS;
248 }
249
250 static const char *http_method(const request_rec *r)
251     { return "http"; }
252
253 static apr_port_t http_port(const request_rec *r)
254     { return DEFAULT_HTTP_PORT; }
255
256 static int ap_pre_http_connection(conn_rec *c)
257 {
258     ap_add_input_filter("HTTP_IN", NULL, NULL, c);
259     ap_add_input_filter("CORE_IN", NULL, NULL, c);
260     ap_add_output_filter("CORE", NULL, NULL, c);
261     return OK;
262 }
263  
264 static int ap_process_http_connection(conn_rec *c)
265 {
266     request_rec *r;
267  
268     /*
269      * Read and process each request found on our connection
270      * until no requests are left or we decide to close.
271      */
272  
273     ap_update_child_status(AP_CHILD_THREAD_FROM_ID(c->id), SERVER_BUSY_READ, NULL);
274     while ((r = ap_read_request(c)) != NULL) {
275  
276         /* process the request if it was read without error */
277  
278         ap_update_child_status(AP_CHILD_THREAD_FROM_ID(c->id), SERVER_BUSY_WRITE, NULL);
279         if (r->status == HTTP_OK)
280             ap_process_request(r);
281  
282         if (ap_extended_status)
283             ap_increment_counts(AP_CHILD_THREAD_FROM_ID(c->id), r);
284  
285         if (!c->keepalive || c->aborted)
286             break;
287  
288         ap_update_child_status(AP_CHILD_THREAD_FROM_ID(c->id), SERVER_BUSY_KEEPALIVE, NULL);
289         apr_pool_destroy(r->pool);
290  
291         if (ap_graceful_stop_signalled())
292             break;
293     }
294  
295     return OK;
296 }
297
298 static void ap_http_insert_filter(request_rec *r)
299 {
300     ap_add_output_filter("BYTERANGE", NULL, r, r->connection);
301     ap_add_output_filter("CONTENT_LENGTH", NULL, r, r->connection);
302     ap_add_output_filter("HTTP_HEADER", NULL, r, r->connection);
303 }
304
305 static void register_hooks(apr_pool_t *p)
306 {
307     ap_hook_pre_connection(ap_pre_http_connection,NULL,NULL,
308                                APR_HOOK_REALLY_LAST);
309     ap_hook_process_connection(ap_process_http_connection,NULL,NULL,
310                                APR_HOOK_REALLY_LAST);
311     ap_hook_http_method(http_method,NULL,NULL,APR_HOOK_REALLY_LAST);
312     ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
313
314     ap_hook_insert_filter(ap_http_insert_filter, NULL, NULL, APR_HOOK_REALLY_LAST);
315     ap_register_input_filter("HTTP_IN", ap_http_filter, AP_FTYPE_CONNECTION);
316     ap_register_input_filter("DECHUNK", ap_dechunk_filter, AP_FTYPE_TRANSCODE);
317     ap_register_output_filter("HTTP_HEADER", ap_http_header_filter, 
318                               AP_FTYPE_HTTP_HEADER);
319     ap_register_output_filter("CHUNK", chunk_filter, AP_FTYPE_TRANSCODE);
320     ap_register_output_filter("BYTERANGE", ap_byterange_filter,
321                               AP_FTYPE_HTTP_HEADER);
322 }
323
324 AP_DECLARE_DATA module http_module = {
325     STANDARD20_MODULE_STUFF,
326     NULL,                       /* create per-directory config structure */
327     NULL,                       /* merge per-directory config structures */
328     NULL,                       /* create per-server config structure */
329     NULL,                       /* merge per-server config structures */
330     http_cmds,                  /* command apr_table_t */
331     register_hooks              /* register hooks */
332 };