Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

95 líneas
2.1KB

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