logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

transports.ex (2010B)


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