cowboy_tls.erl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. %% Copyright (c) 2015-2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(cowboy_tls).
  15. -behavior(ranch_protocol).
  16. -export([start_link/4]).
  17. -export([connection_process/5]).
  18. -spec start_link(ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> {ok, pid()}.
  19. start_link(Ref, Socket, Transport, Opts) ->
  20. Pid = proc_lib:spawn_link(?MODULE, connection_process,
  21. [self(), Ref, Socket, Transport, Opts]),
  22. {ok, Pid}.
  23. -spec connection_process(pid(), ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> ok.
  24. connection_process(Parent, Ref, Socket, Transport, Opts) ->
  25. ok = ranch:accept_ack(Ref),
  26. case ssl:negotiated_protocol(Socket) of
  27. {ok, <<"h2">>} ->
  28. init(Parent, Ref, Socket, Transport, Opts, cowboy_http2);
  29. _ -> %% http/1.1 or no protocol negotiated.
  30. init(Parent, Ref, Socket, Transport, Opts, cowboy_http)
  31. end.
  32. init(Parent, Ref, Socket, Transport, Opts, Protocol) ->
  33. _ = case maps:get(connection_type, Opts, supervisor) of
  34. worker -> ok;
  35. supervisor -> process_flag(trap_exit, true)
  36. end,
  37. Protocol:init(Parent, Ref, Socket, Transport, Opts).