diff --git a/http.c b/http.c index d07730d7..b94fed8c 100644 --- a/http.c +++ b/http.c @@ -2330,6 +2330,8 @@ evhttp_connection_get_peer(struct evhttp_connection *evcon, int evhttp_connection_connect_(struct evhttp_connection *evcon) { + int old_state = evcon->state; + if (evcon->state == EVCON_CONNECTING) return (0); @@ -2367,8 +2369,11 @@ evhttp_connection_connect_(struct evhttp_connection *evcon) /* make sure that we get a write callback */ bufferevent_enable(evcon->bufev, EV_WRITE); + evcon->state = EVCON_CONNECTING; + if (bufferevent_socket_connect_hostname(evcon->bufev, evcon->dns_base, AF_UNSPEC, evcon->address, evcon->port) < 0) { + evcon->state = old_state; event_sock_warn(evcon->fd, "%s: connection to \"%s\" failed", __func__, evcon->address); /* some operating systems return ECONNREFUSED immediately @@ -2379,8 +2384,6 @@ evhttp_connection_connect_(struct evhttp_connection *evcon) return (0); } - evcon->state = EVCON_CONNECTING; - return (0); } diff --git a/test/regress_http.c b/test/regress_http.c index 31412a20..3ed314a0 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -3034,16 +3034,21 @@ http_stream_in_cancel_test(void *arg) static void http_connection_fail_done(struct evhttp_request *req, void *arg) { + struct evhttp_connection *evcon = arg; + struct event_base *base = evhttp_connection_get_base(evcon); + /* An ENETUNREACH error results in an unrecoverable * evhttp_connection error (see evhttp_connection_fail_()). The * connection will be reset, and the user will be notified with a NULL * req parameter. */ tt_assert(!req); + evhttp_connection_free(evcon); + test_ok = 1; end: - event_base_loopexit(arg, NULL); + event_base_loopexit(base, NULL); } /* Test unrecoverable evhttp_connection errors by generating an ENETUNREACH @@ -3074,7 +3079,7 @@ http_connection_fail_test(void *arg) * server using our make request method. */ - req = evhttp_request_new(http_connection_fail_done, data->base); + req = evhttp_request_new(http_connection_fail_done, evcon); tt_assert(req); if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/") == -1) { @@ -3086,8 +3091,7 @@ http_connection_fail_test(void *arg) tt_int_op(test_ok, ==, 1); end: - if (evcon) - evhttp_connection_free(evcon); + ; } static void