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 Devender Reddy <mu...@gmail.com> on 2007/02/09 12:47:44 UTC

How to retrieve the posted variables using the apache module...

Hi,

I am writing an apache module in C and I am facing some problem while
retreiveing the POST elements data.

For example,  I am giving one form, so that the user can fill his
name, and upload a file.
Following is the HTMl for the same.

<form method="post" enctype="multipart/form-data"
action="http://builder8.mail.corp.yahoo.com:8888/hey/there">
<input type="text" name="uname" /><br>
<br>File : <input type="file" name="uploadfile" />
<input type="hidden" name="hide" value="hidden value" />
<input type="submit" value="submit" name="submit" />
</form>

Now I have to write one apache module in C, such that, I can get the
data of name, upload file name and the hidden variable.

I am assuming that these values are stored in the request_rec
structure. I dumped all the elements in this structure, but I couldn't
find the information. So it would be great, if you can help in this.

I know this is a pretty simple question, but this one seems to be a
tough for me as I am new to this area.


Thanks in advance for doing this favour.

-- 
Thanks & Regards
Dev

Re: How to retrieve the posted variables using the apache module...

Posted by Issac Goldstand <ma...@beamartyr.net>.
Devender,
  You might want to take a look at libapreq at 
http://httpd.apache.org/apreq/  It contains an API for getting to the 
POST variables, including uploaded files.

  Issac

Joe Lewis wrote:
> Devender Reddy wrote:
>> Hi,
>>
>> I am writing an apache module in C and I am facing some problem while
>> retreiveing the POST elements data.
> [snip]
>>
>> Now I have to write one apache module in C, such that, I can get the
>> data of name, upload file name and the hidden variable.
> Are you sure you need a module and not a perl or php script?  If you 
> NEED an extension for apache, read on.
>>
>> I am assuming that these values are stored in the request_rec
>> structure. I dumped all the elements in this structure, but I couldn't
>> find the information. So it would be great, if you can help in this.
> Incoming HTTP headers are stored in the request_rec->headers_in 
> apr_table.  You can write a filter that grabs POST data before any 
> other module gets it if you needed to alter the data.  If you don't 
> need to alter the data, you probably don't want an apache extension, 
> just a standard CGI program.
>>
>> I know this is a pretty simple question, but this one seems to be a
>> tough for me as I am new to this area.
> We'll help as far as we are allowed to by list etiquette.
>
> Joe

Re: How to retrieve the posted variables using the apache module...

Posted by Steve <va...@earthlink.net>.
You should take a look at ap_setup_client_block, ap_should_client_block and
ap_get_client_block.

Here is a small code snippet that should get you started...

 static int BUFSIZE = 65536;
        
 if ( ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK )
 {
     if ( ap_should_client_block(r) )
     {
         apr_size_t bytes ;
         char* myBuffer = (char *)apr_pcalloc(r->pool,
sizeof(char)*BUFSIZE);
         do
         {
               bytes = ap_get_client_block(r, myBuffer, BUFSIZE) ;
         }
         while ( bytes );
         ap_rputs( "Post Data: <font color=blue>" , r );
         ap_rputs(myBuffer, r);
         ap_rputs( "</font>\r<BR>\r" , r );
     }
  }



on 12/2/07 12:36 AM, Devender Reddy at mudhiganti123@gmail.com wrote:

> Hi Joe,
> 
>   Thanks  for your reply. Actually, I dumped all the data that is
> there in request_rec->header_in datastructure, but still I couldn't
> find the Posted variables in that datastructure.

[Snip]




Re: How to retrieve the posted variables using the apache module...

