Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom ssl options #61

Merged
merged 1 commit into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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