From patchwork Fri Jan 6 18:46:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Attila Lendvai X-Patchwork-Id: 45884 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 ED47427BBEB; Fri, 6 Jan 2023 18:56:35 +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_H2,SPF_HELO_PASS autolearn=ham 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 85F5F27BBED for ; Fri, 6 Jan 2023 18:56:33 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDrsu-00014b-OF; Fri, 06 Jan 2023 13:56:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDrso-0000y2-Rk for guix-patches@gnu.org; Fri, 06 Jan 2023 13:56:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pDrso-0007bf-JS for guix-patches@gnu.org; Fri, 06 Jan 2023 13:56:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pDrso-0008Kr-Ao for guix-patches@gnu.org; Fri, 06 Jan 2023 13:56:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#54836] [PATCH v4 1/2] 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: Fri, 06 Jan 2023 18:56: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: moreinfo patch To: 54836@debbugs.gnu.org Cc: Attila Lendvai Received: via spool by 54836-submit@debbugs.gnu.org id=B54836.167303132331971 (code B ref 54836); Fri, 06 Jan 2023 18:56:02 +0000 Received: (at 54836) by debbugs.gnu.org; 6 Jan 2023 18:55:23 +0000 Received: from localhost ([127.0.0.1]:55853 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pDrsA-0008Ja-L1 for submit@debbugs.gnu.org; Fri, 06 Jan 2023 13:55:23 -0500 Received: from mail-wm1-f44.google.com ([209.85.128.44]:35718) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pDrs9-0008JK-6b for 54836@debbugs.gnu.org; Fri, 06 Jan 2023 13:55:21 -0500 Received: by mail-wm1-f44.google.com with SMTP id m8-20020a05600c3b0800b003d96f801c48so4209884wms.0 for <54836@debbugs.gnu.org>; Fri, 06 Jan 2023 10:55:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=vJTQ9xGFt0HQmYzZlHtezoP0VLL0lbhtb8NomwgTRuY=; b=CxnO4LiZ8YrrjRgU/hMYZoSAVkVo+2lf7qq8fSeOeMhZkykNVsRCpC7dDKr6XJkyx0 4AxsxDs3OY1/dOl6ebjWJ8Mz6YvfbU7yFsU0sRy9TK+reB6NawyedDUzygeleeSio3zE urWBmrw49ciiT7YxgG2PS5Y4nGsjfbEO1YVw4kIOIMH1sl1g8A7AqCLXi/NDoisMfVMD R4LcSnAMk/rKzuiS9gPWrrfYXIRzshc2gWT0DFA/54sHG8wAlFp0OJLYNc4Ntb9oLSRH MrW/ZsP/FZ3U3hjrrfEGIJZWdtmjDNSabO+exNiOASxdzcfZ/jgfGjoBafRReW6fOJ9E th1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vJTQ9xGFt0HQmYzZlHtezoP0VLL0lbhtb8NomwgTRuY=; b=K26TpWw4smVjub3r2dJacLtEdoHCtE5gChfdRnBKZWTdLWkw7iHfZ946XHQg9uh/zt uuO8wjVWyFKzoPEhzf01sFzMH0E9j2RVWFa2NpWsNjjp6rzuWBtbz2XVU0gngGA8X446 e8C0aiUhZ4tLAcwn11PnK2QlPpz09d0xz1jEvgMmmpyXHSGkvfgn6+yfvK4lJmyE0Jbt V6ApN+jiiL5dg8y3tuFRu8yC/Yus5KZsUgefCXnirn8a9tlAXubEU6fE+Ejrm2OXG4/8 M2AWw+vhPslb+XY/F4P29VybpbXqfE1imBa94vI1w2q4WiEsIfym6jtbrvhm2Xu4wu5p cs8A== X-Gm-Message-State: AFqh2kqWtwsNMOw8k754RCtuHEAaIMOS8iZFJ1TNL363zcTzALLbIo6h x34MqDUX+GhOe3zuFLLB9fyAbIx2lQg= X-Google-Smtp-Source: AMrXdXt9dJga8cUlTOJu/HFzDTycGU8ycDS+cyrR/jSxCB8wpnOEKvuynh/ohKNq7CumZrrv5DLIYQ== X-Received: by 2002:a05:600c:4e09:b0:3d4:5741:af9b with SMTP id b9-20020a05600c4e0900b003d45741af9bmr43947192wmq.0.1673031315214; Fri, 06 Jan 2023 10:55:15 -0800 (PST) Received: from lelap.lan (catv-80-98-247-145.catv.fixed.vodafone.hu. [80.98.247.145]) by smtp.gmail.com with ESMTPSA id h8-20020a05600c350800b003d990372dd5sm7988352wmq.20.2023.01.06.10.55.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Jan 2023 10:55:14 -0800 (PST) From: Attila Lendvai Date: Fri, 6 Jan 2023 19:46:54 +0100 Message-Id: <20230106184654.28037-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-bounces+patchwork=mira.cbaines.net@gnu.org 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. --- i'm (re)sending the two commits that are sitting in my local branch. i freshly rebased them. the conlusion was to not add the accept-all-response-codes? argument, and accordingly i have added a duplicate of http-fetch into the go importer. my fix was applied by Ludo in his own commit. this is why the 3 commits got reduced to one. i'm only proposing to push the first patch. what the second one does was rejected by Ludo. ratinale for the first patch: it renames variables in a way that is less confusing for the programmer, and hence helps avoiding mistakes that i have fixed in my first original commit. an uri and an uri* variable in the same lexical scope is simply calling for mistakes... 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 9138a627ac..2d48a882e1 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