httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marian Marinov ...@yuhu.biz>
Subject input filter problem
Date Wed, 01 May 2013 07:23:26 GMT
Hi guys,
I'm developing a module that needs to do the following things:
1. Read POST arguments
2. Read input headers
3. Read Query string arguments

I have decided to implement it by having an input filter for the POST parsing(since this is
the only way to look into 
the POST without consuming it).
And implement the headers and query string parsing in ap_hook_handler().

However I'm unable to make my input filter function to be called before my handler.

Can you please help?

All I see in the logs is this:
[Wed May 01 05:59:47 2013] [error] [client 10.2.0.4] mod_imp handler called
[Wed May 01 05:59:47 2013] [error] [client 10.2.0.4] mod_imp insert filter(0)
[Wed May 01 05:59:47 2013] [error] [client 10.2.0.4] mod_imp POST: user=123
[Wed May 01 05:59:47 2013] [error] [client 10.2.0.4] mod_imp found 'user'
[Wed May 01 05:59:47 2013] [error] [client 10.2.0.4] mod_imp insert filter(1)
[Wed May 01 05:59:47 2013] [error] [client 10.2.0.4] mod_imp insert filter(2)

I know that using connection->base_server->module_config will not work, unfortunately
I'm out of ideas and started 
trying everything I know.

All I want to do is to tell my handler that the input filter function has matched or not.

This is my code:


static int imp_handler(request_rec *r) {
     imp_config *cfg = ap_get_module_config(r->connection->base_server->module_config,
&imp_module);

     // Only handle initial requests
     if ( !ap_is_initial_req(r) )
         return DECLINED;

     if ( cfg->match == 1 || apr_table_get(r->headers_in, "matched") || apr_table_get(r->notes,
"matched") ) {
         ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, MODULE_NAME " MATCH found");
         cfg->match = 0;
     }
     ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, MODULE_NAME " handler called");

     return DECLINED;
}

static apr_status_t imp_filter(ap_filter_t *f, apr_bucket_brigade *bb, ap_input_mode_t mode,
apr_read_type_e block, 
apr_off_t readbytes) {
     apr_bucket *bktIn, *bktOut;
     apr_status_t ret;
     apr_size_t len;
     const char *data;
     imp_ctx *ctx = f->ctx;
     imp_config *cfg = ap_get_module_config(f->c->base_server->module_config, &imp_module);

     if ( ctx == NULL ) {
         ctx = f->ctx = apr_pcalloc(f->r->pool, sizeof(imp_ctx));
         ctx->count = 0;
     }

     ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, f->r, MODULE_NAME " insert
filter(%d)", ctx->count);
     ctx->count++;

     ret = ap_get_brigade(f->next, bb, mode, block, readbytes);
     if (ret != APR_SUCCESS)
         return ret;

     while(!APR_BRIGADE_EMPTY(bb)) {
         bktIn = APR_BRIGADE_FIRST(bb);

         if (APR_BUCKET_IS_EOS(bktIn)) {
             APR_BUCKET_REMOVE(bktIn);
             APR_BRIGADE_INSERT_TAIL(bb, bktIn);
             break;
         }

         ret=apr_bucket_read(bktIn, &data, &len, block);
         if ( ret != APR_SUCCESS )
             return ret;

         ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, f->r, MODULE_NAME " POST:
%s", data);
         if (strstr(data, "user") != NULL) {
             ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, f->r, MODULE_NAME
" found 'user'");
             cfg->match = 1;
             apr_table_setn(f->r->headers_in, "matched", "1");
             apr_table_setn(f->r->notes, "matched", "1");
         }

         // The following two lines reintroduce the parsed data to the brigade
         bktOut = apr_bucket_heap_create(data, len, 0, f->r->connection->bucket_alloc);
         APR_BRIGADE_INSERT_TAIL(bb, bktOut);

         apr_bucket_delete(bktIn);
     }
     return APR_SUCCESS;
}

static void imp_add_filter(request_rec *r) {
     imp_config *cfg = (imp_config *) ap_get_module_config(r->base_server->module_config,
&imp_module);
     cfg->count = 0;
     cfg->match = 0;
     ap_add_input_filter("imp-post-filter", cfg, r, r->connection);
}


static int imp_pre_conn(conn_rec *c, void *csd) {
     imp_config *cfg = ap_get_module_config(c->base_server->module_config, &imp_module);
     if ( cfg == NULL )
         cfg = apr_pcalloc(c->pool, sizeof(imp_config));
     cfg->count = 0;
     cfg->match = 0;
     return OK;
}

static void imp_register_hooks(apr_pool_t *p) {
     ap_hook_pre_connection(imp_pre_conn, NULL, NULL, APR_HOOK_MIDDLE);
     ap_hook_insert_filter(imp_add_filter, NULL, NULL, APR_HOOK_MIDDLE);
     ap_register_input_filter("imp-post-filter", imp_filter, NULL, AP_FTYPE_RESOURSE);
     ap_hook_header_parser(imp_handler, NULL, NULL, APR_HOOK_MIDDLE);
}


Thank you very much!
Marian

Mime
View raw message