Stash username from url (but don't use it yet)
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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, '/');