@@ -69,8 +69,8 @@ defmodule Plug.Adapters.Cowboy do
69
69
Plug.Adapters.Cowboy.shutdown MyPlug.HTTP
70
70
71
71
"""
72
- @ spec http ( module ( ) , Keyword . t , Keyword . t ) ::
73
- { :ok , pid } | { :error , :eaddrinuse } | { :error , term }
72
+ @ spec http ( module ( ) , Keyword . t ( ) , Keyword . t ( ) ) ::
73
+ { :ok , pid } | { :error , :eaddrinuse } | { :error , term }
74
74
def http ( plug , opts , cowboy_options \\ [ ] ) do
75
75
run ( :http , plug , opts , cowboy_options )
76
76
end
@@ -103,8 +103,8 @@ defmodule Plug.Adapters.Cowboy do
103
103
Plug.Adapters.Cowboy.shutdown MyPlug.HTTPS
104
104
105
105
"""
106
- @ spec https ( module ( ) , Keyword . t , Keyword . t ) ::
107
- { :ok , pid } | { :error , :eaddrinuse } | { :error , term }
106
+ @ spec https ( module ( ) , Keyword . t ( ) , Keyword . t ( ) ) ::
107
+ { :ok , pid } | { :error , :eaddrinuse } | { :error , term }
108
108
def https ( plug , opts , cowboy_options \\ [ ] ) do
109
109
Application . ensure_all_started ( :ssl )
110
110
run ( :https , plug , opts , cowboy_options )
@@ -127,10 +127,13 @@ defmodule Plug.Adapters.Cowboy do
127
127
# TODO: Remove this once we require Elixir v1.5+
128
128
def child_spec ( scheme , plug , opts , cowboy_options \\ [ ] ) do
129
129
[ ref , nb_acceptors , trans_opts , proto_opts ] = args ( scheme , plug , opts , cowboy_options )
130
- ranch_module = case scheme do
131
- :http -> :ranch_tcp
132
- :https -> :ranch_ssl
133
- end
130
+
131
+ ranch_module =
132
+ case scheme do
133
+ :http -> :ranch_tcp
134
+ :https -> :ranch_ssl
135
+ end
136
+
134
137
:ranch . child_spec ( ref , nb_acceptors , ranch_module , trans_opts , :cowboy_protocol , proto_opts )
135
138
end
136
139
@@ -158,6 +161,7 @@ defmodule Plug.Adapters.Cowboy do
158
161
def child_spec ( opts ) do
159
162
scheme = Keyword . fetch! ( opts , :scheme )
160
163
cowboy_opts = Keyword . get ( opts , :options , [ ] )
164
+
161
165
{ plug , plug_opts } =
162
166
case Keyword . fetch! ( opts , :plug ) do
163
167
{ _ , _ } = tuple -> tuple
@@ -167,8 +171,7 @@ defmodule Plug.Adapters.Cowboy do
167
171
{ id , start , restart , shutdown , type , modules } =
168
172
child_spec ( scheme , plug , plug_opts , cowboy_opts )
169
173
170
- % { id: id , start: start , restart: restart ,
171
- shutdown: shutdown , type: type , modules: modules }
174
+ % { id: id , start: start , restart: restart , shutdown: shutdown , type: type , modules: modules }
172
175
end
173
176
174
177
## Helpers
@@ -181,25 +184,29 @@ defmodule Plug.Adapters.Cowboy do
181
184
case Application . spec ( :cowboy , :vsn ) do
182
185
'1.' ++ _ ->
183
186
:ok
187
+
184
188
vsn ->
185
189
raise "you are using Plug.Adapters.Cowboy (for Cowboy 1) but your current Cowboy " <>
186
- "version is #{ vsn } . Please update your mix.exs file accordingly"
190
+ "version is #{ vsn } . Please update your mix.exs file accordingly"
187
191
end
192
+
188
193
{ :error , { :cowboy , _ } } ->
189
194
raise "could not start the Cowboy application. Please ensure it is listed as a dependency in your mix.exs"
190
195
end
196
+
191
197
apply ( :cowboy , :"start_#{ scheme } " , args ( scheme , plug , opts , cowboy_options ) )
192
198
end
193
199
194
200
defp normalize_cowboy_options ( cowboy_options , :http ) do
195
- Keyword . put_new cowboy_options , :port , 4000
201
+ Keyword . put_new ( cowboy_options , :port , 4000 )
196
202
end
197
203
198
204
defp normalize_cowboy_options ( cowboy_options , :https ) do
199
205
assert_ssl_options ( cowboy_options )
200
- cowboy_options = Keyword . put_new cowboy_options , :port , 4040
201
- cowboy_options = Enum . reduce [ :keyfile , :certfile , :cacertfile , :dhfile ] , cowboy_options , & normalize_ssl_file ( & 1 , & 2 )
202
- cowboy_options = Enum . reduce [ :password ] , cowboy_options , & to_charlist ( & 2 , & 1 )
206
+ ssl_file_opts = [ :keyfile , :certfile , :cacertfile , :dhfile ]
207
+ cowboy_options = Keyword . put_new ( cowboy_options , :port , 4040 )
208
+ cowboy_options = Enum . reduce ( ssl_file_opts , cowboy_options , & normalize_ssl_file ( & 1 , & 2 ) )
209
+ cowboy_options = Enum . reduce ( [ :password ] , cowboy_options , & to_charlist ( & 2 , & 1 ) )
203
210
cowboy_options
204
211
end
205
212
@@ -213,7 +220,10 @@ defmodule Plug.Adapters.Cowboy do
213
220
214
221
dispatch = :cowboy_router . compile ( dispatch )
215
222
{ extra_options , transport_options } = Keyword . split ( opts , @ protocol_options )
216
- protocol_options = [ env: [ dispatch: dispatch ] ] ++ add_on_response ( log_request_errors , protocol_options ) ++ extra_options
223
+
224
+ protocol_options =
225
+ [ env: [ dispatch: dispatch ] ] ++
226
+ add_on_response ( log_request_errors , protocol_options ) ++ extra_options
217
227
218
228
[ ref , acceptors , non_keyword_opts ++ transport_options , protocol_options ]
219
229
end
@@ -224,30 +234,47 @@ defmodule Plug.Adapters.Cowboy do
224
234
add_on_response ( log_request_errors , provided_onresponse , protocol_options )
225
235
end
226
236
227
- defp add_on_response ( false , nil , protocol_options ) , do: protocol_options
228
- defp add_on_response ( false , fun , protocol_options ) when is_function ( fun ) , do: [ onresponse: fun ] ++ protocol_options
229
- defp add_on_response ( false , { mod , fun } , protocol_options ) when is_atom ( mod ) and is_atom ( fun ) do
230
- [ onresponse: fn status , headers , body , request ->
231
- apply ( mod , fun , [ status , headers , body , request ] )
232
- end ] ++ protocol_options
237
+ defp add_on_response ( false , nil , protocol_options ) do
238
+ protocol_options
239
+ end
240
+
241
+ defp add_on_response ( false , fun , protocol_options ) when is_function ( fun ) do
242
+ [ onresponse: fun ] ++ protocol_options
243
+ end
244
+
245
+ defp add_on_response ( false , { mod , fun } , protocol_options ) when is_atom ( mod ) and is_atom ( fun ) do
246
+ onresponse = fn status , headers , body , request ->
247
+ apply ( mod , fun , [ status , headers , body , request ] )
248
+ end
249
+
250
+ [ onresponse: onresponse ] ++ protocol_options
233
251
end
234
- defp add_on_response ( true , nil , protocol_options ) , do: [ onresponse: & onresponse / 4 ] ++ protocol_options
252
+
253
+ defp add_on_response ( true , nil , protocol_options ) do
254
+ [ onresponse: & onresponse / 4 ] ++ protocol_options
255
+ end
256
+
235
257
defp add_on_response ( true , fun , protocol_options ) when is_function ( fun ) do
236
- [ onresponse: fn status , headers , body , request ->
237
- onresponse ( status , headers , body , request )
238
- fun . ( status , headers , body , request )
239
- end ] ++ protocol_options
258
+ onresponse = fn status , headers , body , request ->
259
+ onresponse ( status , headers , body , request )
260
+ fun . ( status , headers , body , request )
261
+ end
262
+
263
+ [ onresponse: onresponse ] ++ protocol_options
240
264
end
265
+
241
266
defp add_on_response ( true , { mod , fun } , protocol_options ) when is_atom ( mod ) and is_atom ( fun ) do
242
- [ onresponse: fn status , headers , body , request ->
243
- onresponse ( status , headers , body , request )
244
- apply ( mod , fun , [ status , headers , body , request ] )
245
- end ] ++ protocol_options
267
+ onresponse = fn status , headers , body , request ->
268
+ onresponse ( status , headers , body , request )
269
+ apply ( mod , fun , [ status , headers , body , request ] )
270
+ end
271
+
272
+ [ onresponse: onresponse ] ++ protocol_options
246
273
end
247
274
248
275
defp onresponse ( status , _headers , _body , request ) do
249
276
if status == 400 and empty_headers? ( request ) do
250
- Logger . error """
277
+ Logger . error ( """
251
278
Cowboy returned 400 and there are no headers in the connection.
252
279
253
280
This may happen if Cowboy is unable to parse the request headers,
@@ -263,8 +290,9 @@ defmodule Plug.Adapters.Cowboy do
263
290
max_headers: 100,
264
291
max_request_line_length: 8096
265
292
]
266
- """
293
+ """ )
267
294
end
295
+
268
296
request
269
297
end
270
298
@@ -274,7 +302,7 @@ defmodule Plug.Adapters.Cowboy do
274
302
end
275
303
276
304
defp build_ref ( plug , scheme ) do
277
- Module . concat ( plug , scheme |> to_string |> String . upcase )
305
+ Module . concat ( plug , scheme |> to_string |> String . upcase ( ) )
278
306
end
279
307
280
308
defp dispatch_for ( plug , opts ) do
@@ -288,44 +316,51 @@ defmodule Plug.Adapters.Cowboy do
288
316
cond do
289
317
is_nil ( value ) ->
290
318
cowboy_options
319
+
291
320
Path . type ( value ) == :absolute ->
292
- put_ssl_file cowboy_options , key , value
321
+ put_ssl_file ( cowboy_options , key , value )
322
+
293
323
true ->
294
- put_ssl_file cowboy_options , key , Path . expand ( value , otp_app ( cowboy_options ) )
324
+ put_ssl_file ( cowboy_options , key , Path . expand ( value , otp_app ( cowboy_options ) ) )
295
325
end
296
326
end
297
327
298
328
defp assert_ssl_options ( cowboy_options ) do
299
- unless Keyword . has_key? ( cowboy_options , :key ) or
300
- Keyword . has_key? ( cowboy_options , :keyfile ) do
301
- fail "missing option :key/:keyfile"
329
+ unless Keyword . has_key? ( cowboy_options , :key ) or Keyword . has_key? ( cowboy_options , :keyfile ) do
330
+ fail ( "missing option :key/:keyfile" )
302
331
end
303
- unless Keyword . has_key? ( cowboy_options , :cert ) or
304
- Keyword . has_key? ( cowboy_options , :certfile ) do
305
- fail "missing option :cert/:certfile"
332
+
333
+ unless Keyword . has_key? ( cowboy_options , :cert ) or Keyword . has_key? ( cowboy_options , :certfile ) do
334
+ fail ( "missing option :cert/:certfile" )
306
335
end
307
336
end
308
337
309
338
defp put_ssl_file ( cowboy_options , key , value ) do
310
339
value = to_charlist ( value )
340
+
311
341
unless File . exists? ( value ) do
312
- fail "the file #{ value } required by SSL's #{ inspect key } either does not exist, or the application does not have permission to access it"
342
+ fail (
343
+ "the file #{ value } required by SSL's #{ inspect ( key ) } either does not exist, or the application does not have permission to access it"
344
+ )
313
345
end
346
+
314
347
Keyword . put ( cowboy_options , key , value )
315
348
end
316
349
317
350
defp otp_app ( cowboy_options ) do
318
351
if app = cowboy_options [ :otp_app ] do
319
352
Application . app_dir ( app )
320
353
else
321
- fail "to use a relative certificate with https, the :otp_app " <>
322
- "option needs to be given to the adapter"
354
+ fail (
355
+ "to use a relative certificate with https, the :otp_app " <>
356
+ "option needs to be given to the adapter"
357
+ )
323
358
end
324
359
end
325
360
326
361
defp to_charlist ( cowboy_options , key ) do
327
362
if value = cowboy_options [ key ] do
328
- Keyword . put cowboy_options , key , to_charlist ( value )
363
+ Keyword . put ( cowboy_options , key , to_charlist ( value ) )
329
364
else
330
365
cowboy_options
331
366
end
0 commit comments