2019-07-10 05:13:23 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 05:13:23 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-11-11 13:59:25 +00:00
|
|
|
defmodule Phoenix.Transports.WebSocket.Raw do
|
2018-03-30 13:01:53 +00:00
|
|
|
import Plug.Conn,
|
|
|
|
only: [
|
|
|
|
fetch_query_params: 1,
|
|
|
|
send_resp: 3
|
|
|
|
]
|
|
|
|
|
2017-11-11 13:59:25 +00:00
|
|
|
alias Phoenix.Socket.Transport
|
|
|
|
|
|
|
|
def default_config do
|
|
|
|
[
|
|
|
|
timeout: 60_000,
|
|
|
|
transport_log: false,
|
|
|
|
cowboy: Phoenix.Endpoint.CowboyWebSocket
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def init(%Plug.Conn{method: "GET"} = conn, {endpoint, handler, transport}) do
|
|
|
|
{_, opts} = handler.__transport__(transport)
|
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> fetch_query_params
|
|
|
|
|> Transport.transport_log(opts[:transport_log])
|
2023-08-01 14:00:39 +00:00
|
|
|
|> Transport.force_ssl(handler, endpoint, opts)
|
2018-03-30 13:01:53 +00:00
|
|
|
|> Transport.check_origin(handler, endpoint, opts)
|
2017-11-11 13:59:25 +00:00
|
|
|
|
|
|
|
case conn do
|
|
|
|
%{halted: false} = conn ->
|
Fix incorrect use of connect/1
Hint from Phoenix 1.4.17, which has a connect/7 shim:
lib/phoenix/socket/transport.ex:
def connect(endpoint, handler, _transport_name, transport, serializers, params, _pid \\ self()) do
IO.warn "Phoenix.Socket.Transport.connect/7 is deprecated"
handler.connect(%{
endpoint: endpoint,
transport: transport,
options: [serializer: serializers],
params: params
})
end
2020-10-07 20:06:16 +00:00
|
|
|
case handler.connect(%{
|
2020-10-07 16:47:10 +00:00
|
|
|
endpoint: endpoint,
|
|
|
|
transport: transport,
|
|
|
|
options: [serializer: nil],
|
|
|
|
params: conn.params
|
|
|
|
}) do
|
2017-11-11 13:59:25 +00:00
|
|
|
{:ok, socket} ->
|
|
|
|
{:ok, conn, {__MODULE__, {socket, opts}}}
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2017-11-11 13:59:25 +00:00
|
|
|
:error ->
|
|
|
|
send_resp(conn, :forbidden, "")
|
|
|
|
{:error, conn}
|
|
|
|
end
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2017-11-11 13:59:25 +00:00
|
|
|
_ ->
|
|
|
|
{:error, conn}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def init(conn, _) do
|
|
|
|
send_resp(conn, :bad_request, "")
|
|
|
|
{:error, conn}
|
|
|
|
end
|
|
|
|
|
|
|
|
def ws_init({socket, config}) do
|
|
|
|
Process.flag(:trap_exit, true)
|
|
|
|
{:ok, %{socket: socket}, config[:timeout]}
|
|
|
|
end
|
|
|
|
|
|
|
|
def ws_handle(op, data, state) do
|
|
|
|
state.socket.handler
|
|
|
|
|> apply(:handle, [op, data, state])
|
|
|
|
|> case do
|
|
|
|
{op, data} ->
|
|
|
|
{:reply, {op, data}, state}
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2017-11-11 13:59:25 +00:00
|
|
|
{op, data, state} ->
|
|
|
|
{:reply, {op, data}, state}
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2017-11-11 13:59:25 +00:00
|
|
|
%{} = state ->
|
|
|
|
{:ok, state}
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2017-11-11 13:59:25 +00:00
|
|
|
_ ->
|
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
def ws_info({_, _} = tuple, state) do
|
2017-11-11 13:59:25 +00:00
|
|
|
{:reply, tuple, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
def ws_info(_tuple, state), do: {:ok, state}
|
|
|
|
|
|
|
|
def ws_close(state) do
|
|
|
|
ws_handle(:closed, :normal, state)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ws_terminate(reason, state) do
|
|
|
|
ws_handle(:closed, reason, state)
|
|
|
|
end
|
|
|
|
end
|