<commit_before>defmodule Phoenix.Endpoint.CowboyWebSocket do
  @moduledoc false

  if Code.ensure_loaded?(:cowboy_websocket_handler) do
    @behaviour :cowboy_websocket_handler
  end

  @connection Plug.Adapters.Cowboy.Conn
  @already_sent {:plug_conn, :sent}

  def init({transport, :http}, req, {module, args}) when transport in [:tcp, :ssl] do
    {endpoint, handler, opts} = args
    conn = @connection.conn(req, transport)

    try do
      case Phoenix.Transports.WebSocket.connect(conn, endpoint, handler, opts) do
        {:ok, %Plug.Conn{adapter: {@connection, req}} = conn, args} ->
          timeout = Keyword.fetch!(opts, :timeout)
          req = copy_resp_headers(conn, req)
          {:upgrade, :protocol, __MODULE__, req, {handler, args, timeout}}

        {:error, %Plug.Conn{adapter: {@connection, req}} = conn} ->
          {:shutdown, copy_resp_headers(conn, req), :no_state}
      end
    catch
      kind, reason ->
        # Although we are not performing a call, we are using the call
        # function for now so it is properly handled in error reports.
        mfa = {module, :call, [conn, args]}
        {:upgrade, :protocol, __MODULE__, req, {:error, mfa, kind, reason, System.stacktrace()}}
    after
      receive do
        @already_sent -> :ok
      after
        0 -> :ok
      end
    end<commit_msg>Allow custom websocket transports in Cowboy 1 (#3516)<commit_after>defmodule Phoenix.Endpoint.CowboyWebSocket do
  @moduledoc false

  if Code.ensure_loaded?(:cowboy_websocket_handler) do
    @behaviour :cowboy_websocket_handler
  end

  @connection Plug.Adapters.Cowboy.Conn
  @already_sent {:plug_conn, :sent}

  def init({transport, :http}, req, {module, args}) when transport in [:tcp, :ssl] do
    {endpoint, handler, opts} = args
    conn = @connection.conn(req, transport)

    try do
      case module.connect(conn, endpoint, handler, opts) do
        {:ok, %Plug.Conn{adapter: {@connection, req}} = conn, args} ->
          timeout = Keyword.fetch!(opts, :timeout)
          req = copy_resp_headers(conn, req)
          {:upgrade, :protocol, __MODULE__, req, {handler, args, timeout}}

        {:error, %Plug.Conn{adapter: {@connection, req}} = conn} ->
          {:shutdown, copy_resp_headers(conn, req), :no_state}
      end
    catch
      kind, reason ->
        # Although we are not performing a call, we are using the call
        # function for now so it is properly handled in error reports.
        mfa = {module, :call, [conn, args]}
        {:upgrade, :protocol, __MODULE__, req, {:error, mfa, kind, reason, System.stacktrace()}}
    after
      receive do
        @already_sent -> :ok
      after
        0 -> :ok
      end
    end
{'fraction_non_alphanumeric': 0.12239583333333333, 'fraction_numerical': 0.0026041666666666665, 'mean_word_length': 2.9198250728862973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4427600', 'n_tokens_mistral': 860, 'n_tokens_neox': 820, 'n_words': 250}