You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Pranesh Vadhirajan <va...@teralogics.com> on 2012/01/13 20:10:51 UTC

Some fundamental questions about output filters

Hello,

I've been trying to use output filters in my code in order to read the
response that is being sent from Apache to a web client.  

 

I believe this would be a proper use for an output filter, please correct me
if I'm wrong.  I am attempting to read the content of the response from
within a certain module, and I would like to know how to access the contents
of the output filter etc.  

 

I've been looking into how output filters work, but I'm not sure I
understand the concept.  

 

So here are my basic questions on how to write that code:

 

Can I register my output filter within the registration hooks of my module
code?  For example I did the following and I am getting various errors, and
I am wondering if there's a proper way to do it.  The goal is to be able to
access the contents of the response from within my module and to print it to
my error log (or anywhere else), but I am not sure how to do this. : 

 

static apr_status_t apcontent_filter_in(ap_filter_t *f, apr_bucket_brigade
*b, ap_input_mode_t mode, apr_size_t *readbytes)

{

    const char *str;

    int length;

    apr_bucket *e;

 

        ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);

 

        e = APR_BRIGADE_FIRST(b);

 

        if (e->type == NULL) {

                return APR_SUCCESS;

        }

 

        apr_bucket_read(e, &str, &length, 1);

        fprintf(stderr,"req body: %s\n",str);

 

    return APR_SUCCESS;

}

 

static int my_handler(request_rec* r) {

 

        int is_ip = 0, fqdn_id, ap_id, nid_sl = 0, nid_fl = 0;

        char time_buffer[30]; 

        char* remote_address;

        char* cookie;

        char* session_id = NULL;

 

        apr_bucket_brigade* request_body_brigade;

        ap_filter_t *f;

        apr_size_t readbytes=(apr_size_t)r->sent_bodyct;

 
apcontent_filter_in(f,request_body_brigade,AP_MODE_READBYTES,&readbytes);

 

        return DECLINED;

 

}

 

static void my_register_hooks (apr_pool_t *p) {

        ap_hook_handler(my_handler, NULL, NULL, APR_HOOK_MIDDLE);

        ap_register_input_filter("get_request_body",
apcontent_filter_in,NULL, AP_FTYPE_RESOURCE) ;

}

 

module AP_MODULE_DECLARE_DATA my_module = 

{

        STANDARD20_MODULE_STUFF,^M

        NULL,                                           /* Per-Directory
Configuration */

        NULL,                                           /* Directory Config
Merger */

        NULL,                                           /* Per-Server
Configuration */

        NULL,                                           /* Server Config
Merger */

        NULL,                                           /* Command Table
(Directives) */

        my_register_hooks     /* Registering Hooks */

};

 

If this is not the proper way to read from an output filter could someone
please explain the proper way?

 

Thanks,
Pranesh Vadhirajan

 

 


Re: Input Filters -- not seeing anything

Posted by Joe Lewis <jo...@joe-lewis.com>.
On 01/16/2012 02:21 PM, Pranesh Vadhirajan wrote:
> Thanks for the response, Joe.  Are the ap_*log lines to be used in lieu of
> the fprintfs that I have?   Or do those actually display errors during
> compilation of my code through apxs?
>
> Thanks,
> Pranesh

You would be simply logging a message through apache's defaults.  I 
usually suggest this kind of "logging" vs. fprintf's, since fprintf's 
might not go where you expect.

Joe

RE: Input Filters -- not seeing anything

Posted by Pranesh Vadhirajan <va...@teralogics.com>.
Thanks for the response, Joe.  Are the ap_*log lines to be used in lieu of
the fprintfs that I have?   Or do those actually display errors during
compilation of my code through apxs?

Thanks,
Pranesh

-----Original Message-----
From: Joe Lewis [mailto:jlewis@silverhawk.net] On Behalf Of Joe Lewis
Sent: Monday, January 16, 2012 12:47 PM
To: modules-dev@httpd.apache.org
Subject: Re: Input Filters -- not seeing anything

You can do two things.

