unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Re: Using UNIX sockets (Zelphir Kaltstahl)
       [not found] <mailman.105.1559404818.12235.guile-user@gnu.org>
@ 2019-06-02 14:01 ` Zelphir Kaltstahl
  2019-06-02 16:29   ` Ludovic Courtès
  2019-06-02 14:03 ` Zelphir Kaltstahl
  1 sibling, 1 reply; 3+ messages in thread
From: Zelphir Kaltstahl @ 2019-06-02 14:01 UTC (permalink / raw)
  To: guile-user

Hi Guile Users!

I think I've made some progress on how to communicate with dockerd over
its UNIX socket. This is what I have now:

(use-modules (web client)
             (ice-9 iconv))

(define* (connect-to-docker-socket #:key (socket-path "/var/run/docker.sock"))
  (let ([docker-sock-addr (make-socket-address AF_UNIX socket-path)]
        [docker-sock (socket PF_UNIX SOCK_STREAM 0)])
    ;; socket options:
    ;; https://www.gnu.org/software/libc/manual/html_node/Socket_002dLevel-Options.html
    (setsockopt docker-sock SOL_SOCKET SO_REUSEADDR 1)
    ;; usage of connect:
    ;; https://www.gnu.org/software/guile/manual/html_node/Network-Sockets-and-Communication.html#Network-Sockets-and-Communication
    ;; server side would use `bind`, `accept` and `listen`.
    ;; client side uses `connect` and `close`.
    (connect docker-sock docker-sock-addr)
    docker-sock))


(let ([sock (connect-to-docker-socket)])
  (call-with-values
      (lambda ()
        (http-get "unix:/var/run/docker.sock/containers/json?all=true"
                  #:port sock
                  ;; dockerd uses HTTP 1.1 it seems.
                  ;; other values will result in: "Bad Request: unsupported protocol version"
                  #:version '(1 . 1)
                  #:keep-alive? #f
                  ;; Apparently the `host` header must be specified.
                  ;; The `host` header in this case is ???.
                  #:headers '((host . "localhost"))
                  #:decode-body? #t
                  #:streaming? #f))
    (lambda (response response-text)
      (display response)
      (display response-text)))
  (close sock))

So I am trying to use `web client` to send HTTP requests to the socket
and I thing that the socket is now created correctly (although I am not
sure about it).

Now I get the following error:

web/request.scm:184:10: In procedure build-request:
Bad request: Bad value for header host: "localhost"

However, according to: https://github.com/request/request/issues/2327
any string should be fine as host, host header only being a required
header for HTTP 1.1 requests. I am now unsure how to progress from here.
What will be accepted as a not bad value for the host header instead? I
also tried to use empty string and string of one space and such things,
all with the same result. Initially it was also not clear how to write
the symbol for host header, 'HOST, 'Host or 'host, but that seems to be
figured out now.

Regards,

Zelphir



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

* Re: Using UNIX sockets (Zelphir Kaltstahl)
       [not found] <mailman.105.1559404818.12235.guile-user@gnu.org>
  2019-06-02 14:01 ` Using UNIX sockets (Zelphir Kaltstahl) Zelphir Kaltstahl
@ 2019-06-02 14:03 ` Zelphir Kaltstahl
  1 sibling, 0 replies; 3+ messages in thread
From: Zelphir Kaltstahl @ 2019-06-02 14:03 UTC (permalink / raw)
  To: guile-user

Hi Guile Users!

I think I've made some progress on how to communicate with dockerd over
its UNIX socket. This is what I have now:

(use-modules (web client)
             (ice-9 iconv))

(define* (connect-to-docker-socket #:key (socket-path "/var/run/docker.sock"))
  (let ([docker-sock-addr (make-socket-address AF_UNIX socket-path)]
        [docker-sock (socket PF_UNIX SOCK_STREAM 0)])
    ;; socket options:
    ;; https://www.gnu.org/software/libc/manual/html_node/Socket_002dLevel-Options.html
    (setsockopt docker-sock SOL_SOCKET SO_REUSEADDR 1)
    ;; usage of connect:
    ;; https://www.gnu.org/software/guile/manual/html_node/Network-Sockets-and-Communication.html#Network-Sockets-and-Communication
    ;; server side would use `bind`, `accept` and `listen`.
    ;; client side uses `connect` and `close`.
    (connect docker-sock docker-sock-addr)
    docker-sock))


(let ([sock (connect-to-docker-socket)])
  (call-with-values
      (lambda ()
        (http-get "unix:/var/run/docker.sock/containers/json?all=true"
                  #:port sock
                  ;; dockerd uses HTTP 1.1 it seems.
                  ;; other values will result in: "Bad Request: unsupported protocol version"
                  #:version '(1 . 1)
                  #:keep-alive? #f
                  ;; Apparently the `host` header must be specified.
                  ;; The `host` header in this case is ???.
                  #:headers '((host . "localhost"))
                  #:decode-body? #t
                  #:streaming? #f))
    (lambda (response response-text)
      (display response)
      (display response-text)))
  (close sock))

So I am trying to use `web client` to send HTTP requests to the socket
and I thing that the socket is now created correctly (although I am not
sure about it).

Now I get the following error:

web/request.scm:184:10: In procedure build-request:
Bad request: Bad value for header host: "localhost"

However, according to: https://github.com/request/request/issues/2327
any string should be fine as host, host header only being a required
header for HTTP 1.1 requests. I am now unsure how to progress from here.
What will be accepted as a not bad value for the host header instead? I
also tried to use empty string and string of one space and such things,
all with the same result. Initially it was also not clear how to write
the symbol for host header, 'HOST, 'Host or 'host, but that seems to be
figured out now.

Regards,

Zelphir



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

* Re: Using UNIX sockets (Zelphir Kaltstahl)
  2019-06-02 14:01 ` Using UNIX sockets (Zelphir Kaltstahl) Zelphir Kaltstahl
@ 2019-06-02 16:29   ` Ludovic Courtès
  0 siblings, 0 replies; 3+ messages in thread
From: Ludovic Courtès @ 2019-06-02 16:29 UTC (permalink / raw)
  To: guile-user

Hi Zelphir,

Zelphir Kaltstahl <zelphirkaltstahl@gmail.com> skribis:


[...]

>                   ;; Apparently the `host` header must be specified.
>                   ;; The `host` header in this case is ???.
>                   #:headers '((host . "localhost"))

This should be:

  #:headers '((host . ("localhost" . #f)))

or:

  #:headers '((host . ("localhost" . 1234)))

where 1234 is a port number.

HTH!

Ludo’.




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

end of thread, other threads:[~2019-06-02 16:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.105.1559404818.12235.guile-user@gnu.org>
2019-06-02 14:01 ` Using UNIX sockets (Zelphir Kaltstahl) Zelphir Kaltstahl
2019-06-02 16:29   ` Ludovic Courtès
2019-06-02 14:03 ` Zelphir Kaltstahl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).