Commit 2234b2b03153c03fc6d502dd61ae55e659be4b8b

Ben Straub 2013-01-30T19:03:58

Stash username from url (but don't use it yet)

diff --git a/src/netops.c b/src/netops.c
index 5623ca9..1273814 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -578,7 +578,7 @@ int gitno_select_in(gitno_buffer *buf, long int sec, long int usec)
 	return select((int)buf->socket->socket + 1, &fds, NULL, NULL, &tv);
 }
 
-int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port)
+int gitno_extract_host_and_port(char **host, char **port, char **username, const char *url, const char *default_port)
 {
 	char *colon, *slash, *at, *delim;
 	const char *start;
@@ -600,7 +600,12 @@ int gitno_extract_host_and_port(char **host, char **port, const char *url, const
 	GITERR_CHECK_ALLOC(*port);
 
 	delim = colon == NULL ? slash : colon;
-	start = at == NULL && at < slash ? url : at+1;
+
+	start = url;
+	if (at && at < slash) {
+		start = at+1;
+		*username = git__strndup(url, at - url);
+	}
 
 	*host = git__strndup(start, delim - start);
 	GITERR_CHECK_ALLOC(*host);
diff --git a/src/netops.h b/src/netops.h
index f8ff42c..bb2624a 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -66,6 +66,6 @@ int gitno_send(gitno_socket *socket, const char *msg, size_t len, int flags);
 int gitno_close(gitno_socket *s);
 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);
+int gitno_extract_host_and_port(char **host, char **port, char **username, const char *url, const char *default_port);
 
 #endif
diff --git a/src/transports/git.c b/src/transports/git.c
index ba6dbfe..5c816e1 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -179,7 +179,7 @@ static int _git_uploadpack_ls(
 	const char *url,
 	git_smart_subtransport_stream **stream)
 {
-	char *host, *port;
+	char *host, *port, *user;
 	git_stream *s;
 
 	*stream = NULL;
@@ -192,7 +192,7 @@ static int _git_uploadpack_ls(
 
 	s = (git_stream *)*stream;
 
-	if (gitno_extract_host_and_port(&host, &port, url, GIT_DEFAULT_PORT) < 0)
+	if (gitno_extract_host_and_port(&host, &port, &user, url, GIT_DEFAULT_PORT) < 0)
 		goto on_error;
 
 	if (gitno_connect(&s->socket, host, port, 0) < 0)
@@ -201,6 +201,7 @@ static int _git_uploadpack_ls(
 	t->current_stream = s;
 	git__free(host);
 	git__free(port);
+	git__free(user);
 	return 0;
 
 on_error:
@@ -233,7 +234,7 @@ static int _git_receivepack_ls(
 	const char *url,
 	git_smart_subtransport_stream **stream)
 {
-	char *host, *port;
+	char *host, *port, *user;
 	git_stream *s;
 
 	*stream = NULL;
@@ -246,7 +247,7 @@ static int _git_receivepack_ls(
 
 	s = (git_stream *)*stream;
 
-	if (gitno_extract_host_and_port(&host, &port, url, GIT_DEFAULT_PORT) < 0)
+	if (gitno_extract_host_and_port(&host, &port, &user, url, GIT_DEFAULT_PORT) < 0)
 		goto on_error;
 
 	if (gitno_connect(&s->socket, host, port, 0) < 0)
@@ -255,6 +256,7 @@ static int _git_receivepack_ls(
 	t->current_stream = s;
 	git__free(host);
 	git__free(port);
+	git__free(user);
 	return 0;
 
 on_error:
diff --git a/src/transports/http.c b/src/transports/http.c
index 96a9c39..e5bb107 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -60,6 +60,7 @@ typedef struct {
 	const char *path;
 	char *host;
 	char *port;
+	char *user_from_url;
 	git_cred *cred;
 	http_authmechanism_t auth_mechanism;
 	unsigned connected : 1,
@@ -742,7 +743,7 @@ static int http_action(
 		if (!default_port)
 			return -1;
 
-		if ((ret = gitno_extract_host_and_port(&t->host, &t->port,
+		if ((ret = gitno_extract_host_and_port(&t->host, &t->port, &t->user_from_url,
 				url, default_port)) < 0)
 			return ret;
 
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 808f6af..544e52f 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -788,7 +788,7 @@ static int winhttp_connect(
 		t->use_ssl = 1;
 	}
 
-	if ((ret = gitno_extract_host_and_port(&t->host, &t->port, url, default_port)) < 0)
+	if ((ret = gitno_extract_host_and_port(&t->host, &t->port, &t->parent.user_from_url, url, default_port)) < 0)
 		return ret;
 
 	t->path = strchr(url, '/');