Skip to content

Commit 17966c0

Browse files
Eric Wonggitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43b8bba commit 17966c0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

http-walker.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,15 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
488488
req->localfile = -1;
489489
}
490490

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+
491500
if (obj_req->state == ABORTED) {
492501
ret = error("Request for %s aborted", hex);
493502
} else if (req->curl_result != CURLE_OK &&

http.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,8 +1855,19 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
18551855
unsigned char expn[4096];
18561856
size_t size = eltsize * nmemb;
18571857
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+
18601871
do {
18611872
ssize_t retval = xwrite(freq->localfile,
18621873
(char *) ptr + posn, size - posn);
@@ -1977,6 +1988,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
19771988
freq->slot = get_active_slot();
19781989

19791990
curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1991+
curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
19801992
curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
19811993
curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
19821994
curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);

0 commit comments

Comments
 (0)