0

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. enter image description here 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                      */
};
weefwefwqg3
  • 961
  • 10
  • 23
  • 1
    Please check the return of `fopen`, you might not have writing rights. – deamentiaemundi Nov 23 '16 at 20:53
  • Hello, I already pasted my code too. The picture is just for the purpose of easier to see the code. – weefwefwqg3 Nov 23 '16 at 20:53
  • 1
    yepp, I'm too slow, I know ;-) But insisting on pure text is intentional: you can easily c&p it to compile it and a picture is of not much worth for the programmers here who cannot see it. – deamentiaemundi Nov 23 '16 at 20:56
  • According to this tutorial: http://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm My fopen() is supposed to created a file "params_text.file" if it's not there yet, and append it if the file is already there. – weefwefwqg3 Nov 23 '16 at 20:56
  • 1
    Yes, but do you have the necessary rights to make it and write to it? – deamentiaemundi Nov 23 '16 at 20:57
  • Oh, actually it's a good point about the permission. Do you have any idea how can I set the permission of the flight? Or at least to Apache so that it can write down the file for me? – weefwefwqg3 Nov 23 '16 at 20:59
  • 1
    @匿名柴棍 Use this to check if the user has permission to read/write to the directory - https://stackoverflow.com/questions/7087958/file-stat-vs-access-to-check-permissions-on-a-directory – Chirality Nov 23 '16 at 21:51
  • 1
    When writing Apache modules, you should use APR (Apache Portable Runtime) functions instead of local functions... so instead of fopen() you should use apr_file_open(), and fprintf() use apr_file_printf(). Look here for APR file routines: http://apr.apache.org/docs/apr/1.5/group__apr__file__io.html#gabda14cbf242fb4fe99055434213e5446 – TonyB Nov 23 '16 at 22:24
  • @tonyB, yep. That was what I am asking, I am don't know how to use APR to write the code to archive the same thing that I am doing with fopen(). Could you please tell me how can I re-write that 4 lines of code using APR instead of fopen(). I am stuck with the pointers arguments of those APR functions, because the APR website does not show any sample usage code for those function. – weefwefwqg3 Nov 23 '16 at 22:30
  • 1
    You should look at the documentatilon (which I pointed you to above) or some of the sources in Apache's HTTP server itself (i.e. download apache's source and search for uses of apr_file_open() and others....) The newest stable Apache source is located at: http://httpd.apache.org/download.cgi ) ADDITIONIALLY, your handler should return OK if it is handling the request and DECLINED if not... your are always returning DECLINED... Here's a link to writing a handler: https://httpd.apache.org/docs/2.4/developer/modguide.html If you continue having problems, then post an update. – TonyB Nov 23 '16 at 22:54
  • @tonyB, thank you so much for your detail comment. I will download the doc to study more. For the DECLINED and OK, I thought that because I put APR_HOOK_REALLY_FIRST, my module will always process the HTTPrequest first, and by DECLINED it is then released to be processed by other available modules. Isn't it? Please correct me if I am wrong. Regards. – weefwefwqg3 Nov 23 '16 at 23:31
  • The code you provided was a slight modification of the example from the first link I provided you... You will notice that original version always returns "OK" (other than if it fails the test for the r->handler not being equal to "example-handler", which you commented out). – TonyB Nov 23 '16 at 23:56

0 Answers0