From patchwork Thu Sep 29 14:07:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Hesse X-Patchwork-Id: 2083 Return-Path: Delivered-To: patchwork@archlinux.org Received: from mail.archlinux.org [2a01:4f9:c010:3052::1] by patchwork.archlinux.org with IMAP (fetchmail-6.4.33) for (single-drop); Thu, 29 Sep 2022 14:08:13 +0000 (UTC) Received: from mail.archlinux.org by mail.archlinux.org with LMTP id QKdZHsymNWNkRAkAK+/4rw (envelope-from ) for ; Thu, 29 Sep 2022 14:08:12 +0000 Received: from lists.archlinux.org (lists.archlinux.org [IPv6:2a01:4f9:c010:9eb4::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.archlinux.org (Postfix) with ESMTPS id 0411314FA004; Thu, 29 Sep 2022 14:08:11 +0000 (UTC) Received: from lists.archlinux.org (localhost [127.0.0.1]) by lists.archlinux.org (Postfix) with ESMTP id D28944E65BC; Thu, 29 Sep 2022 14:08:08 +0000 (UTC) Received: from mx.mylinuxtime.de (mx.mylinuxtime.de [195.201.174.144]) by lists.archlinux.org (Postfix) with ESMTPS id C1EEB4E659E for ; Thu, 29 Sep 2022 14:08:04 +0000 (UTC) Received: from leda.eworm.de (unknown [194.36.25.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx.mylinuxtime.de (Postfix) with ESMTPSA id 3EE07200C1D; Thu, 29 Sep 2022 16:08:04 +0200 (CEST) Received: by leda.eworm.de (Postfix, from userid 1000) id C25DA18699D; Thu, 29 Sep 2022 16:08:03 +0200 (CEST) From: Christian Hesse To: Discussion list for pacman development Subject: [PATCH v2 1/1] support http header 'Cache-Control: no-cache' for soft failure Date: Thu, 29 Sep 2022 16:07:59 +0200 Message-Id: <20220929140759.59575-1-list@eworm.de> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220919125210.63357-1-list@eworm.de> References: <20220919125210.63357-1-list@eworm.de> MIME-Version: 1.0 Message-ID-Hash: TYYOY2PLWLGREMZPIJAPLBTQIBRWVL5J X-Message-ID-Hash: TYYOY2PLWLGREMZPIJAPLBTQIBRWVL5J X-MailFrom: eworm@leda.eworm.de X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Christian Hesse X-Mailman-Version: 3.3.5 Precedence: list List-Id: Discussion list for pacman development Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Authentication-Results: mail.archlinux.org; dkim=none; dmarc=none; spf=pass (mail.archlinux.org: domain of pacman-dev-bounces@lists.archlinux.org designates 2a01:4f9:c010:9eb4::1 as permitted sender) smtp.mailfrom=pacman-dev-bounces@lists.archlinux.org X-Rspamd-Queue-Id: 0411314FA004 X-Spamd-Result: default: False [0.79 / 15.00]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; RCVD_IN_DNSWL_MED(-0.20)[2a01:4f9:c010:9eb4::1:from]; MAILLIST(-0.20)[mailman]; R_SPF_ALLOW(-0.20)[+ip6:2a01:4f9:c010:9eb4::1:c]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; NEURAL_HAM(-0.00)[-0.998]; ARC_NA(0.00)[]; R_DKIM_NA(0.00)[]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; ASN(0.00)[asn:24940, ipnet:2a01:4f9::/32, country:DE]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_TWO(0.00)[2]; TO_DN_ALL(0.00)[]; RCVD_TLS_LAST(0.00)[]; RCVD_COUNT_FIVE(0.00)[5]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_NEQ_ENVFROM(0.00)[list@eworm.de,pacman-dev-bounces@lists.archlinux.org]; FROM_HAS_DN(0.00)[]; DMARC_NA(0.00)[eworm.de: no valid DMARC record]; PREVIOUSLY_DELIVERED(0.00)[pacman-dev@lists.archlinux.org]; FORGED_SENDER_MAILLIST(0.00)[] X-Rspamd-Server: mail.archlinux.org From: Christian Hesse The idea has been borrowed from cloud providers' reverse proxies and load balancers. These accept http headers to control caching behaviour, even to control 'negative caching' when error codes are returned. By setting the http header 'Cache-Control: no-cache' when returning with the status code 404 (not found) the server can indicate that this is a soft failure. No error message is shown, and server's error count is not increased. This can be used by servers that are not expected to be complete, for example when serving a local cache [0]. In nginx this can be achived by acting on error_page and sending the extra header by adding a directive: server { listen 8080 default_server; root /var/cache/pacman/pkg; error_page 404 = @no-cache; location @no-cache { add_header Cache-Control "no-cache" always; } } Also this is a perfect match for pacredir [1]. [0] https://wiki.archlinux.org/title/Pacman/Tips_and_tricks#Network_shared_pacman_cache [1] https://git.eworm.de/cgit/pacredir/about/ Signed-off-by: Christian Hesse --- lib/libalpm/dload.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index 4fa17b35..74462bba 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -274,8 +274,10 @@ static size_t dload_parseheader_cb(void *ptr, size_t size, size_t nmemb, void *u { size_t realsize = size * nmemb; const char *fptr, *endptr = NULL; + const char * const cc_header = "Cache-Control:"; const char * const cd_header = "Content-Disposition:"; const char * const fn_key = "filename="; + const char * const nc_key = "no-cache"; struct dload_payload *payload = (struct dload_payload *)user; long respcode; @@ -302,6 +304,15 @@ static size_t dload_parseheader_cb(void *ptr, size_t size, size_t nmemb, void *u } } + /* By setting the http header 'Cache-Control: no-cache' the server can indicate + that this is a soft failure which should not be cached. No error message is + shown, and server's error count is not increased. */ + if(_alpm_raw_ncmp(cc_header, ptr, strlen(cc_header)) == 0) { + if(strstr(ptr, nc_key)) { + payload->errors_ok = 1; + } + } + curl_easy_getinfo(payload->curl, CURLINFO_RESPONSE_CODE, &respcode); if(payload->respcode != respcode) { payload->respcode = respcode;