Place some ap_*log "debug lines" in your code and compile to find the 
line, or use the Gnu debugger and step through apache until you get the 
segmentation fault.


On 01/16/2012 10:45 AM, Pranesh Vadhirajan wrote:
> I forgot to mention in my original email, that I'm receiving segmentation
> faults, but I'm not sure what part of my code is causing this.
>
> Pranesh

You can do two things.

Place some ap_*log "debug lines" in your code and compile to find the 
line, or use the Gnu debugger and step through apache until you get the 
segmentation fault.

Joe
--
www.silverhawk.net

Re: Input Filters -- not seeing anything

Posted by Joe Lewis <jo...@joe-lewis.com>.
You can do two things.

Place some ap_*log "debug lines" in your code and compile to find the 
line, or use the Gnu debugger and step through apache until you get the 
segmentation fault.


On 01/16/2012 10:45 AM, Pranesh Vadhirajan wrote:
> I forgot to mention in my original email, that I'm receiving segmentation
> faults, but I'm not sure what part of my code is causing this.
>
> Pranesh

You can do two things.

Place some ap_*log "debug lines" in your code and compile to find the 
line, or use the Gnu debugger and step through apache until you get the 
segmentation fault.

Joe
--
www.silverhawk.net

RE: Input Filters -- not seeing anything

Posted by Pranesh Vadhirajan <va...@teralogics.com>.
I forgot to mention in my original email, that I'm receiving segmentation 
faults, but I'm not sure what part of my code is causing this.

Pranesh

-----Original Message-----
From: Pranesh Vadhirajan [mailto:vadhirajan@teralogics.com]
Sent: Monday, January 16, 2012 11:41 AM
To: modules-dev@httpd.apache.org
Subject: RE: Input Filters -- not seeing anything

Sorry, that was just a typo.  The include does have the closing " in my source
file.

Thanks,
Pranesh

-----Original Message-----
From: Joe Lewis [mailto:jlewis@silverhawk.net] On Behalf Of Joe Lewis
Sent: Monday, January 16, 2012 11:38 AM
To: modules-dev@httpd.apache.org
Subject: Re: Input Filters -- not seeing anything

I think he meant on your include line :

#include "apr_buckets.h"
#include "util_filter.h

stat



