diff options
-rw-r--r-- | src/cowboy_protocol.erl | 36 | ||||
-rw-r--r-- | src/cowboy_rest.erl | 4 | ||||
-rw-r--r-- | src/cowboy_static.erl | 30 | ||||
-rw-r--r-- | src/cowboy_websocket.erl | 42 |
4 files changed, 57 insertions, 55 deletions
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 2dd7ad0..089cbbc 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -51,9 +51,9 @@ transport :: module(), dispatch :: cowboy_dispatcher:dispatch_rules(), handler :: {module(), any()}, - onrequest :: undefined | fun((#http_req{}) -> #http_req{}), + onrequest :: undefined | fun((cowboy_req:req()) -> cowboy_req:req()), onresponse = undefined :: undefined | fun((cowboy_http:status(), - cowboy_http:headers(), #http_req{}) -> #http_req{}), + cowboy_http:headers(), cowboy_req:req()) -> cowboy_req:req()), urldecode :: {fun((binary(), T) -> binary()), T}, req_empty_lines = 0 :: integer(), max_empty_lines :: integer(), @@ -164,7 +164,7 @@ request({http_error, <<"\r\n">>}, State=#state{req_empty_lines=N}) -> request(_Any, State) -> error_terminate(400, State). --spec parse_header(#http_req{}, #state{}) -> ok. +-spec parse_header(cowboy_req:req(), #state{}) -> ok. parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) -> case erlang:decode_packet(httph_bin, Buffer, []) of {ok, Header, Rest} -> header(Header, Req, State#state{buffer=Rest}); @@ -174,7 +174,7 @@ parse_header(Req, State=#state{buffer=Buffer, max_line_length=MaxLength}) -> {error, _Reason} -> error_terminate(400, State) end. --spec wait_header(#http_req{}, #state{}) -> ok. +-spec wait_header(cowboy_req:req(), #state{}) -> ok. wait_header(Req, State=#state{socket=Socket, transport=Transport, timeout=T, buffer=Buffer}) -> case Transport:recv(Socket, 0, T) of @@ -185,7 +185,7 @@ wait_header(Req, State=#state{socket=Socket, end. -spec header({http_header, integer(), cowboy_http:header(), any(), binary()} - | http_eoh, #http_req{}, #state{}) -> ok. + | http_eoh, cowboy_req:req(), #state{}) -> ok. header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{ transport=Transport}, State=#state{host_tokens=undefined}) -> RawHost2 = cowboy_bstr:to_lower(RawHost), @@ -239,7 +239,7 @@ header(_Any, _Req, State) -> %% in which case we consider the request handled and move on to the next %% one. Note that since we haven't dispatched yet, we don't know the %% handler, host_info, path_info or bindings yet. --spec onrequest(#http_req{}, #state{}) -> ok. +-spec onrequest(cowboy_req:req(), #state{}) -> ok. onrequest(Req, State=#state{onrequest=undefined}) -> dispatch(Req, State); onrequest(Req, State=#state{onrequest=OnRequest}) -> @@ -249,7 +249,7 @@ onrequest(Req, State=#state{onrequest=OnRequest}) -> _ -> next_request(Req2, State, ok) end. --spec dispatch(#http_req{}, #state{}) -> ok. +-spec dispatch(cowboy_req:req(), #state{}) -> ok. dispatch(Req, State=#state{dispatch=Dispatch, host_tokens=HostTokens, path_tokens=PathTokens}) -> case cowboy_dispatcher:match(HostTokens, PathTokens, Dispatch) of @@ -263,7 +263,7 @@ dispatch(Req, State=#state{dispatch=Dispatch, error_terminate(404, State) end. --spec handler_init(#http_req{}, #state{}) -> ok. +-spec handler_init(cowboy_req:req(), #state{}) -> ok. handler_init(Req, State=#state{transport=Transport, handler={Handler, Opts}}) -> try Handler:init({Transport:name(), http}, Req, Opts) of @@ -296,7 +296,7 @@ handler_init(Req, State=#state{transport=Transport, [Handler, Class, Reason, Opts, PLReq, erlang:get_stacktrace()]) end. --spec upgrade_protocol(#http_req{}, #state{}, atom()) -> ok. +-spec upgrade_protocol(cowboy_req:req(), #state{}, atom()) -> ok. upgrade_protocol(Req, State=#state{listener=ListenerPid, handler={Handler, Opts}}, Module) -> case Module:upgrade(ListenerPid, Handler, Opts, Req) of @@ -304,7 +304,7 @@ upgrade_protocol(Req, State=#state{listener=ListenerPid, _Any -> terminate(State) end. --spec handler_handle(any(), #http_req{}, #state{}) -> ok. +-spec handler_handle(any(), cowboy_req:req(), #state{}) -> ok. handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) -> try Handler:handle(Req, HandlerState) of {ok, Req2, HandlerState2} -> @@ -324,7 +324,7 @@ handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) -> %% We don't listen for Transport closes because that would force us %% to receive data and buffer it indefinitely. --spec handler_before_loop(any(), #http_req{}, #state{}) -> ok. +-spec handler_before_loop(any(), cowboy_req:req(), #state{}) -> ok. handler_before_loop(HandlerState, Req, State=#state{hibernate=true}) -> State2 = handler_loop_timeout(State), catch erlang:hibernate(?MODULE, handler_loop, @@ -345,7 +345,7 @@ handler_loop_timeout(State=#state{loop_timeout=Timeout, TRef = erlang:start_timer(Timeout, self(), ?MODULE), State#state{loop_timeout_ref=TRef}. --spec handler_loop(any(), #http_req{}, #state{}) -> ok. +-spec handler_loop(any(), cowboy_req:req(), #state{}) -> ok. handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) -> receive {timeout, TRef, ?MODULE} -> @@ -356,7 +356,7 @@ handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) -> handler_call(HandlerState, Req, State, Message) end. --spec handler_call(any(), #http_req{}, #state{}, any()) -> ok. +-spec handler_call(any(), cowboy_req:req(), #state{}, any()) -> ok. handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}}, Message) -> try Handler:info(Message, Req, HandlerState) of @@ -380,7 +380,7 @@ handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}}, error_terminate(500, State) end. --spec handler_terminate(any(), #http_req{}, #state{}) -> ok. +-spec handler_terminate(any(), cowboy_req:req(), #state{}) -> ok. handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) -> try Handler:terminate(Req#http_req{resp_state=locked}, HandlerState) @@ -395,12 +395,12 @@ handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) -> HandlerState, PLReq, erlang:get_stacktrace()]) end. --spec terminate_request(any(), #http_req{}, #state{}) -> ok. +-spec terminate_request(any(), cowboy_req:req(), #state{}) -> ok. terminate_request(HandlerState, Req, State) -> HandlerRes = handler_terminate(HandlerState, Req, State), next_request(Req, State, HandlerRes). --spec next_request(#http_req{}, #state{}, any()) -> ok. +-spec next_request(cowboy_req:req(), #state{}, any()) -> ok. next_request(Req=#http_req{connection=Conn}, State=#state{ req_keepalive=Keepalive}, HandlerRes) -> RespRes = ensure_response(Req), @@ -416,7 +416,7 @@ next_request(Req=#http_req{connection=Conn}, State=#state{ terminate(State) end. --spec ensure_body_processed(#http_req{}) -> {ok | close, binary()}. +-spec ensure_body_processed(cowboy_req:req()) -> {ok | close, binary()}. ensure_body_processed(#http_req{body_state=done, buffer=Buffer}) -> {ok, Buffer}; ensure_body_processed(Req=#http_req{body_state=waiting}) -> @@ -428,7 +428,7 @@ ensure_body_processed(Req=#http_req{body_state={stream, _, _, _}}) -> {ok, Req2} = cowboy_req:multipart_skip(Req), ensure_body_processed(Req2). --spec ensure_response(#http_req{}) -> ok. +-spec ensure_response(cowboy_req:req()) -> ok. %% The handler has already fully replied to the client. ensure_response(#http_req{resp_state=done}) -> ok; diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index 2f3a2e4..36a97f3 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -54,8 +54,8 @@ %% You do not need to call this function manually. To upgrade to the REST %% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em> %% in your <em>cowboy_http_handler:init/3</em> handler function. --spec upgrade(pid(), module(), any(), #http_req{}) - -> {ok, #http_req{}} | close. +-spec upgrade(pid(), module(), any(), Req) + -> {ok, Req} | close when Req::cowboy_req:req(). upgrade(_ListenerPid, Handler, Opts, Req) -> try case erlang:function_exported(Handler, rest_init, 2) of diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl index 7a84939..aaf798c 100644 --- a/src/cowboy_static.erl +++ b/src/cowboy_static.erl @@ -170,7 +170,6 @@ -module(cowboy_static). %% include files --include("http.hrl"). -include_lib("kernel/include/file.hrl"). %% cowboy_protocol callbacks @@ -212,7 +211,7 @@ init({_Transport, http}, _Req, _Opts) -> %% @private Set up initial state of REST handler. --spec rest_init(#http_req{}, list()) -> {ok, #http_req{}, #state{}}. +-spec rest_init(Req, list()) -> {ok, Req, #state{}} when Req::cowboy_req:req(). rest_init(Req, Opts) -> Directory = proplists:get_value(directory, Opts), Directory1 = directory_path(Directory), @@ -247,14 +246,14 @@ rest_init(Req, Opts) -> %% @private Only allow GET and HEAD requests on files. --spec allowed_methods(#http_req{}, #state{}) -> - {[atom()], #http_req{}, #state{}}. +-spec allowed_methods(Req, #state{}) + -> {[atom()], Req, #state{}} when Req::cowboy_req:req(). allowed_methods(Req, State) -> {['GET', 'HEAD'], Req, State}. %% @private --spec malformed_request(#http_req{}, #state{}) -> - {boolean(), #http_req{}, #state{}}. +-spec malformed_request(Req, #state{}) + -> {boolean(), Req, #state{}} when Req::cowboy_req:req(). malformed_request(Req, #state{filepath=error}=State) -> {true, Req, State}; malformed_request(Req, State) -> @@ -262,8 +261,8 @@ malformed_request(Req, State) -> %% @private Check if the resource exists under the document root. --spec resource_exists(#http_req{}, #state{}) -> - {boolean(), #http_req{}, #state{}}. +-spec resource_exists(Req, #state{}) + -> {boolean(), Req, #state{}} when Req::cowboy_req:req(). resource_exists(Req, #state{fileinfo={error, _}}=State) -> {false, Req, State}; resource_exists(Req, #state{fileinfo={ok, Fileinfo}}=State) -> @@ -273,7 +272,8 @@ resource_exists(Req, #state{fileinfo={ok, Fileinfo}}=State) -> %% @private %% Access to a file resource is forbidden if it exists and the local node does %% not have permission to read it. Directory listings are always forbidden. --spec forbidden(#http_req{}, #state{}) -> {boolean(), #http_req{}, #state{}}. +-spec forbidden(Req, #state{}) + -> {boolean(), Req, #state{}} when Req::cowboy_req:req(). forbidden(Req, #state{fileinfo={_, #file_info{type=directory}}}=State) -> {true, Req, State}; forbidden(Req, #state{fileinfo={error, eacces}}=State) -> @@ -285,8 +285,8 @@ forbidden(Req, #state{fileinfo={ok, #file_info{access=Access}}}=State) -> %% @private Read the time a file system system object was last modified. --spec last_modified(#http_req{}, #state{}) -> - {calendar:datetime(), #http_req{}, #state{}}. +-spec last_modified(Req, #state{}) + -> {calendar:datetime(), Req, #state{}} when Req::cowboy_req:req(). last_modified(Req, #state{fileinfo={ok, #file_info{mtime=Modified}}}=State) -> {Modified, Req, State}. @@ -294,8 +294,8 @@ last_modified(Req, #state{fileinfo={ok, #file_info{mtime=Modified}}}=State) -> %% @private Generate the ETag header value for this file. %% The ETag header value is only generated if the resource is a file that %% exists in document root. --spec generate_etag(#http_req{}, #state{}) -> - {undefined | binary(), #http_req{}, #state{}}. +-spec generate_etag(Req, #state{}) + -> {undefined | binary(), Req, #state{}} when Req::cowboy_req:req(). generate_etag(Req, #state{fileinfo={_, #file_info{type=regular, inode=INode, mtime=Modified, size=Filesize}}, filepath=Filepath, etag_fun={ETagFun, ETagData}}=State) -> @@ -308,7 +308,7 @@ generate_etag(Req, State) -> %% @private Return the content type of a file. --spec content_types_provided(#http_req{}, #state{}) -> tuple(). +-spec content_types_provided(cowboy_req:req(), #state{}) -> tuple(). content_types_provided(Req, #state{filepath=Filepath, mimetypes={MimetypesFun, MimetypesData}}=State) -> Mimetypes = [{T, file_contents} @@ -317,7 +317,7 @@ content_types_provided(Req, #state{filepath=Filepath, %% @private Return a function that writes a file directly to the socket. --spec file_contents(#http_req{}, #state{}) -> tuple(). +-spec file_contents(cowboy_req:req(), #state{}) -> tuple(). file_contents(Req, #state{filepath=Filepath, fileinfo={ok, #file_info{size=Filesize}}}=State) -> {ok, Transport, Socket} = cowboy_req:transport(Req), diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl index b669ef7..cdfcb91 100644 --- a/src/cowboy_websocket.erl +++ b/src/cowboy_websocket.erl @@ -58,7 +58,7 @@ %% You do not need to call this function manually. To upgrade to the WebSocket %% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em> %% in your <em>cowboy_http_handler:init/3</em> handler function. --spec upgrade(pid(), module(), any(), #http_req{}) -> closed. +-spec upgrade(pid(), module(), any(), cowboy_req:req()) -> closed. upgrade(ListenerPid, Handler, Opts, Req) -> ranch_listener:remove_connection(ListenerPid), case catch websocket_upgrade(#state{handler=Handler, opts=Opts}, Req) of @@ -66,7 +66,8 @@ upgrade(ListenerPid, Handler, Opts, Req) -> {'EXIT', _Reason} -> upgrade_error(Req) end. --spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}. +-spec websocket_upgrade(#state{}, Req) + -> {ok, #state{}, Req} when Req::cowboy_req:req(). websocket_upgrade(State, Req) -> {ConnTokens, Req2} = cowboy_req:parse_header('Connection', Req), @@ -78,8 +79,8 @@ websocket_upgrade(State, Req) -> %% @todo Handle the Sec-Websocket-Protocol header. %% @todo Reply a proper error, don't die, if a required header is undefined. --spec websocket_upgrade(undefined | <<_:8>>, #state{}, #http_req{}) - -> {ok, #state{}, #http_req{}}. +-spec websocket_upgrade(undefined | <<_:8>>, #state{}, Req) + -> {ok, #state{}, Req} when Req::cowboy_req:req(). %% No version given. Assuming hixie-76 draft. %% %% We need to wait to send a reply back before trying to read the @@ -105,7 +106,7 @@ websocket_upgrade(Version, State, Req=#http_req{meta=Meta}) {ok, State#state{version=IntVersion, challenge=Challenge}, Req2#http_req{meta=[{websocket_version, IntVersion}|Meta]}}. --spec handler_init(#state{}, #http_req{}) -> closed. +-spec handler_init(#state{}, cowboy_req:req()) -> closed. handler_init(State=#state{handler=Handler, opts=Opts}, Req=#http_req{transport=Transport}) -> try Handler:websocket_init(Transport:name(), Req, Opts) of @@ -132,14 +133,14 @@ handler_init(State=#state{handler=Handler, opts=Opts}, [Handler, Class, Reason, Opts, PLReq, erlang:get_stacktrace()]) end. --spec upgrade_error(#http_req{}) -> closed. +-spec upgrade_error(cowboy_req:req()) -> closed. upgrade_error(Req) -> {ok, _Req2} = cowboy_req:reply(400, [], [], Req#http_req{resp_state=waiting}), closed. %% @see cowboy_protocol:ensure_response/1 --spec upgrade_denied(#http_req{}) -> closed. +-spec upgrade_denied(cowboy_req:req()) -> closed. upgrade_denied(#http_req{resp_state=done}) -> closed; upgrade_denied(Req=#http_req{resp_state=waiting}) -> @@ -152,7 +153,7 @@ upgrade_denied(#http_req{socket=Socket, transport=Transport, Transport:send(Socket, <<"0\r\n\r\n">>), closed. --spec websocket_handshake(#state{}, #http_req{}, any()) -> closed. +-spec websocket_handshake(#state{}, cowboy_req:req(), any()) -> closed. websocket_handshake(State=#state{version=0, origin=Origin, challenge={Key1, Key2}}, Req=#http_req{socket=Socket, transport=Transport, host=Host, port=Port, @@ -196,7 +197,7 @@ websocket_handshake(State=#state{challenge=Challenge}, handler_before_loop(State#state{messages=Transport:messages()}, Req2, HandlerState, <<>>). --spec handler_before_loop(#state{}, #http_req{}, any(), binary()) -> closed. +-spec handler_before_loop(#state{}, cowboy_req:req(), any(), binary()) -> closed. handler_before_loop(State=#state{hibernate=true}, Req=#http_req{socket=Socket, transport=Transport}, HandlerState, SoFar) -> @@ -221,7 +222,7 @@ handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) -> State#state{timeout_ref=TRef}. %% @private --spec handler_loop(#state{}, #http_req{}, any(), binary()) -> closed. +-spec handler_loop(#state{}, cowboy_req:req(), any(), binary()) -> closed. handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef}, Req=#http_req{socket=Socket}, HandlerState, SoFar) -> receive @@ -241,7 +242,7 @@ handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef}, SoFar, websocket_info, Message, fun handler_before_loop/4) end. --spec websocket_data(#state{}, #http_req{}, any(), binary()) -> closed. +-spec websocket_data(#state{}, cowboy_req:req(), any(), binary()) -> closed. %% No more data. websocket_data(State, Req, HandlerState, <<>>) -> handler_before_loop(State, Req, HandlerState, <<>>); @@ -299,7 +300,7 @@ websocket_data(State, Req, HandlerState, _Data) -> websocket_close(State, Req, HandlerState, {error, badframe}). --spec websocket_data(#state{}, #http_req{}, any(), non_neg_integer(), +-spec websocket_data(#state{}, cowboy_req:req(), any(), non_neg_integer(), non_neg_integer(), non_neg_integer(), non_neg_integer(), non_neg_integer(), binary(), binary()) -> closed. %% A fragmented message MUST start a non-zero opcode. @@ -355,7 +356,7 @@ websocket_data(State, Req, HandlerState, _Fin, _Rsv, _Opcode, _Mask, %% hybi routing depending on whether unmasking is needed. --spec websocket_before_unmask(#state{}, #http_req{}, any(), binary(), +-spec websocket_before_unmask(#state{}, cowboy_req:req(), any(), binary(), binary(), opcode(), 0 | 1, non_neg_integer() | undefined) -> closed. websocket_before_unmask(State, Req, HandlerState, Data, Rest, Opcode, Mask, PayloadLen) -> @@ -372,14 +373,14 @@ websocket_before_unmask(State, Req, HandlerState, Data, end. %% hybi unmasking. --spec websocket_unmask(#state{}, #http_req{}, any(), binary(), +-spec websocket_unmask(#state{}, cowboy_req:req(), any(), binary(), opcode(), binary(), mask_key()) -> closed. websocket_unmask(State, Req, HandlerState, RemainingData, Opcode, Payload, MaskKey) -> websocket_unmask(State, Req, HandlerState, RemainingData, Opcode, Payload, MaskKey, <<>>). --spec websocket_unmask(#state{}, #http_req{}, any(), binary(), +-spec websocket_unmask(#state{}, cowboy_req:req(), any(), binary(), opcode(), binary(), mask_key(), binary()) -> closed. websocket_unmask(State, Req, HandlerState, RemainingData, Opcode, << O:32, Rest/bits >>, MaskKey, Acc) -> @@ -410,7 +411,7 @@ websocket_unmask(State, Req, HandlerState, RemainingData, Opcode, Acc). %% hybi dispatching. --spec websocket_dispatch(#state{}, #http_req{}, any(), binary(), +-spec websocket_dispatch(#state{}, cowboy_req:req(), any(), binary(), opcode(), binary()) -> closed. %% First frame of a fragmented message unmasked. Expect intermediate or last. websocket_dispatch(State=#state{frag_state={nofin, Opcode}}, Req, HandlerState, @@ -452,7 +453,7 @@ websocket_dispatch(State, Req, HandlerState, RemainingData, 10, Payload) -> handler_call(State, Req, HandlerState, RemainingData, websocket_handle, {pong, Payload}, fun websocket_data/4). --spec handler_call(#state{}, #http_req{}, any(), binary(), +-spec handler_call(#state{}, cowboy_req:req(), any(), binary(), atom(), any(), fun()) -> closed. handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState, RemainingData, Callback, Message, NextState) -> @@ -483,7 +484,7 @@ handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState, websocket_close(State, Req, HandlerState, {error, handler}) end. --spec websocket_send(binary(), #state{}, #http_req{}) -> closed | ignore. +-spec websocket_send(binary(), #state{}, cowboy_req:req()) -> closed | ignore. %% hixie-76 text frame. websocket_send({text, Payload}, #state{version=0}, #http_req{socket=Socket, transport=Transport}) -> @@ -503,7 +504,8 @@ websocket_send({Type, Payload}, _State, Transport:send(Socket, [<< 1:1, 0:3, Opcode:4, 0:1, Len/bits >>, Payload]). --spec websocket_close(#state{}, #http_req{}, any(), {atom(), atom()}) -> closed. +-spec websocket_close(#state{}, cowboy_req:req(), any(), {atom(), atom()}) + -> closed. websocket_close(State=#state{version=0}, Req=#http_req{socket=Socket, transport=Transport}, HandlerState, Reason) -> Transport:send(Socket, << 255, 0 >>), @@ -514,7 +516,7 @@ websocket_close(State, Req=#http_req{socket=Socket, Transport:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), handler_terminate(State, Req, HandlerState, Reason). --spec handler_terminate(#state{}, #http_req{}, +-spec handler_terminate(#state{}, cowboy_req:req(), any(), atom() | {atom(), atom()}) -> closed. handler_terminate(#state{handler=Handler, opts=Opts}, Req, HandlerState, TerminateReason) -> |