Skip to content

Commit fe13267

Browse files
committed
allow for default plugs to be disabled
1 parent ee0bb1c commit fe13267

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

lib/http_router.ex

+46-19
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ defmodule HttpRouter do
7979
"""
8080

8181
import HttpRouter.Util
82+
alias Application, as: A
8283

8384
@typep ast :: tuple
8485
@http_methods [ :get, :post, :put, :patch, :delete, :any ]
@@ -96,24 +97,35 @@ defmodule HttpRouter do
9697
Module.register_attribute(__MODULE__, :version, accumulate: false)
9798

9899
# Plugs we want early in the stack
99-
# plug HttpRouter.Request.TranslateExtensions
100-
add_parsers = Application.get_env(:http_router, :parsers, [])
101-
plug Plug.Parsers, parsers: [ :json,
102-
:urlencoded,
103-
:multipart ]
104-
++ add_parsers
100+
parsers_opts = [ parsers: options[:parsers] ]
101+
if :json in parsers_opts[:parsers] do
102+
parsers_opts = parsers_opts
103+
|> Keyword.put(:json_decoder, options[:json_decoder])
104+
end
105+
106+
plug Plug.Parsers, parsers_opts
105107
end
106108
end
107109

108110
@doc false
109111
defmacro __before_compile__(env) do
110112
# Plugs we want predefined but aren't necessary to be before
111113
# user-defined plugs
112-
defaults = [ { Plug.Head, [], true },
113-
{ Plug.MethodOverride, [], true },
114-
{ :copy_req_content_type, [], true },
115-
{ :match, [], true },
114+
defaults = [ { :match, [], true },
116115
{ :dispatch, [], true } ]
116+
117+
if options[:allow_copy_req_content_type] == true do
118+
defaults = [ { :copy_req_content_type, [], true } | defaults ]
119+
end
120+
121+
if options[:allow_method_override] == true do
122+
defaults = [ { Plug.MethodOverride, [], true } | defaults ]
123+
end
124+
125+
if options[:allow_head] == true do
126+
defaults = [ { Plug.Head, [], true } | defaults ]
127+
end
128+
117129
{ conn, body } = Enum.reverse(defaults) ++
118130
Module.get_attribute(env.module, :plugs)
119131
|> Plug.Builder.compile
@@ -129,13 +141,15 @@ defmodule HttpRouter do
129141

130142
defoverridable [init: 1, call: 2]
131143

132-
def copy_req_content_type(conn, _opts) do
133-
default = Application.get_env(:http_router, :default_content_type, "application/json; charset=utf-8")
134-
content_type = case Plug.Conn.get_req_header conn, "content-type" do
135-
[content_type] -> content_type
136-
_ -> default
137-
end
138-
conn |> Plug.Conn.put_resp_header("content-type", content_type)
144+
if options[:allow_copy_req_content_type] == true do
145+
def copy_req_content_type(conn, _opts) do
146+
default = options[:default_content_type]
147+
content_type = case Plug.Conn.get_req_header conn, "content-type" do
148+
[content_type] -> content_type
149+
_ -> default
150+
end
151+
conn |> Plug.Conn.put_resp_header("content-type", content_type)
152+
end
139153
end
140154

141155
def match(conn, _opts) do
@@ -273,6 +287,19 @@ defmodule HttpRouter do
273287
end
274288
end
275289

290+
## Private API
291+
292+
@app A.get_env(:http_router, :otp_app, :http_router)
293+
@doc false
294+
def options, do: [
295+
allow_copy_req_content_type: A.get_env(@app, :allow_copy_req_content_type, true),
296+
allow_head: A.get_env(@app, :allow_head, true),
297+
allow_method_override: A.get_env(@app, :allow_method_override, true),
298+
default_content_type: A.get_env(@app, :default_content_type, "text/html; charset=utf-8"),
299+
json_decoder: A.get_env(@app, :json_decoder, Poison),
300+
parsers: A.get_env(@app, :parsers, [:json, :urlencoded, :multipart])
301+
]
302+
276303
defp ignore_args(str) do
277304
str
278305
|> String.to_char_list
@@ -377,9 +404,9 @@ defmodule HttpRouter do
377404
## Grabbed from `Plug.Router`
378405

379406
defp prep_match(method, route, caller) do
380-
{ method, guard } = convert_methods(List.wrap(method))
407+
{ method, guard } = method |> List.wrap |> convert_methods
381408
{ path, guards } = extract_path_and_guards(route, guard)
382-
{ vars, match } = build_spec(Macro.expand(path, caller))
409+
{ vars, match } = path |> Macro.expand(caller) |> build_spec
383410
{ method, guards, vars, match }
384411
end
385412

test/http_router_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ defmodule HttpRouterTest do
133133

134134
assert conn.state === :sent
135135
assert conn.status === 200
136-
assert get_resp_header(conn, "content-type") === ["application/json; charset=utf-8"]
136+
assert get_resp_header(conn, "content-type") === ["text/html; charset=utf-8"]
137137
end
138138

139139
test "resource/2 index with prepended path" do

0 commit comments

Comments
 (0)