On 01/16/2012 09:37 AM, Pranesh Vadhirajan wrote:
> I believe it's a header that is included in the Apache build, so I would
> think it doesn't have that issue.
>
> Thanks,
> Pranesh
>
> -----Original Message-----
> From: Arturo 'Buanzo' Busleiman [mailto:buanzo@buanzo.com.ar]
> Sent: Monday, January 16, 2012 11:18 AM
> To: modules-dev@httpd.apache.org
> Subject: Re: Input Filters -- not seeing anything
>
> Is util_filter.h lacking a closing " in the #include ?
>
>
> On 1/16/12, Pranesh Vadhirajan<va...@teralogics.com>  wrote:
>> Hello,
>>
>> I'm very new to developing Input Filters with Apache. I have written an
>> input filter to read the request body content and print it to my error
> log.
>> I have two print statements in my filtering function (one to let me know
>> that my filter code has been called and the other to print the request
>> content).  Yet, I'm not seeing anything happening (nothing is getting
>> printed to my log).  I have tried different things but I'm not able to
> make
>> anything work, so I've attached the code below hoping to get some insight
> on
>> why my filter doesn't seem to work.  I am building the module using apxs
> and
>> I have a LoadModule directive in the httpd.conf file to load my module.
> I'm
>> new to the filter API and I'm totally out of ideas at this point as to why
>> this is not working.
>>
>>
>> #include "httpd.h"
>> #include "http_core.h"
>> #include "http_protocol.h"
>> #include "http_config.h"
>> #include "http_protocol.h"
>> #include "http_main.h"
>> #include "http_log.h"
>> #include "http_request.h"
>> #include "util_script.h"
>> #include "http_connection.h"
>> #include<stdlib.h>
>> #include<time.h>
>> #include<sys/time.h>
>> #include<sys/types.h>
>> #include<regex.h>
>> #include<stdio.h>
>> #include "uvds_metrics_sessions.h"
>>
>> #include "apr.h"
>> #include "apr_lib.h"
>> #include "apr_general.h"
>> #include "apr_strings.h"
>> #include "ap_config.h"
>> #include "apr_buckets.h"
>> #include "util_filter.h
>>
>> static apr_status_t req_body_filter_in(ap_filter_t *f, apr_bucket_brigade
>> *b, ap_input_mode_t mode, apr_size_t *readbytes)
>> {
>>      const char *str;
>>      int length;
>>      apr_bucket *e;
>>
>>      fprintf(stderr,"reached this point\n");
>>
>>      ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);
>>
>>      e = APR_BRIGADE_FIRST(b);
>>
>>      if (e->type == NULL) {
>>      return APR_SUCCESS;
>>      }
>>
>>      apr_bucket_read(e,&str, (apr_size_t*)&length, APR_NONBLOCK_READ);
>>      fprintf(stderr,"req body: %s\n",str);
>>
>>      return APR_SUCCESS;
>> }
>>
>> static void my_register_hooks (apr_pool_t *p) {
>>          ap_hook_insert_filter(req_body_filter_in, NULL , NULL ,
>> APR_HOOK_MIDDLE) ;
>>          //ap_register_input_filter("get_request_body" , req_body_filter_in
> ,
>> NULL , AP_FTYPE_RESOURCE) ;
>>
>> }
>>
>> module AP_MODULE_DECLARE_DATA my_module =
>> {
>>          STANDARD20_MODULE_STUFF,
>>          NULL,                                           /* Per-Directory
>> Configuration */
>>          NULL,                                           /* Directory
> Config
>> Merger */
>>          NULL,                                           /* Per-Server
>> Configuration */
>>          NULL,                                           /* Server Config
>> Merger */
>>          NULL,                                           /* Command Table
>> (Directives) */
>>          my_register_hooks     				  /*
> Registering
>> Hooks */
>>
>> };
>>
>>

RE: Input Filters -- not seeing anything

Posted by Pranesh Vadhirajan <va...@teralogics.com>.
Sorry, that was just a typo.  The include does have the closing " in my source 
file.

Thanks,
Pranesh

-----Original Message-----
From: Joe Lewis [mailto:jlewis@silverhawk.net] On Behalf Of Joe Lewis
Sent: Monday, January 16, 2012 11:38 AM
To: modules-dev@httpd.apache.org
Subject: Re: Input Filters -- not seeing anything

I think he meant on your include line :

#include "apr_buckets.h"
#include "util_filter.h

stat



