all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Attila Lendvai <attila@lendvai.name>
To: 54836@debbugs.gnu.org
Cc: Attila Lendvai <attila@lendvai.name>
Subject: [bug#54836] [PATCH 2/2] http-client: Factor out open-connection*, rename variables.
Date: Sun, 10 Apr 2022 15:35:37 +0200	[thread overview]
Message-ID: <20220410133536.30422-2-attila@lendvai.name> (raw)
In-Reply-To: <20220410133431.30058-1-attila@lendvai.name>

This is an idempotent refactor.

* guix/http-client.scm (http-fetch): Introduce open-connection*. Rename some
variables to turn programmer mistakes into compile errors.
---
 guix/http-client.scm | 48 ++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/guix/http-client.scm b/guix/http-client.scm
index b8689a22ed..3c5115068d 100644
--- a/guix/http-client.scm
+++ b/guix/http-client.scm
@@ -103,15 +103,17 @@ (define* (http-fetch uri #:key port (text? #f) (buffered? #t)
 When ACCEPT-ALL-RESPONSE-CODES? is false then raise an '&http-get-error'
 condition if downloading fails, otherwise return the response regardless
 of the reponse code."
-  (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 "
@@ -119,10 +121,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)
@@ -135,28 +137,26 @@ (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
            (if accept-all-response-codes?
                (values data (response-content-length resp))
                (raise (condition (&http-get-error
-                                  (uri uri)
+                                  (uri current-uri)
                                   (code code)
                                   (reason (response-reason-phrase resp))
                                   (headers (response-headers resp)))
@@ -165,7 +165,7 @@ (define uri*
                                    (format
                                     #f
                                     (G_ "~a: HTTP download failed: ~a (~s)")
-                                    (uri->string uri) code
+                                    (uri->string current-uri) code
                                     (response-reason-phrase resp)))))))))))))
 
 (define-syntax-rule (false-if-networking-error exp)
-- 
2.34.0





  reply	other threads:[~2022-04-10 13:37 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-10 13:34 [bug#54836] [PATCH 1/2] http-client: Fix redirection Attila Lendvai
2022-04-10 13:35 ` Attila Lendvai [this message]
2022-04-10 13:41 ` [bug#54836] [PATCH v2 1/3] http-client: Added accept-all-response-codes? argument Attila Lendvai
2022-04-10 13:41   ` [bug#54836] [PATCH 2/3] http-client: Fix redirection Attila Lendvai
2022-04-11 12:44     ` [bug#54836] [PATCH 1/2] " Ludovic Courtès
2022-04-10 13:41   ` [bug#54836] [PATCH 3/3] http-client: Factor out open-connection*, rename variables Attila Lendvai
2022-04-11 12:45   ` [bug#54836] [PATCH 1/2] http-client: Fix redirection Ludovic Courtès
2022-04-12  7:28     ` Attila Lendvai
2022-04-27 16:37       ` Attila Lendvai
2022-04-27 20:53       ` Ludovic Courtès
2023-01-03 22:29         ` Maxim Cournoyer
2022-04-28 10:22 ` [bug#54836] [PATCH v3] http-client: Factor out open-connection*, rename variables Attila Lendvai
2023-01-06 18:46 ` [bug#54836] [PATCH v4 1/2] " Attila Lendvai
2023-01-06 18:46   ` [bug#54836] [PATCH v4 2/2] http-client: Added accept-all-response-codes? argument Attila Lendvai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220410133536.30422-2-attila@lendvai.name \
    --to=attila@lendvai.name \
    --cc=54836@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.