Merge pull request #39 from azat/fix-http-for-ipv6

Fix ipv6 support for http. When URL contain domain, not IP address.
This commit is contained in:
Nick Mathewson 2013-02-04 13:49:08 -08:00
commit 2ecd894725
2 changed files with 91 additions and 43 deletions

25
http.c
View File

@ -1327,6 +1327,9 @@ evhttp_error_cb(struct bufferevent *bufev, short what, void *arg)
struct evhttp_connection *evcon = arg; struct evhttp_connection *evcon = arg;
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests); struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
if (evcon->fd == -1)
evcon->fd = bufferevent_getfd(bufev);
switch (evcon->state) { switch (evcon->state) {
case EVCON_CONNECTING: case EVCON_CONNECTING:
if (what & BEV_EVENT_TIMEOUT) { if (what & BEV_EVENT_TIMEOUT) {
@ -1393,6 +1396,9 @@ evhttp_connection_cb(struct bufferevent *bufev, short what, void *arg)
int error; int error;
ev_socklen_t errsz = sizeof(error); ev_socklen_t errsz = sizeof(error);
if (evcon->fd == -1)
evcon->fd = bufferevent_getfd(bufev);
if (!(what & BEV_EVENT_CONNECTED)) { if (!(what & BEV_EVENT_CONNECTED)) {
/* some operating systems return ECONNREFUSED immediately /* some operating systems return ECONNREFUSED immediately
* when connecting to a local address. the cleanup is going * when connecting to a local address. the cleanup is going
@ -2332,16 +2338,21 @@ evhttp_connection_connect_(struct evhttp_connection *evcon)
EVUTIL_ASSERT(!(evcon->flags & EVHTTP_CON_INCOMING)); EVUTIL_ASSERT(!(evcon->flags & EVHTTP_CON_INCOMING));
evcon->flags |= EVHTTP_CON_OUTGOING; evcon->flags |= EVHTTP_CON_OUTGOING;
evcon->fd = bind_socket( if (evcon->bind_address || evcon->bind_port) {
evcon->bind_address, evcon->bind_port, 0 /*reuse*/); evcon->fd = bind_socket(
if (evcon->fd == -1) { evcon->bind_address, evcon->bind_port, 0 /*reuse*/);
event_debug(("%s: failed to bind to \"%s\"", if (evcon->fd == -1) {
__func__, evcon->bind_address)); event_debug(("%s: failed to bind to \"%s\"",
return (-1); __func__, evcon->bind_address));
return (-1);
}
bufferevent_setfd(evcon->bufev, evcon->fd);
} else {
bufferevent_setfd(evcon->bufev, -1);
} }
/* Set up a callback for successful connection setup */ /* Set up a callback for successful connection setup */
bufferevent_setfd(evcon->bufev, evcon->fd);
bufferevent_setcb(evcon->bufev, bufferevent_setcb(evcon->bufev,
NULL /* evhttp_read_cb */, NULL /* evhttp_read_cb */,
NULL /* evhttp_write_cb */, NULL /* evhttp_write_cb */,

View File

@ -80,12 +80,16 @@ static void http_large_delay_cb(struct evhttp_request *req, void *arg);
static void http_badreq_cb(struct evhttp_request *req, void *arg); static void http_badreq_cb(struct evhttp_request *req, void *arg);
static void http_dispatcher_cb(struct evhttp_request *req, void *arg); static void http_dispatcher_cb(struct evhttp_request *req, void *arg);
static int static int
http_bind(struct evhttp *myhttp, ev_uint16_t *pport) http_bind(struct evhttp *myhttp, ev_uint16_t *pport, int ipv6)
{ {
int port; int port;
struct evhttp_bound_socket *sock; struct evhttp_bound_socket *sock;
sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport); if (ipv6)
sock = evhttp_bind_socket_with_handle(myhttp, "::1", *pport);
else
sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport);
if (sock == NULL) if (sock == NULL)
event_errx(1, "Could not start web server"); event_errx(1, "Could not start web server");
@ -98,14 +102,14 @@ http_bind(struct evhttp *myhttp, ev_uint16_t *pport)
} }
static struct evhttp * static struct evhttp *
http_setup(ev_uint16_t *pport, struct event_base *base) http_setup(ev_uint16_t *pport, struct event_base *base, int ipv6)
{ {
struct evhttp *myhttp; struct evhttp *myhttp;
/* Try a few different ports */ /* Try a few different ports */
myhttp = evhttp_new(base); myhttp = evhttp_new(base);
if (http_bind(myhttp, pport) < 0) if (http_bind(myhttp, pport, ipv6) < 0)
return NULL; return NULL;
/* Register a callback for certain types of requests */ /* Register a callback for certain types of requests */
@ -377,10 +381,10 @@ http_basic_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
/* bind to a second socket */ /* bind to a second socket */
if (http_bind(http, &port2) == -1) { if (http_bind(http, &port2, 0) == -1) {
fprintf(stdout, "FAILED (bind)\n"); fprintf(stdout, "FAILED (bind)\n");
exit(1); exit(1);
} }
@ -565,10 +569,10 @@ http_bad_request_test(void *arg)
test_ok = 0; test_ok = 0;
exit_base = data->base; exit_base = data->base;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
/* bind to a second socket */ /* bind to a second socket */
if (http_bind(http, &port2) == -1) if (http_bind(http, &port2, 0) == -1)
TT_DIE(("Bind socket failed")); TT_DIE(("Bind socket failed"));
/* NULL request test */ /* NULL request test */
@ -683,7 +687,7 @@ http_delete_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
fd = http_connect("127.0.0.1", port); fd = http_connect("127.0.0.1", port);
@ -744,7 +748,7 @@ http_allowed_methods_test(void *arg)
exit_base = data->base; exit_base = data->base;
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
fd1 = http_connect("127.0.0.1", port); fd1 = http_connect("127.0.0.1", port);
@ -835,7 +839,7 @@ static void http_request_done(struct evhttp_request *, void *);
static void http_request_empty_done(struct evhttp_request *, void *); static void http_request_empty_done(struct evhttp_request *, void *);
static void static void
http_connection_test_(struct basic_test_data *data, int persistent) http_connection_test_(struct basic_test_data *data, int persistent, const char *address, struct evdns_base *dnsbase, int ipv6)
{ {
ev_uint16_t port = 0; ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL; struct evhttp_connection *evcon = NULL;
@ -843,9 +847,9 @@ http_connection_test_(struct basic_test_data *data, int persistent)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, ipv6);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, dnsbase, address, port);
tt_assert(evcon); tt_assert(evcon);
tt_assert(evhttp_connection_get_base(evcon) == data->base); tt_assert(evhttp_connection_get_base(evcon) == data->base);
@ -918,12 +922,12 @@ http_connection_test_(struct basic_test_data *data, int persistent)
static void static void
http_connection_test(void *arg) http_connection_test(void *arg)
{ {
http_connection_test_(arg, 0); http_connection_test_(arg, 0, "127.0.0.1", NULL, 0);
} }
static void static void
http_persist_connection_test(void *arg) http_persist_connection_test(void *arg)
{ {
http_connection_test_(arg, 1); http_connection_test_(arg, 1, "127.0.0.1", NULL, 0);
} }
static struct regress_dns_server_table search_table[] = { static struct regress_dns_server_table search_table[] = {
@ -955,7 +959,7 @@ http_connection_async_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, dns_base, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, dns_base, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -1064,7 +1068,7 @@ http_cancel_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -1186,7 +1190,7 @@ http_virtual_host_test(void *arg)
exit_base = data->base; exit_base = data->base;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
/* virtual host */ /* virtual host */
second = evhttp_new(NULL); second = evhttp_new(NULL);
@ -1427,7 +1431,7 @@ http_dispatcher_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -1477,7 +1481,7 @@ http_post_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -1617,7 +1621,7 @@ http_put_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -1743,7 +1747,7 @@ http_failure_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
fd = http_connect("127.0.0.1", port); fd = http_connect("127.0.0.1", port);
@ -1832,7 +1836,7 @@ http_close_detection_(struct basic_test_data *data, int with_delay)
const struct timeval sec_tenth = { 0, 100000 }; const struct timeval sec_tenth = { 0, 100000 };
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
/* .1 second timeout */ /* .1 second timeout */
evhttp_set_timeout_tv(http, &sec_tenth); evhttp_set_timeout_tv(http, &sec_tenth);
@ -2498,7 +2502,7 @@ http_base_test(void *ptr)
test_ok = 0; test_ok = 0;
base = event_base_new(); base = event_base_new();
http = http_setup(&port, base); http = http_setup(&port, base, 0);
fd = http_connect("127.0.0.1", port); fd = http_connect("127.0.0.1", port);
@ -2580,7 +2584,7 @@ http_incomplete_test_(struct basic_test_data *data, int use_timeout)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evhttp_set_timeout(http, 1); evhttp_set_timeout(http, 1);
fd = http_connect("127.0.0.1", port); fd = http_connect("127.0.0.1", port);
@ -2794,7 +2798,7 @@ http_chunk_out_test(void *arg)
exit_base = data->base; exit_base = data->base;
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
fd = http_connect("127.0.0.1", port); fd = http_connect("127.0.0.1", port);
@ -2866,7 +2870,7 @@ http_stream_out_test(void *arg)
test_ok = 0; test_ok = 0;
exit_base = data->base; exit_base = data->base;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -2934,7 +2938,7 @@ http_stream_in_test_(struct basic_test_data *data, char const *url,
ev_uint16_t port = 0; ev_uint16_t port = 0;
exit_base = data->base; exit_base = data->base;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL,"127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL,"127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -3005,7 +3009,7 @@ http_stream_in_cancel_test(void *arg)
struct evhttp_request *req = NULL; struct evhttp_request *req = NULL;
ev_uint16_t port = 0; ev_uint16_t port = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -3056,7 +3060,7 @@ http_connection_fail_test(void *arg)
test_ok = 0; test_ok = 0;
/* auto detect a port */ /* auto detect a port */
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evhttp_free(http); evhttp_free(http);
http = NULL; http = NULL;
@ -3107,7 +3111,7 @@ static void
http_make_web_server(evutil_socket_t fd, short what, void *arg) http_make_web_server(evutil_socket_t fd, short what, void *arg)
{ {
ev_uint16_t port = *(ev_uint16_t*)arg; ev_uint16_t port = *(ev_uint16_t*)arg;
http = http_setup(&port, http_make_web_server_base); http = http_setup(&port, http_make_web_server_base, 0);
} }
static void static void
@ -3123,7 +3127,7 @@ http_connection_retry_test(void *arg)
test_ok = 0; test_ok = 0;
/* auto detect a port */ /* auto detect a port */
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evhttp_free(http); evhttp_free(http);
http = NULL; http = NULL;
@ -3275,7 +3279,7 @@ http_multi_line_header_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
fd = http_connect("127.0.0.1", port); fd = http_connect("127.0.0.1", port);
@ -3335,7 +3339,7 @@ http_negative_content_length_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -3394,7 +3398,7 @@ http_data_length_constraints_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port); evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon); tt_assert(evcon);
@ -3552,7 +3556,7 @@ http_terminate_chunked_test(void *arg)
test_ok = 0; test_ok = 0;
http = http_setup(&port, data->base); http = http_setup(&port, data->base, 0);
evhttp_del_cb(http, "/test"); evhttp_del_cb(http, "/test");
tt_assert(evhttp_set_cb(http, "/test", tt_assert(evhttp_set_cb(http, "/test",
terminate_chunked_cb, &terminate_state) == 0); terminate_chunked_cb, &terminate_state) == 0);
@ -3593,6 +3597,37 @@ http_terminate_chunked_test(void *arg)
evhttp_free(http); evhttp_free(http);
} }
static struct regress_dns_server_table ipv6_search_table[] = {
{ "localhost", "AAAA", "::1", 0 },
{ NULL, NULL, NULL, 0 }
};
static void
http_ipv6_for_domain_test(void *arg)
{
struct basic_test_data *data = arg;
struct evdns_base *dns_base = NULL;
ev_uint16_t portnum = 0;
char address[64];
tt_assert(regress_dnsserver(data->base, &portnum, ipv6_search_table));
dns_base = evdns_base_new(data->base, 0/* init name servers */);
tt_assert(dns_base);
/* Add ourself as the only nameserver, and make sure we really are
* the only nameserver. */
evutil_snprintf(address, sizeof(address), "127.0.0.1:%d", portnum);
evdns_base_nameserver_ip_add(dns_base, address);
http_connection_test_(arg, 0 /* not persistent */, "localhost", dns_base, 1 /* ipv6 */);
end:
if (dns_base)
evdns_base_free(dns_base, 0);
regress_clean_dnsserver();
}
#define HTTP_LEGACY(name) \ #define HTTP_LEGACY(name) \
{ #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \ { #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \
http_##name##_test } http_##name##_test }
@ -3640,6 +3675,8 @@ struct testcase_t http_testcases[] = {
HTTP(connection_retry), HTTP(connection_retry),
HTTP(data_length_constraints), HTTP(data_length_constraints),
HTTP(ipv6_for_domain),
END_OF_TESTCASES END_OF_TESTCASES
}; };