On 01/16/2012 09:37 AM, Pranesh Vadhirajan wrote:
> I believe it's a header that is included in the Apache build, so I would
> think it doesn't have that issue.
>
> Thanks,
> Pranesh
>
> -----Original Message-----
> From: Arturo 'Buanzo' Busleiman [mailto:buanzo@buanzo.com.ar]
> Sent: Monday, January 16, 2012 11:18 AM
> To: modules-dev@httpd.apache.org
> Subject: Re: Input Filters -- not seeing anything
>
> Is util_filter.h lacking a closing " in the #include ?
>
>
> On 1/16/12, Pranesh Vadhirajan<va...@teralogics.com>  wrote:
>> Hello,
>>
>> I'm very new to developing Input Filters with Apache. I have written an
>> input filter to read the request body content and print it to my error
> log.
>> I have two print statements in my filtering function (one to let me know
>> that my filter code has been called and the other to print the request
>> content).  Yet, I'm not seeing anything happening (nothing is getting
>> printed to my log).  I have tried different things but I'm not able to
> make
>> anything work, so I've attached the code below hoping to get some insight
> on
>> why my filter doesn't seem to work.  I am building the module using apxs
> and
>> I have a LoadModule directive in the httpd.conf file to load my module.
> I'm
>> new to the filter API and I'm totally out of ideas at this point as to why
>> this is not working.
>>
>>
>> #include "httpd.h"
>> #include "http_core.h"
>> #include "http_protocol.h"
>> #include "http_config.h"
>> #include "http_protocol.h"
>> #include "http_main.h"
>> #include "http_log.h"
>> #include "http_request.h"
>> #include "util_script.h"
>> #include "http_connection.h"
>> #include<stdlib.h>
>> #include<time.h>
>> #include<sys/time.h>
>> #include<sys/types.h>
>> #include<regex.h>
>> #include<stdio.h>
>> #include "uvds_metrics_sessions.h"
>>
>> #include "apr.h"
>> #include "apr_lib.h"
>> #include "apr_general.h"
>> #include "apr_strings.h"
>> #include "ap_config.h"
>> #include "apr_buckets.h"
>> #include "util_filter.h
>>
>> static apr_status_t req_body_filter_in(ap_filter_t *f, apr_bucket_brigade
>> *b, ap_input_mode_t mode, apr_size_t *readbytes)
>> {
>>      const char *str;
>>      int length;
>>      apr_bucket *e;
>>
>>      fprintf(stderr,"reached this point\n");
>>
>>      ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);
>>
>>      e = APR_BRIGADE_FIRST(b);
>>
>>      if (e->type == NULL) {
>>      return APR_SUCCESS;
>>      }
>>
>>      apr_bucket_read(e,&str, (apr_size_t*)&length, APR_NONBLOCK_READ);
>>      fprintf(stderr,"req body: %s\n",str);
>>
>>      return APR_SUCCESS;
>> }
>>
>> static void my_register_hooks (apr_pool_t *p) {
>>          ap_hook_insert_filter(req_body_filter_in, NULL , NULL ,
>> APR_HOOK_MIDDLE) ;
>>          //ap_register_input_filter("get_request_body" , req_body_filter_in
> ,
>> NULL , AP_FTYPE_RESOURCE) ;
>>
>> }
>>
>> module AP_MODULE_DECLARE_DATA my_module =
>> {
>>          STANDARD20_MODULE_STUFF,
>>          NULL,                                           /* Per-Directory
>> Configuration */
>>          NULL,                                           /* Directory
> Config
>> Merger */
>>          NULL,                                           /* Per-Server
>> Configuration */
>>          NULL,                                           /* Server Config
>> Merger */
>>          NULL,                                           /* Command Table
>> (Directives) */
>>          my_register_hooks     				  /*
> Registering
>> Hooks */
>>
>> };
>>
>>

Re: Input Filters -- not seeing anything

Posted by Joe Lewis <jo...@joe-lewis.com>.
I think he meant on your include line :

#include "apr_buckets.h"
#include "util_filter.h

stat



