The right of Erlang nodes to keep and
bear arms shall not be infringed. #2A
Loïc Hoguin, Nine Nines
gen_statem
:
apns_connection.erl
is a good exampleIf the specification allows it, Gun must allow it
Allow Gun to work through the
most exotic network topologies
retry
and retry_timeout
by defaultretry_fun
for more complex needs
{ok, Pid} = gun:open("host.name", 443),
{ok, http2} = gun:await(Pid),
Ref = gun:get(Pid, "/"),
{response, nofin, 200, RespHeaders} = gun:await(Pid, Ref),
{ok, RespBody} = gun:await_body(Pid, Ref).
Ref = gun:get(Pid, "/", [], #{flow => 1}),
{response, nofin, 200, _} = gun:await(Pid, Ref)
{data, nofin, _} = gun:await(Pid, Ref),
{error, timeout} = gun:await(Pid, Ref),
gun:update_flow(Pid, Ref, 2),
{data, nofin, _} = gun:await(Pid, Ref),
{data, nofin, _} = gun:await(Pid, Ref),
{error, timeout} = gun:await(Pid, Ref).
Configurable per stream or per protocol:
{ok, Pid} = gun:open("host.name", 443, #{
http_opts => #{flow => 10}
}).
{ok, Pid} = gun:open("host.name", 443),
{ok, _} = gun:await_up(Pid),
Ref = gun:ws_upgrade(Pid, "/ws"),
{upgrade, [<<"websocket">>], _} = gun:await(Pid, Ref),
gun:ws_send(Pid, Ref, [
{text, <<"Hello!">>},
{binary, <<1,2,3,4>>}
]),
{ws, Frame} = gun:await(Pid, Ref).
Websocket over HTTP/2 currently missing.
CommonOpts = #{content_handlers => [gun_sse_h, gun_data_h]},
{ok, Pid} = gun:open("host.name", 443, #{
http_opts => CommonOpts,
http2_opts => CommonOpts
}).
Ref = gun:get(Pid, "/events", #{
<<"accept">> => <<"text/event-stream">>}),
]),
{response, nofin, 200, RespHeaders} = gun:await(Pid, Ref),
{sse, Event} = gun:await(Pid, Ref),
#{
last_event_id := LastEventID,
event_type := <<"message">>,
data := Data
} = Event.
{ok, Pid} = gun:open("proxy1.name", 443),
Ref1 = gun:connect(Pid, #{
host => "proxy2.name", port => 443
}),
{response, fin, 200, _} = gun:await(Pid, Ref1),
Ref2 = gun:connect(Pid, #{
host => "origin.name", port => 443
}),
{response, fin, 200, _} = gun:await(Pid, Ref2),
Ref3 = gun:get(Pid, "/"),
{response, nofin, 200, RespHeaders} = gun:await(Pid, Ref3),
{ok, Body} = gun:await_body(Pid, Ref3).
#{
transport := tls, protocol := http,
origin_scheme := <<"http">>,
origin_host := "origin.name", origin_port := 443,
intermediaries := [
#{ type := connect,
host := "proxy1.name", port := 443,
transport := tls, protocol := http},
#{ type := connect,
host := "proxy2.name", port := 443,
transport := tls, protocol := http}]
} = gun:info(Pid).
CONNECT over HTTP/2 currently missing.
socks
protocolSome features might make it into 2.0.
Others in 2.x releases.
not_connected
domain_lookup
connecting
tls_handshake
connected
closing
gun_tls_proxy
and gun_tls_proxy_cb
ssl
's callback module optionSponsors: