Received: from gtux.hallam.dk (gtux.hallam.dk [185.113.128.35])
	by gbr-app-1.alpinelinux.org (Postfix) with ESMTPS id B3F5B225A92
	for <~alpine/apk-tools@lists.alpinelinux.org>; Tue,  4 Feb 2025 11:34:59 +0000 (UTC)
Received: (qmail 8586 invoked from network); 4 Feb 2025 11:34:56 -0000
Received: from umbriel.sconet (umbriel.sconet [192.168.0.31])
  by raphael.sconet ([192.168.0.12])
  with SMTP via TCP; 04 Feb 2025 11:34:56 -0000
Received: (nullmailer pid 5171 invoked by uid 1010);
	Tue, 04 Feb 2025 11:34:56 -0000
Date: Tue, 4 Feb 2025 12:34:56 +0100
From: John Hallam <sw@j.hallam.dk>
To: ~alpine/apk-tools@lists.alpinelinux.org
Subject: [PATCH] libfetch: allow tolerant date parsing
Message-ID: <Z6H7YCJsDGaH33NF@umbriel.sconet>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

apk's libfetch is strict when interpreting timestamps in
e.g. Last-Modified headers;  other tools and web servers (e.g. Debian's
apt-cacher-ng) are lax in generation.

The patch below fixes interaction with apt-cacher-ng and allows easy
adding of other obsolete timestamp patterns.

Built and tested with apk-tools 2.14.9.

Best wishes

     John Hallam


--- a/libfetch/http.c
+++ b/libfetch/http.c
@@ -479,11 +479,21 @@
 {
 	char *locale, *r;
 	struct tm tm;
+        int i;
+	/* XXX should add support for date-2 and date-3 */
+	const char *fmts[] ={ "%a, %d %b %Y %H:%M:%S GMT",
+			      "%a %b %d %H:%M:%S %Y",
+			      NULL,	
+	};
 
 	locale = strdupa(setlocale(LC_TIME, NULL));
 	setlocale(LC_TIME, "C");
-	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
-	/* XXX should add support for date-2 and date-3 */
+	i=0;
+	while(fmts[i] != NULL) {
+		r = strptime(p, fmts[i], &tm);
+		if(r != NULL) break;
+		i++;
+	}
 	setlocale(LC_TIME, locale);
 	if (r == NULL)
 		return (-1);