Libevent HTTP Server & compression?
I'm using libevent2 in my application to host a http server. I cant find a built-in way to compress the output.
These are the options I'm considering:
- Apply gzip/deflate compression using zlib in my app before sending out the response
- Hack libevent's http.c to expose evhttp_connection->bufev (the bufferevent object), and apply a zlib filter for outgoing data
(Both read the supported compr开发者_Python百科ession formats from the Accept-Encoding header)
Is there some easier way I'm overlooking, or is this pretty much it?
I use this little trick to obtain the file descriptor of an evhttp_connection, which is right next to the pointer you want. It's a nasty hack, but it's simple, and easier that having to rebuild libevent. It has been tested under x86_64 and runs fine.
static void
send_document_cb(struct evhttp_request *req, void *arg)
{
// ....
struct evhttp_connection *this_connection;
this_connection = evhttp_request_get_connection(req);
int *tricky;
tricky = (((int *)this_connection) + 4);
int fd = *tricky;
printf("fd: %i\n", fd);
// ....
}
Looking at the structure definition (beneath), it appears the bufev you want should be accessible using (((void *)this_connection) + 8) or something very similar.
struct evhttp_connection {
TAILQ_ENTRY(evhttp_connection) next;
evutil_socket_t fd;
struct bufferevent *bufev;
...
}
Non-hacky way to get the FD socket in 2022:
static void _genericCallback( evhttp_request *req, void *arg )
{
evhttp_connection *conn = evhttp_request_get_connection( req );
if( conn )
{
bufferevent *buffevt = evhttp_connection_get_bufferevent( conn );
if( buffevt )
{
evutil_socket_t fd = bufferevent_getfd( buffevt );
if( fd >= 0 )
{
// Use the socket
setsockopt( fd, ... );
}
}
}
}
evhttp_set_gencb( server, _genericCallback, 0 );
精彩评论