From patchwork Thu Apr 28 10:22:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Attila Lendvai X-Patchwork-Id: 38978 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 13B0427BBEA; Thu, 28 Apr 2022 11:52:56 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS 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 8910927BBE9 for ; Thu, 28 Apr 2022 11:52:55 +0100 (BST) Received: from localhost ([::1]:60762 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nk1lW-0001TU-Iz for patchwork@mira.cbaines.net; Thu, 28 Apr 2022 06:52:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52754) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nk1MU-0007Wr-Nr for guix-patches@gnu.org; Thu, 28 Apr 2022 06:27:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:51562) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nk1MU-0007Dh-CA for guix-patches@gnu.org; Thu, 28 Apr 2022 06:27:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nk1MU-0005cq-96 for guix-patches@gnu.org; Thu, 28 Apr 2022 06:27:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54836] [PATCH v3] http-client: Factor out open-connection*, rename variables. References: <20220410133431.30058-1-attila@lendvai.name> In-Reply-To: <20220410133431.30058-1-attila@lendvai.name> Resent-From: Attila Lendvai Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 28 Apr 2022 10:27:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54836 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54836@debbugs.gnu.org Cc: Attila Lendvai Received: via spool by 54836-submit@debbugs.gnu.org id=B54836.165114159121580 (code B ref 54836); Thu, 28 Apr 2022 10:27:02 +0000 Received: (at 54836) by debbugs.gnu.org; 28 Apr 2022 10:26:31 +0000 Received: from localhost ([127.0.0.1]:45459 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nk1Ly-0005c0-UP for submit@debbugs.gnu.org; Thu, 28 Apr 2022 06:26:31 -0400 Received: from mail-ej1-f52.google.com ([209.85.218.52]:38536) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nk1Lw-0005bj-L5 for 54836@debbugs.gnu.org; Thu, 28 Apr 2022 06:26:29 -0400 Received: by mail-ej1-f52.google.com with SMTP id r13so8637756ejd.5 for <54836@debbugs.gnu.org>; Thu, 28 Apr 2022 03:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zuH42bxUCGQfE/N8m73Ld5cYGlr8bKiTowntEZeMA/c=; b=dmu8hHa7z6LZbhA8N/ngyOLYZ4D2VFajpZDopovFFIObZDF0PLzu8z1CuJ/yiAZZWl HocwfFQfGPK8z5fnF9p/1VgrxtE15CALUV9Z+e3zsEukP90NHTG0fi2m+2V15ArzODKN 4dMAM6ILVKkG2Clk7H+zhcR4bym8zLsuuF9ItNHsTpZNkmkudwebUHGxQ4QM4stp5wgw TWwKecqDX7Q56DMeol4qpwAYvnQMQ7DTLq9k25zDieLRJEqshV8+4KOb5q1AojUKHeib 9sCW2MfX4ZTE4eNkryVq/Y9q8TO4rKypiZjZlkbaifh1SUs6zQ89hp38tWDDPXlMlU3j hBIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=zuH42bxUCGQfE/N8m73Ld5cYGlr8bKiTowntEZeMA/c=; b=obn6tt+ukErqHfqElHIPEx1oI68idklZhcGb60mNQknHpnnYo8pZRsdt95PGbWn/ib Tbq4MAZ6q1wqjW8VoxStJrNxX9+xEgdD75LbiG1eo8GL3MRoQxBv1fZ3j9YSvulqD2+O FORQfo/4Qpw3TRyO4z6+fvFCy8oMfUpwwwA2sSCHtTimwDwJF9UVsonbiPzOtLbqBWwy 8fxfUSV/ebwfeT8jUkKXLuakYSXsb9g+ToKe6D2WJ0lDmLLUa36LV24flOQobdIKlT5S Mce1/1tkdZfD61R3EWAKPlKDl0dUmcuJisXHnMDtyJHURWJU796/8mcRD5tfbgmtLkC4 9dwA== X-Gm-Message-State: AOAM532Fw8TuUefqcaTgzZ0Cam0zzvQ+EVjUJZHDGlFjBQpGXuxZFzIJ ZHIkaWkSb/M4pciMlwZF7sSwbLlKrs8= X-Google-Smtp-Source: ABdhPJzmnVdOoSmOpQY/niIA4+Zb7xlvnu/49S7t3EwNHzkDeKa++VquNQfCJp34woXTMGCQza0Crw== X-Received: by 2002:a17:907:98eb:b0:6f3:ce56:c1a2 with SMTP id ke11-20020a17090798eb00b006f3ce56c1a2mr6740861ejc.173.1651141582668; Thu, 28 Apr 2022 03:26:22 -0700 (PDT) Received: from lelap.local (catv-89-132-245-188.catv.fixed.vodafone.hu. [89.132.245.188]) by smtp.gmail.com with ESMTPSA id v21-20020aa7d815000000b0042617ba63b8sm1313694edq.66.2022.04.28.03.26.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 03:26:22 -0700 (PDT) From: Attila Lendvai Date: Thu, 28 Apr 2022 12:22:34 +0200 Message-Id: <20220428102233.14558-1-attila@lendvai.name> X-Mailer: git-send-email 2.35.1 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 This is an idempotent refactor. * guix/http-client.scm (http-fetch): Introduce open-connection*. Rename some variables to turn programmer mistakes into compile time errors. --- v3: i have reordered the commits so that i can send this idempotent refactor. i think this would be a useful addition to master. it makes the code more defensive against future programmer mistakes, but other than that it shouldn't change the semantics. apply this as you see fit. the rest i'll do in the go importer's module. guix/http-client.scm | 66 ++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/guix/http-client.scm b/guix/http-client.scm index a367c41afa..6c61fd3d8e 100644 --- a/guix/http-client.scm +++ b/guix/http-client.scm @@ -100,15 +100,17 @@ (define* (http-fetch uri #:key port (text? #f) (buffered? #t) Write information about redirects to LOG-PORT. Raise an '&http-get-error' condition if downloading fails." - (define uri* + (define parsed-initial-uri (if (string? uri) (string->uri uri) uri)) - (let loop ((uri uri*) - (port (or port (open-connection uri* - #:verify-certificate? - verify-certificate? - #:timeout timeout)))) - (let ((headers (match (uri-userinfo uri) + (define (open-connection* uri) + (open-connection uri + #:verify-certificate? verify-certificate? + #:timeout timeout)) + + (let loop ((current-uri parsed-initial-uri) + (current-port (or port (open-connection parsed-initial-uri)))) + (let ((headers (match (uri-userinfo current-uri) ((? string? str) (cons (cons 'Authorization (string-append "Basic " @@ -116,10 +118,10 @@ (define uri* (string->utf8 str)))) headers)) (_ headers)))) - (unless (or buffered? (not (file-port? port))) - (setvbuf port 'none)) + (unless (or buffered? (not (file-port? current-port))) + (setvbuf current-port 'none)) (let*-values (((resp data) - (http-get uri #:streaming? #t #:port port + (http-get current-uri #:streaming? #t #:port current-port #:keep-alive? keep-alive? #:headers headers)) ((code) @@ -132,36 +134,34 @@ (define uri* 303 ; see other 307 ; temporary redirection 308) ; permanent redirection - (let ((host (uri-host uri)) - (uri (resolve-uri-reference (response-location resp) uri))) + (let ((host (uri-host current-uri)) + (new-uri (resolve-uri-reference (response-location resp) + current-uri))) (if keep-alive? (dump-port data (%make-void-port "w0") (response-content-length resp)) - (close-port port)) + (close-port current-port)) (format log-port (G_ "following redirection to `~a'...~%") - (uri->string uri)) - (loop uri + (uri->string new-uri)) + (loop new-uri (or (and keep-alive? - (or (not (uri-host uri)) - (string=? host (uri-host uri))) - port) - (open-connection uri - #:verify-certificate? - verify-certificate? - #:timeout timeout))))) + (or (not (uri-host new-uri)) + (string=? host (uri-host new-uri))) + current-port) + (open-connection* new-uri))))) (else (raise (condition (&http-get-error - (uri uri) - (code code) - (reason (response-reason-phrase resp)) - (headers (response-headers resp))) - (&message - (message - (format - #f - (G_ "~a: HTTP download failed: ~a (~s)") - (uri->string uri) code - (response-reason-phrase resp)))))))))))) + (uri current-uri) + (code code) + (reason (response-reason-phrase resp)) + (headers (response-headers resp))) + (&message + (message + (format + #f + (G_ "~a: HTTP download failed: ~a (~s)") + (uri->string current-uri) code + (response-reason-phrase resp)))))))))))) (define-syntax-rule (false-if-networking-error exp) "Return #f if EXP triggers a network related exception as can occur when