Packages
ankh
0.14.3
0.17.0
0.16.0
0.15.0
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.0
0.12.1
0.12.0
0.11.0
0.10.0
0.9.0
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.1
0.3.0
0.1.1
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Pure Elixir HTTP/2 implementation
Current section
Files
Jump to
Current section
Files
lib/ankh/tls.ex
defmodule Ankh.TLS do
@moduledoc "TLS transport module"
require Logger
alias Ankh.Transport
@opaque t :: %__MODULE__{socket: :ssl.socket()}
defstruct socket: nil
defimpl Transport do
alias Ankh.TLS
@default_connect_options binary: true,
active: false,
secure_renegotiate: true,
cacertfile: CAStore.file_path()
def new(%TLS{} = transport, socket), do: {:ok, %{transport | socket: socket}}
def connect(%TLS{} = transport, %URI{host: host, port: port}, timeout, options \\ []) do
hostname = String.to_charlist(host)
options = Keyword.merge(options, @default_connect_options)
with {:ok, socket} <- :ssl.connect(hostname, port, options, timeout),
:ok <- :ssl.setopts(socket, active: :once) do
{:ok, %{transport | socket: socket}}
end
end
def accept(%TLS{socket: socket} = transport, options \\ []) do
options = Keyword.merge(options, active: :once)
with :ok <- :ssl.controlling_process(socket, self()),
:ok <- :ssl.setopts(socket, options) do
{:ok, %{transport | socket: socket}}
end
end
def send(%TLS{socket: socket}, data), do: :ssl.send(socket, data)
def recv(%TLS{socket: socket}, size, timeout), do: :ssl.recv(socket, size, timeout)
def close(%TLS{socket: socket} = transport) do
with :ok <- :ssl.close(socket), do: {:ok, %{transport | socket: nil}}
end
def handle_msg(_tls, {:ssl, socket, data}) do
with :ok <- :ssl.setopts(socket, active: :once), do: {:ok, data}
end
def handle_msg(_tls, {:ssl_error, _socket, reason}), do: {:error, reason}
def handle_msg(_tls, {:ssl_closed, _socket}), do: {:error, :closed}
def handle_msg(_tls, msg), do: {:other, msg}
def negotiated_protocol(%TLS{socket: socket}), do: :ssl.negotiated_protocol(socket)
end
end