/
raxx.ex
374 lines (284 loc) · 8.06 KB
/
raxx.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
defmodule Raxx do
@moduledoc """
Tooling to work with HTTP.
Several data structures are defined to model parts of the communication between client and server.
- `Raxx.Request`: metadata sent by a client before sending content.
- `Raxx.Response`: metadata sent by a server before sending content.
- `Raxx.Data`: A part of a messages content.
- `Raxx.Tail`: metadata set by client or server to conclude communication.
This module contains functions to create and manipulate these structures.
**See `Raxx.Server` for implementing a web application.**
"""
@http_methods [
:GET,
:POST,
:PUT,
:PATCH,
:DELETE,
:HEAD,
:OPTIONS
]
@doc """
Construct a `Raxx.Request`.
An HTTP request must have a method and path.
If the location argument is a relative path the scheme and authority values will be unset.
When these values can be inferred from the location they will be set.
The method must be an atom for one of the HTTP methods
`#{inspect(@http_methods)}`
The request will have no body or headers.
These can be added with `set_header/3` and `set_body/2`.
## Examples
iex> request(:HEAD, "/").method
:HEAD
iex> request(:GET, "/").path
[]
iex> request(:GET, "/foo/bar").path
["foo", "bar"]
iex> request(:GET, "https:///").scheme
:https
iex> request(:GET, "https://example.com").authority
"example.com"
iex> request(:GET, "/?foo=bar").query
%{"foo" => "bar"}
iex> request(:GET, "/?foo[bob]=bar").query
%{"foo" => %{"bob" => "bar"}}
iex> request(:GET, "/").headers
[]
iex> request(:GET, "/").body
false
"""
def request(method, url) when is_binary(url) do
url = URI.parse(url)
if url.query do
{:ok, query} = URI2.Query.decode(url.query)
request(method, %{url | query: query})
else
request(method, url)
end
end
def request(method, url) when method in @http_methods do
scheme =
if url.scheme do
url.scheme |> String.to_existing_atom()
end
segments = split_path(url.path || "/")
struct(
Raxx.Request,
scheme: scheme,
authority: url.authority,
method: method,
path: segments,
query: url.query,
headers: [],
body: false
)
end
@doc """
Construct a `Raxx.Response`.
The responses HTTP status code can be provided as an integer,
or will be translated from a known atom.
The response will have no body or headers.
These can be added with `set_header/3` and `set_body/2`.
## Examples
iex> response(200).status
200
iex> response(:no_content).status
204
iex> response(200).headers
[]
iex> response(200).body
false
"""
def response(status_code) when is_integer(status_code) do
struct(Raxx.Response, status: status_code, headers: [], body: false)
end
filepath = Path.join(__DIR__, "status.rfc7231")
@external_resource filepath
{:ok, file} = File.read(filepath)
status_lines = String.split(String.trim(file), "\n")
statuses = status_lines |> Enum.map(fn(status_line) ->
{code, " " <> reason_phrase} = Integer.parse(status_line)
{code, reason_phrase}
end)
for {status_code, reason_phrase} <- statuses do
reason =
reason_phrase
|> String.downcase()
|> String.replace(" ", "_")
|> String.to_atom()
def response(unquote(reason)) do
response(unquote(status_code))
end
end
@doc """
The RFC7231 specified reason phrase for each known HTTP status code
## Examples
iex> reason_phrase(200)
"OK"
iex> reason_phrase(500)
"Internal Server Error"
"""
for {status_code, reason_phrase} <- statuses do
def reason_phrase(unquote(status_code)) do
unquote(reason_phrase)
end
end
@doc """
Construct a `Raxx.Data`
Wrap a piece of data as being part of a message's body.
## Examples
iex> data("Hi").data
"Hi"
"""
def data(data) do
%Raxx.Data{data: data}
end
@doc """
Construct a `Raxx.Tail`
## Examples
iex> tail([{"digest", "opaque-data"}]).headers
[{"digest", "opaque-data"}]
iex> tail().headers
[]
"""
def tail(headers \\ []) do
%Raxx.Tail{headers: headers}
end
@doc """
Does the message struct contain all the data to be sent.
## Examples
iex> request(:GET, "/")
...> |> complete?()
true
iex> response(:ok)
...> |> set_body("Hello, World!")
...> |> complete?()
true
iex> response(:ok)
...> |> set_body(true)
...> |> complete?()
false
"""
def complete?(%{body: body}) when is_binary(body) do
true
end
def complete?(%{body: body}) do
!body
end
@doc """
Add a query value to a request
## Examples
iex> request(:GET, "/")
...> |> set_query(%{"value" => "1"})
...> |> Map.get(:query)
%{"value" => "1"}
"""
def set_query(request = %Raxx.Request{query: nil}, query) do
%{request | query: query}
end
# get_header
# def set_header(r, name, value) do
# if has_header?(r, name) do
# raise "set only once"
# end
# end
@doc """
Set the value of a header field.
## Examples
iex> request(:GET, "/")
...> |> set_header("referer", "example.com")
...> |> set_header("accept", "text/html")
...> |> Map.get(:headers)
[{"referer", "example.com"}, {"accept", "text/html"}]
"""
def set_header(message = %{headers: headers}, name, value) do
if String.downcase(name) != name do
raise "Header keys must be lowercase"
end
if :proplists.is_defined(name, headers) do
raise "Headers should not be duplicated"
end
%{message | headers: headers ++ [{name, value}]}
end
@doc """
Add a complete body to a message.
## Examples
iex> request(:GET, "/")
...> |> set_body("Hello")
...> |> Map.get(:body)
"Hello"
"""
def set_body(message = %{body: false}, body) do
%{message | body: body}
end
@doc """
Split a path on forward slashes.
## Examples
iex> split_path("/foo/bar")
["foo", "bar"]
"""
def split_path(path_string) do
path_string
|> String.split("/", [trim: true])
end
######## COPIED FROM PLUG ########
@doc ~S"""
Escapes the given HTML to string.
iex> html_escape("foo")
"foo"
iex> html_escape("<foo>")
"<foo>"
iex> html_escape("quotes: \" & \'")
"quotes: " & '"
"""
@spec html_escape(String.t()) :: String.t()
def html_escape(data) when is_binary(data) do
IO.iodata_to_binary(to_iodata(data, 0, data, []))
end
@doc ~S"""
Escapes the given HTML to iodata.
iex> html_escape_to_iodata("foo")
"foo"
iex> html_escape_to_iodata("<foo>")
[[[] | "<"], "foo" | ">"]
iex> html_escape_to_iodata("quotes: \" & \'")
[[[[], "quotes: " | """], " " | "&"], " " | "'"]
"""
@spec html_escape_to_iodata(String.t()) :: iodata
def html_escape_to_iodata(data) when is_binary(data) do
to_iodata(data, 0, data, [])
end
escapes = [
{?<, "<"},
{?>, ">"},
{?&, "&"},
{?", """},
{?', "'"}
]
for {match, insert} <- escapes do
defp to_iodata(<<unquote(match), rest::bits>>, skip, original, acc) do
to_iodata(rest, skip + 1, original, [acc | unquote(insert)])
end
end
defp to_iodata(<<_char, rest::bits>>, skip, original, acc) do
to_iodata(rest, skip, original, acc, 1)
end
defp to_iodata(<<>>, _skip, _original, acc) do
acc
end
for {match, insert} <- escapes do
defp to_iodata(<<unquote(match), rest::bits>>, skip, original, acc, len) do
part = binary_part(original, skip, len)
to_iodata(rest, skip + len + 1, original, [acc, part | unquote(insert)])
end
end
defp to_iodata(<<_char, rest::bits>>, skip, original, acc, len) do
to_iodata(rest, skip, original, acc, len + 1)
end
defp to_iodata(<<>>, 0, original, _acc, _len) do
original
end
defp to_iodata(<<>>, skip, original, acc, len) do
[acc | binary_part(original, skip, len)]
end
end