Packages
ace
0.14.7
0.19.0
0.18.10
0.18.9
0.18.8
0.18.7
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.1
0.17.0
0.16.8
0.16.7
0.16.6
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.11
0.15.10
0.15.9
0.15.8
0.15.7
0.15.6
retired
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.8
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.1
0.13.0
0.12.1
0.12.0
retired
0.11.1
0.11.0
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.0
0.2.0
HTTP web server and client, supports http1 and http2
Current section
Files
Jump to
Current section
Files
lib/ace/connection.ex
defmodule Ace.Connection do
# TODO Breaking rename Ace.Socket, add listen function
# possibly rename tcp -> cleartext, ssl -> secure
@moduledoc false
# NOTE documentation hidden until HTTP/1.x merged into master.
@typedoc """
Connection transport type.
Options:
- :tcp
- :tls
"""
@type transport :: :tcp | :tls
@typedoc """
Details of a servers connection with a client.
"""
@type information :: %{
peer: {:inet.ip_address, :inet.port_number},
transport: transport
}
@typedoc """
Generic client connection from either tcp or tls socket.
"""
@type connection :: {:tcp, :inet.socket} | {:tls, :ssl.socket}
@spec accept(connection) :: {:ok, connection}
def accept({:tcp, socket}) do
case :gen_tcp.accept(socket) do
{:ok, connection} ->
{:ok, {:tcp, connection}}
{:error, reason} ->
{:error, reason}
end
end
def accept({:tls, socket}) do
case :ssl.transport_accept(socket) do
{:ok, socket} ->
case :ssl.ssl_accept(socket) do
:ok ->
{:ok, {:tls, socket}}
{:error, :closed} ->
{:error, :econnaborted}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
@spec information(connection) :: information
def information({:tcp, connection}) do
{:ok, peername} = :inet.peername(connection)
%{peer: peername, transport: :tcp}
end
def information({:tls, connection}) do
{:ok, peername} = :ssl.peername(connection)
%{peer: peername, transport: :tls}
end
def port({:tcp, connection}) do
:inet.port(connection)
end
def port({:tls, connection}) do
{:ok, {_, port}} = :ssl.sockname(connection)
{:ok, port}
end
def set_active({:tcp, connection}, :once) do
:inet.setopts(connection, active: :once)
end
def set_active({:tls, connection}, :once) do
:ssl.setopts(connection, active: :once)
end
def send({:tcp, connection}, message) do
:gen_tcp.send(connection, message)
end
def send({:tls, connection}, message) do
:ssl.send(connection, message)
end
def close({:tcp, connection}) do
:gen_tcp.close(connection)
end
def close({:tls, connection}) do
:ssl.close(connection)
end
end