Skip to content

Commit

Permalink
Merge pull request Azolo#61 from steffkes/ssl-options
Browse files Browse the repository at this point in the history
allow custom ssl options
  • Loading branch information
Azolo authored Sep 24, 2018
2 parents 605a40c + c1a7dda commit c8b8fcd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/websockex/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule WebSockex.Conn do
socket_recv_timeout: @socket_recv_timeout_default,
cacerts: nil,
insecure: true,
resp_headers: []
resp_headers: [],
ssl_options: nil

@type socket :: :gen_tcp.socket() | :ssl.sslsocket()
@type header :: {field :: String.t(), value :: String.t()}
Expand Down Expand Up @@ -91,7 +92,8 @@ defmodule WebSockex.Conn do
insecure: Keyword.get(opts, :insecure, true),
socket_connect_timeout:
Keyword.get(opts, :socket_connect_timeout, @socket_connect_timeout_default),
socket_recv_timeout: Keyword.get(opts, :socket_recv_timeout, @socket_recv_timeout_default)
socket_recv_timeout: Keyword.get(opts, :socket_recv_timeout, @socket_recv_timeout_default),
ssl_options: Keyword.get(opts, :ssl_options, nil)
}
end

Expand Down Expand Up @@ -315,6 +317,15 @@ defmodule WebSockex.Conn do

# Crazy SSL Stuff (It will be normal SSL stuff when I figure out Erlang's ssl)

defp ssl_connection_options(%{ssl_options: ssl_options}) when not is_nil(ssl_options) do
[
mode: :binary,
active: false,
packet: 0
]
|> Keyword.merge(ssl_options)
end

defp ssl_connection_options(%{insecure: true}) do
[
:binary,
Expand Down
12 changes: 12 additions & 0 deletions test/websockex/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ defmodule WebSockex.ConnTest do
Process.sleep(50)
assert {:error, _} = :ssl.sockname(socket)
end

test "open_socket with custom ssl options", context do
ssl_options = [cacertfile: Path.join([__DIR__, "..", "support", "priv", "websockexca.cer"])]
conn = WebSockex.Conn.new(context.uri, ssl_options: ssl_options)

assert {:ok,
%WebSockex.Conn{
conn_mod: :ssl,
transport: :ssl,
ssl_options: ^ssl_options
}} = WebSockex.Conn.open_socket(conn)
end
end

test "close_socket", context do
Expand Down

0 comments on commit c8b8fcd

Please sign in to comment.