http: attempt updating base URL only if no error

http.c supports HTTP redirects of the form http://foo/info/refs?service=git-upload-pack -> http://anything -> http://bar/info/refs?service=git-upload-pack (that is to say, as long as the Git part of the path and the query string is preserved in the final redirect destination, the intermediate steps can have any URL). However, if one of the intermediate steps results in an HTTP exception, a confusing "unable to update url base from redirection" message is printed instead of a Curl error message with the HTTP exception code. This was introduced by 2 commits. Commit c93c92f ("http: update base URLs when we see redirects", 2013-09-28) introduced a best-effort optimization that required checking if only the "base" part of the URL differed between the initial request and the final redirect destination, but it performed the check before any HTTP status checking was done. If something went wrong, the normal code path was still followed, so this did not cause any confusing error messages until commit 6628eb4 ("http: always update the base URL for redirects", 2016-12-06), which taught http to die if the non-"base" part of the URL differed. Therefore, teach http to check the HTTP status before attempting to check if only the "base" part of the URL differed. This commit teaches http_request_reauth to return early without updating options->base_url upon an error; the only invoker of this function that passes a non-NULL "options" is remote-curl.c (through "http_get_strbuf"), which only uses options->base_url for an informational message in the situations that this commit cares about (that is, when the return value is not HTTP_OK). The included test checks that the redirect scheme at the beginning of this commit message works, and that returning a 502 in the middle of the redirect scheme produces the correct result. Note that this is different from the test in commit 6628eb4 ("http: always update the base URL for redirects", 2016-12-06) in that this commit tests that a Git-shaped URL (http://.../info/refs?service=git-upload-pack) works, whereas commit 6628eb4 tests that a non-Git-shaped URL (http://.../info/refs/foo?service=git-upload-pack) does not work (even though Git is processing that URL) and is an error that is fatal, not silently swallowed. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Feb 27, 2017 at 18:53 UTC 8e27391a5fdc9194c4ed3ed6c64ec4750a1a08b5
3 files changed +21
http.c
+3
@@ -1727,6 +1727,9 @@ static int http_request_reauth(const char *url,
1727 {
1728 int ret = http_request(url, result, target, options);
1729
1730 + if (ret != HTTP_OK && ret != HTTP_REAUTH)
1731 + return ret;
1732 +
1733 if (options && options->effective_url && options->base_url) {
1734 if (update_url_from_redirect(options->base_url,
1735 url, options->effective_url)) {
t/lib-httpd/apache.conf
+9
@@ -133,6 +133,15 @@ RewriteRule ^/ftp-redir/(.*)$ ftp://localhost:1000/$1 [R=302]
133 RewriteRule ^/loop-redir/x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-(.*) /$1 [R=302]
134 RewriteRule ^/loop-redir/(.*)$ /loop-redir/x-$1 [R=302]
135
136 +# redir-to/502/x?y -> really-redir-to?path=502/x&qs=y which returns 502
137 +# redir-to/x?y -> really-redir-to?path=x&qs=y -> x?y
138 +RewriteCond %{QUERY_STRING} ^(.*)$
139 +RewriteRule ^/redir-to/(.*)$ /really-redir-to?path=$1&qs=%1 [R=302]
140 +RewriteCond %{QUERY_STRING} ^path=502/(.*)&qs=(.*)$
141 +RewriteRule ^/really-redir-to$ - [R=502,L]
142 +RewriteCond %{QUERY_STRING} ^path=(.*)&qs=(.*)$
143 +RewriteRule ^/really-redir-to$ /%1?%2 [R=302]
144 +
145 # The first rule issues a client-side redirect to something
146 # that _doesn't_ look like a git repo. The second rule is a
147 # server-side rewrite, so that it turns out the odd-looking
t/t5550-http-fetch-dumb.sh
+9
@@ -378,5 +378,14 @@ test_expect_success 'http-alternates triggers not-from-user protocol check' '
378 clone $HTTPD_URL/dumb/evil.git evil-user
379 '
380
381 +test_expect_success 'can redirect through non-"info/refs?service=git-upload-pack" URL' '
382 + git clone "$HTTPD_URL/redir-to/dumb/repo.git"
383 +'
384 +
385 +test_expect_success 'print HTTP error when any intermediate redirect throws error' '
386 + test_must_fail git clone "$HTTPD_URL/redir-to/502" 2> stderr &&
387 + test_i18ngrep "unable to access.*/redir-to/502" stderr
388 +'
389 +
390 stop_httpd
391 test_done