-
Notifications
You must be signed in to change notification settings - Fork 64
/
plug_utils.ex
84 lines (65 loc) · 2.51 KB
/
plug_utils.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
defmodule RigCloudEvents.PlugUtils do
@moduledoc """
Common code for dealing with CloudEvents in controllers.
The CloudEvents specification defines how CloudEvents must be accepted depending on
the transport; see [HTTP Transport Binding for CloudEvents].
We _deviate_ a bit from the spec by interpreting requests as using structured mode
if they come with content type `application/json` and without a `ce-specversion`
header. See `RigCloudEvents.PlugUtils.cloudevents_mode/1` for details.
[HTTP Transport Binding for CloudEvents]: https://github.com/cloudevents/spec/blob/master/http-transport-binding.md
"""
alias Plug.Conn
require Logger
# ---
def handle_cloudevent(conn, handlers \\ []) do
mode = cloudevents_mode(conn)
do_handle_cloudevent(conn, handlers, mode)
end
# ---
def do_handle_cloudevent(conn, handlers, mode) do
Logger.debug(fn -> "Received CloudEvent in #{mode} mode" end)
handler = handlers[mode]
if is_nil(handler),
do: Conn.send_resp(conn, :unsupported_media_type, not_implemented_message(mode)),
else: handler.(conn)
end
# ---
defp not_implemented_message(mode),
do: """
The #{mode} CloudEvents HTTP transport binding mode is currently not supported.
"""
# ---
defp cloudevents_mode(conn) do
content_type = content_type(conn)
# The content type is the primary criterion.
case content_type do
{"application", "cloudevents-batch"} ->
:batched
{"application", "cloudevents+" <> _} ->
:structured
{"application", "json"} ->
# According to the spec this should be :binary. But we fall back to :structured
# in case the specversion header is not present. People will send us
# CloudEvents, which are JSONs by default, as JSONs, that is, with content type
# "application/json". To some people this seems intuitive and we try to be nice
# here. Also, with the specversion header not present, it's not proper :binary
# anyway.
if Conn.get_req_header(conn, "ce-specversion") == [] do
# ce-specversion header not present, so we assume structured.
:structured
else
# The body is the JSON encoded data field.
:binary
end
_ ->
# Fallback according to the spec:
:binary
end
end
# ---
defp content_type(conn) do
[as_string] = Conn.get_req_header(conn, "content-type")
{:ok, type, subtype, _params} = Conn.Utils.content_type(as_string)
{type, subtype}
end
end