Teals.Cowboy.StreamHandler (teals v0.1.0)

Copy Markdown View Source

A Cowboy stream handler for listeners on Teals.Ranch. Put it first:

:ranch.start_listener(:api, Teals.Ranch,
  %{socket_opts: [port: 8443, config: config]},
  :cowboy_clear, %{
    env: %{dispatch: dispatch},
    stream_handlers: [Teals.Cowboy.StreamHandler, :cowboy_stream_h]
  })

It does two things:

  • Identity. Cowboy reads a client certificate only from :ssl sockets. The handler gives every request the certificate the TLS handshake accepted, so :cowboy_req.cert/1 and Plug.Conn.get_peer_data/1 return it in DER as they would over :ssl, or :undefined and nil when the client sent none. Without the handler no certificate is reported.

  • WebSockets. RFC 6455 has no half-close, so when a stream switches protocol to :cowboy_websocket the handler sets half_close: false on the socket before Cowboy takes over: the peer's close_notify then ends the WebSocket as {:error, :closed}, as it would over :ssl. If it arrived before the switch, writing ends too, Cowboy cannot send its 101 response and closes the connection, and the WebSocket handler never starts. Without the stream handler, the WebSocket handler's websocket_info/2 receives {:teals_read_closed, socket} instead. A switch to any other module keeps half-close.

Only HTTP/1.1 is supported.