all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] http-client: Support basic authentication.
@ 2015-12-16 10:19 Ricardo Wurmus
  2015-12-16 10:51 ` Ricardo Wurmus
  0 siblings, 1 reply; 5+ messages in thread
From: Ricardo Wurmus @ 2015-12-16 10:19 UTC (permalink / raw)
  To: guix-devel@gnu.org

[-- Attachment #1: Type: text/plain, Size: 536 bytes --]

Hi Guix,

for the Bioconductor importer extension to the CRAN importer I need our
HTTP client to support basic authentication as the DESCRIPTION files are
only available on the SVN web interface, which requires authentication
with a fixed username and password.

The attached patch adds this feature to the ‘http-fetch’ procedure.  Now
“protected” URLs like

    https://readonly:readonly@hedgehog.fhcrc.org/bioconductor/branches/RELEASE_3_2/madman/Rpacks/genomation/DESCRIPTION

can be downloaded.

~~ Ricardo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-http-client-Support-basic-authentication.patch --]
[-- Type: text/x-patch, Size: 2119 bytes --]

From 9a4385353dac7cf74c4950921bbf6f8ecc06d92a Mon Sep 17 00:00:00 2001
From: Ricardo Wurmus <ricardo.wurmus@mdc-berlin.de>
Date: Wed, 16 Dec 2015 11:12:46 +0100
Subject: [PATCH] http-client: Support basic authentication.

* guix/http-client.scm (http-fetch): Add Authorization header to request
  when the URI contains userinfo.
---
 guix/http-client.scm | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/guix/http-client.scm b/guix/http-client.scm
index eb2c3f4..1feb428 100644
--- a/guix/http-client.scm
+++ b/guix/http-client.scm
@@ -32,6 +32,7 @@
   #:use-module (rnrs bytevectors)
   #:use-module (guix ui)
   #:use-module (guix utils)
+  #:use-module (guix base64)
   #:use-module ((guix build utils)
                 #:select (mkdir-p dump-port))
   #:use-module ((guix build download)
@@ -213,12 +214,20 @@ Raise an '&http-get-error' condition if downloading fails."
     (let ((port (or port (open-connection-for-uri uri))))
       (unless buffered?
         (setvbuf port _IONBF))
-      (let*-values (((resp data)
+      (let*-values ((auth-header
+                     (match (uri-userinfo uri)
+                       ((str) (cons 'Authorization
+                                    (string-append "Basic "
+                                                   (base64-encode
+                                                    (string->utf8 str)))))
+                       (_ '())))
+                    ((resp data)
                      ;; Try hard to use the API du jour to get an input port.
                      (if (guile-version>? "2.0.7")
-                         (http-get uri #:streaming? #t #:port port) ; 2.0.9+
+                         (http-get uri #:streaming? #t #:port port
+                                   #:headers auth-header) ; 2.0.9+
                          (http-get* uri #:decode-body? text?        ; 2.0.7
-                                    #:port port)))
+                                    #:port port #:headers auth-header)))
                     ((code)
                      (response-code resp)))
         (case code
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] http-client: Support basic authentication.
  2015-12-16 10:19 [PATCH] http-client: Support basic authentication Ricardo Wurmus
@ 2015-12-16 10:51 ` Ricardo Wurmus
  2015-12-16 12:28   ` Ricardo Wurmus
  2015-12-19 13:51   ` Ludovic Courtès
  0 siblings, 2 replies; 5+ messages in thread
From: Ricardo Wurmus @ 2015-12-16 10:51 UTC (permalink / raw)
  To: guix-devel@gnu.org

I sent this patch after making a tiny untested change, which broke
http-fetch unexpectedly.

This does not work:

      (let*-values ((auth-header
                     (match (uri-userinfo uri)
                       ((str) (cons 'Authorization
                                    (string-append "Basic "
                                                   (base64-encode
                                                    (string->utf8 str)))))
                       (_ '())))
                       ...) ...)

With that I get this error:

    web/request.scm:183:10: In procedure build-request:
    web/request.scm:183:10: Bad request: Header not a pair: ()

Binding the result of (uri-userinfo uri) fixes it:

      (let*-values ((userinfo (uri-userinfo uri))
                    (auth-header
                     (match userinfo
                       ((str) (cons 'Authorization
                                    (string-append "Basic "
                                                   (base64-encode
                                                    (string->utf8 str)))))
                       (_ '())))
                       ...) ...)

I don’t understand this.  Does (uri-userinfo uri) return multiple
values?

~~ Ricardo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] http-client: Support basic authentication.
  2015-12-16 10:51 ` Ricardo Wurmus
@ 2015-12-16 12:28   ` Ricardo Wurmus
  2015-12-19 13:52     ` Ludovic Courtès
  2015-12-19 13:51   ` Ludovic Courtès
  1 sibling, 1 reply; 5+ messages in thread
From: Ricardo Wurmus @ 2015-12-16 12:28 UTC (permalink / raw)
  To: guix-devel@gnu.org

[-- Attachment #1: Type: text/plain, Size: 145 bytes --]

The attached patch is better.  Turns out I really didn’t understand
‘let*-values’, so it’s better to do this in the outer ‘let’.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-http-client-Support-basic-authentication.patch --]
[-- Type: text/x-patch, Size: 2274 bytes --]

From 056ca0bfb03e14c698ffd984c36bb396d5aed492 Mon Sep 17 00:00:00 2001
From: Ricardo Wurmus <ricardo.wurmus@mdc-berlin.de>
Date: Wed, 16 Dec 2015 11:12:46 +0100
Subject: [PATCH] http-client: Support basic authentication.

* guix/http-client.scm (http-fetch): Add Authorization header to request
  when the URI contains userinfo.
---
 guix/http-client.scm | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/guix/http-client.scm b/guix/http-client.scm
index eb2c3f4..c7cbc82 100644
--- a/guix/http-client.scm
+++ b/guix/http-client.scm
@@ -32,6 +32,7 @@
   #:use-module (rnrs bytevectors)
   #:use-module (guix ui)
   #:use-module (guix utils)
+  #:use-module (guix base64)
   #:use-module ((guix build utils)
                 #:select (mkdir-p dump-port))
   #:use-module ((guix build download)
@@ -210,15 +211,23 @@ 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-for-uri uri))))
+    (let ((port (or port (open-connection-for-uri uri)))
+          (auth-header (match (uri-userinfo uri)
+                         ((? string? str)
+                          (list (cons 'Authorization
+                                      (string-append "Basic "
+                                                     (base64-encode
+                                                      (string->utf8 str))))))
+                         (_ '()))))
       (unless buffered?
         (setvbuf port _IONBF))
       (let*-values (((resp data)
                      ;; Try hard to use the API du jour to get an input port.
                      (if (guile-version>? "2.0.7")
-                         (http-get uri #:streaming? #t #:port port) ; 2.0.9+
+                         (http-get uri #:streaming? #t #:port port
+                                   #:headers auth-header) ; 2.0.9+
                          (http-get* uri #:decode-body? text?        ; 2.0.7
-                                    #:port port)))
+                                    #:port port #:headers auth-header)))
                     ((code)
                      (response-code resp)))
         (case code
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] http-client: Support basic authentication.
  2015-12-16 10:51 ` Ricardo Wurmus
  2015-12-16 12:28   ` Ricardo Wurmus
@ 2015-12-19 13:51   ` Ludovic Courtès
  1 sibling, 0 replies; 5+ messages in thread
From: Ludovic Courtès @ 2015-12-19 13:51 UTC (permalink / raw)
  To: Ricardo Wurmus; +Cc: guix-devel@gnu.org

Ricardo Wurmus <ricardo.wurmus@mdc-berlin.de> skribis:

> Binding the result of (uri-userinfo uri) fixes it:
>
>       (let*-values ((userinfo (uri-userinfo uri))
>                     (auth-header
>                      (match userinfo
>                        ((str) (cons 'Authorization
>                                     (string-append "Basic "
>                                                    (base64-encode
>                                                     (string->utf8 str)))))
>                        (_ '())))
>                        ...) ...)
>
> I don’t understand this.  Does (uri-userinfo uri) return multiple
> values?

‘uri-userinfo’ is a simple field accessor; it returns one value.

Ludo’.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] http-client: Support basic authentication.
  2015-12-16 12:28   ` Ricardo Wurmus
@ 2015-12-19 13:52     ` Ludovic Courtès
  0 siblings, 0 replies; 5+ messages in thread
From: Ludovic Courtès @ 2015-12-19 13:52 UTC (permalink / raw)
  To: Ricardo Wurmus; +Cc: guix-devel@gnu.org

Ricardo Wurmus <ricardo.wurmus@mdc-berlin.de> skribis:

> From 056ca0bfb03e14c698ffd984c36bb396d5aed492 Mon Sep 17 00:00:00 2001
> From: Ricardo Wurmus <ricardo.wurmus@mdc-berlin.de>
> Date: Wed, 16 Dec 2015 11:12:46 +0100
> Subject: [PATCH] http-client: Support basic authentication.
>
> * guix/http-client.scm (http-fetch): Add Authorization header to request
>   when the URI contains userinfo.

LGTM, thanks!

Ludo’.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-12-19 13:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-16 10:19 [PATCH] http-client: Support basic authentication Ricardo Wurmus
2015-12-16 10:51 ` Ricardo Wurmus
2015-12-16 12:28   ` Ricardo Wurmus
2015-12-19 13:52     ` Ludovic Courtès
2015-12-19 13:51   ` Ludovic Courtès

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.