From patchwork Thu Mar 3 21:14:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 37602 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 3A39127BBEA; Thu, 3 Mar 2022 21:30:39 +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=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable 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 02FD827BBE9 for ; Thu, 3 Mar 2022 21:30:39 +0000 (GMT) Received: from localhost ([::1]:32956 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPt1x-0001af-QX for patchwork@mira.cbaines.net; Thu, 03 Mar 2022 16:30:37 -0500 Received: from eggs.gnu.org ([209.51.188.92]:35386) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPsnv-0005ft-Lr for guix-patches@gnu.org; Thu, 03 Mar 2022 16:16:09 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50055) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPsnr-0007Io-Fn for guix-patches@gnu.org; Thu, 03 Mar 2022 16:16:05 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nPsnr-0002Hb-C2 for guix-patches@gnu.org; Thu, 03 Mar 2022 16:16:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#54241] [PATCH 3/4] http-client: Correctly handle redirects when #:keep-alive? #t. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 03 Mar 2022 21:16:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54241 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54241@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 54241-submit@debbugs.gnu.org id=B54241.16463421298696 (code B ref 54241); Thu, 03 Mar 2022 21:16:03 +0000 Received: (at 54241) by debbugs.gnu.org; 3 Mar 2022 21:15:29 +0000 Received: from localhost ([127.0.0.1]:43947 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nPsnJ-0002G7-4z for submit@debbugs.gnu.org; Thu, 03 Mar 2022 16:15:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57770) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nPsnF-0002FQ-Us for 54241@debbugs.gnu.org; Thu, 03 Mar 2022 16:15:26 -0500 Received: from [2001:470:142:3::e] (port=58926 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPsnA-00079I-Oz; Thu, 03 Mar 2022 16:15:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=3FOgxt6RAntKXgIo3ctekWMUYLNuoI7nrkNNHJj7ILo=; b=UETos/z1a/NobplX+7K7 k/BWR4KDemZttuqwBGsmlqIyX2+2VXhsHdU/Swn5I2TJvlO+4RKkXSgoj0Krrmdd2QmVlyvU2IgUf slTxcRUqyPR935THz3526G7drgxlIoxpfPa4nDPpLxzseC9gAIzIhXCRJIuKbAeYbheXOPbmUud+o buPuCtyZU84uPtMWC1uWhgFVguB3JAITR21K/IYi4o0OnD3DeoYR7ktDYNAKlHorQqrajHM/7kHOE G2QNTWDuxZzVNongb47zeW6hnDJ8IqPFqOjF4kNA5asxogbbWu0LFj2HaNg4ew1K7AVVpuqrEtKyU +CXXViqfP/aDcQ==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:50082 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPsn9-0006KA-9e; Thu, 03 Mar 2022 16:15:20 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Thu, 3 Mar 2022 22:14:43 +0100 Message-Id: <20220303211444.19928-3-ludo@gnu.org> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20220303211444.19928-1-ludo@gnu.org> References: <20220303211444.19928-1-ludo@gnu.org> 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 Previously PORT would be closed unconditionally, which broke redirects when #:keep-alive? #t is given. * guix/http-client.scm (http-fetch): Make 'port' a parameter of 'loop'. Upon 3xx responses, do not close PORT is KEEP-ALIVE? is true, but consume RESP's body. Add second argument to 'loop'. --- guix/http-client.scm | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/guix/http-client.scm b/guix/http-client.scm index 4b01e31165..4784497e5f 100644 --- a/guix/http-client.scm +++ b/guix/http-client.scm @@ -102,12 +102,12 @@ (define* (http-fetch uri #:key port (text? #f) (buffered? #t) Raise an '&http-get-error' condition if downloading fails." (let loop ((uri (if (string? uri) (string->uri uri) - uri))) - (let ((port (or port (open-connection uri - #:verify-certificate? - verify-certificate? - #:timeout timeout))) - (headers (match (uri-userinfo uri) + uri)) + (port (or port (open-connection uri + #:verify-certificate? + verify-certificate? + #:timeout timeout)))) + (let ((headers (match (uri-userinfo uri) ((? string? str) (cons (cons 'Authorization (string-append "Basic " @@ -131,11 +131,19 @@ (define* (http-fetch uri #:key port (text? #f) (buffered? #t) 303 ; see other 307 ; temporary redirection 308) ; permanent redirection - (let ((uri (resolve-uri-reference (response-location resp) uri))) - (close-port port) + (let ((host (uri-host uri)) + (uri (resolve-uri-reference (response-location resp) uri))) + (if keep-alive? + (dump-port data (%make-void-port "w0") + (response-content-length resp)) + (close-port port)) (format log-port (G_ "following redirection to `~a'...~%") (uri->string uri)) - (loop uri))) + (loop uri + (and keep-alive? + (or (not (uri-host uri)) + (string=? host (uri-host uri))) + port)))) (else (raise (condition (&http-get-error (uri uri)