aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile9
-rw-r--r--README.md6
-rw-r--r--doc/src/guide/getting_started.ezdoc2
-rw-r--r--doc/src/guide/handlers.ezdoc16
-rw-r--r--doc/src/guide/loop_handlers.ezdoc14
-rw-r--r--doc/src/guide/middlewares.ezdoc2
-rw-r--r--doc/src/guide/rest_handlers.ezdoc4
-rw-r--r--doc/src/guide/sub_protocols.ezdoc8
-rw-r--r--doc/src/guide/ws_handlers.ezdoc4
-rw-r--r--doc/src/manual/cowboy_websocket.ezdoc16
-rw-r--r--doc/src/specs/rfc7230_server.ezdoc8
-rw-r--r--erlang.mk649
-rw-r--r--examples/cookie/src/toppage_handler.erl2
-rw-r--r--rebar.config2
-rw-r--r--src/cowboy_clock.erl7
-rw-r--r--src/cowboy_http.erl1066
-rw-r--r--src/cowboy_protocol.erl63
-rw-r--r--src/cowboy_req.erl121
-rw-r--r--src/cowboy_rest.erl4
-rw-r--r--src/cowboy_router.erl2
-rw-r--r--src/cowboy_spdy.erl18
-rw-r--r--src/cowboy_websocket.erl633
-rw-r--r--test/cowboy_ct_hook.erl6
-rw-r--r--test/cowboy_error_h.erl145
-rw-r--r--test/cowboy_test.erl99
-rw-r--r--test/eunit_SUITE.erl22
-rw-r--r--test/handlers/asterisk_h.erl16
-rw-r--r--test/handlers/echo_h.erl16
-rw-r--r--test/handlers/hello_h.erl8
-rw-r--r--test/handlers/input_crash_h.erl2
-rw-r--r--test/http_SUITE.erl111
-rw-r--r--test/http_SUITE_data/http_chunked.erl2
-rw-r--r--test/http_SUITE_data/http_errors.erl4
-rw-r--r--test/http_SUITE_data/rest_missing_callbacks.erl4
-rw-r--r--test/http_SUITE_data/rest_resource_etags.erl4
-rw-r--r--test/loop_handler_SUITE.erl6
-rw-r--r--test/rfc7230_SUITE.erl1388
-rw-r--r--test/spdy_SUITE.erl28
-rw-r--r--test/ws_SUITE.erl11
39 files changed, 2394 insertions, 2134 deletions
diff --git a/Makefile b/Makefile
index 2863118..10629d3 100644
--- a/Makefile
+++ b/Makefile
@@ -4,15 +4,15 @@ 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_OPTS += -pa test -ct_hooks cowboy_ct_hook []
+CT_OPTS += -pa test -ct_hooks cowboy_ct_hook [] # -boot start_sasl
PLT_APPS = crypto public_key ssl
# Dependencies.
DEPS = cowlib ranch
+dep_cowlib = git https://github.com/ninenines/cowlib 1.3.0
+
TEST_DEPS = ct_helper gun
dep_ct_helper = git https://github.com/extend/ct_helper.git master
@@ -20,6 +20,9 @@ dep_ct_helper = git https://github.com/extend/ct_helper.git master
include erlang.mk
+ERLC_OPTS += +warn_export_all +warn_missing_spec +warn_untyped_record
+TEST_ERLC_OPTS += +'{parse_transform, eunit_autoexport}'
+
# Also dialyze the tests.
# DIALYZER_OPTS += --src -r test
diff --git a/README.md b/README.md
index 2919e63..64d9247 100644
--- a/README.md
+++ b/README.md
@@ -21,12 +21,12 @@ No parameterized module. No process dictionary. **Clean** Erlang code.
Sponsors
--------
-The SPDY implementation was sponsored by
-[LeoFS Cloud Storage](http://www.leofs.org).
-
The project is currently sponsored by
[Kato.im](https://kato.im).
+The SPDY implementation was sponsored by
+[LeoFS Cloud Storage](http://www.leofs.org).
+
Online documentation
--------------------
diff --git a/doc/src/guide/getting_started.ezdoc b/doc/src/guide/getting_started.ezdoc
index a959b45..deb7bf2 100644
--- a/doc/src/guide/getting_started.ezdoc
+++ b/doc/src/guide/getting_started.ezdoc
@@ -121,7 +121,7 @@ start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]}
]),
- cowboy:start_http(my_http_listener, 100, [{port, 8080}],
+ {ok, _} = cowboy:start_http(my_http_listener, 100, [{port, 8080}],
[{env, [{dispatch, Dispatch}]}]
),
hello_erlang_sup:start_link().
diff --git a/doc/src/guide/handlers.ezdoc b/doc/src/guide/handlers.ezdoc
index c0fb97e..9336488 100644
--- a/doc/src/guide/handlers.ezdoc
+++ b/doc/src/guide/handlers.ezdoc
@@ -14,8 +14,8 @@ defined during the ^"router configuration^routing^.
A handler that does nothing would look like this:
``` erlang
-init(Req, Opts) ->
- {ok, Req, Opts}.
+init(Req, _Opts) ->
+ {ok, Req, #state{}}.
```
Despite sending no reply, a `204 No Content` reply will be
@@ -25,11 +25,11 @@ sent for every request.
We need to use the Req object for sending a reply.
``` erlang
-init(Req, Opts) ->
+init(Req, _Opts) ->
Req2 = cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain">>}
], <<"Hello World!">>, Req),
- {ok, Req2, Opts}.
+ {ok, Req2, #state{}}.
```
As you can see we return a 3-tuple. `ok` means that the
@@ -60,15 +60,15 @@ return the name of the handler type you want to use. The
following snippet switches to a Websocket handler:
``` erlang
-init(Req, Opts) ->
- {cowboy_websocket, Req, Opts}.
+init(Req, _Opts) ->
+ {cowboy_websocket, Req, #state{}}.
```
You can also switch to your own custom handler type:
``` erlang
-init(Req, Opts) ->
- {my_handler_type, Req, Opts}.
+init(Req, _Opts) ->
+ {my_handler_type, Req, #state{}}.
```
How to implement a custom handler type is described in the
diff --git a/doc/src/guide/loop_handlers.ezdoc b/doc/src/guide/loop_handlers.ezdoc
index 879013b..47893a9 100644
--- a/doc/src/guide/loop_handlers.ezdoc
+++ b/doc/src/guide/loop_handlers.ezdoc
@@ -34,8 +34,8 @@ process enter hibernation until a message is received.
This snippet enables the loop handler.
``` erlang
-init(Req, Opts) ->
- {cowboy_loop, Req, Opts}.
+init(Req, _Opts) ->
+ {cowboy_loop, Req, #state{}}.
```
However it is largely recommended that you set a timeout
@@ -43,8 +43,8 @@ value. The next example sets a timeout value of 30s and
also makes the process hibernate.
``` erlang
-init(Req, Opts) ->
- {cowboy_loop, Req, Opts, 30000, hibernate}.
+init(Req, _Opts) ->
+ {cowboy_loop, Req, #state{}, 30000, hibernate}.
```
:: Receive loop
@@ -94,9 +94,9 @@ a chunk is sent every time a `chunk` message is received,
and the loop is stopped by sending an `eof` message.
``` erlang
-init(Req, Opts) ->
- Req2 = cowboy_req:chunked_reply(200, [], Req),
- {cowboy_loop, Req2, Opts}.
+init(Req, _Opts) ->
+ Req2 = cowboy_req:chunked_reply(200, [], Req),
+ {cowboy_loop, Req2, #state{}}.
info(eof, Req, State) ->
{stop, Req, State};
diff --git a/doc/src/guide/middlewares.ezdoc b/doc/src/guide/middlewares.ezdoc
index 35e9ea9..8b047d7 100644
--- a/doc/src/guide/middlewares.ezdoc
+++ b/doc/src/guide/middlewares.ezdoc
@@ -19,7 +19,7 @@ It is defined in the `cowboy_middleware` behavior.
This callback has two arguments. The first is the `Req` object.
The second is the environment.
-Middlewares can return one of four different values:
+Middlewares can return one of three different values:
* `{ok, Req, Env}` to continue the request processing
* `{suspend, Module, Function, Args}` to hibernate
diff --git a/doc/src/guide/rest_handlers.ezdoc b/doc/src/guide/rest_handlers.ezdoc
index e09790a..e6bb092 100644
--- a/doc/src/guide/rest_handlers.ezdoc
+++ b/doc/src/guide/rest_handlers.ezdoc
@@ -13,8 +13,8 @@ to all handlers. To use REST for the current request, this function
must return a `cowboy_rest` tuple.
``` erlang
-init(Req, Opts) ->
- {cowboy_rest, Req, Opts}.
+init(Req, _Opts) ->
+ {cowboy_rest, Req, #state{}}.
```
Cowboy will then switch to the REST protocol and start executing
diff --git a/doc/src/guide/sub_protocols.ezdoc b/doc/src/guide/sub_protocols.ezdoc
index d34f21e..54e57aa 100644
--- a/doc/src/guide/sub_protocols.ezdoc
+++ b/doc/src/guide/sub_protocols.ezdoc
@@ -14,8 +14,8 @@ the name of the sub protocol module. Everything past this point
is handled by the sub protocol.
``` erlang
-init(Req, Opts) ->
- {cowboy_websocket, Req, Opts}.
+init(Req, _Opts) ->
+ {cowboy_websocket, Req, #state{}}.
```
The return value may also have a `Timeout` value and/or the
@@ -28,8 +28,8 @@ protocol, sets the timeout value to 5 seconds and enables
hibernation:
``` erlang
-init(Req, Opts) ->
- {my_protocol, Req, Opts, 5000, hibernate}.
+init(Req, _Opts) ->
+ {my_protocol, Req, #state{}, 5000, hibernate}.
```
If a sub protocol does not make use of these options, it should
diff --git a/doc/src/guide/ws_handlers.ezdoc b/doc/src/guide/ws_handlers.ezdoc
index 4cdf526..a0cfc29 100644
--- a/doc/src/guide/ws_handlers.ezdoc
+++ b/doc/src/guide/ws_handlers.ezdoc
@@ -16,8 +16,8 @@ to all handlers. To establish a Websocket connection, this function
must return a `ws` tuple.
``` erlang
-init(Req, Opts) ->
- {cowboy_websocket, Req, Opts}.
+init(Req, _Opts) ->
+ {cowboy_websocket, Req, #state{}}.
```
Upon receiving this tuple, Cowboy will switch to the code
diff --git a/doc/src/manual/cowboy_websocket.ezdoc b/doc/src/manual/cowboy_websocket.ezdoc
index 8aac4cf..2519dba 100644
--- a/doc/src/manual/cowboy_websocket.ezdoc
+++ b/doc/src/manual/cowboy_websocket.ezdoc
@@ -22,18 +22,6 @@ Cowboy will terminate the process right after closing the
Websocket connection. This means that there is no real need to
perform any cleanup in the optional `terminate/3` callback.
-:: Types
-
-: close_code() = 1000..4999
-
-Reason for closing the connection.
-
-: frame() = close | ping | pong
- | {text | binary | close | ping | pong, iodata()}
- | {close, close_code(), iodata()}
-
-Frames that can be sent to the client.
-
:: Meta values
: websocket_compress
@@ -118,7 +106,7 @@ Types:
* InFrame = {text | binary | ping | pong, binary()}
* Req = cowboy_req:req()
* State = any()
-* OutFrame = frame()
+* OutFrame = cow_ws:frame()
Handle the data received from the Websocket connection.
@@ -145,7 +133,7 @@ Types:
* Info = any()
* Req = cowboy_req:req()
* State = any()
-* OutFrame = frame()
+* OutFrame = cow_ws:frame()
Handle the Erlang message received.
diff --git a/doc/src/specs/rfc7230_server.ezdoc b/doc/src/specs/rfc7230_server.ezdoc
index 9ccac94..2497f56 100644
--- a/doc/src/specs/rfc7230_server.ezdoc
+++ b/doc/src/specs/rfc7230_server.ezdoc
@@ -48,7 +48,7 @@ outcome of the processing.
The time the request (request line and headers) takes to be
received by the server must be limited and subject to configuration.
A server must wait at least 5 seconds before dropping the connection.
-A 418 status code must be sent if the request line was received
+A 408 status code must be sent if the request line was received
fully when the timeout is triggered.
An HTTP/1.1 server must understand any valid HTTP/1.0 request,
@@ -105,7 +105,7 @@ forms are specific to the CONNECT and site-wide OPTIONS method,
respectively. (RFC7230 5.3.2)
The fragment part of the target URI is not sent. It must be
-ignrored by a server receiving it. (RFC7230 5.1)
+ignored by a server receiving it. (RFC7230 5.1)
```
request-target = origin-form / absolute-form / authority-form / asterisk-form
@@ -362,10 +362,6 @@ version = "HTTP/1.0" / "HTTP/1.1"
Any version number other than HTTP/1.0 or HTTP/1.1 must be
rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)
-A request that has whitespace different than CRLF following the
-version must be rejected with a 400 status code and the closing
-of the connection. (RFC7230 3.1.1)
-
A request that has any whitespace or characters different than
CRLF following the version must be rejected with a 400 status
code and the closing of the connection. (RFC7230 3.1.1)
diff --git a/erlang.mk b/erlang.mk
index 9bffffa..e6833bc 100644
--- a/erlang.mk
+++ b/erlang.mk
@@ -1,4 +1,4 @@
-# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2013-2015, 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
@@ -28,12 +28,30 @@ V ?= 0
gen_verbose_0 = @echo " GEN " $@;
gen_verbose = $(gen_verbose_$(V))
+# "erl" command.
+
+ERL = erl +A0 -noinput -boot start_clean
+
# Core targets.
-all:: deps app rel
+ifneq ($(words $(MAKECMDGOALS)),1)
+.NOTPARALLEL:
+endif
-clean::
+all:: deps
+ @$(MAKE) --no-print-directory app
+ @$(MAKE) --no-print-directory rel
+
+# Noop to avoid a Make warning when there's nothing to do.
+rel::
+ @echo -n
+
+clean:: clean-crashdump
+
+clean-crashdump:
+ifneq ($(wildcard erl_crash.dump),)
$(gen_verbose) rm -f erl_crash.dump
+endif
distclean:: clean
@@ -42,7 +60,7 @@ help::
"erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
"Copyright (c) 2013-2014 Loïc Hoguin <[email protected]>" \
"" \
- "Usage: [V=1] make [target]" \
+ "Usage: [V=1] make [-jNUM] [target]" \
"" \
"Core targets:" \
" all Run deps, app and rel targets in that order" \
@@ -58,7 +76,8 @@ help::
"The target clean only removes files that are commonly removed." \
"Dependencies and releases are left untouched." \
"" \
- "Setting V=1 when calling make enables verbose mode."
+ "Setting V=1 when calling make enables verbose mode." \
+ "Parallel execution is supported through the -j Make flag."
# Core functions.
@@ -68,7 +87,7 @@ define core_http_get
endef
else
define core_http_get
- erl -noshell -eval 'ssl:start(), inets:start(), case httpc:request(get, {"$(2)", []}, [{autoredirect, true}], []) of {ok, {{_, 200, _}, _, Body}} -> case file:write_file("$(1)", Body) of ok -> ok; {error, R1} -> halt(R1) end; {error, R2} -> halt(R2) end, halt(0).'
+ $(ERL) -eval 'ssl:start(), inets:start(), case httpc:request(get, {"$(2)", []}, [{autoredirect, true}], []) of {ok, {{_, 200, _}, _, Body}} -> case file:write_file("$(1)", Body) of ok -> ok; {error, R1} -> halt(R1) end; {error, R2} -> halt(R2) end, halt(0).'
endef
endif
@@ -84,13 +103,16 @@ erlang-mk:
cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
rm -rf $(ERLANG_MK_BUILD_DIR)
-# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: distclean-deps distclean-pkg pkg-list pkg-search
# Configuration.
+AUTOPATCH ?= edown gen_leader gproc
+export AUTOPATCH
+
DEPS_DIR ?= $(CURDIR)/deps
export DEPS_DIR
@@ -128,6 +150,41 @@ distclean:: distclean-deps distclean-pkg
# Deps related targets.
+define dep_autopatch
+ $(ERL) -eval " \
+DepDir = \"$(DEPS_DIR)/$(1)/\", \
+fun() -> \
+ {ok, Conf} = file:consult(DepDir ++ \"rebar.config\"), \
+ File = case lists:keyfind(deps, 1, Conf) of false -> []; {_, Deps} -> \
+ [begin {Method, Repo, Commit} = case Repos of \
+ {git, R} -> {git, R, master}; \
+ {M, R, {branch, C}} -> {M, R, C}; \
+ {M, R, {tag, C}} -> {M, R, C}; \
+ {M, R, C} -> {M, R, C} \
+ end, \
+ io_lib:format(\"DEPS += ~s\ndep_~s = ~s ~s ~s~n\", [Name, Name, Method, Repo, Commit]) \
+ end || {Name, _, Repos} <- Deps] \
+ end, \
+ ok = file:write_file(\"$(DEPS_DIR)/$(1)/Makefile\", [\"ERLC_OPTS = +debug_info\n\n\", File, \"\ninclude erlang.mk\"]) \
+end(), \
+AppSrcOut = \"$(DEPS_DIR)/$(1)/src/$(1).app.src\", \
+AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> \"$(DEPS_DIR)/$(1)/ebin/$(1).app\"; true -> AppSrcOut end, \
+fun() -> \
+ {ok, [{application, $(1), L}]} = file:consult(AppSrcIn), \
+ L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end, \
+ L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, \"git\"}); _ -> L2 end, \
+ ok = file:write_file(AppSrcOut, io_lib:format(\"~p.~n\", [{application, $(1), L3}])) \
+end(), \
+case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end, \
+halt()."
+endef
+
+ifeq ($(V),0)
+define dep_autopatch_verbose
+ @echo " PATCH " $(1);
+endef
+endif
+
define dep_fetch
if [ "$$$$VS" = "git" ]; then \
git clone -n -- $$$$REPO $(DEPS_DIR)/$(1); \
@@ -135,6 +192,8 @@ define dep_fetch
elif [ "$$$$VS" = "hg" ]; then \
hg clone -U $$$$REPO $(DEPS_DIR)/$(1); \
cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
+ elif [ "$$$$VS" = "svn" ]; then \
+ svn checkout $$$$REPO $(DEPS_DIR)/$(1); \
else \
echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
exit 78; \
@@ -157,6 +216,15 @@ else
COMMIT=$(word 3,$(dep_$(1))); \
$(call dep_fetch,$(1))
endif
+ifneq ($(filter $(1),$(AUTOPATCH)),)
+ $(call dep_autopatch_verbose,$(1)) if [ -f $(DEPS_DIR)/$(1)/rebar.config ]; then \
+ $(call dep_autopatch,$(1)); \
+ cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk; \
+ elif [ ! -f $(DEPS_DIR)/$(1)/Makefile ]; then \
+ echo "ERLC_OPTS = +debug_info\ninclude erlang.mk" > $(DEPS_DIR)/$(1)/Makefile; \
+ cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk; \
+ fi
+endif
endef
$(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
@@ -199,32 +267,48 @@ help::
" pkg-list List all known packages" \
" pkg-search q=STRING Search for STRING in the package index"
-# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: clean-app
# Configuration.
-ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
- +warn_shadow_vars +warn_obsolete_guard # +bin_opt_info +warn_missing_spec
+ERLC_OPTS ?= -Werror +debug_info +warn_export_vars +warn_shadow_vars \
+ +warn_obsolete_guard # +bin_opt_info +warn_export_all +warn_missing_spec
COMPILE_FIRST ?=
COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
+ERLC_EXCLUDE ?=
+ERLC_EXCLUDE_PATHS = $(addprefix src/,$(addsuffix .erl,$(ERLC_EXCLUDE)))
+
+ERLC_MIB_OPTS ?=
+COMPILE_MIB_FIRST ?=
+COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST)))
# Verbosity.
appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
appsrc_verbose = $(appsrc_verbose_$(V))
-erlc_verbose_0 = @echo " ERLC " $(filter %.erl %.core,$(?F));
+erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
+ $(filter %.erl %.core,$(?F)));
erlc_verbose = $(erlc_verbose_$(V))
xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
xyrl_verbose = $(xyrl_verbose_$(V))
-# Core targets.
+mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
+mib_verbose = $(mib_verbose_$(V))
+
+# Targets.
+
+ifeq ($(wildcard ebin/test),)
+app:: app-build
+else
+app:: clean app-build
+endif
-app:: erlc-include ebin/$(PROJECT).app
+app-build: erlc-include ebin/$(PROJECT).app
$(eval MODULES := $(shell find ebin -type f -name \*.beam \
| sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
@if [ -z "$$(grep -E '^[^%]*{modules,' src/$(PROJECT).app.src)" ]; then \
@@ -237,9 +321,15 @@ app:: erlc-include ebin/$(PROJECT).app
| sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
> ebin/$(PROJECT).app
+erlc-include:
+ -@if [ -d ebin/ ]; then \
+ find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
+ fi
+
define compile_erl
$(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
- -pa ebin/ -I include/ $(COMPILE_FIRST_PATHS) $(1)
+ -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),\
+ $(COMPILE_FIRST_PATHS) $(1))
endef
define compile_xyrl
@@ -248,10 +338,22 @@ define compile_xyrl
@rm ebin/*.erl
endef
+define compile_mib
+ $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ \
+ -I priv/mibs/ $(COMPILE_MIB_FIRST_PATHS) $(1)
+ $(mib_verbose) erlc -o include/ -- priv/mibs/*.bin
+endef
+
ifneq ($(wildcard src/),)
ebin/$(PROJECT).app::
@mkdir -p ebin/
+ifneq ($(wildcard mibs/),)
+ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
+ @mkdir -p priv/mibs/ include
+ $(if $(strip $?),$(call compile_mib,$?))
+endif
+
ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl) \
$(shell find src -type f -name \*.core)
$(if $(strip $?),$(call compile_erl,$?))
@@ -263,17 +365,56 @@ endif
clean:: clean-app
-# Extra targets.
+clean-app:
+ $(gen_verbose) rm -rf ebin/ priv/mibs/ \
+ $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))
-erlc-include:
- -@if [ -d ebin/ ]; then \
- find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
- fi
+# Copyright (c) 2015, Loïc Hoguin <[email protected]>
+# This file is part of erlang.mk and subject to the terms of the ISC License.
-clean-app:
- $(gen_verbose) rm -rf ebin/
+.PHONY: test-deps test-dir test-build clean-test-dir
+
+# Configuration.
+
+TEST_DIR ?= test
+
+ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
+
+TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
+TEST_ERLC_OPTS += -DTEST=1
+
+# Targets.
+
+$(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
+
+test-deps: $(ALL_TEST_DEPS_DIRS)
+ @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
+
+ifneq ($(strip $(TEST_DIR)),)
+test-dir:
+ $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o $(TEST_DIR) \
+ $(wildcard $(TEST_DIR)/*.erl $(TEST_DIR)/*/*.erl) -pa ebin/
+endif
+
+ifeq ($(wildcard ebin/test),)
+test-build: ERLC_OPTS=$(TEST_ERLC_OPTS)
+test-build: clean deps test-deps
+ @$(MAKE) --no-print-directory app-build test-dir ERLC_OPTS="$(TEST_ERLC_OPTS)"
+ $(gen_verbose) touch ebin/test
+else
+test-build: ERLC_OPTS=$(TEST_ERLC_OPTS)
+test-build: deps test-deps
+ @$(MAKE) --no-print-directory app-build test-dir ERLC_OPTS="$(TEST_ERLC_OPTS)"
+endif
+
+clean:: clean-test-dir
+
+clean-test-dir:
+ifneq ($(wildcard $(TEST_DIR)/*.beam),)
+ $(gen_verbose) rm -f $(TEST_DIR)/*.beam
+endif
-# Copyright (c) 2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2014-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates
@@ -392,6 +533,56 @@ tpl_gen_server = "-module($(n))." \
"" \
"code_change(_OldVsn, State, _Extra) ->" \
" {ok, State}."
+tpl_gen_fsm = "-module($(n))." \
+ "-behaviour(gen_fsm)." \
+ "" \
+ "%% API." \
+ "-export([start_link/0])." \
+ "" \
+ "%% gen_fsm." \
+ "-export([init/1])." \
+ "-export([state_name/2])." \
+ "-export([handle_event/3])." \
+ "-export([state_name/3])." \
+ "-export([handle_sync_event/4])." \
+ "-export([handle_info/3])." \
+ "-export([terminate/3])." \
+ "-export([code_change/4])." \
+ "" \
+ "-record(state, {" \
+ "})." \
+ "" \
+ "%% API." \
+ "" \
+ "-spec start_link() -> {ok, pid()}." \
+ "start_link() ->" \
+ " gen_fsm:start_link(?MODULE, [], [])." \
+ "" \
+ "%% gen_fsm." \
+ "" \
+ "init([]) ->" \
+ " {ok, state_name, \#state{}}." \
+ "" \
+ "state_name(_Event, StateData) ->" \
+ " {next_state, state_name, StateData}." \
+ "" \
+ "handle_event(_Event, StateName, StateData) ->" \
+ " {next_state, StateName, StateData}." \
+ "" \
+ "state_name(_Event, _From, StateData) ->" \
+ " {reply, ignored, state_name, StateData}." \
+ "" \
+ "handle_sync_event(_Event, _From, StateName, StateData) ->" \
+ " {reply, ignored, StateName, StateData}." \
+ "" \
+ "handle_info(_Info, StateName, StateData) ->" \
+ " {next_state, StateName, StateData}." \
+ "" \
+ "terminate(_Reason, _StateName, _StateData) ->" \
+ " ok." \
+ "" \
+ "code_change(_OldVsn, StateName, StateData, _Extra) ->" \
+ " {ok, StateName, StateData}."
tpl_cowboy_http = "-module($(n))." \
"-behaviour(cowboy_http_handler)." \
"" \
@@ -551,85 +742,169 @@ endif
list-templates:
@echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))
-# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2014-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
-.PHONY: build-ct-deps build-ct-suites tests-ct clean-ct distclean-ct
+.PHONY: clean-c_src distclean-c_src-env
+# todo
+
+# Configuration.
+
+C_SRC_DIR = $(CURDIR)/c_src
+C_SRC_ENV ?= $(C_SRC_DIR)/env.mk
+C_SRC_OUTPUT ?= $(CURDIR)/priv/$(PROJECT).so
+
+# System type and C compiler/flags.
+
+UNAME_SYS := $(shell uname -s)
+ifeq ($(UNAME_SYS), Darwin)
+ CC ?= cc
+ CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes
+ CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
+ LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
+else ifeq ($(UNAME_SYS), FreeBSD)
+ CC ?= cc
+ CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
+ CXXFLAGS ?= -O3 -finline-functions -Wall
+else ifeq ($(UNAME_SYS), Linux)
+ CC ?= gcc
+ CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
+ CXXFLAGS ?= -O3 -finline-functions -Wall
+endif
+
+CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
+CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
+
+LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
+LDFLAGS += -shared
+
+# Verbosity.
+
+c_verbose_0 = @echo " C " $(?F);
+c_verbose = $(c_verbose_$(V))
+
+cpp_verbose_0 = @echo " CPP " $(?F);
+cpp_verbose = $(cpp_verbose_$(V))
+
+link_verbose_0 = @echo " LD " $(@F);
+link_verbose = $(link_verbose_$(V))
+
+# Targets.
+
+ifeq ($(wildcard $(C_SRC_DIR)),)
+else ifneq ($(wildcard $(C_SRC_DIR)/Makefile),)
+app::
+ $(MAKE) -C $(C_SRC_DIR)
+
+clean::
+ $(MAKE) -C $(C_SRC_DIR) clean
+
+else
+SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
+OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
+
+COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
+COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
+
+app:: $(C_SRC_ENV) $(C_SRC_OUTPUT)
+
+$(C_SRC_OUTPUT): $(OBJECTS)
+ @mkdir -p priv/
+ $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)
+
+%.o: %.c
+ $(COMPILE_C) $(OUTPUT_OPTION) $<
+
+%.o: %.cc
+ $(COMPILE_CPP) $(OUTPUT_OPTION) $<
+
+%.o: %.C
+ $(COMPILE_CPP) $(OUTPUT_OPTION) $<
+
+%.o: %.cpp
+ $(COMPILE_CPP) $(OUTPUT_OPTION) $<
+
+$(C_SRC_ENV):
+ @$(ERL) -eval "file:write_file(\"$(C_SRC_ENV)\", \
+ io_lib:format( \
+ \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \
+ \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \
+ \"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \
+ [code:root_dir(), erlang:system_info(version), \
+ code:lib_dir(erl_interface, include), \
+ code:lib_dir(erl_interface, lib)])), \
+ halt()."
+
+clean:: clean-c_src
+
+clean-c_src:
+ $(gen_verbose) rm -f $(C_SRC_OUTPUT) $(OBJECTS)
+
+distclean:: distclean-c_src-env
+
+distclean-c_src-env:
+ $(gen_verbose) rm -f $(C_SRC_ENV)
+
+-include $(C_SRC_ENV)
+endif
+
+# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
+# This file is part of erlang.mk and subject to the terms of the ISC License.
+
+.PHONY: ct distclean-ct
# Configuration.
CT_OPTS ?=
-ifneq ($(wildcard test/),)
- CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find test -type f -name \*_SUITE.erl -exec basename {} \;)))
+ifneq ($(wildcard $(TEST_DIR)),)
+ CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find $(TEST_DIR) -type f -name \*_SUITE.erl -exec basename {} \;)))
else
CT_SUITES ?=
endif
-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}'
-
# Core targets.
-tests:: tests-ct
-
-clean:: clean-ct
+tests:: ct
distclean:: distclean-ct
help::
@printf "%s\n" "" \
+ "Common_test targets:" \
+ " ct Run all the common_test suites for this project" \
+ "" \
"All your common_test suites have their associated targets." \
"A suite named http_SUITE can be ran using the ct-http target."
# Plugin-specific targets.
-ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
-
CT_RUN = ct_run \
-no_auto_compile \
- -noshell \
- -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
- -dir test \
+ -noinput \
+ -pa ebin $(DEPS_DIR)/*/ebin \
+ -dir $(TEST_DIR) \
-logdir logs
-$(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
-
-build-ct-deps: $(ALL_TEST_DEPS_DIRS)
- @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
-
-build-ct-suites: build-ct-deps
- $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o test/ \
- $(wildcard test/*.erl test/*/*.erl) -pa ebin/
-
-tests-ct: ERLC_OPTS = $(TEST_ERLC_OPTS)
-tests-ct: clean deps app build-ct-suites
- @if [ -d "test" ] ; \
- then \
- mkdir -p logs/ ; \
- $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) $(CT_OPTS) ; \
- fi
- $(gen_verbose) rm -f test/*.beam
+ifeq ($(CT_SUITES),)
+ct:
+else
+ct: test-build
+ @mkdir -p logs/
+ $(gen_verbose) $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) $(CT_OPTS)
+endif
define ct_suite_target
-ct-$(1): ERLC_OPTS = $(TEST_ERLC_OPTS)
-ct-$(1): clean deps app build-ct-suites
- @if [ -d "test" ] ; \
- then \
- mkdir -p logs/ ; \
- $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) $(CT_OPTS) ; \
- fi
- $(gen_verbose) rm -f test/*.beam
+ct-$(1): test-build
+ @mkdir -p logs/
+ $(gen_verbose) $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) $(CT_OPTS)
endef
$(foreach test,$(CT_SUITES),$(eval $(call ct_suite_target,$(test))))
-clean-ct:
- $(gen_verbose) rm -rf test/*.beam
-
distclean-ct:
$(gen_verbose) rm -rf logs/
-# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: plt distclean-plt dialyze
@@ -671,9 +946,82 @@ dialyze: $(DIALYZER_PLT)
endif
@dialyzer --no_native $(DIALYZER_DIRS) $(DIALYZER_OPTS)
-# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
+# Copyright (c) 2015, Viktor Söderqvist <[email protected]>
+# This file is part of erlang.mk and subject to the terms of the ISC License.
+
+.PHONY: distclean-edoc build-doc-deps
+
+# Configuration.
+
+EDOC_OPTS ?=
+
+# Core targets.
+
+docs:: distclean-edoc build-doc-deps
+ $(gen_verbose) $(ERL) -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), halt().'
+
+distclean:: distclean-edoc
+
+# Plugin-specific targets.
+
+DOC_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DOC_DEPS))
+
+$(foreach dep,$(DOC_DEPS),$(eval $(call dep_target,$(dep))))
+
+build-doc-deps: $(DOC_DEPS_DIRS)
+ @for dep in $(DOC_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
+
+distclean-edoc:
+ $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
+
+# Copyright (c) 2014, Juan Facorro <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
+.PHONY: elvis distclean-elvis
+
+# Configuration.
+
+ELVIS_CONFIG ?= $(CURDIR)/elvis.config
+
+ELVIS ?= $(CURDIR)/elvis
+export ELVIS
+
+ELVIS_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis
+ELVIS_CONFIG_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis.config
+ELVIS_OPTS ?=
+
+# Core targets.
+
+help::
+ @printf "%s\n" "" \
+ "Elvis targets:" \
+ " elvis Run Elvis using the local elvis.config or download the default otherwise"
+
+distclean:: distclean-elvis
+
+# Plugin-specific targets.
+
+$(ELVIS):
+ @$(call core_http_get,$(ELVIS),$(ELVIS_URL))
+ @chmod +x $(ELVIS)
+
+$(ELVIS_CONFIG):
+ @$(call core_http_get,$(ELVIS_CONFIG),$(ELVIS_CONFIG_URL))
+
+elvis: $(ELVIS) $(ELVIS_CONFIG)
+ @$(ELVIS) rock -c $(ELVIS_CONFIG) $(ELVIS_OPTS)
+
+distclean-elvis:
+ $(gen_verbose) rm -rf $(ELVIS)
+
+# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
+# This file is part of erlang.mk and subject to the terms of the ISC License.
+
+# Configuration.
+
+DTL_FULL_PATH ?= 0
+
# Verbosity.
dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
@@ -682,14 +1030,16 @@ dtl_verbose = $(dtl_verbose_$(V))
# Core targets.
define compile_erlydtl
- $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
+ $(dtl_verbose) $(ERL) -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
Compile = fun(F) -> \
- Module = list_to_atom( \
- string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
- erlydtl:compile(F, Module, [{out_dir, "ebin/"}]) \
+ S = fun (1) -> re:replace(filename:rootname(string:sub_string(F, 11), ".dtl"), "/", "_", [{return, list}, global]); \
+ (0) -> filename:basename(F, ".dtl") \
+ end, \
+ Module = list_to_atom(string:to_lower(S($(DTL_FULL_PATH))) ++ "_dtl"), \
+ {ok, _} = erlydtl:compile(F, Module, [{out_dir, "ebin/"}, return_errors, {doc_root, "templates"}]) \
end, \
_ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
- init:stop()'
+ halt().'
endef
ifneq ($(wildcard src/),)
@@ -697,7 +1047,124 @@ ebin/$(PROJECT).app:: $(shell find templates -type f -name \*.dtl 2>/dev/null)
$(if $(strip $?),$(call compile_erlydtl,$?))
endif
-# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]>
+# Copyright (c) 2014 Dave Cottlehuber <[email protected]>
+# This file is part of erlang.mk and subject to the terms of the ISC License.
+
+.PHONY: distclean-escript escript
+
+# Configuration.
+
+ESCRIPT_NAME ?= $(PROJECT)
+ESCRIPT_COMMENT ?= This is an -*- erlang -*- file
+
+ESCRIPT_BEAMS ?= "ebin/*", "deps/*/ebin/*"
+ESCRIPT_SYS_CONFIG ?= "rel/sys.config"
+ESCRIPT_EMU_ARGS ?= -pa . \
+ -sasl errlog_type error \
+ -escript main $(ESCRIPT_NAME)
+ESCRIPT_SHEBANG ?= /usr/bin/env escript
+ESCRIPT_STATIC ?= "deps/*/priv/**", "priv/**"
+
+# Core targets.
+
+distclean:: distclean-escript
+
+help::
+ @printf "%s\n" "" \
+ "Escript targets:" \
+ " escript Build an executable escript archive" \
+
+# Plugin-specific targets.
+
+# Based on https://github.com/synrc/mad/blob/master/src/mad_bundle.erl
+# Copyright (c) 2013 Maxim Sokhatsky, Synrc Research Center
+# Modified MIT License, https://github.com/synrc/mad/blob/master/LICENSE :
+# Software may only be used for the great good and the true happiness of all
+# sentient beings.
+
+define ESCRIPT_RAW
+'Read = fun(F) -> {ok, B} = file:read_file(filename:absname(F)), B end,'\
+'Files = fun(L) -> A = lists:concat([filelib:wildcard(X)||X<- L ]),'\
+' [F || F <- A, not filelib:is_dir(F) ] end,'\
+'Squash = fun(L) -> [{filename:basename(F), Read(F) } || F <- L ] end,'\
+'Zip = fun(A, L) -> {ok,{_,Z}} = zip:create(A, L, [{compress,all},memory]), Z end,'\
+'Ez = fun(Escript) ->'\
+' Static = Files([$(ESCRIPT_STATIC)]),'\
+' Beams = Squash(Files([$(ESCRIPT_BEAMS), $(ESCRIPT_SYS_CONFIG)])),'\
+' Archive = Beams ++ [{ "static.gz", Zip("static.gz", Static)}],'\
+' escript:create(Escript, [ $(ESCRIPT_OPTIONS)'\
+' {archive, Archive, [memory]},'\
+' {shebang, "$(ESCRIPT_SHEBANG)"},'\
+' {comment, "$(ESCRIPT_COMMENT)"},'\
+' {emu_args, " $(ESCRIPT_EMU_ARGS)"}'\
+' ]),'\
+' file:change_mode(Escript, 8#755)'\
+'end,'\
+'Ez("$(ESCRIPT_NAME)"),'\
+'halt().'
+endef
+
+ESCRIPT_COMMAND = $(subst ' ',,$(ESCRIPT_RAW))
+
+escript:: distclean-escript deps app
+ $(gen_verbose) $(ERL) -eval $(ESCRIPT_COMMAND)
+
+distclean-escript:
+ $(gen_verbose) rm -f $(ESCRIPT_NAME)
+
+# Copyright (c) 2014, Enrique Fernandez <[email protected]>
+# Copyright (c) 2015, Loïc Hoguin <[email protected]>
+# This file is contributed to erlang.mk and subject to the terms of the ISC License.
+
+.PHONY: eunit
+
+# Configuration
+
+ifeq ($(strip $(TEST_DIR)),)
+TAGGED_EUNIT_TESTS = {dir,"ebin"}
+else
+ifeq ($(wildcard $(TEST_DIR)),)
+TAGGED_EUNIT_TESTS = {dir,"ebin"}
+else
+# All modules in TEST_DIR
+TEST_DIR_MODS = $(notdir $(basename $(shell find $(TEST_DIR) -type f -name *.beam)))
+# All modules in 'ebin'
+EUNIT_EBIN_MODS = $(notdir $(basename $(shell find ebin -type f -name *.beam)))
+# Only those modules in TEST_DIR with no matching module in 'ebin'.
+# This is done to avoid some tests being executed twice.
+EUNIT_MODS = $(filter-out $(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(TEST_DIR_MODS))
+TAGGED_EUNIT_TESTS = {dir,"ebin"} $(foreach mod,$(EUNIT_MODS),$(shell echo $(mod) | sed -e 's/\(.*\)/{module,\1}/g'))
+endif
+endif
+
+EUNIT_OPTS ?=
+
+# Utility functions
+
+define str-join
+ $(shell echo '$(strip $(1))' | sed -e "s/ /,/g")
+endef
+
+# Core targets.
+
+tests:: eunit
+
+help::
+ @printf "%s\n" "" \
+ "EUnit targets:" \
+ " eunit Run all the EUnit tests for this project"
+
+# Plugin-specific targets.
+
+EUNIT_RUN = $(ERL) \
+ -pa $(TEST_DIR) $(DEPS_DIR)/*/ebin \
+ -pz ebin \
+ -eval 'case eunit:test([$(call str-join,$(TAGGED_EUNIT_TESTS))], [$(EUNIT_OPTS)]) of ok -> halt(0); error -> halt(1) end.'
+
+eunit: test-build
+ $(gen_verbose) $(EUNIT_RUN)
+
+# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: relx-rel distclean-relx-rel distclean-relx
@@ -709,7 +1176,7 @@ RELX_CONFIG ?= $(CURDIR)/relx.config
RELX ?= $(CURDIR)/relx
export RELX
-RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.0.2/relx
+RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.2.0/relx
RELX_OPTS ?=
RELX_OUTPUT_DIR ?= _rel
@@ -753,7 +1220,7 @@ distclean-relx:
# Configuration.
-SHELL_PATH ?= -pa ../$(PROJECT)/ebin $(DEPS_DIR)/*/ebin
+SHELL_PATH ?= -pa $(CURDIR)/ebin $(DEPS_DIR)/*/ebin
SHELL_OPTS ?=
ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
@@ -774,3 +1241,35 @@ build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
shell: build-shell-deps
$(gen_verbose) erl $(SHELL_PATH) $(SHELL_OPTS)
+
+# Copyright (c) 2015, Loïc Hoguin <[email protected]>
+# This file is part of erlang.mk and subject to the terms of the ISC License.
+
+ifneq ($(wildcard $(DEPS_DIR)/triq),)
+.PHONY: triq
+
+# Targets.
+
+tests:: triq
+
+define triq_run
+$(ERL) -pa $(CURDIR)/ebin $(DEPS_DIR)/*/ebin \
+ -eval "try $(1) of true -> halt(0); _ -> halt(1) catch error:undef -> io:format(\"Undefined property or module~n\"), halt() end."
+endef
+
+ifdef t
+ifeq (,$(findstring :,$(t)))
+triq: test-build
+ @$(call triq_run,triq:check($(t)))
+else
+triq: test-build
+ @echo Testing $(t)/0
+ @$(call triq_run,triq:check($(t)()))
+endif
+else
+triq: test-build
+ $(eval MODULES := $(shell find ebin -type f -name \*.beam \
+ | sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
+ $(gen_verbose) $(call triq_run,[true] =:= lists:usort([triq:check(M) || M <- [$(MODULES)]]))
+endif
+endif
diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl
index 7331906..745f626 100644
--- a/examples/cookie/src/toppage_handler.erl
+++ b/examples/cookie/src/toppage_handler.erl
@@ -10,7 +10,7 @@ init(Req, Opts) ->
Req2 = cowboy_req:set_resp_cookie(
<<"server">>, NewValue, [{path, <<"/">>}], Req),
#{client := ClientCookie, server := ServerCookie}
- = cowboy_req:match_cookies([client, server], Req2),
+ = cowboy_req:match_cookies([{client, [], <<>>}, {server, [], <<>>}], Req2),
{ok, Body} = toppage_dtl:render([
{client, ClientCookie},
{server, ServerCookie}
diff --git a/rebar.config b/rebar.config
index 4018be8..04b6f41 100644
--- a/rebar.config
+++ b/rebar.config
@@ -1,4 +1,4 @@
{deps, [
- {cowlib, ".*", {git, "git://github.com/ninenines/cowlib.git", "1.0.0"}},
+ {cowlib, ".*", {git, "git://github.com/ninenines/cowlib.git", "1.3.0"}},
{ranch, ".*", {git, "git://github.com/ninenines/ranch.git", "1.0.0"}}
]}.
diff --git a/src/cowboy_clock.erl b/src/cowboy_clock.erl
index 37767e1..c81b486 100644
--- a/src/cowboy_clock.erl
+++ b/src/cowboy_clock.erl
@@ -65,7 +65,7 @@ init([]) ->
named_table, {read_concurrency, true}]),
T = erlang:universaltime(),
B = update_rfc1123(<<>>, undefined, T),
- {ok, TRef} = timer:send_interval(1000, update),
+ TRef = erlang:send_after(1000, self(), update),
ets:insert(?MODULE, {rfc1123, B}),
{ok, #state{universaltime=T, rfc1123=B, tref=TRef}}.
@@ -83,10 +83,13 @@ handle_cast(_Msg, State) ->
{noreply, State}.
-spec handle_info(any(), State) -> {noreply, State} when State::#state{}.
-handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef}) ->
+handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef0}) ->
+ %% Cancel the timer in case an external process sent an update message.
+ erlang:cancel_timer(TRef0),
T = erlang:universaltime(),
B2 = update_rfc1123(B1, Prev, T),
ets:insert(?MODULE, {rfc1123, B2}),
+ TRef = erlang:send_after(1000, self(), update),
{noreply, #state{universaltime=T, rfc1123=B2, tref=TRef}};
handle_info(_Info, State) ->
{noreply, State}.
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
deleted file mode 100644
index 177787f..0000000
--- a/src/cowboy_http.erl
+++ /dev/null
@@ -1,1066 +0,0 @@
-%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]>
-%% Copyright (c) 2011, Anthony Ramine <[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.
-
-%% Deprecated HTTP parsing API.
--module(cowboy_http).
-
-%% Parsing.
--export([list/2]).
--export([nonempty_list/2]).
--export([content_type/1]).
--export([media_range/2]).
--export([conneg/2]).
--export([language_range/2]).
--export([entity_tag_match/1]).
--export([expectation/2]).
--export([params/2]).
--export([http_date/1]).
--export([rfc1123_date/1]).
--export([rfc850_date/1]).
--export([asctime_date/1]).
--export([whitespace/2]).
--export([digits/1]).
--export([token/2]).
--export([token_ci/2]).
--export([quoted_string/2]).
--export([authorization/2]).
--export([range/1]).
--export([parameterized_tokens/1]).
-
-%% Decoding.
--export([ce_identity/1]).
-
-%% Parsing.
-
--spec nonempty_list(binary(), fun()) -> [any(), ...] | {error, badarg}.
-nonempty_list(Data, Fun) ->
- case list(Data, Fun, []) of
- {error, badarg} -> {error, badarg};
- [] -> {error, badarg};
- L -> lists:reverse(L)
- end.
-
--spec list(binary(), fun()) -> list() | {error, badarg}.
-list(Data, Fun) ->
- case list(Data, Fun, []) of
- {error, badarg} -> {error, badarg};
- L -> lists:reverse(L)
- end.
-
--spec list(binary(), fun(), [binary()]) -> [any()] | {error, badarg}.
-%% From the RFC:
-%% <blockquote>Wherever this construct is used, null elements are allowed,
-%% but do not contribute to the count of elements present.
-%% That is, "(element), , (element) " is permitted, but counts
-%% as only two elements. Therefore, where at least one element is required,
-%% at least one non-null element MUST be present.</blockquote>
-list(Data, Fun, Acc) ->
- whitespace(Data,
- fun (<<>>) -> Acc;
- (<< $,, Rest/binary >>) -> list(Rest, Fun, Acc);
- (Rest) -> Fun(Rest,
- fun (D, I) -> whitespace(D,
- fun (<<>>) -> [I|Acc];
- (<< $,, R/binary >>) -> list(R, Fun, [I|Acc]);
- (_Any) -> {error, badarg}
- end)
- end)
- end).
-
-%% We lowercase the charset header as we know it's case insensitive.
--spec content_type(binary()) -> any().
-content_type(Data) ->
- media_type(Data,
- fun (Rest, Type, SubType) ->
- params(Rest,
- fun (<<>>, Params) ->
- case lists:keyfind(<<"charset">>, 1, Params) of
- false ->
- {Type, SubType, Params};
- {_, Charset} ->
- Charset2 = cowboy_bstr:to_lower(Charset),
- Params2 = lists:keyreplace(<<"charset">>,
- 1, Params, {<<"charset">>, Charset2}),
- {Type, SubType, Params2}
- end;
- (_Rest2, _) ->
- {error, badarg}
- end)
- end).
-
--spec media_range(binary(), fun()) -> any().
-media_range(Data, Fun) ->
- media_type(Data,
- fun (Rest, Type, SubType) ->
- media_range_params(Rest, Fun, Type, SubType, [])
- end).
-
--spec media_range_params(binary(), fun(), binary(), binary(),
- [{binary(), binary()}]) -> any().
-media_range_params(Data, Fun, Type, SubType, Acc) ->
- whitespace(Data,
- fun (<< $;, Rest/binary >>) ->
- whitespace(Rest,
- fun (Rest2) ->
- media_range_param_attr(Rest2, Fun, Type, SubType, Acc)
- end);
- (Rest) -> Fun(Rest, {{Type, SubType, lists:reverse(Acc)}, 1000, []})
- end).
-
--spec media_range_param_attr(binary(), fun(), binary(), binary(),
- [{binary(), binary()}]) -> any().
-media_range_param_attr(Data, Fun, Type, SubType, Acc) ->
- token_ci(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (<< $=, Rest/binary >>, Attr) ->
- media_range_param_value(Rest, Fun, Type, SubType, Acc, Attr)
- end).
-
--spec media_range_param_value(binary(), fun(), binary(), binary(),
- [{binary(), binary()}], binary()) -> any().
-media_range_param_value(Data, Fun, Type, SubType, Acc, <<"q">>) ->
- qvalue(Data,
- fun (Rest, Quality) ->
- accept_ext(Rest, Fun, Type, SubType, Acc, Quality, [])
- end);
-media_range_param_value(Data, Fun, Type, SubType, Acc, Attr) ->
- word(Data,
- fun (Rest, Value) ->
- media_range_params(Rest, Fun,
- Type, SubType, [{Attr, Value}|Acc])
- end).
-
--spec media_type(binary(), fun()) -> any().
-media_type(Data, Fun) ->
- token_ci(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (<< $/, Rest/binary >>, Type) ->
- token_ci(Rest,
- fun (_Rest2, <<>>) -> {error, badarg};
- (Rest2, SubType) -> Fun(Rest2, Type, SubType)
- end);
- %% This is a non-strict parsing clause required by some user agents
- %% that use * instead of */* in the list of media types.
- (Rest, <<"*">> = Type) ->
- token_ci(<<"*", Rest/binary>>,
- fun (_Rest2, <<>>) -> {error, badarg};
- (Rest2, SubType) -> Fun(Rest2, Type, SubType)
- end);
- (_Rest, _Type) -> {error, badarg}
- end).
-
--spec accept_ext(binary(), fun(), binary(), binary(),
- [{binary(), binary()}], 0..1000,
- [{binary(), binary()} | binary()]) -> any().
-accept_ext(Data, Fun, Type, SubType, Params, Quality, Acc) ->
- whitespace(Data,
- fun (<< $;, Rest/binary >>) ->
- whitespace(Rest,
- fun (Rest2) ->
- accept_ext_attr(Rest2, Fun,
- Type, SubType, Params, Quality, Acc)
- end);
- (Rest) ->
- Fun(Rest, {{Type, SubType, lists:reverse(Params)},
- Quality, lists:reverse(Acc)})
- end).
-
--spec accept_ext_attr(binary(), fun(), binary(), binary(),
- [{binary(), binary()}], 0..1000,
- [{binary(), binary()} | binary()]) -> any().
-accept_ext_attr(Data, Fun, Type, SubType, Params, Quality, Acc) ->
- token_ci(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (<< $=, Rest/binary >>, Attr) ->
- accept_ext_value(Rest, Fun, Type, SubType, Params,
- Quality, Acc, Attr);
- (Rest, Attr) ->
- accept_ext(Rest, Fun, Type, SubType, Params,
- Quality, [Attr|Acc])
- end).
-
--spec accept_ext_value(binary(), fun(), binary(), binary(),
- [{binary(), binary()}], 0..1000,
- [{binary(), binary()} | binary()], binary()) -> any().
-accept_ext_value(Data, Fun, Type, SubType, Params, Quality, Acc, Attr) ->
- word(Data,
- fun (Rest, Value) ->
- accept_ext(Rest, Fun,
- Type, SubType, Params, Quality, [{Attr, Value}|Acc])
- end).
-
--spec conneg(binary(), fun()) -> any().
-conneg(Data, Fun) ->
- token_ci(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (Rest, Conneg) ->
- maybe_qparam(Rest,
- fun (Rest2, Quality) ->
- Fun(Rest2, {Conneg, Quality})
- end)
- end).
-
--spec language_range(binary(), fun()) -> any().
-language_range(<< $*, Rest/binary >>, Fun) ->
- language_range_ret(Rest, Fun, '*');
-language_range(Data, Fun) ->
- language_tag(Data,
- fun (Rest, LanguageTag) ->
- language_range_ret(Rest, Fun, LanguageTag)
- end).
-
--spec language_range_ret(binary(), fun(), '*' | {binary(), [binary()]}) -> any().
-language_range_ret(Data, Fun, LanguageTag) ->
- maybe_qparam(Data,
- fun (Rest, Quality) ->
- Fun(Rest, {LanguageTag, Quality})
- end).
-
--spec language_tag(binary(), fun()) -> any().
-language_tag(Data, Fun) ->
- alpha(Data,
- fun (_Rest, Tag) when byte_size(Tag) =:= 0; byte_size(Tag) > 8 ->
- {error, badarg};
- (<< $-, Rest/binary >>, Tag) ->
- language_subtag(Rest, Fun, Tag, []);
- (Rest, Tag) ->
- Fun(Rest, Tag)
- end).
-
--spec language_subtag(binary(), fun(), binary(), [binary()]) -> any().
-language_subtag(Data, Fun, Tag, Acc) ->
- alphanumeric(Data,
- fun (_Rest, SubTag) when byte_size(SubTag) =:= 0;
- byte_size(SubTag) > 8 -> {error, badarg};
- (<< $-, Rest/binary >>, SubTag) ->
- language_subtag(Rest, Fun, Tag, [SubTag|Acc]);
- (Rest, SubTag) ->
- %% Rebuild the full tag now that we know it's correct
- Sub = << << $-, S/binary >> || S <- lists:reverse([SubTag|Acc]) >>,
- Fun(Rest, << Tag/binary, Sub/binary >>)
- end).
-
--spec maybe_qparam(binary(), fun()) -> any().
-maybe_qparam(Data, Fun) ->
- whitespace(Data,
- fun (<< $;, Rest/binary >>) ->
- whitespace(Rest,
- fun (Rest2) ->
- %% This is a non-strict parsing clause required by some user agents
- %% that use the wrong delimiter putting a charset where a qparam is
- %% expected.
- try qparam(Rest2, Fun) of
- Result -> Result
- catch
- error:function_clause ->
- Fun(<<",", Rest2/binary>>, 1000)
- end
- end);
- (Rest) ->
- Fun(Rest, 1000)
- end).
-
--spec qparam(binary(), fun()) -> any().
-qparam(<< Q, $=, Data/binary >>, Fun) when Q =:= $q; Q =:= $Q ->
- qvalue(Data, Fun).
-
--spec entity_tag_match(binary()) -> any().
-entity_tag_match(<< $*, Rest/binary >>) ->
- whitespace(Rest,
- fun (<<>>) -> '*';
- (_Any) -> {error, badarg}
- end);
-entity_tag_match(Data) ->
- nonempty_list(Data, fun entity_tag/2).
-
--spec entity_tag(binary(), fun()) -> any().
-entity_tag(<< "W/", Rest/binary >>, Fun) ->
- opaque_tag(Rest, Fun, weak);
-entity_tag(Data, Fun) ->
- opaque_tag(Data, Fun, strong).
-
--spec opaque_tag(binary(), fun(), weak | strong) -> any().
-opaque_tag(Data, Fun, Strength) ->
- quoted_string(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (Rest, OpaqueTag) -> Fun(Rest, {Strength, OpaqueTag})
- end).
-
--spec expectation(binary(), fun()) -> any().
-expectation(Data, Fun) ->
- token_ci(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (<< $=, Rest/binary >>, Expectation) ->
- word(Rest,
- fun (Rest2, ExtValue) ->
- params(Rest2, fun (Rest3, ExtParams) ->
- Fun(Rest3, {Expectation, ExtValue, ExtParams})
- end)
- end);
- (Rest, Expectation) ->
- Fun(Rest, Expectation)
- end).
-
--spec params(binary(), fun()) -> any().
-params(Data, Fun) ->
- params(Data, Fun, []).
-
--spec params(binary(), fun(), [{binary(), binary()}]) -> any().
-params(Data, Fun, Acc) ->
- whitespace(Data,
- fun (<< $;, Rest/binary >>) ->
- param(Rest,
- fun (Rest2, Attr, Value) ->
- params(Rest2, Fun, [{Attr, Value}|Acc])
- end);
- (Rest) ->
- Fun(Rest, lists:reverse(Acc))
- end).
-
--spec param(binary(), fun()) -> any().
-param(Data, Fun) ->
- whitespace(Data,
- fun (Rest) ->
- token_ci(Rest,
- fun (_Rest2, <<>>) -> {error, badarg};
- (<< $=, Rest2/binary >>, Attr) ->
- word(Rest2,
- fun (Rest3, Value) ->
- Fun(Rest3, Attr, Value)
- end);
- (_Rest2, _Attr) -> {error, badarg}
- end)
- 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.
--spec http_date(binary()) -> any().
-http_date(Data) ->
- case rfc1123_date(Data) of
- {error, badarg} ->
- case rfc850_date(Data) of
- {error, badarg} ->
- case asctime_date(Data) of
- {error, badarg} ->
- {error, badarg};
- HTTPDate ->
- HTTPDate
- end;
- HTTPDate ->
- HTTPDate
- end;
- HTTPDate ->
- HTTPDate
- end.
-
--spec rfc1123_date(binary()) -> any().
-rfc1123_date(Data) ->
- wkday(Data,
- fun (<< ", ", Rest/binary >>, _WkDay) ->
- date1(Rest,
- fun (<< " ", Rest2/binary >>, Date) ->
- time(Rest2,
- fun (<< " GMT", Rest3/binary >>, Time) ->
- http_date_ret(Rest3, {Date, Time});
- (_Any, _Time) ->
- {error, badarg}
- end);
- (_Any, _Date) ->
- {error, badarg}
- end);
- (_Any, _WkDay) ->
- {error, badarg}
- end).
-
--spec rfc850_date(binary()) -> any().
-%% From the RFC:
-%% HTTP/1.1 clients and caches SHOULD assume that an RFC-850 date
-%% which appears to be more than 50 years in the future is in fact
-%% in the past (this helps solve the "year 2000" problem).
-rfc850_date(Data) ->
- weekday(Data,
- fun (<< ", ", Rest/binary >>, _WeekDay) ->
- date2(Rest,
- fun (<< " ", Rest2/binary >>, Date) ->
- time(Rest2,
- fun (<< " GMT", Rest3/binary >>, Time) ->
- http_date_ret(Rest3, {Date, Time});
- (_Any, _Time) ->
- {error, badarg}
- end);
- (_Any, _Date) ->
- {error, badarg}
- end);
- (_Any, _WeekDay) ->
- {error, badarg}
- end).
-
--spec asctime_date(binary()) -> any().
-asctime_date(Data) ->
- wkday(Data,
- fun (<< " ", Rest/binary >>, _WkDay) ->
- date3(Rest,
- fun (<< " ", Rest2/binary >>, PartialDate) ->
- time(Rest2,
- fun (<< " ", Rest3/binary >>, Time) ->
- asctime_year(Rest3,
- PartialDate, Time);
- (_Any, _Time) ->
- {error, badarg}
- end);
- (_Any, _PartialDate) ->
- {error, badarg}
- end);
- (_Any, _WkDay) ->
- {error, badarg1}
- end).
-
--spec asctime_year(binary(), tuple(), tuple()) -> any().
-asctime_year(<< Y1, Y2, Y3, Y4, Rest/binary >>, {Month, Day}, Time)
- when Y1 >= $0, Y1 =< $9, Y2 >= $0, Y2 =< $9,
- Y3 >= $0, Y3 =< $9, Y4 >= $0, Y4 =< $9 ->
- Year = (Y1 - $0) * 1000 + (Y2 - $0) * 100 + (Y3 - $0) * 10 + (Y4 - $0),
- http_date_ret(Rest, {{Year, Month, Day}, Time}).
-
--spec http_date_ret(binary(), tuple()) -> any().
-http_date_ret(Data, DateTime = {Date, _Time}) ->
- whitespace(Data,
- fun (<<>>) ->
- case calendar:valid_date(Date) of
- true -> DateTime;
- false -> {error, badarg}
- end;
- (_Any) ->
- {error, badarg}
- end).
-
-%% We never use it, pretty much just checks the wkday is right.
--spec wkday(binary(), fun()) -> any().
-wkday(<< WkDay:3/binary, Rest/binary >>, Fun)
- when WkDay =:= <<"Mon">>; WkDay =:= <<"Tue">>; WkDay =:= <<"Wed">>;
- WkDay =:= <<"Thu">>; WkDay =:= <<"Fri">>; WkDay =:= <<"Sat">>;
- WkDay =:= <<"Sun">> ->
- Fun(Rest, WkDay);
-wkday(_Any, _Fun) ->
- {error, badarg}.
-
-%% We never use it, pretty much just checks the weekday is right.
--spec weekday(binary(), fun()) -> any().
-weekday(<< "Monday", Rest/binary >>, Fun) ->
- Fun(Rest, <<"Monday">>);
-weekday(<< "Tuesday", Rest/binary >>, Fun) ->
- Fun(Rest, <<"Tuesday">>);
-weekday(<< "Wednesday", Rest/binary >>, Fun) ->
- Fun(Rest, <<"Wednesday">>);
-weekday(<< "Thursday", Rest/binary >>, Fun) ->
- Fun(Rest, <<"Thursday">>);
-weekday(<< "Friday", Rest/binary >>, Fun) ->
- Fun(Rest, <<"Friday">>);
-weekday(<< "Saturday", Rest/binary >>, Fun) ->
- Fun(Rest, <<"Saturday">>);
-weekday(<< "Sunday", Rest/binary >>, Fun) ->
- Fun(Rest, <<"Sunday">>);
-weekday(_Any, _Fun) ->
- {error, badarg}.
-
--spec date1(binary(), fun()) -> any().
-date1(<< D1, D2, " ", M:3/binary, " ", Y1, Y2, Y3, Y4, Rest/binary >>, Fun)
- when D1 >= $0, D1 =< $9, D2 >= $0, D2 =< $9,
- Y1 >= $0, Y1 =< $9, Y2 >= $0, Y2 =< $9,
- Y3 >= $0, Y3 =< $9, Y4 >= $0, Y4 =< $9 ->
- case month(M) of
- {error, badarg} ->
- {error, badarg};
- Month ->
- Fun(Rest, {
- (Y1 - $0) * 1000 + (Y2 - $0) * 100 + (Y3 - $0) * 10 + (Y4 - $0),
- Month,
- (D1 - $0) * 10 + (D2 - $0)
- })
- end;
-date1(_Data, _Fun) ->
- {error, badarg}.
-
--spec date2(binary(), fun()) -> any().
-date2(<< D1, D2, "-", M:3/binary, "-", Y1, Y2, Rest/binary >>, Fun)
- when D1 >= $0, D1 =< $9, D2 >= $0, D2 =< $9,
- Y1 >= $0, Y1 =< $9, Y2 >= $0, Y2 =< $9 ->
- case month(M) of
- {error, badarg} ->
- {error, badarg};
- Month ->
- Year = (Y1 - $0) * 10 + (Y2 - $0),
- Year2 = case Year > 50 of
- true -> Year + 1900;
- false -> Year + 2000
- end,
- Fun(Rest, {
- Year2,
- Month,
- (D1 - $0) * 10 + (D2 - $0)
- })
- end;
-date2(_Data, _Fun) ->
- {error, badarg}.
-
--spec date3(binary(), fun()) -> any().
-date3(<< M:3/binary, " ", D1, D2, Rest/binary >>, Fun)
- when (D1 >= $0 andalso D1 =< $3) orelse D1 =:= $\s,
- D2 >= $0, D2 =< $9 ->
- case month(M) of
- {error, badarg} ->
- {error, badarg};
- Month ->
- Day = case D1 of
- $\s -> D2 - $0;
- D1 -> (D1 - $0) * 10 + (D2 - $0)
- end,
- Fun(Rest, {Month, Day})
- end;
-date3(_Data, _Fun) ->
- {error, badarg}.
-
--spec month(<< _:24 >>) -> 1..12 | {error, badarg}.
-month(<<"Jan">>) -> 1;
-month(<<"Feb">>) -> 2;
-month(<<"Mar">>) -> 3;
-month(<<"Apr">>) -> 4;
-month(<<"May">>) -> 5;
-month(<<"Jun">>) -> 6;
-month(<<"Jul">>) -> 7;
-month(<<"Aug">>) -> 8;
-month(<<"Sep">>) -> 9;
-month(<<"Oct">>) -> 10;
-month(<<"Nov">>) -> 11;
-month(<<"Dec">>) -> 12;
-month(_Any) -> {error, badarg}.
-
--spec time(binary(), fun()) -> any().
-time(<< H1, H2, ":", M1, M2, ":", S1, S2, Rest/binary >>, Fun)
- when H1 >= $0, H1 =< $2, H2 >= $0, H2 =< $9,
- M1 >= $0, M1 =< $5, M2 >= $0, M2 =< $9,
- S1 >= $0, S1 =< $5, S2 >= $0, S2 =< $9 ->
- Hour = (H1 - $0) * 10 + (H2 - $0),
- case Hour < 24 of
- true ->
- Time = {
- Hour,
- (M1 - $0) * 10 + (M2 - $0),
- (S1 - $0) * 10 + (S2 - $0)
- },
- Fun(Rest, Time);
- false ->
- {error, badarg}
- end.
-
--spec whitespace(binary(), fun()) -> any().
-whitespace(<< C, Rest/binary >>, Fun)
- when C =:= $\s; C =:= $\t ->
- whitespace(Rest, Fun);
-whitespace(Data, Fun) ->
- Fun(Data).
-
--spec digits(binary()) -> non_neg_integer() | {error, badarg}.
-digits(Data) ->
- digits(Data,
- fun (Rest, I) ->
- whitespace(Rest,
- fun (<<>>) ->
- I;
- (_Rest2) ->
- {error, badarg}
- end)
- end).
-
--spec digits(binary(), fun()) -> any().
-digits(<< C, Rest/binary >>, Fun)
- when C >= $0, C =< $9 ->
- digits(Rest, Fun, C - $0);
-digits(_Data, _Fun) ->
- {error, badarg}.
-
--spec digits(binary(), fun(), non_neg_integer()) -> any().
-digits(<< C, Rest/binary >>, Fun, Acc)
- when C >= $0, C =< $9 ->
- digits(Rest, Fun, Acc * 10 + (C - $0));
-digits(Data, Fun, Acc) ->
- Fun(Data, Acc).
-
-%% Changes all characters to lowercase.
--spec alpha(binary(), fun()) -> any().
-alpha(Data, Fun) ->
- alpha(Data, Fun, <<>>).
-
--spec alpha(binary(), fun(), binary()) -> any().
-alpha(<<>>, Fun, Acc) ->
- Fun(<<>>, Acc);
-alpha(<< C, Rest/binary >>, Fun, Acc)
- when C >= $a andalso C =< $z;
- C >= $A andalso C =< $Z ->
- C2 = cowboy_bstr:char_to_lower(C),
- alpha(Rest, Fun, << Acc/binary, C2 >>);
-alpha(Data, Fun, Acc) ->
- Fun(Data, Acc).
-
--spec alphanumeric(binary(), fun()) -> any().
-alphanumeric(Data, Fun) ->
- alphanumeric(Data, Fun, <<>>).
-
--spec alphanumeric(binary(), fun(), binary()) -> any().
-alphanumeric(<<>>, Fun, Acc) ->
- Fun(<<>>, Acc);
-alphanumeric(<< C, Rest/binary >>, Fun, Acc)
- when C >= $a andalso C =< $z;
- C >= $A andalso C =< $Z;
- C >= $0 andalso C =< $9 ->
- C2 = cowboy_bstr:char_to_lower(C),
- alphanumeric(Rest, Fun, << Acc/binary, C2 >>);
-alphanumeric(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);
-word(Data, Fun) ->
- token(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (Rest, Token) -> Fun(Rest, Token)
- end).
-
-%% Changes all characters to lowercase.
--spec token_ci(binary(), fun()) -> any().
-token_ci(Data, Fun) ->
- token(Data, Fun, ci, <<>>).
-
--spec token(binary(), fun()) -> any().
-token(Data, Fun) ->
- token(Data, Fun, cs, <<>>).
-
--spec token(binary(), fun(), ci | cs, binary()) -> any().
-token(<<>>, Fun, _Case, Acc) ->
- Fun(<<>>, Acc);
-token(Data = << C, _Rest/binary >>, Fun, _Case, Acc)
- when C =:= $(; C =:= $); C =:= $<; C =:= $>; C =:= $@;
- C =:= $,; C =:= $;; C =:= $:; C =:= $\\; C =:= $";
- C =:= $/; C =:= $[; C =:= $]; C =:= $?; C =:= $=;
- C =:= ${; C =:= $}; C =:= $\s; C =:= $\t;
- C < 32; C =:= 127 ->
- Fun(Data, Acc);
-token(<< C, Rest/binary >>, Fun, Case = ci, Acc) ->
- C2 = cowboy_bstr:char_to_lower(C),
- token(Rest, Fun, Case, << Acc/binary, C2 >>);
-token(<< C, Rest/binary >>, Fun, Case, Acc) ->
- token(Rest, Fun, Case, << Acc/binary, C >>).
-
--spec quoted_string(binary(), fun()) -> any().
-quoted_string(<< $", Rest/binary >>, Fun) ->
- quoted_string(Rest, Fun, <<>>).
-
--spec quoted_string(binary(), fun(), binary()) -> any().
-quoted_string(<<>>, _Fun, _Acc) ->
- {error, badarg};
-quoted_string(<< $", Rest/binary >>, Fun, Acc) ->
- Fun(Rest, Acc);
-quoted_string(<< $\\, C, Rest/binary >>, Fun, Acc) ->
- quoted_string(Rest, Fun, << Acc/binary, C >>);
-quoted_string(<< C, Rest/binary >>, Fun, Acc) ->
- quoted_string(Rest, Fun, << Acc/binary, C >>).
-
--spec qvalue(binary(), fun()) -> any().
-qvalue(<< $0, $., Rest/binary >>, Fun) ->
- qvalue(Rest, Fun, 0, 100);
-%% Some user agents use q=.x instead of q=0.x
-qvalue(<< $., Rest/binary >>, Fun) ->
- qvalue(Rest, Fun, 0, 100);
-qvalue(<< $0, Rest/binary >>, Fun) ->
- Fun(Rest, 0);
-qvalue(<< $1, $., $0, $0, $0, Rest/binary >>, Fun) ->
- Fun(Rest, 1000);
-qvalue(<< $1, $., $0, $0, Rest/binary >>, Fun) ->
- Fun(Rest, 1000);
-qvalue(<< $1, $., $0, Rest/binary >>, Fun) ->
- Fun(Rest, 1000);
-qvalue(<< $1, Rest/binary >>, Fun) ->
- Fun(Rest, 1000);
-qvalue(_Data, _Fun) ->
- {error, badarg}.
-
--spec qvalue(binary(), fun(), integer(), 1 | 10 | 100) -> any().
-qvalue(Data, Fun, Q, 0) ->
- Fun(Data, Q);
-qvalue(<< C, Rest/binary >>, Fun, Q, M)
- when C >= $0, C =< $9 ->
- qvalue(Rest, Fun, Q + (C - $0) * M, M div 10);
-qvalue(Data, Fun, Q, _M) ->
- Fun(Data, Q).
-
-%% Only RFC2617 Basic authorization is supported so far.
--spec authorization(binary(), binary()) -> {binary(), any()} | {error, badarg}.
-authorization(UserPass, Type = <<"basic">>) ->
- whitespace(UserPass,
- fun(D) ->
- authorization_basic_userid(base64:mime_decode(D),
- fun(Rest, Userid) ->
- authorization_basic_password(Rest,
- fun(Password) ->
- {Type, {Userid, Password}}
- end)
- end)
- end);
-authorization(String, Type) ->
- whitespace(String, fun(Rest) -> {Type, Rest} end).
-
--spec authorization_basic_userid(binary(), fun()) -> any().
-authorization_basic_userid(Data, Fun) ->
- authorization_basic_userid(Data, Fun, <<>>).
-
-authorization_basic_userid(<<>>, _Fun, _Acc) ->
- {error, badarg};
-authorization_basic_userid(<<C, _Rest/binary>>, _Fun, Acc)
- when C < 32; C =:= 127; (C =:=$: andalso Acc =:= <<>>) ->
- {error, badarg};
-authorization_basic_userid(<<$:, Rest/binary>>, Fun, Acc) ->
- Fun(Rest, Acc);
-authorization_basic_userid(<<C, Rest/binary>>, Fun, Acc) ->
- authorization_basic_userid(Rest, Fun, <<Acc/binary, C>>).
-
--spec authorization_basic_password(binary(), fun()) -> any().
-authorization_basic_password(Data, Fun) ->
- authorization_basic_password(Data, Fun, <<>>).
-
-authorization_basic_password(<<C, _Rest/binary>>, _Fun, _Acc)
- when C < 32; C=:= 127 ->
- {error, badarg};
-authorization_basic_password(<<>>, Fun, Acc) ->
- Fun(Acc);
-authorization_basic_password(<<C, Rest/binary>>, Fun, Acc) ->
- authorization_basic_password(Rest, Fun, <<Acc/binary, C>>).
-
--spec range(binary()) -> {Unit, [Range]} | {error, badarg} when
- Unit :: binary(),
- Range :: {non_neg_integer(), non_neg_integer() | infinity} | neg_integer().
-range(Data) ->
- token_ci(Data, fun range/2).
-
-range(Data, Token) ->
- whitespace(Data,
- fun(<<"=", Rest/binary>>) ->
- case list(Rest, fun range_beginning/2) of
- {error, badarg} ->
- {error, badarg};
- Ranges ->
- {Token, Ranges}
- end;
- (_) ->
- {error, badarg}
- end).
-
-range_beginning(Data, Fun) ->
- range_digits(Data, suffix,
- fun(D, RangeBeginning) ->
- range_ending(D, Fun, RangeBeginning)
- end).
-
-range_ending(Data, Fun, RangeBeginning) ->
- whitespace(Data,
- fun(<<"-", R/binary>>) ->
- case RangeBeginning of
- suffix ->
- range_digits(R, fun(D, RangeEnding) -> Fun(D, -RangeEnding) end);
- _ ->
- range_digits(R, infinity,
- fun(D, RangeEnding) ->
- Fun(D, {RangeBeginning, RangeEnding})
- end)
- end;
- (_) ->
- {error, badarg}
- end).
-
--spec range_digits(binary(), fun()) -> any().
-range_digits(Data, Fun) ->
- whitespace(Data,
- fun(D) ->
- digits(D, Fun)
- end).
-
--spec range_digits(binary(), any(), fun()) -> any().
-range_digits(Data, Default, Fun) ->
- whitespace(Data,
- fun(<< C, Rest/binary >>) when C >= $0, C =< $9 ->
- digits(Rest, Fun, C - $0);
- (_) ->
- Fun(Data, Default)
- end).
-
--spec parameterized_tokens(binary()) -> any().
-parameterized_tokens(Data) ->
- nonempty_list(Data,
- fun (D, Fun) ->
- token(D,
- fun (_Rest, <<>>) -> {error, badarg};
- (Rest, Token) ->
- parameterized_tokens_params(Rest,
- fun (Rest2, Params) ->
- Fun(Rest2, {Token, Params})
- end, [])
- end)
- end).
-
--spec parameterized_tokens_params(binary(), fun(), [binary() | {binary(), binary()}]) -> any().
-parameterized_tokens_params(Data, Fun, Acc) ->
- whitespace(Data,
- fun (<< $;, Rest/binary >>) ->
- parameterized_tokens_param(Rest,
- fun (Rest2, Param) ->
- parameterized_tokens_params(Rest2, Fun, [Param|Acc])
- end);
- (Rest) ->
- Fun(Rest, lists:reverse(Acc))
- end).
-
--spec parameterized_tokens_param(binary(), fun()) -> any().
-parameterized_tokens_param(Data, Fun) ->
- whitespace(Data,
- fun (Rest) ->
- token(Rest,
- fun (_Rest2, <<>>) -> {error, badarg};
- (<< $=, Rest2/binary >>, Attr) ->
- word(Rest2,
- fun (Rest3, Value) ->
- Fun(Rest3, {Attr, Value})
- end);
- (Rest2, Attr) ->
- Fun(Rest2, Attr)
- end)
- end).
-
-%% Decoding.
-
-%% @todo Move this to cowlib too I suppose. :-)
--spec ce_identity(Data) -> Data when Data::binary().
-ce_identity(Data) ->
- Data.
-
-%% Tests.
-
--ifdef(TEST).
-nonempty_charset_list_test_() ->
- Tests = [
- {<<>>, {error, badarg}},
- {<<"iso-8859-5, unicode-1-1;q=0.8">>, [
- {<<"iso-8859-5">>, 1000},
- {<<"unicode-1-1">>, 800}
- ]},
- %% Some user agents send this invalid value for the Accept-Charset header
- {<<"ISO-8859-1;utf-8;q=0.7,*;q=0.7">>, [
- {<<"iso-8859-1">>, 1000},
- {<<"utf-8">>, 700},
- {<<"*">>, 700}
- ]}
- ],
- [{V, fun() -> R = nonempty_list(V, fun conneg/2) end} || {V, R} <- Tests].
-
-nonempty_language_range_list_test_() ->
- Tests = [
- {<<"da, en-gb;q=0.8, en;q=0.7">>, [
- {<<"da">>, 1000},
- {<<"en-gb">>, 800},
- {<<"en">>, 700}
- ]},
- {<<"en, en-US, en-cockney, i-cherokee, x-pig-latin, es-419">>, [
- {<<"en">>, 1000},
- {<<"en-us">>, 1000},
- {<<"en-cockney">>, 1000},
- {<<"i-cherokee">>, 1000},
- {<<"x-pig-latin">>, 1000},
- {<<"es-419">>, 1000}
- ]}
- ],
- [{V, fun() -> R = nonempty_list(V, fun language_range/2) end}
- || {V, R} <- Tests].
-
-nonempty_token_list_test_() ->
- Tests = [
- {<<>>, {error, badarg}},
- {<<" ">>, {error, badarg}},
- {<<" , ">>, {error, badarg}},
- {<<",,,">>, {error, badarg}},
- {<<"a b">>, {error, badarg}},
- {<<"a , , , ">>, [<<"a">>]},
- {<<" , , , a">>, [<<"a">>]},
- {<<"a, , b">>, [<<"a">>, <<"b">>]},
- {<<"close">>, [<<"close">>]},
- {<<"keep-alive, upgrade">>, [<<"keep-alive">>, <<"upgrade">>]}
- ],
- [{V, fun() -> R = nonempty_list(V, fun token/2) end} || {V, R} <- Tests].
-
-media_range_list_test_() ->
- Tests = [
- {<<"audio/*; q=0.2, audio/basic">>, [
- {{<<"audio">>, <<"*">>, []}, 200, []},
- {{<<"audio">>, <<"basic">>, []}, 1000, []}
- ]},
- {<<"text/plain; q=0.5, text/html, "
- "text/x-dvi; q=0.8, text/x-c">>, [
- {{<<"text">>, <<"plain">>, []}, 500, []},
- {{<<"text">>, <<"html">>, []}, 1000, []},
- {{<<"text">>, <<"x-dvi">>, []}, 800, []},
- {{<<"text">>, <<"x-c">>, []}, 1000, []}
- ]},
- {<<"text/*, text/html, text/html;level=1, */*">>, [
- {{<<"text">>, <<"*">>, []}, 1000, []},
- {{<<"text">>, <<"html">>, []}, 1000, []},
- {{<<"text">>, <<"html">>, [{<<"level">>, <<"1">>}]}, 1000, []},
- {{<<"*">>, <<"*">>, []}, 1000, []}
- ]},
- {<<"text/*;q=0.3, text/html;q=0.7, text/html;level=1, "
- "text/html;level=2;q=0.4, */*;q=0.5">>, [
- {{<<"text">>, <<"*">>, []}, 300, []},
- {{<<"text">>, <<"html">>, []}, 700, []},
- {{<<"text">>, <<"html">>, [{<<"level">>, <<"1">>}]}, 1000, []},
- {{<<"text">>, <<"html">>, [{<<"level">>, <<"2">>}]}, 400, []},
- {{<<"*">>, <<"*">>, []}, 500, []}
- ]},
- {<<"text/html;level=1;quoted=\"hi hi hi\";"
- "q=0.123;standalone;complex=gits, text/plain">>, [
- {{<<"text">>, <<"html">>,
- [{<<"level">>, <<"1">>}, {<<"quoted">>, <<"hi hi hi">>}]}, 123,
- [<<"standalone">>, {<<"complex">>, <<"gits">>}]},
- {{<<"text">>, <<"plain">>, []}, 1000, []}
- ]},
- {<<"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2">>, [
- {{<<"text">>, <<"html">>, []}, 1000, []},
- {{<<"image">>, <<"gif">>, []}, 1000, []},
- {{<<"image">>, <<"jpeg">>, []}, 1000, []},
- {{<<"*">>, <<"*">>, []}, 200, []},
- {{<<"*">>, <<"*">>, []}, 200, []}
- ]}
- ],
- [{V, fun() -> R = list(V, fun media_range/2) end} || {V, R} <- Tests].
-
-entity_tag_match_test_() ->
- Tests = [
- {<<"\"xyzzy\"">>, [{strong, <<"xyzzy">>}]},
- {<<"\"xyzzy\", W/\"r2d2xxxx\", \"c3piozzzz\"">>,
- [{strong, <<"xyzzy">>},
- {weak, <<"r2d2xxxx">>},
- {strong, <<"c3piozzzz">>}]},
- {<<"*">>, '*'}
- ],
- [{V, fun() -> R = entity_tag_match(V) end} || {V, R} <- Tests].
-
-http_date_test_() ->
- 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}}},
- {<<"Sun Nov 6 08:49:37 1994">>, {{1994, 11, 6}, {8, 49, 37}}}
- ],
- [{V, fun() -> R = http_date(V) end} || {V, R} <- Tests].
-
-rfc1123_date_test_() ->
- 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_() ->
- 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_() ->
- 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_() ->
- Tests = [
- {<<"text/plain; charset=iso-8859-4">>,
- {<<"text">>, <<"plain">>, [{<<"charset">>, <<"iso-8859-4">>}]}},
- {<<"multipart/form-data \t;Boundary=\"MultipartIsUgly\"">>,
- {<<"multipart">>, <<"form-data">>, [
- {<<"boundary">>, <<"MultipartIsUgly">>}
- ]}},
- {<<"foo/bar; one=FirstParam; two=SecondParam">>,
- {<<"foo">>, <<"bar">>, [
- {<<"one">>, <<"FirstParam">>},
- {<<"two">>, <<"SecondParam">>}
- ]}}
- ],
- [{V, fun () -> R = content_type(V) end} || {V, R} <- Tests].
-
-parameterized_tokens_test_() ->
- Tests = [
- {<<"foo">>, [{<<"foo">>, []}]},
- {<<"bar; baz=2">>, [{<<"bar">>, [{<<"baz">>, <<"2">>}]}]},
- {<<"bar; baz=2;bat">>, [{<<"bar">>, [{<<"baz">>, <<"2">>}, <<"bat">>]}]},
- {<<"bar; baz=2;bat=\"z=1,2;3\"">>, [{<<"bar">>, [{<<"baz">>, <<"2">>}, {<<"bat">>, <<"z=1,2;3">>}]}]},
- {<<"foo, bar; baz=2">>, [{<<"foo">>, []}, {<<"bar">>, [{<<"baz">>, <<"2">>}]}]}
- ],
- [{V, fun () -> R = parameterized_tokens(V) end} || {V, R} <- Tests].
-
-digits_test_() ->
- Tests = [
- {<<"42 ">>, 42},
- {<<"69\t">>, 69},
- {<<"1337">>, 1337}
- ],
- [{V, fun() -> R = digits(V) end} || {V, R} <- Tests].
-
-http_authorization_test_() ->
- Tests = [
- {<<"basic">>, <<"QWxsYWRpbjpvcGVuIHNlc2FtZQ==">>,
- {<<"basic">>, {<<"Alladin">>, <<"open sesame">>}}},
- {<<"basic">>, <<"dXNlcm5hbWU6">>,
- {<<"basic">>, {<<"username">>, <<>>}}},
- {<<"basic">>, <<"dXNlcm5hbWUK">>,
- {error, badarg}},
- {<<"basic">>, <<"_[]@#$%^&*()-AA==">>,
- {error, badarg}},
- {<<"basic">>, <<"dXNlcjpwYXNzCA==">>,
- {error, badarg}},
- {<<"bearer">>, <<" some_secret_key">>,
- {<<"bearer">>,<<"some_secret_key">>}}
- ],
- [{V, fun() -> R = authorization(V,T) end} || {T, V, R} <- Tests].
-
-http_range_test_() ->
- Tests = [
- {<<"bytes=1-20">>,
- {<<"bytes">>, [{1, 20}]}},
- {<<"bytes=-100">>,
- {<<"bytes">>, [-100]}},
- {<<"bytes=1-">>,
- {<<"bytes">>, [{1, infinity}]}},
- {<<"bytes=1-20,30-40,50-">>,
- {<<"bytes">>, [{1, 20}, {30, 40}, {50, infinity}]}},
- {<<"bytes = 1 - 20 , 50 - , - 300 ">>,
- {<<"bytes">>, [{1, 20}, {50, infinity}, -300]}},
- {<<"bytes=1-20,-500,30-40">>,
- {<<"bytes">>, [{1, 20}, -500, {30, 40}]}},
- {<<"test=1-20,-500,30-40">>,
- {<<"test">>, [{1, 20}, -500, {30, 40}]}},
- {<<"bytes=-">>,
- {error, badarg}},
- {<<"bytes=-30,-">>,
- {error, badarg}}
- ],
- [fun() -> R = range(V) end ||{V, R} <- Tests].
--endif.
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl
index 558dcc3..5a20f6b 100644
--- a/src/cowboy_protocol.erl
+++ b/src/cowboy_protocol.erl
@@ -55,6 +55,7 @@
}).
-include_lib("cowlib/include/cow_inline.hrl").
+-include_lib("cowlib/include/cow_parse.hrl").
%% API.
@@ -74,25 +75,36 @@ get_value(Key, Opts, Default) ->
-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
init(Ref, Socket, Transport, Opts) ->
- Compress = get_value(compress, Opts, false),
- MaxEmptyLines = get_value(max_empty_lines, Opts, 5),
- MaxHeaderNameLength = get_value(max_header_name_length, Opts, 64),
- MaxHeaderValueLength = get_value(max_header_value_length, Opts, 4096),
- MaxHeaders = get_value(max_headers, Opts, 100),
- MaxKeepalive = get_value(max_keepalive, Opts, 100),
- MaxRequestLineLength = get_value(max_request_line_length, Opts, 4096),
- Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]),
- Env = [{listener, Ref}|get_value(env, Opts, [])],
- OnResponse = get_value(onresponse, Opts, undefined),
- Timeout = get_value(timeout, Opts, 5000),
ok = ranch:accept_ack(Ref),
- wait_request(<<>>, #state{socket=Socket, transport=Transport,
- middlewares=Middlewares, compress=Compress, env=Env,
- max_empty_lines=MaxEmptyLines, max_keepalive=MaxKeepalive,
- max_request_line_length=MaxRequestLineLength,
- max_header_name_length=MaxHeaderNameLength,
- max_header_value_length=MaxHeaderValueLength, max_headers=MaxHeaders,
- onresponse=OnResponse, timeout=Timeout, until=until(Timeout)}, 0).
+ Timeout = get_value(timeout, Opts, 5000),
+ Until = until(Timeout),
+ case recv(Socket, Transport, Until) of
+ {ok, Data} ->
+ OnFirstRequest = get_value(onfirstrequest, Opts, undefined),
+ case OnFirstRequest of
+ undefined -> ok;
+ _ -> OnFirstRequest(Ref, Socket, Transport, Opts)
+ end,
+ Compress = get_value(compress, Opts, false),
+ MaxEmptyLines = get_value(max_empty_lines, Opts, 5),
+ MaxHeaderNameLength = get_value(max_header_name_length, Opts, 64),
+ MaxHeaderValueLength = get_value(max_header_value_length, Opts, 4096),
+ MaxHeaders = get_value(max_headers, Opts, 100),
+ MaxKeepalive = get_value(max_keepalive, Opts, 100),
+ MaxRequestLineLength = get_value(max_request_line_length, Opts, 4096),
+ Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]),
+ Env = [{listener, Ref}|get_value(env, Opts, [])],
+ OnResponse = get_value(onresponse, Opts, undefined),
+ parse_request(Data, #state{socket=Socket, transport=Transport,
+ middlewares=Middlewares, compress=Compress, env=Env,
+ max_empty_lines=MaxEmptyLines, max_keepalive=MaxKeepalive,
+ max_request_line_length=MaxRequestLineLength,
+ max_header_name_length=MaxHeaderNameLength,
+ max_header_value_length=MaxHeaderValueLength, max_headers=MaxHeaders,
+ onresponse=OnResponse, timeout=Timeout, until=Until}, 0);
+ {error, _} ->
+ terminate(#state{socket=Socket, transport=Transport}) %% @todo ridiculous
+ end.
-spec until(timeout()) -> non_neg_integer() | infinity.
until(infinity) ->
@@ -264,13 +276,12 @@ match_colon(<< _, Rest/bits >>, N) ->
match_colon(_, _) ->
nomatch.
+parse_hd_name(<< $:, Rest/bits >>, S, M, P, Q, V, H, SoFar) ->
+ parse_hd_before_value(Rest, S, M, P, Q, V, H, SoFar);
+parse_hd_name(<< C, Rest/bits >>, S, M, P, Q, V, H, SoFar) when ?IS_WS(C) ->
+ parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar);
parse_hd_name(<< C, Rest/bits >>, S, M, P, Q, V, H, SoFar) ->
- case C of
- $: -> parse_hd_before_value(Rest, S, M, P, Q, V, H, SoFar);
- $\s -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar);
- $\t -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar);
- ?INLINE_LOWERCASE(parse_hd_name, Rest, S, M, P, Q, V, H, SoFar)
- end.
+ ?LOWER(parse_hd_name, Rest, S, M, P, Q, V, H, SoFar).
parse_hd_name_ws(<< C, Rest/bits >>, S, M, P, Q, V, H, Name) ->
case C of
@@ -429,9 +440,7 @@ parse_host(<< $:, Rest/bits >>, false, Acc) ->
parse_host(<< $], Rest/bits >>, true, Acc) ->
parse_host(Rest, false, << Acc/binary, $] >>);
parse_host(<< C, Rest/bits >>, E, Acc) ->
- case C of
- ?INLINE_LOWERCASE(parse_host, Rest, E, Acc)
- end.
+ ?LOWER(parse_host, Rest, E, Acc).
%% End of request parsing.
%%
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 46a2fc7..29eac3b 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -289,91 +289,43 @@ headers(Req) ->
-spec parse_header(binary(), Req) -> any() when Req::req().
parse_header(Name = <<"content-length">>, Req) ->
- parse_header(Name, Req, 0);
+ parse_header(Name, Req, 0, fun cow_http_hd:parse_content_length/1);
parse_header(Name = <<"cookie">>, Req) ->
- parse_header(Name, Req, []);
+ parse_header(Name, Req, [], fun cow_cookie:parse_cookie/1);
parse_header(Name = <<"transfer-encoding">>, Req) ->
- parse_header(Name, Req, [<<"identity">>]);
+ parse_header(Name, Req, [<<"identity">>], fun cow_http_hd:parse_transfer_encoding/1);
parse_header(Name, Req) ->
parse_header(Name, Req, undefined).
-spec parse_header(binary(), Req, any()) -> any() when Req::req().
-parse_header(Name = <<"accept">>, Req, Default) ->
- parse_header(Name, Req, Default, fun (Value) ->
- cowboy_http:list(Value, fun cowboy_http:media_range/2) end);
-parse_header(Name = <<"accept-charset">>, Req, Default) ->
- parse_header(Name, Req, Default, fun (Value) ->
- cowboy_http:nonempty_list(Value, fun cowboy_http:conneg/2) end);
-parse_header(Name = <<"accept-encoding">>, Req, Default) ->
- parse_header(Name, Req, Default, fun (Value) ->
- cowboy_http:list(Value, fun cowboy_http:conneg/2) end);
-parse_header(Name = <<"accept-language">>, Req, Default) ->
- parse_header(Name, Req, Default, fun (Value) ->
- cowboy_http:nonempty_list(Value, fun cowboy_http:language_range/2) end);
-parse_header(Name = <<"authorization">>, Req, Default) ->
- parse_header(Name, Req, Default, fun (Value) ->
- cowboy_http:token_ci(Value, fun cowboy_http:authorization/2) end);
-parse_header(Name = <<"connection">>, Req, Default) ->
- case header(Name, Req) of
- undefined -> Default;
- Value -> cow_http_hd:parse_connection(Value)
- end;
-parse_header(Name = <<"content-length">>, Req, Default) ->
- case header(Name, Req) of
- undefined -> Default;
- Value -> cow_http_hd:parse_content_length(Value)
- end;
-parse_header(Name = <<"content-type">>, Req, Default) ->
- parse_header(Name, Req, Default, fun cowboy_http:content_type/1);
-parse_header(Name = <<"cookie">>, Req, Default) ->
- case header(Name, Req) of
- undefined -> Default;
- %% Flash player incorrectly sends an empty Cookie header.
- <<>> -> Default;
- Value -> cow_cookie:parse_cookie(Value)
- end;
-parse_header(Name = <<"expect">>, Req, Default) ->
- parse_header(Name, Req, Default, fun (Value) ->
- cowboy_http:nonempty_list(Value, fun cowboy_http:expectation/2) end);
-parse_header(Name, Req, Default)
- when Name =:= <<"if-match">>;
- Name =:= <<"if-none-match">> ->
- parse_header(Name, Req, Default, fun cowboy_http:entity_tag_match/1);
-parse_header(Name, Req, Default)
- when Name =:= <<"if-modified-since">>;
- Name =:= <<"if-unmodified-since">> ->
- parse_header(Name, Req, Default, fun cowboy_http:http_date/1);
-parse_header(Name = <<"range">>, Req, Default) ->
- parse_header(Name, Req, Default, fun cowboy_http:range/1);
-parse_header(Name, Req, Default)
- when Name =:= <<"sec-websocket-protocol">>;
- Name =:= <<"x-forwarded-for">> ->
- parse_header(Name, Req, Default, fun (Value) ->
- cowboy_http:nonempty_list(Value, fun cowboy_http:token/2) end);
-parse_header(Name = <<"transfer-encoding">>, Req, Default) ->
- case header(Name, Req) of
- undefined -> Default;
- Value -> cow_http_hd:parse_transfer_encoding(Value)
- end;
-%% @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) end);
-parse_header(Name = <<"sec-websocket-extensions">>, Req, Default) ->
- parse_header(Name, Req, Default, fun cowboy_http:parameterized_tokens/1).
-
-%% @todo Remove this function when everything moved to cowlib.
+parse_header(Name, Req, Default) ->
+ parse_header(Name, Req, Default, parse_header_fun(Name)).
+
+parse_header_fun(<<"accept">>) -> fun cow_http_hd:parse_accept/1;
+parse_header_fun(<<"accept-charset">>) -> fun cow_http_hd:parse_accept_charset/1;
+parse_header_fun(<<"accept-encoding">>) -> fun cow_http_hd:parse_accept_encoding/1;
+parse_header_fun(<<"accept-language">>) -> fun cow_http_hd:parse_accept_language/1;
+parse_header_fun(<<"authorization">>) -> fun cow_http_hd:parse_authorization/1;
+parse_header_fun(<<"connection">>) -> fun cow_http_hd:parse_connection/1;
+parse_header_fun(<<"content-length">>) -> fun cow_http_hd:parse_content_length/1;
+parse_header_fun(<<"content-type">>) -> fun cow_http_hd:parse_content_type/1;
+parse_header_fun(<<"cookie">>) -> fun cow_cookie:parse_cookie/1;
+parse_header_fun(<<"expect">>) -> fun cow_http_hd:parse_expect/1;
+parse_header_fun(<<"if-match">>) -> fun cow_http_hd:parse_if_match/1;
+parse_header_fun(<<"if-modified-since">>) -> fun cow_http_hd:parse_if_modified_since/1;
+parse_header_fun(<<"if-none-match">>) -> fun cow_http_hd:parse_if_none_match/1;
+parse_header_fun(<<"if-unmodified-since">>) -> fun cow_http_hd:parse_if_unmodified_since/1;
+parse_header_fun(<<"range">>) -> fun cow_http_hd:parse_range/1;
+parse_header_fun(<<"sec-websocket-extensions">>) -> fun cow_http_hd:parse_sec_websocket_extensions/1;
+parse_header_fun(<<"sec-websocket-protocol">>) -> fun cow_http_hd:parse_sec_websocket_protocol_req/1;
+parse_header_fun(<<"transfer-encoding">>) -> fun cow_http_hd:parse_transfer_encoding/1;
+parse_header_fun(<<"upgrade">>) -> fun cow_http_hd:parse_upgrade/1;
+parse_header_fun(<<"x-forwarded-for">>) -> fun cow_http_hd:parse_x_forwarded_for/1.
+
parse_header(Name, Req, Default, ParseFun) ->
case header(Name, Req) of
- undefined ->
- Default;
- Value ->
- case ParseFun(Value) of
- {error, badarg} ->
- error(badarg);
- ParsedValue ->
- ParsedValue
- end
+ undefined -> Default;
+ Value -> ParseFun(Value)
end.
-spec parse_cookies(req()) -> [{binary(), binary()}].
@@ -443,7 +395,7 @@ body(Req=#http_req{body_state=waiting}, Opts) ->
%% Initialize body streaming state.
CFun = case lists:keyfind(content_decode, 1, Opts) of
false ->
- fun cowboy_http:ce_identity/1;
+ fun body_content_decode_identity/1;
{_, CFun0} ->
CFun0
end,
@@ -484,6 +436,10 @@ body(Req, Opts) ->
end,
body_loop(Req, ReadTimeout, ReadLen, ChunkLen, <<>>).
+%% Default identity function for content decoding.
+%% @todo Move into cowlib when more content decode functions get implemented.
+body_content_decode_identity(Data) -> Data.
+
body_loop(Req=#http_req{buffer=Buffer, body_state={stream, Length, _, _, _}},
ReadTimeout, ReadLength, ChunkLength, Acc) ->
{Tag, Res, Req2} = case Buffer of
@@ -868,8 +824,11 @@ chunk(Data, #http_req{socket=Socket, transport=Transport,
ok = Transport:send(Socket, Data);
chunk(Data, #http_req{socket=Socket, transport=Transport,
resp_state=chunks}) ->
- ok = Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
- <<"\r\n">>, Data, <<"\r\n">>]).
+ case iolist_size(Data) of
+ 0 -> ok;
+ Size -> Transport:send(Socket, [integer_to_list(Size, 16),
+ <<"\r\n">>, Data, <<"\r\n">>])
+ end.
%% If ever made public, need to send nothing if HEAD.
-spec last_chunk(Req) -> Req when Req::req().
@@ -907,6 +866,8 @@ maybe_reply(Stacktrace, Req) ->
ok
end.
+do_maybe_reply([{erlang, binary_to_integer, _, _}, {cow_http_hd, parse_content_length, _, _}|_], Req) ->
+ cowboy_req:reply(400, Req);
do_maybe_reply([{cow_http_hd, _, _, _}|_], Req) ->
cowboy_req:reply(400, Req);
do_maybe_reply(_, Req) ->
diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl
index dea47d8..b894354 100644
--- a/src/cowboy_rest.erl
+++ b/src/cowboy_rest.erl
@@ -234,7 +234,7 @@ content_types_provided(Req, State) ->
normalize_content_types({ContentType, Callback})
when is_binary(ContentType) ->
- {cowboy_http:content_type(ContentType), Callback};
+ {cow_http_hd:parse_content_type(ContentType), Callback};
normalize_content_types(Normalized) ->
Normalized.
@@ -896,7 +896,7 @@ generate_etag(Req, State=#state{etag=undefined}) ->
no_call ->
{undefined, Req, State#state{etag=no_call}};
{Etag, Req2, HandlerState} when is_binary(Etag) ->
- [Etag2] = cowboy_http:entity_tag_match(Etag),
+ Etag2 = cow_http_hd:parse_etag(Etag),
{Etag2, Req2, State#state{handler_state=HandlerState, etag=Etag2}};
{Etag, Req2, HandlerState} ->
{Etag, Req2, State#state{handler_state=HandlerState, etag=Etag}}
diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index f3fde32..b806da5 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -425,6 +425,8 @@ split_host_test_() ->
[<<"eu">>, <<"ninenines">>, <<"cowboy">>]},
{<<"ninenines.eu">>,
[<<"eu">>, <<"ninenines">>]},
+ {<<"ninenines.eu.">>,
+ [<<"eu">>, <<"ninenines">>]},
{<<"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z">>,
[<<"z">>, <<"y">>, <<"x">>, <<"w">>, <<"v">>, <<"u">>, <<"t">>,
<<"s">>, <<"r">>, <<"q">>, <<"p">>, <<"o">>, <<"n">>, <<"m">>,
diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl
index 91c4f4a..2628ab5 100644
--- a/src/cowboy_spdy.erl
+++ b/src/cowboy_spdy.erl
@@ -54,15 +54,15 @@
-record(state, {
parent = undefined :: pid(),
- socket,
- transport,
+ socket :: inet:socket(),
+ transport :: module(),
buffer = <<>> :: binary(),
- middlewares,
- env,
- onresponse,
- peer,
- zdef,
- zinf,
+ middlewares :: [module()],
+ env :: cowboy_middleware:env(),
+ onresponse :: cowboy:onresponse_fun(),
+ peer :: {inet:ip_address(), inet:port_number()},
+ zdef :: zlib:zstream(),
+ zinf :: zlib:zstream(),
last_streamid = 0 :: streamid(),
children = [] :: [#child{}]
}).
@@ -387,7 +387,7 @@ delete_child(Pid, State=#state{children=Children}) ->
-> ok.
request_init(FakeSocket, Peer, OnResponse,
Env, Middlewares, Method, Host, Path, Version, Headers) ->
- {Host2, Port} = cow_http:parse_fullhost(Host),
+ {Host2, Port} = cow_http_hd:parse_host(Host),
{Path2, Qs} = cow_http:parse_fullpath(Path),
Version2 = cow_http:parse_version(Version),
Req = cowboy_req:new(FakeSocket, ?MODULE, Peer,
diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl
index 36190a5..9ce4f9d 100644
--- a/src/cowboy_websocket.erl
+++ b/src/cowboy_websocket.erl
@@ -20,21 +20,8 @@
-export([upgrade/6]).
-export([handler_loop/4]).
--type close_code() :: 1000..4999.
--export_type([close_code/0]).
-
--type frame() :: close | ping | pong
- | {text | binary | close | ping | pong, iodata()}
- | {close, close_code(), iodata()}.
--export_type([frame/0]).
-
--type opcode() :: 0 | 1 | 2 | 8 | 9 | 10.
--type mask_key() :: 0..16#ffffffff.
--type frag_state() :: undefined
- | {nofin, opcode(), binary()} | {fin, opcode(), binary()}.
--type rsv() :: << _:3 >>.
-type terminate_reason() :: normal | stop | timeout
- | remote | {remote, close_code(), binary()}
+ | remote | {remote, cow_ws:close_code(), binary()}
| {error, badencoding | badframe | closed | atom()}
| {crash, error | exit | throw, any()}.
@@ -47,15 +34,15 @@
-callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
- | {reply, frame() | [frame()], Req, State}
- | {reply, frame() | [frame()], Req, State, hibernate}
+ | {reply, cow_ws:frame() | [cow_ws:frame()], Req, State}
+ | {reply, cow_ws:frame() | [cow_ws:frame()], Req, State, hibernate}
| {stop, Req, State}
when Req::cowboy_req:req(), State::any().
-callback websocket_info(any(), Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
- | {reply, frame() | [frame()], Req, State}
- | {reply, frame() | [frame()], Req, State, hibernate}
+ | {reply, cow_ws:frame() | [cow_ws:frame()], Req, State}
+ | {reply, cow_ws:frame() | [cow_ws:frame()], Req, State, hibernate}
| {stop, Req, State}
when Req::cowboy_req:req(), State::any().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
@@ -70,11 +57,10 @@
timeout_ref = undefined :: undefined | reference(),
messages = undefined :: undefined | {atom(), atom(), atom()},
hibernate = false :: boolean(),
- frag_state = undefined :: frag_state(),
- utf8_state = <<>> :: binary(),
- deflate_frame = false :: boolean(),
- inflate_state :: undefined | port(),
- deflate_state :: undefined | port()
+ frag_state = undefined :: cow_ws:frag_state(),
+ frag_buffer = <<>> :: binary(),
+ utf8_state = 0 :: cow_ws:utf8_state(),
+ extensions = #{} :: map()
}).
-spec upgrade(Req, Env, module(), any(), timeout(), run | hibernate)
@@ -117,48 +103,55 @@ websocket_upgrade(State, Req) ->
-spec websocket_extensions(#state{}, Req)
-> {ok, #state{}, Req} when Req::cowboy_req:req().
websocket_extensions(State, Req) ->
- case cowboy_req:parse_header(<<"sec-websocket-extensions">>, Req) of
- undefined ->
- {ok, State, cowboy_req:set_meta(websocket_compress, false, Req)};
- Extensions ->
- [Compress] = cowboy_req:get([resp_compress], Req),
- case lists:keyfind(<<"x-webkit-deflate-frame">>, 1, Extensions) of
- {<<"x-webkit-deflate-frame">>, []} when Compress =:= true ->
- Inflate = zlib:open(),
- Deflate = zlib:open(),
- % Since we are negotiating an unconstrained deflate-frame
- % then we must be willing to accept frames using the
- % maximum window size which is 2^15. The negative value
- % indicates that zlib headers are not used.
- ok = zlib:inflateInit(Inflate, -15),
- % Initialize the deflater with a window size of 2^15 bits and disable
- % the zlib headers.
- ok = zlib:deflateInit(Deflate, best_compression, deflated, -15, 8, default),
- {ok, State#state{
- deflate_frame = true,
- inflate_state = Inflate,
- deflate_state = Deflate
- }, cowboy_req:set_meta(websocket_compress, true, Req)};
- _ ->
- {ok, State, cowboy_req:set_meta(websocket_compress, false, Req)}
- end
+ [Compress] = cowboy_req:get([resp_compress], Req),
+ Req2 = cowboy_req:set_meta(websocket_compress, false, Req),
+ case {Compress, cowboy_req:parse_header(<<"sec-websocket-extensions">>, Req2)} of
+ {true, Extensions} when Extensions =/= undefined ->
+ websocket_extensions(State, Req2, Extensions, []);
+ _ ->
+ {ok, State, Req2}
end.
+websocket_extensions(State, Req, [], []) ->
+ {ok, State, Req};
+websocket_extensions(State, Req, [], [<<", ">>|RespHeader]) ->
+ {ok, State, cowboy_req:set_resp_header(<<"sec-websocket-extensions">>, lists:reverse(RespHeader), Req)};
+websocket_extensions(State=#state{extensions=Extensions}, Req, [{<<"permessage-deflate">>, Params}|Tail], RespHeader) ->
+ %% @todo Make deflate options configurable.
+ Opts = #{level => best_compression, mem_level => 8, strategy => default},
+ case cow_ws:negotiate_permessage_deflate(Params, Extensions, Opts) of
+ {ok, RespExt, Extensions2} ->
+ Req2 = cowboy_req:set_meta(websocket_compress, true, Req),
+ websocket_extensions(State#state{extensions=Extensions2},
+ Req2, Tail, [<<", ">>, RespExt|RespHeader]);
+ ignore ->
+ websocket_extensions(State, Req, Tail, RespHeader)
+ end;
+websocket_extensions(State=#state{extensions=Extensions}, Req, [{<<"x-webkit-deflate-frame">>, Params}|Tail], RespHeader) ->
+ %% @todo Make deflate options configurable.
+ Opts = #{level => best_compression, mem_level => 8, strategy => default},
+ case cow_ws:negotiate_x_webkit_deflate_frame(Params, Extensions, Opts) of
+ {ok, RespExt, Extensions2} ->
+ Req2 = cowboy_req:set_meta(websocket_compress, true, Req),
+ websocket_extensions(State#state{extensions=Extensions2},
+ Req2, Tail, [<<", ">>, RespExt|RespHeader]);
+ ignore ->
+ websocket_extensions(State, Req, Tail, RespHeader)
+ end;
+websocket_extensions(State, Req, [_|Tail], RespHeader) ->
+ websocket_extensions(State, Req, Tail, RespHeader).
+
-spec websocket_handshake(#state{}, Req, any())
-> {ok, Req, cowboy_middleware:env()}
| {suspend, module(), atom(), [any()]}
when Req::cowboy_req:req().
-websocket_handshake(State=#state{
- transport=Transport, key=Key, deflate_frame=DeflateFrame},
- Req, HandlerState) ->
+websocket_handshake(State=#state{transport=Transport, key=Key}, Req, HandlerState) ->
Challenge = base64:encode(crypto:hash(sha,
<< Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" >>)),
- Extensions = case DeflateFrame of
- false -> [];
- true -> [{<<"sec-websocket-extensions">>, <<"x-webkit-deflate-frame">>}]
- end,
- Req2 = cowboy_req:upgrade_reply(101, [{<<"upgrade">>, <<"websocket">>},
- {<<"sec-websocket-accept">>, Challenge}|Extensions], Req),
+ Req2 = cowboy_req:upgrade_reply(101, [
+ {<<"upgrade">>, <<"websocket">>},
+ {<<"sec-websocket-accept">>, Challenge}
+ ], Req),
%% Flush the resp_sent message before moving on.
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
State2 = handler_loop_timeout(State),
@@ -213,299 +206,53 @@ handler_loop(State=#state{socket=Socket, messages={OK, Closed, Error},
SoFar, websocket_info, Message, fun handler_before_loop/4)
end.
-%% All frames passing through this function are considered valid,
-%% with the only exception of text and close frames with a payload
-%% which may still contain errors.
-spec websocket_data(#state{}, Req, any(), binary())
-> {ok, Req, cowboy_middleware:env()}
| {suspend, module(), atom(), [any()]}
when Req::cowboy_req:req().
-%% RSV bits MUST be 0 unless an extension is negotiated
-%% that defines meanings for non-zero values.
-websocket_data(State, Req, HandlerState, << _:1, Rsv:3, _/bits >>)
- when Rsv =/= 0, State#state.deflate_frame =:= false ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% Invalid opcode. Note that these opcodes may be used by extensions.
-websocket_data(State, Req, HandlerState, << _:4, Opcode:4, _/bits >>)
- when Opcode > 2, Opcode =/= 8, Opcode =/= 9, Opcode =/= 10 ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% Control frames MUST NOT be fragmented.
-websocket_data(State, Req, HandlerState, << 0:1, _:3, Opcode:4, _/bits >>)
- when Opcode >= 8 ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% A frame MUST NOT use the zero opcode unless fragmentation was initiated.
-websocket_data(State=#state{frag_state=undefined}, Req, HandlerState,
- << _:4, 0:4, _/bits >>) ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% Non-control opcode when expecting control message or next fragment.
-websocket_data(State=#state{frag_state={nofin, _, _}}, Req, HandlerState,
- << _:4, Opcode:4, _/bits >>)
- when Opcode =/= 0, Opcode < 8 ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% Close control frame length MUST be 0 or >= 2.
-websocket_data(State, Req, HandlerState, << _:4, 8:4, _:1, 1:7, _/bits >>) ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% Close control frame with incomplete close code. Need more data.
-websocket_data(State, Req, HandlerState,
- Data = << _:4, 8:4, 1:1, Len:7, _/bits >>)
- when Len > 1, byte_size(Data) < 8 ->
- handler_before_loop(State, Req, HandlerState, Data);
-%% 7 bits payload length.
-websocket_data(State, Req, HandlerState, << Fin:1, Rsv:3/bits, Opcode:4, 1:1,
- Len:7, MaskKey:32, Rest/bits >>)
- when Len < 126 ->
- websocket_data(State, Req, HandlerState,
- Opcode, Len, MaskKey, Rest, Rsv, Fin);
-%% 16 bits payload length.
-websocket_data(State, Req, HandlerState, << Fin:1, Rsv:3/bits, Opcode:4, 1:1,
- 126:7, Len:16, MaskKey:32, Rest/bits >>)
- when Len > 125, Opcode < 8 ->
- websocket_data(State, Req, HandlerState,
- Opcode, Len, MaskKey, Rest, Rsv, Fin);
-%% 63 bits payload length.
-websocket_data(State, Req, HandlerState, << Fin:1, Rsv:3/bits, Opcode:4, 1:1,
- 127:7, 0:1, Len:63, MaskKey:32, Rest/bits >>)
- when Len > 16#ffff, Opcode < 8 ->
- websocket_data(State, Req, HandlerState,
- Opcode, Len, MaskKey, Rest, Rsv, Fin);
-%% When payload length is over 63 bits, the most significant bit MUST be 0.
-websocket_data(State, Req, HandlerState, << _:8, 1:1, 127:7, 1:1, _:7, _/bits >>) ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% All frames sent from the client to the server are masked.
-websocket_data(State, Req, HandlerState, << _:8, 0:1, _/bits >>) ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% For the next two clauses, it can be one of the following:
-%%
-%% * The minimal number of bytes MUST be used to encode the length
-%% * All control frames MUST have a payload length of 125 bytes or less
-websocket_data(State, Req, HandlerState, << _:9, 126:7, _:48, _/bits >>) ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-websocket_data(State, Req, HandlerState, << _:9, 127:7, _:96, _/bits >>) ->
- websocket_close(State, Req, HandlerState, {error, badframe});
-%% Need more data.
-websocket_data(State, Req, HandlerState, Data) ->
- handler_before_loop(State, Req, HandlerState, Data).
-
-%% Initialize or update fragmentation state.
--spec websocket_data(#state{}, Req, any(),
- opcode(), non_neg_integer(), mask_key(), binary(), rsv(), 0 | 1)
- -> {ok, Req, cowboy_middleware:env()}
- | {suspend, module(), atom(), [any()]}
- when Req::cowboy_req:req().
-%% The opcode is only included in the first frame fragment.
-websocket_data(State=#state{frag_state=undefined}, Req, HandlerState,
- Opcode, Len, MaskKey, Data, Rsv, 0) ->
- websocket_payload(State#state{frag_state={nofin, Opcode, <<>>}},
- Req, HandlerState, 0, Len, MaskKey, <<>>, 0, Data, Rsv);
-%% Subsequent frame fragments.
-websocket_data(State=#state{frag_state={nofin, _, _}}, Req, HandlerState,
- 0, Len, MaskKey, Data, Rsv, 0) ->
- websocket_payload(State, Req, HandlerState,
- 0, Len, MaskKey, <<>>, 0, Data, Rsv);
-%% Final frame fragment.
-websocket_data(State=#state{frag_state={nofin, Opcode, SoFar}},
- Req, HandlerState, 0, Len, MaskKey, Data, Rsv, 1) ->
- websocket_payload(State#state{frag_state={fin, Opcode, SoFar}},
- Req, HandlerState, 0, Len, MaskKey, <<>>, 0, Data, Rsv);
-%% Unfragmented frame.
-websocket_data(State, Req, HandlerState, Opcode, Len, MaskKey, Data, Rsv, 1) ->
- websocket_payload(State, Req, HandlerState,
- Opcode, Len, MaskKey, <<>>, 0, Data, Rsv).
-
--spec websocket_payload(#state{}, Req, any(),
- opcode(), non_neg_integer(), mask_key(), binary(), non_neg_integer(),
- binary(), rsv())
- -> {ok, Req, cowboy_middleware:env()}
- | {suspend, module(), atom(), [any()]}
- when Req::cowboy_req:req().
-%% Close control frames with a payload MUST contain a valid close code.
-websocket_payload(State, Req, HandlerState,
- Opcode=8, Len, MaskKey, <<>>, 0,
- << MaskedCode:2/binary, Rest/bits >>, Rsv) ->
- Unmasked = << Code:16 >> = websocket_unmask(MaskedCode, MaskKey, <<>>),
- if Code < 1000; Code =:= 1004; Code =:= 1005; Code =:= 1006;
- (Code > 1011) and (Code < 3000); Code > 4999 ->
+websocket_data(State=#state{frag_state=FragState, extensions=Extensions}, Req, HandlerState, Data) ->
+ case cow_ws:parse_header(Data, Extensions, FragState) of
+ %% All frames sent from the client to the server are masked.
+ {_, _, _, _, undefined, _} ->
websocket_close(State, Req, HandlerState, {error, badframe});
- true ->
- websocket_payload(State, Req, HandlerState,
- Opcode, Len - 2, MaskKey, Unmasked, byte_size(MaskedCode),
- Rest, Rsv)
- end;
-%% Text frames and close control frames MUST have a payload that is valid UTF-8.
-websocket_payload(State=#state{utf8_state=Incomplete},
- Req, HandlerState, Opcode, Len, MaskKey, Unmasked, UnmaskedLen,
- Data, Rsv)
- when (byte_size(Data) < Len) andalso ((Opcode =:= 1) orelse
- ((Opcode =:= 8) andalso (Unmasked =/= <<>>))) ->
- Unmasked2 = websocket_unmask(Data,
- rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
- {Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, false, State),
- case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
- false ->
- websocket_close(State2, Req, HandlerState, {error, badencoding});
- Utf8State ->
- websocket_payload_loop(State2#state{utf8_state=Utf8State},
- Req, HandlerState, Opcode, Len - byte_size(Data), MaskKey,
- << Unmasked/binary, Unmasked3/binary >>,
- UnmaskedLen + byte_size(Data), Rsv)
- end;
-websocket_payload(State=#state{utf8_state=Incomplete},
- Req, HandlerState, Opcode, Len, MaskKey, Unmasked, UnmaskedLen,
- Data, Rsv)
- when Opcode =:= 1; (Opcode =:= 8) and (Unmasked =/= <<>>) ->
- << End:Len/binary, Rest/bits >> = Data,
- Unmasked2 = websocket_unmask(End,
- rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
- {Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, true, State),
- case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
- <<>> ->
- websocket_dispatch(State2#state{utf8_state= <<>>},
- Req, HandlerState, Rest, Opcode,
- << Unmasked/binary, Unmasked3/binary >>);
- _ ->
- websocket_close(State2, Req, HandlerState, {error, badencoding})
- end;
-%% Fragmented text frames may cut payload in the middle of UTF-8 codepoints.
-websocket_payload(State=#state{frag_state={_, 1, _}, utf8_state=Incomplete},
- Req, HandlerState, Opcode=0, Len, MaskKey, Unmasked, UnmaskedLen,
- Data, Rsv)
- when byte_size(Data) < Len ->
- Unmasked2 = websocket_unmask(Data,
- rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
- {Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, false, State),
- case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
- false ->
- websocket_close(State2, Req, HandlerState, {error, badencoding});
- Utf8State ->
- websocket_payload_loop(State2#state{utf8_state=Utf8State},
- Req, HandlerState, Opcode, Len - byte_size(Data), MaskKey,
- << Unmasked/binary, Unmasked3/binary >>,
- UnmaskedLen + byte_size(Data), Rsv)
- end;
-websocket_payload(State=#state{frag_state={Fin, 1, _}, utf8_state=Incomplete},
- Req, HandlerState, Opcode=0, Len, MaskKey, Unmasked, UnmaskedLen,
- Data, Rsv) ->
- << End:Len/binary, Rest/bits >> = Data,
- Unmasked2 = websocket_unmask(End,
- rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
- {Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, Fin =:= fin, State),
- case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
- <<>> ->
- websocket_dispatch(State2#state{utf8_state= <<>>},
- Req, HandlerState, Rest, Opcode,
- << Unmasked/binary, Unmasked3/binary >>);
- Utf8State when is_binary(Utf8State), Fin =:= nofin ->
- websocket_dispatch(State2#state{utf8_state=Utf8State},
- Req, HandlerState, Rest, Opcode,
- << Unmasked/binary, Unmasked3/binary >>);
- _ ->
- websocket_close(State, Req, HandlerState, {error, badencoding})
- end;
-%% Other frames have a binary payload.
-websocket_payload(State, Req, HandlerState,
- Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Data, Rsv)
- when byte_size(Data) < Len ->
- Unmasked2 = websocket_unmask(Data,
- rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
- {Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, false, State),
- websocket_payload_loop(State2, Req, HandlerState,
- Opcode, Len - byte_size(Data), MaskKey,
- << Unmasked/binary, Unmasked3/binary >>, UnmaskedLen + byte_size(Data),
- Rsv);
-websocket_payload(State, Req, HandlerState,
- Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Data, Rsv) ->
- << End:Len/binary, Rest/bits >> = Data,
- Unmasked2 = websocket_unmask(End,
- rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
- {Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, true, State),
- websocket_dispatch(State2, Req, HandlerState, Rest, Opcode,
- << Unmasked/binary, Unmasked3/binary >>).
-
--spec websocket_inflate_frame(binary(), rsv(), boolean(), #state{}) ->
- {binary(), #state{}}.
-websocket_inflate_frame(Data, << Rsv1:1, _:2 >>, _,
- #state{deflate_frame = DeflateFrame} = State)
- when DeflateFrame =:= false orelse Rsv1 =:= 0 ->
- {Data, State};
-websocket_inflate_frame(Data, << 1:1, _:2 >>, false, State) ->
- Result = zlib:inflate(State#state.inflate_state, Data),
- {iolist_to_binary(Result), State};
-websocket_inflate_frame(Data, << 1:1, _:2 >>, true, State) ->
- Result = zlib:inflate(State#state.inflate_state,
- << Data/binary, 0:8, 0:8, 255:8, 255:8 >>),
- {iolist_to_binary(Result), State}.
-
--spec websocket_unmask(B, mask_key(), B) -> B when B::binary().
-websocket_unmask(<<>>, _, Unmasked) ->
- Unmasked;
-websocket_unmask(<< O:32, Rest/bits >>, MaskKey, Acc) ->
- T = O bxor MaskKey,
- websocket_unmask(Rest, MaskKey, << Acc/binary, T:32 >>);
-websocket_unmask(<< O:24 >>, MaskKey, Acc) ->
- << MaskKey2:24, _:8 >> = << MaskKey:32 >>,
- T = O bxor MaskKey2,
- << Acc/binary, T:24 >>;
-websocket_unmask(<< O:16 >>, MaskKey, Acc) ->
- << MaskKey2:16, _:16 >> = << MaskKey:32 >>,
- T = O bxor MaskKey2,
- << Acc/binary, T:16 >>;
-websocket_unmask(<< O:8 >>, MaskKey, Acc) ->
- << MaskKey2:8, _:24 >> = << MaskKey:32 >>,
- T = O bxor MaskKey2,
- << Acc/binary, T:8 >>.
-
-%% Because we unmask on the fly we need to continue from the right mask byte.
--spec rotate_mask_key(mask_key(), non_neg_integer()) -> mask_key().
-rotate_mask_key(MaskKey, UnmaskedLen) ->
- Left = UnmaskedLen rem 4,
- Right = 4 - Left,
- (MaskKey bsl (Left * 8)) + (MaskKey bsr (Right * 8)).
+ {Type, FragState2, Rsv, Len, MaskKey, Rest} ->
+ websocket_payload(State#state{frag_state=FragState2}, Req, HandlerState, Type, Len, MaskKey, Rsv, undefined, <<>>, 0, Rest);
+ more ->
+ handler_before_loop(State, Req, HandlerState, Data);
+ error ->
+ websocket_close(State, Req, HandlerState, {error, badframe})
+ end.
-%% Returns <<>> if the argument is valid UTF-8, false if not,
-%% or the incomplete part of the argument if we need more data.
--spec is_utf8(binary()) -> false | binary().
-is_utf8(Valid = <<>>) ->
- Valid;
-is_utf8(<< _/utf8, Rest/bits >>) ->
- is_utf8(Rest);
-%% 2 bytes. Codepages C0 and C1 are invalid; fail early.
-is_utf8(<< 2#1100000:7, _/bits >>) ->
- false;
-is_utf8(Incomplete = << 2#110:3, _:5 >>) ->
- Incomplete;
-%% 3 bytes.
-is_utf8(Incomplete = << 2#1110:4, _:4 >>) ->
- Incomplete;
-is_utf8(Incomplete = << 2#1110:4, _:4, 2#10:2, _:6 >>) ->
- Incomplete;
-%% 4 bytes. Codepage F4 may have invalid values greater than 0x10FFFF.
-is_utf8(<< 2#11110100:8, 2#10:2, High:6, _/bits >>) when High >= 2#10000 ->
- false;
-is_utf8(Incomplete = << 2#11110:5, _:3 >>) ->
- Incomplete;
-is_utf8(Incomplete = << 2#11110:5, _:3, 2#10:2, _:6 >>) ->
- Incomplete;
-is_utf8(Incomplete = << 2#11110:5, _:3, 2#10:2, _:6, 2#10:2, _:6 >>) ->
- Incomplete;
-%% Invalid.
-is_utf8(_) ->
- false.
+websocket_payload(State=#state{frag_state=FragState, utf8_state=Incomplete, extensions=Extensions},
+ Req, HandlerState, Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen, Data) ->
+ case cow_ws:parse_payload(Data, MaskKey, Incomplete, UnmaskedLen, Type, Len, FragState, Extensions, Rsv) of
+ {ok, CloseCode2, Payload, Utf8State, Rest} ->
+ websocket_dispatch(State#state{utf8_state=Utf8State},
+ Req, HandlerState, Type, << Unmasked/binary, Payload/binary >>, CloseCode2, Rest);
+ {ok, Payload, Utf8State, Rest} ->
+ websocket_dispatch(State#state{utf8_state=Utf8State},
+ Req, HandlerState, Type, << Unmasked/binary, Payload/binary >>, CloseCode, Rest);
+ {more, CloseCode2, Payload, Utf8State} ->
+ websocket_payload_loop(State#state{utf8_state=Utf8State},
+ Req, HandlerState, Type, Len - byte_size(Data), MaskKey, Rsv, CloseCode2,
+ << Unmasked/binary, Payload/binary >>, UnmaskedLen + byte_size(Data));
+ {more, Payload, Utf8State} ->
+ websocket_payload_loop(State#state{utf8_state=Utf8State},
+ Req, HandlerState, Type, Len - byte_size(Data), MaskKey, Rsv, CloseCode,
+ << Unmasked/binary, Payload/binary >>, UnmaskedLen + byte_size(Data));
+ Error = {error, _Reason} ->
+ websocket_close(State, Req, HandlerState, Error)
+ end.
--spec websocket_payload_loop(#state{}, Req, any(),
- opcode(), non_neg_integer(), mask_key(), binary(),
- non_neg_integer(), rsv())
- -> {ok, Req, cowboy_middleware:env()}
- | {suspend, module(), atom(), [any()]}
- when Req::cowboy_req:req().
websocket_payload_loop(State=#state{socket=Socket, transport=Transport,
messages={OK, Closed, Error}, timeout_ref=TRef},
- Req, HandlerState, Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv) ->
+ Req, HandlerState, Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen) ->
Transport:setopts(Socket, [{active, once}]),
receive
{OK, Socket, Data} ->
State2 = handler_loop_timeout(State),
websocket_payload(State2, Req, HandlerState,
- Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Data, Rsv);
+ Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen, Data);
{Closed, Socket} ->
handler_terminate(State, Req, HandlerState, {error, closed});
{Error, Socket, Reason} ->
@@ -514,55 +261,38 @@ websocket_payload_loop(State=#state{socket=Socket, transport=Transport,
websocket_close(State, Req, HandlerState, timeout);
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
websocket_payload_loop(State, Req, HandlerState,
- Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv);
+ Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen);
Message ->
handler_call(State, Req, HandlerState,
<<>>, websocket_info, Message,
fun (State2, Req2, HandlerState2, _) ->
websocket_payload_loop(State2, Req2, HandlerState2,
- Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv)
+ Type, Len, MaskKey, Rsv, CloseCode, Unmasked, UnmaskedLen)
end)
end.
--spec websocket_dispatch(#state{}, Req, any(), binary(), opcode(), binary())
- -> {ok, Req, cowboy_middleware:env()}
- | {suspend, module(), atom(), [any()]}
- when Req::cowboy_req:req().
-%% Continuation frame.
-websocket_dispatch(State=#state{frag_state={nofin, Opcode, SoFar}},
- Req, HandlerState, RemainingData, 0, Payload) ->
- websocket_data(State#state{frag_state={nofin, Opcode,
- << SoFar/binary, Payload/binary >>}}, Req, HandlerState, RemainingData);
-%% Last continuation frame.
-websocket_dispatch(State=#state{frag_state={fin, Opcode, SoFar}},
- Req, HandlerState, RemainingData, 0, Payload) ->
- websocket_dispatch(State#state{frag_state=undefined}, Req, HandlerState,
- RemainingData, Opcode, << SoFar/binary, Payload/binary >>);
-%% Text frame.
-websocket_dispatch(State, Req, HandlerState, RemainingData, 1, Payload) ->
- handler_call(State, Req, HandlerState, RemainingData,
- websocket_handle, {text, Payload}, fun websocket_data/4);
-%% Binary frame.
-websocket_dispatch(State, Req, HandlerState, RemainingData, 2, Payload) ->
- handler_call(State, Req, HandlerState, RemainingData,
- websocket_handle, {binary, Payload}, fun websocket_data/4);
-%% Close control frame.
-websocket_dispatch(State, Req, HandlerState, _RemainingData, 8, <<>>) ->
- websocket_close(State, Req, HandlerState, remote);
-websocket_dispatch(State, Req, HandlerState, _RemainingData, 8,
- << Code:16, Payload/bits >>) ->
- websocket_close(State, Req, HandlerState, {remote, Code, Payload});
-%% Ping control frame. Send a pong back and forward the ping to the handler.
-websocket_dispatch(State=#state{socket=Socket, transport=Transport},
- Req, HandlerState, RemainingData, 9, Payload) ->
- Len = payload_length_to_binary(byte_size(Payload)),
- Transport:send(Socket, << 1:1, 0:3, 10:4, 0:1, Len/bits, Payload/binary >>),
- handler_call(State, Req, HandlerState, RemainingData,
- websocket_handle, {ping, Payload}, fun websocket_data/4);
-%% Pong control frame.
-websocket_dispatch(State, Req, HandlerState, RemainingData, 10, Payload) ->
- handler_call(State, Req, HandlerState, RemainingData,
- websocket_handle, {pong, Payload}, fun websocket_data/4).
+websocket_dispatch(State=#state{socket=Socket, transport=Transport, frag_state=FragState, frag_buffer=SoFar, extensions=Extensions},
+ Req, HandlerState, Type0, Payload0, CloseCode0, RemainingData) ->
+ case cow_ws:make_frame(Type0, Payload0, CloseCode0, FragState) of
+ %% @todo Allow receiving fragments.
+ {fragment, nofin, _, Payload} ->
+ websocket_data(State#state{frag_buffer= << SoFar/binary, Payload/binary >>}, Req, HandlerState, RemainingData);
+ {fragment, fin, Type, Payload} ->
+ handler_call(State#state{frag_state=undefined, frag_buffer= <<>>}, Req, HandlerState, RemainingData,
+ websocket_handle, {Type, << SoFar/binary, Payload/binary >>}, fun websocket_data/4);
+ close ->
+ websocket_close(State, Req, HandlerState, remote);
+ {close, CloseCode, Payload} ->
+ websocket_close(State, Req, HandlerState, {remote, CloseCode, Payload});
+ Frame = ping ->
+ Transport:send(Socket, cow_ws:frame(pong, Extensions)),
+ handler_call(State, Req, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/4);
+ Frame = {ping, Payload} ->
+ Transport:send(Socket, cow_ws:frame({pong, Payload}, Extensions)),
+ handler_call(State, Req, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/4);
+ Frame ->
+ handler_call(State, Req, HandlerState, RemainingData, websocket_handle, Frame, fun websocket_data/4)
+ end.
-spec handler_call(#state{}, Req, any(), binary(), atom(), any(), fun())
-> {ok, Req, cowboy_middleware:env()}
@@ -579,42 +309,42 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
{reply, Payload, Req2, HandlerState2}
when is_list(Payload) ->
case websocket_send_many(Payload, State) of
- {ok, State2} ->
- NextState(State2, Req2, HandlerState2, RemainingData);
- {stop, State2} ->
- handler_terminate(State2, Req2, HandlerState2, stop);
- {{error, _} = Error, State2} ->
- handler_terminate(State2, Req2, HandlerState2, Error)
+ ok ->
+ NextState(State, Req2, HandlerState2, RemainingData);
+ stop ->
+ handler_terminate(State, Req2, HandlerState2, stop);
+ Error = {error, _} ->
+ handler_terminate(State, Req2, HandlerState2, Error)
end;
{reply, Payload, Req2, HandlerState2, hibernate}
when is_list(Payload) ->
case websocket_send_many(Payload, State) of
- {ok, State2} ->
- NextState(State2#state{hibernate=true},
+ ok ->
+ NextState(State#state{hibernate=true},
Req2, HandlerState2, RemainingData);
- {stop, State2} ->
- handler_terminate(State2, Req2, HandlerState2, stop);
- {{error, _} = Error, State2} ->
- handler_terminate(State2, Req2, HandlerState2, Error)
+ stop ->
+ handler_terminate(State, Req2, HandlerState2, stop);
+ Error = {error, _} ->
+ handler_terminate(State, Req2, HandlerState2, Error)
end;
{reply, Payload, Req2, HandlerState2} ->
case websocket_send(Payload, State) of
- {ok, State2} ->
- NextState(State2, Req2, HandlerState2, RemainingData);
- {stop, State2} ->
- handler_terminate(State2, Req2, HandlerState2, stop);
- {{error, _} = Error, State2} ->
- handler_terminate(State2, Req2, HandlerState2, Error)
+ ok ->
+ NextState(State, Req2, HandlerState2, RemainingData);
+ stop ->
+ handler_terminate(State, Req2, HandlerState2, stop);
+ Error = {error, _} ->
+ handler_terminate(State, Req2, HandlerState2, Error)
end;
{reply, Payload, Req2, HandlerState2, hibernate} ->
case websocket_send(Payload, State) of
- {ok, State2} ->
- NextState(State2#state{hibernate=true},
+ ok ->
+ NextState(State#state{hibernate=true},
Req2, HandlerState2, RemainingData);
- {stop, State2} ->
- handler_terminate(State2, Req2, HandlerState2, stop);
- {{error, _} = Error, State2} ->
- handler_terminate(State2, Req2, HandlerState2, Error)
+ stop ->
+ handler_terminate(State, Req2, HandlerState2, stop);
+ Error = {error, _} ->
+ handler_terminate(State, Req2, HandlerState2, Error)
end;
{stop, Req2, HandlerState2} ->
websocket_close(State, Req2, HandlerState2, stop)
@@ -630,103 +360,44 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
])
end.
-websocket_opcode(text) -> 1;
-websocket_opcode(binary) -> 2;
-websocket_opcode(close) -> 8;
-websocket_opcode(ping) -> 9;
-websocket_opcode(pong) -> 10.
-
--spec websocket_deflate_frame(opcode(), binary(), #state{}) ->
- {binary(), rsv(), #state{}}.
-websocket_deflate_frame(Opcode, Payload,
- State=#state{deflate_frame = DeflateFrame})
- when DeflateFrame =:= false orelse Opcode >= 8 ->
- {Payload, << 0:3 >>, State};
-websocket_deflate_frame(_, Payload, State=#state{deflate_state = Deflate}) ->
- Deflated = iolist_to_binary(zlib:deflate(Deflate, Payload, sync)),
- DeflatedBodyLength = erlang:size(Deflated) - 4,
- Deflated1 = case Deflated of
- << Body:DeflatedBodyLength/binary, 0:8, 0:8, 255:8, 255:8 >> -> Body;
- _ -> Deflated
- end,
- {Deflated1, << 1:1, 0:2 >>, State}.
-
--spec websocket_send(frame(), #state{})
--> {ok, #state{}} | {stop, #state{}} | {{error, atom()}, #state{}}.
-websocket_send(Type = close, State=#state{socket=Socket, transport=Transport}) ->
- Opcode = websocket_opcode(Type),
- case Transport:send(Socket, << 1:1, 0:3, Opcode:4, 0:8 >>) of
- ok -> {stop, State};
- Error -> {Error, State}
- end;
-websocket_send(Type, State=#state{socket=Socket, transport=Transport})
- when Type =:= ping; Type =:= pong ->
- Opcode = websocket_opcode(Type),
- {Transport:send(Socket, << 1:1, 0:3, Opcode:4, 0:8 >>), State};
-websocket_send({close, Payload}, State) ->
- websocket_send({close, 1000, Payload}, State);
-websocket_send({Type = close, StatusCode, Payload}, State=#state{
- socket=Socket, transport=Transport}) ->
- Opcode = websocket_opcode(Type),
- Len = 2 + iolist_size(Payload),
- %% Control packets must not be > 125 in length.
- true = Len =< 125,
- BinLen = payload_length_to_binary(Len),
- Transport:send(Socket,
- [<< 1:1, 0:3, Opcode:4, 0:1, BinLen/bits, StatusCode:16 >>, Payload]),
- {stop, State};
-websocket_send({Type, Payload0}, State=#state{socket=Socket, transport=Transport}) ->
- Opcode = websocket_opcode(Type),
- {Payload, Rsv, State2} = websocket_deflate_frame(Opcode, iolist_to_binary(Payload0), State),
- Len = iolist_size(Payload),
- %% Control packets must not be > 125 in length.
- true = if Type =:= ping; Type =:= pong ->
- Len =< 125;
- true ->
- true
- end,
- BinLen = payload_length_to_binary(Len),
- {Transport:send(Socket,
- [<< 1:1, Rsv/bits, Opcode:4, 0:1, BinLen/bits >>, Payload]), State2}.
-
--spec payload_length_to_binary(0..16#7fffffffffffffff)
- -> << _:7 >> | << _:23 >> | << _:71 >>.
-payload_length_to_binary(N) ->
- case N of
- N when N =< 125 -> << N:7 >>;
- N when N =< 16#ffff -> << 126:7, N:16 >>;
- N when N =< 16#7fffffffffffffff -> << 127:7, N:64 >>
+-spec websocket_send(cow_ws:frame(), #state{}) -> ok | stop | {error, atom()}.
+websocket_send(Frame, #state{socket=Socket, transport=Transport, extensions=Extensions}) ->
+ Res = Transport:send(Socket, cow_ws:frame(Frame, Extensions)),
+ case Frame of
+ close -> stop;
+ {close, _} -> stop;
+ {close, _, _} -> stop;
+ _ -> Res
end.
--spec websocket_send_many([frame()], #state{})
- -> {ok, #state{}} | {stop, #state{}} | {{error, atom()}, #state{}}.
-websocket_send_many([], State) ->
- {ok, State};
+-spec websocket_send_many([cow_ws:frame()], #state{}) -> ok | stop | {error, atom()}.
+websocket_send_many([], _) ->
+ ok;
websocket_send_many([Frame|Tail], State) ->
case websocket_send(Frame, State) of
- {ok, State2} -> websocket_send_many(Tail, State2);
- {stop, State2} -> {stop, State2};
- {Error, State2} -> {Error, State2}
+ ok -> websocket_send_many(Tail, State);
+ stop -> stop;
+ Error -> Error
end.
-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},
+websocket_close(State=#state{socket=Socket, transport=Transport, extensions=Extensions},
Req, HandlerState, Reason) ->
case Reason of
Normal when Normal =:= stop; Normal =:= timeout ->
- Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>);
+ Transport:send(Socket, cow_ws:frame({close, 1000, <<>>}, Extensions));
{error, badframe} ->
- Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1002:16 >>);
+ Transport:send(Socket, cow_ws:frame({close, 1002, <<>>}, Extensions));
{error, badencoding} ->
- Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1007:16 >>);
+ Transport:send(Socket, cow_ws:frame({close, 1007, <<>>}, Extensions));
{crash, _, _} ->
- Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1011:16 >>);
+ Transport:send(Socket, cow_ws:frame({close, 1011, <<>>}, Extensions));
remote ->
- Transport:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>);
+ Transport:send(Socket, cow_ws:frame(close, Extensions));
{remote, Code, _} ->
- Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, Code:16 >>)
+ Transport:send(Socket, cow_ws:frame({close, Code, <<>>}, Extensions))
end,
handler_terminate(State, Req, HandlerState, Reason).
diff --git a/test/cowboy_ct_hook.erl b/test/cowboy_ct_hook.erl
index 1586412..8b0c31e 100644
--- a/test/cowboy_ct_hook.erl
+++ b/test/cowboy_ct_hook.erl
@@ -17,7 +17,7 @@
-export([init/2]).
init(_, _) ->
- cowboy_test:start([cowboy, gun]),
- cowboy_test:make_certs(),
- error_logger:add_report_handler(cowboy_error_h),
+ ct_helper:start([cowboy, gun]),
+ ct_helper:make_certs_in_ets(),
+ error_logger:add_report_handler(ct_helper_error_h),
{ok, undefined}.
diff --git a/test/cowboy_error_h.erl b/test/cowboy_error_h.erl
deleted file mode 100644
index b4ae78f..0000000
--- a/test/cowboy_error_h.erl
+++ /dev/null
@@ -1,145 +0,0 @@
-%% Copyright (c) 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.
-
--module(cowboy_error_h).
--behaviour(gen_event).
-
-%% Public interface.
--export([ignore/3]).
-
-%% gen_event.
--export([init/1]).
--export([handle_event/2]).
--export([handle_call/2]).
--export([handle_info/2]).
--export([terminate/2]).
--export([code_change/3]).
-
-%% Public interface.
-
-%% Ignore crashes from Pid occuring in M:F/A.
-ignore(M, F, A) ->
- gen_event:call(error_logger, ?MODULE, {expect, {self(), M, F, A}}).
-
-%% gen_event.
-
-init(_) ->
- spawn(fun() -> error_logger:tty(false) end),
- {ok, []}.
-
-%% Ignore supervisor and progress reports.
-handle_event({info_report, _, {_, progress, _}}, State) ->
- {ok, State};
-handle_event({info_report, _, {_, std_info, _}}, State) ->
- {ok, State};
-handle_event({error_report, _, {_, supervisor_report, _}}, State) ->
- {ok, State};
-%% Ignore gun retry failures.
-handle_event({error_report, _, {_, crash_report,
- [[{initial_call, {gun, init, _}}, _, _,
- {error_info, {error, gone, _}}|_]|_]}},
- State) ->
- {ok, State};
-%% Ignore emulator reports that are a duplicate of what Ranch gives us.
-%%
-%% The emulator always sends strings for errors, which makes it very
-%% difficult to extract the information we need, hence the regexps.
-handle_event(Event = {error, GL, {emulator, _, Msg}}, State)
- when node(GL) =:= node() ->
- Result = re:run(Msg,
- "Error in process ([^\s]+).+? with exit value: "
- ".+?{stacktrace,\\[{([^,]+),([^,]+),(.+)",
- [{capture, all_but_first, list}]),
- case Result of
- nomatch ->
- write_event(Event),
- {ok, State};
- {match, [PidStr, MStr, FStr, Rest]} ->
- A = case Rest of
- "[]" ++ _ ->
- 0;
- "[" ++ Rest2 ->
- count_args(Rest2, 1, 0);
- _ ->
- {match, [AStr]} = re:run(Rest, "([^,]+).+",
- [{capture, all_but_first, list}]),
- list_to_integer(AStr)
- end,
- Crash = {list_to_pid(PidStr), list_to_existing_atom(MStr),
- list_to_existing_atom(FStr), A},
- case lists:member(Crash, State) of
- true ->
- {ok, lists:delete(Crash, State)};
- false ->
- write_event(Event),
- {ok, State}
- end
- end;
-handle_event(Event = {error, GL,
- {_, "Ranch listener" ++ _, [_, _, Pid, {[_, _,
- {stacktrace, [{M, F, A, _}|_]}|_], _}]}},
- State) when node(GL) =:= node() ->
- A2 = if is_list(A) -> length(A); true -> A end,
- Crash = {Pid, M, F, A2},
- case lists:member(Crash, State) of
- true ->
- {ok, lists:delete(Crash, State)};
- false ->
- write_event(Event),
- {ok, State}
- end;
-handle_event(Event = {_, GL, _}, State) when node(GL) =:= node() ->
- write_event(Event),
- {ok, State};
-handle_event(_, State) ->
- {ok, State}.
-
-handle_call({expect, Crash}, State) ->
- {ok, ok, [Crash, Crash|State]};
-handle_call(_, State) ->
- {ok, {error, bad_query}, State}.
-
-handle_info(_, State) ->
- {ok, State}.
-
-terminate(_, _) ->
- spawn(fun() -> error_logger:tty(true) end),
- ok.
-
-code_change(_, State, _) ->
- {ok, State}.
-
-%% Internal.
-
-write_event(Event) ->
- error_logger_tty_h:write_event(
- {erlang:universaltime(), Event},
- io).
-
-count_args("]" ++ _, N, 0) ->
- N;
-count_args("]" ++ Tail, N, Levels) ->
- count_args(Tail, N, Levels - 1);
-count_args("[" ++ Tail, N, Levels) ->
- count_args(Tail, N, Levels + 1);
-count_args("}" ++ Tail, N, Levels) ->
- count_args(Tail, N, Levels - 1);
-count_args("{" ++ Tail, N, Levels) ->
- count_args(Tail, N, Levels + 1);
-count_args("," ++ Tail, N, Levels = 0) ->
- count_args(Tail, N + 1, Levels);
-count_args("," ++ Tail, N, Levels) ->
- count_args(Tail, N, Levels);
-count_args([_|Tail], N, Levels) ->
- count_args(Tail, N, Levels).
diff --git a/test/cowboy_test.erl b/test/cowboy_test.erl
index f4a5706..cced1f9 100644
--- a/test/cowboy_test.erl
+++ b/test/cowboy_test.erl
@@ -15,83 +15,24 @@
-module(cowboy_test).
-compile(export_all).
-%% Start and stop applications and their dependencies.
-
-start(Apps) ->
- _ = [do_start(App) || App <- Apps],
- ok.
-
-do_start(App) ->
- case application:start(App) of
- ok ->
- ok;
- {error, {not_started, Dep}} ->
- do_start(Dep),
- do_start(App)
- end.
-
-%% SSL certificate creation and safekeeping.
-
-make_certs() ->
- {_, Cert, Key} = ct_helper:make_certs(),
- CertOpts = [{cert, Cert}, {key, Key}],
- Pid = spawn(fun() -> receive after infinity -> ok end end),
- ?MODULE = ets:new(?MODULE, [ordered_set, public, named_table,
- {heir, Pid, undefined}]),
- ets:insert(?MODULE, {cert_opts, CertOpts}),
- ok.
-
-get_certs() ->
- ets:lookup_element(?MODULE, cert_opts, 2).
-
-%% Quick configuration value retrieval.
-
-config(Key, Config) ->
- {_, Value} = lists:keyfind(Key, 1, Config),
- Value.
-
-%% Test case description.
-
-doc(String) ->
- ct:comment(String),
- ct:log(String).
-
-%% List of all test cases in the suite.
-
-all(Suite) ->
- lists:usort([F || {F, 1} <- Suite:module_info(exports),
- F =/= module_info,
- F =/= test, %% This is leftover from the eunit parse_transform...
- F =/= all,
- F =/= groups,
- string:substr(atom_to_list(F), 1, 5) =/= "init_",
- string:substr(atom_to_list(F), 1, 4) =/= "end_",
- string:substr(atom_to_list(F), 1, 3) =/= "do_"
- ]).
+-import(ct_helper, [config/2]).
%% Listeners initialization.
init_http(Ref, ProtoOpts, Config) ->
- {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
- {max_keepalive, 50},
- {timeout, 500}
- |ProtoOpts]),
+ {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], ProtoOpts),
Port = ranch:get_port(Ref),
[{type, tcp}, {port, Port}, {opts, []}|Config].
init_https(Ref, ProtoOpts, Config) ->
- Opts = get_certs(),
- {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
- {max_keepalive, 50},
- {timeout, 500}
- |ProtoOpts]),
+ Opts = ct_helper:get_certs_from_ets(),
+ {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], ProtoOpts),
Port = ranch:get_port(Ref),
[{type, ssl}, {port, Port}, {opts, Opts}|Config].
init_spdy(Ref, ProtoOpts, Config) ->
- Opts = get_certs(),
- {ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}],
- ProtoOpts),
+ Opts = ct_helper:get_certs_from_ets(),
+ {ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}], ProtoOpts),
Port = ranch:get_port(Ref),
[{type, ssl}, {port, Port}, {opts, Opts}|Config].
@@ -148,22 +89,17 @@ init_common_groups(Name = spdy_compress, Config, Mod) ->
%% Support functions for testing using Gun.
gun_open(Config) ->
- gun_open(Config, []).
+ gun_open(Config, #{}).
gun_open(Config, Opts) ->
- {ok, ConnPid} = gun:open("localhost", config(port, Config),
- [{retry, 0}, {type, config(type, Config)}|Opts]),
+ {ok, ConnPid} = gun:open("localhost", config(port, Config), Opts#{
+ retry => 0,
+ transport => config(type, Config)
+ }),
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, MRef) ->
- receive {'DOWN', MRef, process, ConnPid, gone} -> ok
+gun_down(ConnPid) ->
+ receive {gun_down, ConnPid, _, _, _, _} -> ok
after 500 -> error(timeout) end.
%% Support functions for testing using a raw socket.
@@ -183,18 +119,21 @@ 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),
+ {ok, Data} = Transport:recv(Socket, 0, 10000),
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),
+ {ok, Data} = Transport:recv(Socket, 0, 10000),
raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
{_, _} ->
Buffer
end.
+raw_recv({raw_client, Socket, Transport}, Length, Timeout) ->
+ Transport:recv(Socket, Length, Timeout).
+
raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
- {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000),
+ {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 10000),
ok.
diff --git a/test/eunit_SUITE.erl b/test/eunit_SUITE.erl
deleted file mode 100644
index 1d817c6..0000000
--- a/test/eunit_SUITE.erl
+++ /dev/null
@@ -1,22 +0,0 @@
-%% 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
-%% copyright notice and this permission notice appear in all copies.
-%%
-%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
--module(eunit_SUITE).
--compile(export_all).
-
-all() ->
- [eunit].
-
-eunit(_) ->
- ok = eunit:test({application, cowboy}).
diff --git a/test/handlers/asterisk_h.erl b/test/handlers/asterisk_h.erl
new file mode 100644
index 0000000..b5ff32d
--- /dev/null
+++ b/test/handlers/asterisk_h.erl
@@ -0,0 +1,16 @@
+%% This module echoes back the value the test is interested in.
+
+-module(asterisk_h).
+
+-export([init/2]).
+
+init(Req, Opts) ->
+ echo(cowboy_req:header(<<"x-echo">>, Req), Req, Opts).
+
+echo(What, Req, Opts) ->
+ F = binary_to_atom(What, latin1),
+ Value = case cowboy_req:F(Req) of
+ V when is_integer(V) -> integer_to_binary(V);
+ V -> V
+ end,
+ {ok, cowboy_req:reply(200, [], Value, Req), Opts}.
diff --git a/test/handlers/echo_h.erl b/test/handlers/echo_h.erl
new file mode 100644
index 0000000..71408c3
--- /dev/null
+++ b/test/handlers/echo_h.erl
@@ -0,0 +1,16 @@
+%% This module echoes back the value the test is interested in.
+
+-module(echo_h).
+
+-export([init/2]).
+
+init(Req, Opts) ->
+ echo(cowboy_req:binding(key, Req), Req, Opts).
+
+echo(What, Req, Opts) ->
+ F = binary_to_atom(What, latin1),
+ Value = case cowboy_req:F(Req) of
+ V when is_integer(V) -> integer_to_binary(V);
+ V -> V
+ end,
+ {ok, cowboy_req:reply(200, [], Value, Req), Opts}.
diff --git a/test/handlers/hello_h.erl b/test/handlers/hello_h.erl
new file mode 100644
index 0000000..3be7b6d
--- /dev/null
+++ b/test/handlers/hello_h.erl
@@ -0,0 +1,8 @@
+%% This module sends a hello world response.
+
+-module(hello_h).
+
+-export([init/2]).
+
+init(Req, Opts) ->
+ {ok, cowboy_req:reply(200, [], <<"Hello world!">>, Req), Opts}.
diff --git a/test/handlers/input_crash_h.erl b/test/handlers/input_crash_h.erl
index e941cca..8cb7cbc 100644
--- a/test/handlers/input_crash_h.erl
+++ b/test/handlers/input_crash_h.erl
@@ -6,5 +6,5 @@
-export([init/2]).
init(Req, content_length) ->
- cowboy_error_h:ignore(cow_http_hd, number, 2),
+ ct_helper_error_h:ignore(erlang, binary_to_integer, 1),
cowboy_req:parse_header(<<"content-length">>, Req).
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 1b819ae..314508b 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -16,11 +16,10 @@
-module(http_SUITE).
-compile(export_all).
--import(cowboy_test, [config/2]).
+-import(ct_helper, [config/2]).
-import(cowboy_test, [gun_open/1]).
--import(cowboy_test, [gun_monitor_open/1]).
--import(cowboy_test, [gun_monitor_open/2]).
--import(cowboy_test, [gun_is_gone/2]).
+-import(cowboy_test, [gun_open/2]).
+-import(cowboy_test, [gun_down/1]).
-import(cowboy_test, [raw_open/1]).
-import(cowboy_test, [raw_send/2]).
-import(cowboy_test, [raw_recv_head/1]).
@@ -41,7 +40,7 @@ all() ->
].
groups() ->
- Tests = cowboy_test:all(?MODULE) -- [
+ Tests = ct_helper:all(?MODULE) -- [
onresponse_crash, onresponse_reply, onresponse_capitalize,
parse_host, set_env_dispatch
],
@@ -95,18 +94,14 @@ init_per_group(Name = https_compress, Config) ->
init_per_group(onresponse, Config) ->
{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
- {max_keepalive, 50},
- {onresponse, fun do_onresponse_hook/4},
- {timeout, 500}
+ {onresponse, fun do_onresponse_hook/4}
]),
Port = ranch:get_port(onresponse),
[{type, tcp}, {port, Port}, {opts, []}|Config];
init_per_group(onresponse_capitalize, Config) ->
{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
- {max_keepalive, 50},
- {onresponse, fun do_onresponse_capitalize_hook/4},
- {timeout, 500}
+ {onresponse, fun do_onresponse_capitalize_hook/4}
]),
Port = ranch:get_port(onresponse_capitalize),
[{type, tcp}, {port, Port}, {opts, []}|Config];
@@ -117,17 +112,13 @@ init_per_group(parse_host, Config) ->
]}
]),
{ok, _} = cowboy:start_http(parse_host, 100, [{port, 0}], [
- {env, [{dispatch, Dispatch}]},
- {max_keepalive, 50},
- {timeout, 500}
+ {env, [{dispatch, Dispatch}]}
]),
Port = ranch:get_port(parse_host),
[{type, tcp}, {port, Port}, {opts, []}|Config];
init_per_group(set_env, Config) ->
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
- {env, [{dispatch, []}]},
- {max_keepalive, 50},
- {timeout, 500}
+ {env, [{dispatch, []}]}
]),
Port = ranch:get_port(set_env),
[{type, tcp}, {port, Port}, {opts, []}|Config].
@@ -342,26 +333,26 @@ echo_body_qs_max_length(Config) ->
ok.
error_init_after_reply(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config),
+ ConnPid = gun_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).
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ gun_down(ConnPid).
headers_dupe(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config),
+ ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/headers/dupe"),
- {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
%% Ensure that only one connection header was received.
[<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
- gun_is_gone(ConnPid, MRef).
+ gun_down(ConnPid).
http10_chunkless(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
+ ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
Ref = gun:get(ConnPid, "/chunked_response"),
- {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
- {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef),
- gun_is_gone(ConnPid, MRef).
+ {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
+ gun_down(ConnPid).
http10_hostless(Config) ->
Name = http10_hostless,
@@ -411,17 +402,17 @@ http10_keepalive_forced(Config) ->
end.
keepalive_max(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config),
+ ConnPid = gun_open(Config),
Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
- || _ <- lists:seq(1, 49)],
+ || _ <- lists:seq(1, 99)],
CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
_ = [begin
- {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
end || Ref <- Refs],
- {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef),
{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
- gun_is_gone(ConnPid, MRef).
+ gun_down(ConnPid).
keepalive_nl(Config) ->
ConnPid = gun_open(Config),
@@ -440,7 +431,7 @@ keepalive_stream_loop(Config) ->
ConnPid = gun_open(Config),
Refs = [begin
Ref = gun:post(ConnPid, "/loop_stream_recv",
- [{<<"transfer-encoding">>, <<"chunked">>}]),
+ [{<<"content-type">>, <<"application/octet-stream">>}]),
_ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
|| ID <- lists:seq(1, 250)],
gun:data(ConnPid, Ref, fin, <<>>),
@@ -481,9 +472,8 @@ multipart_chunked(Config) ->
"\r\n--OHai--\r\n"
"This is an epilogue."
>>,
- Ref = gun:post(ConnPid, "/multipart", [
- {<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>},
- {<<"transfer-encoding">>, <<"chunked">>}]),
+ Ref = gun:post(ConnPid, "/multipart",
+ [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}]),
gun:data(ConnPid, Ref, fin, Body),
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
{ok, RespBody} = gun:await_body(ConnPid, Ref),
@@ -614,10 +604,12 @@ rest_param_all(Config) ->
%% Content-Type without param.
Ref6 = gun:put(ConnPid, "/param_all",
[{<<"content-type">>, <<"text/plain">>}]),
+ gun:data(ConnPid, Ref6, fin, "Hello world!"),
{response, fin, 204, _} = gun:await(ConnPid, Ref6),
%% Content-Type with param.
Ref7 = gun:put(ConnPid, "/param_all",
[{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
+ gun:data(ConnPid, Ref7, fin, "Hello world!"),
{response, fin, 204, _} = gun:await(ConnPid, Ref7),
ok.
@@ -662,12 +654,13 @@ rest_keepalive(Config) ->
rest_keepalive_post(Config) ->
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)],
+ Refs = [begin
+ Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
+ gun:data(ConnPid, Ref1, fin, "Hello world!"),
+ Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
+ gun:data(ConnPid, Ref2, fin, "Hello world!"),
+ {Ref1, Ref2}
+ end || _ <- lists:seq(1, 5)],
_ = [begin
{response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
@@ -748,8 +741,8 @@ rest_resource_etags(Config) ->
{200, <<"\"etag-header-value\"">>, "tuple-strong"},
{200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
{200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
- {500, false, "binary-strong-unquoted"},
- {500, false, "binary-weak-unquoted"}
+ {400, false, "binary-strong-unquoted"},
+ {400, false, "binary-weak-unquoted"}
],
_ = [{Status, ETag, Type} = begin
{Ret, RespETag} = rest_resource_get_etag(Config, Type),
@@ -808,7 +801,7 @@ slowloris(Config) ->
try
[begin
ok = raw_send(Client, [C]),
- receive after 25 -> ok end
+ receive after 250 -> 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"
"Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
@@ -894,11 +887,11 @@ stream_body_set_resp(Config) ->
ok.
stream_body_set_resp_close(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config),
+ ConnPid = gun_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).
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref),
+ gun_down(ConnPid).
stream_body_set_resp_chunked(Config) ->
ConnPid = gun_open(Config),
@@ -909,12 +902,12 @@ stream_body_set_resp_chunked(Config) ->
ok.
stream_body_set_resp_chunked10(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
+ ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
- {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+ {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
- {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
- gun_is_gone(ConnPid, MRef).
+ {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
+ gun_down(ConnPid).
%% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
streamed_response(Config) ->
@@ -933,8 +926,8 @@ streamed_response(Config) ->
te_chunked(Config) ->
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
ConnPid = gun_open(Config),
- Ref = gun:post(ConnPid, "/echo/body",
- [{<<"transfer-encoding">>, <<"chunked">>}], Body),
+ Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
+ gun:data(ConnPid, Ref, fin, Body),
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
{ok, Body} = gun:await_body(ConnPid, Ref),
ok.
@@ -957,7 +950,7 @@ te_chunked_chopped(Config) ->
Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
ConnPid = gun_open(Config),
Ref = gun:post(ConnPid, "/echo/body",
- [{<<"transfer-encoding">>, <<"chunked">>}]),
+ [{<<"content-type">>, <<"text/plain">>}]),
_ = [begin
ok = gun:dbg_send_raw(ConnPid, << C >>),
receive after 10 -> ok end
@@ -971,7 +964,7 @@ te_chunked_delayed(Config) ->
Chunks = do_body_to_chunks(50, Body, []),
ConnPid = gun_open(Config),
Ref = gun:post(ConnPid, "/echo/body",
- [{<<"transfer-encoding">>, <<"chunked">>}]),
+ [{<<"content-type">>, <<"text/plain">>}]),
_ = [begin
ok = gun:dbg_send_raw(ConnPid, Chunk),
receive after 10 -> ok end
@@ -985,7 +978,7 @@ te_chunked_split_body(Config) ->
Chunks = do_body_to_chunks(50, Body, []),
ConnPid = gun_open(Config),
Ref = gun:post(ConnPid, "/echo/body",
- [{<<"transfer-encoding">>, <<"chunked">>}]),
+ [{<<"content-type">>, <<"text/plain">>}]),
_ = [begin
case Chunk of
<<"0\r\n\r\n">> ->
@@ -1009,7 +1002,7 @@ te_chunked_split_crlf(Config) ->
Chunks = do_body_to_chunks(50, Body, []),
ConnPid = gun_open(Config),
Ref = gun:post(ConnPid, "/echo/body",
- [{<<"transfer-encoding">>, <<"chunked">>}]),
+ [{<<"content-type">>, <<"text/plain">>}]),
_ = [begin
%% Split in the newline just before the end of the chunk.
Len = byte_size(Chunk) - (random:uniform(2) - 1),
diff --git a/test/http_SUITE_data/http_chunked.erl b/test/http_SUITE_data/http_chunked.erl
index 87a6852..6433f66 100644
--- a/test/http_SUITE_data/http_chunked.erl
+++ b/test/http_SUITE_data/http_chunked.erl
@@ -6,6 +6,8 @@
init(Req, Opts) ->
Req2 = cowboy_req:chunked_reply(200, Req),
+ %% Try an empty chunk to make sure the stream doesn't get closed.
+ cowboy_req:chunk([<<>>], Req2),
timer:sleep(100),
cowboy_req:chunk("chunked_handler\r\n", Req2),
timer:sleep(100),
diff --git a/test/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl
index f646df5..ee5c2b2 100644
--- a/test/http_SUITE_data/http_errors.erl
+++ b/test/http_SUITE_data/http_errors.erl
@@ -9,9 +9,9 @@ init(Req, _Opts) ->
case_init(Case, Req).
case_init(<<"init_before_reply">> = Case, _Req) ->
- cowboy_error_h:ignore(?MODULE, case_init, 2),
+ ct_helper_error_h:ignore(?MODULE, case_init, 2),
error(Case);
case_init(<<"init_after_reply">> = Case, Req) ->
- cowboy_error_h:ignore(?MODULE, case_init, 2),
+ ct_helper_error_h:ignore(?MODULE, case_init, 2),
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
error(Case).
diff --git a/test/http_SUITE_data/rest_missing_callbacks.erl b/test/http_SUITE_data/rest_missing_callbacks.erl
index e1fcac1..bf77c22 100644
--- a/test/http_SUITE_data/rest_missing_callbacks.erl
+++ b/test/http_SUITE_data/rest_missing_callbacks.erl
@@ -12,13 +12,13 @@ allowed_methods(Req, State) ->
{[<<"GET">>, <<"PUT">>], Req, State}.
content_types_accepted(Req, State) ->
- cowboy_error_h:ignore(cowboy_rest, process_content_type, 3),
+ ct_helper_error_h:ignore(cowboy_rest, process_content_type, 3),
{[
{<<"application/json">>, put_application_json}
], Req, State}.
content_types_provided(Req, State) ->
- cowboy_error_h:ignore(cowboy_rest, set_resp_body, 2),
+ ct_helper_error_h:ignore(cowboy_rest, set_resp_body, 2),
{[
{<<"text/plain">>, get_text_plain}
], Req, State}.
diff --git a/test/http_SUITE_data/rest_resource_etags.erl b/test/http_SUITE_data/rest_resource_etags.erl
index 1ea3005..25b3080 100644
--- a/test/http_SUITE_data/rest_resource_etags.erl
+++ b/test/http_SUITE_data/rest_resource_etags.erl
@@ -23,10 +23,10 @@ generate_etag(Req, State) ->
{<<"\"etag-header-value\"">>, Req, State};
%% Invalid return values from generate_etag/2.
<<"binary-strong-unquoted">> ->
- cowboy_error_h:ignore(cowboy_http, quoted_string, 2),
+ ct_helper_error_h:ignore(cow_http_hd, parse_etag, 1),
{<<"etag-header-value">>, Req, State};
<<"binary-weak-unquoted">> ->
- cowboy_error_h:ignore(cowboy_http, quoted_string, 2),
+ ct_helper_error_h:ignore(cow_http_hd, parse_etag, 1),
{<<"W/etag-header-value">>, Req, State}
end.
diff --git a/test/loop_handler_SUITE.erl b/test/loop_handler_SUITE.erl
index 5f69490..0fd8ad7 100644
--- a/test/loop_handler_SUITE.erl
+++ b/test/loop_handler_SUITE.erl
@@ -15,8 +15,8 @@
-module(loop_handler_SUITE).
-compile(export_all).
--import(cowboy_test, [config/2]).
--import(cowboy_test, [doc/1]).
+-import(ct_helper, [config/2]).
+-import(ct_helper, [doc/1]).
-import(cowboy_test, [gun_open/1]).
%% ct.
@@ -25,7 +25,7 @@ all() ->
cowboy_test:common_all().
groups() ->
- cowboy_test:common_groups(cowboy_test:all(?MODULE)).
+ cowboy_test:common_groups(ct_helper:all(?MODULE)).
init_per_group(Name, Config) ->
cowboy_test:init_common_groups(Name, Config, ?MODULE).
diff --git a/test/rfc7230_SUITE.erl b/test/rfc7230_SUITE.erl
new file mode 100644
index 0000000..15d1fc5
--- /dev/null
+++ b/test/rfc7230_SUITE.erl
@@ -0,0 +1,1388 @@
+%% Copyright (c) 2015, Loïc Hoguin <[email protected]>
+%%
+%% Permission to use, copy, modify, and/or distribute this software for any
+%% purpose with or without fee is hereby granted, provided that the above
+%% copyright notice and this permission notice appear in all copies.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+-module(rfc7230_SUITE).
+-compile(export_all).
+
+-import(ct_helper, [doc/1]).
+-import(cowboy_test, [raw_open/1]).
+-import(cowboy_test, [raw_send/2]).
+-import(cowboy_test, [raw_recv_head/1]).
+-import(cowboy_test, [raw_recv/3]).
+
+all() -> [{group, http}].
+
+groups() -> [{http, [parallel], ct_helper:all(?MODULE)}].
+
+init_per_group(Name = http, Config) ->
+ cowboy_test:init_http(Name = http, [
+ {env, [{dispatch, cowboy_router:compile(init_routes(Config))}]}
+ ], Config).
+
+end_per_group(Name, _) ->
+ ok = cowboy:stop_listener(Name).
+
+init_routes(_) -> [
+ {"localhost", [
+ {"/", hello_h, []},
+ {"/echo/:key", echo_h, []}
+%% @todo Something is clearly wrong about routing * right now.
+%% {"*", asterisk_h, []}
+ ]},
+ {"127.0.0.1", [{"/echo/:key", echo_h, []}]},
+ {"example.org", [{"/echo/:key", echo_h, []}]}
+%% @todo Add IPv6 addresses support to the router. This fails:
+%% {"[2001:db8:85a3::8a2e:370:7334]", [{"/echo/:key", echo_h, []}]}
+].
+
+do_raw(Config, Data) ->
+ Client = raw_open(Config),
+ ok = raw_send(Client, Data),
+ {Version, Code, Reason, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
+ {Headers, Rest2} = cow_http:parse_headers(Rest),
+ case lists:keyfind(<<"content-length">>, 1, Headers) of
+ false ->
+ #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => <<>>};
+ {_, LengthBin} ->
+ Length = binary_to_integer(LengthBin),
+ Body = if
+ byte_size(Rest2) =:= Length -> Rest2;
+ true ->
+ {ok, Body0} = raw_recv(Client, binary_to_integer(LengthBin) - byte_size(Rest2), 5000),
+ << Rest2/bits, Body0/bits >>
+ end,
+ #{client => Client, version => Version, code => Code, reason => Reason, headers => Headers, body => Body}
+ end.
+
+%% Listener.
+
+%% @todo Add to documentation.
+%The default port for "http" connections is 80. The connection
+%uses plain TCP. (RFC7230 2.7.1)
+%
+%The default port for "https" connections is 443. The connection
+%uses TLS. (RFC7230 2.7.2)
+%
+%Any other port may be used for either of them.
+
+%% Before the request.
+
+accept_at_least_1_empty_line(Config) ->
+ doc("A configurable number of empty lines (CRLF) preceding the request "
+ "must be ignored. At least 1 empty line must be ignored. (RFC7230 3.5)"),
+ #{code := 200} = do_raw(Config,
+ "\r\n"
+ "GET / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+reject_response(Config) ->
+ doc("When receiving a response instead of a request, identified by the "
+ "status-line which starts with the HTTP version, the server must "
+ "reject the message with a 501 status code and close the connection. (RFC7230 3.1)"),
+ #{code := 501, client := Client} = do_raw(Config,
+ "HTTP/1.1 200 OK\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% Request.
+
+only_parse_necessary_elements(Config) ->
+ doc("It is only necessary to parse elements required to process the request. (RFC7230 2.5)"),
+ #{code := 200} = do_raw(Config,
+ "GET / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "Content-type: purposefully bad header value\r\n"
+ "\r\n").
+
+%% @todo Add to documentation.
+%Parsed elements are subject to configurable limits. A server must
+%be able to parse elements at least as long as it generates. (RFC7230 2.5)
+
+no_empty_line_after_request_line(Config) ->
+ doc("The general format of HTTP requests is strict. No empty line is "
+ "allowed in-between components except for the empty line "
+ "indicating the end of the list of headers."),
+ #{code := 400} = do_raw(Config,
+ "GET / HTTP/1.1\r\n"
+ "\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+no_empty_line_in_headers(Config) ->
+ doc("The general format of HTTP requests is strict. No empty line is "
+ "allowed in-between components except for the empty line "
+ "indicating the end of the list of headers."),
+ #{code := 400} = do_raw(Config,
+ "GET / HTTP/1.1\r\n"
+ "User-Agent: RFC7230\r\n"
+ "\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+timeout_before_request_line(Config) ->
+ doc("The time the request (request line and headers) takes to be "
+ "received by the server must be limited and subject to configuration. "
+ "No response must be sent before closing if no request was initiated "
+ "by the reception of a complete request-line."),
+ Client = raw_open(Config),
+ ok = raw_send(Client, "GET / HTTP/1.1\r"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+timeout_after_request_line(Config) ->
+ doc("The time the request (request line and headers) takes to be "
+ "received by the server must be limited and subject to configuration. "
+ "A 408 status code must be sent if the request line was received."),
+ #{code := 408, client := Client} = do_raw(Config, "GET / HTTP/1.1\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% @todo Add an HTTP/1.0 test suite.
+%An HTTP/1.1 server must understand any valid HTTP/1.0 request,
+%and respond to those with an HTTP/1.1 message that only use
+%features understood or safely ignored by HTTP/1.0 clients. (RFC7230 A)
+
+%% Request line.
+
+limit_request_line_8000(Config) ->
+ doc("It is recommended to limit the request-line length to a configurable "
+ "limit of at least 8000 octets."),
+ LongPath = ["/long-path" || _ <- lists:seq(1, 799)],
+ #{code := 200} = do_raw(Config, [
+ "GET /?qs=", LongPath, " HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"]).
+
+limit_request_line_9000(Config) ->
+ doc("It is recommended to limit the request-line length to a configurable "
+ "limit of at least 8000 octets. A request line too long must be rejected "
+ "with a 414 status code and the closing of the connection. (RFC7230 3.1.1)"),
+ LongPath = ["/long-path" || _ <- lists:seq(1, 899)],
+ #{code := 414, client := Client} = do_raw(Config, [
+ "GET /very", LongPath, " HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"]),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% Method.
+
+reject_invalid_method(Config) ->
+ doc("The request method is defined as 1+ token characters. An invalid "
+ "method must be rejected with a 400 status code and the "
+ "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET\0 / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+reject_empty_method(Config) ->
+ doc("The request method is defined as 1+ token characters. An empty "
+ "method must be rejected with a 400 status code and the "
+ "closing of the connection. (RFC7230 3.1.1, RFC7230 3.2.6)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ " / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% @todo We probably want to directly match commonly used methods.
+%In practice the only characters in use by registered methods are
+%uppercase letters [A-Z] and the dash "-". (IANA HTTP Method Registry)
+
+limit_method_name(Config) ->
+ doc("The length of the method must be subject to a configurable limit. "
+ "A method too long must be rejected with a 501 status code and the "
+ "closing of the connection. A good default for the method length limit "
+ "is the longest method length the server implements. (RFC7230 3.1.1)"),
+ LongMethod = [$G || _ <- lists:seq(1, 1000)],
+ #{code := 501, client := Client} = do_raw(Config, [
+ LongMethod, " / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"]),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% Between method and request-target.
+
+reject_tab_between_method_and_request_target(Config) ->
+ doc("A request that uses anything other than SP as separator between "
+ "the method and the request-target must be rejected with a 400 "
+ "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET\t/ HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+reject_two_sp_between_method_and_request_target(Config) ->
+ doc("A request that uses anything other than SP as separator between "
+ "the method and the request-target must be rejected with a 400 "
+ "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% Request target.
+
+ignore_uri_fragment_after_path(Config) ->
+ doc("The fragment part of the target URI is not sent. It must be "
+ "ignored by a server receiving it. (RFC7230 5.1)"),
+ Echo = <<"http://localhost/echo/url">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/url#fragment HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+ignore_uri_fragment_after_query(Config) ->
+ doc("The fragment part of the target URI is not sent. It must be "
+ "ignored by a server receiving it. (RFC7230 5.1)"),
+ Echo = <<"http://localhost/echo/url?key=value">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/url?key=value#fragment HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+%% Request target: origin-form.
+
+must_understand_origin_form(Config) ->
+ doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
+ #{code := 200} = do_raw(Config,
+ "GET / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+origin_form_reject_if_connect(Config) ->
+ doc("origin-form is used when the client does not connect to a proxy, "
+ "does not use the CONNECT method and does not issue a site-wide "
+ "OPTIONS request. (RFC7230 5.3.1)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "CONNECT / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% @todo Equivalent test for https.
+origin_form_tcp_scheme(Config) ->
+ doc("The scheme is either resolved from configuration or is \"https\" "
+ "when on a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
+ Echo = <<"http://localhost/echo/url">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/url HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+origin_form_path(Config) ->
+ doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
+ "or the end of the URI. (RFC3986 3.3)"),
+ Echo = <<"/echo/path">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/path HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+origin_form_path_query(Config) ->
+ doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
+ "or the end of the URI. (RFC3986 3.3)"),
+ Echo = <<"/echo/path">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/path?key=value HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+origin_form_path_fragment(Config) ->
+ doc("The absolute-path always starts with \"/\" and ends with either \"?\", \"#\" "
+ "or the end of the URI. (RFC3986 3.3)"),
+ Echo = <<"/echo/path">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/path#fragment HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+origin_form_query(Config) ->
+ doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
+ Echo = <<"key=value">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/qs?key=value HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+origin_form_query_fragment(Config) ->
+ doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
+ Echo = <<"key=value">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET /echo/qs?key=value#fragment HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+%% @todo origin_form: reject paths with too large depth or query strings with too many keys
+
+%% Request target: absolute-form.
+
+must_understand_absolute_form(Config) ->
+ doc("A server must be able to handle at least origin-form and absolute-form. (RFC7230 5.3.2)"),
+ #{code := 200} = do_raw(Config,
+ "GET http://localhost HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_case_insensitive_scheme(Config) ->
+ doc("The scheme is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
+ Echo = <<"http://localhost/echo/url">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET HttP://localhost/echo/url HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_case_insensitive_host(Config) ->
+ doc("The host is case insensitive and normally provided in lowercase. (RFC7230 2.7.3)"),
+ Echo = <<"http://localhost/echo/url">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://LoCaLHOsT/echo/url HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_reject_unknown_schemes(Config) ->
+ doc("Unknown schemes must be rejected with a 400 status code and the closing of the connection."),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET bad://localhost/ HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% @todo Equivalent test for https.
+absolute_form_drop_scheme_tcp(Config) ->
+ doc("The scheme provided with the request must be dropped. The effective "
+ "scheme is either resolved from configuration or is \"https\" when on "
+ "a TLS connection and \"http\" otherwise. (RFC7230 5.5)"),
+ Echo = <<"http://localhost/echo/url">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET https://localhost/echo/url HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_reject_userinfo(Config) ->
+ doc("An authority component with a userinfo component (and its "
+ "\"@\" delimiter) is invalid. The request must be rejected with "
+ "a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET http://username:password@localhost HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+absolute_form_reject_missing_host_without_path(Config) ->
+ doc("A URI with a missing host identifier is invalid. The request must "
+ "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET http:// HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+absolute_form_reject_missing_host_with_path(Config) ->
+ doc("A URI with a missing host identifier is invalid. The request must "
+ "be rejected with a 400 status code and the closing of the connection. (RFC7230 2.7.1)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET http:/// HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+absolute_form_ipv4(Config) ->
+ doc("Absolute form with an IPv4 address for the host. (RFC3986 3.2.2)"),
+ Echo = <<"127.0.0.1">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://127.0.0.1/echo/host HTTP/1.1\r\n"
+ "Host: 127.0.0.1\r\n"
+ "\r\n").
+
+absolute_form_ipv4_port(Config) ->
+ doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
+ Host = <<"127.0.0.1">>,
+ #{code := 200, body := Host} = do_raw(Config,
+ "GET http://127.0.0.1:8080/echo/host HTTP/1.1\r\n"
+ "Host: 127.0.0.1:8080\r\n"
+ "\r\n"),
+ Port = <<"8080">>,
+ #{code := 200, body := Port} = do_raw(Config,
+ "GET http://127.0.0.1:8080/echo/port HTTP/1.1\r\n"
+ "Host: 127.0.0.1:8080\r\n"
+ "\r\n").
+
+%% @todo We need the router to support IPv6 addresses to write proper tests for these:
+%absolute_form_ipv6(Config) ->
+%absolute_form_ipv6_ipv4(Config) ->
+%absolute_form_ipv6_zoneid(Config) ->
+
+absolute_form_reg_name(Config) ->
+ doc("Absolute form with a regular name for the host. (RFC3986 3.2.2)"),
+ Echo = <<"example.org">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://example.org/echo/host HTTP/1.1\r\n"
+ "Host: example.org\r\n"
+ "\r\n").
+
+absolute_form_reg_name_port(Config) ->
+ doc("Absolute form with an IPv4 address for the host and a port number. (RFC3986 3.2.2)"),
+ Host = <<"example.org">>,
+ #{code := 200, body := Host} = do_raw(Config,
+ "GET http://example.org:8080/echo/host HTTP/1.1\r\n"
+ "Host: example.org:8080\r\n"
+ "\r\n"),
+ Port = <<"8080">>,
+ #{code := 200, body := Port} = do_raw(Config,
+ "GET http://example.org:8080/echo/port HTTP/1.1\r\n"
+ "Host: example.org:8080\r\n"
+ "\r\n").
+
+absolute_form_limit_host(Config) ->
+ doc("The maximum length for the host component of the URI must be subject "
+ "to a configurable limit. A good default is 255 characters. "
+ "(RFC7230 3.1.1, RFC3986 3.2.2, RFC1034 3.1)"),
+ LongHost = ["host." || _ <- lists:seq(1, 100)],
+ #{code := 414, client := Client} = do_raw(Config, [
+ "GET http://", LongHost, "/ HTTP/1.1\r\n"
+ "Host: ", LongHost, "\r\n"
+ "\r\n"]),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+absolute_form_invalid_port_0(Config) ->
+ doc("Port number 0 is reserved. The request must be rejected and the connection closed."),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET http://localhost:0/ HTTP/1.1\r\n"
+ "Host: localhost:0\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+absolute_form_invalid_port_65536(Config) ->
+ doc("Port numbers above 65535 are invalid. The request must be rejected and the connection closed."),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET http://localhost:65536/ HTTP/1.1\r\n"
+ "Host: localhost:65536\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% @todo The RFC says to discard the Host header if we are a proxy,
+%% and replace it with the content of absolute-form. This means
+%% that we should probably keep the absolute-form value when
+%% operating in proxy mode. Otherwise the absolute-form value
+%% is simply dropped and the Host header is used.
+
+%% @todo The authority is sent both in the URI and in the host header.
+%% The authority from the URI must be dropped, and the host header
+%% must be used instead. (RFC7230 5.5)
+%%
+%% It is not possible to test that the absolute-form value is dropped
+%% because one of the Host header test ensures that the authority
+%% is the same in both, and errors out otherwise.
+
+absolute_form_path(Config) ->
+ doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
+ "or the end of the URI. (RFC3986 3.3)"),
+ Echo = <<"/echo/path">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://localhost/echo/path HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_path_query(Config) ->
+ doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
+ "or the end of the URI. (RFC3986 3.3)"),
+ Echo = <<"/echo/path">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://localhost/echo/path?key=value HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_path_fragment(Config) ->
+ doc("The path always starts with \"/\" and ends with either \"?\", \"#\" "
+ "or the end of the URI. (RFC3986 3.3)"),
+ Echo = <<"/echo/path">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://localhost/echo/path#fragment HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_no_path(Config) ->
+ doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
+ #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
+ "GET http://localhost HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_no_path_then_query(Config) ->
+ doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
+ #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
+ "GET http://localhost?key=value HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_no_path_then_fragment(Config) ->
+ doc("An empty path component is equivalent to \"/\". (RFC7230 2.7.3)"),
+ #{code := 200, body := <<"Hello world!">>} = do_raw(Config,
+ "GET http://localhost#fragment HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_query(Config) ->
+ doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
+ Echo = <<"key=value">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://localhost/echo/qs?key=value HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+absolute_form_query_fragment(Config) ->
+ doc("The query starts with \"?\" and ends with \"#\" or the end of the URI. (RFC3986 3.4)"),
+ Echo = <<"key=value">>,
+ #{code := 200, body := Echo} = do_raw(Config,
+ "GET http://localhost/echo/qs?key=value#fragment HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+%% @todo absolute_form: reject paths with too large depth or query strings with too many keys
+
+%% Request-target: authority-form.
+
+authority_form_reject_if_not_connect(Config) ->
+ doc("When the method is CONNECT, authority-form must be used. This "
+ "form does not apply to any other methods which must reject the "
+ "request with a 400 status code and the closing of the connection. (RFC7230 5.3.3)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET localhost:80 HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% @todo Implement CONNECT.
+%authority_form_reject_userinfo(Config) ->
+%An authority component with a userinfo component (and its
+%"@" delimiter) is invalid. The request must be rejected with
+%a 400 status code and the closing of the connection. (RFC7230 2.7.1)
+%
+%authority_form_limit_host(Config) ->
+%authority_form_limit_port0(Config) ->
+%authority_form_limit_port65536(Config) ->
+%
+%A request with a too long component of authority-form must be rejected with
+%a 414 status code and the closing of the connection. (RFC7230 3.1.1)
+%
+%The authority is either resolved from configuration or is taken
+%directly from authority-form. (RFC7230 5.5)
+%
+%authority_form_empty_path(Config) ->
+%authority_form_empty_query(Config) ->
+%The path and query are empty when using authority-form. (RFC7230 5.5)
+
+%% Request-target: asterisk-form.
+
+asterisk_form_reject_if_not_options(Config) ->
+ doc("asterisk-form is used for server-wide OPTIONS requests. "
+ "It is invalid with any other methods which must reject the "
+ "request with a 400 status code and the closing of the connection. (RFC7230 5.3.4)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET * HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+asterisk_form_empty_path(Config) ->
+ doc("The path is empty when using asterisk-form. (RFC7230 5.5)"),
+ #{code := 200, body := <<>>} = do_raw(Config,
+ "OPTIONS * HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "X-Echo: path\r\n"
+ "\r\n").
+
+asterisk_form_empty_query(Config) ->
+ doc("The query is empty when using asterisk-form. (RFC7230 5.5)"),
+ #{code := 200, body := <<>>} = do_raw(Config,
+ "OPTIONS * HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "X-Echo: query\r\n"
+ "\r\n").
+
+%% Invalid request-target.
+
+invalid_request_target(Config) ->
+ doc("Any other form is invalid and must be rejected with a 400 status code "
+ "and the closing of the connection."),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET \0 HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% Between request-target and version.
+
+reject_tab_between_request_target_and_version(Config) ->
+ doc("A request that uses anything other than SP as separator between "
+ "the request-target and the version must be rejected with a 400 "
+ "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET /\tHTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+reject_two_sp_between_request_target_and_version(Config) ->
+ doc("A request that uses anything other than SP as separator between "
+ "the request-target and the version must be rejected with a 400 "
+ "status code and the closing of the connection. (RFC7230 3.1.1, RFC7230 3.5)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET / HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% Request version.
+
+reject_invalid_version_http100(Config) ->
+ doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
+ "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
+ #{code := 505} = do_raw(Config,
+ "GET / HTTP/1.00\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+reject_invalid_version_http111(Config) ->
+ doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
+ "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
+ #{code := 505} = do_raw(Config,
+ "GET / HTTP/1.11\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+reject_invalid_version_http12(Config) ->
+ doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
+ "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
+ #{code := 505} = do_raw(Config,
+ "GET / HTTP/1.2\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+reject_invalid_version_http2(Config) ->
+ doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
+ "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
+ #{code := 505} = do_raw(Config,
+ "GET / HTTP/2\r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+reject_empty_version(Config) ->
+ doc("Any version number other than HTTP/1.0 or HTTP/1.1 must be "
+ "rejected by a server or intermediary with a 505 status code. (RFC7230 2.6, RFC7230 A.2)"),
+ #{code := 505} = do_raw(Config,
+ "GET / \r\n"
+ "Host: localhost\r\n"
+ "\r\n").
+
+reject_invalid_whitespace_after_version(Config) ->
+ doc("A request that has whitespace different than CRLF following the "
+ "version must be rejected with a 400 status code and the closing "
+ "of the connection. (RFC7230 3.1.1)"),
+ #{code := 400, client := Client} = do_raw(Config,
+ "GET / HTTP/1.1 \r\n"
+ "Host: localhost\r\n"
+ "\r\n"),
+ {error, closed} = raw_recv(Client, 0, 1000).
+
+%% Request headers.
+
+%invalid_header(Config) ->
+%```
+%headers = *( header-field CRLF ) CRLF
+%header-field = field-name ":" OWS field-value OWS
+%
+%field-name = token
+%field-value = *( SP / HTAB / %21-7E / %80-FF )
+%
+%OWS = *( SP / HTAB )
+%```
+%
+%lower_case_header(Config) ->
+%upper_case_header(Config) ->
+%mixed_case_header(Config) ->
+%The header field name is case insensitive. (RFC7230 3.2)
+%
+%reject_whitespace_before_header_name(Config) ->
+%Messages that contain whitespace before the header name must
+%be rejected with a 400 status code and the closing of the
+%connection. (RFC7230 3.2.4)
+%
+%reject_whitespace_between_header_name_and_colon(Config) ->
+%Messages that contain whitespace between the header name and
+%colon must be rejected with a 400 status code and the closing
+%of the connection. (RFC7230 3.2.4)
+%
+%limit_header_name(Config) ->
+%The header name must be subject to a configurable limit. A
+%good default is 50 characters, well above the longest registered
+%header. Such a request must be rejected with a 431 status code
+%and the closing of the connection. (RFC7230 3.2.5, RFC6585 5, IANA Message Headers registry)
+%
+%limit_header_value(Config) ->
+%The header value and the optional whitespace around it must be
+%subject to a configurable limit. There is no recommendations
+%for the default. 4096 characters is known to work well. Such
+%a request must be rejected with a 431 status code and the closing
+%of the connection. (RFC7230 3.2.5, RFC6585 5)
+%
+%drop_whitespace_before_header_value(Config) ->
+%drop_whitespace_after_header_value(Config) ->
+%Optional whitespace before and after the header value is not
+%part of the value and must be dropped.
+%
+%@todo
+%The order of header fields with differing names is not significant. (RFC7230 3.2.2)
+%
+%@todo
+%The normal procedure for parsing headers is to read each header
+%field into a hash table by field name until the empty line. (RFC7230 3)
+%
+%reject_duplicate_content_length_header(Config) ->
+%reject_duplicate_host_header(Config) ->
+%Requests with duplicate content-length or host headers must be rejected
+%with a 400 status code and the closing of the connection. (RFC7230 3.3.2)
+%
+%combine_duplicate_headers(Config) ->
+%Other duplicate header fields must be combined by inserting a comma
+%between the values in the order they were received. (RFC7230 3.2.2)
+%
+%Duplicate header field names are only allowed when their value is
+%a comma-separated list. In practice there is no need to perform
+%a check while reading the headers as the value will become invalid
+%and the error can be handled while parsing the header later on. (RFC7230 3.2.2)
+%
+%wait_for_eoh_before_processing_request(Config) ->
+%The request must not be processed until all headers have arrived. (RFC7230 3.2.2)
+%
+%limit_headers(Config) ->
+%The number of headers allowed in a request must be subject to
+%a configurable limit. There is no recommendations for the default.
+%100 headers is known to work well. Such a request must be rejected
+%with a 431 status code and the closing of the connection. (RFC7230 3.2.5, RFC6585 5)
+%
+%@todo
+%When parsing header field values, the server must ignore empty
+%list elements, and not count those as the count of elements present. (RFC7230 7)
+%
+%@todo
+%The information in the via header is largely unreliable. (RFC7230 5.7.1)
+%
+%%% Request body.
+%
+%@todo
+%The message body is the octets after decoding any transfer
+%codings. (RFC7230 3.3)
+%
+%no_request_body(Config) ->
+%no_request_body_content_length_zero(Config) ->
+%request_body_content_length(Config) ->
+%request_body_transfer_encoding(Config) ->
+%A request has a message body only if it includes a transfer-encoding
+%header or a non-zero content-length header. (RFC7230 3.3)
+%
+%```
+%Transfer-Encoding = 1#transfer-coding
+%
+%transfer-coding = "chunked" / "compress" / "deflate" / "gzip" / transfer-extension
+%transfer-extension = token *( OWS ";" OWS transfer-parameter )
+%transfer-parameter = token BWS "=" BWS ( token / quoted-string )
+%```
+%
+%case_insensitive_transfer_encoding(Config) ->
+%The transfer-coding is case insensitive. (RFC7230 4)
+%
+%@todo
+%There are no known other transfer-extension with the exception of
+%deprecated aliases "x-compress" and "x-gzip". (IANA HTTP Transfer Coding Registry,
+%RFC7230 4.2.1, RFC7230 4.2.3, RFC7230 8.4.2)
+%
+%must_understand_chunked(Config) ->
+%A server must be able to handle at least chunked transfer-encoding.
+%This is also the only coding that sees widespread use. (RFC7230 3.3.1, RFC7230 4.1)
+%
+%reject_double_chunked_encoding(Config) ->
+%Messages encoded more than once with chunked transfer-encoding
+%must be rejected with a 400 status code and the closing of the
+%connection. (RFC7230 3.3.1)
+%
+%reject_non_terminal_chunked(Config) ->
+%Messages where chunked, when present, is not the last
+%transfer-encoding must be rejected with a 400 status code
+%and the closing of the connection. (RFC7230 3.3.3)
+%
+%@todo
+%Some non-conformant implementations send the "deflate" compressed
+%data without the zlib wrapper. (RFC7230 4.2.2)
+%
+%reject_unknown_transfer_encoding(Config) ->
+%Messages encoded with a transfer-encoding the server does not
+%understand must be rejected with a 501 status code and the
+%closing of the connection. (RFC7230 3.3.1)
+%
+%@todo
+%A server can reject requests with a body and no content-length
+%header with a 411 status code. (RFC7230 3.3.3)
+%
+%```
+%Content-Length = 1*DIGIT
+%```
+%
+%reject_invalid_content_length(Config) ->
+%A request with an invalid content-length header must be rejected
+%with a 400 status code and the closing of the connection. (RFC7230 3.3.3)
+%
+%@todo
+%The content-length header ranges from 0 to infinity. Requests
+%with a message body too large must be rejected with a 413 status
+%code and the closing of the connection. (RFC7230 3.3.2)
+%
+%ignore_content_length_when_transfer_encoding(Config) ->
+%When a message includes both transfer-encoding and content-length
+%headers, the content-length header must be removed before processing
+%the request. (RFC7230 3.3.3)
+%
+%socket_error_while_reading_body(Config) ->
+%If a socket error occurs while reading the body the server
+%must send a 400 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
+%
+%timeout_while_reading_body(Config) ->
+%If a timeout occurs while reading the body the server must
+%send a 408 status code response and close the connection. (RFC7230 3.3.3, RFC7230 3.4)
+%
+%%% Body length.
+%
+%body_length_chunked_before(Config) ->
+%The length of a message with a transfer-encoding header can
+%only be determined on decoding completion. (RFC7230 3.3.3)
+%
+%body_length_chunked_after(Config) ->
+%Upon completion of chunk decoding the server must add a content-length
+%header with the value set to the total length of data read. (RFC7230 4.1.3)
+%
+%body_length_content_length(Config) ->
+%The length of a message with a content-length header is
+%the numeric value in octets found in the header. (RFC7230 3.3.3)
+%
+%body_length_zero(Config) ->
+%A message with no transfer-encoding or content-length header
+%has a body length of 0. (RFC7230 3.3.3)
+%
+%%% Chunked transfer-encoding.
+%
+%reject_invalid_chunk_size(Config) ->
+%
+%```
+%chunked-body = *chunk last-chunk trailer-part CRLF
+%
+%chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
+%chunk-size = 1*HEXDIG
+%chunk-data = 1*OCTET ; a sequence of chunk-size octets
+%
+%last-chunk = 1*("0") [ chunk-ext ] CRLF
+%```
+%
+%The chunk-size field is a string of hex digits indicating the size of
+%the chunk-data in octets.
+%
+%```
+%chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
+%chunk-ext-name = token
+%chunk-ext-val = token / quoted-string
+%```
+%
+%ignore_unknown_chunk_extensions(Config) ->
+%Unknown chunk extensions must be ignored. (RFC7230 4.1.1)
+%
+%reject_invalid_chunk_extensions(Config) ->
+%
+%limit_chunk_size_line(Config) ->
+%The chunk-size line length must be subject to configuration.
+%There are no recommended defaults, although 100 octets should work.
+%Requests with a too long line must be rejected with a 400 status
+%code and the closing of the connection.
+%
+%reject_invalid_chunk_line_crlf(Config) ->
+%reject_invalid_chunk_data_crlf(Config) ->
+%
+%```
+%trailer-part = *( header-field CRLF )
+%```
+%
+%%% @todo see headers above and reject the same way, space etc.
+%reject_invalid_trailer_part(Config) ->
+%
+%ignore_trailer_transfer_encoding(Config) ->
+%ignore_trailer_content_length(Config) ->
+%ignore_trailer_host(Config) ->
+%ignore_trailer_cache_control(Config) ->
+%ignore_trailer_expect(Config) ->
+%ignore_trailer_max_forwards(Config) ->
+%ignore_trailer_pragma(Config) ->
+%ignore_trailer_range(Config) ->
+%ignore_trailer_te(Config) ->
+%ignore_trailer_if_match(Config) ->
+%ignore_trailer_if_none_match(Config) ->
+%ignore_trailer_if_modified_since(Config) ->
+%ignore_trailer_if_unmodified_since(Config) ->
+%ignore_trailer_if_range(Config) ->
+%ignore_trailer_www_authenticate(Config) ->
+%ignore_trailer_authorization(Config) ->
+%ignore_trailer_proxy_authenticate(Config) ->
+%ignore_trailer_proxy_authorization(Config) ->
+%ignore_trailer_content_encoding(Config) ->
+%ignore_trailer_content_type(Config) ->
+%ignore_trailer_content_range(Config) ->
+%ignore_trailer_trailer(Config) ->
+%
+%ignore_trailer_header(Config, Header) ->
+%Trailing headers must not include transfer-encoding, content-length,
+%host, cache-control, expect, max-forwards, pragma, range, te,
+%if-match, if-none-match, if-modified-since, if-unmodified-since,
+%if-range, www-authenticate, authorization, proxy-authenticate,
+%proxy-authorization, age, cache-control, expires, date, location,
+%retry-after, vary, warning, content-encoding, content-type,
+%content-range, or trailer. (RFC7230 4.1.2)
+%
+%Trailer headers can be ignored safely. (RFC7230 4.1.2)
+%
+%When trailer headers are processed, invalid headers must be ignored.
+%Valid headers must be added to the list of headers of the request. (RFC7230 4.1.2)
+%
+%limit_trailer_headers(Config) ->
+%The number of trailer headers must be subject to configuration.
+%There is no known recommendations for the default. A value of 10
+%should cover most cases. Requests with too many trailer headers
+%must be rejected with a 431 status code and the closing of the
+%connection. (RFC6585 5)
+%
+%remove_transfer_encoding_chunked_after_body_read(Config) ->
+%Upon completion of chunk decoding the server must remove "chunked"
+%from the transfer-encoding header. This header must be removed if
+%it becomes empty following this removal. (RFC7230 4.1.3)
+%
+%remove_trailer_after_body_read(Config) ->
+%Upon completion of chunk decoding the server must remove the trailer
+%header from the list of headers. (RFC7230 4.1.3)
+%
+%```
+%Trailer = 1#field-name
+%```
+%
+%ignore_chunked_headers_not_in_trailer(Config) ->
+%The trailer header can be used to list the headers found in the
+%trailer. A server must have the option of ignoring trailer headers
+%that were not listed in the trailer header. (RFC7230 4.4)
+%
+%ignore_chunked_headers_if_trailer_not_in_connection(Config) ->
+%The trailer header must be listed in the connection header field.
+%Trailers must be ignored otherwise.
+%
+%%% @todo Though we need a compatibility mode as some clients don't send it...
+%reject_chunked_missing_end_crlf(Config) ->
+%@todo ending CRLF
+%
+%%% Connection management.
+%
+%@todo can probably test using auth
+%Never assume any two requests on a single connection come
+%from the same user agent. (RFC7230 2.3)
+%
+%```
+%Connection = 1#token ; case-insensitive
+%```
+%
+%The connection token is either case insensitive "close", "keep-alive"
+%or a header field name.
+%
+%There are no corresponding "close" or "keep-alive" headers. (RFC7230 8.1, RFC7230 A.2)
+%
+%The connection header is valid only for the immediate connection,
+%alongside any header field it lists. (RFC7230 6.1)
+%
+%The server must determine if the connection is persistent for
+%every message received by looking at the connection header and
+%HTTP version. (RFC7230 6.3)
+%
+%no_connection_header_keepalive(Config) ->
+%%% @todo http/1.0 suite? connection_keepalive(Config) ->
+%HTTP/1.1 requests with no "close" option and HTTP/1.0 with the
+%"keep-alive" option indicate the connection will persist. (RFC7230 6.1, RFC7230 6.3)
+%
+%connection_close(Config) ->
+%%% @todo http/1.0 suite? no_connection_close(Config) ->
+%HTTP/1.1 requests with the "close" option and HTTP/1.0 with no
+%"keep-alive" option indicate the connection will be closed
+%upon reception of the response by the client. (RFC7230 6.1, RFC7230 6.3)
+%
+%limit_requests_keepalive(Config) ->
+%The maximum number of requests sent using a persistent connection
+%must be subject to configuration. The connection must be closed
+%when the limit is reached. (RFC7230 6.3)
+%
+%skip_request_body_by_closing_connection(Config) ->
+%%A server that doesn't want to read the entire body of a message
+%%must close the connection, if possible after sending the "close"
+%%connection option in the response. (RFC7230 6.3)
+%
+%pipeline(Config) ->
+%%% @todo pipeline_parallel (safe methods can, others can't)
+%A server can receive more than one request before any response
+%is sent. This is called pipelining. The requests can be processed
+%in parallel if they all have safe methods. Responses must be sent
+%in the same order as the requests. (RFC7230 6.3.2)
+%
+%@todo
+%The server must reject abusive traffic by closing the connection.
+%Abusive traffic can come from the form of too many requests in a
+%given amount of time, or too many concurrent connections. Limits
+%must be subject to configuration. (RFC7230 6.4)
+%
+%close_inactive_connections(Config) ->
+%The server must close inactive connections. The timeout
+%must be subject to configuration. (RFC7230 6.5)
+%
+%@todo
+%The server must monitor connections for the close signal
+%and close the socket on its end accordingly. (RFC7230 6.5)
+%
+%@todo
+%A connection close may occur at any time. (RFC7230 6.5)
+%
+%ignore_requests_after_connection_close(Config) ->
+%The server must not process any request after sending or
+%receiving the "close" connection option. (RFC7230 6.6)
+%
+%@todo
+%The server must close the connection in stages to avoid the
+%TCP reset problem. The server starts by closing the write
+%side of the socket. The server then reads until it detects
+%the socket has been closed, until it can be certain its
+%last response has been received by the client, or until
+%a close or timeout occurs. The server then fully close the
+%connection. (6.6)
+%
+%%% Routing.
+%
+%```
+%Host = authority ; same as authority-form
+%```
+%
+%reject_missing_host(Config) ->
+%An HTTP/1.1 request that lacks a host header must be rejected with
+%a 400 status code and the closing of the connection. (RFC7230 5.4)
+%
+%%% @todo http/1.0 missing_host(Config) ->
+%An HTTP/1.0 request that lack a host header is valid. Behavior
+%for these requests is configuration dependent. (RFC7230 5.5)
+%
+%reject_invalid_host(Config) ->
+%A request with an invalid host header must be rejected with a
+%400 status code and the closing of the connection. (RFC7230 5.4)
+%
+%reject_userinfo(Config) ->
+%An authority component with a userinfo component (and its
+%"@" delimiter) is invalid. The request must be rejected with
+%a 400 status code and the closing of the connection. (RFC7230 2.7.1)
+%
+%reject_absolute_form_different_host(Config) ->
+%When using absolute-form the URI authority component must be
+%identical to the host header. Invalid requests must be rejected
+%with a 400 status code and the closing of the connection. (RFC7230 5.4)
+%
+%reject_authority_form_different_host(Config) ->
+%When using authority-form the URI authority component must be
+%identical to the host header. Invalid requests must be rejected
+%with a 400 status code and the closing of the connection.
+%
+%empty_host(Config) ->
+%The host header is empty when the authority component is undefined. (RFC7230 5.4)
+%
+%@todo
+%The effective request URI can be rebuilt by concatenating scheme,
+%"://", authority, path and query components. (RFC7230 5.5)
+%
+%@todo
+%Resources with identical URI except for the scheme component
+%must be treated as different. (RFC7230 2.7.2)
+%
+%%% Response.
+%
+%@todo
+%A server can send more than one response per request only when a
+%1xx response is sent preceding the final response. (RFC7230 5.6)
+%
+%@todo
+%A server that does parallel pipelining must send responses in the
+%same order as the requests came in. (RFC7230 5.6)
+%
+%```
+%HTTP-response = status-line *( header-field CRLF ) CRLF [ message-body ]
+%```
+%
+%@todo
+%The response format must be followed strictly.
+%
+%```
+%status-line = HTTP-version SP status-code SP reason-phrase CRLF
+%status-code = 3DIGIT
+%reason-phrase = *( HTAB / SP / VCHAR / obs-text )
+%```
+%
+%http10_request_http11_response(Config) ->
+%A server must send its own version. (RFC7230 2.6)
+%
+%@todo
+%An HTTP/1.1 server may send an HTTP/1.0 version for compatibility purposes. (RFC7230 2.6)
+%
+%@todo
+%RFC6585 defines additional status code a server can use to reject
+%messages. (RFC7230 9.3, RFC6585)
+%
+%%% Response headers.
+%
+%@todo
+%In responses, OWS must be generated as SP or not generated
+%at all. RWS must be generated as SP. BWS must not be
+%generated. (RFC7230 3.2.3)
+%
+%```
+%header-field = field-name ":" SP field-value
+%
+%field-name = token ; case-insensitive
+%field-value = *( SP / %21-7E / %80-FF )
+%```
+%
+%@todo
+%In quoted-string found in field-value, quoted-pair must only be
+%used for DQUOTE and backslash. (RFC7230 3.2.6)
+%
+%@todo
+%HTTP header values must use US-ASCII encoding and must only send
+%printable characters or SP. (RFC7230 3.2.4, RFC7230 9.4)
+%
+%@todo
+%The server must not generate empty list elements in headers. (RFC7230 7)
+%
+%@todo
+%When encoding an URI as part of a response, only characters that
+%are reserved need to be percent-encoded. (RFC7230 2.7.3)
+%
+%special_set_cookie_handling(Config) ->
+%The set-cookie header must be handled as a special case. There
+%must be exactly one set-cookie header field per cookie. (RFC7230 3.2.2)
+%
+%@todo
+%The server must list headers for or about the immediate connection
+%in the connection header field. (RFC7230 6.1)
+%
+%@todo
+%A server that does not support persistent connections must
+%send "close" in every non-1xx response. (RFC7230 6.1)
+%
+%no_close_in_100_response(Config) ->
+%no_close_in_101_response(Config) ->
+%no_close_in_102_response(Config) ->
+%A server must not send a "close" connection option
+%in 1xx responses. (RFC7230 6.1)
+%
+%@todo
+%The "close" connection must be sent in a message when the
+%sender knows it will close the connection after fully sending
+%the response. (RFC7230 6.6)
+%
+%@todo
+%A server must close the connection after sending or
+%receiving a "close" once the response has been sent. (RFC7230 6.6)
+%
+%close_request_close_response(Config) ->
+%A server must send a "close" in a response to a request
+%containing a "close". (RFC7230 6.6)
+%
+%%% Response body.
+%
+%no_body_in_head_response(Config) -> %% @todo test different ways to send a body in response
+%Responses to HEAD requests never include a message body. (RFC7230 3.3)
+%
+%%% @todo Implement CONNECT
+%2xx responses to CONNECT requests never include a message
+%body. (RFC7230 3.3)
+%
+%no_body_in_100_response(Config) ->
+%no_body_in_101_response(Config) ->
+%no_body_in_102_response(Config) ->
+%no_body_in_204_response(Config) ->
+%no_body_in_304_response(Config) ->
+%1xx, 204 and 304 responses never include a message body. (RFC7230 3.3)
+%
+%same_content_length_as_get_in_head_response(Config) ->
+%same_transfer_encoding_as_get_in_head_response(Config) ->
+%same_content_length_as_200_in_304_response(Config) ->
+%same_transfer_encoding_as_200_in_304_response(Config) ->
+%Responses to HEAD requests and 304 responses can include a
+%content-length or transfer-encoding header. Their value must
+%be the same as if the request was an unconditional GET. (RFC7230 3.3, RFC7230 3.3.1, RFC7230 3.3.2)
+%
+%no_transfer_encoding_in_100_response(Config) ->
+%no_transfer_encoding_in_101_response(Config) ->
+%no_transfer_encoding_in_102_response(Config) ->
+%no_transfer_encoding_in_204_response(Config) ->
+%%% @todo CONNECT no_transfer_encoding_in_2xx_response_to_connect_request(Config) ->
+%no_content_length_in_100_response(Config) ->
+%no_content_length_in_101_response(Config) ->
+%no_content_length_in_102_response(Config) ->
+%no_content_length_in_204_response(Config) ->
+%%% @todo CONNECT no_content_length_in_2xx_response_to_connect_request(Config) ->
+%1xx, 204 responses and "2xx responses to CONNECT requests" must
+%not include a content-length or transfer-encoding header. (RFC7230 3.3.1, RFC7230 3.3.2)
+%
+%```
+%message-body = *OCTET
+%```
+%
+%The message body is the octets after decoding any transfer
+%codings. (RFC7230 3.3)
+%
+%content_length_0_when_no_body(Config) ->
+%content_length_response(Config) ->
+%When the length is known in advance, the server must send a
+%content-length header, including if the length is 0. (RFC7230 3.3.2, RFC7230 3.3.3)
+%
+%chunked_response(Config) ->
+%When the length is not known in advance, the chunked transfer-encoding
+%must be used. (RFC7230 3.3.2, RFC7230 3.3.3)
+%
+%compat_no_content_length_or_transfer_encoding_close_on_body_end(Config) ->
+%For compatibility purposes a server can send no content-length or
+%transfer-encoding header. In this case the connection must be
+%closed after the response has been sent fully. (RFC7230 3.3.2, RFC7230 3.3.3)
+%
+%no_content_length_if_transfer_encoding(Config) ->
+%The content-length header must not be sent when a transfer-encoding
+%header already exists. (RFC7230 3.3.2)
+%
+%@todo
+%The server must not apply the chunked transfer-encoding more than
+%once. (RFC7230 3.3.1)
+%
+%@todo
+%The server must apply the chunked transfer-encoding last. (RFC7230 3.3.1)
+%
+%http10_request_no_transfer_encoding_in_response(Config) ->
+%The transfer-encoding header must not be sent in responses to
+%HTTP/1.0 requests, or in responses that use the HTTP/1.0 version.
+%No transfer codings must be applied in these cases. (RFC7230 3.3.1)
+%
+%```
+%TE = #t-codings
+%
+%t-codings = "trailers" / ( transfer-coding [ t-ranking ] )
+%t-ranking = OWS ";" OWS "q=" rank
+%rank = ( "0" [ "." 0*3DIGIT ] ) / ( "1" [ "." 0*3("0") ] )
+%```
+%
+%no_te_no_trailers(Config) ->
+%te_trailers(Config) ->
+%Trailers can only be sent if the request includes a TE header
+%containing "trailers". (RFC7230 4.1.2)
+%
+%te_ignore_chunked(Config) ->
+%te_ignore_chunked_0(Config) ->
+%The presence of "chunked" in a TE header must be ignored as it
+%is always acceptable with HTTP/1.1. (RFC7230 4.3)
+%
+%%% @todo te_not_acceptable_coding(Config) ->
+%A qvalue of 0 in the TE header means "not acceptable". (RFC7230 4.3)
+%
+%@todo
+%The lack of a TE header or an empty TE header means only "chunked"
+%(with no trailers) or no transfer-encoding is acceptable. (RFC7230 4.3)
+%
+%ignore_te_if_not_in_connection_header(Config) ->
+%The TE header must be listed in the connection header field,
+%or must be ignored otherwise.
+%
+%@todo
+%Trailer headers must be listed in the trailer header field value. (RFC7230 4.4)
+%
+%@todo
+%When defined, the trailer header must also be listed in the connection header. (RFC7230 4.4)
+%
+%:: Upgrade
+%
+%```
+%Upgrade = 1#protocol
+%
+%protocol = protocol-name ["/" protocol-version]
+%protocol-name = token
+%protocol-version = token
+%```
+%
+%The upgrade header contains the list of protocols the
+%client wishes to upgrade to, in order of preference. (RFC7230 6.7)
+%
+%upgrade_safely_ignored(Config) ->
+%The upgrade header can be safely ignored. (RFC7230 6.7)
+%
+%upgrade_must_be_in_connection_header(Config) ->
+%The upgrade header must be listed under the connection header,
+%or must be ignored otherwise. (RFC7230 6.7)
+%
+%@todo
+%A server accepting an upgrade request must send a 101 status
+%code with a upgrade header listing the protocol(s) it upgrades
+%to, in layer-ascending order. In addition the upgrade header
+%must be listed in the connection header. (RFC7230 6.7)
+%
+%%A server must not switch to a protocol not listed in the
+%%request's upgrade header. (RFC7230 6.7)
+%
+%@todo
+%A server that sends a 426 status code must include a upgrade
+%header listing acceptable protocols in order of preference. (RFC7230 6.7)
+%
+%@todo
+%A server can send a upgrade header to any response to advertise
+%its support for other protocols listed in order of preference. (RFC7230 6.7)
+%
+%@todo
+%Immediately after a server responds with a 101 status code
+%it must respond to the original request using the new protocol. (RFC7230 6.7)
+%
+%@todo
+%%A server must not switch protocols unless the original message's
+%%semantics can be honored by the new protocol. OPTIONS requests
+%%can be honored by any protocol. (RFC7230 6.7)
+%
+%http10_ignore_upgrade_header(Config) ->
+%A server must ignore an upgrade header received by an HTTP/1.0
+%request. (RFC7230 6.7)
+%
+%expect_then_upgrade(Config) ->
+%A server receiving both an upgrade header and an expect header
+%containing "100-continue" must send a 100 response before the
+%101 response. (RFC7230 6.7)
+%
+%The upgrade header field cannot be used for switching the
+%connection protocol (e.g. TCP) or switching connections. (RFC7230 6.7)
+%
+%%% Compatibility.
+%
+%@todo
+%A server can choose to be non-conformant to the specifications
+%for the sake of compatibility. Such behavior can be enabled
+%through configuration and/or software identification. (RFC7230 2.5)
diff --git a/test/spdy_SUITE.erl b/test/spdy_SUITE.erl
index b26e5f8..c097e53 100644
--- a/test/spdy_SUITE.erl
+++ b/test/spdy_SUITE.erl
@@ -15,8 +15,8 @@
-module(spdy_SUITE).
-compile(export_all).
--import(cowboy_test, [config/2]).
--import(cowboy_test, [gun_monitor_open/1]).
+-import(ct_helper, [config/2]).
+-import(cowboy_test, [gun_open/1]).
-import(cowboy_test, [raw_open/1]).
-import(cowboy_test, [raw_send/2]).
@@ -26,7 +26,7 @@ all() ->
[{group, spdy}].
groups() ->
- [{spdy, [], cowboy_test:all(?MODULE)}].
+ [{spdy, [], ct_helper:all(?MODULE)}].
init_per_suite(Config) ->
case proplists:get_value(ssl_app, ssl:versions()) of
@@ -64,9 +64,9 @@ init_dispatch(Config) ->
%% Convenience functions.
-do_get(ConnPid, MRef, Host, Path) ->
- StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
- {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
+do_get(ConnPid, Host, Path) ->
+ StreamRef = gun:get(ConnPid, Path, [{<<"host">>, Host}]),
+ {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef),
{IsFin, Status}.
%% Tests.
@@ -80,25 +80,25 @@ check_status(Config) ->
{400, fin, "localhost", "bad-path"},
{404, fin, "localhost", "/this/path/does/not/exist"}
],
- {ConnPid, MRef} = gun_monitor_open(Config),
+ ConnPid = gun_open(Config),
_ = [{Status, Fin, Host, Path} = begin
- {IsFin, Ret} = do_get(ConnPid, MRef, Host, Path),
+ {IsFin, Ret} = do_get(ConnPid, Host, Path),
{Ret, IsFin, Host, Path}
end || {Status, Fin, Host, Path} <- Tests],
gun:close(ConnPid).
echo_body(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config),
+ ConnPid = gun_open(Config),
Body = << 0:800000 >>,
StreamRef = gun:post(ConnPid, "/echo/body", [
{<<"content-type">>, "application/octet-stream"}
], Body),
- {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
- {ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
+ {ok, Body} = gun:await_body(ConnPid, StreamRef),
gun:close(ConnPid).
echo_body_multi(Config) ->
- {ConnPid, MRef} = gun_monitor_open(Config),
+ ConnPid = gun_open(Config),
BodyChunk = << 0:80000 >>,
StreamRef = gun:post(ConnPid, "/echo/body", [
%% @todo I'm still unhappy with this. It shouldn't be required...
@@ -107,8 +107,8 @@ echo_body_multi(Config) ->
]),
_ = [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),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
+ {ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef),
gun:close(ConnPid).
two_frames_one_packet(Config) ->
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl
index e341e10..9cafd3c 100644
--- a/test/ws_SUITE.erl
+++ b/test/ws_SUITE.erl
@@ -15,7 +15,7 @@
-module(ws_SUITE).
-compile(export_all).
--import(cowboy_test, [config/2]).
+-import(ct_helper, [config/2]).
%% ct.
@@ -23,12 +23,9 @@ all() ->
[{group, autobahn}, {group, ws}].
groups() ->
- BaseTests = cowboy_test:all(?MODULE) -- [autobahn_fuzzingclient],
+ BaseTests = ct_helper:all(?MODULE) -- [autobahn_fuzzingclient],
[{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}].
-init_per_suite(Config) ->
- Config.
-
init_per_group(Name = autobahn, Config) ->
%% Some systems have it named pip2.
Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
@@ -41,7 +38,9 @@ init_per_group(Name = autobahn, Config) ->
{skip, "Autobahn Test Suite not installed."};
_ ->
{ok, _} = cowboy:start_http(Name, 100, [{port, 33080}], [
- {env, [{dispatch, init_dispatch()}]}]),
+ {env, [{dispatch, init_dispatch()}]},
+ {compress, true}
+ ]),
Config
end;
init_per_group(Name = ws, Config) ->