diff options
-rw-r--r-- | src/transport.c | 46 | ||||
-rw-r--r-- | src/transports/winhttp.c | 18 | ||||
-rw-r--r-- | tests/transport/register.c | 66 |
3 files changed, 109 insertions, 21 deletions
diff --git a/src/transport.c b/src/transport.c index c0311fa90..d390e1422 100644 --- a/src/transport.c +++ b/src/transport.c @@ -46,34 +46,37 @@ static git_vector custom_transports = GIT_VECTOR_INIT; #define GIT_TRANSPORT_COUNT (sizeof(transports)/sizeof(transports[0])) - 1 -static int transport_find_fn( - git_transport_cb *out, - const char *url, - void **param) +static transport_definition * transport_find_by_url(const char *url) { size_t i = 0; - transport_definition *d, *definition = NULL; + transport_definition *d; /* Find a user transport who wants to deal with this URI */ git_vector_foreach(&custom_transports, i, d) { if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) { - definition = d; - break; + return d; } } /* Find a system transport for this URI */ - if (!definition) { - for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) { - d = &transports[i]; - - if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) { - definition = d; - break; - } + for (i = 0; i < GIT_TRANSPORT_COUNT; ++i) { + d = &transports[i]; + + if (strncasecmp(url, d->prefix, strlen(d->prefix)) == 0) { + return d; } } + return NULL; +} + +static int transport_find_fn( + git_transport_cb *out, + const char *url, + void **param) +{ + transport_definition *definition = transport_find_by_url(url); + #ifdef GIT_WIN32 /* On Windows, it might not be possible to discern between absolute local * and ssh paths - first check if this is a valid local path that points @@ -89,12 +92,15 @@ static int transport_find_fn( /* It could be a SSH remote path. Check to see if there's a : * SSH is an unsupported transport mechanism in this version of libgit2 */ - if (!definition && strrchr(url, ':')) -#ifdef GIT_SSH - definition = &ssh_transport_definition; -#else - definition = &dummy_transport_definition; + if (!definition && strrchr(url, ':')) { + // re-search transports again with ssh:// as url so that we can find a third party ssh transport + definition = transport_find_by_url("ssh://"); +#ifndef GIT_SSH + if (!definition) { + definition = &dummy_transport_definition; + } #endif + } #ifndef GIT_WIN32 /* Check to see if the path points to a file on the local file system */ diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c index 836c488cc..6523086da 100644 --- a/src/transports/winhttp.c +++ b/src/transports/winhttp.c @@ -35,7 +35,8 @@ #define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE 109 #define CACHED_POST_BODY_BUF_SIZE 4096 #define UUID_LENGTH_CCH 32 - +#define TIMEOUT_INFINITE -1 +#define DEFAULT_CONNECT_TIMEOUT 60000 #ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0 #endif @@ -212,6 +213,8 @@ static int winhttp_stream_connect(winhttp_stream *s) BOOL peerdist = FALSE; int error = -1; unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS; + int default_timeout = TIMEOUT_INFINITE; + int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT; /* Prepare URL */ git_buf_printf(&buf, "%s%s", t->connection_data.path, s->service_url); @@ -240,6 +243,11 @@ static int winhttp_stream_connect(winhttp_stream *s) goto on_error; } + if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) { + giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP"); + goto on_error; + } + /* Set proxy if necessary */ if (git_remote__get_http_proxy(t->owner->owner, !!t->connection_data.use_ssl, &proxy_url) < 0) goto on_error; @@ -467,6 +475,8 @@ static int winhttp_connect( int32_t port; const char *default_port = "80"; int error = -1; + int default_timeout = TIMEOUT_INFINITE; + int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT; /* Prepare port */ if (git__strtol32(&port, t->connection_data.port, NULL, 10) < 0) @@ -491,6 +501,12 @@ static int winhttp_connect( goto on_error; } + if (!WinHttpSetTimeouts(t->session, default_timeout, default_connect_timeout, default_timeout, default_timeout)) { + giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP"); + goto on_error; + } + + /* Establish connection */ t->connection = WinHttpConnect( t->session, diff --git a/tests/transport/register.c b/tests/transport/register.c new file mode 100644 index 000000000..937194c1c --- /dev/null +++ b/tests/transport/register.c @@ -0,0 +1,66 @@ +#include "clar_libgit2.h" +#include "git2/sys/transport.h" + +static git_transport _transport = GIT_TRANSPORT_INIT; + +static int dummy_transport(git_transport **transport, git_remote *owner, void *param) +{ + *transport = &_transport; + GIT_UNUSED(owner); + GIT_UNUSED(param); + return 0; +} + +void test_transport_register__custom_transport(void) +{ + git_transport *transport; + + cl_git_pass(git_transport_register("something", dummy_transport, NULL)); + + cl_git_pass(git_transport_new(&transport, NULL, "something://somepath")); + + cl_assert(transport == &_transport); + + cl_git_pass(git_transport_unregister("something")); +} + +void test_transport_register__custom_transport_error_doubleregister(void) +{ + cl_git_pass(git_transport_register("something", dummy_transport, NULL)); + + cl_git_fail_with(git_transport_register("something", dummy_transport, NULL), GIT_EEXISTS); + + cl_git_pass(git_transport_unregister("something")); +} + +void test_transport_register__custom_transport_error_remove_non_existing(void) +{ + cl_git_fail_with(git_transport_unregister("something"), GIT_ENOTFOUND); +} + +void test_transport_register__custom_transport_ssh(void) +{ + git_transport *transport; + +#ifndef GIT_SSH + cl_git_fail_with(git_transport_new(&transport, NULL, "ssh://somehost:somepath"), -1); + cl_git_fail_with(git_transport_new(&transport, NULL, "git@somehost:somepath"), -1); +#else + cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath")); +#endif + + cl_git_pass(git_transport_register("ssh", dummy_transport, NULL)); + + cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath")); + + cl_assert(transport == &_transport); + + cl_git_pass(git_transport_unregister("ssh")); + +#ifndef GIT_SSH + cl_git_fail_with(git_transport_new(&transport, NULL, "ssh://somehost:somepath"), -1); + cl_git_fail_with(git_transport_new(&transport, NULL, "git@somehost:somepath"), -1); +#else + cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath")); +#endif +} |