From b618204216235d5998080c659c8ad53185fdf206 Mon Sep 17 00:00:00 2001 From: Greg Hazel Date: Thu, 14 Feb 2013 09:54:56 -0800 Subject: [PATCH] fix #73 and fix http_connection_fail_test to catch it --- http.c | 7 +++++-- test/regress_http.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/http.c b/http.c index 3994b917..9b96ffb3 100644 --- a/http.c +++ b/http.c @@ -2181,6 +2181,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); @@ -2209,8 +2211,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 @@ -2221,8 +2226,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 cc3bf27a..9ece455b 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -3013,16 +3013,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 @@ -3053,7 +3058,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) { @@ -3065,8 +3070,7 @@ http_connection_fail_test(void *arg) tt_int_op(test_ok, ==, 1); end: - if (evcon) - evhttp_connection_free(evcon); + ; } static void