http: avoid disconnecting on 404s for loose objects

404s are common when fetching loose objects on static HTTP servers, and reestablishing a connection for every single 404 adds additional latency. Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Wong committed Jul 11, 2016 at 20:51 UTC 17966c0a63d25b1cc2dd1e98d30873e643bd581f
2 files changed +23 -2
http-walker.c
+9
@@ -488,6 +488,15 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
488 req->localfile = -1;
489 }
490
491 + /*
492 + * we turned off CURLOPT_FAILONERROR to avoid losing a
493 + * persistent connection and got CURLE_OK.
494 + */
495 + if (req->http_code == 404 && req->curl_result == CURLE_OK &&
496 + (starts_with(req->url, "http://") ||
497 + starts_with(req->url, "https://")))
498 + req->curl_result = CURLE_HTTP_RETURNED_ERROR;
499 +
500 if (obj_req->state == ABORTED) {
501 ret = error("Request for %s aborted", hex);
502 } else if (req->curl_result != CURLE_OK &&
http.c
+14 -2
@@ -1855,8 +1855,19 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
1855 unsigned char expn[4096];
1856 size_t size = eltsize * nmemb;
1857 int posn = 0;
1858 - struct http_object_request *freq =
1859 - (struct http_object_request *)data;
1858 + struct http_object_request *freq = data;
1859 + struct active_request_slot *slot = freq->slot;
1860 +
1861 + if (slot) {
1862 + CURLcode c = curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE,
1863 + &slot->http_code);
1864 + if (c != CURLE_OK)
1865 + die("BUG: curl_easy_getinfo for HTTP code failed: %s",
1866 + curl_easy_strerror(c));
1867 + if (slot->http_code >= 400)
1868 + return size;
1869 + }
1870 +
1871 do {
1872 ssize_t retval = xwrite(freq->localfile,
1873 (char *) ptr + posn, size - posn);
@@ -1977,6 +1988,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
1988 freq->slot = get_active_slot();
1989
1990 curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1991 + curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
1992 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1993 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
1994 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);