httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joe Lewis <...@joe-lewis.com>
Subject Re: Intercepting HTTP 301/302 redirects
Date Thu, 01 Mar 2012 16:33:23 GMT
On 02/29/2012 07:46 PM, Swaminathan Bhaskar wrote:
> Thanks for the quick response Joe. Just to make sure, here is what I did:
>
> <IfModule mod_myfilter.c>
> <Location />
>         SetOutputFilter myfilter
> </Location>
> </IfModule>
>
> and the code
>
> #include <stdio.h>
> #include <httpd.h>
> #include <http_protocol.h>
> #include <http_config.h>
> #include <util_filter.h>
>
> #define MY_FILTER_NAME "myfilter"
>
> static int my_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
> {
>     fprintf(stderr, "mod_myfilter: status = %d, status-line = %s\n", 
> f->r->status, f->r->status_line);
>
>     ap_pass_brigade(f->next, bb);
>
>     return APR_SUCCESS;
> }
>
> static void my_filter_hooks(apr_pool_t *pool)
> {
>     ap_register_output_filter(MY_FILTER_NAME, my_output_filter, NULL, 
> AP_FTYPE_RESOURCE);
>
>     fprintf(stderr, "mod_myfilter: registered my_output_filter\n");
> }
>
> module AP_MODULE_DECLARE_DATA myfilter_module = {
>     STANDARD20_MODULE_STUFF,
>     NULL,
>     NULL,
>     NULL,
>     NULL,
>     NULL,
>     my_filter_hooks
> };
>
> I setup an intentional redirect for testing
>
> Redirect 301 /red.htm http://localhost/green.htm
>
> When I try http://localhost/, should I not see the output from myfilter ?

Maybe.  Remember, errors don't go through the same outputs as regular 
responses.  If you want to filter the results of anything outside of the 
standard 2xx HTTP responses, you have to insert an error filter as well, 
hence my reference to ap_hook_insert_error_filter().  As an example 
(borrowed from some of my source and modified for yours) :

static void insert_my_output_error_filter(request_rec *r) {
   ap_add_output_filter(MY_FILTER_NAME,NULL,r,r->connection);
}

static void my_filter_hooks(apr_pool_t *p) {
   ap_hook_insert_error_filter(insert_my_output_error_filter, NULL, 
NULL, APR_HOOK_LAST);
   
ap_register_output_filter(MY_FILTER_NAME,my_output_filter,NULL,AP_FTYPE_RESOURCE);
};

Again, output filters and errors do not coincide.  If you want to catch 
both, you have to hook both.  (Same thing for r->headers_out and 
r->err_headers_out - r->headers_out won't make it into r->err_headers_out).

Joe
--
http://www.silverhawk.net

Mime
View raw message