On 01/16/2012 09:37 AM, Pranesh Vadhirajan wrote:
> I believe it's a header that is included in the Apache build, so I would
> think it doesn't have that issue.
>
> Thanks,
> Pranesh
>
> -----Original Message-----
> From: Arturo 'Buanzo' Busleiman [mailto:buanzo@buanzo.com.ar]
> Sent: Monday, January 16, 2012 11:18 AM
> To: modules-dev@httpd.apache.org
> Subject: Re: Input Filters -- not seeing anything
>
> Is util_filter.h lacking a closing " in the #include ?
>
>
> On 1/16/12, Pranesh Vadhirajan<va...@teralogics.com>  wrote:
>> Hello,
>>
>> I'm very new to developing Input Filters with Apache. I have written an
>> input filter to read the request body content and print it to my error
> log.
>> I have two print statements in my filtering function (one to let me know
>> that my filter code has been called and the other to print the request
>> content).  Yet, I'm not seeing anything happening (nothing is getting
>> printed to my log).  I have tried different things but I'm not able to
> make
>> anything work, so I've attached the code below hoping to get some insight
> on
>> why my filter doesn't seem to work.  I am building the module using apxs
> and
>> I have a LoadModule directive in the httpd.conf file to load my module.
> I'm
>> new to the filter API and I'm totally out of ideas at this point as to why
>> this is not working.
>>
>>
>> #include "httpd.h"
>> #include "http_core.h"
>> #include "http_protocol.h"
>> #include "http_config.h"
>> #include "http_protocol.h"
>> #include "http_main.h"
>> #include "http_log.h"
>> #include "http_request.h"
>> #include "util_script.h"
>> #include "http_connection.h"
>> #include<stdlib.h>
>> #include<time.h>
>> #include<sys/time.h>
>> #include<sys/types.h>
>> #include<regex.h>
>> #include<stdio.h>
>> #include "uvds_metrics_sessions.h"
>>
>> #include "apr.h"
>> #include "apr_lib.h"
>> #include "apr_general.h"
>> #include "apr_strings.h"
>> #include "ap_config.h"
>> #include "apr_buckets.h"
>> #include "util_filter.h
>>
>> static apr_status_t req_body_filter_in(ap_filter_t *f, apr_bucket_brigade
>> *b, ap_input_mode_t mode, apr_size_t *readbytes)
>> {
>>      const char *str;
>>      int length;
>>      apr_bucket *e;
>>
>>      fprintf(stderr,"reached this point\n");
>>
>>      ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);
>>
>>      e = APR_BRIGADE_FIRST(b);
>>
>>      if (e->type == NULL) {
>>      return APR_SUCCESS;
>>      }
>>
>>      apr_bucket_read(e,&str, (apr_size_t*)&length, APR_NONBLOCK_READ);
>>      fprintf(stderr,"req body: %s\n",str);
>>
>>      return APR_SUCCESS;
>> }
>>
>> static void my_register_hooks (apr_pool_t *p) {
>>          ap_hook_insert_filter(req_body_filter_in, NULL , NULL ,
>> APR_HOOK_MIDDLE) ;
>>          //ap_register_input_filter("get_request_body" , req_body_filter_in
> ,
>> NULL , AP_FTYPE_RESOURCE) ;
>>
>> }
>>
>> module AP_MODULE_DECLARE_DATA my_module =
>> {
>>          STANDARD20_MODULE_STUFF,
>>          NULL,                                           /* Per-Directory
>> Configuration */
>>          NULL,                                           /* Directory
> Config
>> Merger */
>>          NULL,                                           /* Per-Server
>> Configuration */
>>          NULL,                                           /* Server Config
>> Merger */
>>          NULL,                                           /* Command Table
>> (Directives) */
>>          my_register_hooks     				  /*
> Registering
>> Hooks */
>>
>> };
>>
>>

RE: Input Filters -- not seeing anything

Posted by Pranesh Vadhirajan <va...@teralogics.com>.
I believe it's a header that is included in the Apache build, so I would
think it doesn't have that issue.

Thanks,
Pranesh

-----Original Message-----
From: Arturo 'Buanzo' Busleiman [mailto:buanzo@buanzo.com.ar] 
Sent: Monday, January 16, 2012 11:18 AM
To: modules-dev@httpd.apache.org
Subject: Re: Input Filters -- not seeing anything

Is util_filter.h lacking a closing " in the #include ?


