Browse files

Fix type specs using dialyzer

  • Loading branch information...
1 parent 820d502 commit 315a7a5ef5a226c082120264bcc2a0acd36311ef @madtrick committed Jun 30, 2012
Showing with 18 additions and 18 deletions.
  1. +1 −1 include/wsock.hrl
  2. +3 −3 src/wsock_handshake.erl
  3. +7 −8 src/wsock_http.erl
  4. +5 −4 src/wsock_message.erl
  5. +2 −2 test/spec/wsock_handshake_spec.erl
View
2 include/wsock.hrl
@@ -22,7 +22,7 @@
payload_len :: byte(),
extended_payload_len :: byte(),
extended_payload_len_cont :: integer(),
- masking_key :: binary(),
+ masking_key :: integer(),
payload :: binary()}).
-record(message, {
View
6 src/wsock_handshake.erl
@@ -26,7 +26,7 @@
-define(INVALID_CLIENT_OPEN, invalid_handshake_opening).
-define(INVALID_SERVER_RESPONSE, invalid_server_response).
--spec handle_open(Message::#http_message{}) -> {ok, #handshake{}} | {error, atom()}.
+-spec handle_open(Message::#http_message{}) -> {ok, #handshake{}} | {error, ?INVALID_CLIENT_OPEN}.
handle_open(Message) ->
case validate_handshake_open(Message) of
true ->
@@ -35,7 +35,7 @@ handle_open(Message) ->
{error, ?INVALID_CLIENT_OPEN}
end.
--spec handle_response(Response::#http_message{}, Handshake::#handshake{}) -> boolean().
+-spec handle_response(Response::#http_message{}, Handshake::#handshake{}) -> {ok, ?INVALID_SERVER_RESPONSE} | {ok, #handshake{}}.
handle_response(Response, Handshake) ->
case validate_handshake_response(Response, Handshake) of
true ->
@@ -80,7 +80,7 @@ open(Resource, Host, Port) ->
],
Message = wsock_http:build(request, RequestLine, Headers),
- #handshake{ version = ?VERSION, type = open, message = Message}.
+ {ok, #handshake{ version = ?VERSION, type = open, message = Message}}.
%=======================
View
15 src/wsock_http.erl
@@ -38,7 +38,7 @@ decode(Data, Type) ->
end.
--spec build(Type::atom(), StartLine::list({atom(), string()}), Headers::list({string(), string()})) -> list(string()).
+-spec build(Type::atom(), StartLine::list({atom(), string()}), Headers::list({string(), string()})) -> #http_message{}.
build(Type, StartLine, Headers) ->
#http_message{type = Type, start_line = StartLine, headers = Headers}.
@@ -65,8 +65,7 @@ get_header_value(Key, Message) ->
LowerCasedKey = string:to_lower(Key),
get_header_value_case_insensitive(LowerCasedKey, Message#http_message.headers).
--spec get_header_value_case_insensitive(Key::string(), list()) -> undefined;
-(Key::string(), list()) -> string().
+-spec get_header_value_case_insensitive(Key::string(), list()) -> undefined | string().
get_header_value_case_insensitive(_, []) ->
undefined;
@@ -92,31 +91,31 @@ split(Data)->
<<>> =/= Element
end, Fragments).
--spec process_startline(StartLine::binary(), Type:: request | response) -> list() | {error, term()}.
+-spec process_startline(StartLine::binary(), Type:: request | response) -> {ok, list({atom(), string()})} | {error, nomatch}.
process_startline(StartLine, request) ->
process_startline(StartLine, "(GET)\s+([\S/])\s+HTTP\/([0-9]\.[0-9])", [method, resource, version]);
process_startline(StartLine, response) ->
process_startline(StartLine, "HTTP/([0-9]\.[0-9])\s([0-9]{3,3})\s([a-zA-z0-9 ]+)", [version, status, reason]).
--spec process_startline(StartLine::binary(), Regexp::list(), Keys::list(atom())) -> term().
+-spec process_startline(StartLine::binary(), Regexp::list(), Keys::list(atom())) -> {ok, list({atom(), string()})} | {error, nomatch}.
process_startline(StartLine, Regexp, Keys) ->
case regexp_run(Regexp, StartLine) of
{match, [_ | Matchs]} ->
{ok , lists:zip(Keys, Matchs)};
nomatch -> {error, nomatch}
end.
--spec regexp_run(Regexp::list(), String::binary()) -> {match, list()}.
+-spec regexp_run(Regexp::list(), String::binary()) -> {match, list()} | nomatch.
regexp_run(Regexp, String) ->
re:run(String, Regexp, [{capture, all, list}, caseless]).
--spec process_headers(Headers::list(binary())) -> list({list(), list()}).
+-spec process_headers(Headers::list(binary())) -> {ok, list({string(), string()})} | {error, nomatch}.
process_headers(Headers) ->
process_headers(Headers, []).
--spec process_headers(Headers::list(binary()), Acc::list({list(), list()})) -> list({list(), list()}) | {error, term()}.
+-spec process_headers(Headers::list(binary()), Acc::list({list(), list()})) -> {ok, list({string(), string()})} | {error, nomatch}.
process_headers([Header | Tail], Acc) ->
case regexp_run("([!-9;-~]+)\s*:\s*(.+)", Header) of
{match, [_Match, HeaderName, HeaderValue]} ->
View
9 src/wsock_message.erl
@@ -23,7 +23,7 @@
-define(FRAGMENT_SIZE, 4096).
-type message_type() :: begin_message | continue_message.
--spec encode(Data::string() | binary(), Options::list()) -> binary().
+-spec encode(Data::string() | binary(), Options::list()) -> [binary()] | {error, missing_datatype}.
encode(Data, Options) when is_list(Data) ->
encode(list_to_binary(Data), Options);
@@ -86,13 +86,14 @@ frame(Data, Options) ->
Frame = wsock_framing:frame(Data, Options),
wsock_framing:to_binary(Frame).
--spec decode(Data::binary(), Type :: message_type(), Message::#message{}, Masked::boolean()) -> list(#message{}).
+-spec decode(Data::binary(), Type :: message_type(), Message::#message{}, Masked::boolean()) -> list(#message{}) | {error, frames_unmasked | fragmented_control_message}.
decode(Data, begin_message, _Message, Masked) ->
do_decode(Data, begin_message, [], Masked);
decode(Data, continue_message, Message, Masked) ->
do_decode(Data, continue_message, [Message | []], Masked).
+-spec do_decode(Data::binary(), Type:: message_type(), Acc::list(), Masked::boolean()) -> list(#message{}) | {error, frames_unmasked | fragmented_control_message}.
do_decode(Data, Type, Acc, Masked) ->
Frames = wsock_framing:from_binary(Data),
case Masked of
@@ -126,7 +127,7 @@ do_decode(Data, Type, Acc, Masked) ->
end
end.
--spec process_frames(Type:: message_type(), Frames :: list(#frame{}), Messages :: list(#message{})) -> list(#message{}).
+-spec process_frames(Type:: message_type(), Frames :: list(#frame{}), Messages :: list(#message{})) -> list(#message{}) | {error, fragmented_control_message}.
process_frames(_, [], Acc) ->
Acc;
process_frames(begin_message, Frames, Acc) ->
@@ -145,7 +146,7 @@ wtf([Frame | Frames], Type, XMessage, Acc) ->
process_frames(begin_message, Frames, [Message | Acc])
end.
--spec process_frame(Frame :: #frame{}, MessageType :: message_type(), Message :: #message{})-> {fragmented | completed, #message{}}.
+-spec process_frame(Frame :: #frame{}, MessageType :: message_type(), Message :: #message{})-> {fragmented | completed, #message{}} | {error, fragmented_control_message}.
process_frame(Frame, begin_message, Message) ->
case contextualize_frame(Frame) of
control_fragment ->
View
4 test/spec/wsock_handshake_spec.erl
@@ -72,7 +72,7 @@ spec() ->
Host = "localhost",
Port = 8080,
- HandShake = wsock_handshake:open(Resource, Host, Port),
+ {ok, HandShake} = wsock_handshake:open(Resource, Host, Port),
assert_that(HandShake#handshake.version, is(13)),
assert_that(HandShake#handshake.type, is(open)),
@@ -94,7 +94,7 @@ spec() ->
Host = "localhost",
Port = 8080,
- OpenHandShake = wsock_handshake:open(Resource, Host, Port),
+ {ok, OpenHandShake} = wsock_handshake:open(Resource, Host, Port),
Key = wsock_http:get_header_value("sec-websocket-key", OpenHandShake#handshake.message),
BinResponse = list_to_binary(["HTTP/1.1 101 Switch Protocols\r\n

0 comments on commit 315a7a5

Please sign in to comment.