1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * mod_buffer.c --- Buffer the input and output filter stacks, collapse
19 * many small buckets into fewer large buckets.
23 #include "apr_strings.h"
24 #include "apr_buckets.h"
27 #include "ap_config.h"
28 #include "util_filter.h"
30 #include "http_config.h"
32 #include "http_request.h"
34 static const char bufferFilterName[] = "BUFFER";
35 module AP_MODULE_DECLARE_DATA buffer_module;
37 #define DEFAULT_BUFFER_SIZE 128*1024
39 typedef struct buffer_conf {
40 apr_off_t size; /* size of the buffer */
41 int size_set; /* has the size been set */
44 typedef struct buffer_ctx {
45 apr_bucket_brigade *bb;
46 apr_bucket_brigade *tmp;
53 * Buffer buckets being written to the output filter stack.
55 static apr_status_t buffer_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
58 request_rec *r = f->r;
59 buffer_ctx *ctx = f->ctx;
60 apr_status_t rv = APR_SUCCESS;
63 /* first time in? create a context */
66 /* buffering won't work on subrequests, it would be nice if
67 * it did. Within subrequests, we have no EOS to check for,
68 * so we don't know when to flush the buffer to the network
71 ap_remove_output_filter(f);
72 return ap_pass_brigade(f->next, bb);
75 ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
76 ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
77 ctx->conf = ap_get_module_config(f->r->per_dir_config, &buffer_module);
80 /* Do nothing if asked to filter nothing. */
81 if (APR_BRIGADE_EMPTY(bb)) {
82 return ap_pass_brigade(f->next, bb);
85 /* Empty buffer means we can potentially optimise below */
86 if (APR_BRIGADE_EMPTY(ctx->bb)) {
90 while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(bb)) {
95 e = APR_BRIGADE_FIRST(bb);
97 /* EOS means we are done. */
98 if (APR_BUCKET_IS_EOS(e)) {
100 /* should we add an etag? */
102 /* pass the EOS across */
103 APR_BUCKET_REMOVE(e);
104 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
106 /* pass what we have down the chain */
107 rv = ap_pass_brigade(f->next, ctx->bb);
111 /* A flush takes precedence over buffering */
112 if (APR_BUCKET_IS_FLUSH(e)) {
114 /* pass the flush bucket across */
115 APR_BUCKET_REMOVE(e);
116 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
118 /* pass what we have down the chain */
119 rv = ap_pass_brigade(f->next, ctx->bb);
123 /* metadata buckets are preserved as is */
124 if (APR_BUCKET_IS_METADATA(e)) {
126 * Remove meta data bucket from old brigade and insert into the
129 APR_BUCKET_REMOVE(e);
130 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
134 /* is our buffer full?
135 * If so, send what we have down the filter chain. If the buffer
136 * gets full, we can no longer compute a content length.
138 apr_brigade_length(ctx->bb, 1, &len);
139 if (len > ctx->conf->size) {
141 /* pass what we have down the chain */
142 rv = ap_pass_brigade(f->next, ctx->bb);
144 /* should break out of the loop, since our write to the client
145 * failed in some way. */
150 /* at this point we are ready to buffer.
151 * Buffering takes advantage of an optimisation in the handling of
152 * bucket brigades. Heap buckets are always created at a fixed
153 * size, regardless of the size of the data placed into them.
154 * The apr_brigade_write() call will first try and pack the data
155 * into any free space in the most recent heap bucket, before
156 * allocating a new bucket if necessary.
158 if (APR_SUCCESS == (rv = apr_bucket_read(e, &data, &size,
161 /* further optimisation: if the buckets are already heap
162 * buckets, and the buckets stay exactly APR_BUCKET_BUFF_SIZE
163 * long (as they would be if we were reading bits of a
164 * large bucket), then move the buckets instead of copying
167 if (move && APR_BUCKET_IS_HEAP(e)) {
168 APR_BUCKET_REMOVE(e);
169 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
170 if (APR_BUCKET_BUFF_SIZE != size) {
174 apr_brigade_write(ctx->bb, NULL, NULL, data, size);
175 apr_bucket_delete(e);
187 * Buffer buckets being read from the input filter stack.
189 static apr_status_t buffer_in_filter(ap_filter_t *f, apr_bucket_brigade *bb,
190 ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
192 apr_bucket *e, *after;
194 buffer_ctx *ctx = f->ctx;
196 /* buffer on main requests only */
197 if (!ap_is_initial_req(f->r)) {
198 ap_remove_input_filter(f);
199 return ap_get_brigade(f->next, bb, mode, block, readbytes);
202 /* first time in? create a context */
204 ctx = f->ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
205 ctx->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
206 ctx->tmp = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
207 ctx->conf = ap_get_module_config(f->r->per_dir_config, &buffer_module);
210 /* just get out of the way of things we don't want. */
211 if (mode != AP_MODE_READBYTES) {
212 return ap_get_brigade(f->next, bb, mode, block, readbytes);
215 /* if our buffer is empty, read off the network until the buffer is full */
216 if (APR_BRIGADE_EMPTY(ctx->bb)) {
219 ctx->remaining = ctx->conf->size;
221 while (!ctx->seen_eos && !seen_flush && ctx->remaining > 0) {
225 if (APR_BRIGADE_EMPTY(ctx->tmp)) {
226 rv = ap_get_brigade(f->next, ctx->tmp, mode, block,
229 /* if an error was received, bail out now. If the error is
230 * EAGAIN and we have not yet seen an EOS, we will definitely
231 * be called again, at which point we will send our buffered
232 * data. Instead of sending EAGAIN, some filters return an
233 * empty brigade instead when data is not yet available. In
234 * this case, pass through the APR_SUCCESS and emulate the
237 if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(ctx->tmp)) {
243 e = APR_BRIGADE_FIRST(ctx->tmp);
245 /* if we see an EOS, we are done */
246 if (APR_BUCKET_IS_EOS(e)) {
247 APR_BUCKET_REMOVE(e);
248 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
253 /* flush buckets clear the buffer */
254 if (APR_BUCKET_IS_FLUSH(e)) {
255 APR_BUCKET_REMOVE(e);
256 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
261 /* pass metadata buckets through */
262 if (APR_BUCKET_IS_METADATA(e)) {
263 APR_BUCKET_REMOVE(e);
264 APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
268 /* read the bucket in, pack it into the buffer */
269 if (APR_SUCCESS == (rv = apr_bucket_read(e, &data, &size,
271 apr_brigade_write(ctx->bb, NULL, NULL, data, size);
272 ctx->remaining -= size;
273 apr_bucket_delete(e);
278 } while (!APR_BRIGADE_EMPTY(ctx->tmp));
282 /* give the caller the data they asked for from the buffer */
283 apr_brigade_partition(ctx->bb, readbytes, &after);
284 e = APR_BRIGADE_FIRST(ctx->bb);
286 if (APR_BUCKET_IS_EOS(e)) {
287 /* last bucket read, step out of the way */
288 ap_remove_input_filter(f);
290 APR_BUCKET_REMOVE(e);
291 APR_BRIGADE_INSERT_TAIL(bb, e);
292 e = APR_BRIGADE_FIRST(ctx->bb);
298 static void *create_buffer_config(apr_pool_t *p, char *dummy)
300 buffer_conf *new = (buffer_conf *) apr_pcalloc(p, sizeof(buffer_conf));
302 new->size_set = 0; /* unset */
303 new->size = DEFAULT_BUFFER_SIZE; /* default size */
308 static void *merge_buffer_config(apr_pool_t *p, void *basev, void *addv)
310 buffer_conf *new = (buffer_conf *) apr_pcalloc(p, sizeof(buffer_conf));
311 buffer_conf *add = (buffer_conf *) addv;
312 buffer_conf *base = (buffer_conf *) basev;
314 new->size = (add->size_set == 0) ? base->size : add->size;
315 new->size_set = add->size_set || base->size_set;
320 static const char *set_buffer_size(cmd_parms *cmd, void *dconf, const char *arg)
322 buffer_conf *conf = dconf;
324 if (APR_SUCCESS != apr_strtoff(&(conf->size), arg, NULL, 10) || conf->size
326 return "BufferSize must be a size in bytes, and greater than zero";
333 static const command_rec buffer_cmds[] = { AP_INIT_TAKE1("BufferSize",
334 set_buffer_size, NULL, ACCESS_CONF,
335 "Maximum size of the buffer used by the buffer filter"), { NULL } };
337 static void register_hooks(apr_pool_t *p)
339 ap_register_output_filter(bufferFilterName, buffer_out_filter, NULL,
340 AP_FTYPE_CONTENT_SET);
341 ap_register_input_filter(bufferFilterName, buffer_in_filter, NULL,
342 AP_FTYPE_CONTENT_SET);
345 AP_DECLARE_MODULE(buffer) = {
346 STANDARD20_MODULE_STUFF,
347 create_buffer_config, /* create per-directory config structure */
348 merge_buffer_config, /* merge per-directory config structures */
349 NULL, /* create per-server config structure */
350 NULL, /* merge per-server config structures */
351 buffer_cmds, /* command apr_table_t */
352 register_hooks /* register hooks */