On 1/16/12, Pranesh Vadhirajan <va...@teralogics.com> wrote:
> Hello,
>
> I'm very new to developing Input Filters with Apache. I have written an
> input filter to read the request body content and print it to my error
log.
> I have two print statements in my filtering function (one to let me know
> that my filter code has been called and the other to print the request
> content).  Yet, I'm not seeing anything happening (nothing is getting
> printed to my log).  I have tried different things but I'm not able to
make
> anything work, so I've attached the code below hoping to get some insight
on
> why my filter doesn't seem to work.  I am building the module using apxs
and
> I have a LoadModule directive in the httpd.conf file to load my module.
I'm
> new to the filter API and I'm totally out of ideas at this point as to why
> this is not working.
>
>
> #include "httpd.h"
> #include "http_core.h"
> #include "http_protocol.h"
> #include "http_config.h"
> #include "http_protocol.h"
> #include "http_main.h"
> #include "http_log.h"
> #include "http_request.h"
> #include "util_script.h"
> #include "http_connection.h"
> #include <stdlib.h>
> #include <time.h>
> #include <sys/time.h>
> #include <sys/types.h>
> #include <regex.h>
> #include <stdio.h>
> #include "uvds_metrics_sessions.h"
>
> #include "apr.h"
> #include "apr_lib.h"
> #include "apr_general.h"
> #include "apr_strings.h"
> #include "ap_config.h"
> #include "apr_buckets.h"
> #include "util_filter.h
>
> static apr_status_t req_body_filter_in(ap_filter_t *f, apr_bucket_brigade
> *b, ap_input_mode_t mode, apr_size_t *readbytes)
> {
>     const char *str;
>     int length;
>     apr_bucket *e;
>
>     fprintf(stderr,"reached this point\n");
>
>     ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);
>
>     e = APR_BRIGADE_FIRST(b);
>
>     if (e->type == NULL) {
>     return APR_SUCCESS;
>     }
>
>     apr_bucket_read(e, &str, (apr_size_t*)&length, APR_NONBLOCK_READ);
>     fprintf(stderr,"req body: %s\n",str);
>
>     return APR_SUCCESS;
> }
>
> static void my_register_hooks (apr_pool_t *p) {
>         ap_hook_insert_filter(req_body_filter_in, NULL , NULL ,
> APR_HOOK_MIDDLE) ;
>         //ap_register_input_filter("get_request_body" , req_body_filter_in
,
> NULL , AP_FTYPE_RESOURCE) ;
>
> }
>
> module AP_MODULE_DECLARE_DATA my_module =
> {
>         STANDARD20_MODULE_STUFF,
>         NULL,                                           /* Per-Directory
> Configuration */
>         NULL,                                           /* Directory
Config
> Merger */
>         NULL,                                           /* Per-Server
> Configuration */
>         NULL,                                           /* Server Config
> Merger */
>         NULL,                                           /* Command Table
> (Directives) */
>         my_register_hooks     				  /*
Registering
> Hooks */
>
> };
>
>

-- 
Sent from my mobile device

Re: Input Filters -- not seeing anything

Posted by Arturo 'Buanzo' Busleiman <bu...@buanzo.com.ar>.
Is util_filter.h lacking a closing " in the #include ?


On 1/16/12, Pranesh Vadhirajan <va...@teralogics.com> wrote:
> Hello,
>
> I'm very new to developing Input Filters with Apache. I have written an
> input filter to read the request body content and print it to my error log.
> I have two print statements in my filtering function (one to let me know
> that my filter code has been called and the other to print the request
> content).  Yet, I'm not seeing anything happening (nothing is getting
> printed to my log).  I have tried different things but I'm not able to make
> anything work, so I've attached the code below hoping to get some insight on
> why my filter doesn't seem to work.  I am building the module using apxs and
> I have a LoadModule directive in the httpd.conf file to load my module.  I'm
> new to the filter API and I'm totally out of ideas at this point as to why
> this is not working.
>
>
> #include "httpd.h"
> #include "http_core.h"
> #include "http_protocol.h"
> #include "http_config.h"
> #include "http_protocol.h"
> #include "http_main.h"
> #include "http_log.h"
> #include "http_request.h"
> #include "util_script.h"
> #include "http_connection.h"
> #include <stdlib.h>
> #include <time.h>
> #include <sys/time.h>
> #include <sys/types.h>
> #include <regex.h>
> #include <stdio.h>
> #include "uvds_metrics_sessions.h"
>
> #include "apr.h"
> #include "apr_lib.h"
> #include "apr_general.h"
> #include "apr_strings.h"
> #include "ap_config.h"
> #include "apr_buckets.h"
> #include "util_filter.h
>
> static apr_status_t req_body_filter_in(ap_filter_t *f, apr_bucket_brigade
> *b, ap_input_mode_t mode, apr_size_t *readbytes)
> {
>     const char *str;
>     int length;
>     apr_bucket *e;
>
>     fprintf(stderr,"reached this point\n");
>
>     ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);
>
>     e = APR_BRIGADE_FIRST(b);
>
>     if (e->type == NULL) {
>     return APR_SUCCESS;
>     }
>
>     apr_bucket_read(e, &str, (apr_size_t*)&length, APR_NONBLOCK_READ);
>     fprintf(stderr,"req body: %s\n",str);
>
>     return APR_SUCCESS;
> }
>
> static void my_register_hooks (apr_pool_t *p) {
>         ap_hook_insert_filter(req_body_filter_in, NULL , NULL ,
> APR_HOOK_MIDDLE) ;
>         //ap_register_input_filter("get_request_body" , req_body_filter_in ,
> NULL , AP_FTYPE_RESOURCE) ;
>
> }
>
> module AP_MODULE_DECLARE_DATA my_module =
> {
>         STANDARD20_MODULE_STUFF,
>         NULL,                                           /* Per-Directory
> Configuration */
>         NULL,                                           /* Directory Config
> Merger */
>         NULL,                                           /* Per-Server
> Configuration */
>         NULL,                                           /* Server Config
> Merger */
>         NULL,                                           /* Command Table
> (Directives) */
>         my_register_hooks     				  /* Registering
> Hooks */
>
> };
>
>

