Skip to content

Commit

Permalink
Use unique bare list concatenation to allow non keyword options (#113)
Browse files Browse the repository at this point in the history
* Use bare list concatenation to allow non keyword options

* Make options unique

* Format fixes

---------

Co-authored-by: Mat Trudel <[email protected]>
  • Loading branch information
danschultzer and mtrudel authored Feb 24, 2024
1 parent 35af3b0 commit df032cf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/thousand_island/transports/ssl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ defmodule ThousandIsland.Transports.SSL do
reuseaddr: true
]

# We can't use Keyword functions here because :ssl accepts non-keyword style options
resolved_options =
default_options
|> Keyword.merge(user_options)
|> Keyword.merge(@hardcoded_options)
Enum.uniq_by(
@hardcoded_options ++ user_options ++ default_options,
fn
{key, _} when is_atom(key) -> key
key when is_atom(key) -> key
end
)

if not Enum.any?(
[:keyfile, :key, :sni_hosts, :sni_fun],
Expand Down
11 changes: 8 additions & 3 deletions lib/thousand_island/transports/tcp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ defmodule ThousandIsland.Transports.TCP do
reuseaddr: true
]

# We can't use Keyword functions here because :gen_tcp accepts non-keyword style options
resolved_options =
default_options
|> Keyword.merge(user_options)
|> Keyword.merge(@hardcoded_options)
Enum.uniq_by(
@hardcoded_options ++ user_options ++ default_options,
fn
{key, _} when is_atom(key) -> key
key when is_atom(key) -> key
end
)

:gen_tcp.listen(port, resolved_options)
end
Expand Down
30 changes: 30 additions & 0 deletions test/thousand_island/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ defmodule ThousandIsland.ServerTest do
{:ok, ~c"{:ok, [mode: :binary]}"} = :gen_tcp.recv(client, 0, 100)
end

test "tcp should allow Erlang style bare options" do
{:ok, _, port} = start_handler(Echo, transport_options: [:inet6])
{:ok, client} = :gen_tcp.connect(:localhost, port, active: false)
:gen_tcp.send(client, "HI")
{:ok, ~c"HI"} = :gen_tcp.recv(client, 0, 100)
end

test "ssl should allow default options to be overridden" do
{:ok, _, port} =
start_handler(ReadOpt,
Expand Down Expand Up @@ -433,6 +440,29 @@ defmodule ThousandIsland.ServerTest do
:ssl.send(client, "mode")
{:ok, ~c"{:ok, [mode: :binary]}"} = :ssl.recv(client, 0, 100)
end

test "ssl should allow Erlang style bare options" do
{:ok, _, port} =
start_handler(Echo,
transport_module: ThousandIsland.Transports.SSL,
transport_options:
[:inet6] ++
[
certfile: Path.join(__DIR__, "../support/cert.pem"),
keyfile: Path.join(__DIR__, "../support/key.pem")
]
)

{:ok, client} =
:ssl.connect(:localhost, port,
active: false,
verify: :verify_none,
cacertfile: Path.join(__DIR__, "../support/ca.pem")
)

:ssl.send(client, "HI")
{:ok, ~c"HI"} = :ssl.recv(client, 0, 100)
end
end

describe "invalid configuration" do
Expand Down

0 comments on commit df032cf

Please sign in to comment.