Move extract_host_and_port to netops and add default port argument Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
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);