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.
17 #define APR_WANT_STRFUNC
21 #include "apr_strings.h"
25 #include "util_filter.h"
27 /* NOTE: Apache's current design doesn't allow a pool to be passed thru,
28 so we depend on a global to hold the correct pool
30 #define FILTER_POOL apr_hook_global_pool
31 #include "apr_hooks.h" /* for apr_hook_global_pool */
34 ** This macro returns true/false if a given filter should be inserted BEFORE
35 ** another filter. This will happen when one of: 1) there isn't another
36 ** filter; 2) that filter has a higher filter type (class); 3) that filter
37 ** corresponds to a different request.
39 #define INSERT_BEFORE(f, before_this) ((before_this) == NULL \
40 || (before_this)->frec->ftype > (f)->frec->ftype \
41 || (before_this)->r != (f)->r)
43 /* Trie structure to hold the mapping from registered
44 * filter names to filters
47 typedef struct filter_trie_node filter_trie_node;
51 filter_trie_node *child;
52 } filter_trie_child_ptr;
54 /* Each trie node has an array of pointers to its children.
55 * The array is kept in sorted order so that add_any_filter()
56 * can do a binary search
58 struct filter_trie_node {
59 ap_filter_rec_t *frec;
60 filter_trie_child_ptr *children;
65 #define TRIE_INITIAL_SIZE 4
67 /* Link a trie node to its parent
69 static void trie_node_link(apr_pool_t *p, filter_trie_node *parent,
70 filter_trie_node *child, int c)
74 if (parent->nchildren == parent->size) {
75 filter_trie_child_ptr *new;
77 new = (filter_trie_child_ptr *)apr_palloc(p, parent->size *
78 sizeof(filter_trie_child_ptr));
79 memcpy(new, parent->children, parent->nchildren *
80 sizeof(filter_trie_child_ptr));
81 parent->children = new;
84 for (i = 0; i < parent->nchildren; i++) {
85 if (c == parent->children[i].c) {
88 else if (c < parent->children[i].c) {
92 for (j = parent->nchildren; j > i; j--) {
93 parent->children[j].c = parent->children[j - 1].c;
94 parent->children[j].child = parent->children[j - 1].child;
96 parent->children[i].c = c;
97 parent->children[i].child = child;
102 /* Allocate a new node for a trie.
103 * If parent is non-NULL, link the new node under the parent node with
104 * key 'c' (or, if an existing child node matches, return that one)
106 static filter_trie_node *trie_node_alloc(apr_pool_t *p,
107 filter_trie_node *parent, char c)
109 filter_trie_node *new_node;
112 for (i = 0; i < parent->nchildren; i++) {
113 if (c == parent->children[i].c) {
114 return parent->children[i].child;
116 else if (c < parent->children[i].c) {
121 (filter_trie_node *)apr_palloc(p, sizeof(filter_trie_node));
122 trie_node_link(p, parent, new_node, c);
124 else { /* No parent node */
125 new_node = (filter_trie_node *)apr_palloc(p,
126 sizeof(filter_trie_node));
129 new_node->frec = NULL;
130 new_node->nchildren = 0;
131 new_node->size = TRIE_INITIAL_SIZE;
132 new_node->children = (filter_trie_child_ptr *)apr_palloc(p,
133 new_node->size * sizeof(filter_trie_child_ptr));
137 static filter_trie_node *registered_output_filters = NULL;
138 static filter_trie_node *registered_input_filters = NULL;
141 static apr_status_t filter_cleanup(void *ctx)
143 registered_output_filters = NULL;
144 registered_input_filters = NULL;
148 static ap_filter_rec_t *get_filter_handle(const char *name,
149 const filter_trie_node *filter_set)
153 const filter_trie_node *node;
156 for (n = name; *n; n++) {
159 end = node->nchildren - 1;
160 while (end >= start) {
161 int middle = (end + start) / 2;
162 char ch = node->children[middle].c;
164 node = node->children[middle].child;
180 if (node && node->frec) {
187 AP_DECLARE(ap_filter_rec_t *)ap_get_output_filter_handle(const char *name)
189 return get_filter_handle(name, registered_output_filters);
192 AP_DECLARE(ap_filter_rec_t *)ap_get_input_filter_handle(const char *name)
194 return get_filter_handle(name, registered_input_filters);
197 static ap_filter_rec_t *register_filter(const char *name,
198 ap_filter_func filter_func,
199 ap_init_filter_func filter_init,
200 ap_filter_type ftype,
201 filter_trie_node **reg_filter_set)
203 ap_filter_rec_t *frec;
204 char *normalized_name;
206 filter_trie_node *node;
208 if (!*reg_filter_set) {
209 *reg_filter_set = trie_node_alloc(FILTER_POOL, NULL, 0);
212 normalized_name = apr_pstrdup(FILTER_POOL, name);
213 ap_str_tolower(normalized_name);
215 node = *reg_filter_set;
216 for (n = normalized_name; *n; n++) {
217 filter_trie_node *child = trie_node_alloc(FILTER_POOL, node, *n);
218 if (apr_isalpha(*n)) {
219 trie_node_link(FILTER_POOL, node, child, apr_toupper(*n));
227 frec = apr_palloc(FILTER_POOL, sizeof(*frec));
229 frec->name = normalized_name;
231 frec->filter_func = filter_func;
232 frec->filter_init_func = filter_init;
235 apr_pool_cleanup_register(FILTER_POOL, NULL, filter_cleanup,
236 apr_pool_cleanup_null);
240 AP_DECLARE(ap_filter_rec_t *) ap_register_input_filter(const char *name,
241 ap_in_filter_func filter_func,
242 ap_init_filter_func filter_init,
243 ap_filter_type ftype)
246 f.in_func = filter_func;
247 return register_filter(name, f, filter_init, ftype,
248 ®istered_input_filters);
251 AP_DECLARE(ap_filter_rec_t *) ap_register_output_filter(const char *name,
252 ap_out_filter_func filter_func,
253 ap_init_filter_func filter_init,
254 ap_filter_type ftype)
257 f.out_func = filter_func;
258 return register_filter(name, f, filter_init, ftype,
259 ®istered_output_filters);
262 static ap_filter_t *add_any_filter_handle(ap_filter_rec_t *frec, void *ctx,
263 request_rec *r, conn_rec *c,
264 ap_filter_t **r_filters,
265 ap_filter_t **p_filters,
266 ap_filter_t **c_filters)
268 apr_pool_t* p = r ? r->pool : c->pool;
269 ap_filter_t *f = apr_palloc(p, sizeof(*f));
272 if (frec->ftype < AP_FTYPE_PROTOCOL) {
277 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
278 "a content filter was added without a request: %s", frec->name);
282 else if (frec->ftype < AP_FTYPE_CONNECTION) {
287 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
288 "a protocol filter was added without a request: %s", frec->name);
302 if (INSERT_BEFORE(f, *outf)) {
306 ap_filter_t *first = NULL;
309 /* If we are adding our first non-connection filter,
310 * Then don't try to find the right location, it is
311 * automatically first.
313 if (*r_filters != *c_filters) {
315 while (first && (first->next != (*outf))) {
320 if (first && first != (*outf)) {
327 ap_filter_t *fscan = *outf;
328 while (!INSERT_BEFORE(f, fscan->next))
331 f->next = fscan->next;
335 if (frec->ftype < AP_FTYPE_CONNECTION && (*r_filters == *c_filters)) {
336 *r_filters = *p_filters;
341 static ap_filter_t *add_any_filter(const char *name, void *ctx,
342 request_rec *r, conn_rec *c,
343 const filter_trie_node *reg_filter_set,
344 ap_filter_t **r_filters,
345 ap_filter_t **p_filters,
346 ap_filter_t **c_filters)
348 if (reg_filter_set) {
350 const filter_trie_node *node;
352 node = reg_filter_set;
353 for (n = name; *n; n++) {
356 end = node->nchildren - 1;
357 while (end >= start) {
358 int middle = (end + start) / 2;
359 char ch = node->children[middle].c;
361 node = node->children[middle].child;
377 if (node && node->frec) {
378 return add_any_filter_handle(node->frec, ctx, r, c, r_filters,
379 p_filters, c_filters);
383 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
384 "an unknown filter was not added: %s", name);
388 AP_DECLARE(ap_filter_t *) ap_add_input_filter(const char *name, void *ctx,
389 request_rec *r, conn_rec *c)
391 return add_any_filter(name, ctx, r, c, registered_input_filters,
392 r ? &r->input_filters : NULL,
393 r ? &r->proto_input_filters : NULL, &c->input_filters);
396 AP_DECLARE(ap_filter_t *) ap_add_input_filter_handle(ap_filter_rec_t *f,
401 return add_any_filter_handle(f, ctx, r, c, r ? &r->input_filters : NULL,
402 r ? &r->proto_input_filters : NULL,
406 AP_DECLARE(ap_filter_t *) ap_add_output_filter(const char *name, void *ctx,
407 request_rec *r, conn_rec *c)
409 return add_any_filter(name, ctx, r, c, registered_output_filters,
410 r ? &r->output_filters : NULL,
411 r ? &r->proto_output_filters : NULL, &c->output_filters);
414 AP_DECLARE(ap_filter_t *) ap_add_output_filter_handle(ap_filter_rec_t *f,
419 return add_any_filter_handle(f, ctx, r, c, r ? &r->output_filters : NULL,
420 r ? &r->proto_output_filters : NULL,
424 static void remove_any_filter(ap_filter_t *f, ap_filter_t **r_filt, ap_filter_t **p_filt,
425 ap_filter_t **c_filt)
427 ap_filter_t **curr = r_filt ? r_filt : c_filt;
428 ap_filter_t *fscan = *curr;
430 if (p_filt && *p_filt == f)
431 *p_filt = (*p_filt)->next;
434 *curr = (*curr)->next;
438 while (fscan->next != f) {
439 if (!(fscan = fscan->next)) {
444 fscan->next = f->next;
447 AP_DECLARE(void) ap_remove_input_filter(ap_filter_t *f)
449 remove_any_filter(f, f->r ? &f->r->input_filters : NULL,
450 f->r ? &f->r->proto_input_filters : NULL,
451 &f->c->input_filters);
454 AP_DECLARE(void) ap_remove_output_filter(ap_filter_t *f)
456 remove_any_filter(f, f->r ? &f->r->output_filters : NULL,
457 f->r ? &f->r->proto_output_filters : NULL,
458 &f->c->output_filters);
462 * Read data from the next filter in the filter stack. Data should be
463 * modified in the bucket brigade that is passed in. The core allocates the
464 * bucket brigade, modules that wish to replace large chunks of data or to
465 * save data off to the side should probably create their own temporary
466 * brigade especially for that use.
468 AP_DECLARE(apr_status_t) ap_get_brigade(ap_filter_t *next,
469 apr_bucket_brigade *bb,
470 ap_input_mode_t mode,
471 apr_read_type_e block,
475 return next->frec->filter_func.in_func(next, bb, mode, block,
478 return AP_NOBODY_READ;
481 /* Pass the buckets to the next filter in the filter stack. If the
482 * current filter is a handler, we should get NULL passed in instead of
483 * the current filter. At that point, we can just call the first filter in
484 * the stack, or r->output_filters.
486 AP_DECLARE(apr_status_t) ap_pass_brigade(ap_filter_t *next,
487 apr_bucket_brigade *bb)
491 if ((e = APR_BRIGADE_LAST(bb)) && APR_BUCKET_IS_EOS(e) && next->r) {
492 /* This is only safe because HTTP_HEADER filter is always in
493 * the filter stack. This ensures that there is ALWAYS a
494 * request-based filter that we can attach this to. If the
495 * HTTP_FILTER is removed, and another filter is not put in its
496 * place, then handlers like mod_cgi, which attach their own
497 * EOS bucket to the brigade will be broken, because we will
498 * get two EOS buckets on the same request.
500 next->r->eos_sent = 1;
502 /* remember the eos for internal redirects, too */
504 request_rec *prev = next->r->prev;
512 return next->frec->filter_func.out_func(next, bb);
514 return AP_NOBODY_WROTE;
517 AP_DECLARE(apr_status_t) ap_save_brigade(ap_filter_t *f,
518 apr_bucket_brigade **saveto,
519 apr_bucket_brigade **b, apr_pool_t *p)
522 apr_status_t rv, srv = APR_SUCCESS;
524 /* If have never stored any data in the filter, then we had better
525 * create an empty bucket brigade so that we can concat.
528 *saveto = apr_brigade_create(p, f->c->bucket_alloc);
531 APR_RING_FOREACH(e, &(*b)->list, apr_bucket, link) {
532 rv = apr_bucket_setaside(e, p);
534 /* If the bucket type does not implement setaside, then
535 * (hopefully) morph it into a bucket type which does, and set
537 if (rv == APR_ENOTIMPL) {
541 rv = apr_bucket_read(e, &s, &n, APR_BLOCK_READ);
542 if (rv == APR_SUCCESS) {
543 rv = apr_bucket_setaside(e, p);
547 if (rv != APR_SUCCESS) {
549 /* Return an error but still save the brigade if
550 * ->setaside() is really not implemented. */
551 if (rv != APR_ENOTIMPL) {
556 APR_BRIGADE_CONCAT(*saveto, *b);
560 AP_DECLARE_NONSTD(apr_status_t) ap_filter_flush(apr_bucket_brigade *bb,
563 ap_filter_t *f = ctx;
565 return ap_pass_brigade(f, bb);
568 AP_DECLARE(apr_status_t) ap_fflush(ap_filter_t *f, apr_bucket_brigade *bb)
572 b = apr_bucket_flush_create(f->c->bucket_alloc);
573 APR_BRIGADE_INSERT_TAIL(bb, b);
574 return ap_pass_brigade(f, bb);
577 AP_DECLARE_NONSTD(apr_status_t) ap_fputstrs(ap_filter_t *f,
578 apr_bucket_brigade *bb, ...)
584 rv = apr_brigade_vputstrs(bb, ap_filter_flush, f, args);
589 AP_DECLARE_NONSTD(apr_status_t) ap_fprintf(ap_filter_t *f,
590 apr_bucket_brigade *bb,
598 rv = apr_brigade_vprintf(bb, ap_filter_flush, f, fmt, args);