Search code examples
clibevent

difference between two call backs in libevent


In libevent, are both of the following APIs going to invoke the call back function when the HTTP transaction completes?

//from event2/http.h
/**
 * Creates a new request object that needs to be filled in with the request
 * parameters.  The callback is executed when the request completed or an
 * error occurred.
 */
struct evhttp_request *evhttp_request_new(
    void (*cb)(struct evhttp_request *, void *), void *arg);

/*The callback function will be called on the completion of the request after
 * the output data has been written and before the evhttp_request object
 * is destroyed ....*/
void evhttp_request_set_on_complete_cb(struct evhttp_request *req,
    void (*cb)(struct evhttp_request *, void *), void *cb_arg);

The wording for evhttp_request_new(..) is different from that of evhttp_request_set_on_complete_cb(..), but in my test, the call back in evhttp_request_new(..) is indeed invoked at the end of HTTP transaction.


Solution

  • I've inspected those functions in http.c (libevent 2.1.8) and I found that their inner usage differ:

    1) evhttp_request_new() CREATES evhttp_request object AND SETS req->cb that is USED BY:

    • evhttp_connection_incoming_fail() ---> calls req->cb as reply-function about this failing that comes from anywhere - corrupted error, too long, firstline function, etc...
    • evhttp_connection_done() ---> calls as (*req->cb)(req, req->cb_arg); just after processing request/response.

    It is positioned by evhttp_connection and error

    2) evhttp_request_set_on_complete_cb() SETS req->on_complete_cb FOR EXISTING evhttp_request object in additional to req->cb. USED ONLY BY: evhttp_send_done (this function called from evhttp_send()->evhttp_write_buffer(): evcon->cb=evhttp_send_done() after whole requested page data is sent / chunked request finish).

    It is positioned after data writing over, and only if some error did not occure.