Skip to content

Commit 2807de5

Browse files
committed
http: handle ipv6 addresses
1 parent 780ad7a commit 2807de5

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/transports/httpclient.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,26 @@ GIT_INLINE(int) apply_proxy_credentials(
631631
request->proxy_credentials);
632632
}
633633

634+
static int puts_host_and_port(git_buf *buf, git_net_url *url, bool force_port)
635+
{
636+
bool ipv6 = git_net_url_is_ipv6(url);
637+
638+
if (ipv6)
639+
git_buf_putc(buf, '[');
640+
641+
git_buf_puts(buf, url->host);
642+
643+
if (ipv6)
644+
git_buf_putc(buf, ']');
645+
646+
if (force_port || !git_net_url_is_default_port(url)) {
647+
git_buf_putc(buf, ':');
648+
git_buf_puts(buf, url->port);
649+
}
650+
651+
return git_buf_oom(buf) ? -1 : 0;
652+
}
653+
634654
static int generate_connect_request(
635655
git_http_client *client,
636656
git_http_request *request)
@@ -641,14 +661,17 @@ static int generate_connect_request(
641661
git_buf_clear(&client->request_msg);
642662
buf = &client->request_msg;
643663

644-
git_buf_printf(buf, "CONNECT %s:%s HTTP/1.1\r\n",
645-
client->server.url.host, client->server.url.port);
664+
git_buf_puts(buf, "CONNECT ");
665+
puts_host_and_port(buf, &client->server.url, true);
666+
git_buf_puts(buf, " HTTP/1.1\r\n");
646667

647668
git_buf_puts(buf, "User-Agent: ");
648669
git_http__user_agent(buf);
649670
git_buf_puts(buf, "\r\n");
650671

651-
git_buf_printf(buf, "Host: %s\r\n", client->proxy.url.host);
672+
git_buf_puts(buf, "Host: ");
673+
puts_host_and_port(buf, &client->proxy.url, false);
674+
git_buf_puts(buf, "\r\n");
652675

653676
if ((error = apply_proxy_credentials(buf, client, request) < 0))
654677
return -1;
@@ -687,11 +710,8 @@ static int generate_request(
687710
git_http__user_agent(buf);
688711
git_buf_puts(buf, "\r\n");
689712

690-
git_buf_printf(buf, "Host: %s", request->url->host);
691-
692-
if (!git_net_url_is_default_port(request->url))
693-
git_buf_printf(buf, ":%s", request->url->port);
694-
713+
git_buf_puts(buf, "Host: ");
714+
puts_host_and_port(buf, request->url, false);
695715
git_buf_puts(buf, "\r\n");
696716

697717
if (request->accept)
@@ -902,7 +922,7 @@ static int proxy_connect(
902922
int error;
903923

904924
if (!client->proxy_connected || !client->keepalive) {
905-
git_trace(GIT_TRACE_DEBUG, "Connecting to proxy %s:%s",
925+
git_trace(GIT_TRACE_DEBUG, "Connecting to proxy %s port %s",
906926
client->proxy.url.host, client->proxy.url.port);
907927

908928
if ((error = server_create_stream(&client->proxy)) < 0 ||
@@ -1023,7 +1043,7 @@ static int http_client_connect(
10231043
goto on_error;
10241044
}
10251045

1026-
git_trace(GIT_TRACE_DEBUG, "Connecting to remote %s:%s",
1046+
git_trace(GIT_TRACE_DEBUG, "Connecting to remote %s port %s",
10271047
client->server.url.host, client->server.url.port);
10281048

10291049
if ((error = server_connect(client)) < 0)

0 commit comments

Comments
 (0)