cowboy_clear.erl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. %% Copyright (c) 2016-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_clear).
  15. -behavior(ranch_protocol).
  16. -export([start_link/3]).
  17. -export([start_link/4]).
  18. -export([connection_process/4]).
  19. %% Ranch 1.
  20. -spec start_link(ranch:ref(), inet:socket(), module(), cowboy:opts()) -> {ok, pid()}.
  21. start_link(Ref, _Socket, Transport, Opts) ->
  22. start_link(Ref, Transport, Opts).
  23. %% Ranch 2.
  24. -spec start_link(ranch:ref(), module(), cowboy:opts()) -> {ok, pid()}.
  25. start_link(Ref, Transport, Opts) ->
  26. Pid = proc_lib:spawn_link(?MODULE, connection_process,
  27. [self(), Ref, Transport, Opts]),
  28. {ok, Pid}.
  29. -spec connection_process(pid(), ranch:ref(), module(), cowboy:opts()) -> ok.
  30. connection_process(Parent, Ref, Transport, Opts) ->
  31. ProxyInfo = case maps:get(proxy_header, Opts, false) of
  32. true ->
  33. {ok, ProxyInfo0} = ranch:recv_proxy_header(Ref, 1000),
  34. ProxyInfo0;
  35. false ->
  36. undefined
  37. end,
  38. {ok, Socket} = ranch:handshake(Ref),
  39. init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, cowboy_http).
  40. init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, Protocol) ->
  41. _ = case maps:get(connection_type, Opts, supervisor) of
  42. worker -> ok;
  43. supervisor -> process_flag(trap_exit, true)
  44. end,
  45. Protocol:init(Parent, Ref, Socket, Transport, ProxyInfo, Opts).