-- 
Sent from my mobile device

Input Filters -- not seeing anything

Posted by Pranesh Vadhirajan <va...@teralogics.com>.
Hello,

I'm very new to developing Input Filters with Apache. I have written an
input filter to read the request body content and print it to my error log.
I have two print statements in my filtering function (one to let me know
that my filter code has been called and the other to print the request
content).  Yet, I'm not seeing anything happening (nothing is getting
printed to my log).  I have tried different things but I'm not able to make
anything work, so I've attached the code below hoping to get some insight on
why my filter doesn't seem to work.  I am building the module using apxs and
I have a LoadModule directive in the httpd.conf file to load my module.  I'm
new to the filter API and I'm totally out of ideas at this point as to why
this is not working.


#include "httpd.h" 
#include "http_core.h"
#include "http_protocol.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_main.h"
#include "http_log.h"
#include "http_request.h"
#include "util_script.h"
#include "http_connection.h"
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include "uvds_metrics_sessions.h"

#include "apr.h"
#include "apr_lib.h"
#include "apr_general.h"
#include "apr_strings.h"
#include "ap_config.h"
#include "apr_buckets.h"
#include "util_filter.h

static apr_status_t req_body_filter_in(ap_filter_t *f, apr_bucket_brigade
*b, ap_input_mode_t mode, apr_size_t *readbytes)
{
    const char *str;
    int length;
    apr_bucket *e;

    fprintf(stderr,"reached this point\n");

    ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);

    e = APR_BRIGADE_FIRST(b);

    if (e->type == NULL) {
    return APR_SUCCESS;
    }

    apr_bucket_read(e, &str, (apr_size_t*)&length, APR_NONBLOCK_READ);
    fprintf(stderr,"req body: %s\n",str);

    return APR_SUCCESS;
}

static void my_register_hooks (apr_pool_t *p) {
        ap_hook_insert_filter(req_body_filter_in, NULL , NULL ,
APR_HOOK_MIDDLE) ;
        //ap_register_input_filter("get_request_body" , req_body_filter_in ,
NULL , AP_FTYPE_RESOURCE) ;

}

module AP_MODULE_DECLARE_DATA my_module =
{
        STANDARD20_MODULE_STUFF,
        NULL,                                           /* Per-Directory
Configuration */
        NULL,                                           /* Directory Config
Merger */
        NULL,                                           /* Per-Server
Configuration */
        NULL,                                           /* Server Config
Merger */
        NULL,                                           /* Command Table
(Directives) */
        my_register_hooks     				  /* Registering
Hooks */

};


Re: Some fundamental questions about output filters

Posted by Joshua Marantz <jm...@google.com>.
Every new module-implementor should look at this example:

http://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/examples/mod_example_hooks.c

