Skip to content

Commit 729cbb8

Browse files
whatyouhidejosevalim
authored andcommitted
Format the codebase (#657)
1 parent 26db484 commit 729cbb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2421
-1638
lines changed

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: elixir
22
sudo: false
3-
elixir: 1.5.2
3+
elixir: 1.6.1
44
otp_release: 20.1
55
env:
66
- COWBOY_VERSION=1.0
@@ -13,6 +13,9 @@ notifications:
1313
recipients:
1414
1515
16+
script:
17+
- if [[ `elixir -v` = *"1.6"* ]]; then mix format --check-formatted; fi
18+
- mix test
1619
after_script:
1720
- mix deps.get --only docs
1821
- MIX_ENV=docs mix inch.report

config/config.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use Mix.Config
22

3-
if Mix.env == :test do
3+
if Mix.env() == :test do
44
config :plug, :statuses, %{
55
418 => "Totally not a teapot",
66
451 => "Unavailable For Legal Reasons"
77
}
88
end
99

10-
config :cowboy, omg: 1
10+
config :cowboy, omg: 1

lib/plug.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ defmodule Plug do
5959
use Application
6060

6161
@callback init(opts) :: opts
62-
@callback call(Plug.Conn.t, opts) :: Plug.Conn.t
62+
@callback call(Plug.Conn.t(), opts) :: Plug.Conn.t()
6363

6464
@doc false
6565
def start(_type, _args) do
66-
Logger.add_translator {Plug.Adapters.Translator, :translate}
66+
Logger.add_translator({Plug.Adapters.Translator, :translate})
6767
Plug.Supervisor.start_link()
6868
end
6969
end

lib/plug/adapters/cowboy.ex

+81-46
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ defmodule Plug.Adapters.Cowboy do
6969
Plug.Adapters.Cowboy.shutdown MyPlug.HTTP
7070
7171
"""
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}
7474
def http(plug, opts, cowboy_options \\ []) do
7575
run(:http, plug, opts, cowboy_options)
7676
end
@@ -103,8 +103,8 @@ defmodule Plug.Adapters.Cowboy do
103103
Plug.Adapters.Cowboy.shutdown MyPlug.HTTPS
104104
105105
"""
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}
108108
def https(plug, opts, cowboy_options \\ []) do
109109
Application.ensure_all_started(:ssl)
110110
run(:https, plug, opts, cowboy_options)
@@ -127,10 +127,13 @@ defmodule Plug.Adapters.Cowboy do
127127
# TODO: Remove this once we require Elixir v1.5+
128128
def child_spec(scheme, plug, opts, cowboy_options \\ []) do
129129
[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+
134137
:ranch.child_spec(ref, nb_acceptors, ranch_module, trans_opts, :cowboy_protocol, proto_opts)
135138
end
136139

@@ -158,6 +161,7 @@ defmodule Plug.Adapters.Cowboy do
158161
def child_spec(opts) do
159162
scheme = Keyword.fetch!(opts, :scheme)
160163
cowboy_opts = Keyword.get(opts, :options, [])
164+
161165
{plug, plug_opts} =
162166
case Keyword.fetch!(opts, :plug) do
163167
{_, _} = tuple -> tuple
@@ -167,8 +171,7 @@ defmodule Plug.Adapters.Cowboy do
167171
{id, start, restart, shutdown, type, modules} =
168172
child_spec(scheme, plug, plug_opts, cowboy_opts)
169173

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}
172175
end
173176

174177
## Helpers
@@ -181,25 +184,29 @@ defmodule Plug.Adapters.Cowboy do
181184
case Application.spec(:cowboy, :vsn) do
182185
'1.' ++ _ ->
183186
:ok
187+
184188
vsn ->
185189
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"
187191
end
192+
188193
{:error, {:cowboy, _}} ->
189194
raise "could not start the Cowboy application. Please ensure it is listed as a dependency in your mix.exs"
190195
end
196+
191197
apply(:cowboy, :"start_#{scheme}", args(scheme, plug, opts, cowboy_options))
192198
end
193199

194200
defp normalize_cowboy_options(cowboy_options, :http) do
195-
Keyword.put_new cowboy_options, :port, 4000
201+
Keyword.put_new(cowboy_options, :port, 4000)
196202
end
197203

198204
defp normalize_cowboy_options(cowboy_options, :https) do
199205
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))
203210
cowboy_options
204211
end
205212

@@ -213,7 +220,10 @@ defmodule Plug.Adapters.Cowboy do
213220

214221
dispatch = :cowboy_router.compile(dispatch)
215222
{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
217227

218228
[ref, acceptors, non_keyword_opts ++ transport_options, protocol_options]
219229
end
@@ -224,30 +234,47 @@ defmodule Plug.Adapters.Cowboy do
224234
add_on_response(log_request_errors, provided_onresponse, protocol_options)
225235
end
226236

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
233251
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+
235257
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
240264
end
265+
241266
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
246273
end
247274

248275
defp onresponse(status, _headers, _body, request) do
249276
if status == 400 and empty_headers?(request) do
250-
Logger.error """
277+
Logger.error("""
251278
Cowboy returned 400 and there are no headers in the connection.
252279
253280
This may happen if Cowboy is unable to parse the request headers,
@@ -263,8 +290,9 @@ defmodule Plug.Adapters.Cowboy do
263290
max_headers: 100,
264291
max_request_line_length: 8096
265292
]
266-
"""
293+
""")
267294
end
295+
268296
request
269297
end
270298

