logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

raw.ex (2063B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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.check_origin(handler, endpoint, opts)
  25. case conn do
  26. %{halted: false} = conn ->
  27. case handler.connect(%{
  28. endpoint: endpoint,
  29. transport: transport,
  30. options: [serializer: nil],
  31. params: conn.params
  32. }) do
  33. {:ok, socket} ->
  34. {:ok, conn, {__MODULE__, {socket, opts}}}
  35. :error ->
  36. send_resp(conn, :forbidden, "")
  37. {:error, conn}
  38. end
  39. _ ->
  40. {:error, conn}
  41. end
  42. end
  43. def init(conn, _) do
  44. send_resp(conn, :bad_request, "")
  45. {:error, conn}
  46. end
  47. def ws_init({socket, config}) do
  48. Process.flag(:trap_exit, true)
  49. {:ok, %{socket: socket}, config[:timeout]}
  50. end
  51. def ws_handle(op, data, state) do
  52. state.socket.handler
  53. |> apply(:handle, [op, data, state])
  54. |> case do
  55. {op, data} ->
  56. {:reply, {op, data}, state}
  57. {op, data, state} ->
  58. {:reply, {op, data}, state}
  59. %{} = state ->
  60. {:ok, state}
  61. _ ->
  62. {:ok, state}
  63. end
  64. end
  65. def ws_info({_, _} = tuple, state) do
  66. {:reply, tuple, state}
  67. end
  68. def ws_info(_tuple, state), do: {:ok, state}
  69. def ws_close(state) do
  70. ws_handle(:closed, :normal, state)
  71. end
  72. def ws_terminate(reason, state) do
  73. ws_handle(:closed, reason, state)
  74. end
  75. end