Although I'm not particularly proud of how complicated our apache-gasket
has gotten -- it could use a refactor or 5, this is mod_pagespeed's output
filter registration call:


http://code.google.com/p/modpagespeed/source/browse/trunk/src/net/instaweb/apache/mod_instaweb.cc#753

Also see instaweb_out_filter and process_bucket in the same file.

-Josh

On Fri, Jan 13, 2012 at 2:10 PM, Pranesh Vadhirajan <
vadhirajan@teralogics.com> wrote:

> Hello,****
>
> I’ve been trying to use output filters in my code in order to read the
> response that is being sent from Apache to a web client.  ****
>
> ** **
>
> I believe this would be a proper use for an output filter, please correct
> me if I’m wrong.  I am attempting to read the content of the response from
> within a certain module, and I would like to know how to access the
> contents of the output filter etc.  ****
>
> ** **
>
> I’ve been looking into how output filters work, but I’m not sure I
> understand the concept.  ****
>
> ** **
>
> So here are my basic questions on how to write that code:****
>
> ** **
>
> Can I register my output filter within the registration hooks of my module
> code?  For example I did the following and I am getting various errors, and
> I am wondering if there’s a proper way to do it.  The goal is to be able to
> access the contents of the response from within my module and to print it
> to my error log (or anywhere else), but I am not sure how to do this. : **
> **
>
> ** **
>
> static apr_status_t apcontent_filter_in(ap_filter_t *f, apr_bucket_brigade
> *b, ap_input_mode_t mode, apr_size_t *readbytes)****
>
> {****
>
>     const char *str;****
>
>     int length;****
>
>     apr_bucket *e;****
>
> ** **
>
>         ap_get_brigade(f->next, b, mode, APR_BLOCK_READ,1);****
>
> ** **
>
>         e = APR_BRIGADE_FIRST(b);****
>
> ** **
>
>         if (e->type == NULL) {****
>
>                 return APR_SUCCESS;****
>
>         }****
>
> ** **
>
>         apr_bucket_read(e, &str, &length, 1);****
>
>         fprintf(stderr,"req body: %s\n",str);****
>
> ** **
>
>     return APR_SUCCESS;****
>
> }****
>
> ** **
>
> static int my_handler(request_rec* r) {****
>
> ** **
>
>         int is_ip = 0, fqdn_id, ap_id, nid_sl = 0, nid_fl = 0;****
>
>         char time_buffer[30]; ****
>
>         char* remote_address;****
>
>         char* cookie;****
>
>         char* session_id = NULL;****
>
> ** **
>
>         apr_bucket_brigade* request_body_brigade;****
>
>         ap_filter_t *f;****
>
>         apr_size_t readbytes=(apr_size_t)r->sent_bodyct;****
>
>
> apcontent_filter_in(f,request_body_brigade,AP_MODE_READBYTES,&readbytes);*
> ***
>
> ** **
>
>         return DECLINED;****
>
> ** **
>
> }****
>
> ** **
>
> static void my_register_hooks (apr_pool_t *p) {****
>
>         ap_hook_handler(my_handler, NULL, NULL, APR_HOOK_MIDDLE);****
>
>         ap_register_input_filter("get_request_body",
> apcontent_filter_in,NULL, AP_FTYPE_RESOURCE) ;****
>
> }****
>
> ** **
>
> module AP_MODULE_DECLARE_DATA my_module = ****
>
> {****
>
>         STANDARD20_MODULE_STUFF,^M****
>
>         NULL,                                           /* Per-Directory
> Configuration */****
>
>         NULL,                                           /* Directory
> Config Merger */****
>
>         NULL,                                           /* Per-Server
> Configuration */****
>
>         NULL,                                           /* Server Config
> Merger */****
>
>         NULL,                                           /* Command Table
> (Directives) */****
>
>         my_register_hooks     /* Registering Hooks */****
>
> };****
>
> ** **
>
> If this is not the proper way to read from an output filter could someone
> please explain the proper way?****
>
> ** **
>
> Thanks,
> Pranesh Vadhirajan****
>
> ** **
>
> ** **
>