Commit db84b7988bfbc9caf4fa584d775b4b43154261db

Carlos Martín Nieto 2011-09-04T15:32:11

Move extract_host_and_port to netops and add default port argument Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>

diff --git a/src/netops.c b/src/netops.c
index b525192..f44209a 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -161,3 +161,33 @@ int gitno_select_in(gitno_buffer *buf, long int sec, long int usec)
 	/* The select(2) interface is silly */
 	return select(buf->fd + 1, &fds, NULL, NULL, &tv);
 }
+
+int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port)
+{
+	char *colon, *slash, *delim;
+	int error = GIT_SUCCESS;
+
+	colon = strchr(url, ':');
+	slash = strchr(url, '/');
+
+	if (slash == NULL)
+			return git__throw(GIT_EOBJCORRUPTED, "Malformed URL: missing /");
+
+	if (colon == NULL) {
+		*port = git__strdup(default_port);
+	} else {
+		*port = git__strndup(colon + 1, slash - colon - 1);
+	}
+	if (*port == NULL)
+		return GIT_ENOMEM;;
+
+
+	delim = colon == NULL ? slash : colon;
+	*host = git__strndup(url, delim - url);
+	if (*host == NULL) {
+		free(*port);
+		error = GIT_ENOMEM;
+	}
+
+	return error;
+}
diff --git a/src/netops.h b/src/netops.h
index c259ed2..6e3cd72 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -26,4 +26,6 @@ int gitno_connect(const char *host, const char *port);
 int gitno_send(int s, const char *msg, size_t len, int flags);
 int gitno_select_in(gitno_buffer *buf, long int sec, long int usec);
 
+int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port);
+
 #endif
diff --git a/src/transport_git.c b/src/transport_git.c
index 7b0edcf..fceece3 100644
--- a/src/transport_git.c
+++ b/src/transport_git.c
@@ -102,37 +102,6 @@ cleanup:
 	return error;
 }
 
-/* The URL should already have been stripped of the protocol */
-static int extract_host_and_port(char **host, char **port, const char *url)
-{
-	char *colon, *slash, *delim;
-	int error = GIT_SUCCESS;
-
-	colon = strchr(url, ':');
-	slash = strchr(url, '/');
-
-	if (slash == NULL)
-			return git__throw(GIT_EOBJCORRUPTED, "Malformed URL: missing /");
-
-	if (colon == NULL) {
-		*port = git__strdup(GIT_DEFAULT_PORT);
-	} else {
-		*port = git__strndup(colon + 1, slash - colon - 1);
-	}
-	if (*port == NULL)
-		return GIT_ENOMEM;;
-
-
-	delim = colon == NULL ? slash : colon;
-	*host = git__strndup(url, delim - url);
-	if (*host == NULL) {
-		free(*port);
-		error = GIT_ENOMEM;
-	}
-
-	return error;
-}
-
 /*
  * Parse the URL and connect to a server, storing the socket in
  * out. For convenience this also takes care of asking for the remote
@@ -148,9 +117,10 @@ static int do_connect(transport_git *t, const char *url)
 	if (!git__prefixcmp(url, prefix))
 		url += strlen(prefix);
 
-	error = extract_host_and_port(&host, &port, url);
+	error = gitno_extract_host_and_port(&host, &port, url, GIT_DEFAULT_PORT);
 	if (error < GIT_SUCCESS)
 		return error;
+
 	s = gitno_connect(host, port);
 	connected = 1;
 	error = send_request(s, NULL, url);