-
Notifications
You must be signed in to change notification settings - Fork 573
/
conn.ex
74 lines (61 loc) · 1.89 KB
/
conn.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
defmodule Plug.Adapters.Cowboy.Conn do
@behaviour Plug.Conn.Adapter
@moduledoc false
def conn(req, transport) do
{path, req} = :cowboy_req.path req
{host, req} = :cowboy_req.host req
{port, req} = :cowboy_req.port req
{meth, req} = :cowboy_req.method req
{hdrs, req} = :cowboy_req.headers req
{qs, req} = :cowboy_req.qs req
{peer, req} = :cowboy_req.peer req
{remote_ip, _} = peer
%Plug.Conn{
adapter: {__MODULE__, req},
host: host,
method: meth,
owner: self(),
path_info: split_path(path),
peer: peer,
port: port,
remote_ip: remote_ip,
query_string: qs,
req_headers: hdrs,
request_path: path,
scheme: scheme(transport)
}
end
def send_resp(req, status, headers, body) do
status = Integer.to_string(status) <> " " <> Plug.Conn.Status.reason_phrase(status)
{:ok, req} = :cowboy_req.reply(status, headers, body, req)
{:ok, nil, req}
end
def send_file(req, 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
body_fun = fn(socket, transport) -> transport.sendfile(socket, path, offset, length) end
{:ok, req} = :cowboy_req.reply(status, headers, :cowboy_req.set_resp_body_fun(length, body_fun, req))
{:ok, nil, req}
end
def send_chunked(req, status, headers) do
{:ok, req} = :cowboy_req.chunked_reply(status, headers, req)
{:ok, nil, req}
end
def chunk(req, body) do
:cowboy_req.chunk(body, req)
end
def read_req_body(req, opts \\ []) do
:cowboy_req.body(req, opts)
end
## Helpers
defp scheme(:tcp), do: :http
defp scheme(:ssl), do: :https
defp split_path(path) do
segments = :binary.split(path, "/", [:global])
for segment <- segments, segment != "", do: segment
end
end