Merge branch 'jk/http-auth-redirects' into maint
[gitweb.git] / http.c
diff --git a/http.c b/http.c
index 6bb2e456ff7a26f33038bde8acc4dd9089cbb858..3447945ee4f4e0e4b273d979ea1805b4c0f63b4c 100644 (file)
--- a/http.c
+++ b/http.c
@@ -45,7 +45,7 @@ static long curl_low_speed_time = -1;
 static int curl_ftp_no_epsv;
 static const char *curl_http_proxy;
 static const char *curl_cookie_file;
-static struct credential http_auth = CREDENTIAL_INIT;
+struct credential http_auth = CREDENTIAL_INIT;
 static int http_proactive_auth;
 static const char *user_agent;
 
@@ -806,7 +806,6 @@ int handle_curl_result(struct slot_results *results)
                        credential_reject(&http_auth);
                        return HTTP_NOAUTH;
                } else {
-                       credential_fill(&http_auth);
                        return HTTP_REAUTH;
                }
        } else {
@@ -895,17 +894,81 @@ static int http_request(const char *url,
                curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE,
                                options->content_type);
 
+       if (options && options->effective_url)
+               curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
+                               options->effective_url);
+
        curl_slist_free_all(headers);
        strbuf_release(&buf);
 
        return ret;
 }
 
+/*
+ * Update the "base" url to a more appropriate value, as deduced by
+ * redirects seen when requesting a URL starting with "url".
+ *
+ * The "asked" parameter is a URL that we asked curl to access, and must begin
+ * with "base".
+ *
+ * The "got" parameter is the URL that curl reported to us as where we ended
+ * up.
+ *
+ * Returns 1 if we updated the base url, 0 otherwise.
+ *
+ * Our basic strategy is to compare "base" and "asked" to find the bits
+ * specific to our request. We then strip those bits off of "got" to yield the
+ * new base. So for example, if our base is "http://example.com/foo.git",
+ * and we ask for "http://example.com/foo.git/info/refs", we might end up
+ * with "https://other.example.com/foo.git/info/refs". We would want the
+ * new URL to become "https://other.example.com/foo.git".
+ *
+ * Note that this assumes a sane redirect scheme. It's entirely possible
+ * in the example above to end up at a URL that does not even end in
+ * "info/refs".  In such a case we simply punt, as there is not much we can
+ * do (and such a scheme is unlikely to represent a real git repository,
+ * which means we are likely about to abort anyway).
+ */
+static int update_url_from_redirect(struct strbuf *base,
+                                   const char *asked,
+                                   const struct strbuf *got)
+{
+       const char *tail;
+       size_t tail_len;
+
+       if (!strcmp(asked, got->buf))
+               return 0;
+
+       if (prefixcmp(asked, base->buf))
+               die("BUG: update_url_from_redirect: %s is not a superset of %s",
+                   asked, base->buf);
+
+       tail = asked + base->len;
+       tail_len = strlen(tail);
+
+       if (got->len < tail_len ||
+           strcmp(tail, got->buf + got->len - tail_len))
+               return 0; /* insane redirect scheme */
+
+       strbuf_reset(base);
+       strbuf_add(base, got->buf, got->len - tail_len);
+       return 1;
+}
+
 static int http_request_reauth(const char *url,
                               void *result, int target,
                               struct http_get_options *options)
 {
        int ret = http_request(url, result, target, options);
+
+       if (options && options->effective_url && options->base_url) {
+               if (update_url_from_redirect(options->base_url,
+                                            url, options->effective_url)) {
+                       credential_from_url(&http_auth, options->base_url->buf);
+                       url = options->effective_url->buf;
+               }
+       }
+
        if (ret != HTTP_REAUTH)
                return ret;
 
@@ -924,6 +987,9 @@ static int http_request_reauth(const char *url,
                        die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
                }
        }
+
+       credential_fill(&http_auth);
+
        return http_request(url, result, target, options);
 }
 
@@ -1006,7 +1072,7 @@ static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
        strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
        tmp = strbuf_detach(&buf, NULL);
 
-       if (http_get_file(url, tmp, 0) != HTTP_OK) {
+       if (http_get_file(url, tmp, NULL) != HTTP_OK) {
                error("Unable to get pack index %s", url);
                free(tmp);
                tmp = NULL;