Posted by Issac Goldstand <ma...@beamartyr.net>.
Devender,
  Look at apreq (http://httpd.apache.org/apreq/)  This module is
designed to do exactly what you want, and will work with both Apache 1.3
and Apache 2.x

  Issac

Devender Reddy wrote:
> Thanks for the information. Right now I'm working on Apache 1.3 version.
> Kindly let me know whether filters are supported for this version. If
> supported, it would be great if you can share me some links to those.
> 
> What I'm trying to do is....
> 
> Basically, I want to filter the data (POST data + File contents from
> upload)
> 
>   - send the file contents to different server (server1)
>   - get the response back from the server
>   - then add the response (from server1) to the already existing POST
>   data
>   - then send that to some other server (server2).
> 
> 
> If filters are not supported, then kindly let me know how to go about this
> scenario.
> 
> Thanks & Regards,
> Dev.
> 
> On 2/12/07, Joe Lewis <jo...@joe-lewis.com> wrote:
>>
>> Dev;
>>
>> You really want an filter, not a handler.  By the time you get to a
>> handler, the input "POST" information has been read.  As suggested, use
>> the apreq API's to grab it in a handler, or if you have to modify or
>> catch it before any other modules, put it into an input filter.
>>
>> Joe
>>
>> Devender Reddy wrote:
>> > Hi Joe,
>> >
>> >  Thanks  for your reply. Actually, I dumped all the data that is
>> > there in request_rec->header_in datastructure, but still I couldn't
>> > find the Posted variables in that datastructure.
>> >
>> > FYI, Here I am putting the HTML file, where the user enters his name
>> > and will upload one file.
>> >
>> > <form method="post" enctype="multipart/form-data"
>> > action="http://www.example.com/hey/there">
>> > <input type="text" name="uname" /><br>
>> > <br>File : <input type="file" name="uploadfile" />
>> > <input type="hidden" name="hide" value="hidden value" />
>> > <input type="submit" value="submit" name="submit" />
>> > </form>
>> >
>> > and Here I had written a module, to retrieve the name, upload file
>> > name and hidden values.
>> >
>> > int iterate_func(void *req, const char *key, const char *value)
>> > {
>> >    int stat;
>> >    char *line;
>> >    request_rec *r = (request_rec *)req;
>> >    if (key == NULL || value == NULL || value[0] == '\0')
>> >        return 1;
>> >
>> >    line = ap_psprintf(r->pool, "%s => %s\n", key, value);
>> >    stat = ap_rputs(line, r);
>> >
>> >    return 1;
>> > }
>> >
>> >
>> > static int hello_handler (request_rec * r)
>> > {
>> >    .......
>> >
>> >    ap_send_http_header (r);
>> >   ap_table_do(iterate_func, r, r->headers_in, NULL);
>> >
>> >  /* This is the another way of trying to get the header_in contents. */
>> >
>> >    table *in = r->headers_in;
>> >  array_header *arr = ap_table_elts (in);
>> >
>> >  char **list = (char **) arr->elts;
>> >  int i;
>> >
>> >  ap_rprintf (r, "<H1>Header in Elements are : </H1>\n");
>> >  for (i = 0; i < arr->nelts; i++)
>> >    {
>> >      ap_rprintf (r, "item %d -> %s;  \n", i, list[i]);
>> >    }
>> >
>> >  table *out = r->headers_out;
>> >  arr = ap_table_elts(out);
>> >
>> >  list = (char **) arr->elts;
>> >
>> >  ap_rprintf (r, "<H1>Header out Elements are : </H1>\n");
>> >  for (i = 0; i < arr->nelts; i++)
>> >    {
>> >      ap_rprintf (r, "item %d -> %s;  \n", i, list[i]);
>> >    }
>> >
>> >  return OK;
>> > }
>> >
>> > /* Make the name of the content handler known to Apache */
>> > static handler_rec hello_handlers[] = {
>> >  {"hello-handler", hello_handler},
>> >  {NULL}
>> > };
>> >
>> > /* Tell Apache what phases of the transaction we handle */
>> > module MODULE_VAR_EXPORT hello_module = {
>> >  STANDARD_MODULE_STUFF,
>> >  NULL,                         /* module initializer                 */
>> >  NULL,                         /* per-directory config creator       */
>> >  NULL,                         /* dir config merger                  */
>> >  NULL,                         /* server config creator              */
>> >  NULL,                         /* server config merger               */
>> >  NULL,                         /* command table                      */
>> >  hello_handlers,               /* [9]  content handlers              */
>> >  NULL,                         /* [2]  URI-to-filename translation   */
>> >  NULL,                         /* [5]  check/validate user_id        */
>> >  NULL,                         /* [6]  check user_id is valid *here* */
>> >  NULL,                         /* [4]  check access by host address  */
>> >  NULL,                         /* [7]  MIME type checker/setter      */
>> >  NULL,                         /* [8]  fixups                        */
>> >  NULL,                         /* [10] logger                        */
>> >  NULL,                         /* [3]  header parser                 */
>> >  NULL,                         /* process initialization             */
>> >  NULL,                         /* process exit/cleanup               */
>> >  NULL                          /* [1]  post read_request handling    */
>> > };
>> >
>> >
>> > So please let me know, if I did anything wrong.
>> > Thanks in advance for doing this favour.
>> >
>> > Dev
>> >
>> > On 2/9/07, Joe Lewis <jo...@joe-lewis.com> wrote:
>> >> Devender Reddy wrote:
>> >> > Hi,
>> >> >
>> >> > I am writing an apache module in C and I am facing some problem
>> while
>> >> > retreiveing the POST elements data.
>> >> [snip]
>> >> >
>> >> > Now I have to write one apache module in C, such that, I can get the
>> >> > data of name, upload file name and the hidden variable.
>> >> Are you sure you need a module and not a perl or php script?  If you
>> >> NEED an extension for apache, read on.
>> >> >
>> >> > I am assuming that these values are stored in the request_rec
>> >> > structure. I dumped all the elements in this structure, but I
>> couldn't
>> >> > find the information. So it would be great, if you can help in this.
>> >> Incoming HTTP headers are stored in the request_rec->headers_in
>> >> apr_table.  You can write a filter that grabs POST data before any
>> other
>> >> module gets it if you needed to alter the data.  If you don't need to
>> >> alter the data, you probably don't want an apache extension, just a
>> >> standard CGI program.
>> >> >
>> >> > I know this is a pretty simple question, but this one seems to be a
>> >> > tough for me as I am new to this area.
>> >> We'll help as far as we are allowed to by list etiquette.
>> >>
>> >> Joe
>> >> --
>> >> Joseph Lewis <http://sharktooth.org/>
>> >> "Divide the fire, and you will sooner put it out." - Publius Syrus
>> >>
>> >
>> >
>>
>>
>> -- 
>> Joseph Lewis <http://sharktooth.org/>
>> "Divide the fire, and you will sooner put it out." - Publius Syrus
>>
> 
> 
> 

Re: How to retrieve the posted variables using the apache module...

Posted by Devender Reddy <mu...@gmail.com>.
Thanks for the information. Right now I'm working on Apache 1.3 version.
Kindly let me know whether filters are supported for this version. If
supported, it would be great if you can share me some links to those.

What I'm trying to do is....

Basically, I want to filter the data (POST data + File contents from upload)

   - send the file contents to different server (server1)
   - get the response back from the server
   - then add the response (from server1) to the already existing POST
   data
   - then send that to some other server (server2).


If filters are not supported, then kindly let me know how to go about this
scenario.

Thanks & Regards,
Dev.

On 2/12/07, Joe Lewis <jo...@joe-lewis.com> wrote:
>
> Dev;
>
> You really want an filter, not a handler.  By the time you get to a
> handler, the input "POST" information has been read.  As suggested, use
> the apreq API's to grab it in a handler, or if you have to modify or
> catch it before any other modules, put it into an input filter.
>
> Joe
>
> Devender Reddy wrote:
> > Hi Joe,
> >
> >  Thanks  for your reply. Actually, I dumped all the data that is
> > there in request_rec->header_in datastructure, but still I couldn't
> > find the Posted variables in that datastructure.
> >
> > FYI, Here I am putting the HTML file, where the user enters his name
> > and will upload one file.
> >
> > <form method="post" enctype="multipart/form-data"
> > action="http://www.example.com/hey/there">
> > <input type="text" name="uname" /><br>
> > <br>File : <input type="file" name="uploadfile" />
> > <input type="hidden" name="hide" value="hidden value" />
> > <input type="submit" value="submit" name="submit" />
> > </form>
> >
> > and Here I had written a module, to retrieve the name, upload file
> > name and hidden values.
> >
> > int iterate_func(void *req, const char *key, const char *value)
> > {
> >    int stat;
> >    char *line;
> >    request_rec *r = (request_rec *)req;
> >    if (key == NULL || value == NULL || value[0] == '\0')
> >        return 1;
> >
> >    line = ap_psprintf(r->pool, "%s => %s\n", key, value);
> >    stat = ap_rputs(line, r);
> >
> >    return 1;
> > }
> >
> >
> > static int hello_handler (request_rec * r)
> > {
> >    .......
> >
> >    ap_send_http_header (r);
> >   ap_table_do(iterate_func, r, r->headers_in, NULL);
> >
> >  /* This is the another way of trying to get the header_in contents. */
> >
> >    table *in = r->headers_in;
> >  array_header *arr = ap_table_elts (in);
> >
> >  char **list = (char **) arr->elts;
> >  int i;
> >
> >  ap_rprintf (r, "<H1>Header in Elements are : </H1>\n");
> >  for (i = 0; i < arr->nelts; i++)
> >    {
> >      ap_rprintf (r, "item %d -> %s;  \n", i, list[i]);
> >    }
> >
> >  table *out = r->headers_out;
> >  arr = ap_table_elts(out);
> >
> >  list = (char **) arr->elts;
> >
> >  ap_rprintf (r, "<H1>Header out Elements are : </H1>\n");
> >  for (i = 0; i < arr->nelts; i++)
> >    {
> >      ap_rprintf (r, "item %d -> %s;  \n", i, list[i]);
> >    }
> >
> >  return OK;
> > }
> >
> > /* Make the name of the content handler known to Apache */
> > static handler_rec hello_handlers[] = {
> >  {"hello-handler", hello_handler},
> >  {NULL}
> > };
> >
> > /* Tell Apache what phases of the transaction we handle */
> > module MODULE_VAR_EXPORT hello_module = {
> >  STANDARD_MODULE_STUFF,
> >  NULL,                         /* module initializer                 */
> >  NULL,                         /* per-directory config creator       */
> >  NULL,                         /* dir config merger                  */
> >  NULL,                         /* server config creator              */
> >  NULL,                         /* server config merger               */
> >  NULL,                         /* command table                      */
> >  hello_handlers,               /* [9]  content handlers              */
> >  NULL,                         /* [2]  URI-to-filename translation   */
> >  NULL,                         /* [5]  check/validate user_id        */
> >  NULL,                         /* [6]  check user_id is valid *here* */
> >  NULL,                         /* [4]  check access by host address  */
> >  NULL,                         /* [7]  MIME type checker/setter      */
> >  NULL,                         /* [8]  fixups                        */
> >  NULL,                         /* [10] logger                        */
> >  NULL,                         /* [3]  header parser                 */
> >  NULL,                         /* process initialization             */
> >  NULL,                         /* process exit/cleanup               */
> >  NULL                          /* [1]  post read_request handling    */
> > };
> >
> >
> > So please let me know, if I did anything wrong.
> > Thanks in advance for doing this favour.
> >
> > Dev
> >
> > On 2/9/07, Joe Lewis <jo...@joe-lewis.com> wrote:
> >> Devender Reddy wrote:
> >> > Hi,
> >> >
> >> > I am writing an apache module in C and I am facing some problem while
> >> > retreiveing the POST elements data.
> >> [snip]
> >> >
> >> > Now I have to write one apache module in C, such that, I can get the
> >> > data of name, upload file name and the hidden variable.
> >> Are you sure you need a module and not a perl or php script?  If you
> >> NEED an extension for apache, read on.
> >> >
> >> > I am assuming that these values are stored in the request_rec
> >> > structure. I dumped all the elements in this structure, but I
> couldn't
> >> > find the information. So it would be great, if you can help in this.
> >> Incoming HTTP headers are stored in the request_rec->headers_in
> >> apr_table.  You can write a filter that grabs POST data before any
> other
> >> module gets it if you needed to alter the data.  If you don't need to
> >> alter the data, you probably don't want an apache extension, just a
> >> standard CGI program.
> >> >
> >> > I know this is a pretty simple question, but this one seems to be a
> >> > tough for me as I am new to this area.
> >> We'll help as far as we are allowed to by list etiquette.
> >>
> >> Joe
> >> --
> >> Joseph Lewis <http://sharktooth.org/>
> >> "Divide the fire, and you will sooner put it out." - Publius Syrus
> >>
> >
> >
>
>
> --
> Joseph Lewis <http://sharktooth.org/>
> "Divide the fire, and you will sooner put it out." - Publius Syrus
>



-- 
Thanks & Regards
Dev

Re: How to retrieve the posted variables using the apache module...

Posted by Devender Reddy <mu...@gmail.com>.
Hi Joe,

  Thanks  for your reply. Actually, I dumped all the data that is
there in request_rec->header_in datastructure, but still I couldn't
find the Posted variables in that datastructure.

FYI, Here I am putting the HTML file, where the user enters his name
and will upload one file.

<form method="post" enctype="multipart/form-data"
action="http://www.example.com/hey/there">
<input type="text" name="uname" /><br>
<br>File : <input type="file" name="uploadfile" />
<input type="hidden" name="hide" value="hidden value" />
<input type="submit" value="submit" name="submit" />
</form>

and Here I had written a module, to retrieve the name, upload file
name and hidden values.

int iterate_func(void *req, const char *key, const char *value)
{
    int stat;
    char *line;
    request_rec *r = (request_rec *)req;
    if (key == NULL || value == NULL || value[0] == '\0')
        return 1;

    line = ap_psprintf(r->pool, "%s => %s\n", key, value);
    stat = ap_rputs(line, r);

    return 1;
}


static int hello_handler (request_rec * r)
{
    .......

    ap_send_http_header (r);
   ap_table_do(iterate_func, r, r->headers_in, NULL);

  /* This is the another way of trying to get the header_in contents. */

    table *in = r->headers_in;
  array_header *arr = ap_table_elts (in);

  char **list = (char **) arr->elts;
  int i;

  ap_rprintf (r, "<H1>Header in Elements are : </H1>\n");
  for (i = 0; i < arr->nelts; i++)
    {
      ap_rprintf (r, "item %d -> %s;  \n", i, list[i]);
    }

  table *out = r->headers_out;
  arr = ap_table_elts(out);

  list = (char **) arr->elts;

  ap_rprintf (r, "<H1>Header out Elements are : </H1>\n");
  for (i = 0; i < arr->nelts; i++)
    {
      ap_rprintf (r, "item %d -> %s;  \n", i, list[i]);
    }

  return OK;
}

/* Make the name of the content handler known to Apache */
static handler_rec hello_handlers[] = {
  {"hello-handler", hello_handler},
  {NULL}
};

/* Tell Apache what phases of the transaction we handle */
module MODULE_VAR_EXPORT hello_module = {
  STANDARD_MODULE_STUFF,
  NULL,                         /* module initializer                 */
  NULL,                         /* per-directory config creator       */
  NULL,                         /* dir config merger                  */
  NULL,                         /* server config creator              */
  NULL,                         /* server config merger               */
  NULL,                         /* command table                      */
  hello_handlers,               /* [9]  content handlers              */
  NULL,                         /* [2]  URI-to-filename translation   */
  NULL,                         /* [5]  check/validate user_id        */
  NULL,                         /* [6]  check user_id is valid *here* */
  NULL,                         /* [4]  check access by host address  */
  NULL,                         /* [7]  MIME type checker/setter      */
  NULL,                         /* [8]  fixups                        */
  NULL,                         /* [10] logger                        */
  NULL,                         /* [3]  header parser                 */
  NULL,                         /* process initialization             */
  NULL,                         /* process exit/cleanup               */
  NULL                          /* [1]  post read_request handling    */
};


So please let me know, if I did anything wrong.
Thanks in advance for doing this favour.

Dev

On 2/9/07, Joe Lewis <jo...@joe-lewis.com> wrote:
> Devender Reddy wrote:
> > Hi,
> >
> > I am writing an apache module in C and I am facing some problem while
> > retreiveing the POST elements data.
> [snip]
> >
> > Now I have to write one apache module in C, such that, I can get the
> > data of name, upload file name and the hidden variable.
> Are you sure you need a module and not a perl or php script?  If you
> NEED an extension for apache, read on.
> >
> > I am assuming that these values are stored in the request_rec
> > structure. I dumped all the elements in this structure, but I couldn't
> > find the information. So it would be great, if you can help in this.
> Incoming HTTP headers are stored in the request_rec->headers_in
> apr_table.  You can write a filter that grabs POST data before any other
> module gets it if you needed to alter the data.  If you don't need to
> alter the data, you probably don't want an apache extension, just a
> standard CGI program.
> >
> > I know this is a pretty simple question, but this one seems to be a
> > tough for me as I am new to this area.
> We'll help as far as we are allowed to by list etiquette.
>
> Joe
> --
> Joseph Lewis <http://sharktooth.org/>
> "Divide the fire, and you will sooner put it out." - Publius Syrus
>


-- 
Thanks & Regards
Dev

Re: How to retrieve the posted variables using the apache module...

Posted by Joe Lewis <jo...@joe-lewis.com>.
Devender Reddy wrote:
> Hi,
>
> I am writing an apache module in C and I am facing some problem while
> retreiveing the POST elements data.
[snip]
>
> Now I have to write one apache module in C, such that, I can get the
> data of name, upload file name and the hidden variable.
Are you sure you need a module and not a perl or php script?  If you 
NEED an extension for apache, read on.
>
> I am assuming that these values are stored in the request_rec
> structure. I dumped all the elements in this structure, but I couldn't
> find the information. So it would be great, if you can help in this.
Incoming HTTP headers are stored in the request_rec->headers_in 
apr_table.  You can write a filter that grabs POST data before any other 
module gets it if you needed to alter the data.  If you don't need to 
alter the data, you probably don't want an apache extension, just a 
standard CGI program.
>
> I know this is a pretty simple question, but this one seems to be a
> tough for me as I am new to this area.
We'll help as far as we are allowed to by list etiquette.

Joe
-- 
Joseph Lewis <http://sharktooth.org/>
"Divide the fire, and you will sooner put it out." - Publius Syrus