[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] ibevent: HTTP POST service example?
Hello
Do you have an example of an HTTP server in libevent which handles a
POST request?
Apparently, calling evbuffer_readline (hreq->input_buffer); from inside
a callback registered with evhttp_set_cb don't work as expected.
I google-code and did not found anything.
I am coding a tiny login form. Here is an attempt of my login evhttp callback.
But I am stuck. I don't know how to get the POST-ed data in it.
static void
http_barklogin_cb (struct evhttp_request *hrq, void *data)
{
static long logcount;
struct evbuffer *evb = NULL;
time_t now = 0;
char cnowbuf[80];
char *hostnam = 0;
char hostheader[80];
pid_t pid = getpid ();
time (&now);
memset (hostheader, 0, sizeof (hostheader));
memset (cnowbuf, 0, sizeof (cnowbuf));
strftime (cnowbuf, sizeof (cnowbuf) - 1, "%Y %b %d @ %H:%M:%S %Z",
localtime (&now));
hostnam = (bk_http_host && *bk_http_host) ? bk_http_host :
bk_host_name; snprintf (hostheader, sizeof (hostheader) - 1, "%s:%d",
hostnam, bk_http_port);
bk_debug2 ("barklogin #%ld hostheader %s", logcount, hostheader);
bk_debug3 ("barklogin remote %s:%d uri %s type#%d %s",
hrq->remote_host, hrq->remote_port, hrq->uri, hrq->type,
(hrq->type == EVHTTP_REQ_GET) ? "GET"
: (hrq->type == EVHTTP_REQ_POST) ? "POST"
: (hrq->type == EVHTTP_REQ_HEAD) ? "HEAD" : "???");
if (hrq->type == EVHTTP_REQ_GET)
{
logcount++;
evb = evbuffer_new ();
evbuffer_add_printf (evb,
"<html><head><title>Bark [%s,%d]
Login</title>" "</head>"
"<body><h1>Bark <small><tt>[%s,%d]
</tt></small> Login</h1>\n", bk_host_name, pid, bk_host_name, pid);
evbuffer_add_printf (evb,
"<p>Login #%ld to Bark <tt><i>%s</i></tt> at
<i>%s</i> from <tt>%s</tt>:\n" "<form name='loginform' action='%s'
method='post'><br />\n", logcount, hostheader, cnowbuf,
hrq->remote_host, hrq->uri);
evbuffer_add_printf (evb,
"<b>user:</b> <input type='text'
name='barkuser' width='40'/> <br/><br/>\n"); evbuffer_add_printf (evb,
"<b>password:</b> <input type='password'
name='barkpasswd' width='32'/> <br/><br/>\n"); evbuffer_add_printf (evb,
"<input type='submit' name='barklogin'
value='Login'/> - \n"); evbuffer_add_printf (evb,
"<input type='reset' name='barkclear'
value='Clear'/> \n"); evbuffer_add_printf (evb,
"<input type='hidden' name='barkpid' value='%
d'/> \n", pid);
evbuffer_add_printf (evb, "\n</form></p></body></html>\n");
evhttp_add_header (hrq->output_headers, "Host", hostheader);
evhttp_send_reply (hrq, HTTP_OK, "Everything is fine", evb);
evbuffer_free (evb);
}
else if (hrq->type == EVHTTP_REQ_POST)
{
char* contline = 0;
const char *contyp =
evhttp_find_header (hrq->input_headers, "Content-Type");
const char *contlenstr =
evhttp_find_header (hrq->input_headers, "Content-Length");
int contlen = 0;
bk_debug3 ("contyp %s ntoread %d contlenstr %s", contyp,
(int) hrq->ntoread, contlenstr);
if (strcmp (contyp, "application/x-www-form-urlencoded"))
bk_fatal ("expecting login form data, got Content-Type: %s",
contyp); contlen = contlenstr ? atoi (contlenstr) : 0;
bk_debug3("contlen %d input_buffer", contlen, hrq->input_buffer);
contline = evbuffer_readline (hrq->input_buffer);
bk_debug3("contline/%d:%s", contline?strlen(contline):0,
contline); /* should have a reusable routine to transform the form
encoded data into a dictionnary */
#warning unimplemented login post, should make a dictionnary of the form data!
bk_fatal ("unimplemented login");
}
}
BTW I hope to release my code under GPLv3 licence in 2011. This is a
hobby unrelated to my work (professionally I am working on GCC,
the MELT branch, see gcc-melt.org for more)
Cheers
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.