diff options
author | Loïc Hoguin <[email protected]> | 2018-06-04 08:40:31 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-06-04 08:40:31 +0200 |
commit | 93c88fdc541c3f8a4ebbae3699bee90d00dc637f (patch) | |
tree | 236d186d109db248821674ead9e3148617d3e5d1 /src | |
parent | 1be0151ec7b6a98064e648d5598f56cbdec65dc7 (diff) | |
download | gun-93c88fdc541c3f8a4ebbae3699bee90d00dc637f.tar.gz gun-93c88fdc541c3f8a4ebbae3699bee90d00dc637f.tar.bz2 gun-93c88fdc541c3f8a4ebbae3699bee90d00dc637f.zip |
Remove the dependency on Ranch
We instead of two new modules, gun_tcp and gun_tls.
They only have 6 functions so far, much less than
what Ranch provided before.
Also renames ssl to tls where applicable. It's still
possible to use the ssl transport option but it's now
undocumented.
Diffstat (limited to 'src')
-rw-r--r-- | src/gun.erl | 19 | ||||
-rw-r--r-- | src/gun_http.erl | 2 | ||||
-rw-r--r-- | src/gun_http2.erl | 30 | ||||
-rw-r--r-- | src/gun_tcp.erl | 49 | ||||
-rw-r--r-- | src/gun_tls.erl | 49 |
5 files changed, 126 insertions, 23 deletions
diff --git a/src/gun.erl b/src/gun.erl index 1224c82..8156015 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -99,7 +99,7 @@ retry => non_neg_integer(), retry_timeout => pos_integer(), trace => boolean(), - transport => tcp | ssl, + transport => tcp | tls | ssl, transport_opts => [gen_tcp:connect_option()] | [ssl:connect_option()], ws_opts => ws_opts() }. @@ -161,7 +161,12 @@ open(Host, Port, Opts) when is_list(Host); is_atom(Host); is_tuple(Host) -> open_unix(SocketPath, Opts) -> do_open({local, SocketPath}, 0, Opts). -do_open(Host, Port, Opts) -> +do_open(Host, Port, Opts0) -> + %% We accept both ssl and tls but only use tls in the code. + Opts = case Opts0 of + #{transport := ssl} -> Opts0#{transport => tls}; + _ -> Opts0 + end, case check_options(maps:to_list(Opts)) of ok -> case supervisor:start_child(gun_sup, [self(), Host, Port, Opts]) of @@ -215,7 +220,7 @@ check_options([{retry_timeout, T}|Opts]) when is_integer(T), T >= 0 -> check_options(Opts); check_options([{trace, B}|Opts]) when B =:= true; B =:= false -> check_options(Opts); -check_options([{transport, T}|Opts]) when T =:= tcp; T =:= ssl -> +check_options([{transport, T}|Opts]) when T =:= tcp; T =:= tls -> check_options(Opts); check_options([{transport_opts, L}|Opts]) when is_list(L) -> check_options(Opts); @@ -588,17 +593,17 @@ init(Parent, Owner, Host, Port, Opts) -> ok = proc_lib:init_ack(Parent, {ok, self()}), Retry = maps:get(retry, Opts, 5), Transport = case maps:get(transport, Opts, default_transport(Port)) of - tcp -> ranch_tcp; - ssl -> ranch_ssl + tcp -> gun_tcp; + tls -> gun_tls end, OwnerRef = monitor(process, Owner), connect(#state{parent=Parent, owner=Owner, owner_ref=OwnerRef, host=Host, port=Port, opts=Opts, transport=Transport}, Retry). -default_transport(443) -> ssl; +default_transport(443) -> tls; default_transport(_) -> tcp. -connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport=ranch_ssl}, Retries) -> +connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport=gun_tls}, Retries) -> Protocols = [case P of http -> <<"http/1.1">>; http2 -> <<"h2">> diff --git a/src/gun_http.erl b/src/gun_http.erl index 2fd0aac..61409fb 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -500,7 +500,7 @@ ws_upgrade(State=#http_state{socket=Socket, transport=Transport, owner=Owner, ou {<<"sec-websocket-key">>, Key} |Headers2 ], - IsSecure = Transport:secure(), + IsSecure = Transport =:= gun_tls, Headers = case lists:keymember(<<"host">>, 1, Headers0) of true -> Headers3; false when Port =:= 80, not IsSecure -> [{<<"host">>, Host}|Headers3]; diff --git a/src/gun_http2.erl b/src/gun_http2.erl index 1f25e83..cf83970 100644 --- a/src/gun_http2.erl +++ b/src/gun_http2.erl @@ -370,9 +370,9 @@ prepare_headers(EncodeState, Transport, Method, Host0, Port, Path, Headers0) -> lists:keydelete(<<"upgrade">>, 1, Headers0)))))), Headers = [ {<<":method">>, Method}, - {<<":scheme">>, case Transport:secure() of - true -> <<"https">>; - false -> <<"http">> + {<<":scheme">>, case Transport of + gun_tls -> <<"https">>; + gun_tcp -> <<"http">> end}, {<<":authority">>, Authority}, {<<":path">>, Path} @@ -465,17 +465,17 @@ send_data(State=#http2_state{socket=Socket, transport=Transport, opts=Opts, min(RemoteMaxFrameSize, ConfiguredMaxFrameSize) ), case Data of - {sendfile, Offset, Bytes, Path} when Bytes =< MaxSendSize -> - Transport:send(Socket, cow_http2:data_header(StreamID, IsFin, Bytes)), - Transport:sendfile(Socket, Path, Offset, Bytes), - {State#http2_state{local_window=ConnWindow - Bytes}, - Stream#stream{local=IsFin, local_window=StreamWindow - Bytes}}; - {sendfile, Offset, Bytes, Path} -> - Transport:send(Socket, cow_http2:data_header(StreamID, nofin, MaxSendSize)), - Transport:sendfile(Socket, Path, Offset, MaxSendSize), - send_data(State#http2_state{local_window=ConnWindow - MaxSendSize}, - Stream#stream{local_window=StreamWindow - MaxSendSize}, - IsFin, {sendfile, Offset + MaxSendSize, Bytes - MaxSendSize, Path}, In); +% {sendfile, Offset, Bytes, Path} when Bytes =< MaxSendSize -> +% Transport:send(Socket, cow_http2:data_header(StreamID, IsFin, Bytes)), +% Transport:sendfile(Socket, Path, Offset, Bytes), +% {State#http2_state{local_window=ConnWindow - Bytes}, +% Stream#stream{local=IsFin, local_window=StreamWindow - Bytes}}; +% {sendfile, Offset, Bytes, Path} -> +% Transport:send(Socket, cow_http2:data_header(StreamID, nofin, MaxSendSize)), +% Transport:sendfile(Socket, Path, Offset, MaxSendSize), +% send_data(State#http2_state{local_window=ConnWindow - MaxSendSize}, +% Stream#stream{local_window=StreamWindow - MaxSendSize}, +% IsFin, {sendfile, Offset + MaxSendSize, Bytes - MaxSendSize, Path}, In); Iolist0 -> IolistSize = iolist_size(Iolist0), if @@ -500,7 +500,7 @@ send_trailers(State=#http2_state{socket=Socket, transport=Transport, encode_stat queue_data(Stream=#stream{local_buffer=Q0, local_buffer_size=Size0}, IsFin, Data, In) -> DataSize = case Data of - {sendfile, _, Bytes, _} -> Bytes; +% {sendfile, _, Bytes, _} -> Bytes; Iolist -> iolist_size(Iolist) end, Q = queue:In({IsFin, DataSize, Data}, Q0), diff --git a/src/gun_tcp.erl b/src/gun_tcp.erl new file mode 100644 index 0000000..80bc45b --- /dev/null +++ b/src/gun_tcp.erl @@ -0,0 +1,49 @@ +%% Copyright (c) 2011-2018, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(gun_tcp). + +-export([messages/0]). +-export([connect/4]). +-export([send/2]). +-export([setopts/2]). +-export([sockname/1]). +-export([close/1]). + +messages() -> {tcp, tcp_closed, tcp_error}. + +-spec connect(inet:ip_address() | inet:hostname(), + inet:port_number(), any(), timeout()) + -> {ok, inet:socket()} | {error, atom()}. +connect(Host, Port, Opts, Timeout) when is_integer(Port) -> + gen_tcp:connect(Host, Port, + Opts ++ [binary, {active, false}, {packet, raw}], + Timeout). + +-spec send(inet:socket(), iodata()) -> ok | {error, atom()}. +send(Socket, Packet) -> + gen_tcp:send(Socket, Packet). + +-spec setopts(inet:socket(), list()) -> ok | {error, atom()}. +setopts(Socket, Opts) -> + inet:setopts(Socket, Opts). + +-spec sockname(inet:socket()) + -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}. +sockname(Socket) -> + inet:sockname(Socket). + +-spec close(inet:socket()) -> ok. +close(Socket) -> + gen_tcp:close(Socket). diff --git a/src/gun_tls.erl b/src/gun_tls.erl new file mode 100644 index 0000000..8ce46c3 --- /dev/null +++ b/src/gun_tls.erl @@ -0,0 +1,49 @@ +%% Copyright (c) 2011-2018, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(gun_tls). + +-export([messages/0]). +-export([connect/4]). +-export([send/2]). +-export([setopts/2]). +-export([sockname/1]). +-export([close/1]). + +messages() -> {ssl, ssl_closed, ssl_error}. + +-spec connect(inet:ip_address() | inet:hostname(), + inet:port_number(), any(), timeout()) + -> {ok, inet:socket()} | {error, atom()}. +connect(Host, Port, Opts, Timeout) when is_integer(Port) -> + ssl:connect(Host, Port, + Opts ++ [binary, {active, false}, {packet, raw}], + Timeout). + +-spec send(ssl:sslsocket(), iodata()) -> ok | {error, atom()}. +send(Socket, Packet) -> + ssl:send(Socket, Packet). + +-spec setopts(ssl:sslsocket(), list()) -> ok | {error, atom()}. +setopts(Socket, Opts) -> + ssl:setopts(Socket, Opts). + +-spec sockname(ssl:sslsocket()) + -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}. +sockname(Socket) -> + ssl:sockname(Socket). + +-spec close(ssl:sslsocket()) -> ok. +close(Socket) -> + ssl:close(Socket). |