Skip to content

Accept Unix domain socket addresses in the connection URI - #94

Open
vegardkrogh wants to merge 1 commit into
CuatroElixir:mainfrom
vegardkrogh:unix-socket-address
Open

vegardkrogh wants to merge 1 commit into
CuatroElixir:mainfrom
vegardkrogh:unix-socket-address

Conversation

@vegardkrogh

Copy link
Copy Markdown

This lets a client connect to a server listening on a Unix domain socket.

Mint and OTP already support the address {:local, path} with port 0. Slipstream's URI validation and its connect call were the only blockers.

Changes

  • Slipstream.Configuration accepts port 0 when the URI host is {:local, path}. Port 0 with a binary host is still rejected.
  • Slipstream.Connection.Impl.http_connect/1 gets a new first clause for such a URI. It hands Mint {:local, path} with port 0 and sets the :hostname option that Mint requires for a non-binary address, defaulting to "localhost".
  • The WebSocket upgrade request sends Host: <hostname> for such a URI unless the caller gives a Host header. Without it, Mint sends Host: localhost:0, which Cowboy rejects with a 400.
  • The new Mint call carries a function-level @dialyzer nowarn, the same way path/1 does. Mint's success typing narrows the address to a binary, although its spec and code accept any :inet.socket_address().
  • The :uri option docs describe the new form.

Usage

uri: %URI{scheme: "ws", host: {:local, "/path/to.sock"}, port: 0, path: "/socket/websocket"}

Tests

  • Configuration tests check that the Unix-socket URI passes validation and that port 0 with a binary host still fails.
  • Integration tests serve the test endpoint on a Unix socket through Plug.Cowboy and connect with and without a given Host header.
  • Locally on Elixir 1.20.4 / OTP 29, mix test --warnings-as-errors passes with 96 tests, and mix format --check-formatted and mix dialyzer pass.
  • mix coveralls reports 100% for both changed files. The total is 99.3%, the same as main locally, because of existing gaps in slipstream.ex, pipeline.ex and events.ex.

I left the changelog alone because entries are added at release time.

Mint and OTP's local address family already support connecting to
`{:local, path}` with port 0, but Slipstream rejected such a URI during
configuration validation and passed the URI host and port to Mint
unchanged.

- `Slipstream.Configuration` accepts port 0 when the URI host is
  `{:local, path}`.
- `Slipstream.Connection.Impl.http_connect/1` connects to `{:local, path}`
  with port 0 and sets the `:hostname` option Mint requires for a
  non-binary address (default "localhost").
- The WebSocket upgrade request sends `Host: <hostname>` for such an
  address unless a Host header is given, since Mint would otherwise send
  `Host: localhost:0`, which Cowboy rejects with a 400.

Usage:

    uri: %URI{scheme: "ws", host: {:local, "/path/to.sock"}, port: 0,
              path: "/socket/websocket"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant