]> granicus.if.org Git - apache/blob - modules/filters/mod_ext_filter.c
Remove all references to CORE_PRIVATE.
[apache] / modules / filters / mod_ext_filter.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * mod_ext_filter allows Unix-style filters to filter http content.
19  */
20
21 #include "httpd.h"
22 #include "http_config.h"
23 #include "http_log.h"
24 #include "http_protocol.h"
25
26 #include "http_core.h"
27 #include "apr_buckets.h"
28 #include "util_filter.h"
29 #include "util_script.h"
30 #include "util_time.h"
31 #include "apr_strings.h"
32 #include "apr_hash.h"
33 #include "apr_lib.h"
34 #include "apr_poll.h"
35 #define APR_WANT_STRFUNC
36 #include "apr_want.h"
37
38 typedef struct ef_server_t {
39     apr_pool_t *p;
40     apr_hash_t *h;
41 } ef_server_t;
42
43 typedef struct ef_filter_t {
44     const char *name;
45     enum {INPUT_FILTER=1, OUTPUT_FILTER} mode;
46     ap_filter_type ftype;
47     const char *command;
48     const char *enable_env;
49     const char *disable_env;
50     char **args;
51     const char *intype;             /* list of IMTs we process (well, just one for now) */
52 #define INTYPE_ALL (char *)1
53     const char *outtype;            /* IMT of filtered output */
54 #define OUTTYPE_UNCHANGED (char *)1
55     int preserves_content_length;
56 } ef_filter_t;
57
58 typedef struct ef_dir_t {
59     int debug;
60     int log_stderr;
61 } ef_dir_t;
62
63 typedef struct ef_ctx_t {
64     apr_pool_t *p;
65     apr_proc_t *proc;
66     apr_procattr_t *procattr;
67     ef_dir_t *dc;
68     ef_filter_t *filter;
69     int noop;
70 #if APR_FILES_AS_SOCKETS
71     apr_pollset_t *pollset;
72 #endif
73 } ef_ctx_t;
74
75 module AP_MODULE_DECLARE_DATA ext_filter_module;
76 static const server_rec *main_server;
77
78 static apr_status_t ef_output_filter(ap_filter_t *, apr_bucket_brigade *);
79 static apr_status_t ef_input_filter(ap_filter_t *, apr_bucket_brigade *,
80                                     ap_input_mode_t, apr_read_type_e,
81                                     apr_off_t);
82
83 #define DBGLVL_SHOWOPTIONS         1
84 #define DBGLVL_ERRORCHECK          2
85 #define DBGLVL_GORY                9
86
87 #define ERRFN_USERDATA_KEY         "EXTFILTCHILDERRFN"
88
89 static void *create_ef_dir_conf(apr_pool_t *p, char *dummy)
90 {
91     ef_dir_t *dc = (ef_dir_t *)apr_pcalloc(p, sizeof(ef_dir_t));
92
93     dc->debug = -1;
94     dc->log_stderr = -1;
95
96     return dc;
97 }
98
99 static void *create_ef_server_conf(apr_pool_t *p, server_rec *s)
100 {
101     ef_server_t *conf;
102
103     conf = (ef_server_t *)apr_pcalloc(p, sizeof(ef_server_t));
104     conf->p = p;
105     conf->h = apr_hash_make(conf->p);
106     return conf;
107 }
108
109 static void *merge_ef_dir_conf(apr_pool_t *p, void *basev, void *overridesv)
110 {
111     ef_dir_t *a = (ef_dir_t *)apr_pcalloc (p, sizeof(ef_dir_t));
112     ef_dir_t *base = (ef_dir_t *)basev, *over = (ef_dir_t *)overridesv;
113
114     if (over->debug != -1) {        /* if admin coded something... */
115         a->debug = over->debug;
116     }
117     else {
118         a->debug = base->debug;
119     }
120
121     if (over->log_stderr != -1) {   /* if admin coded something... */
122         a->log_stderr = over->log_stderr;
123     }
124     else {
125         a->log_stderr = base->log_stderr;
126     }
127
128     return a;
129 }
130
131 static const char *add_options(cmd_parms *cmd, void *in_dc,
132                                const char *arg)
133 {
134     ef_dir_t *dc = in_dc;
135
136     if (!strncasecmp(arg, "DebugLevel=", 11)) {
137         dc->debug = atoi(arg + 11);
138     }
139     else if (!strcasecmp(arg, "LogStderr")) {
140         dc->log_stderr = 1;
141     }
142     else if (!strcasecmp(arg, "NoLogStderr")) {
143         dc->log_stderr = 0;
144     }
145     else {
146         return apr_pstrcat(cmd->temp_pool,
147                            "Invalid ExtFilterOptions option: ",
148                            arg,
149                            NULL);
150     }
151
152     return NULL;
153 }
154
155 static const char *parse_cmd(apr_pool_t *p, const char **args, ef_filter_t *filter)
156 {
157     if (**args == '"') {
158         const char *start = *args + 1;
159         char *parms;
160         int escaping = 0;
161         apr_status_t rv;
162
163         ++*args; /* move past leading " */
164         /* find true end of args string (accounting for escaped quotes) */
165         while (**args && (**args != '"' || (**args == '"' && escaping))) {
166             if (escaping) {
167                 escaping = 0;
168             }
169             else if (**args == '\\') {
170                 escaping = 1;
171             }
172             ++*args;
173         }
174         if (**args != '"') {
175             return "Expected cmd= delimiter";
176         }
177         /* copy *just* the arg string for parsing, */
178         parms = apr_pstrndup(p, start, *args - start);
179         ++*args; /* move past trailing " */
180
181         /* parse and tokenize the args. */
182         rv = apr_tokenize_to_argv(parms, &(filter->args), p);
183         if (rv != APR_SUCCESS) {
184             return "cmd= parse error";
185         }
186     }
187     else
188     {
189         /* simple path */
190         /* Allocate space for two argv pointers and parse the args. */
191         filter->args = (char **)apr_palloc(p, 2 * sizeof(char *));
192         filter->args[0] = ap_getword_white(p, args);
193         filter->args[1] = NULL; /* end of args */
194     }
195     if (!filter->args[0]) {
196         return "Invalid cmd= parameter";
197     }
198     filter->command = filter->args[0];
199
200     return NULL;
201 }
202
203 static const char *define_filter(cmd_parms *cmd, void *dummy, const char *args)
204 {
205     ef_server_t *conf = ap_get_module_config(cmd->server->module_config,
206                                              &ext_filter_module);
207     const char *token;
208     const char *name;
209     char *normalized_name;
210     ef_filter_t *filter;
211
212     name = ap_getword_white(cmd->pool, &args);
213     if (!name) {
214         return "Filter name not found";
215     }
216
217     /* During request processing, we find information about the filter
218      * by looking up the filter name provided by core server in our
219      * hash table.  But the core server has normalized the filter
220      * name by converting it to lower case.  Thus, when adding the
221      * filter to our hash table we have to use lower case as well.
222      */
223     normalized_name = apr_pstrdup(cmd->pool, name);
224     ap_str_tolower(normalized_name);
225
226     if (apr_hash_get(conf->h, normalized_name, APR_HASH_KEY_STRING)) {
227         return apr_psprintf(cmd->pool, "ExtFilter %s is already defined",
228                             name);
229     }
230
231     filter = (ef_filter_t *)apr_pcalloc(conf->p, sizeof(ef_filter_t));
232     filter->name = name;
233     filter->mode = OUTPUT_FILTER;
234     filter->ftype = AP_FTYPE_RESOURCE;
235     apr_hash_set(conf->h, normalized_name, APR_HASH_KEY_STRING, filter);
236
237     while (*args) {
238         while (apr_isspace(*args)) {
239             ++args;
240         }
241
242         /* Nasty parsing...  I wish I could simply use ap_getword_white()
243          * here and then look at the token, but ap_getword_white() doesn't
244          * do the right thing when we have cmd="word word word"
245          */
246         if (!strncasecmp(args, "preservescontentlength", 22)) {
247             token = ap_getword_white(cmd->pool, &args);
248             if (!strcasecmp(token, "preservescontentlength")) {
249                 filter->preserves_content_length = 1;
250             }
251             else {
252                 return apr_psprintf(cmd->pool,
253                                     "mangled argument `%s'",
254                                     token);
255             }
256             continue;
257         }
258
259         if (!strncasecmp(args, "mode=", 5)) {
260             args += 5;
261             token = ap_getword_white(cmd->pool, &args);
262             if (!strcasecmp(token, "output")) {
263                 filter->mode = OUTPUT_FILTER;
264             }
265             else if (!strcasecmp(token, "input")) {
266                 filter->mode = INPUT_FILTER;
267             }
268             else {
269                 return apr_psprintf(cmd->pool, "Invalid mode: `%s'",
270                                     token);
271             }
272             continue;
273         }
274
275         if (!strncasecmp(args, "ftype=", 6)) {
276             args += 6;
277             token = ap_getword_white(cmd->pool, &args);
278             filter->ftype = atoi(token);
279             continue;
280         }
281
282         if (!strncasecmp(args, "enableenv=", 10)) {
283             args += 10;
284             token = ap_getword_white(cmd->pool, &args);
285             filter->enable_env = token;
286             continue;
287         }
288
289         if (!strncasecmp(args, "disableenv=", 11)) {
290             args += 11;
291             token = ap_getword_white(cmd->pool, &args);
292             filter->disable_env = token;
293             continue;
294         }
295
296         if (!strncasecmp(args, "intype=", 7)) {
297             args += 7;
298             filter->intype = ap_getword_white(cmd->pool, &args);
299             continue;
300         }
301
302         if (!strncasecmp(args, "outtype=", 8)) {
303             args += 8;
304             filter->outtype = ap_getword_white(cmd->pool, &args);
305             continue;
306         }
307
308         if (!strncasecmp(args, "cmd=", 4)) {
309             args += 4;
310             if ((token = parse_cmd(cmd->pool, &args, filter))) {
311                 return token;
312             }
313             continue;
314         }
315
316         return apr_psprintf(cmd->pool, "Unexpected parameter: `%s'",
317                             args);
318     }
319
320     /* parsing is done...  register the filter
321      */
322     if (filter->mode == OUTPUT_FILTER) {
323         /* XXX need a way to ensure uniqueness among all filters */
324         ap_register_output_filter(filter->name, ef_output_filter, NULL, filter->ftype);
325     }
326     else if (filter->mode == INPUT_FILTER) {
327         /* XXX need a way to ensure uniqueness among all filters */
328         ap_register_input_filter(filter->name, ef_input_filter, NULL, filter->ftype);
329     }
330     else {
331         ap_assert(1 != 1); /* we set the field wrong somehow */
332     }
333
334     return NULL;
335 }
336
337 static const command_rec cmds[] =
338 {
339     AP_INIT_ITERATE("ExtFilterOptions",
340                     add_options,
341                     NULL,
342                     ACCESS_CONF, /* same as SetInputFilter/SetOutputFilter */
343                     "valid options: DebugLevel=n, LogStderr, NoLogStderr"),
344     AP_INIT_RAW_ARGS("ExtFilterDefine",
345                      define_filter,
346                      NULL,
347                      RSRC_CONF,
348                      "Define an external filter"),
349     {NULL}
350 };
351
352 static int ef_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_s)
353 {
354     main_server = main_s;
355     return OK;
356 }
357
358 static void register_hooks(apr_pool_t *p)
359 {
360     ap_hook_post_config(ef_init, NULL, NULL, APR_HOOK_MIDDLE);
361 }
362
363 static apr_status_t set_resource_limits(request_rec *r,
364                                         apr_procattr_t *procattr)
365 {
366 #if defined(RLIMIT_CPU)  || defined(RLIMIT_NPROC) || \
367     defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined (RLIMIT_AS)
368     core_dir_config *conf =
369         (core_dir_config *)ap_get_module_config(r->per_dir_config,
370                                                 &core_module);
371     apr_status_t rv;
372
373 #ifdef RLIMIT_CPU
374     rv = apr_procattr_limit_set(procattr, APR_LIMIT_CPU, conf->limit_cpu);
375     ap_assert(rv == APR_SUCCESS); /* otherwise, we're out of sync with APR */
376 #endif
377 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
378     rv = apr_procattr_limit_set(procattr, APR_LIMIT_MEM, conf->limit_mem);
379     ap_assert(rv == APR_SUCCESS); /* otherwise, we're out of sync with APR */
380 #endif
381 #ifdef RLIMIT_NPROC
382     rv = apr_procattr_limit_set(procattr, APR_LIMIT_NPROC, conf->limit_nproc);
383     ap_assert(rv == APR_SUCCESS); /* otherwise, we're out of sync with APR */
384 #endif
385
386 #endif /* if at least one limit defined */
387
388     return APR_SUCCESS;
389 }
390
391 static apr_status_t ef_close_file(void *vfile)
392 {
393     return apr_file_close(vfile);
394 }
395
396 static void child_errfn(apr_pool_t *pool, apr_status_t err, const char *description)
397 {
398     request_rec *r;
399     void *vr;
400     apr_file_t *stderr_log;
401     char errbuf[200];
402     char time_str[APR_CTIME_LEN];
403
404     apr_pool_userdata_get(&vr, ERRFN_USERDATA_KEY, pool);
405     r = vr;
406     apr_file_open_stderr(&stderr_log, pool);
407     ap_recent_ctime(time_str, apr_time_now());
408     apr_file_printf(stderr_log,
409                     "[%s] [client %s] mod_ext_filter (%d)%s: %s\n",
410                     time_str,
411                     r->connection->remote_ip,
412                     err,
413                     apr_strerror(err, errbuf, sizeof(errbuf)),
414                     description);
415 }
416
417 /* init_ext_filter_process: get the external filter process going
418  * This is per-filter-instance (i.e., per-request) initialization.
419  */
420 static apr_status_t init_ext_filter_process(ap_filter_t *f)
421 {
422     ef_ctx_t *ctx = f->ctx;
423     apr_status_t rc;
424     ef_dir_t *dc = ctx->dc;
425     const char * const *env;
426
427     ctx->proc = apr_pcalloc(ctx->p, sizeof(*ctx->proc));
428
429     rc = apr_procattr_create(&ctx->procattr, ctx->p);
430     ap_assert(rc == APR_SUCCESS);
431
432     rc = apr_procattr_io_set(ctx->procattr,
433                             APR_CHILD_BLOCK,
434                             APR_CHILD_BLOCK,
435                             APR_CHILD_BLOCK);
436     ap_assert(rc == APR_SUCCESS);
437
438     rc = set_resource_limits(f->r, ctx->procattr);
439     ap_assert(rc == APR_SUCCESS);
440
441     if (dc->log_stderr > 0) {
442         rc = apr_procattr_child_err_set(ctx->procattr,
443                                       f->r->server->error_log, /* stderr in child */
444                                       NULL);
445         ap_assert(rc == APR_SUCCESS);
446     }
447
448     rc = apr_procattr_child_errfn_set(ctx->procattr, child_errfn);
449     ap_assert(rc == APR_SUCCESS);
450     apr_pool_userdata_set(f->r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ctx->p);
451
452     if (dc->debug >= DBGLVL_ERRORCHECK) {
453         rc = apr_procattr_error_check_set(ctx->procattr, 1);
454         ap_assert(rc == APR_SUCCESS);
455     }
456
457     /* add standard CGI variables as well as DOCUMENT_URI, DOCUMENT_PATH_INFO,
458      * and QUERY_STRING_UNESCAPED
459      */
460     ap_add_cgi_vars(f->r);
461     ap_add_common_vars(f->r);
462     apr_table_setn(f->r->subprocess_env, "DOCUMENT_URI", f->r->uri);
463     apr_table_setn(f->r->subprocess_env, "DOCUMENT_PATH_INFO", f->r->path_info);
464     if (f->r->args) {
465             /* QUERY_STRING is added by ap_add_cgi_vars */
466         char *arg_copy = apr_pstrdup(f->r->pool, f->r->args);
467         ap_unescape_url(arg_copy);
468         apr_table_setn(f->r->subprocess_env, "QUERY_STRING_UNESCAPED",
469                        ap_escape_shell_cmd(f->r->pool, arg_copy));
470     }
471
472     env = (const char * const *) ap_create_environment(ctx->p,
473                                                        f->r->subprocess_env);
474
475     rc = apr_proc_create(ctx->proc,
476                             ctx->filter->command,
477                             (const char * const *)ctx->filter->args,
478                             env, /* environment */
479                             ctx->procattr,
480                             ctx->p);
481     if (rc != APR_SUCCESS) {
482         ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, f->r,
483                       "couldn't create child process to run `%s'",
484                       ctx->filter->command);
485         return rc;
486     }
487
488     apr_pool_note_subprocess(ctx->p, ctx->proc, APR_KILL_AFTER_TIMEOUT);
489
490     /* We don't want the handle to the child's stdin inherited by any
491      * other processes created by httpd.  Otherwise, when we close our
492      * handle, the child won't see EOF because another handle will still
493      * be open.
494      */
495
496     apr_pool_cleanup_register(ctx->p, ctx->proc->in,
497                          apr_pool_cleanup_null, /* other mechanism */
498                          ef_close_file);
499
500 #if APR_FILES_AS_SOCKETS
501     {
502         apr_pollfd_t pfd = { 0 };
503
504         rc = apr_pollset_create(&ctx->pollset, 2, ctx->p, 0);
505         ap_assert(rc == APR_SUCCESS);
506
507         pfd.p         = ctx->p;
508         pfd.desc_type = APR_POLL_FILE;
509         pfd.reqevents = APR_POLLOUT;
510         pfd.desc.f    = ctx->proc->in;
511         rc = apr_pollset_add(ctx->pollset, &pfd);
512         ap_assert(rc == APR_SUCCESS);
513
514         pfd.reqevents = APR_POLLIN;
515         pfd.desc.f    = ctx->proc->out;
516         rc = apr_pollset_add(ctx->pollset, &pfd);
517         ap_assert(rc == APR_SUCCESS);
518     }
519 #endif
520
521     return APR_SUCCESS;
522 }
523
524 static const char *get_cfg_string(ef_dir_t *dc, ef_filter_t *filter, apr_pool_t *p)
525 {
526     const char *debug_str = dc->debug == -1 ?
527         "DebugLevel=0" : apr_psprintf(p, "DebugLevel=%d", dc->debug);
528     const char *log_stderr_str = dc->log_stderr < 1 ?
529         "NoLogStderr" : "LogStderr";
530     const char *preserve_content_length_str = filter->preserves_content_length ?
531         "PreservesContentLength" : "!PreserveContentLength";
532     const char *intype_str = !filter->intype ?
533         "*/*" : filter->intype;
534     const char *outtype_str = !filter->outtype ?
535         "(unchanged)" : filter->outtype;
536
537     return apr_psprintf(p,
538                         "ExtFilterOptions %s %s %s ExtFilterInType %s "
539                         "ExtFilterOuttype %s",
540                         debug_str, log_stderr_str, preserve_content_length_str,
541                         intype_str, outtype_str);
542 }
543
544 static ef_filter_t *find_filter_def(const server_rec *s, const char *fname)
545 {
546     ef_server_t *sc;
547     ef_filter_t *f;
548
549     sc = ap_get_module_config(s->module_config, &ext_filter_module);
550     f = apr_hash_get(sc->h, fname, APR_HASH_KEY_STRING);
551     if (!f && s != main_server) {
552         s = main_server;
553         sc = ap_get_module_config(s->module_config, &ext_filter_module);
554         f = apr_hash_get(sc->h, fname, APR_HASH_KEY_STRING);
555     }
556     return f;
557 }
558
559 static apr_status_t init_filter_instance(ap_filter_t *f)
560 {
561     ef_ctx_t *ctx;
562     ef_dir_t *dc;
563     apr_status_t rv;
564
565     f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(ef_ctx_t));
566     dc = ap_get_module_config(f->r->per_dir_config,
567                               &ext_filter_module);
568     ctx->dc = dc;
569     /* look for the user-defined filter */
570     ctx->filter = find_filter_def(f->r->server, f->frec->name);
571     if (!ctx->filter) {
572         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
573                       "couldn't find definition of filter '%s'",
574                       f->frec->name);
575         return APR_EINVAL;
576     }
577     ctx->p = f->r->pool;
578     if (ctx->filter->intype &&
579         ctx->filter->intype != INTYPE_ALL) {
580         const char *ctypes;
581
582         if (ctx->filter->mode == INPUT_FILTER) {
583             ctypes = apr_table_get(f->r->headers_in, "Content-Type");
584         }
585         else {
586             ctypes = f->r->content_type;
587         }
588
589         if (ctypes) {
590             const char *ctype = ap_getword(f->r->pool, &ctypes, ';');
591
592             if (strcasecmp(ctx->filter->intype, ctype)) {
593                 /* wrong IMT for us; don't mess with the output */
594                 ctx->noop = 1;
595             }
596         }
597         else {
598             ctx->noop = 1;
599         }
600     }
601     if (ctx->filter->enable_env &&
602         !apr_table_get(f->r->subprocess_env, ctx->filter->enable_env)) {
603         /* an environment variable that enables the filter isn't set; bail */
604         ctx->noop = 1;
605     }
606     if (ctx->filter->disable_env &&
607         apr_table_get(f->r->subprocess_env, ctx->filter->disable_env)) {
608         /* an environment variable that disables the filter is set; bail */
609         ctx->noop = 1;
610     }
611     if (!ctx->noop) {
612         rv = init_ext_filter_process(f);
613         if (rv != APR_SUCCESS) {
614             return rv;
615         }
616         if (ctx->filter->outtype &&
617             ctx->filter->outtype != OUTTYPE_UNCHANGED) {
618             ap_set_content_type(f->r, ctx->filter->outtype);
619         }
620         if (ctx->filter->preserves_content_length != 1) {
621             /* nasty, but needed to avoid confusing the browser
622              */
623             apr_table_unset(f->r->headers_out, "Content-Length");
624         }
625     }
626
627     if (dc->debug >= DBGLVL_SHOWOPTIONS) {
628         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r,
629                       "%sfiltering `%s' of type `%s' through `%s', cfg %s",
630                       ctx->noop ? "NOT " : "",
631                       f->r->uri ? f->r->uri : f->r->filename,
632                       f->r->content_type ? f->r->content_type : "(unspecified)",
633                       ctx->filter->command,
634                       get_cfg_string(dc, ctx->filter, f->r->pool));
635     }
636
637     return APR_SUCCESS;
638 }
639
640 /* drain_available_output():
641  *
642  * if any data is available from the filter, read it and append it
643  * to the the bucket brigade
644  */
645 static apr_status_t drain_available_output(ap_filter_t *f,
646                                            apr_bucket_brigade *bb)
647 {
648     request_rec *r = f->r;
649     conn_rec *c = r->connection;
650     ef_ctx_t *ctx = f->ctx;
651     ef_dir_t *dc = ctx->dc;
652     apr_size_t len;
653     char buf[4096];
654     apr_status_t rv;
655     apr_bucket *b;
656
657     while (1) {
658         len = sizeof(buf);
659         rv = apr_file_read(ctx->proc->out,
660                       buf,
661                       &len);
662         if ((rv && !APR_STATUS_IS_EAGAIN(rv)) ||
663             dc->debug >= DBGLVL_GORY) {
664             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
665                           "apr_file_read(child output), len %" APR_SIZE_T_FMT,
666                           !rv ? len : -1);
667         }
668         if (rv != APR_SUCCESS) {
669             return rv;
670         }
671         b = apr_bucket_heap_create(buf, len, NULL, c->bucket_alloc);
672         APR_BRIGADE_INSERT_TAIL(bb, b);
673         return APR_SUCCESS;
674     }
675     /* we should never get here; if we do, a bogus error message would be
676      * the least of our problems
677      */
678     return APR_ANONYMOUS;
679 }
680
681 static apr_status_t pass_data_to_filter(ap_filter_t *f, const char *data,
682                                         apr_size_t len, apr_bucket_brigade *bb)
683 {
684     ef_ctx_t *ctx = f->ctx;
685     ef_dir_t *dc = ctx->dc;
686     apr_status_t rv;
687     apr_size_t bytes_written = 0;
688     apr_size_t tmplen;
689
690     do {
691         tmplen = len - bytes_written;
692         rv = apr_file_write(ctx->proc->in,
693                        (const char *)data + bytes_written,
694                        &tmplen);
695         bytes_written += tmplen;
696         if (rv != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(rv)) {
697             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, f->r,
698                           "apr_file_write(child input), len %" APR_SIZE_T_FMT,
699                           tmplen);
700             return rv;
701         }
702         if (APR_STATUS_IS_EAGAIN(rv)) {
703             /* XXX handle blocking conditions here...  if we block, we need
704              * to read data from the child process and pass it down to the
705              * next filter!
706              */
707             rv = drain_available_output(f, bb);
708             if (APR_STATUS_IS_EAGAIN(rv)) {
709 #if APR_FILES_AS_SOCKETS
710                 int num_events;
711                 const apr_pollfd_t *pdesc;
712
713                 rv = apr_pollset_poll(ctx->pollset, f->r->server->timeout,
714                                       &num_events, &pdesc);
715                 if (rv || dc->debug >= DBGLVL_GORY) {
716                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG,
717                                   rv, f->r, "apr_pollset_poll()");
718                 }
719                 if (rv != APR_SUCCESS && !APR_STATUS_IS_EINTR(rv)) {
720                     /* some error such as APR_TIMEUP */
721                     return rv;
722                 }
723 #else /* APR_FILES_AS_SOCKETS */
724                 /* Yuck... I'd really like to wait until I can read
725                  * or write, but instead I have to sleep and try again
726                  */
727                 apr_sleep(100000); /* 100 milliseconds */
728                 if (dc->debug >= DBGLVL_GORY) {
729                     ap_log_rerror(APLOG_MARK, APLOG_DEBUG,
730                                   0, f->r, "apr_sleep()");
731                 }
732 #endif /* APR_FILES_AS_SOCKETS */
733             }
734             else if (rv != APR_SUCCESS) {
735                 return rv;
736             }
737         }
738     } while (bytes_written < len);
739     return rv;
740 }
741
742 /* ef_unified_filter:
743  *
744  * runs the bucket brigade bb through the filter and puts the result into
745  * bb, dropping the previous content of bb (the input)
746  */
747
748 static int ef_unified_filter(ap_filter_t *f, apr_bucket_brigade *bb)
749 {
750     request_rec *r = f->r;
751     conn_rec *c = r->connection;
752     ef_ctx_t *ctx = f->ctx;
753     apr_bucket *b;
754     ef_dir_t *dc;
755     apr_size_t len;
756     const char *data;
757     apr_status_t rv;
758     char buf[4096];
759     apr_bucket *eos = NULL;
760     apr_bucket_brigade *bb_tmp;
761
762     dc = ctx->dc;
763     bb_tmp = apr_brigade_create(r->pool, c->bucket_alloc);
764
765     for (b = APR_BRIGADE_FIRST(bb);
766          b != APR_BRIGADE_SENTINEL(bb);
767          b = APR_BUCKET_NEXT(b))
768     {
769         if (APR_BUCKET_IS_EOS(b)) {
770             eos = b;
771             break;
772         }
773
774         rv = apr_bucket_read(b, &data, &len, APR_BLOCK_READ);
775         if (rv != APR_SUCCESS) {
776             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "apr_bucket_read()");
777             return rv;
778         }
779
780         /* Good cast, we just tested len isn't negative */
781         if (len > 0 &&
782             (rv = pass_data_to_filter(f, data, (apr_size_t)len, bb_tmp))
783                 != APR_SUCCESS) {
784             return rv;
785         }
786     }
787
788     apr_brigade_cleanup(bb);
789     APR_BRIGADE_CONCAT(bb, bb_tmp);
790     apr_brigade_destroy(bb_tmp);
791
792     if (eos) {
793         /* close the child's stdin to signal that no more data is coming;
794          * that will cause the child to finish generating output
795          */
796         if ((rv = apr_file_close(ctx->proc->in)) != APR_SUCCESS) {
797             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
798                           "apr_file_close(child input)");
799             return rv;
800         }
801         /* since we've seen eos and closed the child's stdin, set the proper pipe
802          * timeout; we don't care if we don't return from apr_file_read() for a while...
803          */
804         rv = apr_file_pipe_timeout_set(ctx->proc->out,
805                                        r->server->timeout);
806         if (rv) {
807             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
808                           "apr_file_pipe_timeout_set(child output)");
809             return rv;
810         }
811     }
812
813     do {
814         len = sizeof(buf);
815         rv = apr_file_read(ctx->proc->out,
816                       buf,
817                       &len);
818         if ((rv && !APR_STATUS_IS_EOF(rv) && !APR_STATUS_IS_EAGAIN(rv)) ||
819             dc->debug >= DBGLVL_GORY) {
820             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
821                           "apr_file_read(child output), len %" APR_SIZE_T_FMT,
822                           !rv ? len : -1);
823         }
824         if (APR_STATUS_IS_EAGAIN(rv)) {
825             if (eos) {
826                 /* should not occur, because we have an APR timeout in place */
827                 AP_DEBUG_ASSERT(1 != 1);
828             }
829             return APR_SUCCESS;
830         }
831
832         if (rv == APR_SUCCESS) {
833             b = apr_bucket_heap_create(buf, len, NULL, c->bucket_alloc);
834             APR_BRIGADE_INSERT_TAIL(bb, b);
835         }
836     } while (rv == APR_SUCCESS);
837
838     if (!APR_STATUS_IS_EOF(rv)) {
839         return rv;
840     }
841
842     if (eos) {
843         b = apr_bucket_eos_create(c->bucket_alloc);
844         APR_BRIGADE_INSERT_TAIL(bb, b);
845     }
846
847     return APR_SUCCESS;
848 }
849
850 static apr_status_t ef_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
851 {
852     request_rec *r = f->r;
853     ef_ctx_t *ctx = f->ctx;
854     apr_status_t rv;
855
856     if (!ctx) {
857         if ((rv = init_filter_instance(f)) != APR_SUCCESS) {
858             return rv;
859         }
860         ctx = f->ctx;
861     }
862     if (ctx->noop) {
863         ap_remove_output_filter(f);
864         return ap_pass_brigade(f->next, bb);
865     }
866
867     rv = ef_unified_filter(f, bb);
868     if (rv != APR_SUCCESS) {
869         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
870                       "ef_unified_filter() failed");
871     }
872
873     if ((rv = ap_pass_brigade(f->next, bb)) != APR_SUCCESS) {
874         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
875                       "ap_pass_brigade() failed");
876     }
877     return rv;
878 }
879
880 static int ef_input_filter(ap_filter_t *f, apr_bucket_brigade *bb,
881                            ap_input_mode_t mode, apr_read_type_e block,
882                            apr_off_t readbytes)
883 {
884     ef_ctx_t *ctx = f->ctx;
885     apr_status_t rv;
886
887     if (!ctx) {
888         if ((rv = init_filter_instance(f)) != APR_SUCCESS) {
889             return rv;
890         }
891         ctx = f->ctx;
892     }
893
894     if (ctx->noop) {
895         ap_remove_input_filter(f);
896         return ap_get_brigade(f->next, bb, mode, block, readbytes);
897     }
898
899     rv = ap_get_brigade(f->next, bb, mode, block, readbytes);
900     if (rv != APR_SUCCESS) {
901         return rv;
902     }
903
904     rv = ef_unified_filter(f, bb);
905     return rv;
906 }
907
908 module AP_MODULE_DECLARE_DATA ext_filter_module =
909 {
910     STANDARD20_MODULE_STUFF,
911     create_ef_dir_conf,
912     merge_ef_dir_conf,
913     create_ef_server_conf,
914     NULL,
915     cmds,
916     register_hooks
917 };