0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 12:40:25 +00:00

Rename cowboy_http_req to cowboy_req

This commit is contained in:
Loïc Hoguin 2012-08-27 13:28:57 +02:00
parent 9e2622becb
commit d3dcaf109b
26 changed files with 115 additions and 115 deletions

View file

@ -75,7 +75,7 @@ upgrade(_ListenerPid, Handler, Opts, Req) ->
" for the reason ~p:~p~n** Options were ~p~n"
"** Request was ~p~n** Stacktrace: ~p~n~n",
[Handler, Class, Reason, Opts, PLReq, erlang:get_stacktrace()]),
{ok, _Req2} = cowboy_http_req:reply(500, Req),
{ok, _Req2} = cowboy_req:reply(500, Req),
close
end.
@ -122,7 +122,7 @@ allowed_methods(Req=#http_req{method=Method}, State) ->
end.
method_not_allowed(Req, State, Methods) ->
{ok, Req2} = cowboy_http_req:set_resp_header(
{ok, Req2} = cowboy_req:set_resp_header(
<<"Allow">>, method_not_allowed_build(Methods, []), Req),
respond(Req2, State, 405).
@ -149,7 +149,7 @@ is_authorized(Req, State) ->
{true, Req2, HandlerState} ->
forbidden(Req2, State#state{handler_state=HandlerState});
{{false, AuthHead}, Req2, HandlerState} ->
{ok, Req3} = cowboy_http_req:set_resp_header(
{ok, Req3} = cowboy_req:set_resp_header(
<<"Www-Authenticate">>, AuthHead, Req2),
respond(Req3, State#state{handler_state=HandlerState}, 401)
end.
@ -207,7 +207,7 @@ content_types_provided(Req=#http_req{meta=Meta}, State) ->
CTP2 = [normalize_content_types(P) || P <- CTP],
State2 = State#state{
handler_state=HandlerState, content_types_p=CTP2},
{Accept, Req3} = cowboy_http_req:parse_header('Accept', Req2),
{Accept, Req3} = cowboy_req:parse_header('Accept', Req2),
case Accept of
undefined ->
{PMT, _Fun} = HeadCTP = hd(CTP2),
@ -302,7 +302,7 @@ languages_provided(Req, State) ->
{LP, Req2, HandlerState} ->
State2 = State#state{handler_state=HandlerState, languages_p=LP},
{AcceptLanguage, Req3} =
cowboy_http_req:parse_header('Accept-Language', Req2),
cowboy_req:parse_header('Accept-Language', Req2),
case AcceptLanguage of
undefined ->
set_language(Req3, State2#state{language_a=hd(LP)});
@ -348,7 +348,7 @@ match_language(Req, State, Accept, [Provided|Tail],
end.
set_language(Req=#http_req{meta=Meta}, State=#state{language_a=Language}) ->
{ok, Req2} = cowboy_http_req:set_resp_header(
{ok, Req2} = cowboy_req:set_resp_header(
<<"Content-Language">>, Language, Req),
charsets_provided(Req2#http_req{meta=[{language, Language}|Meta]}, State).
@ -365,7 +365,7 @@ charsets_provided(Req, State) ->
{CP, Req2, HandlerState} ->
State2 = State#state{handler_state=HandlerState, charsets_p=CP},
{AcceptCharset, Req3} =
cowboy_http_req:parse_header('Accept-Charset', Req2),
cowboy_req:parse_header('Accept-Charset', Req2),
case AcceptCharset of
undefined ->
set_content_type(Req3, State2#state{
@ -413,7 +413,7 @@ set_content_type(Req=#http_req{meta=Meta}, State=#state{
undefined -> ContentType;
Charset -> [ContentType, <<"; charset=">>, Charset]
end,
{ok, Req2} = cowboy_http_req:set_resp_header(
{ok, Req2} = cowboy_req:set_resp_header(
<<"Content-Type">>, ContentType2, Req),
encodings_provided(Req2#http_req{meta=[{charset, Charset}|Meta]}, State).
@ -468,7 +468,7 @@ variances(Req, State=#state{content_types_p=CTP,
[] ->
resource_exists(Req3, State2);
[[<<", ">>, H]|Variances5] ->
{ok, Req4} = cowboy_http_req:set_resp_header(
{ok, Req4} = cowboy_req:set_resp_header(
<<"Variances">>, [H|Variances5], Req3),
resource_exists(Req4, State2)
end.
@ -478,7 +478,7 @@ resource_exists(Req, State) ->
fun if_match_exists/2, fun if_match_musnt_exist/2).
if_match_exists(Req, State) ->
case cowboy_http_req:parse_header('If-Match', Req) of
case cowboy_req:parse_header('If-Match', Req) of
{undefined, Req2} ->
if_unmodified_since_exists(Req2, State);
{'*', Req2} ->
@ -496,13 +496,13 @@ if_match(Req, State, EtagsList) ->
end.
if_match_musnt_exist(Req, State) ->
case cowboy_http_req:header('If-Match', Req) of
case cowboy_req:header('If-Match', Req) of
{undefined, Req2} -> is_put_to_missing_resource(Req2, State);
{_Any, Req2} -> precondition_failed(Req2, State)
end.
if_unmodified_since_exists(Req, State) ->
case cowboy_http_req:parse_header('If-Unmodified-Since', Req) of
case cowboy_req:parse_header('If-Unmodified-Since', Req) of
{undefined, Req2} ->
if_none_match_exists(Req2, State);
{{error, badarg}, Req2} ->
@ -520,7 +520,7 @@ if_unmodified_since(Req, State, IfUnmodifiedSince) ->
end.
if_none_match_exists(Req, State) ->
case cowboy_http_req:parse_header('If-None-Match', Req) of
case cowboy_req:parse_header('If-None-Match', Req) of
{undefined, Req2} ->
if_modified_since_exists(Req2, State);
{'*', Req2} ->
@ -548,7 +548,7 @@ precondition_is_head_get(Req, State) ->
precondition_failed(Req, State).
if_modified_since_exists(Req, State) ->
case cowboy_http_req:parse_header('If-Modified-Since', Req) of
case cowboy_req:parse_header('If-Modified-Since', Req) of
{undefined, Req2} ->
method(Req2, State);
{{error, badarg}, Req2} ->
@ -595,7 +595,7 @@ is_put_to_missing_resource(Req, State) ->
moved_permanently(Req, State, OnFalse) ->
case call(Req, State, moved_permanently) of
{{true, Location}, Req2, HandlerState} ->
{ok, Req3} = cowboy_http_req:set_resp_header(
{ok, Req3} = cowboy_req:set_resp_header(
<<"Location">>, Location, Req2),
respond(Req3, State#state{handler_state=HandlerState}, 301);
{false, Req2, HandlerState} ->
@ -616,7 +616,7 @@ previously_existed(Req, State) ->
moved_temporarily(Req, State) ->
case call(Req, State, moved_temporarily) of
{{true, Location}, Req2, HandlerState} ->
{ok, Req3} = cowboy_http_req:set_resp_header(
{ok, Req3} = cowboy_req:set_resp_header(
<<"Location">>, Location, Req2),
respond(Req3, State#state{handler_state=HandlerState}, 307);
{false, Req2, HandlerState} ->
@ -666,7 +666,7 @@ create_path(Req=#http_req{meta=Meta}, State) ->
{Path, Req2, HandlerState} ->
Location = create_path_location(Req2, Path),
State2 = State#state{handler_state=HandlerState},
{ok, Req3} = cowboy_http_req:set_resp_header(
{ok, Req3} = cowboy_req:set_resp_header(
<<"Location">>, Location, Req2),
put_resource(Req3#http_req{meta=[{put_path, Path}|Meta]},
State2, 303)
@ -717,7 +717,7 @@ put_resource(Req=#http_req{raw_path=RawPath, meta=Meta}, State) ->
%% pushed to the resource in the request body. The path to the new resource
%% may be different from the request path, and is stored as request metadata.
%% It is always defined past this point. It can be retrieved as demonstrated:
%% {PutPath, Req2} = cowboy_http_req:meta(put_path, Req)
%% {PutPath, Req2} = cowboy_req:meta(put_path, Req)
put_resource(Req, State, OnTrue) ->
case call(Req, State, content_types_accepted) of
no_call ->
@ -728,7 +728,7 @@ put_resource(Req, State, OnTrue) ->
CTA2 = [normalize_content_types(P) || P <- CTA],
State2 = State#state{handler_state=HandlerState},
{ContentType, Req3}
= cowboy_http_req:parse_header('Content-Type', Req2),
= cowboy_req:parse_header('Content-Type', Req2),
choose_content_type(Req3, State2, OnTrue, ContentType, CTA2)
end.
@ -757,13 +757,13 @@ choose_content_type(Req, State, OnTrue, ContentType, [_Any|Tail]) ->
%% This is easily testable because we would have set the Location
%% header by this point if we did so.
is_new_resource(Req, State) ->
case cowboy_http_req:has_resp_header(<<"Location">>, Req) of
case cowboy_req:has_resp_header(<<"Location">>, Req) of
true -> respond(Req, State, 201);
false -> has_resp_body(Req, State)
end.
has_resp_body(Req, State) ->
case cowboy_http_req:has_resp_body(Req) of
case cowboy_req:has_resp_body(Req) of
true -> multiple_choices(Req, State);
false -> respond(Req, State, 204)
end.
@ -781,7 +781,7 @@ set_resp_body(Req=#http_req{method=Method},
Req4 = Req3;
LastModified ->
LastModifiedStr = httpd_util:rfc1123_date(LastModified),
{ok, Req4} = cowboy_http_req:set_resp_header(
{ok, Req4} = cowboy_req:set_resp_header(
<<"Last-Modified">>, LastModifiedStr, Req3)
end,
{Req5, State4} = set_resp_expires(Req4, State3),
@ -792,9 +792,9 @@ set_resp_body(Req=#http_req{method=Method},
State5 = State4#state{handler_state=HandlerState},
{ok, Req7} = case Body of
{stream, Len, Fun1} ->
cowboy_http_req:set_resp_body_fun(Len, Fun1, Req6);
cowboy_req:set_resp_body_fun(Len, Fun1, Req6);
_Contents ->
cowboy_http_req:set_resp_body(Body, Req6)
cowboy_req:set_resp_body(Body, Req6)
end,
multiple_choices(Req7, State5)
end;
@ -812,7 +812,7 @@ set_resp_etag(Req, State) ->
undefined ->
{Req2, State2};
Etag ->
{ok, Req3} = cowboy_http_req:set_resp_header(
{ok, Req3} = cowboy_req:set_resp_header(
<<"ETag">>, encode_etag(Etag), Req2),
{Req3, State2}
end.
@ -828,7 +828,7 @@ set_resp_expires(Req, State) ->
{Req2, State2};
Expires ->
ExpiresStr = httpd_util:rfc1123_date(Expires),
{ok, Req3} = cowboy_http_req:set_resp_header(
{ok, Req3} = cowboy_req:set_resp_header(
<<"Expires">>, ExpiresStr, Req2),
{Req3, State2}
end.
@ -908,7 +908,7 @@ next(Req, State, StatusCode) when is_integer(StatusCode) ->
respond(Req, State, StatusCode).
respond(Req, State, StatusCode) ->
{ok, Req2} = cowboy_http_req:reply(StatusCode, Req),
{ok, Req2} = cowboy_req:reply(StatusCode, Req),
terminate(Req2, State).
terminate(Req, #state{handler=Handler, handler_state=HandlerState}) ->

View file

@ -231,7 +231,7 @@ rest_init(Req, Opts) ->
end,
{Filepath, Req1} = case lists:keyfind(file, 1, Opts) of
{_, Filepath2} -> {filepath_path(Filepath2), Req};
false -> cowboy_http_req:path_info(Req)
false -> cowboy_req:path_info(Req)
end,
State = case check_path(Filepath) of
error ->
@ -320,7 +320,7 @@ content_types_provided(Req, #state{filepath=Filepath,
-spec file_contents(#http_req{}, #state{}) -> tuple().
file_contents(Req, #state{filepath=Filepath,
fileinfo={ok, #file_info{size=Filesize}}}=State) ->
{ok, Transport, Socket} = cowboy_http_req:transport(Req),
{ok, Transport, Socket} = cowboy_req:transport(Req),
Writefile = content_function(Transport, Socket, Filepath),
{{stream, Filesize, Writefile}, Req, State}.

View file

@ -69,11 +69,11 @@ upgrade(ListenerPid, Handler, Opts, Req) ->
-spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}.
websocket_upgrade(State, Req) ->
{ConnTokens, Req2}
= cowboy_http_req:parse_header('Connection', Req),
= cowboy_req:parse_header('Connection', Req),
true = lists:member(<<"upgrade">>, ConnTokens),
%% @todo Should probably send a 426 if the Upgrade header is missing.
{[<<"websocket">>], Req3} = cowboy_http_req:parse_header('Upgrade', Req2),
{Version, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Version">>, Req3),
{[<<"websocket">>], Req3} = cowboy_req:parse_header('Upgrade', Req2),
{Version, Req4} = cowboy_req:header(<<"Sec-Websocket-Version">>, Req3),
websocket_upgrade(Version, State, Req4).
%% @todo Handle the Sec-Websocket-Protocol header.
@ -87,9 +87,9 @@ websocket_upgrade(State, Req) ->
%% a reply before sending it. Therefore we calculate the challenge
%% key only in websocket_handshake/3.
websocket_upgrade(undefined, State, Req=#http_req{meta=Meta}) ->
{Origin, Req2} = cowboy_http_req:header(<<"Origin">>, Req),
{Key1, Req3} = cowboy_http_req:header(<<"Sec-Websocket-Key1">>, Req2),
{Key2, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Key2">>, Req3),
{Origin, Req2} = cowboy_req:header(<<"Origin">>, Req),
{Key1, Req3} = cowboy_req:header(<<"Sec-Websocket-Key1">>, Req2),
{Key2, Req4} = cowboy_req:header(<<"Sec-Websocket-Key2">>, Req3),
false = lists:member(undefined, [Origin, Key1, Key2]),
EOP = binary:compile_pattern(<< 255 >>),
{ok, State#state{version=0, origin=Origin, challenge={Key1, Key2},
@ -98,7 +98,7 @@ websocket_upgrade(undefined, State, Req=#http_req{meta=Meta}) ->
websocket_upgrade(Version, State, Req=#http_req{meta=Meta})
when Version =:= <<"7">>; Version =:= <<"8">>;
Version =:= <<"13">> ->
{Key, Req2} = cowboy_http_req:header(<<"Sec-Websocket-Key">>, Req),
{Key, Req2} = cowboy_req:header(<<"Sec-Websocket-Key">>, Req),
false = Key =:= undefined,
Challenge = hybi_challenge(Key),
IntVersion = list_to_integer(binary_to_list(Version)),
@ -134,7 +134,7 @@ handler_init(State=#state{handler=Handler, opts=Opts},
-spec upgrade_error(#http_req{}) -> closed.
upgrade_error(Req) ->
{ok, _Req2} = cowboy_http_req:reply(400, [], [],
{ok, _Req2} = cowboy_req:reply(400, [], [],
Req#http_req{resp_state=waiting}),
closed.
@ -143,7 +143,7 @@ upgrade_error(Req) ->
upgrade_denied(#http_req{resp_state=done}) ->
closed;
upgrade_denied(Req=#http_req{resp_state=waiting}) ->
{ok, _Req2} = cowboy_http_req:reply(400, [], [], Req),
{ok, _Req2} = cowboy_req:reply(400, [], [], Req),
closed;
upgrade_denied(#http_req{method='HEAD', resp_state=chunks}) ->
closed;
@ -158,24 +158,24 @@ websocket_handshake(State=#state{version=0, origin=Origin,
transport=Transport, raw_host=Host, port=Port,
raw_path=Path, raw_qs=QS}, HandlerState) ->
Location = hixie76_location(Transport:name(), Host, Port, Path, QS),
{ok, Req2} = cowboy_http_req:upgrade_reply(
{ok, Req2} = cowboy_req:upgrade_reply(
<<"101 WebSocket Protocol Handshake">>,
[{<<"Upgrade">>, <<"WebSocket">>},
{<<"Sec-Websocket-Location">>, Location},
{<<"Sec-Websocket-Origin">>, Origin}],
Req#http_req{resp_state=waiting}),
%% Flush the resp_sent message before moving on.
receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end,
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
%% We replied with a proper response. Proxies should be happy enough,
%% we can now read the 8 last bytes of the challenge keys and send
%% the challenge response directly to the socket.
%%
%% We use a trick here to read exactly 8 bytes of the body regardless
%% of what's in the buffer.
{ok, Req3} = cowboy_http_req:init_stream(
{ok, Req3} = cowboy_req:init_stream(
fun cowboy_http:te_identity/2, {0, 8},
fun cowboy_http:ce_identity/1, Req2),
case cowboy_http_req:body(Req3) of
case cowboy_req:body(Req3) of
{ok, Key3, Req4} ->
Challenge = hixie76_challenge(Key1, Key2, Key3),
Transport:send(Socket, Challenge),
@ -186,13 +186,13 @@ websocket_handshake(State=#state{version=0, origin=Origin,
end;
websocket_handshake(State=#state{challenge=Challenge},
Req=#http_req{transport=Transport}, HandlerState) ->
{ok, Req2} = cowboy_http_req:upgrade_reply(
{ok, Req2} = cowboy_req:upgrade_reply(
101,
[{<<"Upgrade">>, <<"websocket">>},
{<<"Sec-Websocket-Accept">>, Challenge}],
Req#http_req{resp_state=waiting}),
%% Flush the resp_sent message before moving on.
receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end,
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
handler_before_loop(State#state{messages=Transport:messages()},
Req2, HandlerState, <<>>).

View file

@ -208,7 +208,7 @@ header({http_header, _I, 'Connection', _R, Connection},
when Keepalive < MaxKeepalive ->
Req2 = Req#http_req{headers=[{'Connection', Connection}|Headers]},
{ConnTokens, Req3}
= cowboy_http_req:parse_header('Connection', Req2),
= cowboy_req:parse_header('Connection', Req2),
ConnAtom = cowboy_http:connection_to_atom(ConnTokens),
parse_header(Req3#http_req{connection=ConnAtom}, State);
header({http_header, _I, Field, _R, Value}, Req, State) ->
@ -399,7 +399,7 @@ next_request(Req=#http_req{connection=Conn}, State=#state{
RespRes = ensure_response(Req),
{BodyRes, Buffer} = ensure_body_processed(Req),
%% Flush the resp_sent message before moving on.
receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end,
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
case {HandlerRes, BodyRes, RespRes, Conn} of
{ok, ok, ok, keepalive} ->
?MODULE:parse_request(State#state{
@ -413,12 +413,12 @@ next_request(Req=#http_req{connection=Conn}, State=#state{
ensure_body_processed(#http_req{body_state=done, buffer=Buffer}) ->
{ok, Buffer};
ensure_body_processed(Req=#http_req{body_state=waiting}) ->
case cowboy_http_req:skip_body(Req) of
case cowboy_req:skip_body(Req) of
{ok, Req2} -> {ok, Req2#http_req.buffer};
{error, _Reason} -> {close, <<>>}
end;
ensure_body_processed(Req=#http_req{body_state={multipart, _, _}}) ->
{ok, Req2} = cowboy_http_req:multipart_skip(Req),
{ok, Req2} = cowboy_req:multipart_skip(Req),
ensure_body_processed(Req2).
-spec ensure_response(#http_req{}) -> ok.
@ -428,7 +428,7 @@ ensure_response(#http_req{resp_state=done}) ->
%% No response has been sent but everything apparently went fine.
%% Reply with 204 No Content to indicate this.
ensure_response(Req=#http_req{resp_state=waiting}) ->
_ = cowboy_http_req:reply(204, [], [], Req),
_ = cowboy_req:reply(204, [], [], Req),
ok;
%% Terminate the chunked body for HTTP/1.1 only.
ensure_response(#http_req{method='HEAD', resp_state=chunks}) ->
@ -445,9 +445,9 @@ ensure_response(#http_req{socket=Socket, transport=Transport,
error_terminate(Code, State=#state{socket=Socket, transport=Transport,
onresponse=OnResponse}) ->
receive
{cowboy_http_req, resp_sent} -> ok
{cowboy_req, resp_sent} -> ok
after 0 ->
_ = cowboy_http_req:reply(Code, #http_req{
_ = cowboy_req:reply(Code, #http_req{
socket=Socket, transport=Transport, onresponse=OnResponse,
connection=close, pid=self(), resp_state=waiting}),
ok

View file

@ -19,7 +19,7 @@
%% It should always be used instead of the one used in your function call
%% because it keeps the state of the request. It also allows Cowboy to do
%% some lazy evaluation and cache results where possible.
-module(cowboy_http_req).
-module(cowboy_req).
%% Request API.
-export([method/1]).
@ -711,7 +711,7 @@ set_resp_body(Body, Req) ->
%% if the response is later sent using anything other than `reply/2' or
%% `reply/3'.
%%
%% @see cowboy_http_req:transport/1.
%% @see cowboy_req:transport/1.
-spec set_resp_body_fun(non_neg_integer(), fun(() -> {sent, non_neg_integer()}),
#http_req{}) -> {ok, #http_req{}}.
set_resp_body_fun(StreamLen, StreamFun, Req) ->
@ -775,7 +775,7 @@ chunked_reply(Status, Req) ->
chunked_reply(Status, [], Req).
%% @doc Initiate the sending of a chunked reply to the client.
%% @see cowboy_http_req:chunk/2
%% @see cowboy_req:chunk/2
-spec chunked_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{})
-> {ok, #http_req{}}.
chunked_reply(Status, Headers, Req=#http_req{