HTTP: handle "relative" redirects
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
diff --git a/src/netops.c b/src/netops.c
index 3257e77..7a61ef8 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -609,6 +609,9 @@ int gitno_connection_data_from_url(
data->use_ssl = true;
}
+ if (url[0] == '/')
+ default_port = data->use_ssl ? "443" : "80";
+
if (!default_port) {
giterr_set(GITERR_NET, "Unrecognized URL prefix");
goto cleanup;
@@ -618,6 +621,13 @@ int gitno_connection_data_from_url(
&data->host, &data->port, &data->user, &data->pass,
url, default_port);
+ if (url[0] == '/') {
+ /* Relative redirect; reuse original host name and port */
+ git__free(data->host);
+ data->host = original_host;
+ original_host = NULL;
+ }
+
if (!error) {
const char *path = strchr(url, '/');
size_t pathlen = strlen(path);
diff --git a/tests-clar/network/urlparse.c b/tests-clar/network/urlparse.c
index 8892781..274d7e9 100644
--- a/tests-clar/network/urlparse.c
+++ b/tests-clar/network/urlparse.c
@@ -125,6 +125,34 @@ void test_network_urlparse__connection_data_http_downgrade(void)
-1);
}
+void test_network_urlparse__connection_data_relative_redirect(void)
+{
+ cl_git_pass(gitno_connection_data_from_url(&conndata,
+ "http://foo.com/bar/baz/biff", NULL));
+ cl_git_pass(gitno_connection_data_from_url(&conndata,
+ "/zap/baz/biff?bam", NULL));
+ cl_assert_equal_s(conndata.host, "foo.com");
+ cl_assert_equal_s(conndata.port, "80");
+ cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam");
+ cl_assert_equal_p(conndata.user, NULL);
+ cl_assert_equal_p(conndata.pass, NULL);
+ cl_assert_equal_i(conndata.use_ssl, false);
+}
+
+void test_network_urlparse__connection_data_relative_redirect_ssl(void)
+{
+ cl_git_pass(gitno_connection_data_from_url(&conndata,
+ "https://foo.com/bar/baz/biff", NULL));
+ cl_git_pass(gitno_connection_data_from_url(&conndata,
+ "/zap/baz/biff?bam", NULL));
+ cl_assert_equal_s(conndata.host, "foo.com");
+ cl_assert_equal_s(conndata.port, "443");
+ cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam");
+ cl_assert_equal_p(conndata.user, NULL);
+ cl_assert_equal_p(conndata.pass, NULL);
+ cl_assert_equal_i(conndata.use_ssl, true);
+}
+
/* Run this under valgrind */
void test_network_urlparse__connection_data_cleanup(void)
{