Chunked responses in libevent2

Posted by Sri on Stack Overflow See other posts from Stack Overflow or by Sri
Published on 2010-03-18T07:15:02Z Indexed on 2010/03/18 7:21 UTC
Read the original article Hit count: 503

Hi

I am trying to do a chunked response (of large files) in libevent this way::

evhttp_send_reply_start(request, HTTP_OK, "OK");

int fd = open("filename", O_RDONLY);
size_t fileSize = <get_file_size>;
struct evbuffer *databuff = NULL;
for (off_t offset = 0;offset < fileSize;)
{
    databuff = evbuffer_new();

    size_t bytesLeft = fileSize - offset;
    size_t bytesToRead = bytesLeft > MAX_READ_SIZE ? MAX_READ_SIZE : bytesLeft;

    evbuffer_add_file(databuff, fd, offset, bytesToRead);
    offset += bytesToRead;

    evhttp_send_reply_chunk(request, databuff); // send it
    evbuffer_free(databuff);                    // destroy it
}

evhttp_send_reply_end(request);

fclose(fptr);

Problem is with this I have a feeling the add_file is asynchronous so the 3rd or so evhttp_send_reply_chunk gives me an error (or something similar):

[warn] evhttp_send_chain Closed(45): Bad file descriptor

I set MAX_READ_SIZE to be 8 to actually test out chunked transfer encoding.

I noticed there was a evhttp_request_set_chunked_cb (struct evhttp_request *, void(*cb)(struct evhttp_request *, void *)) method I could use but could not find any examples on how to use.

For instance, how could I pass an argument to the callback? The argument seems to be the same argument that was passed to the request handler which is not what I want, because I want to create an object that holds the file descriptor and the file offset I am sending out.

Appreciate all help.

Thanks in advance Sri

© Stack Overflow or respective owner

Related posts about transfer-encoding

Related posts about chunked