I am a newbie, and I'm writing an Apache module to handle http request. I want to store all of the http requests' parameters and their values to a txt file. And, here is my code, but it didn't succeeded.
Whenever, I tried to put some parameters in the URL, the page crashed. In the error.log of apache, it shows
"Segmentation Fault (core dumped)"
. I am aware that there is API of APR_FILE_() here instead of using standard C library. But I am so dump to understand all of pointers of those functions and the API does not have any sample usage of the apr_file_() functions, thus being stuck with file I/O. So I would be really thankful if someone could help me with the problem.
For your ready reference: I paste my code here to:
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
/* The sample content handler */
static int learning_handler(request_rec *r)
{
/* First off, we need to check if this is a call for the "example-handler" handler.
* If it is, we accept it and do our things, if not, we simply return DECLINED,
* and the server will try somewhere else.
*/
//if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
/* Set the appropriate content type */
ap_set_content_type(r, "text/html");
/* Print out the IP address of the client connecting to us: */
ap_rprintf(r, "<h2>Hello, %s!</h2>", r->useragent_ip);
/* If we were reached through a GET or a POST request, be happy, else sad. */
if ( !strcmp(r->method, "POST") || !strcmp(r->method, "GET") ) {
ap_rputs("You used a GET or a POST method, that makes us happy!<br/>", r);
}
else {
ap_rputs("You did not use POST or GET, that makes us sad :(<br/>", r);
}
/* Lastly, if there was a query string, let's save it file */
if (r->args) {
ap_rprintf(r, "Your query string was: %s", r->args);
FILE * fp;
fp = fopen ("params_file.txt", "w+");
fprintf(fp, "%s",r->args);
fclose(fp);
}
return DECLINED;
}
static void learning_register_hooks(apr_pool_t *p)
{
ap_hook_handler(learning_handler, NULL, NULL, APR_HOOK_REALLY_FIRST);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA learning_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
learning_register_hooks /* register hooks */
};