diff options
author | Loïc Hoguin <[email protected]> | 2019-09-24 13:21:26 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-09-24 13:21:26 +0200 |
commit | a3c2edbb8c807717e2f10520c6cf1e77a62eab2e (patch) | |
tree | 452a3901dfd39f2e1ca02e040d1383513cac48a9 /src/gun_raw.erl | |
parent | 2709f328b9976c937d417f9d03b6d8b90ca2d1c5 (diff) | |
download | gun-a3c2edbb8c807717e2f10520c6cf1e77a62eab2e.tar.gz gun-a3c2edbb8c807717e2f10520c6cf1e77a62eab2e.tar.bz2 gun-a3c2edbb8c807717e2f10520c6cf1e77a62eab2e.zip |
Initial support for raw send/recv operations
Gun can now be used to send or receive arbitrary data in the
following scenarios:
* Directly after connecting to a server (this is not terribly
useful but it works nevertheless due to the Gun architecture)
* After connecting through one or more Socks and/or HTTP proxies.
This allows using Gun's proxy capabilities to access servers
located beyond firewalls.
* After performing an HTTP/1.1 Upgrade. This allows using Gun
to implement custom protocols that require upgrading from
an HTTP/1.1 connection.
As there is still no support for HTTP/2 CONNECT for the time
being, there are no relevant streams attached to those use
cases and therefore the raw protocol currently expects users
to use 'undefined' as the StreamRef value. This is not a
final decision and will most likely produce a Dialyzer
warning at this time.
Diffstat (limited to 'src/gun_raw.erl')
-rw-r--r-- | src/gun_raw.erl | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/gun_raw.erl b/src/gun_raw.erl new file mode 100644 index 0000000..da71cd6 --- /dev/null +++ b/src/gun_raw.erl @@ -0,0 +1,61 @@ +%% Copyright (c) 2019, 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_raw). + +-export([check_options/1]). +-export([name/0]). +-export([opts_name/0]). +-export([has_keepalive/0]). +-export([init/4]). +-export([handle/4]). +-export([closing/4]). +-export([close/4]). +-export([data/7]). +%% @todo down + +-record(raw_state, { + owner :: pid(), + socket :: inet:socket() | ssl:sslsocket(), + transport :: module() +}). + +%% @todo Reject ALL options. +check_options(_) -> + ok. + +name() -> raw. +opts_name() -> raw_opts. +has_keepalive() -> false. + +init(Owner, Socket, Transport, _Opts) -> + {connected_data_only, #raw_state{owner=Owner, socket=Socket, transport=Transport}}. + +handle(Data, State=#raw_state{owner=Owner}, _, EvHandlerState) -> + %% When we take over the entire connection there is no stream reference. + Owner ! {gun_data, self(), undefined, nofin, Data}, + {{state, State}, EvHandlerState}. + +%% We can always close immediately. +closing(_, _, _, EvHandlerState) -> + {close, EvHandlerState}. + +close(_, _, _, EvHandlerState) -> + EvHandlerState. + +%% @todo Initiate closing on IsFin=fin. +data(State=#raw_state{socket=Socket, transport=Transport}, undefined, + _ReplyTo, _IsFin, Data, _EvHandler, EvHandlerState) -> + Transport:send(Socket, Data), + {State, EvHandlerState}. |