http: handle ipv6 addresses
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
diff --git a/src/transports/httpclient.c b/src/transports/httpclient.c
index d9cbf17..d397574 100644
--- a/src/transports/httpclient.c
+++ b/src/transports/httpclient.c
@@ -631,6 +631,26 @@ GIT_INLINE(int) apply_proxy_credentials(
request->proxy_credentials);
}
+static int puts_host_and_port(git_buf *buf, git_net_url *url, bool force_port)
+{
+ bool ipv6 = git_net_url_is_ipv6(url);
+
+ if (ipv6)
+ git_buf_putc(buf, '[');
+
+ git_buf_puts(buf, url->host);
+
+ if (ipv6)
+ git_buf_putc(buf, ']');
+
+ if (force_port || !git_net_url_is_default_port(url)) {
+ git_buf_putc(buf, ':');
+ git_buf_puts(buf, url->port);
+ }
+
+ return git_buf_oom(buf) ? -1 : 0;
+}
+
static int generate_connect_request(
git_http_client *client,
git_http_request *request)
@@ -641,14 +661,17 @@ static int generate_connect_request(
git_buf_clear(&client->request_msg);
buf = &client->request_msg;
- git_buf_printf(buf, "CONNECT %s:%s HTTP/1.1\r\n",
- client->server.url.host, client->server.url.port);
+ git_buf_puts(buf, "CONNECT ");
+ puts_host_and_port(buf, &client->server.url, true);
+ git_buf_puts(buf, " HTTP/1.1\r\n");
git_buf_puts(buf, "User-Agent: ");
git_http__user_agent(buf);
git_buf_puts(buf, "\r\n");
- git_buf_printf(buf, "Host: %s\r\n", client->proxy.url.host);
+ git_buf_puts(buf, "Host: ");
+ puts_host_and_port(buf, &client->proxy.url, false);
+ git_buf_puts(buf, "\r\n");
if ((error = apply_proxy_credentials(buf, client, request) < 0))
return -1;
@@ -687,11 +710,8 @@ static int generate_request(
git_http__user_agent(buf);
git_buf_puts(buf, "\r\n");
- git_buf_printf(buf, "Host: %s", request->url->host);
-
- if (!git_net_url_is_default_port(request->url))
- git_buf_printf(buf, ":%s", request->url->port);
-
+ git_buf_puts(buf, "Host: ");
+ puts_host_and_port(buf, request->url, false);
git_buf_puts(buf, "\r\n");
if (request->accept)
@@ -902,7 +922,7 @@ static int proxy_connect(
int error;
if (!client->proxy_connected || !client->keepalive) {
- git_trace(GIT_TRACE_DEBUG, "Connecting to proxy %s:%s",
+ git_trace(GIT_TRACE_DEBUG, "Connecting to proxy %s port %s",
client->proxy.url.host, client->proxy.url.port);
if ((error = server_create_stream(&client->proxy)) < 0 ||
@@ -1023,7 +1043,7 @@ static int http_client_connect(
goto on_error;
}
- git_trace(GIT_TRACE_DEBUG, "Connecting to remote %s:%s",
+ git_trace(GIT_TRACE_DEBUG, "Connecting to remote %s port %s",
client->server.url.host, client->server.url.port);
if ((error = server_connect(client)) < 0)