[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [Libevent-users] asynchronous or simultanous web service using evhttp.h
On Fri, 12 Nov 2010 09:10:31 +0200
Mihai Draghicioiu <mihai.draghicioiu@xxxxxxxxx> wrote:
> Just use an event loop and http callbacks in your server code. That's
> what the whole point of libevent is, i think. I can show you an
> example libevent based http page http://mappinghell.net:28888 it
> refreshes some data each 2 seconds in the web page using ajax. You'll
> need to manually set some http headers to make it correct, and take
> advantage of the browser cache and other http features.
I am not sure that answers my question:
> On Fri, Nov 12, 2010 at 12:48 AM, Basile Starynkevitch
> <basile@xxxxxxxxxxxxxxxxx> wrote:
[...]
> >
> > Is there a possibility to use evhttp.h functions asynchronously, in
> > particular to handle several (eg. 2-4) HTTP requests at once (typically
> > I was thinking of AJAX, e.g. a browser tab doing some XMLHttpRequest
> > while another tab is actively used by the user who is e.g. POST-ing
> > from an XHTML form; so the application would need to reply to the
> > XMLHttpRequest while it is replying to the user in another browser
> > tab.), ? How can I achieve that? By creating a thread for each request?
> > By waiting for several HTTP requests in libevent? How?
Because I wanted to process several HTTP requests at once, that is
having some rather complex computation replying to one HTTP request
(probably inside its own thread) while accepting other HTTP requests
simultanously.
By looking inside libevent*/http.c, I noticed that libevent2 [2.0.8-rc
exactly] defines EVHTTP_USER_OWNED while libevent1 [1.4.14b-stable
exactly] don't have it (but has a EVHTTP_REQ_OWN_CONNECTION) and always
call the client callback.
In other words, I believe that (at least with libevent1) an HTTP
callback could not be as simple as
static void* http_work(void* httpdata);
/* callback passed to evhttp_set_cb */
void http_cb (struct evhttp_request *hrq, void *data)
{
pthread_t thr;
/* create a thread to do the real request work */
pthread_create (&thr, (pthread_attr_t*)0, http_work, rq);
/* this won't work, since libevent will destroy hrq! */
}
static void* http_work (void* httpdata)
{
static long reqcount;
struct evbuffer *evb = NULL;
struct evhttp_request* hrq = httpdata;
reqcount++;
evb = evbuffer_new ();
evbuffer_add_printf (evb, "<html><head><title>Hello</title></head>"
"<body><h1>request %ld</h1></body></html>\n",
reqcount);
evhttp_send_reply (hrq, HTTP_OK, "Everything is fine", evb);
evbuffer_free (evb);
return NULL;
}
BTW, for those working on libevent documentation, I would suggest
adding such simple examples about the HTTP part in it.
Maybe I should consider libmicrohttpd http://www.gnu.org/software/libmicrohttpd/ instead of libevent?
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.