Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.8KB

  1. defmodule Phoenix.Transports.WebSocket.Raw do
  2. import Plug.Conn,
  3. only: [
  4. fetch_query_params: 1,
  5. send_resp: 3
  6. ]
  7. alias Phoenix.Socket.Transport
  8. def default_config do
  9. [
  10. timeout: 60_000,
  11. transport_log: false,
  12. cowboy: Phoenix.Endpoint.CowboyWebSocket
  13. ]
  14. end
  15. def init(%Plug.Conn{method: "GET"} = conn, {endpoint, handler, transport}) do
  16. {_, opts} = handler.__transport__(transport)
  17. conn =
  18. conn
  19. |> fetch_query_params
  20. |> Transport.transport_log(opts[:transport_log])
  21. |> Transport.force_ssl(handler, endpoint, opts)
  22. |> Transport.check_origin(handler, endpoint, opts)
  23. case conn do
  24. %{halted: false} = conn ->
  25. case Transport.connect(endpoint, handler, transport, __MODULE__, nil, conn.params) do
  26. {:ok, socket} ->
  27. {:ok, conn, {__MODULE__, {socket, opts}}}
  28. :error ->
  29. send_resp(conn, :forbidden, "")
  30. {:error, conn}
  31. end
  32. _ ->
  33. {:error, conn}
  34. end
  35. end
  36. def init(conn, _) do
  37. send_resp(conn, :bad_request, "")
  38. {:error, conn}
  39. end
  40. def ws_init({socket, config}) do
  41. Process.flag(:trap_exit, true)
  42. {:ok, %{socket: socket}, config[:timeout]}
  43. end
  44. def ws_handle(op, data, state) do
  45. state.socket.handler
  46. |> apply(:handle, [op, data, state])
  47. |> case do
  48. {op, data} ->
  49. {:reply, {op, data}, state}
  50. {op, data, state} ->
  51. {:reply, {op, data}, state}
  52. %{} = state ->
  53. {:ok, state}
  54. _ ->
  55. {:ok, state}
  56. end
  57. end
  58. def ws_info({_, _} = tuple, state) do
  59. {:reply, tuple, state}
  60. end
  61. def ws_info(_tuple, state), do: {:ok, state}
  62. def ws_close(state) do
  63. ws_handle(:closed, :normal, state)
  64. end
  65. def ws_terminate(reason, state) do
  66. ws_handle(:closed, reason, state)
  67. end
  68. end