aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2015-03-12 19:22:19 +0100
committerLoïc Hoguin <[email protected]>2015-03-12 19:22:19 +0100
commitc409897f508eedff8ecc6f0860c9379fcc11bf23 (patch)
treed5a651df4ef5e8f9c6c6bb77366defa53c686e20
parentea2de24f18741fc89d7a1dd6a3a0a43f3ccb1fd4 (diff)
downloadgun-c409897f508eedff8ecc6f0860c9379fcc11bf23.tar.gz
gun-c409897f508eedff8ecc6f0860c9379fcc11bf23.tar.bz2
gun-c409897f508eedff8ecc6f0860c9379fcc11bf23.zip
Add initial Websocket support
All autobahntestsuite tests pass including the permessage-deflate compression tests. Some of the tests pass in a non-strict fashion. They are testing for protocol errors and expect events to happen in a particular order, which is not respected by Gun. Gun fails earlier than is expected due to concurrent processing of frames. The implementation when error occurs during handshake is probably a bit rough at this point. The documentation is also incomplete and/or wrong at this time, though this is the general state of the Gun documentation and will be resolved in a separate commit.
-rw-r--r--Makefile10
-rw-r--r--erlang.mk1329
-rw-r--r--src/gun.erl84
-rw-r--r--src/gun_http.erl160
-rw-r--r--src/gun_ws.erl125
-rw-r--r--test/gun_ct_hook.erl21
-rw-r--r--test/twitter_SUITE.erl34
-rw-r--r--test/ws_SUITE.erl176
-rw-r--r--test/ws_SUITE_data/server.json7
9 files changed, 1676 insertions, 270 deletions
diff --git a/Makefile b/Makefile
index 9d8a30f..ef34191 100644
--- a/Makefile
+++ b/Makefile
@@ -4,14 +4,18 @@ PROJECT = gun
# Options.
-CT_SUITES = twitter
+CT_SUITES = twitter ws
+CT_OPTS += -pa test -ct_hooks gun_ct_hook [] -boot start_sasl
+
PLT_APPS = ssl
# Dependencies.
DEPS = cowlib ranch
-dep_cowlib = pkg://cowlib master
-dep_ranch = pkg://ranch master
+dep_cowlib = git https://github.com/ninenines/cowlib 1.3.0
+
+TEST_DEPS = ct_helper
+dep_ct_helper = git https://github.com/extend/ct_helper.git master
# Standard targets.
diff --git a/erlang.mk b/erlang.mk
index 7da0151..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
@@ -12,72 +12,106 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-# Project.
+.PHONY: all deps app rel docs tests clean distclean help erlang-mk
-PROJECT ?= $(notdir $(CURDIR))
-
-# Packages database file.
-
-PKG_FILE ?= $(CURDIR)/.erlang.mk.packages.v1
-export PKG_FILE
+ERLANG_MK_VERSION = 1
-PKG_FILE_URL ?= https://raw.github.com/extend/erlang.mk/master/packages.v1.tsv
+# Core configuration.
-define get_pkg_file
- wget --no-check-certificate -O $(PKG_FILE) $(PKG_FILE_URL) || rm $(PKG_FILE)
-endef
+PROJECT ?= $(notdir $(CURDIR))
+PROJECT := $(strip $(PROJECT))
-# Verbosity and tweaks.
+# Verbosity.
V ?= 0
-appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
-appsrc_verbose = $(appsrc_verbose_$(V))
+gen_verbose_0 = @echo " GEN " $@;
+gen_verbose = $(gen_verbose_$(V))
-erlc_verbose_0 = @echo " ERLC " $(filter %.erl %.core,$(?F));
-erlc_verbose = $(erlc_verbose_$(V))
+# "erl" command.
-xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
-xyrl_verbose = $(xyrl_verbose_$(V))
+ERL = erl +A0 -noinput -boot start_clean
-dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
-dtl_verbose = $(dtl_verbose_$(V))
+# Core targets.
-gen_verbose_0 = @echo " GEN " $@;
-gen_verbose = $(gen_verbose_$(V))
+ifneq ($(words $(MAKECMDGOALS)),1)
+.NOTPARALLEL:
+endif
-.PHONY: rel clean-rel all clean-all app clean deps clean-deps \
- docs clean-docs build-tests tests build-plt dialyze
+all:: deps
+ @$(MAKE) --no-print-directory app
+ @$(MAKE) --no-print-directory rel
-# Release.
+# Noop to avoid a Make warning when there's nothing to do.
+rel::
+ @echo -n
-RELX_CONFIG ?= $(CURDIR)/relx.config
+clean:: clean-crashdump
-ifneq ($(wildcard $(RELX_CONFIG)),)
+clean-crashdump:
+ifneq ($(wildcard erl_crash.dump),)
+ $(gen_verbose) rm -f erl_crash.dump
+endif
-RELX ?= $(CURDIR)/relx
-export RELX
+distclean:: clean
+
+help::
+ @printf "%s\n" \
+ "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 [-jNUM] [target]" \
+ "" \
+ "Core targets:" \
+ " all Run deps, app and rel targets in that order" \
+ " deps Fetch dependencies (if needed) and compile them" \
+ " app Compile the project" \
+ " rel Build a release for this project, if applicable" \
+ " docs Build the documentation for this project" \
+ " tests Run the tests for this project" \
+ " clean Delete temporary and output files from most targets" \
+ " distclean Delete all temporary and output files" \
+ " help Display this help and exit" \
+ "" \
+ "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." \
+ "Parallel execution is supported through the -j Make flag."
+
+# Core functions.
+
+ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
+define core_http_get
+ wget --no-check-certificate -O $(1) $(2)|| rm $(1)
+endef
+else
+define core_http_get
+ $(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
-RELX_URL ?= https://github.com/erlware/relx/releases/download/v0.6.0/relx
-RELX_OPTS ?=
+# Automated update.
-define get_relx
- wget -O $(RELX) $(RELX_URL) || rm $(RELX)
- chmod +x $(RELX)
-endef
+ERLANG_MK_BUILD_CONFIG ?= build.config
+ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
-rel: clean-rel all $(RELX)
- @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
+erlang-mk:
+ git clone https://github.com/ninenines/erlang.mk $(ERLANG_MK_BUILD_DIR)
+ if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR); fi
+ cd $(ERLANG_MK_BUILD_DIR) && make
+ cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
+ rm -rf $(ERLANG_MK_BUILD_DIR)
-$(RELX):
- @$(call get_relx)
+# 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.
-clean-rel:
- @rm -rf _rel
+.PHONY: distclean-deps distclean-pkg pkg-list pkg-search
-endif
+# Configuration.
-# Deps directory.
+AUTOPATCH ?= edown gen_leader gproc
+export AUTOPATCH
DEPS_DIR ?= $(CURDIR)/deps
export DEPS_DIR
@@ -86,9 +120,6 @@ REBAR_DEPS_DIR = $(DEPS_DIR)
export REBAR_DEPS_DIR
ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
-ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
-
-# Application.
ifeq ($(filter $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
ifeq ($(ERL_LIBS),)
@@ -99,26 +130,206 @@ endif
endif
export ERL_LIBS
-ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
- +warn_shadow_vars +warn_obsolete_guard # +bin_opt_info +warn_missing_spec
+PKG_FILE2 ?= $(CURDIR)/.erlang.mk.packages.v2
+export PKG_FILE2
+
+PKG_FILE_URL ?= https://raw.githubusercontent.com/ninenines/erlang.mk/master/packages.v2.tsv
+
+# Core targets.
+
+deps:: $(ALL_DEPS_DIRS)
+ @for dep in $(ALL_DEPS_DIRS) ; do \
+ if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ] ; then \
+ $(MAKE) -C $$dep ; \
+ else \
+ echo "include $(CURDIR)/erlang.mk" | ERLC_OPTS=+debug_info $(MAKE) -f - -C $$dep ; \
+ fi ; \
+ done
+
+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); \
+ cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
+ 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; \
+ fi
+endef
+
+define dep_target
+$(DEPS_DIR)/$(1):
+ @mkdir -p $(DEPS_DIR)
+ifeq (,$(dep_$(1)))
+ @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
+ @DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
+ VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
+ REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
+ COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
+ $(call dep_fetch,$(1))
+else
+ @VS=$(word 1,$(dep_$(1))); \
+ REPO=$(word 2,$(dep_$(1))); \
+ 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))))
+
+distclean-deps:
+ $(gen_verbose) rm -rf $(DEPS_DIR)
+
+# Packages related targets.
+
+$(PKG_FILE2):
+ @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
+
+pkg-list: $(PKG_FILE2)
+ @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
+ "Name:\t\t" $$1 "\n" \
+ "Repository:\t" $$3 "\n" \
+ "Website:\t" $$5 "\n" \
+ "Description:\t" $$6 "\n" }'
+
+ifdef q
+pkg-search: $(PKG_FILE2)
+ @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
+ "Name:\t\t" $$1 "\n" \
+ "Repository:\t" $$3 "\n" \
+ "Website:\t" $$5 "\n" \
+ "Description:\t" $$6 "\n" }'
+else
+pkg-search:
+ $(error Usage: make pkg-search q=STRING)
+endif
+
+ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
+distclean-pkg:
+ $(gen_verbose) rm -f $(PKG_FILE2)
+endif
+
+help::
+ @printf "%s\n" "" \
+ "Package-related targets:" \
+ " pkg-list List all known packages" \
+ " pkg-search q=STRING Search for STRING in the package index"
+
+# 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_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-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
+ $(filter %.erl %.core,$(?F)));
+erlc_verbose = $(erlc_verbose_$(V))
-all: deps app
+xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
+xyrl_verbose = $(xyrl_verbose_$(V))
+
+mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
+mib_verbose = $(mib_verbose_$(V))
-clean-all: clean clean-deps clean-docs
- $(gen_verbose) rm -rf .$(PROJECT).plt $(DEPS_DIR) logs
+# Targets.
+
+ifeq ($(wildcard ebin/test),)
+app:: app-build
+else
+app:: clean app-build
+endif
-app: 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/.$$//'))
+ | sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
+ @if [ -z "$$(grep -E '^[^%]*{modules,' src/$(PROJECT).app.src)" ]; then \
+ echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
+ exit 1; \
+ fi
+ $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
$(appsrc_verbose) cat src/$(PROJECT).app.src \
- | sed 's/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/' \
+ | sed "s/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/" \
+ | 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
@@ -127,166 +338,938 @@ define compile_xyrl
@rm ebin/*.erl
endef
-define compile_dtl
- $(dtl_verbose) erl -noshell -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/"}]) \
- end, \
- _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
- init:stop()'
+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
-ebin/$(PROJECT).app: $(shell find src -type f -name \*.erl) \
- $(shell find src -type f -name \*.core) \
- $(shell find src -type f -name \*.xrl) \
- $(shell find src -type f -name \*.yrl) \
- $(shell find templates -type f -name \*.dtl 2>/dev/null)
+ifneq ($(wildcard src/),)
+ebin/$(PROJECT).app::
@mkdir -p ebin/
- $(if $(strip $(filter %.erl %.core,$?)), \
- $(call compile_erl,$(filter %.erl %.core,$?)))
- $(if $(strip $(filter %.xrl %.yrl,$?)), \
- $(call compile_xyrl,$(filter %.xrl %.yrl,$?)))
- $(if $(strip $(filter %.dtl,$?)), \
- $(call compile_dtl,$(filter %.dtl,$?)))
-clean:
- $(gen_verbose) rm -rf ebin/ test/*.beam erl_crash.dump
+ifneq ($(wildcard mibs/),)
+ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
+ @mkdir -p priv/mibs/ include
+ $(if $(strip $?),$(call compile_mib,$?))
+endif
-# Dependencies.
+ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl) \
+ $(shell find src -type f -name \*.core)
+ $(if $(strip $?),$(call compile_erl,$?))
-define get_dep
- @mkdir -p $(DEPS_DIR)
-ifeq (,$(findstring pkg://,$(word 1,$(dep_$(1)))))
- git clone -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1)
-else
- @if [ ! -f $(PKG_FILE) ]; then $(call get_pkg_file); fi
- git clone -n -- `awk 'BEGIN { FS = "\t" }; \
- $$$$1 == "$(subst pkg://,,$(word 1,$(dep_$(1))))" { print $$$$2 }' \
- $(PKG_FILE)` $(DEPS_DIR)/$(1)
+ebin/$(PROJECT).app:: $(shell find src -type f -name \*.xrl) \
+ $(shell find src -type f -name \*.yrl)
+ $(if $(strip $?),$(call compile_xyrl,$?))
endif
- cd $(DEPS_DIR)/$(1) ; git checkout -q $(word 2,$(dep_$(1)))
-endef
-define dep_target
-$(DEPS_DIR)/$(1):
- $(call get_dep,$(1))
-endef
+clean:: clean-app
-$(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
+clean-app:
+ $(gen_verbose) rm -rf ebin/ priv/mibs/ \
+ $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))
-deps: $(ALL_DEPS_DIRS)
- @for dep in $(ALL_DEPS_DIRS) ; do \
- if [ -f $$dep/Makefile ] ; then \
- $(MAKE) -C $$dep ; \
- else \
- echo "include $(CURDIR)/erlang.mk" | $(MAKE) -f - -C $$dep ; \
- fi ; \
- done
+# 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-deps:
- @for dep in $(ALL_DEPS_DIRS) ; do \
- if [ -f $$dep/Makefile ] ; then \
- $(MAKE) -C $$dep clean ; \
- else \
- echo "include $(CURDIR)/erlang.mk" | $(MAKE) -f - -C $$dep clean ; \
- fi ; \
- done
+.PHONY: test-deps test-dir test-build clean-test-dir
-# Documentation.
+# Configuration.
-EDOC_OPTS ?=
+TEST_DIR ?= test
-docs: clean-docs
- $(gen_verbose) erl -noshell \
- -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), init:stop().'
+ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
-clean-docs:
- $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
+TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
+TEST_ERLC_OPTS += -DTEST=1
-# Tests.
+# Targets.
$(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
-TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
-TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1 +'{parse_transform, eunit_autoexport}'
-
-build-test-deps: $(ALL_TEST_DEPS_DIRS)
+test-deps: $(ALL_TEST_DEPS_DIRS)
@for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
-build-tests: build-test-deps
- $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -o test/ \
- $(wildcard test/*.erl test/*/*.erl) -pa ebin/
+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-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
+
+# Core targets.
+
+help::
+ @printf "%s\n" "" \
+ "Bootstrap targets:" \
+ " bootstrap Generate a skeleton of an OTP application" \
+ " bootstrap-lib Generate a skeleton of an OTP library" \
+ " bootstrap-rel Generate the files needed to build a release" \
+ " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
+ " list-templates List available templates"
+
+# Bootstrap templates.
+
+bs_appsrc = "{application, $(PROJECT), [" \
+ " {description, \"\"}," \
+ " {vsn, \"0.1.0\"}," \
+ " {id, \"git\"}," \
+ " {modules, []}," \
+ " {registered, []}," \
+ " {applications, [" \
+ " kernel," \
+ " stdlib" \
+ " ]}," \
+ " {mod, {$(PROJECT)_app, []}}," \
+ " {env, []}" \
+ "]}."
+bs_appsrc_lib = "{application, $(PROJECT), [" \
+ " {description, \"\"}," \
+ " {vsn, \"0.1.0\"}," \
+ " {id, \"git\"}," \
+ " {modules, []}," \
+ " {registered, []}," \
+ " {applications, [" \
+ " kernel," \
+ " stdlib" \
+ " ]}" \
+ "]}."
+bs_Makefile = "PROJECT = $(PROJECT)" \
+ "include erlang.mk"
+bs_app = "-module($(PROJECT)_app)." \
+ "-behaviour(application)." \
+ "" \
+ "-export([start/2])." \
+ "-export([stop/1])." \
+ "" \
+ "start(_Type, _Args) ->" \
+ " $(PROJECT)_sup:start_link()." \
+ "" \
+ "stop(_State) ->" \
+ " ok."
+bs_relx_config = "{release, {$(PROJECT)_release, \"1\"}, [$(PROJECT)]}." \
+ "{extended_start_script, true}." \
+ "{sys_config, \"rel/sys.config\"}." \
+ "{vm_args, \"rel/vm.args\"}."
+bs_sys_config = "[" \
+ "]."
+bs_vm_args = "-name $(PROJECT)@127.0.0.1" \
+ "-setcookie $(PROJECT)" \
+ "-heart"
+# Normal templates.
+tpl_supervisor = "-module($(n))." \
+ "-behaviour(supervisor)." \
+ "" \
+ "-export([start_link/0])." \
+ "-export([init/1])." \
+ "" \
+ "start_link() ->" \
+ " supervisor:start_link({local, ?MODULE}, ?MODULE, [])." \
+ "" \
+ "init([]) ->" \
+ " Procs = []," \
+ " {ok, {{one_for_one, 1, 5}, Procs}}."
+tpl_gen_server = "-module($(n))." \
+ "-behaviour(gen_server)." \
+ "" \
+ "%% API." \
+ "-export([start_link/0])." \
+ "" \
+ "%% gen_server." \
+ "-export([init/1])." \
+ "-export([handle_call/3])." \
+ "-export([handle_cast/2])." \
+ "-export([handle_info/2])." \
+ "-export([terminate/2])." \
+ "-export([code_change/3])." \
+ "" \
+ "-record(state, {" \
+ "})." \
+ "" \
+ "%% API." \
+ "" \
+ "-spec start_link() -> {ok, pid()}." \
+ "start_link() ->" \
+ " gen_server:start_link(?MODULE, [], [])." \
+ "" \
+ "%% gen_server." \
+ "" \
+ "init([]) ->" \
+ " {ok, \#state{}}." \
+ "" \
+ "handle_call(_Request, _From, State) ->" \
+ " {reply, ignored, State}." \
+ "" \
+ "handle_cast(_Msg, State) ->" \
+ " {noreply, State}." \
+ "" \
+ "handle_info(_Info, State) ->" \
+ " {noreply, State}." \
+ "" \
+ "terminate(_Reason, _State) ->" \
+ " ok." \
+ "" \
+ "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)." \
+ "" \
+ "-export([init/3])." \
+ "-export([handle/2])." \
+ "-export([terminate/3])." \
+ "" \
+ "-record(state, {" \
+ "})." \
+ "" \
+ "init(_, Req, _Opts) ->" \
+ " {ok, Req, \#state{}}." \
+ "" \
+ "handle(Req, State=\#state{}) ->" \
+ " {ok, Req2} = cowboy_req:reply(200, Req)," \
+ " {ok, Req2, State}." \
+ "" \
+ "terminate(_Reason, _Req, _State) ->" \
+ " ok."
+tpl_cowboy_loop = "-module($(n))." \
+ "-behaviour(cowboy_loop_handler)." \
+ "" \
+ "-export([init/3])." \
+ "-export([info/3])." \
+ "-export([terminate/3])." \
+ "" \
+ "-record(state, {" \
+ "})." \
+ "" \
+ "init(_, Req, _Opts) ->" \
+ " {loop, Req, \#state{}, 5000, hibernate}." \
+ "" \
+ "info(_Info, Req, State) ->" \
+ " {loop, Req, State, hibernate}." \
+ "" \
+ "terminate(_Reason, _Req, _State) ->" \
+ " ok."
+tpl_cowboy_rest = "-module($(n))." \
+ "" \
+ "-export([init/3])." \
+ "-export([content_types_provided/2])." \
+ "-export([get_html/2])." \
+ "" \
+ "init(_, _Req, _Opts) ->" \
+ " {upgrade, protocol, cowboy_rest}." \
+ "" \
+ "content_types_provided(Req, State) ->" \
+ " {[{{<<\"text\">>, <<\"html\">>, '*'}, get_html}], Req, State}." \
+ "" \
+ "get_html(Req, State) ->" \
+ " {<<\"<html><body>This is REST!</body></html>\">>, Req, State}."
+tpl_cowboy_ws = "-module($(n))." \
+ "-behaviour(cowboy_websocket_handler)." \
+ "" \
+ "-export([init/3])." \
+ "-export([websocket_init/3])." \
+ "-export([websocket_handle/3])." \
+ "-export([websocket_info/3])." \
+ "-export([websocket_terminate/3])." \
+ "" \
+ "-record(state, {" \
+ "})." \
+ "" \
+ "init(_, _, _) ->" \
+ " {upgrade, protocol, cowboy_websocket}." \
+ "" \
+ "websocket_init(_, Req, _Opts) ->" \
+ " Req2 = cowboy_req:compact(Req)," \
+ " {ok, Req2, \#state{}}." \
+ "" \
+ "websocket_handle({text, Data}, Req, State) ->" \
+ " {reply, {text, Data}, Req, State};" \
+ "websocket_handle({binary, Data}, Req, State) ->" \
+ " {reply, {binary, Data}, Req, State};" \
+ "websocket_handle(_Frame, Req, State) ->" \
+ " {ok, Req, State}." \
+ "" \
+ "websocket_info(_Info, Req, State) ->" \
+ " {ok, Req, State}." \
+ "" \
+ "websocket_terminate(_Reason, _Req, _State) ->" \
+ " ok."
+tpl_ranch_protocol = "-module($(n))." \
+ "-behaviour(ranch_protocol)." \
+ "" \
+ "-export([start_link/4])." \
+ "-export([init/4])." \
+ "" \
+ "-type opts() :: []." \
+ "-export_type([opts/0])." \
+ "" \
+ "-record(state, {" \
+ " socket :: inet:socket()," \
+ " transport :: module()" \
+ "})." \
+ "" \
+ "start_link(Ref, Socket, Transport, Opts) ->" \
+ " Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts])," \
+ " {ok, Pid}." \
+ "" \
+ "-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok." \
+ "init(Ref, Socket, Transport, _Opts) ->" \
+ " ok = ranch:accept_ack(Ref)," \
+ " loop(\#state{socket=Socket, transport=Transport})." \
+ "" \
+ "loop(State) ->" \
+ " loop(State)."
+
+# Plugin-specific targets.
+
+bootstrap:
+ifneq ($(wildcard src/),)
+ $(error Error: src/ directory already exists)
+endif
+ @printf "%s\n" $(bs_Makefile) > Makefile
+ @mkdir src/
+ @printf "%s\n" $(bs_appsrc) > src/$(PROJECT).app.src
+ @printf "%s\n" $(bs_app) > src/$(PROJECT)_app.erl
+ $(eval n := $(PROJECT)_sup)
+ @printf "%s\n" $(tpl_supervisor) > src/$(PROJECT)_sup.erl
+
+bootstrap-lib:
+ifneq ($(wildcard src/),)
+ $(error Error: src/ directory already exists)
+endif
+ @printf "%s\n" $(bs_Makefile) > Makefile
+ @mkdir src/
+ @printf "%s\n" $(bs_appsrc_lib) > src/$(PROJECT).app.src
+
+bootstrap-rel:
+ifneq ($(wildcard relx.config),)
+ $(error Error: relx.config already exists)
+endif
+ifneq ($(wildcard rel/),)
+ $(error Error: rel/ directory already exists)
+endif
+ @printf "%s\n" $(bs_relx_config) > relx.config
+ @mkdir rel/
+ @printf "%s\n" $(bs_sys_config) > rel/sys.config
+ @printf "%s\n" $(bs_vm_args) > rel/vm.args
+
+new:
+ifeq ($(wildcard src/),)
+ $(error Error: src/ directory does not exist)
+endif
+ifndef t
+ $(error Usage: make new t=TEMPLATE n=NAME)
+endif
+ifndef tpl_$(t)
+ $(error Unknown template)
+endif
+ifndef n
+ $(error Usage: make new t=TEMPLATE n=NAME)
+endif
+ @printf "%s\n" $(tpl_$(t)) > src/$(n).erl
+
+list-templates:
+ @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))
+
+# 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: 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_DIR)),)
+ CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find $(TEST_DIR) -type f -name \*_SUITE.erl -exec basename {} \;)))
+else
+ CT_SUITES ?=
+endif
+
+# Core targets.
+
+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.
+
CT_RUN = ct_run \
-no_auto_compile \
- -noshell \
- -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
- -dir test \
- -logdir logs \
- $(CT_OPTS)
-
-CT_SUITES ?=
-
-define test_target
-test_$(1): ERLC_OPTS = $(TEST_ERLC_OPTS)
-test_$(1): clean deps app build-tests
- @if [ -d "test" ] ; \
- then \
- mkdir -p logs/ ; \
- $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) ; \
- fi
- $(gen_verbose) rm -f test/*.beam
+ -noinput \
+ -pa ebin $(DEPS_DIR)/*/ebin \
+ -dir $(TEST_DIR) \
+ -logdir logs
+
+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): test-build
+ @mkdir -p logs/
+ $(gen_verbose) $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) $(CT_OPTS)
endef
-$(foreach test,$(CT_SUITES),$(eval $(call test_target,$(test))))
+$(foreach test,$(CT_SUITES),$(eval $(call ct_suite_target,$(test))))
-tests: ERLC_OPTS = $(TEST_ERLC_OPTS)
-tests: clean deps app build-tests
- @if [ -d "test" ] ; \
- then \
- mkdir -p logs/ ; \
- $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) ; \
- fi
- $(gen_verbose) rm -f test/*.beam
+distclean-ct:
+ $(gen_verbose) rm -rf logs/
+
+# 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
-# Dialyzer.
+# Configuration.
DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt
export DIALYZER_PLT
PLT_APPS ?=
+DIALYZER_DIRS ?= --src -r src
DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
-Wunmatched_returns # -Wunderspecs
-build-plt: deps app
+# Core targets.
+
+distclean:: distclean-plt
+
+help::
+ @printf "%s\n" "" \
+ "Dialyzer targets:" \
+ " plt Build a PLT file for this project" \
+ " dialyze Analyze the project using Dialyzer"
+
+# Plugin-specific targets.
+
+$(DIALYZER_PLT): deps app
@dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
+plt: $(DIALYZER_PLT)
+
+distclean-plt:
+ $(gen_verbose) rm -f $(DIALYZER_PLT)
+
+ifneq ($(wildcard $(DIALYZER_PLT)),)
dialyze:
- @dialyzer --src src --no_native $(DIALYZER_OPTS)
+else
+dialyze: $(DIALYZER_PLT)
+endif
+ @dialyzer --no_native $(DIALYZER_DIRS) $(DIALYZER_OPTS)
-# Packages.
+# 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.
-$(PKG_FILE):
- @$(call get_pkg_file)
+.PHONY: distclean-edoc build-doc-deps
-pkg-list: $(PKG_FILE)
- @cat $(PKG_FILE) | awk 'BEGIN { FS = "\t" }; { print \
- "Name:\t\t" $$1 "\n" \
- "Repository:\t" $$2 "\n" \
- "Website:\t" $$3 "\n" \
- "Description:\t" $$4 "\n" }'
+# Configuration.
-ifdef q
-pkg-search: $(PKG_FILE)
- @cat $(PKG_FILE) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
- "Name:\t\t" $$1 "\n" \
- "Repository:\t" $$2 "\n" \
- "Website:\t" $$3 "\n" \
- "Description:\t" $$4 "\n" }'
+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));
+dtl_verbose = $(dtl_verbose_$(V))
+
+# Core targets.
+
+define compile_erlydtl
+ $(dtl_verbose) $(ERL) -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
+ Compile = fun(F) -> \
+ 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)", " ")], \
+ halt().'
+endef
+
+ifneq ($(wildcard src/),)
+ebin/$(PROJECT).app:: $(shell find templates -type f -name \*.dtl 2>/dev/null)
+ $(if $(strip $?),$(call compile_erlydtl,$?))
+endif
+
+# 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
-pkg-search:
- @echo "Usage: make pkg-search q=STRING"
+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
+
+# Configuration.
+
+RELX_CONFIG ?= $(CURDIR)/relx.config
+
+RELX ?= $(CURDIR)/relx
+export RELX
+
+RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.2.0/relx
+RELX_OPTS ?=
+RELX_OUTPUT_DIR ?= _rel
+
+ifeq ($(firstword $(RELX_OPTS)),-o)
+ RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS))
+else
+ RELX_OPTS += -o $(RELX_OUTPUT_DIR)
+endif
+
+# Core targets.
+
+ifneq ($(wildcard $(RELX_CONFIG)),)
+rel:: distclean-relx-rel relx-rel
+endif
+
+distclean:: distclean-relx-rel distclean-relx
+
+# Plugin-specific targets.
+
+define relx_fetch
+ $(call core_http_get,$(RELX),$(RELX_URL))
+ chmod +x $(RELX)
+endef
+
+$(RELX):
+ @$(call relx_fetch)
+
+relx-rel: $(RELX)
+ @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
+
+distclean-relx-rel:
+ $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR)
+
+distclean-relx:
+ $(gen_verbose) rm -rf $(RELX)
+
+# Copyright (c) 2014, M Robert Martin <[email protected]>
+# This file is contributed to erlang.mk and subject to the terms of the ISC License.
+
+.PHONY: shell
+
+# Configuration.
+
+SHELL_PATH ?= -pa $(CURDIR)/ebin $(DEPS_DIR)/*/ebin
+SHELL_OPTS ?=
+
+ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
+
+# Core targets
+
+help::
+ @printf "%s\n" "" \
+ "Shell targets:" \
+ " shell Run an erlang shell with SHELL_OPTS or reasonable default"
+
+# Plugin-specific targets.
+
+$(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep))))
+
+build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
+ @for dep in $(ALL_SHELL_DEPS_DIRS) ; do $(MAKE) -C $$dep ; done
+
+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/src/gun.erl b/src/gun.erl
index 71af26e..3b98732 100644
--- a/src/gun.erl
+++ b/src/gun.erl
@@ -58,6 +58,7 @@
%% Websocket.
-export([ws_upgrade/2]).
-export([ws_upgrade/3]).
+-export([ws_upgrade/4]).
-export([ws_send/2]).
%% Debug.
@@ -85,6 +86,8 @@
| {type, conn_type()}].
-export_type([opts/0]).
+-type ws_opts() :: [{compress, boolean()}].
+
-record(state, {
parent :: pid(),
owner :: pid(),
@@ -98,7 +101,7 @@
socket :: inet:socket() | ssl:sslsocket(),
transport :: module(),
protocol :: module(),
- proto_opts :: gun_http:opts(), %% @todo Make a tuple with SPDY and WS too.
+ proto_opts :: gun_http:opts(), %% @todo Make a tuple with SPDY too.
protocol_state :: any()
}).
@@ -338,14 +341,19 @@ cancel(ServerPid, StreamRef) ->
%% Websocket.
--spec ws_upgrade(pid(), iodata()) -> ok.
+-spec ws_upgrade(pid(), iodata()) -> reference().
ws_upgrade(ServerPid, Path) ->
- ws_upgrade(ServerPid, Path, []).
+ ws_upgrade(ServerPid, Path, [], []).
--spec ws_upgrade(pid(), iodata(), headers()) -> ok.
+-spec ws_upgrade(pid(), iodata(), headers()) -> reference().
ws_upgrade(ServerPid, Path, Headers) ->
- _ = ServerPid ! {ws_upgrade, self(), Path, Headers},
- ok.
+ ws_upgrade(ServerPid, Path, Headers, []).
+
+-spec ws_upgrade(pid(), iodata(), headers(), ws_opts()) -> reference().
+ws_upgrade(ServerPid, Path, Headers, Opts) ->
+ StreamRef = make_ref(),
+ _ = ServerPid ! {ws_upgrade, self(), StreamRef, Path, Headers, Opts},
+ StreamRef.
-spec ws_send(pid(), ws_frame() | [ws_frame()]) -> ok.
ws_send(ServerPid, Frames) ->
@@ -428,8 +436,10 @@ connect(State=#state{owner=Owner, host=Host, port=Port, type=Type,
retry(State, Retries - 1)
end.
-retry(State=#state{keepalive_ref=KeepaliveRef}, Retries) when
- is_reference(KeepaliveRef) ->
+%% Exit normally if the retry functionality has been disabled.
+retry(_, 0) ->
+ ok;
+retry(State=#state{keepalive_ref=KeepaliveRef}, Retries) when is_reference(KeepaliveRef) ->
_ = erlang:cancel_timer(KeepaliveRef),
%% Flush if we have a keepalive message
receive
@@ -458,7 +468,7 @@ before_loop(State=#state{keepalive=Keepalive}) ->
KeepaliveRef = erlang:send_after(Keepalive, self(), keepalive),
loop(State#state{keepalive_ref=KeepaliveRef}).
-loop(State=#state{parent=Parent, owner=Owner, host=Host,
+loop(State=#state{parent=Parent, owner=Owner, host=Host, port=Port,
retry=Retry, socket=Socket, transport=Transport,
protocol=Protocol, protocol_state=ProtoState}) ->
{OK, Closed, Error} = Transport:messages(),
@@ -470,7 +480,9 @@ loop(State=#state{parent=Parent, owner=Owner, host=Host,
Transport:close(Socket),
retry(State#state{socket=undefined, transport=undefined,
protocol=undefined}, Retry);
- ProtoState2 ->
+ {upgrade, Protocol2, ProtoState2} ->
+ ws_loop(State#state{protocol=Protocol2, protocol_state=ProtoState2});
+ ProtoState2 ->
loop(State#state{protocol_state=ProtoState2})
end;
{Closed, Socket} ->
@@ -494,11 +506,11 @@ loop(State=#state{parent=Parent, owner=Owner, host=Host,
before_loop(State#state{protocol_state=ProtoState2});
{request, Owner, StreamRef, Method, Path, Headers} ->
ProtoState2 = Protocol:request(ProtoState,
- StreamRef, Method, Host, Path, Headers),
+ StreamRef, Method, Host, Port, Path, Headers),
loop(State#state{protocol_state=ProtoState2});
{request, Owner, StreamRef, Method, Path, Headers, Body} ->
ProtoState2 = Protocol:request(ProtoState,
- StreamRef, Method, Host, Path, Headers, Body),
+ StreamRef, Method, Host, Port, Path, Headers, Body),
loop(State#state{protocol_state=ProtoState2});
{data, Owner, StreamRef, IsFin, Data} ->
ProtoState2 = Protocol:data(ProtoState,
@@ -507,11 +519,10 @@ loop(State=#state{parent=Parent, owner=Owner, host=Host,
{cancel, Owner, StreamRef} ->
ProtoState2 = Protocol:cancel(ProtoState, StreamRef),
loop(State#state{protocol_state=ProtoState2});
- {ws_upgrade, Owner, Path, Headers} when Protocol =/= gun_spdy ->
- %% @todo
- ProtoState2 = Protocol:ws_upgrade(ProtoState,
- Path, Headers),
- ws_loop(State#state{protocol=gun_ws, protocol_state=ProtoState2});
+ {ws_upgrade, Owner, StreamRef, Path, Headers, Opts} when Protocol =/= gun_spdy ->
+ ProtoState2 = Protocol:ws_upgrade(ProtoState, StreamRef, Host, Port, Path, Headers, Opts),
+ loop(State#state{protocol_state=ProtoState2});
+ %% @todo can fail if http/1.0
{shutdown, Owner} ->
%% @todo Protocol:shutdown?
ok;
@@ -525,9 +536,9 @@ loop(State=#state{parent=Parent, owner=Owner, host=Host,
element(2, Any) ! {gun_error, self(), {notowner,
"Operations are restricted to the owner of the connection."}},
loop(State);
- {ws_upgrade, _, _, _} ->
- Owner ! {gun_error, self(), {badstate,
- "Websocket over SPDY isn't supported."}},
+ {ws_upgrade, _, StreamRef, _, _} ->
+ Owner ! {gun_error, self(), StreamRef, {badstate,
+ "Websocket is only supported over HTTP/1.1."}},
loop(State);
{ws_send, _, _} ->
Owner ! {gun_error, self(), {badstate,
@@ -545,23 +556,34 @@ ws_loop(State=#state{parent=Parent, owner=Owner, retry=Retry, socket=Socket,
ok = Transport:setopts(Socket, [{active, once}]),
receive
{OK, Socket, Data} ->
- ProtoState2 = Protocol:handle(ProtoState, Data),
- ws_loop(State#state{protocol_state=ProtoState2});
+ case Protocol:handle(Data, ProtoState) of
+ close ->
+ Transport:close(Socket),
+ retry(State#state{socket=undefined, transport=undefined, protocol=undefined}, Retry);
+ ProtoState2 ->
+ ws_loop(State#state{protocol_state=ProtoState2})
+ end;
{Closed, Socket} ->
Transport:close(Socket),
- retry(State#state{socket=undefined, transport=undefined,
- protocol=undefined}, Retry);
+ retry(State#state{socket=undefined, transport=undefined, protocol=undefined}, Retry);
{Error, Socket, _} ->
Transport:close(Socket),
- retry(State#state{socket=undefined, transport=undefined,
- protocol=undefined}, Retry);
- %% @todo keepalive
- {ws_send, Owner, Frames} when is_list(Frames) ->
- todo; %% @todo
+ retry(State#state{socket=undefined, transport=undefined, protocol=undefined}, Retry);
+ %% Ignore any previous HTTP keep-alive.
+ keepalive ->
+ ws_loop(State);
+% {ws_send, Owner, Frames} when is_list(Frames) ->
+% todo; %% @todo
{ws_send, Owner, Frame} ->
- {todo, Frame}; %% @todo
+ case Protocol:send(Frame, ProtoState) of
+ close ->
+ Transport:close(Socket),
+ retry(State#state{socket=undefined, transport=undefined, protocol=undefined}, Retry);
+ ProtoState2 ->
+ ws_loop(State#state{protocol_state=ProtoState2})
+ end;
{shutdown, Owner} ->
- %% @todo Protocol:shutdown?
+ %% @todo Protocol:shutdown? %% @todo close frame
ok;
{system, From, Request} ->
sys:handle_system_msg(Request, From, Parent, ?MODULE, [],
diff --git a/src/gun_http.erl b/src/gun_http.erl
index bd6565c..f2a2d23 100644
--- a/src/gun_http.erl
+++ b/src/gun_http.erl
@@ -18,16 +18,19 @@
-export([handle/2]).
-export([close/1]).
-export([keepalive/1]).
--export([request/6]).
-export([request/7]).
+-export([request/8]).
-export([data/4]).
-export([cancel/2]).
+-export([ws_upgrade/7]).
-type opts() :: [{version, cow_http:version()}].
-export_type([opts/0]).
-type io() :: head | {body, non_neg_integer()} | body_close | body_chunked.
+-type websocket_info() :: {websocket, reference(), binary(), [], []}. %% key, extensions, protocols
+
-record(http_state, {
owner :: pid(),
socket :: inet:socket() | ssl:sslsocket(),
@@ -35,7 +38,7 @@
version = 'HTTP/1.1' :: cow_http:version(),
connection = keepalive :: keepalive | close,
buffer = <<>> :: binary(),
- streams = [] :: [{reference(), boolean()}], %% ref + whether stream is alive
+ streams = [] :: [{reference() | websocket_info(), boolean()}], %% ref + whether stream is alive
in = head :: io(),
in_state :: {non_neg_integer(), non_neg_integer()},
out = head :: io()
@@ -128,31 +131,40 @@ handle_head(Data, State=#http_state{owner=Owner, version=ClientVersion,
connection=Conn, streams=[{StreamRef, IsAlive}|_]}) ->
{Version, Status, _, Rest} = cow_http:parse_status_line(Data),
{Headers, Rest2} = cow_http:parse_headers(Rest),
- In = io_from_headers(Version, Headers),
- IsFin = case In of head -> fin; _ -> nofin end,
- case IsAlive of
- false ->
- ok;
- true ->
- Owner ! {gun_response, self(), StreamRef,
- IsFin, Status, Headers},
- ok
- end,
- Conn2 = if
- Conn =:= close -> close;
- Version =:= 'HTTP/1.0' -> close;
- ClientVersion =:= 'HTTP/1.0' -> close;
- true -> conn_from_headers(Headers)
- end,
- %% We always reset in_state even if not chunked.
- if
- IsFin =:= fin, Conn2 =:= close ->
- close;
- IsFin =:= fin ->
- handle(Rest2, end_stream(State#http_state{in=In,
- in_state={0, 0}, connection=Conn2}));
- true ->
- handle(Rest2, State#http_state{in=In, in_state={0, 0}, connection=Conn2})
+ case {Status, StreamRef} of
+ {101, {websocket, _, WsKey, WsExtensions, WsProtocols, WsOpts}} ->
+ ws_handshake(Rest2, State, Headers, WsKey, WsExtensions, WsProtocols, WsOpts);
+ _ ->
+ In = io_from_headers(Version, Headers),
+ IsFin = case In of head -> fin; _ -> nofin end,
+ case IsAlive of
+ false ->
+ ok;
+ true ->
+ StreamRef2 = case StreamRef of
+ {websocket, SR, _, _, _} -> SR;
+ _ -> StreamRef
+ end,
+ Owner ! {gun_response, self(), StreamRef2,
+ IsFin, Status, Headers},
+ ok
+ end,
+ Conn2 = if
+ Conn =:= close -> close;
+ Version =:= 'HTTP/1.0' -> close;
+ ClientVersion =:= 'HTTP/1.0' -> close;
+ true -> conn_from_headers(Headers)
+ end,
+ %% We always reset in_state even if not chunked.
+ if
+ IsFin =:= fin, Conn2 =:= close ->
+ close;
+ IsFin =:= fin ->
+ handle(Rest2, end_stream(State#http_state{in=In,
+ in_state={0, 0}, connection=Conn2}));
+ true ->
+ handle(Rest2, State#http_state{in=In, in_state={0, 0}, connection=Conn2})
+ end
end.
send_data_if_alive(<<>>, _, nofin) ->
@@ -187,27 +199,28 @@ keepalive(State) ->
State.
request(State=#http_state{socket=Socket, transport=Transport, version=Version,
- out=head}, StreamRef, Method, Host, Path, Headers) ->
+ out=head}, StreamRef, Method, Host, Port, Path, Headers) ->
Headers2 = case Version of
'HTTP/1.0' -> lists:keydelete(<<"transfer-encoding">>, 1, Headers);
'HTTP/1.1' -> Headers
end,
Headers3 = case lists:keymember(<<"host">>, 1, Headers) of
- false -> [{<<"host">>, Host}|Headers2];
+ false -> [{<<"host">>, [Host, $:, integer_to_binary(Port)]}|Headers2];
true -> Headers2
end,
%% We use Headers2 because this is the smallest list.
Conn = conn_from_headers(Headers2),
+ %% @todo This should probably also check for content-type like SPDY.
Out = io_from_headers(Version, Headers2),
Transport:send(Socket, cow_http:request(Method, Path, Version, Headers3)),
new_stream(State#http_state{connection=Conn, out=Out}, StreamRef).
request(State=#http_state{socket=Socket, transport=Transport, version=Version,
- out=head}, StreamRef, Method, Host, Path, Headers, Body) ->
+ out=head}, StreamRef, Method, Host, Port, Path, Headers, Body) ->
Headers2 = lists:keydelete(<<"content-length">>, 1,
lists:keydelete(<<"transfer-encoding">>, 1, Headers)),
Headers3 = case lists:keymember(<<"host">>, 1, Headers) of
- false -> [{<<"host">>, Host}|Headers2];
+ false -> [{<<"host">>, [Host, $:, integer_to_binary(Port)]}|Headers2];
true -> Headers2
end,
%% We use Headers2 because this is the smallest list.
@@ -334,3 +347,88 @@ cancel_stream(State=#http_state{streams=Streams}, StreamRef) ->
end_stream(State=#http_state{streams=[_|Tail]}) ->
State#http_state{in=head, streams=Tail}.
+
+%% Websocket upgrade.
+
+%% Ensure version is 1.1.
+ws_upgrade(#http_state{version='HTTP/1.0'}, _, _, _, _, _, _) ->
+ error; %% @todo
+ws_upgrade(State=#http_state{socket=Socket, transport=Transport, out=head},
+ StreamRef, Host, Port, Path, Headers, WsOpts) ->
+ %% @todo Add option for setting protocol.
+ {ExtHeaders, GunExtensions} = case maps:get(compress, WsOpts, false) of
+ true -> {[{<<"sec-websocket-extensions">>, <<"permessage-deflate; client_max_window_bits; server_max_window_bits=15">>}],
+ [<<"permessage-deflate">>]};
+ false -> {[], []}
+ end,
+ Key = cow_ws:key(),
+ Headers2 = [
+ {<<"connection">>, <<"upgrade">>},
+ {<<"upgrade">>, <<"websocket">>},
+ {<<"sec-websocket-version">>, <<"13">>},
+ {<<"sec-websocket-key">>, Key}
+ |ExtHeaders
+ ],
+ Headers3 = case lists:keymember(<<"host">>, 1, Headers) of
+ false -> [{<<"host">>, [Host, $:, integer_to_binary(Port)]}|Headers2];
+ true -> Headers2
+ end,
+ Transport:send(Socket, cow_http:request(<<"GET">>, Path, 'HTTP/1.1', Headers3)),
+ new_stream(State#http_state{connection=keepalive, out=head},
+ {websocket, StreamRef, Key, GunExtensions, [], WsOpts}).
+
+ws_handshake(Buffer, State, Headers, Key, GunExtensions, GunProtocols, Opts) ->
+ %% @todo check upgrade, connection
+ case lists:keyfind(<<"sec-websocket-accept">>, 1, Headers) of
+ false ->
+ close;
+ {_, Accept} ->
+ case cow_ws:encode_key(Key) of
+ Accept -> ws_handshake_extensions(Buffer, State, Headers, GunExtensions, GunProtocols, Opts);
+ _ -> close
+ end
+ end.
+
+ws_handshake_extensions(Buffer, State, Headers, GunExtensions, GunProtocols, Opts) ->
+ case lists:keyfind(<<"sec-websocket-extensions">>, 1, Headers) of
+ false ->
+ ws_handshake_protocols(Buffer, State, Headers, #{}, GunProtocols);
+ {_, ExtHd} ->
+ case ws_validate_extensions(cow_http_hd:parse_sec_websocket_extensions(ExtHd), GunExtensions, #{}, Opts) of
+ close -> close;
+ Extensions -> ws_handshake_protocols(Buffer, State, Headers, Extensions, GunProtocols)
+ end
+ end.
+
+ws_validate_extensions([], _, Acc, _) ->
+ Acc;
+ws_validate_extensions([{Name = <<"permessage-deflate">>, Params}|Tail], GunExts, Acc, Opts) ->
+ case lists:member(Name, GunExts) of
+ true ->
+ case cow_ws:validate_permessage_deflate(Params, Acc, Opts) of
+ {ok, Acc2} -> ws_validate_extensions(Tail, GunExts, Acc2, Opts);
+ error -> close
+ end;
+ %% Fail the connection if extension was not requested.
+ false ->
+ close
+ end;
+%% Fail the connection on unknown extension.
+ws_validate_extensions(_, _, _, _) ->
+ close.
+
+%% @todo Validate protocols.
+ws_handshake_protocols(Buffer, State, _Headers, Extensions, _GunProtocols = []) ->
+ Protocols = [],
+ ws_handshake_end(Buffer, State, Extensions, Protocols).
+
+ws_handshake_end(Buffer, #http_state{owner=Owner, socket=Socket, transport=Transport}, Extensions, Protocols) ->
+ %% Send ourselves the remaining buffer, if any.
+ _ = case Buffer of
+ <<>> ->
+ ok;
+ _ ->
+ {OK, _, _} = Transport:messages(),
+ self() ! {OK, Socket, Buffer}
+ end,
+ gun_ws:init(Owner, Socket, Transport, Extensions, Protocols).
diff --git a/src/gun_ws.erl b/src/gun_ws.erl
new file mode 100644
index 0000000..5379362
--- /dev/null
+++ b/src/gun_ws.erl
@@ -0,0 +1,125 @@
+%% 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(gun_ws).
+
+-export([init/5]).
+-export([handle/2]).
+-export([send/2]).
+
+-record(payload, {
+ type = undefined :: cow_ws:frame_type(),
+ rsv = undefined :: cow_ws:rsv(),
+ len = undefined :: non_neg_integer(),
+ mask_key = undefined :: cow_ws:mask_key(),
+ close_code = undefined :: undefined | cow_ws:close_code(),
+ unmasked = <<>> :: binary(),
+ unmasked_len = 0 :: non_neg_integer()
+}).
+
+-record(ws_state, {
+ owner :: pid(),
+ socket :: inet:socket() | ssl:sslsocket(),
+ transport :: module(),
+ buffer = <<>> :: binary(),
+ in = head :: head | #payload{} | close,
+ frag_state = undefined :: cow_ws:frag_state(),
+ frag_buffer = <<>> :: binary(),
+ utf8_state = 0 :: cow_ws:utf8_state(),
+ extensions = #{} :: cow_ws:extensions()
+}).
+
+%% @todo Protocols
+init(Owner, Socket, Transport, Extensions, _Protocols) ->
+ Owner ! {gun_ws_upgrade, self(), ok},
+ {upgrade, ?MODULE, #ws_state{owner=Owner, socket=Socket, transport=Transport, extensions=Extensions}}.
+
+%% Do not handle anything if we received a close frame.
+handle(_, State=#ws_state{in=close}) ->
+ State;
+%% Shortcut for common case when Data is empty after processing a frame.
+handle(<<>>, State=#ws_state{in=head}) ->
+ State;
+handle(Data, State=#ws_state{buffer=Buffer, in=head, frag_state=FragState, extensions=Extensions}) ->
+ Data2 = << Buffer/binary, Data/binary >>,
+ case cow_ws:parse_header(Data2, Extensions, FragState) of
+ {Type, FragState2, Rsv, Len, MaskKey, Rest} ->
+ handle(Rest, State#ws_state{buffer= <<>>,
+ in=#payload{type=Type, rsv=Rsv, len=Len, mask_key=MaskKey}, frag_state=FragState2});
+ more ->
+ State#ws_state{buffer=Data2};
+ error ->
+ close({error, badframe}, State)
+ end;
+handle(Data, State=#ws_state{in=In=#payload{type=Type, rsv=Rsv, len=Len, mask_key=MaskKey, close_code=CloseCode,
+ unmasked=Unmasked, unmasked_len=UnmaskedLen}, frag_state=FragState, utf8_state=Utf8State, extensions=Extensions}) ->
+ case cow_ws:parse_payload(Data, MaskKey, Utf8State, UnmaskedLen, Type, Len, FragState, Extensions, Rsv) of
+ {ok, CloseCode2, Payload, Utf8State2, Rest} ->
+ dispatch(Rest, State#ws_state{in=head, utf8_state=Utf8State2}, Type, << Unmasked/binary, Payload/binary >>, CloseCode2);
+ {ok, Payload, Utf8State2, Rest} ->
+ dispatch(Rest, State#ws_state{in=head, utf8_state=Utf8State2}, Type, << Unmasked/binary, Payload/binary >>, CloseCode);
+ {more, CloseCode2, Payload, Utf8State2} ->
+ State#ws_state{in=In#payload{close_code=CloseCode2, unmasked= << Unmasked/binary, Payload/binary >>,
+ len=Len - byte_size(Data), unmasked_len=2 + byte_size(Data)}, utf8_state=Utf8State2};
+ {more, Payload, Utf8State2} ->
+ State#ws_state{in=In#payload{unmasked= << Unmasked/binary, Payload/binary >>,
+ len=Len - byte_size(Data), unmasked_len=UnmaskedLen + byte_size(Data)}, utf8_state=Utf8State2};
+ Error = {error, _Reason} ->
+ close(Error, State)
+ end.
+
+dispatch(Rest, State=#ws_state{owner=Owner, frag_state=FragState, frag_buffer=SoFar},
+ Type0, Payload0, CloseCode0) ->
+ case cow_ws:make_frame(Type0, Payload0, CloseCode0, FragState) of
+ {fragment, nofin, _, Payload} ->
+ handle(Rest, State#ws_state{frag_buffer= << SoFar/binary, Payload/binary >>});
+ {fragment, fin, Type, Payload} ->
+ Owner ! {gun_ws, self(), {Type, << SoFar/binary, Payload/binary >>}},
+ handle(Rest, State#ws_state{frag_state=undefined, frag_buffer= <<>>});
+ ping ->
+ State2 = send(pong, State),
+ handle(Rest, State2);
+ {ping, Payload} ->
+ State2 = send({pong, Payload}, State),
+ handle(Rest, State2);
+ pong ->
+ handle(Rest, State);
+ {pong, _} ->
+ handle(Rest, State);
+ Frame ->
+ Owner ! {gun_ws, self(), Frame},
+ case Frame of
+ close -> handle(Rest, State#ws_state{in=close});
+ {close, _, _} -> handle(Rest, State#ws_state{in=close});
+ _ -> handle(Rest, State)
+ end
+ end.
+
+close(Reason, State) ->
+ case Reason of
+ Normal when Normal =:= stop; Normal =:= timeout ->
+ send({close, 1000, <<>>}, State);
+ {error, badframe} ->
+ send({close, 1002, <<>>}, State);
+ {error, badencoding} ->
+ send({close, 1007, <<>>}, State)
+ end.
+
+send(Frame, State=#ws_state{socket=Socket, transport=Transport, extensions=Extensions}) ->
+ Transport:send(Socket, cow_ws:masked_frame(Frame, Extensions)),
+ case Frame of
+ close -> close;
+ {close, _, _} -> close;
+ _ -> State
+ end.
diff --git a/test/gun_ct_hook.erl b/test/gun_ct_hook.erl
new file mode 100644
index 0000000..72db623
--- /dev/null
+++ b/test/gun_ct_hook.erl
@@ -0,0 +1,21 @@
+%% 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(gun_ct_hook).
+
+-export([init/2]).
+
+init(_, _) ->
+ ct_helper:start([gun]),
+ {ok, undefined}.
diff --git a/test/twitter_SUITE.erl b/test/twitter_SUITE.erl
index 17086e6..a303440 100644
--- a/test/twitter_SUITE.erl
+++ b/test/twitter_SUITE.erl
@@ -13,42 +13,11 @@
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-module(twitter_SUITE).
-
--include_lib("common_test/include/ct.hrl").
-
-%% ct.
--export([all/0]).
--export([init_per_suite/1]).
--export([end_per_suite/1]).
-
-%% Tests.
--export([spdy/1]).
-
-%% ct.
+-compile(export_all).
all() ->
[spdy].
-init_per_suite(Config) ->
- ok = application:start(ranch),
- ok = application:start(crypto),
- ok = application:start(cowlib),
- ok = application:start(asn1),
- ok = application:start(public_key),
- ok = application:start(ssl),
- ok = application:start(gun),
- Config.
-
-end_per_suite(_) ->
- ok = application:stop(gun),
- ok = application:stop(ssl),
- ok = application:stop(public_key),
- ok = application:stop(asn1),
- ok = application:stop(cowlib),
- ok = application:stop(crypto),
- ok = application:stop(ranch),
- ok.
-
spdy(_) ->
{ok, Pid} = gun:open("twitter.com", 443),
Ref = gun:get(Pid, "/"),
@@ -66,6 +35,7 @@ data_loop(Pid, Ref) ->
ct:print("data ~p", [Data]),
data_loop(Pid, Ref);
{gun_data, Pid, Ref, fin, Data} ->
+ gun:close(Pid),
ct:print("data ~p~nend", [Data])
after 5000 ->
error(timeout)
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl
new file mode 100644
index 0000000..82f3632
--- /dev/null
+++ b/test/ws_SUITE.erl
@@ -0,0 +1,176 @@
+%% 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(ws_SUITE).
+-compile(export_all).
+
+-import(ct_helper, [config/2]).
+
+%% ct.
+
+all() ->
+ [{group, autobahn}].
+
+groups() ->
+ [{autobahn, [], [autobahn_fuzzingserver]}].
+
+init_per_group(autobahn, Config) ->
+ %% Some systems have it named pip2.
+ Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
+ case string:str(Out, "autobahntestsuite") of
+ 0 ->
+ ct:print("Skipping the autobahn group because the "
+ "Autobahn Test Suite is not installed.~nTo install it, "
+ "please follow the instructions on this page:~n~n "
+ "http://autobahn.ws/testsuite/installation.html"),
+ {skip, "Autobahn Test Suite not installed."};
+ _ ->
+ Config
+ end.
+
+end_per_group(_, _) ->
+ oK.
+
+%% Tests.
+
+autobahn_fuzzingserver(Config) ->
+ Self = self(),
+ spawn_link(fun() -> start_port(Config, Self) end),
+ receive autobahn_ready -> ok end,
+ N = get_case_count(),
+ run_cases(0, N),
+ Report = config(priv_dir, Config) ++ "reports/clients/index.html",
+ ct:log("<h2><a href=\"~s\">Full report</a></h2>~n", [Report]),
+ ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
+ log_output(),
+ terminate(),
+ {ok, HTML} = file:read_file(Report),
+ case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
+ true -> error(failed);
+ false -> ok
+ end.
+
+start_port(Config, Pid) ->
+ Port = open_port({spawn, "wstest -m fuzzingserver -s " ++ config(data_dir, Config) ++ "server.json"},
+ [{line, 10000}, {cd, config(priv_dir, Config)}, binary]),
+ receive_preamble(Port, Pid),
+ receive_infinity(Port).
+
+receive_preamble(Port, Pid) ->
+ receive
+ {Port, {data, {eol, Line = <<"Ok, will run", _/bits>>}}} ->
+ Pid ! autobahn_ready,
+ io:format(user, "~s~n", [Line]);
+ {Port, {data, {eol, Line}}} ->
+ io:format(user, "~s~n", [Line]),
+ receive_preamble(Port, Pid)
+ after 5000 ->
+ terminate(),
+ error(timeout)
+ end.
+
+receive_infinity(Port) ->
+ receive
+ {Port, {data, {eol, <<"Updating reports", _/bits>>}}} ->
+ receive_infinity(Port);
+ {Port, {data, {eol, Line}}} ->
+ io:format(user, "~s~n", [Line]),
+ receive_infinity(Port)
+ end.
+
+get_case_count() ->
+ {Pid, Ref} = connect("/getCaseCount"),
+ receive
+ {gun_ws, Pid, {text, N}} ->
+ close(Pid, Ref),
+ binary_to_integer(N);
+ Msg ->
+ ct:print("Unexpected message ~p", [Msg]),
+ terminate(),
+ error(failed)
+ end.
+
+run_cases(Total, Total) ->
+ ok;
+run_cases(N, Total) ->
+ {Pid, Ref} = connect(["/runCase?case=", integer_to_binary(N + 1), "&agent=Gun"]),
+ loop(Pid, Ref),
+ update_reports(),
+ run_cases(N + 1, Total).
+
+loop(Pid, Ref) ->
+ receive
+ {gun_ws, Pid, close} ->
+ gun:ws_send(Pid, close),
+ loop(Pid, Ref);
+ {gun_ws, Pid, {close, Code, _}} ->
+ gun:ws_send(Pid, {close, Code, <<>>}),
+ loop(Pid, Ref);
+ {gun_ws, Pid, Frame} ->
+ gun:ws_send(Pid, Frame),
+ loop(Pid, Ref);
+ {'DOWN', Ref, process, Pid, normal} ->
+ close(Pid, Ref);
+ Msg ->
+ ct:print("Unexpected message ~p", [Msg]),
+ close(Pid, Ref)
+ end.
+
+update_reports() ->
+ {Pid, Ref} = connect("/updateReports?agent=Gun"),
+ receive
+ {gun_ws, Pid, close} ->
+ close(Pid, Ref)
+ after 5000 ->
+ error(failed)
+ end.
+
+log_output() ->
+ ok.
+
+connect(Path) ->
+ {ok, Pid} = gun:open("127.0.0.1", 33080, [{type, tcp}, {retry, 0}]),
+ Ref = monitor(process, Pid),
+ gun:ws_upgrade(Pid, Path, [], #{compress => true}),
+ receive
+ {gun_ws_upgrade, Pid, ok} ->
+ ok;
+ Msg ->
+ ct:print("Unexpected message ~p", [Msg]),
+ terminate(),
+ error(failed)
+ end,
+ {Pid, Ref}.
+
+close(Pid, Ref) ->
+ demonitor(Ref),
+ gun:close(Pid),
+ flush(Pid).
+
+flush(Pid) ->
+ receive
+ {gun_ws, Pid, _} ->
+ flush(Pid);
+ {gun_ws_upgrade, Pid, _} ->
+ flush(Pid);
+ {'DOWN', _, process, Pid, _} ->
+ flush(Pid)
+ after 0 ->
+ ok
+ end.
+
+terminate() ->
+ Res = os:cmd("killall wstest"),
+ io:format(user, "~s", [Res]),
+ ok.
diff --git a/test/ws_SUITE_data/server.json b/test/ws_SUITE_data/server.json
new file mode 100644
index 0000000..902b9b3
--- /dev/null
+++ b/test/ws_SUITE_data/server.json
@@ -0,0 +1,7 @@
+{
+ "url": "ws://localhost:33080",
+
+ "cases": ["*"],
+ "exclude-cases": [],
+ "exclude-agent-cases": {}
+}