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
:sslsockets. The handler gives every request the certificate the TLS handshake accepted, so:cowboy_req.cert/1andPlug.Conn.get_peer_data/1return it in DER as they would over:ssl, or:undefinedandnilwhen 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_websocketthe handler setshalf_close: falseon the socket before Cowboy takes over: the peer'sclose_notifythen 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'swebsocket_info/2receives{:teals_read_closed, socket}instead. A switch to any other module keeps half-close.
Only HTTP/1.1 is supported.