[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] How to read data coming from a POST request



Hi,

I would like to thank you for this great library. I am using libevent http API to run a simple web server. I started using the http-server.c example but now I am trying to accept post requests. I can receive data and reply back but I would like to read the parameters sent from the user agent. I tried this:

void servlet(struct evhttp_request *req, struct evbuffer *evb) {
    size_t len = evbuffer_get_length(evhttp_request_get_input_buffer(req));
    struct evbuffer *in_evb = evhttp_request_get_input_buffer(req);
    char *data;
    evbuffer_copyout(in_evb, data, len);
    time_t now;
    time(&now);
    evbuffer_add_printf(evb, "<html>\n <head>\n"
   "  <title>%s</title>\n"
   " </head>\n"
   " <body>\n"
   "  <h1>%s</h1>\n"
   " <p>Current time is: %s</p>" 
   " <p>received data size: %d</p></body></html>"
   " <p>received data: </p></body></html>",
   "C servlet title", 
   "C servlet", 
   ctime(&now), 
   (int)len);
    evhttp_add_header(evhttp_request_get_output_headers(req),
   "Content-Type", "text/html");
evhttp_send_reply(req, 200, "OK", evb);
}

But is I try a POST with curl I get 

Bus error: 10

and the server crashes. I am using a Mac. Without 

    struct evbuffer *in_evb = evhttp_request_get_input_buffer(req);
    char *data;

the code works. Do you have an idea why. Is there any example I could use?

Thanks a lot.

--
Riccardo Tacconi
Ruby on Rails and PHP development - System Administration
VIRTUELOGIC LIMITED

http://github.com/rtacconi
http://riccardotacconi.blogspot.com
http://twitter.com/rtacconi