Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Libs by

I want to a HTTP request over a UNIX socket, like the following example using curl and docker:

$ curl --unix-socket /var/run/docker.sock http://localhost/images/json
[
  {
    "Containers": -1,
    "Created": 1577395270,
    "Id": "sha256:ce6c1e7ac56533e2742030f033cf0d8cf0adc996c7bb87453eb5adc266b2ef2e",
    "Labels": null,
    "ParentId": "",
    "RepoDigests": [
      "busybox@sha256:7fe0cb3632d9ea7b2a9ab4427e339e01f7cdfeff50674804cb8946664976c610"
    ],
    "RepoTags": [
      "busybox:musl"
    ],
    "SharedSize": -1,
    "Size": 1461385,
    "VirtualSize": 1461385
  }
]

As far as I've seen, clj-http doesn't seem to support this.

What would be a recommended/idiomatic way of doing this in Clojure?

1 Answer

+1 vote
by
selected by
 
Best answer

The short answer is: you cant.

Java doesn't have support (as far as I can find/see) for writing to unix sockets.

The longer answer is:

You can send and pipe to sockets. The netcat "nc" tools can work here i.e you can open a tcp socket with:

Option A
Use netcat

nc -lk localhost 8787

And then find a way to nc to redirect to the socket you want

Option B.

You could call curl as above using an external process with ProcessBuilder.

Option C.

There are libraries that allow you to create unix sockets from java, like https://github.com/mcfunley/juds. This involves using jni which the "juds" abstracts away

by
I started work on this at https://github.com/into-docker/clj-docker-client and was able to make the connection via https://square.github.io/okhttp/

But now we have a dedicated lib for this:
https://github.com/into-docker/unixsocket-http

Hope this helps some one else too!
...