handshake_protocol.erl 866 B

1234567891011121314151617181920212223242526272829303132
  1. -module(handshake_protocol).
  2. -behaviour(ranch_protocol).
  3. -export([start_link/3]).
  4. -export([init/3]).
  5. start_link(Ref, Transport, Opts) ->
  6. Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
  7. {ok, Pid}.
  8. init(Ref, Transport, Opts) ->
  9. SniHost = case ranch:handshake(Ref) of
  10. %% Due to a bug in ssl (https://bugs.erlang.org/browse/ERL-951,
  11. %% fixed in OTP 22.0.3) the value for sni may be {sni, Hostname}
  12. %% instead of Hostname.
  13. {continue, #{sni := {sni, Hostname}}} ->
  14. Hostname;
  15. {continue, #{sni := Hostname}} ->
  16. Hostname
  17. end,
  18. SniHostOpts = maps:get(SniHost, Opts),
  19. {ok, Socket} = ranch:handshake_continue(Ref, SniHostOpts),
  20. loop(Socket, Transport).
  21. loop(Socket, Transport) ->
  22. case Transport:recv(Socket, 0, 5000) of
  23. {ok, Data} ->
  24. Transport:send(Socket, Data),
  25. loop(Socket, Transport);
  26. _ ->
  27. ok = Transport:close(Socket)
  28. end.