Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libevent HTTP Server & compression?

Tags:

http

libevent

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:

  1. Apply gzip/deflate compression using zlib in my app before sending out the response
  2. 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 compression formats from the Accept-Encoding header)

Is there some easier way I'm overlooking, or is this pretty much it?

like image 316
tejas Avatar asked Jun 17 '26 00:06

tejas


1 Answers

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; 

    ...   
}
like image 148
Orwellophile Avatar answered Jun 20 '26 12:06

Orwellophile