Does ktor support sending requests to unix sockets...
# ktor
r
Does ktor support sending requests to unix sockets like curl does in this example?
curl --unix-socket /run/podman/podman.sock <http://d/v5.0.0/libpod/info>
a
Yes. The support for Unix domain sockets has been implemented in Ktor 2.0.0. Here is an example of how to get the Docker version by sending a request through the Unix domain socket:
Copy code
val selectorManager = SelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)
val socket = aSocket(selectorManager).tcp().connect(UnixSocketAddress("/var/run/docker.sock"))

val writeChannel = socket.openWriteChannel(autoFlush = true)
val request = "GET /version HTTP/1.1\r\nHost: localhost\r\n\r\n"

writeChannel.writeStringUtf8(request)

val readChannel = socket.openReadChannel()
var line: String? = readChannel.readUTF8Line()
while (line != null) {
    println(line)
    line = readChannel.readUTF8Line()
}
socket.close()
r
I don't think that's very nice to use 😅 I'll probably stick with OkHttp for this for now: https://github.com/square/okhttp/blob/master/samples/unixdomainsockets/src/main/java/okhttp3/unixdomainsockets/ClientAndServer.java
a
The Unix domains support for the CIO engine will be available in Ktor 3.2.0. Here is an example:
Copy code
val client = HttpClient(CIO)

val response = client.get("<http://localhost/version>") {
    unixSocket("/var/run/docker.sock")
}

println(response.bodyAsText())
👍 2