2013-07-22 86 views
0

在下面的示例程序:为什么evhttp_request_get_connection()总是返回NULL?

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

void response_cb(struct evhttp_request* req, void *arg) { 
    assert(evhttp_request_get_response_code(req)<400);/* passes */ 
    assert(evhttp_request_get_connection(req));/* FAILS ??? */ 
} 

int main(int argc, char **argv) { 
    struct event_base* ev_base; 
    struct evhttp_connection *http_conn; 
    struct evhttp_request *req; 

    ev_base = event_base_new(); 
    http_conn = evhttp_connection_base_new(ev_base, NULL, "google.com", 80); 
    req = evhttp_request_new(response_cb, NULL); 

    evhttp_make_request(http_conn, req, EVHTTP_REQ_GET, "/"); 

    event_base_dispatch(ev_base); 
    return -1; 
} 

在response_cb第一断言通过,如预期的,但秒失败即evhttp_request_get_connection(REQ)返回NULL。这是为什么?

为evhttp_request_get_connection权利要求中的文档:

返回与请求或NULL相关联的连接对象。

但我仍然有一个连接。我不会在任何地方处理它。

我做错了什么,或者这是一个错误还是只是一些模糊的功能?

回答

0

由于连接已经被释放(关闭,或者如果启用HTTP Keepalive时保持打开状态以便重用),您无法获得指向响应回调中连接的指针。

您可以在evhttp_connection_done()(http.c,大约第780至817行)中看到在请求的连接字段(evcon)设置为NULL后调用响应回调。但我同意evhttp API不是很清楚。

+0

保持活力似乎与此问题没有任何关系。 根据http.c:evhttp_connection_done()req-> evcon = NULL为所有传出连接设置,不仅用于非保持连接。 –

+0

那么,这只是一个例子,并非彻底检查连接可能被重用的所有情况。对不起,如果这似乎。 –

相关问题