git: bf599c03f09d - main - fetch(1): do not consider HTTP 5XX errors as soft failures

From: Eugene Grosbein <eugen_at_FreeBSD.org>
Date: Mon, 24 Jan 2022 07:38:35 UTC
The branch main has been updated by eugen:

URL: https://cgit.FreeBSD.org/src/commit/?id=bf599c03f09dea0f7e188e002b42d782af6841c3

commit bf599c03f09dea0f7e188e002b42d782af6841c3
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2022-01-24 07:35:49 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2022-01-24 07:38:26 +0000

    fetch(1): do not consider HTTP 5XX errors as soft failures
    
    This change fixes "fetch -a" looping forever on "502 Bad gateway"
    error and similar.
    
    MFC after:      1 month
---
 usr.bin/fetch/fetch.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/usr.bin/fetch/fetch.c b/usr.bin/fetch/fetch.c
index 3e24707d6021..55209538e49b 100644
--- a/usr.bin/fetch/fetch.c
+++ b/usr.bin/fetch/fetch.c
@@ -425,7 +425,7 @@ query_auth(struct url *URL)
  * Fetch a file
  */
 static int
-fetch(char *URL, const char *path)
+fetch(char *URL, const char *path, int *is_http)
 {
 	struct url *url;
 	struct url_stat us;
@@ -475,6 +475,9 @@ fetch(char *URL, const char *path)
 			strcpy(url->scheme, SCHEME_HTTP);
 	}
 
+	/* for both of http and https */
+	*is_http = strncmp(url->scheme, SCHEME_HTTP, sizeof(SCHEME_HTTP)) == 0;
+
 	/* common flags */
 	switch (family) {
 	case PF_INET:
@@ -911,7 +914,7 @@ main(int argc, char *argv[])
 	struct sigaction sa;
 	const char *p, *s;
 	char *end, *q;
-	int c, e, r;
+	int c, e, is_http, r;
 
 
 	while ((c = getopt_long(argc, argv,
@@ -1176,16 +1179,16 @@ main(int argc, char *argv[])
 
 		if (o_flag) {
 			if (o_stdout) {
-				e = fetch(*argv, "-");
+				e = fetch(*argv, "-", &is_http);
 			} else if (o_directory) {
 				asprintf(&q, "%s/%s", o_filename, p);
-				e = fetch(*argv, q);
+				e = fetch(*argv, q, &is_http);
 				free(q);
 			} else {
-				e = fetch(*argv, o_filename);
+				e = fetch(*argv, o_filename, &is_http);
 			}
 		} else {
-			e = fetch(*argv, p);
+			e = fetch(*argv, p, &is_http);
 		}
 
 		if (sigint)
@@ -1201,7 +1204,13 @@ main(int argc, char *argv[])
 			    && fetchLastErrCode != FETCH_MOVED
 			    && fetchLastErrCode != FETCH_URL
 			    && fetchLastErrCode != FETCH_RESOLV
-			    && fetchLastErrCode != FETCH_UNKNOWN)) {
+			    && fetchLastErrCode != FETCH_UNKNOWN
+			    && (is_http
+			    	&& fetchLastErrCode != FETCH_PROTO
+			    	&& fetchLastErrCode != FETCH_SERVER
+			    	&& fetchLastErrCode != FETCH_TEMP
+			    	&& fetchLastErrCode != FETCH_TIMEOUT
+			    ))) {
 				if (w_secs && v_level)
 					fprintf(stderr, "Waiting %ld seconds "
 					    "before retrying\n", w_secs);