Skip to content

Commit eb2c9cc

Browse files
author
José Valim
committed
Handle stream_handlers in a single place
1 parent c267211 commit eb2c9cc

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

lib/plug/cowboy.ex

+23-23
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ defmodule Plug.Cowboy do
5353
{cowboy_options, non_keyword_options} = Enum.split_with(cowboy_options, &match?({_, _}, &1))
5454

5555
cowboy_options
56-
|> set_compress()
5756
|> normalize_cowboy_options(scheme)
5857
|> to_args(scheme, plug, plug_opts, non_keyword_options)
5958
end
@@ -205,27 +204,6 @@ defmodule Plug.Cowboy do
205204
apply(:cowboy, start, args(scheme, plug, opts, cowboy_options))
206205
end
207206

208-
@default_stream_handlers [Plug.Cowboy.Stream]
209-
210-
defp set_compress(cowboy_options) do
211-
compress = Keyword.get(cowboy_options, :compress)
212-
stream_handlers = Keyword.get(cowboy_options, :stream_handlers)
213-
214-
case {compress, stream_handlers} do
215-
{true, nil} ->
216-
Keyword.put_new(cowboy_options, :stream_handlers, [
217-
:cowboy_compress_h | @default_stream_handlers
218-
])
219-
220-
{true, _} ->
221-
raise "cannot set both compress and stream_handlers at once. " <>
222-
"If you wish to set compress, please add `:cowboy_compress_h` to your stream handlers."
223-
224-
_ ->
225-
cowboy_options
226-
end
227-
end
228-
229207
defp normalize_cowboy_options(cowboy_options, :http) do
230208
Keyword.put_new(cowboy_options, :port, 4000)
231209
end
@@ -249,7 +227,7 @@ defmodule Plug.Cowboy do
249227
dispatch = :cowboy_router.compile(dispatch || dispatch_for(plug, plug_opts))
250228
{extra_options, opts} = Keyword.split(opts, @protocol_options)
251229

252-
extra_options = Keyword.put_new(extra_options, :stream_handlers, @default_stream_handlers)
230+
extra_options = set_stream_handlers(extra_options)
253231
protocol_and_extra_options = :maps.from_list(protocol_options ++ extra_options)
254232
protocol_options = Map.merge(%{env: %{dispatch: dispatch}}, protocol_and_extra_options)
255233
{transport_options, socket_options} = Keyword.pop(opts, :transport_options, [])
@@ -280,6 +258,28 @@ defmodule Plug.Cowboy do
280258
[ref || build_ref(plug, scheme), transport_options, protocol_options]
281259
end
282260

261+
@default_stream_handlers [Plug.Cowboy.Stream]
262+
263+
defp set_stream_handlers(opts) do
264+
compress = Keyword.get(opts, :compress)
265+
stream_handlers = Keyword.get(opts, :stream_handlers)
266+
267+
case {compress, stream_handlers} do
268+
{true, nil} ->
269+
Keyword.put_new(opts, :stream_handlers, [:cowboy_compress_h | @default_stream_handlers])
270+
271+
{true, _} ->
272+
raise "cannot set both compress and stream_handlers at once. " <>
273+
"If you wish to set compress, please add `:cowboy_compress_h` to your stream handlers."
274+
275+
{_, nil} ->
276+
Keyword.put_new(opts, :stream_handlers, @default_stream_handlers)
277+
278+
{_, _} ->
279+
opts
280+
end
281+
end
282+
283283
defp build_ref(plug, scheme) do
284284
Module.concat(plug, scheme |> to_string |> String.upcase())
285285
end

0 commit comments

Comments
 (0)