Packages
livebook
0.17.3
0.19.8
0.19.7
0.19.6
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.3
0.17.2
0.17.1
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.14.0-rc.1
0.14.0-rc.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
Automate code & data workflows with interactive notebooks
Current section
Files
Jump to
Current section
Files
lib/livebook/proxy/adapter.ex
defmodule Livebook.Proxy.Adapter do
@moduledoc false
# Implements a Plug adapter for handling `conn` within the runtime.
#
# All actions are forwarded to the parent process (`Livebook.Proxy.Server`),
# which operates within Livebook itself.
@behaviour Plug.Conn.Adapter
@impl true
def send_resp({pid, ref}, status, headers, body) do
send(pid, {:send_resp, self(), ref, status, headers, body})
receive do
{^ref, :ok} -> {:ok, body, {pid, ref}}
{:DOWN, ^ref, _, _, reason} -> exit_fun(:send_resp, 4, reason)
end
end
@impl true
def get_peer_data({pid, ref}) do
send(pid, {:get_peer_data, self(), ref})
receive do
{^ref, peer_data} -> peer_data
{:DOWN, ^ref, _, _, reason} -> exit_fun(:get_peer_data, 1, reason)
end
end
@impl true
def get_http_protocol({pid, ref}) do
send(pid, {:get_http_protocol, self(), ref})
receive do
{^ref, http_protocol} -> http_protocol
{:DOWN, ^ref, _, _, reason} -> exit_fun(:get_http_protocol, 1, reason)
end
end
@impl true
def read_req_body({pid, ref}, opts) do
send(pid, {:read_req_body, self(), ref, opts})
receive do
{^ref, {:ok, data}} -> {:ok, data, {pid, ref}}
{^ref, {:more, data}} -> {:more, data, {pid, ref}}
{^ref, {:error, _} = error} -> error
{:DOWN, ^ref, _, _, reason} -> exit_fun(:read_req_body, 2, reason)
end
end
@impl true
def send_chunked({pid, ref}, status, headers) do
send(pid, {:send_chunked, self(), ref, status, headers})
receive do
{^ref, :ok} -> {:ok, nil, {pid, ref}}
{:DOWN, ^ref, _, _, reason} -> exit_fun(:send_chunked, 3, reason)
end
end
@impl true
def chunk({pid, ref}, chunk) do
send(pid, {:chunk, self(), ref, chunk})
receive do
{^ref, :ok} -> :ok
{^ref, {:error, _} = error} -> error
{:DOWN, ^ref, _, _, reason} -> exit_fun(:chunk, 2, reason)
end
end
@impl true
def inform({pid, ref}, status, headers) do
send(pid, {:inform, self(), ref, status, headers})
receive do
{^ref, :ok} -> {:ok, {pid, ref}}
{:DOWN, ^ref, _, _, reason} -> exit_fun(:inform, 3, reason)
end
end
@impl true
def send_file({pid, ref}, status, headers, path, offset, length) do
%File.Stat{type: :regular, size: size} = File.stat!(path)
length =
cond do
length == :all -> size
is_integer(length) -> length
end
{:ok, body} =
File.open!(path, [:read, :raw, :binary], fn device ->
:file.pread(device, offset, length)
end)
send(pid, {:send_resp, self(), ref, status, headers, body})
receive do
{^ref, :ok} -> {:ok, body, {pid, ref}}
{:DOWN, ^ref, _, _, reason} -> exit_fun(:send_file, 6, reason)
end
end
@impl true
def upgrade(_payload, _protocol, _opts), do: {:error, :not_supported}
@impl true
def push(_payload, _path, _headers), do: {:error, :not_supported}
defp exit_fun(fun, arity, reason) do
exit({{__MODULE__, fun, arity}, reason})
end
end