The last uses of the hostandport variable, besides being strdup'ed
before being split into host and port, is to fill the host header in the
git protocol and to test whether to proxy the request.

Instead of relying on parse_connect_url() to return a host:port string
that makes sense there, re-derive one from the host and port variables.
This will allow to refactor parse_connect_url() to return separate host
and port strings.

Signed-off-by: Mike Hommey <m...@glandium.org>
---
 connect.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/connect.c b/connect.c
index d5af65f..3d7bd8e 100644
--- a/connect.c
+++ b/connect.c
@@ -695,18 +695,32 @@ struct child_process *git_connect(int fd[2], const char 
*url,
                 * connect, unless the user has overridden us in
                 * the environment.
                 */
-               char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
-               if (target_host)
-                       target_host = xstrdup(target_host);
-               else
-                       target_host = xstrdup(hostandport);
+               struct strbuf target_host = STRBUF_INIT;
+               struct strbuf virtual_host = STRBUF_INIT;
+               const char *colon = strchr(host, ':');
+               char *override_vhost = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
+
+               /* If the host contains a colon (ipv6 address), it needs to
+                * be enclosed with square brackets. */
+               if (colon)
+                       strbuf_addch(&target_host, '[');
+               strbuf_addstr(&target_host, host);
+               if (colon)
+                       strbuf_addch(&target_host, ']');
+               if (port) {
+                       strbuf_addch(&target_host, ':');
+                       strbuf_addstr(&target_host, port);
+               }
+
+               strbuf_addstr(&virtual_host, override_vhost ? override_vhost
+                                                           : target_host.buf);
 
                transport_check_allowed("git");
 
                /* These underlying connection commands die() if they
                 * cannot connect.
                 */
-               if (git_use_proxy(hostandport))
+               if (git_use_proxy(target_host.buf))
                        conn = git_proxy_connect(fd, host, port);
                else
                        git_tcp_connect(fd, host, port, flags);
@@ -720,8 +734,9 @@ struct child_process *git_connect(int fd[2], const char 
*url,
                packet_write(fd[1],
                             "%s %s%chost=%s%c",
                             prog, path, 0,
-                            target_host, 0);
-               free(target_host);
+                            virtual_host.buf, 0);
+               strbuf_release(&virtual_host);
+               strbuf_release(&target_host);
        } else {
                conn = xmalloc(sizeof(*conn));
                child_process_init(conn);
-- 
2.8.3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to