From patchwork Mon Feb 7 09:06:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xinglu Chen X-Patchwork-Id: 37056 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id DA91C27BBEA; Mon, 7 Feb 2022 10:15:06 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FROM_SUSPICIOUS_NTLD,MAILING_LIST_MULTI,PDS_OTHER_BAD_TLD, SPF_HELO_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 97D5327BBE9 for ; Mon, 7 Feb 2022 10:15:06 +0000 (GMT) Received: from localhost ([::1]:33874 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nH133-00029i-OV for patchwork@mira.cbaines.net; Mon, 07 Feb 2022 05:15:05 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40374) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nH00B-00068E-OV for guix-patches@gnu.org; Mon, 07 Feb 2022 04:08:04 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:47041) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nH00A-0004Jj-Kq for guix-patches@gnu.org; Mon, 07 Feb 2022 04:08:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nH00A-0002ur-08; Mon, 07 Feb 2022 04:08:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#53818] [PATCH v2 2/7] http-client: Make 'http-fetch/cached' take '#:headers' argument. Resent-From: Xinglu Chen Original-Sender: "Debbugs-submit" Resent-CC: maximedevos@telenet.be, guix-patches@gnu.org Resent-Date: Mon, 07 Feb 2022 09:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53818 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 53818@debbugs.gnu.org Cc: Maxime Devos X-Debbugs-Original-Xcc: Maxime Devos Received: via spool by 53818-submit@debbugs.gnu.org id=B53818.164422483911116 (code B ref 53818); Mon, 07 Feb 2022 09:08:01 +0000 Received: (at 53818) by debbugs.gnu.org; 7 Feb 2022 09:07:19 +0000 Received: from localhost ([127.0.0.1]:40925 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nGzzT-0002t1-1e for submit@debbugs.gnu.org; Mon, 07 Feb 2022 04:07:19 -0500 Received: from h178-251-242-94.cust.a3fiber.se ([178.251.242.94]:55268 helo=mail.yoctocell.xyz) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nGzz9-0002re-7U for 53818@debbugs.gnu.org; Mon, 07 Feb 2022 04:07:03 -0500 From: Xinglu Chen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yoctocell.xyz; s=mail; t=1644224813; bh=akL6Y2/X0FEbcEKilz3Py/WZu638Uz61mmUyiLjh3JI=; h=From:To:Subject:In-Reply-To:References:Date; b=daLPuNj11/d24yygmKE0YHg9U889uuPeRQdfSAINyfd5ZNVeJ/4wOrpqB2gi3Efki a4MZkFUsxvUPiNcK5RGFo0P7QqjCswJlfltMdkYv4+ROuiUAreSuxPRNFmSfFgnUfM NFwbRl5nreY3hiric5qlJskiU2c6fMyVBrCAlR2g= In-Reply-To: References: Message-Id: Date: Mon, 07 Feb 2022 10:06:52 +0100 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/http-client.scm (http-fetch/cached): Take ‘#:headers’ keyword argument. --- guix/http-client.scm | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/guix/http-client.scm b/guix/http-client.scm index 10bc278023..058f09852f 100644 --- a/guix/http-client.scm +++ b/guix/http-client.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2015 Mark H Weaver ;;; Copyright © 2012, 2015 Free Software Foundation, Inc. ;;; Copyright © 2017 Tobias Geerinckx-Rice +;;; Copyright © 2022 Xinglu Chen ;;; ;;; This file is part of GNU Guix. ;;; @@ -283,9 +284,11 @@ (define* (http-fetch/cached uri #:key (ttl (%http-cache-ttl)) text? (write-cache dump-port) (cache-miss (const #t)) (log-port (current-error-port)) - (timeout 10)) + (timeout 10) + (headers '((user-agent . "GNU Guile")))) "Like 'http-fetch', return an input port, but cache its contents in -~/.cache/guix. The cache remains valid for TTL seconds. +~/.cache/guix. The cache remains valid for TTL seconds. HEADERS is an alist +of extra HTTP headers. The cache time will automatically be added to HEADERS, so there is no need to specify it. Call WRITE-CACHE with the HTTP input port and the cache output port to write the data to cache. Call CACHE-MISS with URI just before fetching data from @@ -300,8 +303,8 @@ (define cache-time (and cache-port (stat:mtime (stat cache-port)))) - (define headers - `((user-agent . "GNU Guile") + (define headers* + `(,@headers ,@(if cache-time `((if-modified-since . ,(time-utc->date (make-time time-utc 0 cache-time)))) @@ -316,7 +319,7 @@ (define headers (raise c)))) (let ((port (http-fetch uri #:text? text? #:log-port log-port - #:headers headers #:timeout timeout))) + #:headers headers* #:timeout timeout))) (cache-miss uri) (mkdir-p (dirname file)) (when cache-port