HTTP over AF_UNIX: HTTP connection to unix socket
Asked Answered
C

1

28

We have HTTP server , for which we have HTTP client based application (on Linux) working fine.

But now we need to listen on Unix domain sockets from our client application. So is it possible to send/receive httprequest, httpresponse packet from the unix domain socket?

Scenerio1:When connecting to localhost, it is required to eliminate the SSL overhead by connecting HTTP to the unix socket instead of HTTPS to the local port.

Basically Looking for a standard encoding a unix socket path in an HTTP URL.

Many Thanks in advance.

Chintz answered 8/2, 2013 at 11:6 Comment(0)
B
24

So long as your socket is a stream socket (SOCK_STREAM rather than SOCK_DGRAM) then it's technically possible. There's nothing in HTTP that requires TCP/IP, it just requires a reliable bidirectional stream.

However I've never seen an HTTP client that knows how to connect to such a socket. There's no URL format that I know of that would work, should you actually need to use a URL to talk to the server.

Also note that some things that normal web servers depend on (such as getpeername(), to identify the client) make no sense when you're not using TCP/IP.

EDIT I just saw your edit about mapping localhost to the UNIX socket. This is perfectly feasible, you just need to ensure that the client knows how to find the path of the UNIX socket that should be used instead of connecting to 127.0.0.1:xxx

Boulware answered 8/2, 2013 at 11:9 Comment(4)
@PowerPC As far as I know (I've looked) there is no defined URL scheme for HTTP that allows you to specify a socket path instead of a hostname. I know of apps (e.g. MySQL) that take the approach of simply converting "localhost" to mean "use the UNIX socket" instead of TCP/IP.Boulware
Curl and Go both support this now, eg on recent curl curl -v --unix-socket /var/run/docker.sock http:/containers/json will talk to Docker which has a Go implementation over unix sockets.Incriminate
On Python, the library requests-unixsocket exists for that purpose, using the non-standard scheme http+unix://%2Fpath%2Fto%2Fsocket/url-pathSuperphosphate
@Boulware I realize this is an old answer, but just wanted to mention: Reverse proxy setups commonly make use of unix sockets, to avoid wasting TCP ports and for lower resource use/better performance, among other things. nginx and apache, for example, can proxy HTTP requests to a local unix socket, where another nginx, apache, php, or other service is listening. In fact, most of the internal web applications at my company on Linux servers use exactly that kind of setup.Deckhand

© 2022 - 2024 — McMap. All rights reserved.