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

[Libevent-users] parse query string



Hi,

I have the following code called from libevent's http-server.c demo:

#include "servlet.h"

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);
    evhttp_parse_query_str(data, evhttp_request_get_input_headers(req));
    
    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>\n"
   " <p>received data: %s</p></body></html>",
   "C servlet title", 
   "C servlet", 
   ctime(&now), 
   (int)len,
   data);
    evhttp_add_header(evhttp_request_get_output_headers(req),
   "Content-Type", "text/html");
evhttp_send_reply(req, 200, "OK", evb);
    free(data);
}

in data I have this string "param1=value1&param2=value2". I would like to use the in-build parser and I added:

evhttp_parse_query_str(data, evhttp_request_get_input_headers(req));

but I am not sure how I can access the parsed key/values and if I am using the right function. I can see from the source the the function doing the parsing is evhttp_parse_query_impl(const char *str, struct evkeyvalq *headers,int is_whole_uri) in http.c but this line:

evhttp_add_header_internal(headers, key, decoded_value);

tells me that the decoded value in stored in evhttp_add_header_internal. I am a bit confused... sorry to post so simple questions :-(

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

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