-
Notifications
You must be signed in to change notification settings - Fork 16
/
decode.ex
70 lines (56 loc) · 2 KB
/
decode.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
defmodule Conduit.Plug.Decode do
use Conduit.Plug.Builder
@moduledoc """
Decodes the message body based on the content encoding.
It uses in order of preference:
1. The content encoding specified for the plug.
2. The content encoding specified on the message.
3. The default content encoding `identity`.
The location of the content encoding can be changed from `content_encoding`
to a header with the `:header` option. This is useful for having multiple
encodings, like a transfer encoding.
This plug should be used in an incoming pipeline. Generally before
a `Conduit.Plug.Parse` plug.
## Examples
plug Conduit.Plug.Decode
plug Conduit.Plug.Decode, content_encoding: "gzip"
plug Conduit.Plug.Decode, header: "transfer_encoding"
iex> import Conduit.Message
iex> message =
iex> %Conduit.Message{}
iex> |> put_body("{}")
iex> |> Conduit.Plug.Decode.run
iex> message.body
"{}"
iex> message.content_encoding
"identity"
iex> import Conduit.Message
iex> message =
iex> %Conduit.Message{}
iex> |> put_body("{}")
iex> |> Conduit.Plug.Decode.run(header: "transfer_encoding")
iex> message.body
"{}"
iex> get_header(message, "transfer_encoding")
"identity"
"""
alias Conduit.Encoding
@doc """
Formats the message body based on the content encoding.
"""
@default_content_encoding "identity"
def call(message, next, opts) do
content_encoding =
Keyword.get(opts, :content_encoding) || Map.get(message, :content_encoding) || @default_content_encoding
message
|> put_content_encoding_at(Keyword.get(opts, :header), content_encoding)
|> Encoding.decode(content_encoding, opts)
|> next.()
end
defp put_content_encoding_at(message, nil, content_encoding) do
put_content_encoding(message, content_encoding)
end
defp put_content_encoding_at(message, header, content_encoding) do
put_header(message, header, content_encoding)
end
end