aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--Makefile4
-rw-r--r--README.md9
-rwxr-xr-xall.sh2
-rw-r--r--erlang.mk23
-rw-r--r--guide/toc.md2
-rw-r--r--guide/ws_handlers.md271
-rw-r--r--guide/ws_protocol.md45
-rw-r--r--manual/cowboy.md2
-rw-r--r--manual/cowboy_req.md3
-rw-r--r--rebar.config2
-rw-r--r--src/cowboy.erl16
-rw-r--r--src/cowboy_app.erl10
-rw-r--r--src/cowboy_bstr.erl14
-rw-r--r--src/cowboy_client.erl275
-rw-r--r--src/cowboy_clock.erl37
-rw-r--r--src/cowboy_handler.erl8
-rw-r--r--src/cowboy_http.erl250
-rw-r--r--src/cowboy_http_handler.erl17
-rw-r--r--src/cowboy_loop_handler.erl23
-rw-r--r--src/cowboy_middleware.erl11
-rw-r--r--src/cowboy_protocol.erl45
-rw-r--r--src/cowboy_req.erl294
-rw-r--r--src/cowboy_rest.erl7
-rw-r--r--src/cowboy_router.erl47
-rw-r--r--src/cowboy_spdy.erl33
-rw-r--r--src/cowboy_static.erl17
-rw-r--r--src/cowboy_sub_protocol.erl15
-rw-r--r--src/cowboy_sup.erl14
-rw-r--r--src/cowboy_websocket.erl18
-rw-r--r--src/cowboy_websocket_handler.erl34
-rw-r--r--test/http_SUITE.erl1296
-rw-r--r--test/http_SUITE_data/http_loop_stream_recv.erl41
-rw-r--r--test/spdy_SUITE.erl86
34 files changed, 978 insertions, 1998 deletions
diff --git a/.gitignore b/.gitignore
index 9bed3c0..d708ffc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,12 +1,7 @@
.cowboy.plt
.erlang.mk.packages.*
-.eunit
_rel
deps
-doc/*.css
-doc/*.html
-doc/*.png
-doc/edoc-info
ebin
logs
relx
diff --git a/Makefile b/Makefile
index c29165f..c3d194d 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,8 @@ PROJECT = cowboy
# Options.
+ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
+ +warn_shadow_vars +warn_obsolete_guard +warn_missing_spec
COMPILE_FIRST = cowboy_middleware cowboy_sub_protocol
CT_SUITES = eunit http spdy ws
PLT_APPS = crypto public_key ssl
@@ -11,7 +13,7 @@ PLT_APPS = crypto public_key ssl
# Dependencies.
DEPS = cowlib ranch
-dep_cowlib = pkg://cowlib 0.5.0
+dep_cowlib = pkg://cowlib 0.6.1
dep_ranch = pkg://ranch 0.9.0
TEST_DEPS = ct_helper gun
diff --git a/README.md b/README.md
index 92f2563..8313bc6 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@ Goals
-----
Cowboy aims to provide a **complete** HTTP stack in a **small** code base.
-It is optimized for **low latency** and **low memory usage**, in parts
+It is optimized for **low latency** and **low memory usage**, in part
because it uses **binary strings**.
Cowboy provides **routing** capabilities, selectively dispatching requests
@@ -21,8 +21,11 @@ No parameterized module. No process dictionary. **Clean** Erlang code.
Sponsors
--------
-The SPDY protocol development is sponsored
-by [LeoFS Cloud Storage](http://www.leofs.org).
+The SPDY implementation was sponsored by
+[LeoFS Cloud Storage](http://www.leofs.org).
+
+The project is currently sponsored by
+[Kato.im](https://kato.im).
Getting Started
---------------
diff --git a/all.sh b/all.sh
index e8c7818..9134bf8 100755
--- a/all.sh
+++ b/all.sh
@@ -1,7 +1,7 @@
#!/bin/sh
KERL_INSTALL_PATH=~/erlang
-KERL_RELEASES="r15b01 r15b02 r15b03 r16b r16b01 r16b02 r16b03 r16b03-1 maint master"
+KERL_RELEASES="r15b01 r15b02 r15b03 r16b r16b01 r16b02 r16b03-1 17.0 17.0_native maint master"
make build-tests
diff --git a/erlang.mk b/erlang.mk
index 1e0a05e..7da0151 100644
--- a/erlang.mk
+++ b/erlang.mk
@@ -1,4 +1,4 @@
-# Copyright (c) 2013, Loïc Hoguin <[email protected]>
+# Copyright (c) 2013-2014, 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
@@ -58,7 +58,7 @@ ifneq ($(wildcard $(RELX_CONFIG)),)
RELX ?= $(CURDIR)/relx
export RELX
-RELX_URL ?= https://github.com/erlware/relx/releases/download/v0.5.2/relx
+RELX_URL ?= https://github.com/erlware/relx/releases/download/v0.6.0/relx
RELX_OPTS ?=
define get_relx
@@ -132,7 +132,7 @@ define compile_dtl
Compile = fun(F) -> \
Module = list_to_atom( \
string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
- erlydtl_compiler:compile(F, Module, [{out_dir, "ebin/"}]) \
+ erlydtl:compile(F, Module, [{out_dir, "ebin/"}]) \
end, \
_ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
init:stop()'
@@ -209,11 +209,14 @@ clean-docs:
$(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
+TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
+TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1 +'{parse_transform, eunit_autoexport}'
+
build-test-deps: $(ALL_TEST_DEPS_DIRS)
@for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
build-tests: build-test-deps
- $(gen_verbose) erlc -v $(ERLC_OPTS) -o test/ \
+ $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -o test/ \
$(wildcard test/*.erl test/*/*.erl) -pa ebin/
CT_OPTS ?=
@@ -228,7 +231,7 @@ CT_RUN = ct_run \
CT_SUITES ?=
define test_target
-test_$(1): ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
+test_$(1): ERLC_OPTS = $(TEST_ERLC_OPTS)
test_$(1): clean deps app build-tests
@if [ -d "test" ] ; \
then \
@@ -240,7 +243,7 @@ endef
$(foreach test,$(CT_SUITES),$(eval $(call test_target,$(test))))
-tests: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
+tests: ERLC_OPTS = $(TEST_ERLC_OPTS)
tests: clean deps app build-tests
@if [ -d "test" ] ; \
then \
@@ -251,16 +254,18 @@ tests: clean deps app build-tests
# Dialyzer.
+DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt
+export DIALYZER_PLT
+
PLT_APPS ?=
DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
-Wunmatched_returns # -Wunderspecs
build-plt: deps app
- @dialyzer --build_plt --output_plt .$(PROJECT).plt \
- --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
+ @dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
dialyze:
- @dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS)
+ @dialyzer --src src --no_native $(DIALYZER_OPTS)
# Packages.
diff --git a/guide/toc.md b/guide/toc.md
index 5229167..26925c5 100644
--- a/guide/toc.md
+++ b/guide/toc.md
@@ -61,7 +61,7 @@ Server push technologies
Using Websocket
---------------
- * The Websocket protocol
+ * [The Websocket protocol](ws_protocol.md)
* [Handling Websocket connections](ws_handlers.md)
Advanced HTTP
diff --git a/guide/ws_handlers.md b/guide/ws_handlers.md
index 94bc95e..99f69dc 100644
--- a/guide/ws_handlers.md
+++ b/guide/ws_handlers.md
@@ -1,75 +1,230 @@
-Websocket handlers
-==================
+Handling Websocket connections
+==============================
-Purpose
--------
+A special handler is required for handling Websocket connections.
+Websocket handlers allow you to initialize the connection,
+handle incoming frames from the socket, handle incoming Erlang
+messages and then clean up on termination.
-Websocket is an extension to HTTP to emulate plain TCP connections
-between the user's browser and the server. Requests that are upgraded
-are then handled by websocket handlers.
+Websocket handlers essentially act as a bridge between the client
+and the Erlang system. They will typically do little more than
+socket communication and decoding/encoding of frames.
-Both sides of the socket can send data at any time asynchronously.
+Initialization
+--------------
-Websocket is an IETF standard. Cowboy supports the standard and all
-the drafts that were previously implemented by browsers. Websocket
-is implemented by most browsers today, although for backward
-compatibility reasons a solution like [Bullet](https://github.com/extend/bullet)
-might be preferred.
+First, the `init/3` callback is called. This callback is common
+to all handlers. To establish a Websocket connection, this function
+must return an `upgrade` tuple.
-Usage
------
+``` erlang
+init(_, Req, Opts) ->
+ {upgrade, protocol, cowboy_websocket}.
+```
+
+It is also possible to return an update Req object and options
+using the longer form of this tuple.
+
+``` erlang
+init(_Type, Req, Opts) ->
+ {upgrade, protocol, cowboy_websocket, Req, Opts}.
+```
-Websocket handlers are a bridge between the client and your system.
-They can receive data from the client, through `websocket_handle/3`,
-or from the system, through `websocket_info/3`. It is up to the
-handler to decide to process this data, and optionally send a reply
-to the client.
+Upon receiving this tuple, Cowboy will switch to the code
+that handles Websocket connections. It does not immediately
+perform the handshake however. First, it calls the `websocket_init/3`
+callback.
-The first thing to do to be able to handle websockets is to tell
-Cowboy that it should upgrade the connection to use the Websocket
-protocol, as follow.
+This function must be used to initialize the state, and can
+also be used to register the process, start a timer, etc.
+As long as the function returns an `ok` tuple, then Cowboy
+performs the Websocket handshake.
``` erlang
-init({tcp, http}, Req, Opts) ->
- {upgrade, protocol, cowboy_websocket}.
+websocket_init(_Type, Req, _Opts) ->
+ {ok, Req, #state{}}.
```
-Cowboy will then switch the protocol and call `websocket_init`,
-followed by zero or more calls to `websocket_handle` and
-`websocket_info`. Then, when the connection is shutting down,
-`websocket_terminate` will be called.
+A `shutdown` tuple can be returned to refuse to perform the
+handshake. When doing so, Cowboy will send a `400 Bad Request`
+response to the client and close the connection.
+
+``` erlang
+websocket_init(_Type, Req, _Opts) ->
+ {shutdown, Req}.
+```
+
+It is also possible to perform a `cowboy_req:reply/{2,3,4}`
+before returning a `shutdown` tuple, allowing you to override
+the response sent back to the client.
+
+Note that browser support for handling Websocket connection
+failures may vary.
+
+If the sec-websocket-protocol header was sent with the request
+for establishing a Websocket connection, then the Websocket
+handler *must* select one of these subprotocol and send it
+back to the client, otherwise the client might decide to close
+the connection, assuming no correct subprotocol was found.
+
+``` erlang
+websocket_init(_Type, Req, _Opts) ->
+ case cowboy_req:parse_header(<<"sec-websocket-protocol">>, Req) of
+ {ok, undefined, Req2} ->
+ {ok, Req, #state{}};
+ {ok, Subprotocols, Req2} ->
+ case lists:keymember(<<"mychat2">>, 1, Subprotocols) of
+ true ->
+ Req3 = cowboy:set_resp_header(<<"sec-websocket-protocol">>,
+ <<"mychat2">>, Req2),
+ {ok, Req3, #state{}};
+ false ->
+ {shutdown, Req2}
+ end
+ end.
+```
+
+It is not recommended to wait too long inside the `websocket_init/3`
+function. Any extra initialization may be done after returning by
+sending yourself a message before doing anything. Any message sent
+to `self()` from `websocket_init/3` is guaranteed to arrive before
+any frames from the client.
+
+It is also very easy to ensure that this message arrives before
+any message from other processes by sending it before registering
+or enabling timers.
+
+``` erlang
+websocket_init(_Type, Req, _Opts) ->
+ self() ! post_init,
+ %% Register process here...
+ {ok, Req, #state{}}.
+
+websocket_info(post_init, Req, State) ->
+ %% Perform post_init initialization here...
+ {ok, Req, State}.
+```
+
+Handling frames from the client
+-------------------------------
+
+Cowboy will call `websocket_handle/3` whenever a text, binary,
+ping or pong frame arrives from the client. Note that in the
+case of ping and pong frames, no action is expected as Cowboy
+automatically replies to ping frames.
+
+The handler can decide to send frames to the socket, shutdown
+or just continue without sending anything.
-The following handler sends a message every second. It also echoes
-back what it receives.
+The following snippet echoes back any text frame received and
+ignores all others.
``` erlang
--module(my_ws_handler).
--behaviour(cowboy_websocket_handler).
-
--export([init/3]).
--export([websocket_init/3]).
--export([websocket_handle/3]).
--export([websocket_info/3]).
--export([websocket_terminate/3]).
-
-init({tcp, http}, Req, Opts) ->
- {upgrade, protocol, cowboy_websocket}.
-
-websocket_init(TransportName, Req, _Opts) ->
- erlang:start_timer(1000, self(), <<"Hello!">>),
- {ok, Req, undefined_state}.
-
-websocket_handle({text, Msg}, Req, State) ->
- {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
-websocket_handle(_Data, Req, State) ->
- {ok, Req, State}.
-
-websocket_info({timeout, _Ref, Msg}, Req, State) ->
- erlang:start_timer(1000, self(), <<"How' you doin'?">>),
- {reply, {text, Msg}, Req, State};
+websocket_handle(Frame = {text, _}, Req, State) ->
+ {reply, Frame, Req, State};
+websocket_handle(_Frame, Req, State) ->
+ {ok, Req, State}.
+```
+
+Handling Erlang messages
+------------------------
+
+Cowboy will call `websocket_info/3` whenever an Erlang message
+arrives.
+
+The handler can decide to send frames to the socket, shutdown
+or just continue without sending anything.
+
+The following snippet forwards any `log` message to the socket
+and ignores all others.
+
+``` erlang
+websocket_info({log, Text}, Req, State) ->
+ {reply, {text, Text}, Req, State};
websocket_info(_Info, Req, State) ->
- {ok, Req, State}.
+ {ok, Req, State}.
+```
+
+Sending frames to the socket
+----------------------------
+
+Cowboy allows sending either a single frame or a list of
+frames to the socket. Any frame can be sent: text, binary, ping,
+pong or close frames.
+
+The following example sends three frames using a single `reply`
+tuple.
-websocket_terminate(_Reason, _Req, _State) ->
- ok.
+``` erlang
+websocket_info(hello_world, Req, State) ->
+ {reply, [
+ {text, "Hello"},
+ {text, <<"world!">>},
+ {binary, <<0:8000>>}
+ ], Req, State};
+%% More websocket_info/3 clauses here...
+```
+
+Note that the payload for text and binary frames is of type
+`iodata()`, meaning it can be either a `binary()` or an
+`iolist()`.
+
+Sending a `close` frame will immediately initiate the closing
+of the Websocket connection. Be aware that any additional
+frames sent by the client or any Erlang messages waiting to
+be received will not be processed. Also note that when replying
+a list of frames that includes close, any frame found after the
+close frame will not be sent.
+
+Ping and timeout
+----------------
+
+The biggest performance improvement you can do when dealing
+with a huge number of Websocket connections is to reduce the
+number of timers that are started on the server. A common use
+of timers when dealing with connections is for sending a ping
+every once in a while. This should be done exclusively on the
+client side. Indeed, a server handling one million Websocket
+connections will perform a lot better when it doesn't have to
+handle one million extra timers too!
+
+Cowboy will automatically respond to ping frames sent by the
+client. It will still forward the frame to the handler for
+informative purpose, but no further action is required.
+
+Cowboy can be configured to automatically close the Websocket
+connection when no data arrives on the socket. It is highly
+recommended to configure a timeout for it, as otherwise you
+may end up with zombie "half-connected" sockets that may
+leave the process alive forever.
+
+A good timeout value is 60 seconds.
+
+``` erlang
+websocket_init(_Type, Req, _Opts) ->
+ {ok, Req, #state{}, 60000}.
```
+
+This value cannot be changed once it is set. It defaults to
+`infinity`.
+
+Hibernate
+---------
+
+Most tuples returned from handler callbacks can include an
+extra value `hibernate`. After doing any necessary operations
+following the return of the callback, Cowboy will hibernate
+the process.
+
+It is highly recommended to hibernate processes that do not
+handle much traffic. It is a good idea to hibernate all
+connections by default and investigate only when you start
+noticing increased CPU usage.
+
+Supporting older browsers
+-------------------------
+
+Unfortunately Websocket is a relatively recent technology,
+which means that not all browsers support it. A library like
+[Bullet](https://github.com/extend/bullet) can be used to
+emulate Websocket connections on older browsers.
diff --git a/guide/ws_protocol.md b/guide/ws_protocol.md
new file mode 100644
index 0000000..390751e
--- /dev/null
+++ b/guide/ws_protocol.md
@@ -0,0 +1,45 @@
+The Websocket protocol
+======================
+
+This chapter explains what Websocket is and why it is
+a vital component of soft realtime Web applications.
+
+Description
+-----------
+
+Websocket is an extension to HTTP that emulates plain TCP
+connections between the client, typically a Web browser,
+and the server. It uses the HTTP Upgrade mechanism to
+establish the connection.
+
+Websocket connections are asynchronous, unlike HTTP. This
+means that not only can the client send frames to the server
+at any time, but the server can also send frames to the client
+without the client initiating anything other than the
+Websocket connection itself. This allows the server to push
+data to the client directly.
+
+Websocket is an IETF standard. Cowboy supports the standard
+and all drafts that were previously implemented by browsers,
+excluding the initial flawed draft sometimes known as
+"version 0".
+
+Implementation
+--------------
+
+Cowboy implements Websocket as a protocol upgrade. Once the
+upgrade is performed from the `init/3` callback, Cowboy
+switches to Websocket. Please consult the next chapter for
+more information on initiating and handling Websocket
+connections.
+
+The implementation of Websocket in Cowboy is validated using
+the Autobahn test suite, which is an extensive suite of tests
+covering all aspects of the protocol. Cowboy passes the
+suite with 100% success, including all optional tests.
+
+Cowboy's Websocket implementation also includes the
+x-webkit-deflate-frame compression draft which is being used
+by some browsers to reduce the size of data being transmitted.
+Cowboy will automatically use compression as long as the
+`compress` protocol option is set when starting the listener.
diff --git a/manual/cowboy.md b/manual/cowboy.md
index c2ef859..0eab0f8 100644
--- a/manual/cowboy.md
+++ b/manual/cowboy.md
@@ -76,7 +76,7 @@ Exports
> Start listening for SPDY connections. Returns the pid for this
> listener's supervisor.
-### stop_listener(Ref) -> ok
+### stop_listener(Ref) -> ok | {error, not_found}
> Types:
> * Ref = ranch:ref()
diff --git a/manual/cowboy_req.md b/manual/cowboy_req.md
index b943ea0..91139ac 100644
--- a/manual/cowboy_req.md
+++ b/manual/cowboy_req.md
@@ -614,7 +614,8 @@ Response related exports
>
> If a `Length` is provided, it will be sent in the
> content-length header in the response. It is recommended
-> to set the length if it can be known in advance.
+> to set the length if it can be known in advance. Otherwise,
+> the transfer-encoding header will be set to identity.
>
> This function will only be called if the response is sent
> using the `reply/2` or `reply/3` function.
diff --git a/rebar.config b/rebar.config
index 662c8c2..249620b 100644
--- a/rebar.config
+++ b/rebar.config
@@ -1,4 +1,4 @@
{deps, [
- {cowlib, ".*", {git, "git://github.com/extend/cowlib.git", "0.5.0"}},
+ {cowlib, ".*", {git, "git://github.com/extend/cowlib.git", "0.6.1"}},
{ranch, ".*", {git, "git://github.com/extend/ranch.git", "0.9.0"}}
]}.
diff --git a/src/cowboy.erl b/src/cowboy.erl
index 610a33a..2b50dfb 100644
--- a/src/cowboy.erl
+++ b/src/cowboy.erl
@@ -12,7 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Convenience API to start and stop HTTP/HTTPS listeners.
-module(cowboy).
-export([start_http/4]).
@@ -37,7 +36,6 @@
fun((http_status(), http_headers(), iodata(), Req) -> Req).
-export_type([onresponse_fun/0]).
-%% @doc Start an HTTP listener.
-spec start_http(ranch:ref(), non_neg_integer(), ranch_tcp:opts(),
cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
@@ -45,7 +43,6 @@ start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
ranch:start_listener(Ref, NbAcceptors,
ranch_tcp, TransOpts, cowboy_protocol, ProtoOpts).
-%% @doc Start an HTTPS listener.
-spec start_https(ranch:ref(), non_neg_integer(), ranch_ssl:opts(),
cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
@@ -53,7 +50,6 @@ start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
ranch:start_listener(Ref, NbAcceptors,
ranch_ssl, TransOpts, cowboy_protocol, ProtoOpts).
-%% @doc Start a SPDY listener.
-spec start_spdy(ranch:ref(), non_neg_integer(), ranch_ssl:opts(),
cowboy_spdy:opts()) -> {ok, pid()} | {error, any()}.
start_spdy(Ref, NbAcceptors, TransOpts, ProtoOpts)
@@ -66,20 +62,14 @@ start_spdy(Ref, NbAcceptors, TransOpts, ProtoOpts)
ranch:start_listener(Ref, NbAcceptors,
ranch_ssl, TransOpts2, cowboy_spdy, ProtoOpts).
-%% @doc Stop a listener.
--spec stop_listener(ranch:ref()) -> ok.
+-spec stop_listener(ranch:ref()) -> ok | {error, not_found}.
stop_listener(Ref) ->
ranch:stop_listener(Ref).
-%% @doc Convenience function for setting an environment value.
-%%
-%% Allows you to update live an environment value used by middlewares.
-%% This function is primarily intended to simplify updating the dispatch
-%% list used for routing.
-spec set_env(ranch:ref(), atom(), any()) -> ok.
set_env(Ref, Name, Value) ->
Opts = ranch:get_protocol_options(Ref),
{_, Env} = lists:keyfind(env, 1, Opts),
- Env2 = [{Name, Value}|lists:keydelete(Name, 1, Env)],
- Opts2 = lists:keyreplace(env, 1, Opts, {env, Env2}),
+ Opts2 = lists:keyreplace(env, 1, Opts,
+ {env, lists:keystore(Name, 1, Env, {Name, Value})}),
ok = ranch:set_protocol_options(Ref, Opts2).
diff --git a/src/cowboy_app.erl b/src/cowboy_app.erl
index d4d0389..1161d91 100644
--- a/src/cowboy_app.erl
+++ b/src/cowboy_app.erl
@@ -12,18 +12,16 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @private
-module(cowboy_app).
-behaviour(application).
-%% API.
-export([start/2]).
-export([stop/1]).
-%% API.
-
-start(_Type, _Args) ->
+-spec start(_, _) -> {ok, pid()}.
+start(_, _) ->
cowboy_sup:start_link().
-stop(_State) ->
+-spec stop(_) -> ok.
+stop(_) ->
ok.
diff --git a/src/cowboy_bstr.erl b/src/cowboy_bstr.erl
index 92e3f29..98d2cf7 100644
--- a/src/cowboy_bstr.erl
+++ b/src/cowboy_bstr.erl
@@ -12,7 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Binary string manipulation.
-module(cowboy_bstr).
%% Binary strings.
@@ -24,11 +23,9 @@
-export([char_to_lower/1]).
-export([char_to_upper/1]).
-%% @doc Capitalize a token.
-%%
%% The first letter and all letters after a dash are capitalized.
%% This is the form seen for header names in the HTTP/1.1 RFC and
-%% others. Note that using this form isn't required, as header name
+%% others. Note that using this form isn't required, as header names
%% are case insensitive, and it is only provided for use with eventual
%% badly implemented clients.
-spec capitalize_token(B) -> B when B::binary().
@@ -43,19 +40,14 @@ capitalize_token(<< C, Rest/bits >>, true, Acc) ->
capitalize_token(<< C, Rest/bits >>, false, Acc) ->
capitalize_token(Rest, false, << Acc/binary, (char_to_lower(C)) >>).
-%% @doc Convert a binary string to lowercase.
-spec to_lower(B) -> B when B::binary().
to_lower(B) ->
<< << (char_to_lower(C)) >> || << C >> <= B >>.
-%% @doc Convert a binary string to uppercase.
-spec to_upper(B) -> B when B::binary().
to_upper(B) ->
<< << (char_to_upper(C)) >> || << C >> <= B >>.
-%% @doc Convert [A-Z] characters to lowercase.
-%% @end
-%% We gain noticeable speed by matching each value directly.
-spec char_to_lower(char()) -> char().
char_to_lower($A) -> $a;
char_to_lower($B) -> $b;
@@ -85,7 +77,6 @@ char_to_lower($Y) -> $y;
char_to_lower($Z) -> $z;
char_to_lower(Ch) -> Ch.
-%% @doc Convert [a-z] characters to uppercase.
-spec char_to_upper(char()) -> char().
char_to_upper($a) -> $A;
char_to_upper($b) -> $B;
@@ -118,9 +109,7 @@ char_to_upper(Ch) -> Ch.
%% Tests.
-ifdef(TEST).
-
capitalize_token_test_() ->
- %% {Header, Result}
Tests = [
{<<"heLLo-woRld">>, <<"Hello-World">>},
{<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>},
@@ -131,5 +120,4 @@ capitalize_token_test_() ->
{<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>}
],
[{H, fun() -> R = capitalize_token(H) end} || {H, R} <- Tests].
-
-endif.
diff --git a/src/cowboy_client.erl b/src/cowboy_client.erl
deleted file mode 100644
index 2e69236..0000000
--- a/src/cowboy_client.erl
+++ /dev/null
@@ -1,275 +0,0 @@
-%% Copyright (c) 2012-2014, 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.
-
-%% @private
--module(cowboy_client).
-
--export([init/1]).
--export([state/1]).
--export([transport/1]).
-
--export([connect/4]).
--export([raw_request/2]).
--export([request/3]).
--export([request/4]).
--export([request/5]).
--export([response/1]).
--export([response_body/1]).
--export([skip_body/1]).
--export([stream_status/1]).
--export([stream_headers/1]).
--export([stream_header/1]).
--export([stream_body/1]).
-
--record(client, {
- state = wait :: wait | request | response | response_body,
- opts = [] :: [any()],
- socket = undefined :: undefined | inet:socket(),
- transport = undefined :: module(),
- timeout = 5000 :: timeout(), %% @todo Configurable.
- buffer = <<>> :: binary(),
- connection = keepalive :: keepalive | close,
- version = 'HTTP/1.1' :: cowboy:http_version(),
- response_body = undefined :: undefined | non_neg_integer()
-}).
-
-init(Opts) ->
- {ok, #client{opts=Opts}}.
-
-state(#client{state=State}) ->
- State.
-
-transport(#client{socket=undefined}) ->
- {error, notconnected};
-transport(#client{transport=Transport, socket=Socket}) ->
- {ok, Transport, Socket}.
-
-connect(Transport, Host, Port, Client)
- when is_binary(Host) ->
- connect(Transport, binary_to_list(Host), Port, Client);
-connect(Transport, Host, Port, Client=#client{state=State, opts=Opts})
- when is_atom(Transport), is_list(Host),
- is_integer(Port), is_record(Client, client),
- State =:= wait ->
- {ok, Socket} = Transport:connect(Host, Port, Opts),
- {ok, Client#client{state=request, socket=Socket, transport=Transport}}.
-
-raw_request(Data, Client=#client{state=response_body}) ->
- {done, Client2} = skip_body(Client),
- raw_request(Data, Client2);
-raw_request(Data, Client=#client{
- state=State, socket=Socket, transport=Transport})
- when State =:= request ->
- ok = Transport:send(Socket, Data),
- {ok, Client}.
-
-request(Method, URL, Client) ->
- request(Method, URL, [], <<>>, Client).
-
-request(Method, URL, Headers, Client) ->
- request(Method, URL, Headers, <<>>, Client).
-
-request(Method, URL, Headers, Body, Client=#client{state=response_body}) ->
- {done, Client2} = skip_body(Client),
- request(Method, URL, Headers, Body, Client2);
-request(Method, URL, Headers, Body, Client=#client{
- state=State, version=Version})
- when State =:= wait; State =:= request ->
- {Transport, FullHost, Host, Port, Path} = parse_url(URL),
- {ok, Client2} = case State of
- wait -> connect(Transport, Host, Port, Client);
- request -> {ok, Client}
- end,
- VersionBin = atom_to_binary(Version, latin1),
- %% @todo do keepalive too, allow override...
- Headers2 = case lists:keyfind(<<"host">>, 1, Headers) of
- false -> [{<<"host">>, FullHost}|Headers];
- _ -> Headers
- end,
- Headers3 = [
- {<<"user-agent">>, <<"Cow">>}
- |Headers2],
- Headers4 = case iolist_size(Body) of
- 0 -> Headers3;
- Length -> [{<<"content-length">>, integer_to_list(Length)}|Headers3]
- end,
- HeadersData = [[Name, <<": ">>, Value, <<"\r\n">>]
- || {Name, Value} <- Headers4],
- Data = [Method, <<" ">>, Path, <<" ">>, VersionBin, <<"\r\n">>,
- HeadersData, <<"\r\n">>, Body],
- raw_request(Data, Client2).
-
-parse_url(<< "https://", Rest/binary >>) ->
- parse_url(Rest, ranch_ssl);
-parse_url(<< "http://", Rest/binary >>) ->
- parse_url(Rest, ranch_tcp);
-parse_url(URL) ->
- parse_url(URL, ranch_tcp).
-
-parse_url(URL, Transport) ->
- case binary:split(URL, <<"/">>) of
- [Peer] ->
- {Host, Port} = parse_peer(Peer, Transport),
- {Transport, Peer, Host, Port, <<"/">>};
- [Peer, Path] ->
- {Host, Port} = parse_peer(Peer, Transport),
- {Transport, Peer, Host, Port, [<<"/">>, Path]}
- end.
-
-parse_peer(Peer, Transport) ->
- case binary:split(Peer, <<":">>) of
- [Host] when Transport =:= ranch_tcp ->
- {binary_to_list(Host), 80};
- [Host] when Transport =:= ranch_ssl ->
- {binary_to_list(Host), 443};
- [Host, Port] ->
- {binary_to_list(Host), list_to_integer(binary_to_list(Port))}
- end.
-
-response(Client=#client{state=response_body}) ->
- {done, Client2} = skip_body(Client),
- response(Client2);
-response(Client=#client{state=request}) ->
- case stream_status(Client) of
- {ok, Status, _, Client2} ->
- case stream_headers(Client2) of
- {ok, Headers, Client3} ->
- {ok, Status, Headers, Client3};
- {error, Reason} ->
- {error, Reason}
- end;
- {error, Reason} ->
- {error, Reason}
- end.
-
-response_body(Client=#client{state=response_body}) ->
- response_body_loop(Client, <<>>).
-
-response_body_loop(Client, Acc) ->
- case stream_body(Client) of
- {ok, Data, Client2} ->
- response_body_loop(Client2, << Acc/binary, Data/binary >>);
- {done, Client2} ->
- {ok, Acc, Client2};
- {error, Reason} ->
- {error, Reason}
- end.
-
-skip_body(Client=#client{state=response_body}) ->
- case stream_body(Client) of
- {ok, _, Client2} -> skip_body(Client2);
- Done -> Done
- end.
-
-stream_status(Client=#client{state=State, buffer=Buffer})
- when State =:= request ->
- case binary:split(Buffer, <<"\r\n">>) of
- [Line, Rest] ->
- parse_version(Client#client{state=response, buffer=Rest}, Line);
- _ ->
- case recv(Client) of
- {ok, Data} ->
- Buffer2 = << Buffer/binary, Data/binary >>,
- stream_status(Client#client{buffer=Buffer2});
- {error, Reason} ->
- {error, Reason}
- end
- end.
-
-parse_version(Client, << "HTTP/1.1 ", Rest/binary >>) ->
- parse_status(Client, Rest, 'HTTP/1.1');
-parse_version(Client, << "HTTP/1.0 ", Rest/binary >>) ->
- parse_status(Client, Rest, 'HTTP/1.0').
-
-parse_status(Client, << S3, S2, S1, " ", StatusStr/binary >>, Version)
- when S3 >= $0, S3 =< $9, S2 >= $0, S2 =< $9, S1 >= $0, S1 =< $9 ->
- Status = (S3 - $0) * 100 + (S2 - $0) * 10 + S1 - $0,
- {ok, Status, StatusStr, Client#client{version=Version}}.
-
-stream_headers(Client=#client{state=State})
- when State =:= response ->
- stream_headers(Client, []).
-
-stream_headers(Client, Acc) ->
- case stream_header(Client) of
- {ok, Name, Value, Client2} ->
- stream_headers(Client2, [{Name, Value}|Acc]);
- {done, Client2} ->
- {ok, Acc, Client2};
- {error, Reason} ->
- {error, Reason}
- end.
-
-stream_header(Client=#client{state=State, buffer=Buffer,
- response_body=RespBody}) when State =:= response ->
- case binary:split(Buffer, <<"\r\n">>) of
- [<<>>, Rest] ->
- %% If we have a body, set response_body.
- Client2 = case RespBody of
- undefined -> Client#client{state=request};
- 0 -> Client#client{state=request};
- _ -> Client#client{state=response_body}
- end,
- {done, Client2#client{buffer=Rest}};
- [Line, Rest] ->
- %% @todo Do a better parsing later on.
- [Name, Value] = binary:split(Line, <<": ">>),
- Name2 = cowboy_bstr:to_lower(Name),
- Client2 = case Name2 of
- <<"content-length">> ->
- Length = list_to_integer(binary_to_list(Value)),
- if Length >= 0 -> ok end,
- Client#client{response_body=Length};
- _ ->
- Client
- end,
- {ok, Name2, Value, Client2#client{buffer=Rest}};
- _ ->
- case recv(Client) of
- {ok, Data} ->
- Buffer2 = << Buffer/binary, Data/binary >>,
- stream_header(Client#client{buffer=Buffer2});
- {error, Reason} ->
- {error, Reason}
- end
- end.
-
-stream_body(Client=#client{state=response_body, response_body=RespBody})
- when RespBody =:= undefined; RespBody =:= 0 ->
- {done, Client#client{state=request, response_body=undefined}};
-stream_body(Client=#client{state=response_body, buffer=Buffer,
- response_body=Length}) when is_integer(Length) ->
- case byte_size(Buffer) of
- 0 ->
- case recv(Client) of
- {ok, Body} when byte_size(Body) =< Length ->
- Length2 = Length - byte_size(Body),
- {ok, Body, Client#client{response_body=Length2}};
- {ok, Data} ->
- << Body:Length/binary, Rest/binary >> = Data,
- {ok, Body, Client#client{buffer=Rest,
- response_body=undefined}};
- {error, Reason} ->
- {error, Reason}
- end;
- N when N =< Length ->
- Length2 = Length - N,
- {ok, Buffer, Client#client{buffer= <<>>, response_body=Length2}};
- _ ->
- << Body:Length/binary, Rest/binary >> = Buffer,
- {ok, Body, Client#client{buffer=Rest, response_body=undefined}}
- end.
-
-recv(#client{socket=Socket, transport=Transport, timeout=Timeout}) ->
- Transport:recv(Socket, 0, Timeout).
diff --git a/src/cowboy_clock.erl b/src/cowboy_clock.erl
index 8362c99..2de3470 100644
--- a/src/cowboy_clock.erl
+++ b/src/cowboy_clock.erl
@@ -12,8 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Date and time related functions.
-%%
%% While a gen_server process runs in the background to update
%% the cache of formatted dates every second, all API calls are
%% local and directly read from the ETS cache table, providing
@@ -41,68 +39,63 @@
tref = undefined :: undefined | timer:tref()
}).
--define(SERVER, ?MODULE).
--define(TABLE, ?MODULE).
-
%% API.
-%% @private
-spec start_link() -> {ok, pid()}.
start_link() ->
- gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-%% @private
-spec stop() -> stopped.
stop() ->
- gen_server:call(?SERVER, stop).
+ gen_server:call(?MODULE, stop).
-%% @doc Return the current date and time formatted according to RFC-1123.
-spec rfc1123() -> binary().
rfc1123() ->
- ets:lookup_element(?TABLE, rfc1123, 2).
+ ets:lookup_element(?MODULE, rfc1123, 2).
-%% @doc Return the given date and time formatted according to RFC-1123.
-spec rfc1123(calendar:datetime()) -> binary().
rfc1123(DateTime) ->
update_rfc1123(<<>>, undefined, DateTime).
%% gen_server.
-%% @private
+-spec init([]) -> {ok, #state{}}.
init([]) ->
- ?TABLE = ets:new(?TABLE, [set, protected,
+ ?MODULE = ets:new(?MODULE, [set, protected,
named_table, {read_concurrency, true}]),
T = erlang:universaltime(),
B = update_rfc1123(<<>>, undefined, T),
{ok, TRef} = timer:send_interval(1000, update),
- ets:insert(?TABLE, {rfc1123, B}),
+ ets:insert(?MODULE, {rfc1123, B}),
{ok, #state{universaltime=T, rfc1123=B, tref=TRef}}.
-%% @private
+-spec handle_call(any(), _, State)
+ -> {reply, ignored, State} | {stop, normal, stopped, State}
+ when State::#state{}.
handle_call(stop, _From, State=#state{tref=TRef}) ->
{ok, cancel} = timer:cancel(TRef),
{stop, normal, stopped, State};
handle_call(_Request, _From, State) ->
{reply, ignored, State}.
-%% @private
+-spec handle_cast(_, State) -> {noreply, State} when State::#state{}.
handle_cast(_Msg, State) ->
{noreply, State}.
-%% @private
+-spec handle_info(any(), State) -> {noreply, State} when State::#state{}.
handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef}) ->
T = erlang:universaltime(),
B2 = update_rfc1123(B1, Prev, T),
- ets:insert(?TABLE, {rfc1123, B2}),
+ ets:insert(?MODULE, {rfc1123, B2}),
{noreply, #state{universaltime=T, rfc1123=B2, tref=TRef}};
handle_info(_Info, State) ->
{noreply, State}.
-%% @private
+-spec terminate(_, _) -> ok.
terminate(_Reason, _State) ->
ok.
-%% @private
+-spec code_change(_, State, _) -> {ok, State} when State::#state{}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
@@ -174,7 +167,6 @@ month(12) -> <<"Dec">>.
%% Tests.
-ifdef(TEST).
-
update_rfc1123_test_() ->
Tests = [
{<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined,
@@ -215,5 +207,4 @@ pad_int_test_() ->
{56, <<"56">>}, {57, <<"57">>}, {58, <<"58">>}, {59, <<"59">>}
],
[{I, fun() -> O = pad_int(I) end} || {I, O} <- Tests].
-
-endif.
diff --git a/src/cowboy_handler.erl b/src/cowboy_handler.erl
index ac95de5..7780e20 100644
--- a/src/cowboy_handler.erl
+++ b/src/cowboy_handler.erl
@@ -12,7 +12,7 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Handler middleware.
+%% Handler middleware.
%%
%% Execute the handler given by the <em>handler</em> and <em>handler_opts</em>
%% environment values. The result of this execution is added to the
@@ -27,8 +27,6 @@
%% by default. This can be configured through the <em>loop_max_buffer</em>
%% environment value. The request will be terminated with an
%% <em>{error, overflow}</em> reason if this threshold is reached.
-%%
-%% @see cowboy_http_handler
-module(cowboy_handler).
-behaviour(cowboy_middleware).
@@ -45,7 +43,6 @@
resp_sent = false :: boolean()
}).
-%% @private
-spec execute(Req, Env)
-> {ok, Req, Env} | {error, 500, Req}
| {suspend, ?MODULE, handler_loop, [any()]}
@@ -148,8 +145,6 @@ handler_after_callback(Req, State=#state{resp_sent=false}, Handler,
handler_after_callback(Req, State, Handler, HandlerState) ->
handler_before_loop(Req, State, Handler, HandlerState).
-%% We don't listen for Transport closes because that would force us
-%% to receive data and buffer it indefinitely.
-spec handler_before_loop(Req, #state{}, module(), any())
-> {ok, Req, cowboy_middleware:env()}
| {error, 500, Req} | {suspend, module(), atom(), [any()]}
@@ -177,7 +172,6 @@ handler_loop_timeout(State=#state{loop_timeout=Timeout,
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
State#state{loop_timeout_ref=TRef}.
-%% @private
-spec handler_loop(Req, #state{}, module(), any())
-> {ok, Req, cowboy_middleware:env()}
| {error, 500, Req} | {suspend, module(), atom(), [any()]}
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index 30866de..754e74a 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -13,7 +13,7 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Core HTTP parsing API.
+%% Deprecated HTTP parsing API.
-module(cowboy_http).
%% Parsing.
@@ -40,19 +40,10 @@
-export([parameterized_tokens/1]).
%% Decoding.
--export([te_chunked/2]).
--export([te_identity/2]).
-export([ce_identity/1]).
-%% Interpretation.
--export([urldecode/1]).
--export([urldecode/2]).
--export([urlencode/1]).
--export([urlencode/2]).
-
%% Parsing.
-%% @doc Parse a non-empty list of the given type.
-spec nonempty_list(binary(), fun()) -> [any(), ...] | {error, badarg}.
nonempty_list(Data, Fun) ->
case list(Data, Fun, []) of
@@ -61,7 +52,6 @@ nonempty_list(Data, Fun) ->
L -> lists:reverse(L)
end.
-%% @doc Parse a list of the given type.
-spec list(binary(), fun()) -> list() | {error, badarg}.
list(Data, Fun) ->
case list(Data, Fun, []) of
@@ -89,8 +79,6 @@ list(Data, Fun, Acc) ->
end)
end).
-%% @doc Parse a content type.
-%%
%% We lowercase the charset header as we know it's case insensitive.
-spec content_type(binary()) -> any().
content_type(Data) ->
@@ -112,7 +100,6 @@ content_type(Data) ->
end)
end).
-%% @doc Parse a media range.
-spec media_range(binary(), fun()) -> any().
media_range(Data, Fun) ->
media_type(Data,
@@ -155,7 +142,6 @@ media_range_param_value(Data, Fun, Type, SubType, Acc, Attr) ->
Type, SubType, [{Attr, Value}|Acc])
end).
-%% @doc Parse a media type.
-spec media_type(binary(), fun()) -> any().
media_type(Data, Fun) ->
token_ci(Data,
@@ -215,8 +201,6 @@ accept_ext_value(Data, Fun, Type, SubType, Params, Quality, Acc, Attr) ->
Type, SubType, Params, Quality, [{Attr, Value}|Acc])
end).
-%% @doc Parse a conneg header (Accept-Charset, Accept-Encoding),
-%% followed by an optional quality value.
-spec conneg(binary(), fun()) -> any().
conneg(Data, Fun) ->
token_ci(Data,
@@ -228,7 +212,6 @@ conneg(Data, Fun) ->
end)
end).
-%% @doc Parse a language range, followed by an optional quality value.
-spec language_range(binary(), fun()) -> any().
language_range(<< $*, Rest/binary >>, Fun) ->
language_range_ret(Rest, Fun, '*');
@@ -289,12 +272,10 @@ maybe_qparam(Data, Fun) ->
Fun(Rest, 1000)
end).
-%% @doc Parse a quality parameter string (for example q=0.500).
-spec qparam(binary(), fun()) -> any().
qparam(<< Q, $=, Data/binary >>, Fun) when Q =:= $q; Q =:= $Q ->
qvalue(Data, Fun).
-%% @doc Parse either a list of entity tags or a "*".
-spec entity_tag_match(binary()) -> any().
entity_tag_match(<< $*, Rest/binary >>) ->
whitespace(Rest,
@@ -304,7 +285,6 @@ entity_tag_match(<< $*, Rest/binary >>) ->
entity_tag_match(Data) ->
nonempty_list(Data, fun entity_tag/2).
-%% @doc Parse an entity-tag.
-spec entity_tag(binary(), fun()) -> any().
entity_tag(<< "W/", Rest/binary >>, Fun) ->
opaque_tag(Rest, Fun, weak);
@@ -318,7 +298,6 @@ opaque_tag(Data, Fun, Strength) ->
(Rest, OpaqueTag) -> Fun(Rest, {Strength, OpaqueTag})
end).
-%% @doc Parse an expectation.
-spec expectation(binary(), fun()) -> any().
expectation(Data, Fun) ->
token_ci(Data,
@@ -334,7 +313,6 @@ expectation(Data, Fun) ->
Fun(Rest, Expectation)
end).
-%% @doc Parse a list of parameters (a=b;c=d).
-spec params(binary(), fun()) -> any().
params(Data, Fun) ->
params(Data, Fun, []).
@@ -366,9 +344,6 @@ param(Data, Fun) ->
end)
end).
-%% @doc Parse an HTTP date (RFC1123, RFC850 or asctime date).
-%% @end
-%%
%% While this may not be the most efficient date parsing we can do,
%% it should work fine for our purposes because all HTTP dates should
%% be sent as RFC1123 dates in HTTP/1.1.
@@ -391,7 +366,6 @@ http_date(Data) ->
HTTPDate
end.
-%% @doc Parse an RFC1123 date.
-spec rfc1123_date(binary()) -> any().
rfc1123_date(Data) ->
wkday(Data,
@@ -411,7 +385,6 @@ rfc1123_date(Data) ->
{error, badarg}
end).
-%% @doc Parse an RFC850 date.
-spec rfc850_date(binary()) -> any().
%% From the RFC:
%% HTTP/1.1 clients and caches SHOULD assume that an RFC-850 date
@@ -435,7 +408,6 @@ rfc850_date(Data) ->
{error, badarg}
end).
-%% @doc Parse an asctime date.
-spec asctime_date(binary()) -> any().
asctime_date(Data) ->
wkday(Data,
@@ -594,7 +566,6 @@ time(<< H1, H2, ":", M1, M2, ":", S1, S2, Rest/binary >>, Fun)
{error, badarg}
end.
-%% @doc Skip whitespace.
-spec whitespace(binary(), fun()) -> any().
whitespace(<< C, Rest/binary >>, Fun)
when C =:= $\s; C =:= $\t ->
@@ -602,7 +573,6 @@ whitespace(<< C, Rest/binary >>, Fun)
whitespace(Data, Fun) ->
Fun(Data).
-%% @doc Parse a list of digits as a non negative integer.
-spec digits(binary()) -> non_neg_integer() | {error, badarg}.
digits(Data) ->
digits(Data,
@@ -629,8 +599,6 @@ digits(<< C, Rest/binary >>, Fun, Acc)
digits(Data, Fun, Acc) ->
Fun(Data, Acc).
-%% @doc Parse a list of case-insensitive alpha characters.
-%%
%% Changes all characters to lowercase.
-spec alpha(binary(), fun()) -> any().
alpha(Data, Fun) ->
@@ -647,7 +615,6 @@ alpha(<< C, Rest/binary >>, Fun, Acc)
alpha(Data, Fun, Acc) ->
Fun(Data, Acc).
-%% @doc Parse either a token or a quoted string.
-spec word(binary(), fun()) -> any().
word(Data = << $", _/binary >>, Fun) ->
quoted_string(Data, Fun);
@@ -657,14 +624,11 @@ word(Data, Fun) ->
(Rest, Token) -> Fun(Rest, Token)
end).
-%% @doc Parse a case-insensitive token.
-%%
%% Changes all characters to lowercase.
-spec token_ci(binary(), fun()) -> any().
token_ci(Data, Fun) ->
token(Data, Fun, ci, <<>>).
-%% @doc Parse a token.
-spec token(binary(), fun()) -> any().
token(Data, Fun) ->
token(Data, Fun, cs, <<>>).
@@ -685,7 +649,6 @@ token(<< C, Rest/binary >>, Fun, Case = ci, Acc) ->
token(<< C, Rest/binary >>, Fun, Case, Acc) ->
token(Rest, Fun, Case, << Acc/binary, C >>).
-%% @doc Parse a quoted string.
-spec quoted_string(binary(), fun()) -> any().
quoted_string(<< $", Rest/binary >>, Fun) ->
quoted_string(Rest, Fun, <<>>).
@@ -700,7 +663,6 @@ quoted_string(<< $\\, C, Rest/binary >>, Fun, Acc) ->
quoted_string(<< C, Rest/binary >>, Fun, Acc) ->
quoted_string(Rest, Fun, << Acc/binary, C >>).
-%% @doc Parse a quality value.
-spec qvalue(binary(), fun()) -> any().
qvalue(<< $0, $., Rest/binary >>, Fun) ->
qvalue(Rest, Fun, 0, 100);
@@ -729,8 +691,7 @@ qvalue(<< C, Rest/binary >>, Fun, Q, M)
qvalue(Data, Fun, Q, _M) ->
Fun(Data, Q).
-%% @doc Parse authorization value according rfc 2617.
-%% Only Basic authorization is supported so far.
+%% Only RFC2617 Basic authorization is supported so far.
-spec authorization(binary(), binary()) -> {binary(), any()} | {error, badarg}.
authorization(UserPass, Type = <<"basic">>) ->
whitespace(UserPass,
@@ -746,7 +707,6 @@ authorization(UserPass, Type = <<"basic">>) ->
authorization(String, Type) ->
whitespace(String, fun(Rest) -> {Type, Rest} end).
-%% @doc Parse user credentials.
-spec authorization_basic_userid(binary(), fun()) -> any().
authorization_basic_userid(Data, Fun) ->
authorization_basic_userid(Data, Fun, <<>>).
@@ -773,7 +733,6 @@ authorization_basic_password(<<>>, Fun, Acc) ->
authorization_basic_password(<<C, Rest/binary>>, Fun, Acc) ->
authorization_basic_password(Rest, Fun, <<Acc/binary, C>>).
-%% @doc Parse range header according rfc 2616.
-spec range(binary()) -> {Unit, [Range]} | {error, badarg} when
Unit :: binary(),
Range :: {non_neg_integer(), non_neg_integer() | infinity} | neg_integer().
@@ -831,7 +790,6 @@ range_digits(Data, Default, Fun) ->
Fun(Data, Default)
end).
-%% @doc Parse a non empty list of tokens followed with optional parameters.
-spec parameterized_tokens(binary()) -> any().
parameterized_tokens(Data) ->
nonempty_list(Data,
@@ -876,170 +834,15 @@ parameterized_tokens_param(Data, Fun) ->
%% Decoding.
-%% @doc Decode a stream of chunks.
--spec te_chunked(Bin, TransferState)
- -> more | {more, non_neg_integer(), Bin, TransferState}
- | {ok, Bin, Bin, TransferState}
- | {done, non_neg_integer(), Bin} | {error, badarg}
- when Bin::binary(), TransferState::{non_neg_integer(), non_neg_integer()}.
-te_chunked(<< "0\r\n\r\n", Rest/binary >>, {0, Streamed}) ->
- {done, Streamed, Rest};
-te_chunked(Data, {0, Streamed}) ->
- %% @todo We are expecting an hex size, not a general token.
- token(Data,
- fun (<< "\r\n", Rest/binary >>, BinLen) ->
- case list_to_integer(binary_to_list(BinLen), 16) of
- %% Final chunk is parsed in one go above. Rest would be
- %% <<\r\n">> if complete.
- 0 when byte_size(Rest) < 2 ->
- more;
- %% Normal chunk. Add 2 to Len for trailing <<"\r\n">>. Note
- %% that repeated <<"-2\r\n">> would be streamed, and
- %% accumulated, until out of memory if Len could be -2.
- Len when Len > 0 ->
- te_chunked(Rest, {Len + 2, Streamed})
- end;
- %% Chunk size shouldn't take too many bytes,
- %% don't try to stream forever.
- (Rest, _) when byte_size(Rest) < 16 ->
- more;
- (_, _) ->
- {error, badarg}
- end);
-%% <<"\n">> from trailing <<"\r\n">>.
-te_chunked(<< "\n", Rest/binary>>, {1, Streamed}) ->
- {ok, <<>>, Rest, {0, Streamed}};
-te_chunked(<<>>, State={1, _Streamed}) ->
- {more, 1, <<>>, State};
-%% Remainder of chunk (if any) and as much of trailing <<"\r\n">> as possible.
-te_chunked(Data, {ChunkRem, Streamed}) when byte_size(Data) >= ChunkRem - 2 ->
- ChunkSize = ChunkRem - 2,
- Streamed2 = Streamed + ChunkSize,
- case Data of
- << Chunk:ChunkSize/binary, "\r\n", Rest/binary >> ->
- {ok, Chunk, Rest, {0, Streamed2}};
- << Chunk:ChunkSize/binary, "\r" >> ->
- {more, 1, Chunk, {1, Streamed2}};
- << Chunk:ChunkSize/binary >> ->
- {more, 2, Chunk, {2, Streamed2}}
- end;
-%% Incomplete chunk.
-te_chunked(Data, {ChunkRem, Streamed}) ->
- ChunkRem2 = ChunkRem - byte_size(Data),
- Streamed2 = Streamed + byte_size(Data),
- {more, ChunkRem2, Data, {ChunkRem2, Streamed2}}.
-
-%% @doc Decode an identity stream.
--spec te_identity(Bin, TransferState)
- -> {more, non_neg_integer(), Bin, TransferState}
- | {done, Bin, non_neg_integer(), Bin}
- when Bin::binary(), TransferState::{non_neg_integer(), non_neg_integer()}.
-te_identity(Data, {Streamed, Total})
- when Streamed + byte_size(Data) < Total ->
- Streamed2 = Streamed + byte_size(Data),
- {more, Total - Streamed2, Data, {Streamed2, Total}};
-te_identity(Data, {Streamed, Total}) ->
- Size = Total - Streamed,
- << Data2:Size/binary, Rest/binary >> = Data,
- {done, Data2, Total, Rest}.
-
-%% @doc Decode an identity content.
+%% @todo Move this to cowlib too I suppose. :-)
-spec ce_identity(binary()) -> {ok, binary()}.
ce_identity(Data) ->
{ok, Data}.
-%% Interpretation.
-
-%% @doc Decode a URL encoded binary.
-%% @equiv urldecode(Bin, crash)
--spec urldecode(binary()) -> binary().
-urldecode(Bin) when is_binary(Bin) ->
- urldecode(Bin, <<>>, crash).
-
-%% @doc Decode a URL encoded binary.
-%% The second argument specifies how to handle percent characters that are not
-%% followed by two valid hex characters. Use `skip' to ignore such errors,
-%% if `crash' is used the function will fail with the reason `badarg'.
--spec urldecode(binary(), crash | skip) -> binary().
-urldecode(Bin, OnError) when is_binary(Bin) ->
- urldecode(Bin, <<>>, OnError).
-
--spec urldecode(binary(), binary(), crash | skip) -> binary().
-urldecode(<<$%, H, L, Rest/binary>>, Acc, OnError) ->
- G = unhex(H),
- M = unhex(L),
- if G =:= error; M =:= error ->
- case OnError of skip -> ok; crash -> erlang:error(badarg) end,
- urldecode(<<H, L, Rest/binary>>, <<Acc/binary, $%>>, OnError);
- true ->
- urldecode(Rest, <<Acc/binary, (G bsl 4 bor M)>>, OnError)
- end;
-urldecode(<<$%, Rest/binary>>, Acc, OnError) ->
- case OnError of skip -> ok; crash -> erlang:error(badarg) end,
- urldecode(Rest, <<Acc/binary, $%>>, OnError);
-urldecode(<<$+, Rest/binary>>, Acc, OnError) ->
- urldecode(Rest, <<Acc/binary, $ >>, OnError);
-urldecode(<<C, Rest/binary>>, Acc, OnError) ->
- urldecode(Rest, <<Acc/binary, C>>, OnError);
-urldecode(<<>>, Acc, _OnError) ->
- Acc.
-
--spec unhex(byte()) -> byte() | error.
-unhex(C) when C >= $0, C =< $9 -> C - $0;
-unhex(C) when C >= $A, C =< $F -> C - $A + 10;
-unhex(C) when C >= $a, C =< $f -> C - $a + 10;
-unhex(_) -> error.
-
-
-%% @doc URL encode a string binary.
-%% @equiv urlencode(Bin, [])
--spec urlencode(binary()) -> binary().
-urlencode(Bin) ->
- urlencode(Bin, []).
-
-%% @doc URL encode a string binary.
-%% The `noplus' option disables the default behaviour of quoting space
-%% characters, `\s', as `+'. The `upper' option overrides the default behaviour
-%% of writing hex numbers using lowecase letters to using uppercase letters
-%% instead.
--spec urlencode(binary(), [noplus|upper]) -> binary().
-urlencode(Bin, Opts) ->
- Plus = not lists:member(noplus, Opts),
- Upper = lists:member(upper, Opts),
- urlencode(Bin, <<>>, Plus, Upper).
-
--spec urlencode(binary(), binary(), boolean(), boolean()) -> binary().
-urlencode(<<C, Rest/binary>>, Acc, P=Plus, U=Upper) ->
- if C >= $0, C =< $9 -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C >= $A, C =< $Z -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C >= $a, C =< $z -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C =:= $.; C =:= $-; C =:= $~; C =:= $_ ->
- urlencode(Rest, <<Acc/binary, C>>, P, U);
- C =:= $ , Plus ->
- urlencode(Rest, <<Acc/binary, $+>>, P, U);
- true ->
- H = C band 16#F0 bsr 4, L = C band 16#0F,
- H1 = if Upper -> tohexu(H); true -> tohexl(H) end,
- L1 = if Upper -> tohexu(L); true -> tohexl(L) end,
- urlencode(Rest, <<Acc/binary, $%, H1, L1>>, P, U)
- end;
-urlencode(<<>>, Acc, _Plus, _Upper) ->
- Acc.
-
--spec tohexu(byte()) -> byte().
-tohexu(C) when C < 10 -> $0 + C;
-tohexu(C) when C < 16 -> $A + C - 10.
-
--spec tohexl(byte()) -> byte().
-tohexl(C) when C < 10 -> $0 + C;
-tohexl(C) when C < 16 -> $a + C - 10.
-
%% Tests.
-ifdef(TEST).
-
nonempty_charset_list_test_() ->
- %% {Value, Result}
Tests = [
{<<>>, {error, badarg}},
{<<"iso-8859-5, unicode-1-1;q=0.8">>, [
@@ -1056,7 +859,6 @@ nonempty_charset_list_test_() ->
[{V, fun() -> R = nonempty_list(V, fun conneg/2) end} || {V, R} <- Tests].
nonempty_language_range_list_test_() ->
- %% {Value, Result}
Tests = [
{<<"da, en-gb;q=0.8, en;q=0.7">>, [
{<<"da">>, 1000},
@@ -1075,7 +877,6 @@ nonempty_language_range_list_test_() ->
|| {V, R} <- Tests].
nonempty_token_list_test_() ->
- %% {Value, Result}
Tests = [
{<<>>, {error, badarg}},
{<<" ">>, {error, badarg}},
@@ -1091,7 +892,6 @@ nonempty_token_list_test_() ->
[{V, fun() -> R = nonempty_list(V, fun token/2) end} || {V, R} <- Tests].
media_range_list_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"audio/*; q=0.2, audio/basic">>, [
{{<<"audio">>, <<"*">>, []}, 200, []},
@@ -1136,7 +936,6 @@ media_range_list_test_() ->
[{V, fun() -> R = list(V, fun media_range/2) end} || {V, R} <- Tests].
entity_tag_match_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"\"xyzzy\"">>, [{strong, <<"xyzzy">>}]},
{<<"\"xyzzy\", W/\"r2d2xxxx\", \"c3piozzzz\"">>,
@@ -1148,7 +947,6 @@ entity_tag_match_test_() ->
[{V, fun() -> R = entity_tag_match(V) end} || {V, R} <- Tests].
http_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}},
{<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}},
@@ -1157,28 +955,24 @@ http_date_test_() ->
[{V, fun() -> R = http_date(V) end} || {V, R} <- Tests].
rfc1123_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}
],
[{V, fun() -> R = rfc1123_date(V) end} || {V, R} <- Tests].
rfc850_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}
],
[{V, fun() -> R = rfc850_date(V) end} || {V, R} <- Tests].
asctime_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sun Nov 6 08:49:37 1994">>, {{1994, 11, 6}, {8, 49, 37}}}
],
[{V, fun() -> R = asctime_date(V) end} || {V, R} <- Tests].
content_type_test_() ->
- %% {ContentType, Result}
Tests = [
{<<"text/plain; charset=iso-8859-4">>,
{<<"text">>, <<"plain">>, [{<<"charset">>, <<"iso-8859-4">>}]}},
@@ -1195,7 +989,6 @@ content_type_test_() ->
[{V, fun () -> R = content_type(V) end} || {V, R} <- Tests].
parameterized_tokens_test_() ->
- %% {ParameterizedTokens, Result}
Tests = [
{<<"foo">>, [{<<"foo">>, []}]},
{<<"bar; baz=2">>, [{<<"bar">>, [{<<"baz">>, <<"2">>}]}]},
@@ -1206,7 +999,6 @@ parameterized_tokens_test_() ->
[{V, fun () -> R = parameterized_tokens(V) end} || {V, R} <- Tests].
digits_test_() ->
- %% {Digits, Result}
Tests = [
{<<"42 ">>, 42},
{<<"69\t">>, 69},
@@ -1214,41 +1006,6 @@ digits_test_() ->
],
[{V, fun() -> R = digits(V) end} || {V, R} <- Tests].
-urldecode_test_() ->
- F = fun(Qs, O) ->
- try urldecode(Qs, O) of
- R ->
- {ok, R}
- catch _:E ->
- {error, E}
- end
- end,
- Tests = [
- {<<"%20">>, crash, {ok, <<" ">>}},
- {<<"+">>, crash, {ok, <<" ">>}},
- {<<"%00">>, crash, {ok, <<0>>}},
- {<<"%fF">>, crash, {ok, <<255>>}},
- {<<"123">>, crash, {ok, <<"123">>}},
- {<<"%i5">>, skip, {ok, <<"%i5">>}},
- {<<"%5">>, skip, {ok, <<"%5">>}},
- {<<"%i5">>, crash, {error, badarg}},
- {<<"%5">>, crash, {error, badarg}}
- ],
- [{Qs, fun() -> R = F(Qs,O) end} || {Qs, O, R} <- Tests].
-
-urlencode_test_() ->
- Tests = [
- {<<255,0>>, [], <<"%ff%00">>},
- {<<255,0>>, [upper], <<"%FF%00">>},
- {<<" ">>, [], <<"+">>},
- {<<" ">>, [noplus], <<"%20">>},
- {<<"aBc">>, [], <<"aBc">>},
- {<<".-~_">>, [], <<".-~_">>}
- ],
- Tests2 = [{<<255, " ">>,<<"%ff+">>}],
- [{V, fun() -> R = urlencode(V, O) end} || {V, O, R} <- Tests] ++
- [{V, fun() -> R = urlencode(V) end} || {V, R} <- Tests2].
-
http_authorization_test_() ->
Tests = [
{<<"basic">>, <<"QWxsYWRpbjpvcGVuIHNlc2FtZQ==">>,
@@ -1288,5 +1045,4 @@ http_range_test_() ->
{error, badarg}}
],
[fun() -> R = range(V) end ||{V, R} <- Tests].
-
-endif.
diff --git a/src/cowboy_http_handler.erl b/src/cowboy_http_handler.erl
index 218e0a4..14c7987 100644
--- a/src/cowboy_http_handler.erl
+++ b/src/cowboy_http_handler.erl
@@ -12,23 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for short-lived HTTP handlers.
-%%
-%% <em>init/3</em> allows you to initialize a state for all subsequent
-%% callbacks, and indicate to Cowboy whether you accept to handle the
-%% request or want to shutdown without handling it, in which case the
-%% <em>handle/2</em> call will simply be skipped.
-%%
-%% <em>handle/2</em> allows you to handle the request. It receives the
-%% state previously defined.
-%%
-%% <em>terminate/3</em> allows you to clean up. It receives the
-%% termination reason and the state previously defined.
-%%
-%% There is no required operation to perform in any of these callbacks
-%% other than returning the proper values. Make sure you always return
-%% the last modified Req so that Cowboy has the up to date information
-%% about the request.
-module(cowboy_http_handler).
-type opts() :: any().
diff --git a/src/cowboy_loop_handler.erl b/src/cowboy_loop_handler.erl
index f283f0b..edef77f 100644
--- a/src/cowboy_loop_handler.erl
+++ b/src/cowboy_loop_handler.erl
@@ -12,29 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for long-lived HTTP handlers.
-%%
-%% <em>init/3</em> allows you to initialize a state for all subsequent
-%% callbacks, and indicate to Cowboy whether you accept to handle the
-%% request or want to shutdown without handling it, in which case the
-%% receive loop and <em>info/3</em> calls will simply be skipped.
-%%
-%% <em>info/3</em> allows you to handle the messages this process will
-%% receive. It receives the message and the state previously defined.
-%% It can decide to stop the receive loop or continue receiving.
-%%
-%% <em>terminate/3</em> allows you to clean up. It receives the
-%% termination reason and the state previously defined.
-%%
-%% There is no required operation to perform in any of these callbacks
-%% other than returning the proper values. Make sure you always return
-%% the last modified Req so that Cowboy has the up to date information
-%% about the request.
-%%
-%% It is recommended to use hibernate if this process is not going to
-%% receive a lot of messages. It is also recommended to use a timeout
-%% value so that the connection gets closed after a long period of
-%% inactivity.
-module(cowboy_loop_handler).
-type opts() :: any().
diff --git a/src/cowboy_middleware.erl b/src/cowboy_middleware.erl
index b268366..fa0f5bc 100644
--- a/src/cowboy_middleware.erl
+++ b/src/cowboy_middleware.erl
@@ -12,17 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for middlewares.
-%%
-%% Only one function needs to be implemented, <em>execute/2</em>.
-%% It receives the Req and the environment and returns them
-%% optionally modified. It can decide to stop the processing with
-%% or without an error. It is also possible to hibernate the process
-%% if needed.
-%%
-%% A middleware can perform any operation. Make sure you always return
-%% the last modified Req so that Cowboy has the up to date information
-%% about the request.
-module(cowboy_middleware).
-type env() :: [{atom(), any()}].
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl
index ec05eb3..22faf1b 100644
--- a/src/cowboy_protocol.erl
+++ b/src/cowboy_protocol.erl
@@ -13,39 +13,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc HTTP protocol handler.
-%%
-%% The available options are:
-%% <dl>
-%% <dt>compress</dt><dd>Whether to automatically compress the response
-%% body when the conditions are met. Disabled by default.</dd>
-%% <dt>env</dt><dd>The environment passed and optionally modified
-%% by middlewares.</dd>
-%% <dt>max_empty_lines</dt><dd>Max number of empty lines before a request.
-%% Defaults to 5.</dd>
-%% <dt>max_header_name_length</dt><dd>Max length allowed for header names.
-%% Defaults to 64.</dd>
-%% <dt>max_header_value_length</dt><dd>Max length allowed for header values.
-%% Defaults to 4096.</dd>
-%% <dt>max_headers</dt><dd>Max number of headers allowed.
-%% Defaults to 100.</dd>
-%% <dt>max_keepalive</dt><dd>Max number of requests allowed in a single
-%% keep-alive session. Defaults to 100.</dd>
-%% <dt>max_request_line_length</dt><dd>Max length allowed for the request
-%% line. Defaults to 4096.</dd>
-%% <dt>middlewares</dt><dd>The list of middlewares to execute when a
-%% request is received.</dd>
-%% <dt>onrequest</dt><dd>Optional fun that allows Req interaction before
-%% any dispatching is done. Host info, path info and bindings are thus
-%% not available at this point.</dd>
-%% <dt>onresponse</dt><dd>Optional fun that allows replacing a response
-%% sent by the application.</dd>
-%% <dt>timeout</dt><dd>Time in milliseconds a client has to send the
-%% full request line and headers. Defaults to 5000 milliseconds.</dd>
-%% </dl>
-%%
-%% Note that there is no need to monitor these processes when using Cowboy as
-%% an application as it already supervises them under the listener supervisor.
-module(cowboy_protocol).
%% API.
@@ -93,7 +60,6 @@
%% API.
-%% @doc Start an HTTP protocol process.
-spec start_link(ranch:ref(), inet:socket(), module(), opts()) -> {ok, pid()}.
start_link(Ref, Socket, Transport, Opts) ->
Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
@@ -101,15 +67,13 @@ start_link(Ref, Socket, Transport, Opts) ->
%% Internal.
-%% @doc Faster alternative to proplists:get_value/3.
-%% @private
+%% Faster alternative to proplists:get_value/3.
get_value(Key, Opts, Default) ->
case lists:keyfind(Key, 1, Opts) of
{_, Value} -> Value;
_ -> Default
end.
-%% @private
-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
init(Ref, Socket, Transport, Opts) ->
Compress = get_value(compress, Opts, false),
@@ -172,7 +136,6 @@ wait_request(Buffer, State=#state{socket=Socket, transport=Transport,
terminate(State)
end.
-%% @private
-spec parse_request(binary(), #state{}, non_neg_integer()) -> ok.
%% Empty lines must be using \r\n.
parse_request(<< $\n, _/binary >>, State, _) ->
@@ -375,7 +338,8 @@ parse_hd_value(<< $\r, Rest/bits >>, S, M, P, Q, V, Headers, Name, SoFar) ->
<< $\n >> ->
wait_hd_value_nl(<<>>, S, M, P, Q, V, Headers, Name, SoFar);
<< $\n, C, Rest2/bits >> when C =:= $\s; C =:= $\t ->
- parse_hd_value(Rest2, S, M, P, Q, V, Headers, Name, SoFar);
+ parse_hd_value(Rest2, S, M, P, Q, V, Headers, Name,
+ << SoFar/binary, C >>);
<< $\n, Rest2/bits >> ->
parse_header(Rest2, S, M, P, Q, V, [{Name, SoFar}|Headers])
end;
@@ -480,7 +444,6 @@ execute(Req, State, Env, [Middleware|Tail]) ->
error_terminate(Code, Req2, State)
end.
-%% @private
-spec resume(#state{}, cowboy_middleware:env(), [module()],
module(), module(), [any()]) -> ok.
resume(State, Env, Tail, Module, Function, Args) ->
@@ -511,8 +474,8 @@ next_request(Req, State=#state{req_keepalive=Keepalive, timeout=Timeout},
_ -> close
end,
%% Flush the resp_sent message before moving on.
- receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
if HandlerRes =:= ok, Buffer =/= close ->
+ receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
?MODULE:parse_request(Buffer,
State#state{req_keepalive=Keepalive + 1,
until=until(Timeout)}, 0);
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 815e4ca..eec4e88 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -13,31 +13,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc HTTP request manipulation API.
-%%
-%% The functions in this module try to follow this pattern for their
-%% return types:
-%% <dl>
-%% <dt>access:</dt>
-%% <dd><em>{Value, Req}</em></dd>
-%% <dt>action:</dt>
-%% <dd><em>{Result, Req} | {Result, Value, Req} | {error, atom()}</em></dd>
-%% <dt>modification:</dt>
-%% <dd><em>Req</em></dd>
-%% <dt>question (<em>has_*</em> or <em>is_*</em>):</dt>
-%% <dd><em>boolean()</em></dd>
-%% </dl>
-%%
-%% Exceptions include <em>chunk/2</em> which always returns <em>'ok'</em>,
-%% and <em>to_list/1</em> which returns a list of key/values.
-%%
-%% Also note that all body reading functions perform actions, as Cowboy
-%% doesn't read the request body until they are called.
-%%
-%% Whenever <em>Req</em> is returned, it should always be kept in place of
-%% the one given as argument in your function call, because it keeps
-%% track of the request and response state. Doing so allows Cowboy to do
-%% some lazy evaluation and cache results when possible.
-module(cowboy_req).
%% Request API.
@@ -125,11 +100,7 @@
-> {ok, binary()}
| {error, atom()}).
-type transfer_decode_fun() :: fun((binary(), any())
- -> {ok, binary(), binary(), any()}
- | more | {more, non_neg_integer(), binary(), any()}
- | {done, non_neg_integer(), binary()}
- | {done, binary(), non_neg_integer(), binary()}
- | {error, atom()}).
+ -> cow_http_te:decode_ret()).
-type resp_body_fun() :: fun((any(), module()) -> ok).
-type send_chunk_fun() :: fun((iodata()) -> ok | {error, atom()}).
@@ -184,13 +155,6 @@
%% Request API.
-%% @doc Create a new HTTP Req object.
-%%
-%% This function takes care of setting the owner's pid to self().
-%% @private
-%%
-%% Since we always need to parse the Connection header, we do it
-%% in an optimized way and add the parsed value to p_headers' cache.
-spec new(any(), module(),
undefined | {inet:ip_address(), inet:port_number()},
binary(), binary(), binary(),
@@ -213,72 +177,58 @@ new(Socket, Transport, Peer, Method, Path, Query,
false ->
Req; %% keepalive
{_, ConnectionHeader} ->
- Tokens = parse_connection_before(ConnectionHeader, []),
+ Tokens = cow_http_hd:parse_connection(ConnectionHeader),
Connection = connection_to_atom(Tokens),
Req#http_req{connection=Connection,
p_headers=[{<<"connection">>, Tokens}]}
end
end.
-%% @doc Return the HTTP method of the request.
-spec method(Req) -> {binary(), Req} when Req::req().
method(Req) ->
{Req#http_req.method, Req}.
-%% @doc Return the HTTP version used for the request.
-spec version(Req) -> {cowboy:http_version(), Req} when Req::req().
version(Req) ->
{Req#http_req.version, Req}.
-%% @doc Return the peer address and port number of the remote host.
-spec peer(Req)
-> {{inet:ip_address(), inet:port_number()}, Req}
when Req::req().
peer(Req) ->
{Req#http_req.peer, Req}.
-%% @doc Return the host binary string.
-spec host(Req) -> {binary(), Req} when Req::req().
host(Req) ->
{Req#http_req.host, Req}.
-%% @doc Return the extra host information obtained from partially matching
-%% the hostname using <em>'...'</em>.
-spec host_info(Req)
-> {cowboy_router:tokens() | undefined, Req} when Req::req().
host_info(Req) ->
{Req#http_req.host_info, Req}.
-%% @doc Return the port used for this request.
-spec port(Req) -> {inet:port_number(), Req} when Req::req().
port(Req) ->
{Req#http_req.port, Req}.
-%% @doc Return the path binary string.
-spec path(Req) -> {binary(), Req} when Req::req().
path(Req) ->
{Req#http_req.path, Req}.
-%% @doc Return the extra path information obtained from partially matching
-%% the patch using <em>'...'</em>.
-spec path_info(Req)
-> {cowboy_router:tokens() | undefined, Req} when Req::req().
path_info(Req) ->
{Req#http_req.path_info, Req}.
-%% @doc Return the raw query string directly taken from the request.
-spec qs(Req) -> {binary(), Req} when Req::req().
qs(Req) ->
{Req#http_req.qs, Req}.
-%% @equiv qs_val(Name, Req, undefined)
-spec qs_val(binary(), Req)
-> {binary() | true | undefined, Req} when Req::req().
qs_val(Name, Req) when is_binary(Name) ->
qs_val(Name, Req, undefined).
-%% @doc Return the query string value for the given key, or a default if
-%% missing.
-spec qs_val(binary(), Req, Default)
-> {binary() | true | Default, Req} when Req::req(), Default::any().
qs_val(Name, Req=#http_req{qs=RawQs, qs_vals=undefined}, Default)
@@ -291,7 +241,6 @@ qs_val(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of query string values.
-spec qs_vals(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) ->
QsVals = cow_qs:parse_qs(RawQs),
@@ -299,10 +248,7 @@ qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) ->
qs_vals(Req=#http_req{qs_vals=QsVals}) ->
{QsVals, Req}.
-%% @doc Return the request URL as a binary without the path and query string.
-%%
%% The URL includes the scheme, host and port only.
-%% @see cowboy_req:url/1
-spec host_url(Req) -> {undefined | binary(), Req} when Req::req().
host_url(Req=#http_req{port=undefined}) ->
{undefined, Req};
@@ -319,8 +265,6 @@ host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
end,
{<< "http", Secure/binary, "://", Host/binary, PortBin/binary >>, Req}.
-%% @doc Return the full request URL as a binary.
-%%
%% The URL includes the scheme, host, port, path and query string.
-spec url(Req) -> {undefined | binary(), Req} when Req::req().
url(Req=#http_req{}) ->
@@ -336,13 +280,10 @@ url(HostURL, Req=#http_req{path=Path, qs=QS}) ->
end,
{<< HostURL/binary, Path/binary, QS2/binary >>, Req}.
-%% @equiv binding(Name, Req, undefined)
-spec binding(atom(), Req) -> {any() | undefined, Req} when Req::req().
binding(Name, Req) when is_atom(Name) ->
binding(Name, Req, undefined).
-%% @doc Return the binding value for the given key obtained when matching
-%% the host and path against the dispatch list, or a default if missing.
-spec binding(atom(), Req, Default)
-> {any() | Default, Req} when Req::req(), Default::any().
binding(Name, Req, Default) when is_atom(Name) ->
@@ -351,18 +292,15 @@ binding(Name, Req, Default) when is_atom(Name) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of binding values.
-spec bindings(Req) -> {[{atom(), any()}], Req} when Req::req().
bindings(Req) ->
{Req#http_req.bindings, Req}.
-%% @equiv header(Name, Req, undefined)
-spec header(binary(), Req)
-> {binary() | undefined, Req} when Req::req().
header(Name, Req) ->
header(Name, Req, undefined).
-%% @doc Return the header value for the given key, or a default if missing.
-spec header(binary(), Req, Default)
-> {binary() | Default, Req} when Req::req(), Default::any().
header(Name, Req, Default) ->
@@ -371,16 +309,10 @@ header(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of headers.
-spec headers(Req) -> {cowboy:http_headers(), Req} when Req::req().
headers(Req) ->
{Req#http_req.headers, Req}.
-%% @doc Semantically parse headers.
-%%
-%% When the value isn't found, a proper default value for the type
-%% returned is used as a return value.
-%% @see parse_header/3
-spec parse_header(binary(), Req)
-> {ok, any(), Req} | {undefined, binary(), Req}
| {error, badarg} when Req::req().
@@ -390,14 +322,10 @@ parse_header(Name, Req=#http_req{p_headers=PHeaders}) ->
{Name, Value} -> {ok, Value, Req}
end.
-%% @doc Default values for semantic header parsing.
-spec parse_header_default(binary()) -> any().
parse_header_default(<<"transfer-encoding">>) -> [<<"identity">>];
parse_header_default(_Name) -> undefined.
-%% @doc Semantically parse headers.
-%%
-%% When the header is unknown, the value is returned directly without parsing.
-spec parse_header(binary(), Req, any())
-> {ok, any(), Req} | {undefined, binary(), Req}
| {error, badarg} when Req::req().
@@ -427,7 +355,7 @@ parse_header(Name = <<"authorization">>, Req, Default) ->
cowboy_http:token_ci(Value, fun cowboy_http:authorization/2)
end);
parse_header(Name = <<"content-length">>, Req, Default) ->
- parse_header(Name, Req, Default, fun cowboy_http:digits/1);
+ parse_header(Name, Req, Default, fun cow_http_hd:parse_content_length/1);
parse_header(Name = <<"content-type">>, Req, Default) ->
parse_header(Name, Req, Default, fun cowboy_http:content_type/1);
parse_header(Name = <<"cookie">>, Req, Default) ->
@@ -454,10 +382,10 @@ parse_header(Name, Req, Default)
fun (Value) ->
cowboy_http:nonempty_list(Value, fun cowboy_http:token/2)
end);
-%% @todo Extension parameters.
-parse_header(Name, Req, Default)
- when Name =:= <<"transfer-encoding">>;
- Name =:= <<"upgrade">> ->
+parse_header(Name = <<"transfer-encoding">>, Req, Default) ->
+ parse_header(Name, Req, Default, fun cow_http_hd:parse_transfer_encoding/1);
+%% @todo Product version.
+parse_header(Name = <<"upgrade">>, Req, Default) ->
parse_header(Name, Req, Default,
fun (Value) ->
cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
@@ -481,14 +409,11 @@ parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) ->
end
end.
-%% @equiv cookie(Name, Req, undefined)
-spec cookie(binary(), Req)
-> {binary() | undefined, Req} when Req::req().
cookie(Name, Req) when is_binary(Name) ->
cookie(Name, Req, undefined).
-%% @doc Return the cookie value for the given key, or a default if
-%% missing.
-spec cookie(binary(), Req, Default)
-> {binary() | Default, Req} when Req::req(), Default::any().
cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
@@ -504,7 +429,6 @@ cookie(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of cookie values.
-spec cookies(Req) -> {list({binary(), binary()}), Req} when Req::req().
cookies(Req=#http_req{cookies=undefined}) ->
case parse_header(<<"cookie">>, Req) of
@@ -519,16 +443,10 @@ cookies(Req=#http_req{cookies=undefined}) ->
cookies(Req=#http_req{cookies=Cookies}) ->
{Cookies, Req}.
-%% @equiv meta(Name, Req, undefined)
-spec meta(atom(), Req) -> {any() | undefined, Req} when Req::req().
meta(Name, Req) ->
meta(Name, Req, undefined).
-%% @doc Return metadata information about the request.
-%%
-%% Metadata information varies from one protocol to another. Websockets
-%% would define the protocol version here, while REST would use it to
-%% indicate which media type, language and charset were retained.
-spec meta(atom(), Req, any()) -> {any(), Req} when Req::req().
meta(Name, Req, Default) ->
case lists:keyfind(Name, 1, Req#http_req.meta) of
@@ -536,18 +454,12 @@ meta(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Set metadata information.
-%%
-%% You can use this function to attach information about the request.
-%%
-%% If the value already exists it will be overwritten.
-spec set_meta(atom(), any(), Req) -> Req when Req::req().
set_meta(Name, Value, Req=#http_req{meta=Meta}) ->
- Req#http_req{meta=[{Name, Value}|lists:keydelete(Name, 1, Meta)]}.
+ Req#http_req{meta=lists:keystore(Name, 1, Meta, {Name, Value})}.
%% Request Body API.
-%% @doc Return whether the request message has a body.
-spec has_body(req()) -> boolean().
has_body(Req) ->
case lists:keyfind(<<"content-length">>, 1, Req#http_req.headers) of
@@ -559,8 +471,6 @@ has_body(Req) ->
lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers)
end.
-%% @doc Return the request message body length, if known.
-%%
%% The length may not be known if Transfer-Encoding is not identity,
%% and the body hasn't been read at the time of the call.
-spec body_length(Req) -> {undefined | non_neg_integer(), Req} when Req::req().
@@ -573,12 +483,6 @@ body_length(Req) ->
{undefined, Req2}
end.
-%% @doc Initialize body streaming and set custom decoding functions.
-%%
-%% Calling this function is optional. It should only be used if you
-%% need to override the default behavior of Cowboy. Otherwise you
-%% should call stream_body/{1,2} directly.
-%%
%% Two decodings happen. First a decoding function is applied to the
%% transferred data, and then another is applied to the actual content.
%%
@@ -587,34 +491,17 @@ body_length(Req) ->
%% also initialized through this function.
%%
%% Content encoding is generally used for compression.
-%%
-%% Standard encodings can be found in cowboy_http.
-spec init_stream(transfer_decode_fun(), any(), content_decode_fun(), Req)
-> {ok, Req} when Req::req().
init_stream(TransferDecode, TransferState, ContentDecode, Req) ->
{ok, Req#http_req{body_state=
{stream, 0, TransferDecode, TransferState, ContentDecode}}}.
-%% @equiv stream_body(1000000, Req)
-spec stream_body(Req) -> {ok, binary(), Req}
| {done, Req} | {error, atom()} when Req::req().
stream_body(Req) ->
stream_body(1000000, Req).
-%% @doc Stream the request's body.
-%%
-%% This is the most low level function to read the request body.
-%%
-%% In most cases, if they weren't defined before using init_stream/4,
-%% this function will guess which transfer and content encodings were
-%% used for building the request body, and configure the decoding
-%% functions that will be used when streaming.
-%%
-%% It then starts streaming the body, returning {ok, Data, Req}
-%% for each streamed part, and {done, Req} when it's finished streaming.
-%%
-%% You can limit the size of the chunks being returned by using the
-%% first argument which is the size in bytes. It defaults to 1000000 bytes.
-spec stream_body(non_neg_integer(), Req) -> {ok, binary(), Req}
| {done, Req} | {error, atom()} when Req::req().
stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version,
@@ -632,7 +519,7 @@ stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version,
{ok, [<<"chunked">>], Req2} ->
stream_body(MaxLength, Req2#http_req{body_state=
{stream, 0,
- fun cowboy_http:te_chunked/2, {0, 0},
+ fun cow_http_te:stream_chunked/2, {0, 0},
fun cowboy_http:ce_identity/1}});
{ok, [<<"identity">>], Req2} ->
{Length, Req3} = body_length(Req2),
@@ -642,7 +529,7 @@ stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version,
Length ->
stream_body(MaxLength, Req3#http_req{body_state=
{stream, Length,
- fun cowboy_http:te_identity/2, {0, Length},
+ fun cow_http_te:stream_identity/2, {0, Length},
fun cowboy_http:ce_identity/1}})
end
end;
@@ -666,31 +553,34 @@ stream_body_recv(MaxLength, Req=#http_req{
{error, Reason} -> {error, Reason}
end.
+%% @todo Handle chunked after-the-facts headers.
+%% @todo Depending on the length returned we might want to 0 or +5 it.
-spec transfer_decode(binary(), Req)
-> {ok, binary(), Req} | {error, atom()} when Req::req().
transfer_decode(Data, Req=#http_req{body_state={stream, _,
TransferDecode, TransferState, ContentDecode}}) ->
case TransferDecode(Data, TransferState) of
- {ok, Data2, Rest, TransferState2} ->
- content_decode(ContentDecode, Data2,
- Req#http_req{buffer=Rest, body_state={stream, 0,
- TransferDecode, TransferState2, ContentDecode}});
- %% @todo {header(s) for chunked
more ->
stream_body_recv(0, Req#http_req{buffer=Data, body_state={stream,
0, TransferDecode, TransferState, ContentDecode}});
- {more, Length, Data2, TransferState2} ->
+ {more, Data2, TransferState2} ->
+ content_decode(ContentDecode, Data2,
+ Req#http_req{body_state={stream, 0,
+ TransferDecode, TransferState2, ContentDecode}});
+ {more, Data2, Length, TransferState2} when is_integer(Length) ->
content_decode(ContentDecode, Data2,
Req#http_req{body_state={stream, Length,
TransferDecode, TransferState2, ContentDecode}});
+ {more, Data2, Rest, TransferState2} ->
+ content_decode(ContentDecode, Data2,
+ Req#http_req{buffer=Rest, body_state={stream, 0,
+ TransferDecode, TransferState2, ContentDecode}});
{done, Length, Rest} ->
Req2 = transfer_decode_done(Length, Rest, Req),
{done, Req2};
{done, Data2, Length, Rest} ->
Req2 = transfer_decode_done(Length, Rest, Req),
- content_decode(ContentDecode, Data2, Req2);
- {error, Reason} ->
- {error, Reason}
+ content_decode(ContentDecode, Data2, Req2)
end.
-spec transfer_decode_done(non_neg_integer(), binary(), Req)
@@ -707,7 +597,6 @@ transfer_decode_done(Length, Rest, Req=#http_req{
Req#http_req{buffer=Rest, body_state=done,
headers=Headers3, p_headers=PHeaders3}.
-%% @todo Probably needs a Rest.
-spec content_decode(content_decode_fun(), binary(), Req)
-> {ok, binary(), Req} | {error, atom()} when Req::req().
content_decode(ContentDecode, Data, Req) ->
@@ -716,12 +605,10 @@ content_decode(ContentDecode, Data, Req) ->
{error, Reason} -> {error, Reason}
end.
-%% @equiv body(8000000, Req)
-spec body(Req) -> {ok, binary(), Req} | {error, atom()} when Req::req().
body(Req) ->
body(8000000, Req).
-%% @doc Return the body sent with the request.
-spec body(non_neg_integer() | infinity, Req)
-> {ok, binary(), Req} | {error, atom()} when Req::req().
body(MaxBodyLength, Req) ->
@@ -757,15 +644,12 @@ skip_body(Req) ->
{error, Reason} -> {error, Reason}
end.
-%% @equiv body_qs(16000, Req)
-spec body_qs(Req)
-> {ok, [{binary(), binary() | true}], Req} | {error, atom()}
when Req::req().
body_qs(Req) ->
body_qs(16000, Req).
-%% @doc Return the body sent with the request, parsed as an
-%% application/x-www-form-urlencoded string.
%% Essentially a POST query string.
-spec body_qs(non_neg_integer() | infinity, Req)
-> {ok, [{binary(), binary() | true}], Req} | {error, atom()}
@@ -780,7 +664,6 @@ body_qs(MaxBodyLength, Req) ->
%% Multipart API.
-%% @doc Return the next part's headers.
-spec part(Req)
-> {ok, cow_multipart:headers(), Req} | {done, Req}
when Req::req().
@@ -805,7 +688,6 @@ part(Buffer, Req=#http_req{multipart={Boundary, _}}) ->
{done, Req#http_req{multipart=undefined}}
end.
-%% @doc Return the current part's body.
-spec part_body(Req)
-> {ok, binary(), Req} | {more, binary(), Req}
when Req::req().
@@ -852,8 +734,6 @@ stream_multipart(Req=#http_req{multipart={Boundary, Buffer}}) ->
%% Response API.
-%% @doc Add a cookie header to the response.
-%%
%% The cookie name cannot contain any of the following characters:
%% =,;\s\t\r\n\013\014
%%
@@ -865,46 +745,19 @@ set_resp_cookie(Name, Value, Opts, Req) ->
Cookie = cow_cookie:setcookie(Name, Value, Opts),
set_resp_header(<<"set-cookie">>, Cookie, Req).
-%% @doc Add a header to the response.
-spec set_resp_header(binary(), iodata(), Req)
-> Req when Req::req().
set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) ->
Req#http_req{resp_headers=[{Name, Value}|RespHeaders]}.
-%% @doc Add a body to the response.
-%%
-%% The body set here is ignored if the response is later sent using
-%% anything other than reply/2 or reply/3. The response body is expected
-%% to be a binary or an iolist.
-spec set_resp_body(iodata(), Req) -> Req when Req::req().
set_resp_body(Body, Req) ->
Req#http_req{resp_body=Body}.
-%% @doc Add a body stream function to the response.
-%%
-%% The body set here is ignored if the response is later sent using
-%% anything other than reply/2 or reply/3.
-%%
-%% Setting a response stream function without a length means that the
-%% body will be sent until the connection is closed. Cowboy will make
-%% sure that the connection is closed with no extra step required.
-%%
-%% To inform the client that a body has been sent with this request,
-%% Cowboy will add a "Transfer-Encoding: identity" header to the
-%% response.
-spec set_resp_body_fun(resp_body_fun(), Req) -> Req when Req::req().
set_resp_body_fun(StreamFun, Req) when is_function(StreamFun) ->
Req#http_req{resp_body=StreamFun}.
-%% @doc Add a body function to the response.
-%%
-%% The body set here is ignored if the response is later sent using
-%% anything other than reply/2 or reply/3.
-%%
-%% Cowboy will call the given response stream function after sending the
-%% headers. This function must send the specified number of bytes to the
-%% socket it will receive as argument.
-%%
%% If the body function crashes while writing the response body or writes
%% fewer bytes than declared the behaviour is undefined.
-spec set_resp_body_fun(non_neg_integer(), resp_body_fun(), Req)
@@ -918,12 +771,10 @@ set_resp_body_fun(chunked, StreamFun, Req)
when is_function(StreamFun) ->
Req#http_req{resp_body={chunked, StreamFun}}.
-%% @doc Return whether the given header has been set for the response.
-spec has_resp_header(binary(), req()) -> boolean().
has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
lists:keymember(Name, 1, RespHeaders).
-%% @doc Return whether a body has been set for the response.
-spec has_resp_body(req()) -> boolean().
has_resp_body(#http_req{resp_body=RespBody}) when is_function(RespBody) ->
true;
@@ -934,25 +785,21 @@ has_resp_body(#http_req{resp_body={Length, _}}) ->
has_resp_body(#http_req{resp_body=RespBody}) ->
iolist_size(RespBody) > 0.
-%% @doc Remove a header previously set for the response.
-spec delete_resp_header(binary(), Req)
-> Req when Req::req().
delete_resp_header(Name, Req=#http_req{resp_headers=RespHeaders}) ->
RespHeaders2 = lists:keydelete(Name, 1, RespHeaders),
Req#http_req{resp_headers=RespHeaders2}.
-%% @equiv reply(Status, [], [], Req)
-spec reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req().
reply(Status, Req=#http_req{resp_body=Body}) ->
reply(Status, [], Body, Req).
-%% @equiv reply(Status, Headers, [], Req)
-spec reply(cowboy:http_status(), cowboy:http_headers(), Req)
-> {ok, Req} when Req::req().
reply(Status, Headers, Req=#http_req{resp_body=Body}) ->
reply(Status, Headers, Body, Req).
-%% @doc Send a reply to the client.
-spec reply(cowboy:http_status(), cowboy:http_headers(),
iodata() | {non_neg_integer() | resp_body_fun()}, Req)
-> {ok, Req} when Req::req().
@@ -1080,22 +927,16 @@ reply_no_compress(Status, Headers, Body, Req,
Req),
Req2.
-%% @equiv chunked_reply(Status, [], Req)
-spec chunked_reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req().
chunked_reply(Status, Req) ->
chunked_reply(Status, [], Req).
-%% @doc Initiate the sending of a chunked reply to the client.
-%% @see cowboy_req:chunk/2
-spec chunked_reply(cowboy:http_status(), cowboy:http_headers(), Req)
-> {ok, Req} when Req::req().
chunked_reply(Status, Headers, Req) ->
{_, Req2} = chunked_response(Status, Headers, Req),
{ok, Req2}.
-%% @doc Send a chunk of data.
-%%
-%% A chunked reply must have been initiated before calling this function.
-spec chunk(iodata(), req()) -> ok | {error, atom()}.
chunk(_Data, #http_req{method= <<"HEAD">>}) ->
ok;
@@ -1110,8 +951,7 @@ chunk(Data, #http_req{socket=Socket, transport=Transport,
Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
<<"\r\n">>, Data, <<"\r\n">>]).
-%% @doc Finish the chunked reply.
-%% @todo If ever made public, need to send nothing if HEAD.
+%% If ever made public, need to send nothing if HEAD.
-spec last_chunk(Req) -> Req when Req::req().
last_chunk(Req=#http_req{socket=Socket, transport=cowboy_spdy}) ->
_ = cowboy_spdy:stream_close(Socket),
@@ -1120,8 +960,6 @@ last_chunk(Req=#http_req{socket=Socket, transport=Transport}) ->
_ = Transport:send(Socket, <<"0\r\n\r\n">>),
Req#http_req{resp_state=done}.
-%% @doc Send an upgrade reply.
-%% @private
-spec upgrade_reply(cowboy:http_status(), cowboy:http_headers(), Req)
-> {ok, Req} when Req::req().
upgrade_reply(Status, Headers, Req=#http_req{transport=Transport,
@@ -1132,10 +970,7 @@ upgrade_reply(Status, Headers, Req=#http_req{transport=Transport,
], <<>>, Req),
{ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
-%% @doc Send a reply if one hasn't been sent already.
-%%
%% Meant to be used internally for sending errors after crashes.
-%% @private
-spec maybe_reply(cowboy:http_status(), req()) -> ok.
maybe_reply(Status, Req) ->
receive
@@ -1145,8 +980,6 @@ maybe_reply(Status, Req) ->
ok
end.
-%% @doc Ensure the response has been sent fully.
-%% @private
-spec ensure_response(req(), cowboy:http_status()) -> ok.
%% The response has already been fully sent to the client.
ensure_response(#http_req{resp_state=done}, _) ->
@@ -1168,12 +1001,10 @@ ensure_response(#http_req{}, _) ->
%% Private setter/getter API.
-%% @private
-spec append_buffer(binary(), Req) -> Req when Req::req().
append_buffer(Suffix, Req=#http_req{buffer=Buffer}) ->
Req#http_req{buffer= << Buffer/binary, Suffix/binary >>}.
-%% @private
-spec get(atom(), req()) -> any(); ([atom()], req()) -> any().
get(List, Req) when is_list(List) ->
[g(Atom, Req) || Atom <- List];
@@ -1208,7 +1039,6 @@ g(socket, #http_req{socket=Ret}) -> Ret;
g(transport, #http_req{transport=Ret}) -> Ret;
g(version, #http_req{version=Ret}) -> Ret.
-%% @private
-spec set([{atom(), any()}], Req) -> Req when Req::req().
set([], Req) -> Req;
set([{bindings, Val}|Tail], Req) -> set(Tail, Req#http_req{bindings=Val});
@@ -1238,7 +1068,6 @@ set([{socket, Val}|Tail], Req) -> set(Tail, Req#http_req{socket=Val});
set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val});
set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}).
-%% @private
-spec set_bindings(cowboy_router:tokens(), cowboy_router:tokens(),
cowboy_router:bindings(), Req) -> Req when Req::req().
set_bindings(HostInfo, PathInfo, Bindings, Req) ->
@@ -1247,13 +1076,6 @@ set_bindings(HostInfo, PathInfo, Bindings, Req) ->
%% Misc API.
-%% @doc Compact the request data by removing all non-system information.
-%%
-%% This essentially removes the host and path info, query string, bindings,
-%% headers and cookies.
-%%
-%% Use it when you really need to save up memory, for example when having
-%% many concurrent long-running connections.
-spec compact(Req) -> Req when Req::req().
compact(Req) ->
Req#http_req{host_info=undefined,
@@ -1261,13 +1083,10 @@ compact(Req) ->
bindings=undefined, headers=[],
p_headers=[], cookies=[]}.
-%% @doc Prevent any further responses.
-%% @private
-spec lock(Req) -> Req when Req::req().
lock(Req) ->
Req#http_req{resp_state=locked}.
-%% @doc Convert the Req object to a list of key/values.
-spec to_list(req()) -> [{atom(), any()}].
to_list(Req) ->
lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))).
@@ -1360,7 +1179,7 @@ response_connection([], Connection) ->
response_connection([{Name, Value}|Tail], Connection) ->
case Name of
<<"connection">> ->
- Tokens = parse_connection_before(Value, []),
+ Tokens = cow_http_hd:parse_connection(Value),
connection_to_atom(Tokens);
_ ->
response_connection(Tail, Connection)
@@ -1402,57 +1221,6 @@ atom_to_connection(keepalive) ->
atom_to_connection(close) ->
<<"close">>.
-%% Optimized parsing functions for the Connection header.
-parse_connection_before(<<>>, Acc) ->
- lists:reverse(Acc);
-parse_connection_before(<< C, Rest/bits >>, Acc)
- when C =:= $,; C =:= $\s; C =:= $\t ->
- parse_connection_before(Rest, Acc);
-parse_connection_before(Buffer, Acc) ->
- parse_connection(Buffer, Acc, <<>>).
-
-%% An evil block of code appeared!
-parse_connection(<<>>, Acc, <<>>) ->
- lists:reverse(Acc);
-parse_connection(<<>>, Acc, Token) ->
- lists:reverse([Token|Acc]);
-parse_connection(<< C, Rest/bits >>, Acc, Token)
- when C =:= $,; C =:= $\s; C =:= $\t ->
- parse_connection_before(Rest, [Token|Acc]);
-parse_connection(<< C, Rest/bits >>, Acc, Token) ->
- case C of
- $A -> parse_connection(Rest, Acc, << Token/binary, $a >>);
- $B -> parse_connection(Rest, Acc, << Token/binary, $b >>);
- $C -> parse_connection(Rest, Acc, << Token/binary, $c >>);
- $D -> parse_connection(Rest, Acc, << Token/binary, $d >>);
- $E -> parse_connection(Rest, Acc, << Token/binary, $e >>);
- $F -> parse_connection(Rest, Acc, << Token/binary, $f >>);
- $G -> parse_connection(Rest, Acc, << Token/binary, $g >>);
- $H -> parse_connection(Rest, Acc, << Token/binary, $h >>);
- $I -> parse_connection(Rest, Acc, << Token/binary, $i >>);
- $J -> parse_connection(Rest, Acc, << Token/binary, $j >>);
- $K -> parse_connection(Rest, Acc, << Token/binary, $k >>);
- $L -> parse_connection(Rest, Acc, << Token/binary, $l >>);
- $M -> parse_connection(Rest, Acc, << Token/binary, $m >>);
- $N -> parse_connection(Rest, Acc, << Token/binary, $n >>);
- $O -> parse_connection(Rest, Acc, << Token/binary, $o >>);
- $P -> parse_connection(Rest, Acc, << Token/binary, $p >>);
- $Q -> parse_connection(Rest, Acc, << Token/binary, $q >>);
- $R -> parse_connection(Rest, Acc, << Token/binary, $r >>);
- $S -> parse_connection(Rest, Acc, << Token/binary, $s >>);
- $T -> parse_connection(Rest, Acc, << Token/binary, $t >>);
- $U -> parse_connection(Rest, Acc, << Token/binary, $u >>);
- $V -> parse_connection(Rest, Acc, << Token/binary, $v >>);
- $W -> parse_connection(Rest, Acc, << Token/binary, $w >>);
- $X -> parse_connection(Rest, Acc, << Token/binary, $x >>);
- $Y -> parse_connection(Rest, Acc, << Token/binary, $y >>);
- $Z -> parse_connection(Rest, Acc, << Token/binary, $z >>);
- C -> parse_connection(Rest, Acc, << Token/binary, C >>)
- end.
-
-%% @doc Walk through a tokens list and return whether
-%% the connection is keepalive or closed.
-%%
%% We don't match on "keep-alive" since it is the default value.
-spec connection_to_atom([binary()]) -> keepalive | close.
connection_to_atom([]) ->
@@ -1525,7 +1293,6 @@ status(B) when is_binary(B) -> B.
%% Tests.
-ifdef(TEST).
-
url_test() ->
{undefined, _} =
url(#http_req{transport=ranch_tcp, host= <<>>, port= undefined,
@@ -1553,19 +1320,7 @@ url_test() ->
path= <<"/path">>, qs= <<"dummy=2785">>, pid=self()}),
ok.
-parse_connection_test_() ->
- %% {Binary, Result}
- Tests = [
- {<<"close">>, [<<"close">>]},
- {<<"ClOsE">>, [<<"close">>]},
- {<<"Keep-Alive">>, [<<"keep-alive">>]},
- {<<"keep-alive, Upgrade">>, [<<"keep-alive">>, <<"upgrade">>]}
- ],
- [{B, fun() -> R = parse_connection_before(B, []) end}
- || {B, R} <- Tests].
-
connection_to_atom_test_() ->
- %% {Tokens, Result}
Tests = [
{[<<"close">>], close},
{[<<"keep-alive">>], keepalive},
@@ -1589,5 +1344,4 @@ merge_headers_test_() ->
{<<"server">>,<<"Cowboy">>}]}
],
[fun() -> Res = merge_headers(L,R) end || {L, R, Res} <- Tests].
-
-endif.
diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl
index 3eac8dd..63e4dd9 100644
--- a/src/cowboy_rest.erl
+++ b/src/cowboy_rest.erl
@@ -12,8 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc REST protocol implementation.
-%%
%% Originally based on the Webmachine Diagram from Alan Dean and
%% Justin Sheehy.
-module(cowboy_rest).
@@ -57,11 +55,6 @@
expires :: undefined | no_call | calendar:datetime()
}).
-%% @doc Upgrade a HTTP request to the REST protocol.
-%%
-%% You do not need to call this function manually. To upgrade to the REST
-%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em>
-%% in your <em>cowboy_http_handler:init/3</em> handler function.
-spec upgrade(Req, Env, module(), any())
-> {ok, Req, Env} | {error, 500, Req}
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index 7c49943..ef91c6d 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -12,7 +12,7 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Routing middleware.
+%% Routing middleware.
%%
%% Resolve the handler to be used for the request based on the
%% routing information found in the <em>dispatch</em> environment value.
@@ -51,8 +51,6 @@
-opaque dispatch_rules() :: [dispatch_rule()].
-export_type([dispatch_rules/0]).
-%% @doc Compile a list of routes into the dispatch format used
-%% by Cowboy's routing.
-spec compile(routes()) -> dispatch_rules().
compile(Routes) ->
compile(Routes, []).
@@ -162,7 +160,6 @@ compile_brackets_split(<< $], Rest/binary >>, Acc, 0) ->
compile_brackets_split(<< C, Rest/binary >>, Acc, N) ->
compile_brackets_split(Rest, << Acc/binary, C >>, N).
-%% @private
-spec execute(Req, Env)
-> {ok, Req, Env} | {error, 400 | 404, Req}
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
@@ -183,7 +180,7 @@ execute(Req, Env) ->
%% Internal.
-%% @doc Match hostname tokens and path tokens against dispatch rules.
+%% Match hostname tokens and path tokens against dispatch rules.
%%
%% It is typically used for matching tokens for the hostname and path of
%% the request against a global dispatch rule for your listener.
@@ -300,7 +297,6 @@ check_constraint({_, int}, Value) ->
check_constraint({_, function, Fun}, Value) ->
Fun(Value).
-%% @doc Split a hostname into a list of tokens.
-spec split_host(binary()) -> tokens().
split_host(Host) ->
split_host(Host, []).
@@ -317,8 +313,6 @@ split_host(Host, Acc) ->
split_host(Rest, [Segment|Acc])
end.
-%% @doc Split a path into a list of path segments.
-%%
%% Following RFC2396, this function may return path segments containing any
%% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
%% and part of a path segment.
@@ -332,9 +326,9 @@ split_path(Path, Acc) ->
try
case binary:match(Path, <<"/">>) of
nomatch when Path =:= <<>> ->
- lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
+ lists:reverse([cow_qs:urldecode(S) || S <- Acc]);
nomatch ->
- lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
+ lists:reverse([cow_qs:urldecode(S) || S <- [Path|Acc]]);
{Pos, _} ->
<< Segment:Pos/binary, _:8, Rest/bits >> = Path,
split_path(Rest, [Segment|Acc])
@@ -376,9 +370,7 @@ list_match(_List, _Match, _Binds) ->
%% Tests.
-ifdef(TEST).
-
compile_test_() ->
- %% {Routes, Result}
Tests = [
%% Match any host and path.
{[{'_', [{'_', h, o}]}],
@@ -390,14 +382,22 @@ compile_test_() ->
{[<<"path">>, <<"to">>, <<"resource">>], [], hb, ob}]}]},
{[{'_', [{"/path/to/resource/", h, o}]}],
[{'_', [], [{[<<"path">>, <<"to">>, <<"resource">>], [], h, o}]}]},
- {[{'_', [{"/путь/к/ресурсу/", h, o}]}],
- [{'_', [], [{[<<"путь">>, <<"к">>, <<"ресурсу">>], [], h, o}]}]},
+ % Cyrillic from a latin1 encoded file.
+ {[{'_', [{[47,208,191,209,131,209,130,209,140,47,208,186,47,209,128,
+ 208,181,209,129,209,131,209,128,209,129,209,131,47], h, o}]}],
+ [{'_', [], [{[<<208,191,209,131,209,130,209,140>>, <<208,186>>,
+ <<209,128,208,181,209,129,209,131,209,128,209,129,209,131>>],
+ [], h, o}]}]},
{[{"cowboy.example.org.", [{'_', h, o}]}],
[{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]},
{[{".cowboy.example.org", [{'_', h, o}]}],
[{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]},
- {[{"некий.сайт.рф.", [{'_', h, o}]}],
- [{[<<"рф">>, <<"сайт">>, <<"некий">>], [], [{'_', [], h, o}]}]},
+ % Cyrillic from a latin1 encoded file.
+ {[{[208,189,208,181,208,186,208,184,208,185,46,209,129,208,176,
+ 208,185,209,130,46,209,128,209,132,46], [{'_', h, o}]}],
+ [{[<<209,128,209,132>>, <<209,129,208,176,208,185,209,130>>,
+ <<208,189,208,181,208,186,208,184,208,185>>],
+ [], [{'_', [], h, o}]}]},
{[{":subdomain.example.org", [{"/hats/:name/prices", h, o}]}],
[{[<<"org">>, <<"example">>, subdomain], [], [
{[<<"hats">>, name, <<"prices">>], [], h, o}]}]},
@@ -425,7 +425,6 @@ compile_test_() ->
fun() -> Rs = compile(Rt) end} || {Rt, Rs} <- Tests].
split_host_test_() ->
- %% {Host, Result}
Tests = [
{<<"">>, []},
{<<"*">>, [<<"*">>]},
@@ -442,7 +441,6 @@ split_host_test_() ->
[{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
split_path_test_() ->
- %% {Path, Result, QueryString}
Tests = [
{<<"/">>, []},
{<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>]},
@@ -473,7 +471,6 @@ match_test_() ->
{'_', [], match_any, []}
]}
],
- %% {Host, Path, Result}
Tests = [
{<<"any">>, <<"/">>, {ok, match_any, [], []}},
{<<"www.any.ninenines.eu">>, <<"/users/42/mails">>,
@@ -506,8 +503,9 @@ match_info_test_() ->
{[<<"eu">>, <<"ninenines">>, '...'], [], [
{'_', [], match_any, []}
]},
- {[<<"рф">>, <<"сайт">>], [], [
- {[<<"путь">>, '...'], [], match_path, []}
+ % Cyrillic from a latin1 encoded file.
+ {[<<209,128,209,132>>, <<209,129,208,176,208,185,209,130>>], [], [
+ {[<<208,191,209,131,209,130,209,140>>, '...'], [], match_path, []}
]}
],
Tests = [
@@ -523,8 +521,10 @@ match_info_test_() ->
{ok, match_path, [], [], undefined, [<<"path_info">>]}},
{<<"www.ninenines.eu">>, <<"/pathinfo/is/next/foo/bar">>,
{ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}},
- {<<"сайт.рф">>, <<"/путь/домой">>,
- {ok, match_path, [], [], undefined, [<<"домой">>]}}
+ % Cyrillic from a latin1 encoded file.
+ {<<209,129,208,176,208,185,209,130,46,209,128,209,132>>,
+ <<47,208,191,209,131,209,130,209,140,47,208,180,208,190,208,188,208,190,208,185>>,
+ {ok, match_path, [], [], undefined, [<<208,180,208,190,208,188,208,190,208,185>>]}}
],
[{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
R = match(Dispatch, H, P)
@@ -569,5 +569,4 @@ match_same_bindings_test() ->
{error, notfound, path} = match(Dispatch3,
<<"ninenines.eu">>, <<"/path/to">>),
ok.
-
-endif.
diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl
index 7deb3b8..e5aeb21 100644
--- a/src/cowboy_spdy.erl
+++ b/src/cowboy_spdy.erl
@@ -12,10 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc SPDY protocol handler.
-%%
-%% Note that there is no need to monitor these processes when using Cowboy as
-%% an application as it already supervises them under the listener supervisor.
-module(cowboy_spdy).
%% API.
@@ -41,8 +37,11 @@
-export([send/2]).
-export([sendfile/2]).
+-type streamid() :: non_neg_integer().
+-type socket() :: {pid(), streamid()}.
+
-record(child, {
- streamid :: non_neg_integer(),
+ streamid :: streamid(),
pid :: pid(),
input = nofin :: fin | nofin,
in_buffer = <<>> :: binary(),
@@ -63,7 +62,7 @@
peer,
zdef,
zinf,
- last_streamid = 0 :: non_neg_integer(),
+ last_streamid = 0 :: streamid(),
children = [] :: [#child{}]
}).
@@ -75,7 +74,6 @@
%% API.
-%% @doc Start a SPDY protocol process.
-spec start_link(any(), inet:socket(), module(), any()) -> {ok, pid()}.
start_link(Ref, Socket, Transport, Opts) ->
proc_lib:start_link(?MODULE, init,
@@ -83,15 +81,13 @@ start_link(Ref, Socket, Transport, Opts) ->
%% Internal.
-%% @doc Faster alternative to proplists:get_value/3.
-%% @private
+%% Faster alternative to proplists:get_value/3.
get_value(Key, Opts, Default) ->
case lists:keyfind(Key, 1, Opts) of
{_, Value} -> Value;
_ -> Default
end.
-%% @private
-spec init(pid(), ranch:ref(), inet:socket(), module(), opts()) -> ok.
init(Parent, Ref, Socket, Transport, Opts) ->
process_flag(trap_exit, true),
@@ -209,6 +205,7 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
terminate(State)
end.
+-spec system_continue(_, _, #state{}) -> ok.
system_continue(_, _, State) ->
loop(State).
@@ -216,6 +213,7 @@ system_continue(_, _, State) ->
system_terminate(Reason, _, _, _) ->
exit(Reason).
+-spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::#state{}.
system_code_change(Misc, _, _, _) ->
{ok, Misc}.
@@ -358,6 +356,11 @@ delete_child(Pid, State=#state{children=Children}) ->
%% Request process.
+-spec request_init(socket(), {inet:ip_address(), inet:port_number()},
+ cowboy:onrequest_fun(), cowboy:onresponse_fun(),
+ cowboy_middleware:env(), [module()],
+ binary(), binary(), binary(), binary(), [{binary(), binary()}])
+ -> ok.
request_init(FakeSocket, Peer, OnRequest, OnResponse,
Env, Middlewares, Method, Host, Path, Version, Headers) ->
{Host2, Port} = cow_http:parse_fullhost(Host),
@@ -394,7 +397,6 @@ execute(Req, Env, [Middleware|Tail]) ->
cowboy_req:maybe_reply(Status, Req2)
end.
-%% @private
-spec resume(cowboy_middleware:env(), [module()],
module(), module(), [any()]) -> ok.
resume(Env, Tail, Module, Function, Args) ->
@@ -412,6 +414,7 @@ resume(Env, Tail, Module, Function, Args) ->
%% Reply functions used by cowboy_req.
+-spec reply(socket(), binary(), cowboy:http_headers(), iodata()) -> ok.
reply(Socket = {Pid, _}, Status, Headers, Body) ->
_ = case iolist_size(Body) of
0 -> Pid ! {reply, Socket, Status, Headers};
@@ -419,23 +422,29 @@ reply(Socket = {Pid, _}, Status, Headers, Body) ->
end,
ok.
+-spec stream_reply(socket(), binary(), cowboy:http_headers()) -> ok.
stream_reply(Socket = {Pid, _}, Status, Headers) ->
_ = Pid ! {stream_reply, Socket, Status, Headers},
ok.
+-spec stream_data(socket(), iodata()) -> ok.
stream_data(Socket = {Pid, _}, Data) ->
_ = Pid ! {stream_data, Socket, Data},
ok.
+-spec stream_close(socket()) -> ok.
stream_close(Socket = {Pid, _}) ->
_ = Pid ! {stream_close, Socket},
ok.
%% Internal transport functions.
+-spec name() -> spdy.
name() ->
spdy.
+-spec recv(socket(), non_neg_integer(), timeout())
+ -> {ok, binary()} | {error, timeout}.
recv(Socket = {Pid, _}, Length, Timeout) ->
_ = Pid ! {recv, Socket, self(), Length, Timeout},
receive
@@ -443,12 +452,14 @@ recv(Socket = {Pid, _}, Length, Timeout) ->
Ret
end.
+-spec send(socket(), iodata()) -> ok.
send(Socket, Data) ->
stream_data(Socket, Data).
%% We don't wait for the result of the actual sendfile call,
%% therefore we can't know how much was actually sent.
%% This isn't a problem as we don't use this value in Cowboy.
+-spec sendfile(socket(), file:name_all()) -> {ok, undefined}.
sendfile(Socket = {Pid, _}, Filepath) ->
_ = Pid ! {sendfile, Socket, Filepath},
{ok, undefined}.
diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl
index 4bf84da..d10ee87 100644
--- a/src/cowboy_static.erl
+++ b/src/cowboy_static.erl
@@ -39,10 +39,11 @@
-type state() :: {binary(), {ok, #file_info{}} | {error, atom()}, extra()}.
+-spec init(_, _, _) -> {upgrade, protocol, cowboy_rest}.
init(_, _, _) ->
{upgrade, protocol, cowboy_rest}.
-%% @doc Resolve the file that will be sent and get its file information.
+%% Resolve the file that will be sent and get its file information.
%% If the handler is configured to manage a directory, check that the
%% requested file is inside the configured directory.
@@ -197,7 +198,7 @@ bad_path_win32_check_test_() ->
end} || P <- Tests].
-endif.
-%% @doc Reject requests that tried to access a file outside
+%% Reject requests that tried to access a file outside
%% the target directory.
-spec malformed_request(Req, State)
@@ -205,7 +206,7 @@ bad_path_win32_check_test_() ->
malformed_request(Req, State) ->
{State =:= error, Req, State}.
-%% @doc Directories, files that can't be accessed at all and
+%% Directories, files that can't be accessed at all and
%% files with no read flag are forbidden.
-spec forbidden(Req, State)
@@ -221,7 +222,7 @@ forbidden(Req, State={_, {ok, #file_info{access=Access}}, _})
forbidden(Req, State) ->
{false, Req, State}.
-%% @doc Detect the mimetype of the file.
+%% Detect the mimetype of the file.
-spec content_types_provided(Req, State)
-> {[{binary(), get_file}], Req, State}
@@ -236,7 +237,7 @@ content_types_provided(Req, State={Path, _, Extra}) ->
{[{Type, get_file}], Req, State}
end.
-%% @doc Assume the resource doesn't exist if it's not a regular file.
+%% Assume the resource doesn't exist if it's not a regular file.
-spec resource_exists(Req, State)
-> {boolean(), Req, State}
@@ -246,7 +247,7 @@ resource_exists(Req, State={_, {ok, #file_info{type=regular}}, _}) ->
resource_exists(Req, State) ->
{false, Req, State}.
-%% @doc Generate an etag for the file.
+%% Generate an etag for the file.
-spec generate_etag(Req, State)
-> {{strong | weak, binary()}, Req, State}
@@ -266,7 +267,7 @@ generate_default_etag(Size, Mtime) ->
{strong, list_to_binary(integer_to_list(
erlang:phash2({Size, Mtime}, 16#ffffffff)))}.
-%% @doc Return the time of last modification of the file.
+%% Return the time of last modification of the file.
-spec last_modified(Req, State)
-> {calendar:datetime(), Req, State}
@@ -274,7 +275,7 @@ generate_default_etag(Size, Mtime) ->
last_modified(Req, State={_, {ok, #file_info{mtime=Modified}}, _}) ->
{Modified, Req, State}.
-%% @doc Stream the file.
+%% Stream the file.
%% @todo Export cowboy_req:resp_body_fun()?
-spec get_file(Req, State)
diff --git a/src/cowboy_sub_protocol.erl b/src/cowboy_sub_protocol.erl
index ee4701c..713c3cd 100644
--- a/src/cowboy_sub_protocol.erl
+++ b/src/cowboy_sub_protocol.erl
@@ -13,21 +13,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for sub protocols.
-%%
-%% Only one function needs to be implemented, <em>upgrade/4</em>.
-%% It receives the Req, the environment, the handler that the request has been
-%% routed to and the handler's options. It acts exactly the same as a
-%% middleware, so returns the same values a middleware's execute/2.
-%%
-%% Once the sub protocol has processed the request it should add the result
-%% to the environment. This is done by adding the tuple {result, Value} to the
-%% environment list. To continue handling requests on the current connection the
-%% Value should be the atom ok. Any other value will prevent the processing of
-%% subsequent requests.
-%%
-%% <em>upgrade/4</em> will be called when a handler's init/3 returns
-%% {upgrade, protocol, Module}, where Module is the module of the sub protocol.
-module(cowboy_sub_protocol).
-callback upgrade(Req, Env, module(), any())
diff --git a/src/cowboy_sup.erl b/src/cowboy_sup.erl
index 21e1810..cf48595 100644
--- a/src/cowboy_sup.erl
+++ b/src/cowboy_sup.erl
@@ -12,26 +12,18 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @private
-module(cowboy_sup).
-behaviour(supervisor).
-%% API.
-export([start_link/0]).
-
-%% supervisor.
-export([init/1]).
--define(SUPERVISOR, ?MODULE).
-
-%% API.
-
-spec start_link() -> {ok, pid()}.
start_link() ->
- supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []).
-
-%% supervisor.
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+-spec init([])
+ -> {ok, {{supervisor:strategy(), 10, 10}, [supervisor:child_spec()]}}.
init([]) ->
Procs = [{cowboy_clock, {cowboy_clock, start_link, []},
permanent, 5000, worker, [cowboy_clock]}],
diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl
index 6220a74..98e25e6 100644
--- a/src/cowboy_websocket.erl
+++ b/src/cowboy_websocket.erl
@@ -12,8 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Websocket protocol implementation.
-%%
%% Cowboy supports versions 7 through 17 of the Websocket drafts.
%% It also supports RFC6455, the proposed standard for Websocket.
-module(cowboy_websocket).
@@ -23,10 +21,7 @@
%% @todo Remove when we support only R16B+.
-compile(nowarn_deprecated_function).
-%% API.
-export([upgrade/4]).
-
-%% Internal.
-export([handler_loop/4]).
-type close_code() :: 1000..4999.
@@ -42,6 +37,8 @@
-type frag_state() :: undefined
| {nofin, opcode(), binary()} | {fin, opcode(), binary()}.
-type rsv() :: << _:3 >>.
+-type terminate_reason() :: {normal | error | remote, atom()}
+ | {remote, close_code(), binary()}.
-record(state, {
env :: cowboy_middleware:env(),
@@ -60,11 +57,6 @@
deflate_state :: undefined | port()
}).
-%% @doc Upgrade an HTTP request to the Websocket protocol.
-%%
-%% You do not need to call this function manually. To upgrade to the Websocket
-%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em>
-%% in your <em>cowboy_http_handler:init/3</em> handler function.
-spec upgrade(Req, Env, module(), any())
-> {ok, Req, Env} | {error, 400, Req}
| {suspend, module(), atom(), [any()]}
@@ -213,7 +205,6 @@ handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) ->
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
State#state{timeout_ref=TRef}.
-%% @private
-spec handler_loop(#state{}, Req, any(), binary())
-> {ok, Req, cowboy_middleware:env()}
| {suspend, module(), atom(), [any()]}
@@ -730,8 +721,7 @@ websocket_send_many([Frame|Tail], State) ->
{Error, State2} -> {Error, State2}
end.
--spec websocket_close(#state{}, Req, any(),
- {atom(), atom()} | {remote, close_code(), binary()})
+-spec websocket_close(#state{}, Req, any(), terminate_reason())
-> {ok, Req, cowboy_middleware:env()}
when Req::cowboy_req:req().
websocket_close(State=#state{socket=Socket, transport=Transport},
@@ -752,7 +742,7 @@ websocket_close(State=#state{socket=Socket, transport=Transport},
end,
handler_terminate(State, Req, HandlerState, Reason).
--spec handler_terminate(#state{}, Req, any(), atom() | {atom(), atom()})
+-spec handler_terminate(#state{}, Req, any(), terminate_reason())
-> {ok, Req, cowboy_middleware:env()}
when Req::cowboy_req:req().
handler_terminate(#state{env=Env, handler=Handler},
diff --git a/src/cowboy_websocket_handler.erl b/src/cowboy_websocket_handler.erl
index d6adfa5..177e5f6 100644
--- a/src/cowboy_websocket_handler.erl
+++ b/src/cowboy_websocket_handler.erl
@@ -12,40 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Handler for HTTP WebSocket requests.
-%%
-%% WebSocket handlers must implement five callbacks: <em>init/3</em>,
-%% <em>websocket_init/3</em>, <em>websocket_handle/3</em>,
-%% <em>websocket_info/3</em> and <em>websocket_terminate/3</em>.
-%% These callbacks will only be called if the connection is upgraded
-%% to WebSocket in the HTTP handler's <em>init/3</em> callback.
-%% They are then called in that order, although <em>websocket_handle/3</em>
-%% will be called for each packet received, and <em>websocket_info</em>
-%% for each message received.
-%%
-%% <em>websocket_init/3</em> is meant for initialization. It receives
-%% information about the transport and protocol used, along with the handler
-%% options from the dispatch list. You can define a request-wide state here.
-%% If you are going to want to compact the request, you should probably do it
-%% here.
-%%
-%% <em>websocket_handle/3</em> receives the data from the socket. It can reply
-%% something, do nothing or close the connection.
-%%
-%% <em>websocket_info/3</em> receives messages sent to the process. It has
-%% the same reply format as <em>websocket_handle/3</em> described above. Note
-%% that unlike in a <em>gen_server</em>, when <em>websocket_info/3</em>
-%% replies something, it is always to the socket, not to the process that
-%% originated the message.
-%%
-%% <em>websocket_terminate/3</em> is meant for cleaning up. It also receives
-%% the request and the state previously defined, along with a reason for
-%% termination.
-%%
-%% All of <em>websocket_init/3</em>, <em>websocket_handle/3</em> and
-%% <em>websocket_info/3</em> can decide to hibernate the process by adding
-%% an extra element to the returned tuple, containing the atom
-%% <em>hibernate</em>. Doing so helps save memory and improve CPU usage.
-module(cowboy_websocket_handler).
-type opts() :: any().
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 20c65c8..4b93c67 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -14,93 +14,11 @@
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-module(http_SUITE).
+-compile(export_all).
-include_lib("common_test/include/ct.hrl").
%% ct.
--export([all/0]).
--export([groups/0]).
--export([init_per_suite/1]).
--export([end_per_suite/1]).
--export([init_per_group/2]).
--export([end_per_group/2]).
-
-%% Callbacks.
--export([etag_gen/3]).
--export([mimetypes_text_html/1]).
-
-%% Tests.
--export([check_raw_status/1]).
--export([check_status/1]).
--export([chunked_response/1]).
--export([echo_body/1]).
--export([echo_body_max_length/1]).
--export([echo_body_qs/1]).
--export([echo_body_qs_max_length/1]).
--export([error_chain_handle_after_reply/1]).
--export([error_chain_handle_before_reply/1]).
--export([error_handle_after_reply/1]).
--export([error_init_after_reply/1]).
--export([error_init_reply_handle_error/1]).
--export([headers_dupe/1]).
--export([http10_chunkless/1]).
--export([http10_hostless/1]).
--export([keepalive_max/1]).
--export([keepalive_nl/1]).
--export([keepalive_stream_loop/1]).
--export([multipart/1]).
--export([multipart_large/1]).
--export([nc_rand/1]).
--export([nc_zero/1]).
--export([onrequest/1]).
--export([onrequest_reply/1]).
--export([onresponse_capitalize/1]).
--export([onresponse_crash/1]).
--export([onresponse_reply/1]).
--export([parse_host/1]).
--export([pipeline/1]).
--export([pipeline_long_polling/1]).
--export([rest_bad_accept/1]).
--export([rest_bad_content_type/1]).
--export([rest_expires/1]).
--export([rest_keepalive/1]).
--export([rest_keepalive_post/1]).
--export([rest_missing_get_callbacks/1]).
--export([rest_missing_put_callbacks/1]).
--export([rest_nodelete/1]).
--export([rest_options_default/1]).
--export([rest_param_all/1]).
--export([rest_patch/1]).
--export([rest_post_charset/1]).
--export([rest_postonly/1]).
--export([rest_resource_etags/1]).
--export([rest_resource_etags_if_none_match/1]).
--export([set_env_dispatch/1]).
--export([set_resp_body/1]).
--export([set_resp_header/1]).
--export([set_resp_overwrite/1]).
--export([slowloris/1]).
--export([slowloris2/1]).
--export([static_attribute_etag/1]).
--export([static_function_etag/1]).
--export([static_mimetypes_function/1]).
--export([static_specify_file/1]).
--export([static_specify_file_catchall/1]).
--export([static_test_file/1]).
--export([static_test_file_css/1]).
--export([stream_body_set_resp/1]).
--export([stream_body_set_resp_close/1]).
--export([stream_body_set_resp_chunked/1]).
--export([stream_body_set_resp_chunked10/1]).
--export([streamed_response/1]).
--export([te_chunked/1]).
--export([te_chunked_chopped/1]).
--export([te_chunked_delayed/1]).
--export([te_chunked_split_body/1]).
--export([te_chunked_split_crlf/1]).
--export([te_identity/1]).
-
-%% ct.
all() ->
[
@@ -206,8 +124,12 @@ groups() ->
init_per_suite(Config) ->
application:start(crypto),
- application:start(cowlib),
+ application:start(asn1),
+ application:start(public_key),
+ application:start(ssl),
application:start(ranch),
+ application:start(cowlib),
+ application:start(gun),
application:start(cowboy),
Dir = ?config(priv_dir, Config) ++ "/static",
ct_helper:create_static_dir(Dir),
@@ -217,67 +139,46 @@ end_per_suite(Config) ->
Dir = ?config(static_dir, Config),
ct_helper:delete_static_dir(Dir),
application:stop(cowboy),
- application:stop(ranch),
+ application:stop(gun),
application:stop(cowlib),
+ application:stop(ranch),
+ application:stop(ssl),
+ application:stop(public_key),
+ application:stop(asn1),
application:stop(crypto),
ok.
-init_per_group(http, Config) ->
+init_tcp_group(Ref, ProtoOpts, Config) ->
Transport = ranch_tcp,
- {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
+ {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
{timeout, 500}
- ]),
- Port = ranch:get_port(http),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client}|Config];
-init_per_group(https, Config) ->
+ |ProtoOpts]),
+ Port = ranch:get_port(Ref),
+ [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
+
+init_ssl_group(Ref, ProtoOpts, Config) ->
Transport = ranch_ssl,
{_, Cert, Key} = ct_helper:make_certs(),
Opts = [{cert, Cert}, {key, Key}],
- application:start(asn1),
- application:start(public_key),
- application:start(ssl),
- {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, 0}], [
+ {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
{timeout, 500}
- ]),
- Port = ranch:get_port(https),
- {ok, Client} = cowboy_client:init(Opts),
- [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
- {transport, Transport}, {client, Client}|Config];
+ |ProtoOpts]),
+ Port = ranch:get_port(Ref),
+ [{type, ssl}, {port, Port}, {opts, Opts}, {transport, Transport}|Config].
+
+init_per_group(http, Config) ->
+ init_tcp_group(http, [], Config);
+init_per_group(https, Config) ->
+ init_ssl_group(https, [], Config);
init_per_group(http_compress, Config) ->
- Transport = ranch_tcp,
- {ok, _} = cowboy:start_http(http_compress, 100, [{port, 0}], [
- {compress, true},
- {env, [{dispatch, init_dispatch(Config)}]},
- {max_keepalive, 50},
- {timeout, 500}
- ]),
- Port = ranch:get_port(http_compress),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client}|Config];
+ init_tcp_group(http_compress, [{compress, true}], Config);
init_per_group(https_compress, Config) ->
- Transport = ranch_ssl,
- {_, Cert, Key} = ct_helper:make_certs(),
- Opts = [{cert, Cert}, {key, Key}],
- application:start(asn1),
- application:start(public_key),
- application:start(ssl),
- {ok, _} = cowboy:start_https(https_compress, 100, Opts ++ [{port, 0}], [
- {compress, true},
- {env, [{dispatch, init_dispatch(Config)}]},
- {max_keepalive, 50},
- {timeout, 500}
- ]),
- Port = ranch:get_port(https_compress),
- {ok, Client} = cowboy_client:init(Opts),
- [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
- {transport, Transport}, {client, Client}|Config];
+ init_ssl_group(https_compress, [{compress, true}], Config);
+%% Most, if not all of these, should be in separate test suites.
init_per_group(onrequest, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
@@ -287,9 +188,8 @@ init_per_group(onrequest, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(onrequest),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client}|Config];
+ [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+ {transport, Transport}|Config];
init_per_group(onresponse, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
@@ -299,9 +199,8 @@ init_per_group(onresponse, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(onresponse),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client}|Config];
+ [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+ {transport, Transport}|Config];
init_per_group(onresponse_capitalize, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
@@ -311,9 +210,8 @@ init_per_group(onresponse_capitalize, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(onresponse_capitalize),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client}|Config];
+ [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+ {transport, Transport}|Config];
init_per_group(parse_host, Config) ->
Transport = ranch_tcp,
Dispatch = cowboy_router:compile([
@@ -327,9 +225,8 @@ init_per_group(parse_host, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(http),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client}|Config];
+ [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+ {transport, Transport}|Config];
init_per_group(set_env, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
@@ -338,16 +235,9 @@ init_per_group(set_env, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(set_env),
- {ok, Client} = cowboy_client:init([]),
- [{scheme, <<"http">>}, {port, Port}, {opts, []},
- {transport, Transport}, {client, Client}|Config].
+ [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+ {transport, Transport}|Config].
-end_per_group(Name, _) when Name =:= https; Name =:= https_compress ->
- cowboy:stop_listener(Name),
- application:stop(ssl),
- application:stop(public_key),
- application:stop(asn1),
- ok;
end_per_group(Name, _) ->
cowboy:stop_listener(Name),
ok.
@@ -424,46 +314,83 @@ etag_gen(_, _, _) ->
mimetypes_text_html(_) ->
<<"text/html">>.
+%% Support functions for testing using Gun.
+
+gun_open(Config) ->
+ gun_open(Config, []).
+
+gun_open(Config, Opts) ->
+ {_, Port} = lists:keyfind(port, 1, Config),
+ {_, Type} = lists:keyfind(type, 1, Config),
+ {ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}, {type, Type}|Opts]),
+ ConnPid.
+
+gun_monitor_open(Config) ->
+ gun_monitor_open(Config, []).
+
+gun_monitor_open(Config, Opts) ->
+ ConnPid = gun_open(Config, Opts),
+ {ConnPid, monitor(process, ConnPid)}.
+
+gun_is_gone(ConnPid) ->
+ gun_is_gone(ConnPid, monitor(process, ConnPid)).
+
+gun_is_gone(ConnPid, MRef) ->
+ receive {'DOWN', MRef, process, ConnPid, gone} -> ok
+ after 500 -> error(timeout) end.
+
+%% Support functions for testing using a raw socket.
+
+raw_open(Config) ->
+ {_, Port} = lists:keyfind(port, 1, Config),
+ {_, Type} = lists:keyfind(type, 1, Config),
+ Transport = case Type of
+ tcp -> gen_tcp;
+ ssl -> ssl
+ end,
+ {_, Opts} = lists:keyfind(opts, 1, Config),
+ {ok, Socket} = Transport:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw},
+ {reuseaddr, true}, {nodelay, true}|Opts]),
+ {raw_client, Socket, Transport}.
+
+raw_send({raw_client, Socket, Transport}, Data) ->
+ Transport:send(Socket, Data).
+
+raw_recv_head({raw_client, Socket, Transport}) ->
+ {ok, Data} = Transport:recv(Socket, 0, 5000),
+ raw_recv_head(Socket, Transport, Data).
+
+raw_recv_head(Socket, Transport, Buffer) ->
+ case binary:match(Buffer, <<"\r\n\r\n">>) of
+ nomatch ->
+ {ok, Data} = Transport:recv(Socket, 0, 5000),
+ raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
+ {_, _} ->
+ Buffer
+ end.
+
+raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
+ {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000),
+ ok.
+
%% Convenience functions.
quick_raw(Data, Config) ->
- Client = ?config(client, Config),
- Transport = ?config(transport, Config),
- {ok, Client2} = cowboy_client:connect(
- Transport, "localhost", ?config(port, Config), Client),
- {ok, Client3} = cowboy_client:raw_request(Data, Client2),
- case cowboy_client:response(Client3) of
- {ok, Status, _, _} -> Status;
- {error, _} -> closed
+ Client = raw_open(Config),
+ ok = raw_send(Client, Data),
+ case catch raw_recv_head(Client) of
+ {'EXIT', _} -> closed;
+ Resp -> element(2, cow_http:parse_status_line(Resp))
end.
-build_url(Path, Config) ->
- {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
- {port, Port} = lists:keyfind(port, 1, Config),
- PortBin = list_to_binary(integer_to_list(Port)),
- PathBin = list_to_binary(Path),
- << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
-
-quick_get(URL, Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url(URL, Config), Client),
- {ok, Status, _, _} = cowboy_client:response(Client2),
+quick_get(Path, Config) ->
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, Path),
+ {response, _, Status, _} = gun:await(ConnPid, Ref),
+ gun:close(ConnPid),
Status.
-body_to_chunks(_, <<>>, Acc) ->
- lists:reverse([<<"0\r\n\r\n">>|Acc]);
-body_to_chunks(ChunkSize, Body, Acc) ->
- BodySize = byte_size(Body),
- ChunkSize2 = case BodySize < ChunkSize of
- true -> BodySize;
- false -> ChunkSize
- end,
- << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
- ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
- body_to_chunks(ChunkSize, Rest,
- [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
-
%% Tests.
check_raw_status(Config) ->
@@ -548,135 +475,95 @@ check_status(Config) ->
end || {Status, URL} <- Tests].
chunked_response(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/chunked_response", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/chunked_response"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
- {ok, Transport, Socket} = cowboy_client:transport(Client3),
- {ok, <<"11\r\nchunked_handler\r\n\r\nB\r\nworks fine!\r\n0\r\n\r\n">>}
- = Transport:recv(Socket, 44, 1000),
- {error, closed} = cowboy_client:response(Client3).
+ {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
+ ok.
%% Check if sending requests whose size is around the MTU breaks something.
echo_body(Config) ->
- Client = ?config(client, Config),
MTU = ct_helper:get_loopback_mtu(),
_ = [begin
Body = list_to_binary(lists:duplicate(Size, $a)),
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/echo/body", Config),
- [{<<"connection">>, <<"close">>}],
- Body, Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3)
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body", [], Body),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref)
end || Size <- lists:seq(MTU - 500, MTU)],
ok.
%% Check if sending request whose size is bigger than 1000000 bytes causes 413
echo_body_max_length(Config) ->
- Client = ?config(client, Config),
- Body = <<$a:8000008>>,
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/echo/body", Config),
- [{<<"connection">>, <<"close">>}],
- Body, Client),
- {ok, 413, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body", [], << 0:8000008 >>),
+ {response, nofin, 413, _} = gun:await(ConnPid, Ref),
+ ok.
% check if body_qs echo's back results
echo_body_qs(Config) ->
- Client = ?config(client, Config),
- Body = <<"echo=67890">>,
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/echo/body_qs", Config),
- [{<<"connection">>, <<"close">>}],
- Body, Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, <<"67890">>, _} = cowboy_client:response_body(Client3).
-
-%% Check if sending request whose size is bigger 16000 bytes causes 413
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, <<"67890">>} = gun:await_body(ConnPid, Ref),
+ ok.
+
echo_body_qs_max_length(Config) ->
- Client = ?config(client, Config),
- DefaultMaxBodyQsLength = 16000,
- % subtract "echo=" minus 1 byte from max to hit the limit
- Bits = (DefaultMaxBodyQsLength - 4) * 8,
- AppendedBody = <<$a:Bits>>,
- Body = <<"echo=", AppendedBody/binary>>,
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/echo/body_qs", Config),
- [{<<"connection">>, <<"close">>}],
- Body, Client),
- {ok, 413, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:15996/unit:8 >>),
+ {response, nofin, 413, _} = gun:await(ConnPid, Ref),
+ ok.
error_chain_handle_after_reply(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, Client3} = cowboy_client:request(<<"GET">>,
- build_url("/handler_errors?case=handle_after_reply", Config), Client2),
- {ok, 200, _, Client4} = cowboy_client:response(Client3),
- {ok, 200, _, Client5} = cowboy_client:response(Client4),
- {error, closed} = cowboy_client:response(Client5).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Ref1 = gun:get(ConnPid, "/"),
+ Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref2, MRef),
+ gun_is_gone(ConnPid, MRef).
error_chain_handle_before_reply(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, Client3} = cowboy_client:request(<<"GET">>,
- build_url("/handler_errors?case=handle_before_reply", Config), Client2),
- {ok, 200, _, Client4} = cowboy_client:response(Client3),
- {ok, 500, _, Client5} = cowboy_client:response(Client4),
- {error, closed} = cowboy_client:response(Client5).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Ref1 = gun:get(ConnPid, "/"),
+ Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_before_reply"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
+ {response, fin, 500, _} = gun:await(ConnPid, Ref2, MRef),
+ gun_is_gone(ConnPid, MRef).
error_handle_after_reply(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/handler_errors?case=handle_after_reply", Config), Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {error, closed} = cowboy_client:response(Client3).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Ref = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+ gun_is_gone(ConnPid, MRef).
error_init_after_reply(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/handler_errors?case=init_after_reply", Config), Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {error, closed} = cowboy_client:response(Client3).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+ gun_is_gone(ConnPid, MRef).
error_init_reply_handle_error(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {error, closed} = cowboy_client:response(Client3).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Ref = gun:get(ConnPid, "/handler_errors?case=init_reply_handle_error"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+ gun_is_gone(ConnPid, MRef).
headers_dupe(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/headers/dupe", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
- {<<"connection">>, <<"close">>}
- = lists:keyfind(<<"connection">>, 1, Headers),
- Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
- 1 = length(Connections),
- {error, closed} = cowboy_client:response(Client3).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Ref = gun:get(ConnPid, "/headers/dupe"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+ %% Ensure that only one connection header was received.
+ [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
+ gun_is_gone(ConnPid, MRef).
http10_chunkless(Config) ->
- Client = ?config(client, Config),
- Transport = ?config(transport, Config),
- {ok, Client2} = cowboy_client:connect(
- Transport, "localhost", ?config(port, Config), Client),
- Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
- {ok, Client3} = cowboy_client:raw_request(Data, Client2),
- {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
+ {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
+ Ref = gun:get(ConnPid, "/chunked_response"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
- %% Hack: we just try to get 28 bytes and compare.
- {ok, Transport, Socket} = cowboy_client:transport(Client4),
- Buffer = element(7, Client4),
- Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
- {ok, Recv} -> << Buffer/binary, Recv/binary >>;
- _ -> Buffer
- end,
- <<"chunked_handler\r\nworks fine!">> = Buffer2.
+ {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef),
+ gun_is_gone(ConnPid, MRef).
http10_hostless(Config) ->
Port10 = ?config(port, Config) + 10,
@@ -694,66 +581,48 @@ http10_hostless(Config) ->
cowboy:stop_listener(http10).
keepalive_max(Config) ->
- Client = ?config(client, Config),
- URL = build_url("/", Config),
- ok = keepalive_max_loop(Client, URL, 50).
-
-keepalive_max_loop(Client, _, 0) ->
- {error, closed} = cowboy_client:response(Client),
- ok;
-keepalive_max_loop(Client, URL, N) ->
- Headers = [{<<"connection">>, <<"keep-alive">>}],
- {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
- {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
- Expected = case N of
- 1 -> <<"close">>;
- N -> <<"keep-alive">>
- end,
- {<<"connection">>, Expected}
- = lists:keyfind(<<"connection">>, 1, RespHeaders),
- keepalive_max_loop(Client3, URL, N - 1).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
+ || _ <- lists:seq(1, 49)],
+ CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
+ _ = [begin
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+ {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+ end || Ref <- Refs],
+ {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
+ {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
+ gun_is_gone(ConnPid, MRef).
keepalive_nl(Config) ->
- Client = ?config(client, Config),
- URL = build_url("/", Config),
- ok = keepalive_nl_loop(Client, URL, 10).
-
-keepalive_nl_loop(Client, _, 0) ->
- {error, closed} = cowboy_client:response(Client),
- ok;
-keepalive_nl_loop(Client, URL, N) ->
- Headers = [{<<"connection">>, <<"keep-alive">>}],
- {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
- {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
- {<<"connection">>, <<"keep-alive">>}
- = lists:keyfind(<<"connection">>, 1, RespHeaders),
- {ok, Transport, Socket} = cowboy_client:transport(Client2),
- ok = Transport:send(Socket, <<"\r\n">>), %% empty line
- keepalive_nl_loop(Client3, URL, N - 1).
+ ConnPid = gun_open(Config),
+ Refs = [begin
+ Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
+ gun:dbg_send_raw(ConnPid, <<"\r\n">>),
+ Ref
+ end || _ <- lists:seq(1, 10)],
+ _ = [begin
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+ end || Ref <- Refs],
+ ok.
keepalive_stream_loop(Config) ->
- Client = ?config(client, Config),
- Transport = ?config(transport, Config),
- {ok, Client2} = cowboy_client:connect(
- Transport, "localhost", ?config(port, Config), Client),
- keepalive_stream_loop(Client2, 10).
-
-keepalive_stream_loop(Client, 0) ->
- {error, closed} = cowboy_client:response(Client),
- ok;
-keepalive_stream_loop(Client, N) ->
- {ok, _} = cowboy_client:raw_request("POST /loop_stream_recv HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: keepalive\r\n"
- "Transfer-Encoding: chunked\r\n\r\n", Client),
- _ = [{ok, _} = cowboy_client:raw_request(<<"4\r\n",Id:32,"\r\n">>, Client) ||
- Id <- lists:seq(1, 250)],
- {ok, _} = cowboy_client:raw_request(<<"0\r\n\r\n">>, Client),
- {ok, 200, _, _} = cowboy_client:response(Client),
- keepalive_stream_loop(Client, N-1).
+ ConnPid = gun_open(Config),
+ Refs = [begin
+ Ref = gun:post(ConnPid, "/loop_stream_recv",
+ [{<<"transfer-encoding">>, <<"chunked">>}]),
+ _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
+ || ID <- lists:seq(1, 250)],
+ gun:data(ConnPid, Ref, fin, <<>>),
+ Ref
+ end || _ <- lists:seq(1, 10)],
+ _ = [begin
+ {response, fin, 200, _} = gun:await(ConnPid, Ref)
+ end || Ref <- Refs],
+ ok.
multipart(Config) ->
- Client = ?config(client, Config),
+ ConnPid = gun_open(Config),
Body = <<
"This is a preamble."
"\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
@@ -761,31 +630,30 @@ multipart(Config) ->
"\r\n--OHai--\r\n"
"This is an epilogue."
>>,
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/multipart", Config),
+ Ref = gun:post(ConnPid, "/multipart",
[{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
- Body, Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, RespBody, _} = cowboy_client:response_body(Client3),
+ Body),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, RespBody} = gun:await_body(ConnPid, Ref),
Parts = binary_to_term(RespBody),
Parts = [
{[{<<"x-name">>, <<"answer">>}], <<"42">>},
{[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
- ].
+ ],
+ ok.
multipart_large(Config) ->
- Client = ?config(client, Config),
+ ConnPid = gun_open(Config),
Boundary = "----------",
Big = << 0:9000000/unit:8 >>,
Bigger = << 0:9999999/unit:8 >>,
Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
"--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
"--", Boundary, "--\r\n"],
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/multipart/large", Config),
+ Ref = gun:post(ConnPid, "/multipart/large",
[{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
- Body, Client),
- {ok, 200, _, _} = cowboy_client:response(Client2),
+ Body),
+ {response, fin, 200, _} = gun:await(ConnPid, Ref),
ok.
nc_reqs(Config, Input) ->
@@ -812,20 +680,20 @@ nc_zero(Config) ->
nc_reqs(Config, "/dev/zero").
onrequest(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
- {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
+ {ok, <<"http_handler">>} = gun:await_body(ConnPid, Ref),
+ ok.
onrequest_reply(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/?reply=1", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/?reply=1"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
- {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
+ {ok, <<"replied!">>} = gun:await_body(ConnPid, Ref),
+ ok.
%% Hook for the above onrequest tests.
onrequest_hook(Req) ->
@@ -839,12 +707,11 @@ onrequest_hook(Req) ->
end.
onresponse_capitalize(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, Transport, Socket} = cowboy_client:transport(Client2),
- {ok, Data} = Transport:recv(Socket, 0, 1000),
- false = nomatch =:= binary:match(Data, <<"Content-Length">>).
+ Client = raw_open(Config),
+ ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
+ Data = raw_recv_head(Client),
+ false = nomatch =:= binary:match(Data, <<"Content-Length">>),
+ ok.
%% Hook for the above onresponse_capitalize test.
onresponse_capitalize_hook(Status, Headers, Body, Req) ->
@@ -854,20 +721,17 @@ onresponse_capitalize_hook(Status, Headers, Body, Req) ->
Req2.
onresponse_crash(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/handler_errors?case=init_before_reply", Config), Client),
- {ok, 777, Headers, _} = cowboy_client:response(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
+ {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
onresponse_reply(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/"),
+ {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
- %% Make sure we don't get the body initially sent.
- {error, closed} = cowboy_client:response_body(Client3).
+ ok.
%% Hook for the above onresponse tests.
onresponse_hook(_, Headers, _, Req) ->
@@ -876,192 +740,145 @@ onresponse_hook(_, Headers, _, Req) ->
Req2.
parse_host(Config) ->
+ ConnPid = gun_open(Config),
Tests = [
- {<<"example.org\n8080">>, <<"example.org:8080">>},
- {<<"example.org\n80">>, <<"example.org">>},
- {<<"192.0.2.1\n8080">>, <<"192.0.2.1:8080">>},
- {<<"192.0.2.1\n80">>, <<"192.0.2.1">>},
- {<<"[2001:db8::1]\n8080">>, <<"[2001:db8::1]:8080">>},
- {<<"[2001:db8::1]\n80">>, <<"[2001:db8::1]">>},
- {<<"[::ffff:192.0.2.1]\n8080">>, <<"[::ffff:192.0.2.1]:8080">>},
- {<<"[::ffff:192.0.2.1]\n80">>, <<"[::ffff:192.0.2.1]">>}
+ {<<"example.org:8080">>, <<"example.org\n8080">>},
+ {<<"example.org">>, <<"example.org\n80">>},
+ {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
+ {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
+ {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
+ {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
+ {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
+ {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
],
[begin
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/req_attr?attr=host_and_port", Config),
- [{<<"host">>, Host}],
- Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Value, Client4} = cowboy_client:response_body(Client3),
- {error, closed} = cowboy_client:response(Client4),
- Value
- end || {Value, Host} <- Tests].
+ Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
+ [{<<"host">>, Host}]),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref)
+ end || {Host, Body} <- Tests],
+ ok.
pipeline(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, Client3} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client2),
- {ok, Client4} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client3),
- {ok, Client5} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client4),
- {ok, Client6} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
- {ok, 200, _, Client7} = cowboy_client:response(Client6),
- {ok, 200, _, Client8} = cowboy_client:response(Client7),
- {ok, 200, _, Client9} = cowboy_client:response(Client8),
- {ok, 200, _, Client10} = cowboy_client:response(Client9),
- {ok, 200, _, Client11} = cowboy_client:response(Client10),
- {error, closed} = cowboy_client:response(Client11).
+ ConnPid = gun_open(Config),
+ Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
+ _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
+ ok.
pipeline_long_polling(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/long_polling", Config), Client),
- {ok, Client3} = cowboy_client:request(<<"GET">>,
- build_url("/long_polling", Config), Client2),
- {ok, 102, _, Client4} = cowboy_client:response(Client3),
- {ok, 102, _, Client5} = cowboy_client:response(Client4),
- {error, closed} = cowboy_client:response(Client5).
+ ConnPid = gun_open(Config),
+ Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)],
+ _ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
+ ok.
rest_param_all(Config) ->
- Client = ?config(client, Config),
- URL = build_url("/param_all", Config),
- % Accept without param
- {ok, Client2} = cowboy_client:request(<<"GET">>, URL,
- [{<<"accept">>, <<"text/plain">>}], Client),
- Client3 = check_response(Client2, <<"[]">>),
- % Accept with param
- {ok, Client4} = cowboy_client:request(<<"GET">>, URL,
- [{<<"accept">>, <<"text/plain;level=1">>}], Client3),
- Client5 = check_response(Client4, <<"level=1">>),
- % Accept with param and quality
- {ok, Client6} = cowboy_client:request(<<"GET">>, URL,
- [{<<"accept">>,
- <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}],
- Client5),
- Client7 = check_response(Client6, <<"level=1">>),
- {ok, Client8} = cowboy_client:request(<<"GET">>, URL,
- [{<<"accept">>,
- <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}],
- Client7),
- Client9 = check_response(Client8, <<"level=2">>),
- % Without Accept
- {ok, Client10} = cowboy_client:request(<<"GET">>, URL, [], Client9),
- Client11 = check_response(Client10, <<"'*'">>),
- % Content-Type without param
- {ok, Client12} = cowboy_client:request(<<"PUT">>, URL,
- [{<<"content-type">>, <<"text/plain">>}], Client11),
- {ok, 204, _, Client13} = cowboy_client:response(Client12),
- % Content-Type with param
- {ok, Client14} = cowboy_client:request(<<"PUT">>, URL,
- [{<<"content-type">>, <<"text/plain; charset=utf-8">>}], Client13),
- {ok, 204, _, _} = cowboy_client:response(Client14).
-
-check_response(Client, Body) ->
- {ok, 200, _, Client2} = cowboy_client:response(Client),
- {ok, Body, Client3} = cowboy_client:response_body(Client2),
- Client3.
+ ConnPid = gun_open(Config),
+ %% Accept without param.
+ Ref1 = gun:get(ConnPid, "/param_all",
+ [{<<"accept">>, <<"text/plain">>}]),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
+ {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
+ %% Accept with param.
+ Ref2 = gun:get(ConnPid, "/param_all",
+ [{<<"accept">>, <<"text/plain;level=1">>}]),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
+ {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
+ %% Accept with param and quality.
+ Ref3 = gun:get(ConnPid, "/param_all",
+ [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
+ {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
+ Ref4 = gun:get(ConnPid, "/param_all",
+ [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
+ {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
+ %% Without Accept.
+ Ref5 = gun:get(ConnPid, "/param_all"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
+ {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
+ %% Content-Type without param.
+ Ref6 = gun:put(ConnPid, "/param_all",
+ [{<<"content-type">>, <<"text/plain">>}]),
+ {response, fin, 204, _} = gun:await(ConnPid, Ref6),
+ %% Content-Type with param.
+ Ref7 = gun:put(ConnPid, "/param_all",
+ [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
+ {response, fin, 204, _} = gun:await(ConnPid, Ref7),
+ ok.
rest_bad_accept(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/bad_accept", Config),
- [{<<"accept">>, <<"1">>}],
- Client),
- {ok, 400, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/bad_accept",
+ [{<<"accept">>, <<"1">>}]),
+ {response, fin, 400, _} = gun:await(ConnPid, Ref),
+ ok.
rest_bad_content_type(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"PATCH">>,
- build_url("/bad_content_type", Config),
- [{<<"content-type">>, <<"text/plain, text/html">>}],
- <<"Whatever">>, Client),
- {ok, 415, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:patch(ConnPid, "/bad_content_type",
+ [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
+ {response, fin, 415, _} = gun:await(ConnPid, Ref),
+ ok.
rest_expires(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/rest_expires", Config), Client),
- {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
- {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
- {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/rest_expires"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
+ {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
ok.
rest_keepalive(Config) ->
- Client = ?config(client, Config),
- URL = build_url("/simple", Config),
- ok = rest_keepalive_loop(Client, URL, 10).
-
-rest_keepalive_loop(_, _, 0) ->
- ok;
-rest_keepalive_loop(Client, URL, N) ->
- Headers = [{<<"connection">>, <<"keep-alive">>}],
- {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
- {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
- {<<"connection">>, <<"keep-alive">>}
- = lists:keyfind(<<"connection">>, 1, RespHeaders),
- rest_keepalive_loop(Client3, URL, N - 1).
+ ConnPid = gun_open(Config),
+ Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
+ _ = [begin
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+ end || Ref <- Refs],
+ ok.
rest_keepalive_post(Config) ->
- Client = ?config(client, Config),
- ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
-
-rest_keepalive_post_loop(_, _, _, 0) ->
- ok;
-rest_keepalive_post_loop(Config, Client, simple_post, N) ->
- Headers = [
- {<<"connection">>, <<"keep-alive">>},
- {<<"content-type">>, <<"text/plain">>}
- ],
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/simple_post", Config), Headers, "12345", Client),
- {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
- {<<"connection">>, <<"keep-alive">>}
- = lists:keyfind(<<"connection">>, 1, RespHeaders),
- rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
-rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
- Headers = [
- {<<"connection">>, <<"keep-alive">>},
- {<<"content-type">>, <<"text/plain">>}
- ],
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/forbidden_post", Config), Headers, "12345", Client),
- {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
- {<<"connection">>, <<"keep-alive">>}
- = lists:keyfind(<<"connection">>, 1, RespHeaders),
- rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
+ ConnPid = gun_open(Config),
+ Refs = [{
+ gun:post(ConnPid, "/forbidden_post",
+ [{<<"content-type">>, <<"text/plain">>}]),
+ gun:post(ConnPid, "/simple_post",
+ [{<<"content-type">>, <<"text/plain">>}])
+ } || _ <- lists:seq(1, 5)],
+ _ = [begin
+ {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
+ {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
+ {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
+ {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
+ end || {Ref1, Ref2} <- Refs],
+ ok.
rest_missing_get_callbacks(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/missing_get_callbacks", Config), Client),
- {ok, 500, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/missing_get_callbacks"),
+ {response, fin, 500, _} = gun:await(ConnPid, Ref),
+ ok.
rest_missing_put_callbacks(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"PUT">>,
- build_url("/missing_put_callbacks", Config),
- [{<<"content-type">>, <<"application/json">>}],
- <<"{}">>, Client),
- {ok, 500, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:put(ConnPid, "/missing_put_callbacks",
+ [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
+ {response, fin, 500, _} = gun:await(ConnPid, Ref),
+ ok.
rest_nodelete(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"DELETE">>,
- build_url("/nodelete", Config), Client),
- {ok, 500, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:delete(ConnPid, "/nodelete"),
+ {response, fin, 500, _} = gun:await(ConnPid, Ref),
+ ok.
rest_options_default(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"OPTIONS">>,
- build_url("/rest_empty_resource", Config), Client),
- {ok, 200, Headers, _} = cowboy_client:response(Client2),
- {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers).
+ ConnPid = gun_open(Config),
+ Ref = gun:options(ConnPid, "/rest_empty_resource"),
+ {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
+ ok.
rest_patch(Config) ->
Tests = [
@@ -1070,40 +887,34 @@ rest_patch(Config) ->
{400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
{415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
],
- Client = ?config(client, Config),
+ ConnPid = gun_open(Config),
_ = [begin
- {ok, Client2} = cowboy_client:request(<<"PATCH">>,
- build_url("/patch", Config), Headers, Body, Client),
- {ok, Status, _, _} = cowboy_client:response(Client2),
- ok
- end || {Status, Headers, Body} <- Tests].
+ Ref = gun:patch(ConnPid, "/patch", Headers, Body),
+ {response, fin, Status, _} = gun:await(ConnPid, Ref)
+ end || {Status, Headers, Body} <- Tests],
+ ok.
rest_post_charset(Config) ->
- Client = ?config(client, Config),
- Headers = [
- {<<"content-type">>, <<"text/plain;charset=UTF-8">>}
- ],
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/post_charset", Config), Headers, "12345", Client),
- {ok, 204, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/post_charset",
+ [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
+ {response, fin, 204, _} = gun:await(ConnPid, Ref),
+ ok.
rest_postonly(Config) ->
- Client = ?config(client, Config),
- Headers = [
- {<<"content-type">>, <<"text/plain">>}
- ],
- {ok, Client2} = cowboy_client:request(<<"POST">>,
- build_url("/postonly", Config), Headers, "12345", Client),
- {ok, 204, _, _} = cowboy_client:response(Client2).
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/postonly",
+ [{<<"content-type">>, <<"text/plain">>}], "12345"),
+ {response, fin, 204, _} = gun:await(ConnPid, Ref),
+ ok.
rest_resource_get_etag(Config, Type) ->
rest_resource_get_etag(Config, Type, []).
rest_resource_get_etag(Config, Type, Headers) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/resetags?type=" ++ Type, Config), Headers, Client),
- {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
+ {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
case lists:keyfind(<<"etag">>, 1, RespHeaders) of
false -> {Status, false};
{<<"etag">>, ETag} -> {Status, ETag}
@@ -1137,48 +948,44 @@ rest_resource_etags_if_none_match(Config) ->
end || {Status, ETag, Type} <- Tests].
set_env_dispatch(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, 400, _, _} = cowboy_client:response(Client2),
+ ConnPid1 = gun_open(Config),
+ Ref1 = gun:get(ConnPid1, "/"),
+ {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
ok = cowboy:set_env(set_env, dispatch,
cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
- {ok, Client3} = cowboy_client:request(<<"GET">>,
- build_url("/", Config), Client),
- {ok, 200, _, _} = cowboy_client:response(Client3).
+ ConnPid2 = gun_open(Config),
+ Ref2 = gun:get(ConnPid2, "/"),
+ {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
+ ok.
set_resp_body(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/set_resp/body", Config), Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, <<"A flameless dance does not equal a cycle">>, _}
- = cowboy_client:response_body(Client3).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/set_resp/body"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, <<"A flameless dance does not equal a cycle">>}
+ = gun:await_body(ConnPid, Ref),
+ ok.
set_resp_header(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/set_resp/header", Config), Client),
- {ok, 200, Headers, _} = cowboy_client:response(Client2),
- {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
- {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/set_resp/header"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
+ {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
+ ok.
set_resp_overwrite(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/set_resp/overwrite", Config), Client),
- {ok, 200, Headers, _} = cowboy_client:response(Client2),
- {<<"server">>, <<"DesireDrive/1.0">>}
- = lists:keyfind(<<"server">>, 1, Headers).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/set_resp/overwrite"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
+ ok.
slowloris(Config) ->
- Client = ?config(client, Config),
- Transport = ?config(transport, Config),
- {ok, Client2} = cowboy_client:connect(
- Transport, "localhost", ?config(port, Config), Client),
+ Client = raw_open(Config),
try
[begin
- {ok, _} = cowboy_client:raw_request([C], Client2),
+ ok = raw_send(Client, [C]),
receive after 25 -> ok end
end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
@@ -1189,249 +996,214 @@ slowloris(Config) ->
end.
slowloris2(Config) ->
- Client = ?config(client, Config),
- Transport = ?config(transport, Config),
- {ok, Client2} = cowboy_client:connect(
- Transport, "localhost", ?config(port, Config), Client),
- {ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2),
+ Client = raw_open(Config),
+ ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
receive after 300 -> ok end,
- {ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2),
+ ok = raw_send(Client, "Host: localhost\r\n"),
receive after 300 -> ok end,
- {ok, 408, _, _} = cowboy_client:response(Client2).
+ Data = raw_recv_head(Client),
+ {_, 408, _, _} = cow_http:parse_status_line(Data),
+ ok.
static_attribute_etag(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/static_attribute_etag/index.html", Config), Client),
- {ok, Client3} = cowboy_client:request(<<"GET">>,
- build_url("/static_attribute_etag/index.html", Config), Client2),
- {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
- {ok, 200, Headers2, _} = cowboy_client:response(Client4),
- {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
- {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
- false = ETag1 =:= undefined,
- ETag1 = ETag2.
+ ConnPid = gun_open(Config),
+ Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
+ Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
+ {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
+ {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
+ {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
+ {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
+ true = ETag =/= undefined,
+ ok.
static_function_etag(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/static_function_etag/index.html", Config), Client),
- {ok, Client3} = cowboy_client:request(<<"GET">>,
- build_url("/static_function_etag/index.html", Config), Client2),
- {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
- {ok, 200, Headers2, _} = cowboy_client:response(Client4),
- {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
- {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
- false = ETag1 =:= undefined,
- ETag1 = ETag2.
+ ConnPid = gun_open(Config),
+ Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
+ Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
+ {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
+ {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
+ {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
+ {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
+ true = ETag =/= undefined,
+ ok.
static_mimetypes_function(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/static_mimetypes_function/index.html", Config), Client),
- {ok, 200, Headers, _} = cowboy_client:response(Client2),
- {<<"content-type">>, <<"text/html">>}
- = lists:keyfind(<<"content-type">>, 1, Headers).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+ ok.
static_specify_file(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/static_specify_file", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
- {<<"content-type">>, <<"text/css">>}
- = lists:keyfind(<<"content-type">>, 1, Headers),
- {ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/static_specify_file"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+ {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
+ ok.
static_specify_file_catchall(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/static_specify_file/none", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
- {<<"content-type">>, <<"text/css">>}
- = lists:keyfind(<<"content-type">>, 1, Headers),
- {ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/static_specify_file/none"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+ {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
+ ok.
static_test_file(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/static/unknown", Config), Client),
- {ok, 200, Headers, _} = cowboy_client:response(Client2),
- {<<"content-type">>, <<"application/octet-stream">>}
- = lists:keyfind(<<"content-type">>, 1, Headers).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/static/unknown"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+ ok.
static_test_file_css(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/static/style.css", Config), Client),
- {ok, 200, Headers, _} = cowboy_client:response(Client2),
- {<<"content-type">>, <<"text/css">>}
- = lists:keyfind(<<"content-type">>, 1, Headers).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/static/style.css"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+ ok.
stream_body_set_resp(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/stream_body/set_resp", Config), Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, <<"stream_body_set_resp">>, _}
- = cowboy_client:response_body(Client3).
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/stream_body/set_resp"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
+ ok.
stream_body_set_resp_close(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/stream_body/set_resp_close", Config), Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Transport, Socket} = cowboy_client:transport(Client3),
- case element(7, Client3) of
- <<"stream_body_set_resp_close">> ->
- ok;
- Buffer ->
- {ok, Rest} = Transport:recv(Socket, 26 - byte_size(Buffer), 1000),
- <<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>,
- ok
- end,
- {error, closed} = Transport:recv(Socket, 0, 1000).
+ {ConnPid, MRef} = gun_monitor_open(Config),
+ Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+ {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
+ gun_is_gone(ConnPid, MRef).
stream_body_set_resp_chunked(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/stream_body/set_resp_chunked", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
- {ok, Transport, Socket} = cowboy_client:transport(Client3),
- case element(7, Client3) of
- <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">> ->
- ok;
- Buffer ->
- {ok, Rest} = Transport:recv(Socket, 44 - byte_size(Buffer), 1000),
- <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">>
- = <<Buffer/binary, Rest/binary>>,
- ok
- end.
+ {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
+ ok.
stream_body_set_resp_chunked10(Config) ->
- Client = ?config(client, Config),
- Transport = ?config(transport, Config),
- {ok, Client2} = cowboy_client:connect(
- Transport, "localhost", ?config(port, Config), Client),
- Data = ["GET /stream_body/set_resp_chunked HTTP/1.0\r\n",
- "Host: localhost\r\n\r\n"],
- {ok, Client3} = cowboy_client:raw_request(Data, Client2),
- {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
- false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
- {ok, Transport, Socket} = cowboy_client:transport(Client4),
- case element(7, Client4) of
- <<"stream_body_set_resp_chunked">> ->
- ok;
- Buffer ->
- {ok, Rest} = Transport:recv(Socket, 28 - byte_size(Buffer), 1000),
- <<"stream_body_set_resp_chunked">>
- = <<Buffer/binary, Rest/binary>>,
- ok
- end,
- {error, closed} = Transport:recv(Socket, 0, 1000).
+ {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
+ Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+ false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
+ {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
+ gun_is_gone(ConnPid, MRef).
+%% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
streamed_response(Config) ->
- Client = ?config(client, Config),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/streamed_response", Config), Client),
- {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+ Client = raw_open(Config),
+ ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
+ Data = raw_recv_head(Client),
+ {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
+ {Headers, Rest2} = cow_http:parse_headers(Rest),
false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
- {ok, Transport, Socket} = cowboy_client:transport(Client3),
- {ok, <<"streamed_handler\r\nworks fine!">>}
- = Transport:recv(Socket, 29, 1000),
- {error, closed} = cowboy_client:response(Client3).
+ Rest2Size = byte_size(Rest2),
+ ok = case <<"streamed_handler\r\nworks fine!">> of
+ Rest2 -> ok;
+ << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
+ end.
te_chunked(Config) ->
- Client = ?config(client, Config),
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
- Chunks = body_to_chunks(50, Body, []),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/echo/body", Config),
- [{<<"transfer-encoding">>, <<"chunked">>}],
- Chunks, Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3).
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body",
+ [{<<"transfer-encoding">>, <<"chunked">>}], Body),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref),
+ ok.
+
+body_to_chunks(_, <<>>, Acc) ->
+ lists:reverse([<<"0\r\n\r\n">>|Acc]);
+body_to_chunks(ChunkSize, Body, Acc) ->
+ BodySize = byte_size(Body),
+ ChunkSize2 = case BodySize < ChunkSize of
+ true -> BodySize;
+ false -> ChunkSize
+ end,
+ << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
+ ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
+ body_to_chunks(ChunkSize, Rest,
+ [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
te_chunked_chopped(Config) ->
- Client = ?config(client, Config),
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/echo/body", Config),
- [{<<"transfer-encoding">>, <<"chunked">>}], Client),
- {ok, Transport, Socket} = cowboy_client:transport(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body",
+ [{<<"transfer-encoding">>, <<"chunked">>}]),
_ = [begin
- ok = Transport:send(Socket, << C >>),
- ok = timer:sleep(10)
+ ok = gun:dbg_send_raw(ConnPid, << C >>),
+ receive after 10 -> ok end
end || << C >> <= Body2],
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3).
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref),
+ ok.
te_chunked_delayed(Config) ->
- Client = ?config(client, Config),
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
Chunks = body_to_chunks(50, Body, []),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/echo/body", Config),
- [{<<"transfer-encoding">>, <<"chunked">>}], Client),
- {ok, Transport, Socket} = cowboy_client:transport(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body",
+ [{<<"transfer-encoding">>, <<"chunked">>}]),
_ = [begin
- ok = Transport:send(Socket, Chunk),
- ok = timer:sleep(10)
+ ok = gun:dbg_send_raw(ConnPid, Chunk),
+ receive after 10 -> ok end
end || Chunk <- Chunks],
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3).
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref),
+ ok.
te_chunked_split_body(Config) ->
- Client = ?config(client, Config),
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
Chunks = body_to_chunks(50, Body, []),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/echo/body", Config),
- [{<<"transfer-encoding">>, <<"chunked">>}], Client),
- {ok, Transport, Socket} = cowboy_client:transport(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body",
+ [{<<"transfer-encoding">>, <<"chunked">>}]),
_ = [begin
case Chunk of
- %% Final chunk.
<<"0\r\n\r\n">> ->
- ok = Transport:send(Socket, Chunk);
+ ok = gun:dbg_send_raw(ConnPid, Chunk);
_ ->
- %% Chunk of form <<"9\r\nChunkBody\r\n">>.
[Size, ChunkBody, <<>>] =
binary:split(Chunk, [<<"\r\n">>], [global]),
PartASize = random:uniform(byte_size(ChunkBody)),
<<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
- ok = Transport:send(Socket, [Size, <<"\r\n">>, PartA]),
- ok = timer:sleep(10),
- ok = Transport:send(Socket, [PartB, <<"\r\n">>])
+ ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
+ receive after 10 -> ok end,
+ ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
end
end || Chunk <- Chunks],
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3).
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref),
+ ok.
te_chunked_split_crlf(Config) ->
- Client = ?config(client, Config),
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
Chunks = body_to_chunks(50, Body, []),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/echo/body", Config),
- [{<<"transfer-encoding">>, <<"chunked">>}], Client),
- {ok, Transport, Socket} = cowboy_client:transport(Client2),
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body",
+ [{<<"transfer-encoding">>, <<"chunked">>}]),
_ = [begin
- %% <<"\r\n">> is last 2 bytes of Chunk split before or after <<"\r">>.
+ %% Split in the newline just before the end of the chunk.
Len = byte_size(Chunk) - (random:uniform(2) - 1),
- <<Chunk2:Len/binary, End/binary>> = Chunk,
- ok = Transport:send(Socket, Chunk2),
- ok = timer:sleep(10),
- ok = Transport:send(Socket, End)
+ << Chunk2:Len/binary, End/binary >> = Chunk,
+ ok = gun:dbg_send_raw(ConnPid, Chunk2),
+ receive after 10 -> ok end,
+ ok = gun:dbg_send_raw(ConnPid, End)
end || Chunk <- Chunks],
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3).
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref),
+ ok.
te_identity(Config) ->
- Client = ?config(client, Config),
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
- {ok, Client2} = cowboy_client:request(<<"GET">>,
- build_url("/echo/body", Config), [], Body, Client),
- {ok, 200, _, Client3} = cowboy_client:response(Client2),
- {ok, Body, _} = cowboy_client:response_body(Client3).
+ ConnPid = gun_open(Config),
+ Ref = gun:post(ConnPid, "/echo/body", [], Body),
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, Body} = gun:await_body(ConnPid, Ref),
+ ok.
diff --git a/test/http_SUITE_data/http_loop_stream_recv.erl b/test/http_SUITE_data/http_loop_stream_recv.erl
index 87113c6..9f7646a 100644
--- a/test/http_SUITE_data/http_loop_stream_recv.erl
+++ b/test/http_SUITE_data/http_loop_stream_recv.erl
@@ -8,34 +8,27 @@
init({_, http}, Req, _) ->
receive after 100 -> ok end,
self() ! stream,
- {loop, Req, 1, 100}.
+ {loop, Req, undefined, 100}.
-info(stream, Req, Id) ->
- case stream_next(Req) of
- {ok, Id, Req2} ->
- info(stream, Req2, Id+1);
+info(stream, Req, undefined) ->
+ stream(Req, 1, <<>>).
+
+stream(Req, ID, Acc) ->
+ case cowboy_req:stream_body(Req) of
+ {ok, Data, Req2} ->
+ parse_id(Req2, ID, << Acc/binary, Data/binary >>);
{done, Req2} ->
{ok, Req3} = cowboy_req:reply(200, Req2),
- {ok, Req3, Id}
+ {ok, Req3, undefined}
+ end.
+
+parse_id(Req, ID, Data) ->
+ case Data of
+ << ID:32, Rest/bits >> ->
+ parse_id(Req, ID + 1, Rest);
+ _ ->
+ stream(Req, ID, Data)
end.
terminate({normal, shutdown}, _, _) ->
ok.
-
-stream_next(Req) ->
- stream_next(<<>>, Req).
-
-stream_next(Buffer, Req) ->
- case cowboy_req:stream_body(Req) of
- {ok, Packet, Req2} ->
- case <<Buffer/binary, Packet/binary>> of
- <<Id:32>> ->
- {ok, Id, Req2};
- Buffer2 when byte_size(Buffer2) < 4 ->
- stream_next(Buffer2, Req2);
- _InvalidBuffer ->
- {error, invalid_chunk}
- end;
- Other ->
- Other
- end.
diff --git a/test/spdy_SUITE.erl b/test/spdy_SUITE.erl
index 72ec035..a7e1a7e 100644
--- a/test/spdy_SUITE.erl
+++ b/test/spdy_SUITE.erl
@@ -100,24 +100,19 @@ init_dispatch(Config) ->
%% Convenience functions.
-quick_get(Pid, Host, Path) ->
- MRef = monitor(process, Pid),
- StreamRef = gun:get(Pid, Path, [{":host", Host}]),
- receive
- {'DOWN', MRef, _, _, Reason} ->
- error(Reason);
- {gun_response, Pid, StreamRef, IsFin,
- << Status:3/binary, _/bits >>, Headers} ->
- {IsFin, list_to_integer(binary_to_list(Status)), Headers}
- after 1000 ->
- error(timeout)
- end.
+gun_monitor_open(Config) ->
+ {_, Port} = lists:keyfind(port, 1, Config),
+ {ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}]),
+ {ConnPid, monitor(process, ConnPid)}.
+
+quick_get(ConnPid, MRef, Host, Path) ->
+ StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
+ {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
+ {IsFin, Status}.
%% Tests.
check_status(Config) ->
- {_, Port} = lists:keyfind(port, 1, Config),
- {ok, Pid} = gun:open("localhost", Port),
Tests = [
{200, nofin, "localhost", "/"},
{200, nofin, "localhost", "/chunked"},
@@ -126,64 +121,33 @@ check_status(Config) ->
{400, fin, "localhost", "bad-path"},
{404, fin, "localhost", "/this/path/does/not/exist"}
],
+ {ConnPid, MRef} = gun_monitor_open(Config),
_ = [{Status, Fin, Host, Path} = begin
- {IsFin, Ret, _} = quick_get(Pid, Host, Path),
+ {IsFin, Ret} = quick_get(ConnPid, MRef, Host, Path),
{Ret, IsFin, Host, Path}
end || {Status, Fin, Host, Path} <- Tests],
- gun:close(Pid).
+ gun:close(ConnPid).
echo_body(Config) ->
- {_, Port} = lists:keyfind(port, 1, Config),
- {ok, Pid} = gun:open("localhost", Port),
- MRef = monitor(process, Pid),
+ {ConnPid, MRef} = gun_monitor_open(Config),
Body = << 0:800000 >>,
- StreamRef = gun:post(Pid, "/echo/body", [
- {<<"content-length">>, integer_to_list(byte_size(Body))},
+ StreamRef = gun:post(ConnPid, "/echo/body", [
{<<"content-type">>, "application/octet-stream"}
], Body),
- receive
- {'DOWN', MRef, _, _, Reason} ->
- error(Reason);
- {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
- ok
- after 1000 ->
- error(response_timeout)
- end,
- receive
- {'DOWN', MRef, _, _, Reason2} ->
- error({gun_data, Reason2});
- {gun_data, Pid, StreamRef, fin, Body} ->
- ok
- after 1000 ->
- error(data_timeout)
- end,
- gun:close(Pid).
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
+ {ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
+ gun:close(ConnPid).
echo_body_multi(Config) ->
- {_, Port} = lists:keyfind(port, 1, Config),
- {ok, Pid} = gun:open("localhost", Port),
- MRef = monitor(process, Pid),
+ {ConnPid, MRef} = gun_monitor_open(Config),
BodyChunk = << 0:80000 >>,
- StreamRef = gun:post(Pid, "/echo/body", [
+ StreamRef = gun:post(ConnPid, "/echo/body", [
+ %% @todo I'm still unhappy with this. It shouldn't be required...
{<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
{<<"content-type">>, "application/octet-stream"}
]),
- _ = [gun:data(Pid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
- gun:data(Pid, StreamRef, fin, BodyChunk),
- receive
- {'DOWN', MRef, _, _, Reason} ->
- error(Reason);
- {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
- ok
- after 1000 ->
- error(response_timeout)
- end,
- receive
- {'DOWN', MRef, _, _, Reason2} ->
- error({gun_data, Reason2});
- {gun_data, Pid, StreamRef, fin, << 0:800000 >>} ->
- ok
- after 1000 ->
- error(data_timeout)
- end,
- gun:close(Pid).
+ _ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
+ gun:data(ConnPid, StreamRef, fin, BodyChunk),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
+ {ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef),
+ gun:close(ConnPid).