Stream client request body with libevent evhttp?

2019-05-30 05:50发布

I'd like to stream a client POST request body with libevent and evhttp. I've found examples of sending requests with fixed bodies, but am not sure how to setup a a request with a body I will need to continuously write and update for an undetermined period. Is it possible to do this will libevent? My current baseline of code looks something like this:

#include <evhttp.h>
#include <event2/event.h>
#include <event2/http.h>

void http_request_done(struct evhttp_request *req, void *arg) {
  printf("DONE!\n");
}

int main(int argc, char **argv) {
  struct event_base *base = event_base_new();
  struct evhttp_connection *conn = evhttp_connection_base_new(base, NULL, "127.0.0.1", 3000);
  struct evhttp_request *req = evhttp_request_new(http_request_done, NULL);

  evhttp_make_request(conn, req, EVHTTP_REQ_POST, "/");
  evhttp_connection_set_timeout(req->evcon, 600);
  event_base_loop(base, EVLOOP_NONBLOCK);
  event_base_dispatch(base);

  return 0;
}

How do I go about sending a POST request with a streaming body?

标签: c http libevent
1条回答
萌系小妹纸
2楼-- · 2019-05-30 06:34

LibEvent has chunked functions for this. You can see code examples like this and this one

We can see in documentation those functions - chunk() in loop between start()/end():`

evhttp_send_reply_start(struct evhttp_request *req)

evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf)

evhttp_send_reply_end(struct evhttp_request *req)`.

Those are for sending, and if you need to get incoming chunked data there is evhttp_request_set_chunked_cb()

查看更多
登录 后发表回答