forked from elixir-plug/plug_cowboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.ex
158 lines (133 loc) · 4.01 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
defmodule Plug.Cowboy.Conn do
@behaviour Plug.Conn.Adapter
@moduledoc false
def conn(req) do
%{
path: path,
host: host,
port: port,
method: method,
headers: headers,
qs: qs,
peer: {remote_ip, _}
} = req
%Plug.Conn{
adapter: {__MODULE__, req},
host: host,
method: method,
owner: self(),
path_info: split_path(path),
port: port,
remote_ip: remote_ip,
query_string: qs,
req_headers: to_headers_list(headers),
request_path: path,
scheme: String.to_atom(:cowboy_req.scheme(req))
}
end
@impl true
def send_resp(req, status, headers, body) do
headers = to_headers_map(headers)
status = Integer.to_string(status) <> " " <> Plug.Conn.Status.reason_phrase(status)
req = :cowboy_req.reply(status, headers, body, req)
{:ok, nil, req}
end
@impl true
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 = {:sendfile, offset, length, path}
headers = to_headers_map(headers)
req = :cowboy_req.reply(status, headers, body, req)
{:ok, nil, req}
end
@impl true
def send_chunked(req, status, headers) do
headers = to_headers_map(headers)
req = :cowboy_req.stream_reply(status, headers, req)
{:ok, nil, req}
end
@impl true
def chunk(req, body) do
:cowboy_req.stream_body(body, :nofin, req)
end
@impl true
def read_req_body(req, opts) do
length = Keyword.get(opts, :length, 8_000_000)
read_length = Keyword.get(opts, :read_length, 1_000_000)
read_timeout = Keyword.get(opts, :read_timeout, 15_000)
opts = %{length: read_length, period: read_timeout}
read_req_body(req, opts, length, [])
end
defp read_req_body(req, opts, length, acc) when length >= 0 do
case :cowboy_req.read_body(req, opts) do
{:ok, data, req} -> {:ok, IO.iodata_to_binary([acc | data]), req}
{:more, data, req} -> read_req_body(req, opts, length - byte_size(data), [acc | data])
end
end
defp read_req_body(req, _opts, _length, acc) do
{:more, IO.iodata_to_binary(acc), req}
end
@impl true
def inform(req, status, headers) do
:cowboy_req.inform(status, to_headers_map(headers), req)
end
@impl true
def upgrade(req, :websocket, {_handler, _state, _cowboy_opts} = args) do
{:ok, Map.put(req, :upgrade, {:websocket, args})}
end
def upgrade(_req, _protocol, _args), do: {:error, :not_supported}
@impl true
def push(req, path, headers) do
opts =
case {req.port, req.sock} do
{:undefined, {_, port}} -> %{port: port}
{port, _} when port in [80, 443] -> %{}
{port, _} -> %{port: port}
end
:cowboy_req.push(path, to_headers_map(headers), req, opts)
end
@impl true
def get_peer_data(%{peer: {ip, port}, cert: cert}) do
%{
address: ip,
port: port,
ssl_cert: if(cert == :undefined, do: nil, else: cert)
}
end
@impl true
def get_http_protocol(req) do
:cowboy_req.version(req)
end
## Helpers
defp to_headers_list(headers) when is_list(headers) do
headers
end
defp to_headers_list(headers) when is_map(headers) do
:maps.to_list(headers)
end
defp to_headers_map(headers) when is_list(headers) do
# Group set-cookie headers into a list for a single `set-cookie`
# key since cowboy 2 requires headers as a map.
Enum.reduce(headers, %{}, fn
{key = "set-cookie", value}, acc ->
case acc do
%{^key => existing} -> %{acc | key => [value | existing]}
%{} -> Map.put(acc, key, [value])
end
{key, value}, acc ->
case acc do
%{^key => existing} -> %{acc | key => existing <> ", " <> value}
%{} -> Map.put(acc, key, value)
end
end)
end
defp split_path(path) do
segments = :binary.split(path, "/", [:global])
for segment <- segments, segment != "", do: segment
end
end