@matheusbsbs / matheus-bagliano / commits / be4a015b0f
Fork 0

http-fetch.c: consolidate code to detect missing fetch target

At a handful places we check two error codes from curl library to see if the file we asked was missing from the remote (e.g. we asked for a loose object when it is in a pack) to decide what to do next. This consolidates the check into a single function. NOTE: the original did not check for HTTP_RETURNED_ERROR when error code is 404, but this version does to make sure 404 is from HTTP and not some other protcol. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 16, 2006 at 10:58 UTC be4a015b0fed2bd96e509f45b80b63db056b9ca9
1 file changed +15 -8
http-fetch.c
+15 -8
@@ -144,6 +144,17 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
144 return size;
145 }
146
147 +static int missing__target(int code, int result)
148 +{
149 + return /* file:// URL -- do we ever use one??? */
150 + (result == CURLE_FILE_COULDNT_READ_FILE) ||
151 + /* http:// and https:// URL */
152 + (code == 404 && result == CURLE_HTTP_RETURNED_ERROR)
153 + ;
154 +}
155 +
156 +#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
157 +
158 static void fetch_alternates(const char *base);
159
160 static void process_object_response(void *callback_data);
@@ -323,8 +334,7 @@ static void process_object_response(void *callback_data)
334 obj_req->state = COMPLETE;
335
336 /* Use alternates if necessary */
326 - if (obj_req->http_code == 404 ||
327 - obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE) {
337 + if (missing_target(obj_req)) {
338 fetch_alternates(alt->base);
339 if (obj_req->repo->next != NULL) {
340 obj_req->repo =
@@ -537,8 +547,7 @@ static void process_alternates_response(void *callback_data)
547 return;
548 }
549 } else if (slot->curl_result != CURLE_OK) {
540 - if (slot->http_code != 404 &&
541 - slot->curl_result != CURLE_FILE_COULDNT_READ_FILE) {
550 + if (!missing_target(slot)) {
551 got_alternates = -1;
552 return;
553 }
@@ -941,8 +950,7 @@ static int fetch_indices(struct alt_base *repo)
950 if (start_active_slot(slot)) {
951 run_active_slot(slot);
952 if (results.curl_result != CURLE_OK) {
944 - if (results.http_code == 404 ||
945 - results.curl_result == CURLE_FILE_COULDNT_READ_FILE) {
953 + if (missing_target(&results)) {
954 repo->got_indices = 1;
955 free(buffer.buffer);
956 return 0;
@@ -1123,8 +1131,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
1131 ret = error("Request for %s aborted", hex);
1132 } else if (obj_req->curl_result != CURLE_OK &&
1133 obj_req->http_code != 416) {
1126 - if (obj_req->http_code == 404 ||
1127 - obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE)
1134 + if (missing_target(obj_req))
1135 ret = -1; /* Be silent, it is probably in a pack. */
1136 else
1137 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",