@@ -79,6 +79,7 @@ defmodule HttpRouter do
79
79
"""
80
80
81
81
import HttpRouter.Util
82
+ alias Application , as: A
82
83
83
84
@ typep ast :: tuple
84
85
@ http_methods [ :get , :post , :put , :patch , :delete , :any ]
@@ -96,24 +97,35 @@ defmodule HttpRouter do
96
97
Module . register_attribute ( __MODULE__ , :version , accumulate: false )
97
98
98
99
# 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
105
107
end
106
108
end
107
109
108
110
@ doc false
109
111
defmacro __before_compile__ ( env ) do
110
112
# Plugs we want predefined but aren't necessary to be before
111
113
# 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 } ,
116
115
{ :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
+
117
129
{ conn , body } = Enum . reverse ( defaults ) ++
118
130
Module . get_attribute ( env . module , :plugs )
119
131
|> Plug.Builder . compile
@@ -129,13 +141,15 @@ defmodule HttpRouter do
129
141
130
142
defoverridable [ init: 1 , call: 2 ]
131
143
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
139
153
end
140
154
141
155
def match ( conn , _opts ) do
@@ -273,6 +287,19 @@ defmodule HttpRouter do
273
287
end
274
288
end
275
289
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
+
276
303
defp ignore_args ( str ) do
277
304
str
278
305
|> String . to_char_list
@@ -377,9 +404,9 @@ defmodule HttpRouter do
377
404
## Grabbed from `Plug.Router`
378
405
379
406
defp prep_match ( method , route , caller ) do
380
- { method , guard } = convert_methods ( List . wrap ( method ) )
407
+ { method , guard } = method |> List . wrap |> convert_methods
381
408
{ 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
383
410
{ method , guards , vars , match }
384
411
end
385
412
0 commit comments