Permalink
Browse files

Improve types for R15B+

Removes compatibility with versions < R15B.

Also use one export per line to improve future diffs.
  • Loading branch information...
1 parent 3d2aad3 commit eb0d73274fad8ac9fb15f1631055f168e1505284 Loïc Hoguin committed May 31, 2012
View
@@ -52,7 +52,7 @@ intct:
build-plt:
@$(DIALYZER) --build_plt --output_plt .$(PROJECT).plt \
- --apps kernel stdlib sasl inets crypto public_key ssl
+ --apps kernel stdlib sasl tools inets crypto public_key ssl deps/ranch
dialyze:
@$(DIALYZER) --src src --plt .$(PROJECT).plt --no_native \
View
@@ -1,6 +1,6 @@
{deps, [
- {ranch, "0.2.0",
- {git, "git://github.com/extend/ranch.git", {tag, "0.2.0"}}}
+ {ranch, "0.2.1",
+ {git, "git://github.com/extend/ranch.git", {tag, "0.2.1"}}}
]}.
{erl_opts, [
%% bin_opt_info,
View
@@ -1,7 +1,7 @@
{cover_enabled, true}.
{deps, [
- {ranch, "0.2.0",
- {git, "git://github.com/extend/ranch.git", {tag, "0.2.0"}}},
+ {ranch, "0.2.1",
+ {git, "git://github.com/extend/ranch.git", {tag, "0.2.1"}}},
{proper, "1.0",
{git, "git://github.com/manopapad/proper.git", {tag, "v1.0"}}}
]}.
View
@@ -16,38 +16,14 @@
-module(cowboy_app).
-behaviour(application).
--export([start/2, stop/1, profile_output/0]). %% API.
-
--type application_start_type() :: normal
- | {takeover, node()} | {failover, node()}.
+%% API.
+-export([start/2]).
+-export([stop/1]).
%% API.
--spec start(application_start_type(), any()) -> {ok, pid()}.
-start(_Type, _Args) ->
- consider_profiling(),
+start(_, _) ->
cowboy_sup:start_link().
--spec stop(any()) -> ok.
-stop(_State) ->
+stop(_) ->
ok.
-
--spec profile_output() -> ok.
-profile_output() ->
- eprof:stop_profiling(),
- eprof:log("procs.profile"),
- eprof:analyze(procs),
- eprof:log("total.profile"),
- eprof:analyze(total).
-
-%% Internal.
-
--spec consider_profiling() -> profiling | not_profiling.
-consider_profiling() ->
- case application:get_env(profile) of
- {ok, true} ->
- {ok, _Pid} = eprof:start(),
- eprof:start_profiling([self()]);
- _ ->
- not_profiling
- end.
View
@@ -15,8 +15,12 @@
%% @doc Binary string manipulation.
-module(cowboy_bstr).
--export([to_lower/1]). %% Binary strings.
--export([char_to_lower/1, char_to_upper/1]). %% Characters.
+%% Binary strings.
+-export([to_lower/1]).
+
+%% Characters.
+-export([char_to_lower/1]).
+-export([char_to_upper/1]).
%% @doc Convert a binary string to lowercase.
-spec to_lower(binary()) -> binary().
View
@@ -21,9 +21,19 @@
-module(cowboy_clock).
-behaviour(gen_server).
--export([start_link/0, stop/0, rfc1123/0, rfc2109/1]). %% API.
--export([init/1, handle_call/3, handle_cast/2,
- handle_info/2, terminate/2, code_change/3]). %% gen_server.
+%% API.
+-export([start_link/0]).
+-export([stop/0]).
+-export([rfc1123/0]).
+-export([rfc2109/1]).
+
+%% 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, {
universaltime = undefined :: undefined | calendar:datetime(),
@@ -61,8 +71,8 @@ rfc1123() ->
%% HTTP responses.
-spec rfc2109(calendar:datetime()) -> binary().
rfc2109(LocalTime) ->
- {{YYYY,MM,DD},{Hour,Min,Sec}} =
- case calendar:local_time_to_universal_time_dst(LocalTime) of
+ {{YYYY,MM,DD},{Hour,Min,Sec}} = case
+ calendar:local_time_to_universal_time_dst(LocalTime) of
[Gmt] -> Gmt;
[_,Gmt] -> Gmt;
[] ->
@@ -87,12 +97,9 @@ rfc2109(LocalTime) ->
SecBin = pad_int(Sec),
WeekDay = weekday(Wday),
Month = month(MM),
- <<WeekDay/binary, ", ",
- DayBin/binary, " ", Month/binary, " ",
- YearBin/binary, " ",
- HourBin/binary, ":",
- MinBin/binary, ":",
- SecBin/binary, " GMT">>.
+ << WeekDay/binary, ", ", DayBin/binary, " ", Month/binary, " ",
+ YearBin/binary, " ", HourBin/binary, ":", MinBin/binary, ":",
+ SecBin/binary, " GMT">>.
%% gen_server.
View
@@ -17,7 +17,10 @@
-module(cowboy_cookies).
--export([parse_cookie/1, cookie/3, cookie/2]). %% API.
+%% API.
+-export([parse_cookie/1]).
+-export([cookie/3]).
+-export([cookie/2]).
%% Types.
-type kv() :: {Name::binary(), Value::binary()}.
@@ -16,7 +16,10 @@
%% @doc Dispatch requests according to a hostname and path.
-module(cowboy_dispatcher).
--export([split_host/1, split_path/2, match/3]). %% API.
+%% API.
+-export([split_host/1]).
+-export([split_path/2]).
+-export([match/3]).
-type bindings() :: list({atom(), binary()}).
-type tokens() :: list(binary()).
@@ -25,7 +28,9 @@
-type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}.
-type dispatch_rules() :: list(dispatch_rule()).
--export_type([bindings/0, tokens/0, dispatch_rules/0]).
+-export_type([bindings/0]).
+-export_type([tokens/0]).
+-export_type([dispatch_rules/0]).
-include_lib("eunit/include/eunit.hrl").
View
@@ -17,18 +17,38 @@
-module(cowboy_http).
%% Parsing.
--export([list/2, nonempty_list/2, content_type/1, media_range/2, conneg/2,
- language_range/2, entity_tag_match/1, expectation/2, params/2,
- http_date/1, rfc1123_date/1, rfc850_date/1, asctime_date/1,
- whitespace/2, digits/1, token/2, token_ci/2, quoted_string/2]).
+-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]).
%% Decoding.
--export([te_chunked/2, te_identity/2, ce_identity/1]).
+-export([te_chunked/2]).
+-export([te_identity/2]).
+-export([ce_identity/1]).
%% Interpretation.
--export([connection_to_atom/1, version_to_binary/1,
- urldecode/1, urldecode/2, urlencode/1,
- urlencode/2, x_www_form_urlencoded/2]).
+-export([connection_to_atom/1]).
+-export([version_to_binary/1]).
+-export([urldecode/1]).
+-export([urldecode/2]).
+-export([urlencode/1]).
+-export([urlencode/2]).
+-export([x_www_form_urlencoded/2]).
-type method() :: 'OPTIONS' | 'GET' | 'HEAD'
| 'POST' | 'PUT' | 'DELETE' | 'TRACE' | binary().
@@ -53,7 +73,12 @@
-type headers() :: [{header(), iodata()}].
-type status() :: non_neg_integer() | binary().
--export_type([method/0, uri/0, version/0, header/0, headers/0, status/0]).
+-export_type([method/0]).
+-export_type([uri/0]).
+-export_type([version/0]).
+-export_type([header/0]).
+-export_type([headers/0]).
+-export_type([status/0]).
-include_lib("eunit/include/eunit.hrl").
@@ -827,7 +852,6 @@ unhex(C) when C >= $A, C =< $F -> C - $A + 10;
unhex(C) when C >= $a, C =< $f -> C - $a + 10;
unhex(_) -> error.
-
%% @doc URL encode a string binary.
%% @equiv urlencode(Bin, [])
-spec urlencode(binary()) -> binary().
@@ -12,37 +12,37 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Handler for HTTP requests.
+%% @doc Behaviour for short-lived HTTP handlers.
%%
-%% HTTP handlers must implement three callbacks: <em>init/3</em>,
-%% <em>handle/2</em> and <em>terminate/2</em>, called one after another in
-%% that order.
+%% <em>init/3</em> allows you to initialize a state for all subsequent
+%% callbacks, and indicate to Cowboy whether you accept to handle the
+%% request or want to shutdown without handling it, in which case the
+%% <em>handle/2</em> call will simply be skipped.
%%
-%% <em>init/3</em> is meant for initialization. It receives information about
-%% the transport and protocol used, along with the handler options from the
-%% dispatch list, and allows you to upgrade the protocol if needed. You can
-%% define a request-wide state here.
+%% <em>handle/2</em> allows you to handle the request. It receives the
+%% state previously defined.
%%
-%% <em>handle/2</em> is meant for handling the request. It receives the
-%% request and the state previously defined.
+%% <em>terminate/2</em> allows you to clean up. It receives the state
+%% previously defined.
%%
-%% <em>terminate/2</em> is meant for cleaning up. It also receives the
-%% request and the state previously defined.
-%%
-%% You do not have to read the request body or even send a reply if you do
-%% not need to. Cowboy will properly handle these cases and clean-up afterwards.
-%% In doubt it'll simply close the connection.
-%%
-%% Note that when upgrading the connection to WebSocket you do not need to
-%% define the <em>handle/2</em> and <em>terminate/2</em> callbacks.
+%% There is no required operation to perform in any of these callbacks
+%% other than returning the proper values. Make sure you always return
+%% the last modified Req so that Cowboy has the up to date information
+%% about the request.
-module(cowboy_http_handler).
--export([behaviour_info/1]).
+-type opts() :: any().
+-type state() :: any().
-%% @private
--spec behaviour_info(_)
- -> undefined | [{handle, 2} | {init, 3} | {terminate, 2}, ...].
-behaviour_info(callbacks) ->
- [{init, 3}, {handle, 2}, {terminate, 2}];
-behaviour_info(_Other) ->
- undefined.
+-callback init({atom(), http}, Req, opts())
+ -> {ok, Req, state()}
+ | {loop, Req, state()}
+ | {loop, Req, state(), hibernate}
+ | {loop, Req, state(), timeout()}
+ | {loop, Req, state(), timeout(), hibernate}
+ | {shutdown, Req, state()}
+ | {upgrade, protocol, module()}
+ when Req::cowboy_req:req().
+-callback handle(Req, State) -> {ok, Req, State}
+ when Req::cowboy_req:req(), State::state().
+-callback terminate(cowboy_req:req(), state()) -> ok.
@@ -0,0 +1,57 @@
+%% Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>
+%%
+%% 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.
+
+%% @doc Behaviour for long-lived HTTP handlers.
+%%
+%% <em>init/3</em> allows you to initialize a state for all subsequent
+%% callbacks, and indicate to Cowboy whether you accept to handle the
+%% request or want to shutdown without handling it, in which case the
+%% receive loop and <em>info/3</em> calls will simply be skipped.
+%%
+%% <em>info/3</em> allows you to handle the messages this process will
+%% receive. It receives the message and the state previously defined.
+%% It can decide to stop the receive loop or continue receiving.
+%%
+%% <em>terminate/2</em> allows you to clean up. It receives the state
+%% previously defined.
+%%
+%% There is no required operation to perform in any of these callbacks
+%% other than returning the proper values. Make sure you always return
+%% the last modified Req so that Cowboy has the up to date information
+%% about the request.
+%%
+%% It is recommended to use hibernate if this process is not going to
+%% receive a lot of messages. It is also recommended to use a timeout
+%% value so that the connection gets closed after a long period of
+%% inactivity.
+-module(cowboy_loop_handler).
+
+-type opts() :: any().
+-type state() :: any().
+
+-callback init({atom(), http}, Req, opts())
+ -> {ok, Req, state()}
+ | {loop, Req, state()}
+ | {loop, Req, state(), hibernate}
+ | {loop, Req, state(), timeout()}
+ | {loop, Req, state(), timeout(), hibernate}
+ | {shutdown, Req, state()}
+ | {upgrade, protocol, module()}
+ when Req::cowboy_req:req().
+-callback info(any(), Req, State)
+ -> {ok, Req, State}
+ | {loop, Req, State}
+ | {loop, Req, State, hibernate}
+ when Req::cowboy_req:req(), State::state().
+-callback terminate(cowboy_req:req(), state()) -> ok.
View
@@ -27,7 +27,8 @@
-type end_of_part() :: {end_of_part, cont(more(part_result()))}.
-type disposition() :: {binary(), [{binary(), binary()}]}.
--export([parser/1, content_disposition/1]).
+-export([parser/1]).
+-export([content_disposition/1]).
-include_lib("eunit/include/eunit.hrl").
View
@@ -34,8 +34,13 @@
%% @see cowboy_http_handler
-module(cowboy_protocol).
--export([start_link/4]). %% API.
--export([init/4, parse_request/1, handler_loop/3]). %% FSM.
+%% API.
+-export([start_link/4]).
+
+%% FSM.
+-export([init/4]).
+-export([parse_request/1]).
+-export([handler_loop/3]).
-include("http.hrl").
-include_lib("eunit/include/eunit.hrl").
Oops, something went wrong.

0 comments on commit eb0d732

Please sign in to comment.