zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

cowboy_tls.erl (2182B)


      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 
     15 -module(cowboy_tls).
     16 -behavior(ranch_protocol).
     17 
     18 -export([start_link/3]).
     19 -export([start_link/4]).
     20 -export([connection_process/4]).
     21 
     22 %% Ranch 1.
     23 -spec start_link(ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> {ok, pid()}.
     24 start_link(Ref, _Socket, Transport, Opts) ->
     25 	start_link(Ref, Transport, Opts).
     26 
     27 %% Ranch 2.
     28 -spec start_link(ranch:ref(), module(), cowboy:opts()) -> {ok, pid()}.
     29 start_link(Ref, Transport, Opts) ->
     30 	Pid = proc_lib:spawn_link(?MODULE, connection_process,
     31 		[self(), Ref, Transport, Opts]),
     32 	{ok, Pid}.
     33 
     34 -spec connection_process(pid(), ranch:ref(), module(), cowboy:opts()) -> ok.
     35 connection_process(Parent, Ref, Transport, Opts) ->
     36 	ProxyInfo = case maps:get(proxy_header, Opts, false) of
     37 		true ->
     38 			{ok, ProxyInfo0} = ranch:recv_proxy_header(Ref, 1000),
     39 			ProxyInfo0;
     40 		false ->
     41 			undefined
     42 	end,
     43 	{ok, Socket} = ranch:handshake(Ref),
     44 	case ssl:negotiated_protocol(Socket) of
     45 		{ok, <<"h2">>} ->
     46 			init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, cowboy_http2);
     47 		_ -> %% http/1.1 or no protocol negotiated.
     48 			init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, cowboy_http)
     49 	end.
     50 
     51 init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, Protocol) ->
     52 	_ = case maps:get(connection_type, Opts, supervisor) of
     53 		worker -> ok;
     54 		supervisor -> process_flag(trap_exit, true)
     55 	end,
     56 	Protocol:init(Parent, Ref, Socket, Transport, ProxyInfo, Opts).