@@ -274,7 +302,7 @@ defmodule Plug.Adapters.Cowboy do
274302
end
275303

276304
defp build_ref(plug, scheme) do
277-
Module.concat(plug, scheme |> to_string |> String.upcase)
305+
Module.concat(plug, scheme |> to_string |> String.upcase())
278306
end
279307

280308
defp dispatch_for(plug, opts) do
@@ -288,44 +316,51 @@ defmodule Plug.Adapters.Cowboy do
288316
cond do
289317
is_nil(value) ->
290318
cowboy_options
319+
291320
Path.type(value) == :absolute ->
292-
put_ssl_file cowboy_options, key, value
321+
put_ssl_file(cowboy_options, key, value)
322+
293323
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)))
295325
end
296326
end
297327

298328
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")
302331
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")
306335
end
307336
end
308337

309338
defp put_ssl_file(cowboy_options, key, value) do
310339
value = to_charlist(value)
340+
311341
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+
)
313345
end
346+
314347
Keyword.put(cowboy_options, key, value)
315348
end
316349

317350
defp otp_app(cowboy_options) do
318351
if app = cowboy_options[:otp_app] do
319352
Application.app_dir(app)
320353
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+
)
323358
end
324359
end
325360

326361
defp to_charlist(cowboy_options, key) do
327362
if value = cowboy_options[key] do
328-
Keyword.put cowboy_options, key, to_charlist(value)
363+
Keyword.put(cowboy_options, key, to_charlist(value))
329364
else
330365
cowboy_options
331366
end

lib/plug/adapters/cowboy/conn.ex

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ defmodule Plug.Adapters.Cowboy.Conn do
33
@moduledoc false
44

55
def conn(req, transport) do
6-
{path, req} = :cowboy_req.path req
7-
{host, req} = :cowboy_req.host req
8-
{port, req} = :cowboy_req.port req
9-
{meth, req} = :cowboy_req.method req
10-
{hdrs, req} = :cowboy_req.headers req
11-
{qs, req} = :cowboy_req.qs req
12-
{peer, req} = :cowboy_req.peer req
6+
{path, req} = :cowboy_req.path(req)
7+
{host, req} = :cowboy_req.host(req)
8+
{port, req} = :cowboy_req.port(req)
9+
{meth, req} = :cowboy_req.method(req)
10+
{hdrs, req} = :cowboy_req.headers(req)
11+
{qs, req} = :cowboy_req.qs(req)
12+
{peer, req} = :cowboy_req.peer(req)
1313
{remote_ip, _} = peer
1414

1515
%Plug.Conn{
@@ -43,9 +43,11 @@ defmodule Plug.Adapters.Cowboy.Conn do
4343
is_integer(length) -> length
4444
end
4545

46-
body_fun = fn(socket, transport) -> transport.sendfile(socket, path, offset, length) end
46+
body_fun = fn socket, transport -> transport.sendfile(socket, path, offset, length) end
47+
48+
{:ok, req} =
49+
:cowboy_req.reply(status, headers, :cowboy_req.set_resp_body_fun(length, body_fun, req))
4750

48-
{:ok, req} = :cowboy_req.reply(status, headers, :cowboy_req.set_resp_body_fun(length, body_fun, req))
4951
{:ok, nil, req}
5052
end
5153

lib/plug/adapters/cowboy/handler.ex

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Plug.Adapters.Cowboy.Handler do
99

1010
def upgrade(req, env, __MODULE__, {transport, plug, opts}) do
1111
conn = @connection.conn(req, transport)
12+
1213
try do
1314
%{adapter: {@connection, req}} =
1415
conn
@@ -22,10 +23,12 @@ defmodule Plug.Adapters.Cowboy.Handler do
2223
exception = Exception.normalize(:error, value, stack)
2324
reason = {{exception, stack}, {plug, :call, [conn, opts]}}
2425
terminate(reason, req, stack)
26+
2527
:throw, value ->
2628
stack = System.stacktrace()
2729
reason = {{{:nocatch, value}, stack}, {plug, :call, [conn, opts]}}
2830
terminate(reason, req, stack)
31+
2932
:exit, value ->
3033
stack = System.stacktrace()
3134
reason = {value, {plug, :call, [conn, opts]}}
@@ -39,11 +42,12 @@ defmodule Plug.Adapters.Cowboy.Handler do
3942
end
4043
end
4144

42-
defp maybe_send(%Plug.Conn{state: :unset}, _plug), do: raise Plug.Conn.NotSentError
45+
defp maybe_send(%Plug.Conn{state: :unset}, _plug), do: raise(Plug.Conn.NotSentError)
4346
defp maybe_send(%Plug.Conn{state: :set} = conn, _plug), do: Plug.Conn.send_resp(conn)
44-
defp maybe_send(%Plug.Conn{} = conn, _plug), do: conn
47+
defp maybe_send(%Plug.Conn{} = conn, _plug), do: conn
48+
4549
defp maybe_send(other, plug) do
46-
raise "Cowboy adapter expected #{inspect plug} to return Plug.Conn but got: #{inspect other}"
50+
raise "Cowboy adapter expected #{inspect(plug)} to return Plug.Conn but got: #{inspect(other)}"
4751
end
4852

4953
defp terminate(reason, req, stack) do

0 commit comments

Comments
 (0)