Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP over AF_UNIX: HTTP connection to unix socket

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.

like image 316
Rohit Avatar asked Feb 08 '13 11:02

Rohit


People also ask

What is AF_UNIX socket?

The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain sockets can be either unnamed, or bound to a filesystem pathname (marked as being of type socket).

How do I join a UNIX socket?

Create a socket with the socket() system call. Connect the socket to the address of the server using the connect() system call. Send and receive data. There are a number of ways to do this, but the simplest way is to use the read() and write() system calls.

Do UNIX domain sockets use ports?

UNIX domain sockets do not use port numbers to distinguish between multiple instances. Traditionally they use paths and are represented in the file system as socket Inodes.

Is TCP or UNIX socket faster?

Unix domain sockets are often twice as fast as a TCP socket when both peers are on the same host. The Unix domain protocols are not an actual protocol suite, but a way of performing client/server communication on a single host using the same API that is used for clients and servers on different hosts.


1 Answers

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

like image 121
Alnitak Avatar answered Oct 09 '22 18:10

Alnitak