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?

3 Answers

+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!
0 votes
by
edited by

Http web client does not work with unix sockets.

I submitted a JDK bug/enhancement (if it gets approved: https://bugs.openjdk.java.net/browse/JDK-8275838 )

I also tweeted about this - maybe it gets picked up by someone more involved with that piece of code: https://twitter.com/ieugen222/status/1451877192766574592

First issue is with jdk.internal.net.http.HttpRequestBuilderImpl#checkURI (might be others)

    static void checkURI(URI uri) {
    String scheme = uri.getScheme();
    if (scheme == null)
        throw newIAE("URI with undefined scheme");
    scheme = scheme.toLowerCase(Locale.US);
    if (!(scheme.equals("https") || scheme.equals("http"))) {
        throw newIAE("invalid URI scheme %s", scheme);
    }
    if (uri.getHost() == null) {
        throw newIAE("unsupported URI %s", uri);
    }
}
0 votes
by

They say they introduced support for Unix sockets in JDK 16: https://openjdk.java.net/jeps/380

Here's a good blog post about them:
https://inside.java/2021/02/03/jep380-unix-domain-sockets-channels/

...