mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Rename cowboy_http_req to cowboy_req
This commit is contained in:
parent
9e2622becb
commit
d3dcaf109b
26 changed files with 115 additions and 115 deletions
14
README.md
14
README.md
|
@ -85,7 +85,7 @@ init({tcp, http}, Req, Opts) ->
|
|||
{ok, Req, undefined_state}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(200, [], <<"Hello World!">>, Req),
|
||||
{ok, Req2} = cowboy_req:reply(200, [], <<"Hello World!">>, Req),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(Req, State) ->
|
||||
|
@ -110,7 +110,7 @@ init({tcp, http}, Req, Opts) ->
|
|||
{loop, Req, undefined_state, ?TIMEOUT, hibernate}.
|
||||
|
||||
info({reply, Body}, Req, State) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(200, [], Body, Req),
|
||||
{ok, Req2} = cowboy_req:reply(200, [], Body, Req),
|
||||
{ok, Req2, State};
|
||||
info(Message, Req, State) ->
|
||||
{loop, Req, State, hibernate}.
|
||||
|
@ -149,13 +149,13 @@ example, host rule `['...', <<"ninenines">>, <<"eu">>]` can match both
|
|||
"cowboy.bugs.ninenines.eu" and "ninenines.eu" and path rule
|
||||
`[<<"projects">>, '...']` can match both "/projects" and
|
||||
"/projects/cowboy/issues/42". The host leading segments and the path trailing
|
||||
segments can later be retrieved through `cowboy_http_req:host_info/1` and
|
||||
`cowboy_http_req:path_info/1`.
|
||||
segments can later be retrieved through `cowboy_req:host_info/1` and
|
||||
`cowboy_req:path_info/1`.
|
||||
|
||||
Any other atom used as a token will bind the value to this atom when
|
||||
matching. To follow on our hostnames example, `[<<"ninenines">>, ext]`
|
||||
would bind the values `<<"eu">>` and `<<"fr">>` to the ext atom, that you
|
||||
can later retrieve in your handler by calling `cowboy_http_req:binding/{2,3}`.
|
||||
can later retrieve in your handler by calling `cowboy_req:binding/{2,3}`.
|
||||
|
||||
You can also accept any match spec by using the atom `'_'` directly instead of
|
||||
a list of tokens. Our hello world example above uses this to forward all
|
||||
|
@ -168,7 +168,7 @@ Requests handling
|
|||
|
||||
Requests are passed around in the Request variable. Although they are
|
||||
defined as a record, it is recommended to access them only through the
|
||||
cowboy_http_req module API.
|
||||
cowboy_req module API.
|
||||
|
||||
You can retrieve the HTTP method, HTTP version, peer address and port,
|
||||
host tokens, raw host, used port, path tokens, raw path, query string
|
||||
|
@ -177,7 +177,7 @@ request. You can also read the request body, if any, optionally parsing
|
|||
it as a query string. Finally, the request allows you to send a response
|
||||
to the client.
|
||||
|
||||
See the cowboy_http_req module for more information.
|
||||
See the cowboy_req module for more information.
|
||||
|
||||
Websockets
|
||||
----------
|
||||
|
|
|
@ -11,12 +11,12 @@ init(_Transport, Req, []) ->
|
|||
{ok, Req, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{ok, Req2} = cowboy_http_req:chunked_reply(200, Req),
|
||||
ok = cowboy_http_req:chunk("Hello\r\n", Req2),
|
||||
{ok, Req2} = cowboy_req:chunked_reply(200, Req),
|
||||
ok = cowboy_req:chunk("Hello\r\n", Req2),
|
||||
ok = timer:sleep(1000),
|
||||
ok = cowboy_http_req:chunk("World\r\n", Req2),
|
||||
ok = cowboy_req:chunk("World\r\n", Req2),
|
||||
ok = timer:sleep(1000),
|
||||
ok = cowboy_http_req:chunk("Chunked!\r\n", Req2),
|
||||
ok = cowboy_req:chunk("Chunked!\r\n", Req2),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -11,19 +11,19 @@ init(_Transport, Req, []) ->
|
|||
{ok, Req, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{Method, Req2} = cowboy_http_req:method(Req),
|
||||
{Echo, Req3} = cowboy_http_req:qs_val(<<"echo">>, Req2),
|
||||
{Method, Req2} = cowboy_req:method(Req),
|
||||
{Echo, Req3} = cowboy_req:qs_val(<<"echo">>, Req2),
|
||||
{ok, Req4} = echo(Method, Echo, Req3),
|
||||
{ok, Req4, State}.
|
||||
|
||||
echo('GET', undefined, Req) ->
|
||||
cowboy_http_req:reply(400, [], <<"Missing echo parameter.">>, Req);
|
||||
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
|
||||
echo('GET', Echo, Req) ->
|
||||
cowboy_http_req:reply(200,
|
||||
cowboy_req:reply(200,
|
||||
[{<<"Content-Encoding">>, <<"utf-8">>}], Echo, Req);
|
||||
echo(_, _, Req) ->
|
||||
%% Method not allowed.
|
||||
cowboy_http_req:reply(405, Req).
|
||||
cowboy_req:reply(405, Req).
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
ok.
|
||||
|
|
|
@ -11,25 +11,25 @@ init(_Transport, Req, []) ->
|
|||
{ok, Req, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{Method, Req2} = cowboy_http_req:method(Req),
|
||||
{HasBody, Req3} = cowboy_http_req:has_body(Req2),
|
||||
{Method, Req2} = cowboy_req:method(Req),
|
||||
{HasBody, Req3} = cowboy_req:has_body(Req2),
|
||||
{ok, Req4} = maybe_echo(Method, HasBody, Req3),
|
||||
{ok, Req4, State}.
|
||||
|
||||
maybe_echo('POST', true, Req) ->
|
||||
{PostVals, Req2} = cowboy_http_req:body_qs(Req),
|
||||
{PostVals, Req2} = cowboy_req:body_qs(Req),
|
||||
Echo = proplists:get_value(<<"echo">>, PostVals),
|
||||
echo(Echo, Req2);
|
||||
maybe_echo('POST', false, Req) ->
|
||||
cowboy_http_req:reply(400, [], <<"Missing body.">>, Req);
|
||||
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
|
||||
maybe_echo(_, _, Req) ->
|
||||
%% Method not allowed.
|
||||
cowboy_http_req:reply(405, Req).
|
||||
cowboy_req:reply(405, Req).
|
||||
|
||||
echo(undefined, Req) ->
|
||||
cowboy_http_req:reply(400, [], <<"Missing echo parameter.">>, Req);
|
||||
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
|
||||
echo(Echo, Req) ->
|
||||
cowboy_http_req:reply(200,
|
||||
cowboy_req:reply(200,
|
||||
[{<<"Content-Encoding">>, <<"utf-8">>}], Echo, Req).
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -11,7 +11,7 @@ init(_Transport, Req, []) ->
|
|||
{ok, Req, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(200, [], <<"Hello world!">>, Req),
|
||||
{ok, Req2} = cowboy_req:reply(200, [], <<"Hello world!">>, Req),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -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}) ->
|
||||
|
|
|
@ -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}.
|
||||
|
||||
|
|
|
@ -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, <<>>).
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{
|
|
@ -8,9 +8,9 @@ init({_Transport, http}, Req, _Opts) ->
|
|||
{ok, Req, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{ok, Req2} = cowboy_http_req:chunked_reply(200, Req),
|
||||
cowboy_http_req:chunk("chunked_handler\r\n", Req2),
|
||||
cowboy_http_req:chunk("works fine!", Req2),
|
||||
{ok, Req2} = cowboy_req:chunked_reply(200, Req),
|
||||
cowboy_req:chunk("chunked_handler\r\n", Req2),
|
||||
cowboy_req:chunk("works fine!", Req2),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -597,13 +597,13 @@ onrequest_reply(Config) ->
|
|||
|
||||
%% Hook for the above onrequest tests.
|
||||
onrequest_hook(Req) ->
|
||||
case cowboy_http_req:qs_val(<<"reply">>, Req) of
|
||||
case cowboy_req:qs_val(<<"reply">>, Req) of
|
||||
{undefined, Req2} ->
|
||||
{ok, Req3} = cowboy_http_req:set_resp_header(
|
||||
{ok, Req3} = cowboy_req:set_resp_header(
|
||||
'Server', <<"Serenity">>, Req2),
|
||||
Req3;
|
||||
{_, Req2} ->
|
||||
{ok, Req3} = cowboy_http_req:reply(
|
||||
{ok, Req3} = cowboy_req:reply(
|
||||
200, [], <<"replied!">>, Req2),
|
||||
Req3
|
||||
end.
|
||||
|
@ -626,7 +626,7 @@ onresponse_reply(Config) ->
|
|||
|
||||
%% Hook for the above onresponse tests.
|
||||
onresponse_hook(_, Headers, Req) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(
|
||||
{ok, Req2} = cowboy_req:reply(
|
||||
<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
|
||||
Req2.
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ init({_Transport, http}, Req, Opts) ->
|
|||
{ok, Req, #state{headers=Headers, body=Body}}.
|
||||
|
||||
handle(Req, State=#state{headers=Headers, body=Body}) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(200, Headers, Body, Req),
|
||||
{ok, Req2} = cowboy_req:reply(200, Headers, Body, Req),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -8,11 +8,11 @@ init({_, http}, Req, _) ->
|
|||
{ok, Req, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{true, Req1} = cowboy_http_req:has_body(Req),
|
||||
{ok, Body, Req2} = cowboy_http_req:body(Req1),
|
||||
{Size, Req3} = cowboy_http_req:body_length(Req2),
|
||||
{true, Req1} = cowboy_req:has_body(Req),
|
||||
{ok, Body, Req2} = cowboy_req:body(Req1),
|
||||
{Size, Req3} = cowboy_req:body_length(Req2),
|
||||
Size = byte_size(Body),
|
||||
{ok, Req4} = cowboy_http_req:reply(200, [], Body, Req3),
|
||||
{ok, Req4} = cowboy_req:reply(200, [], Body, Req3),
|
||||
{ok, Req4, State}.
|
||||
|
||||
terminate(_, _) ->
|
||||
|
|
|
@ -5,18 +5,18 @@
|
|||
-export([init/3, handle/2, terminate/2]).
|
||||
|
||||
init({_Transport, http}, Req, _Opts) ->
|
||||
{Case, Req1} = cowboy_http_req:qs_val(<<"case">>, Req),
|
||||
{Case, Req1} = cowboy_req:qs_val(<<"case">>, Req),
|
||||
case_init(Case, Req1).
|
||||
|
||||
case_init(<<"init_before_reply">> = Case, _Req) ->
|
||||
erlang:error(Case);
|
||||
|
||||
case_init(<<"init_after_reply">> = Case, Req) ->
|
||||
{ok, _Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req),
|
||||
{ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req),
|
||||
erlang:error(Case);
|
||||
|
||||
case_init(<<"init_reply_handle_error">> = Case, Req) ->
|
||||
{ok, Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req),
|
||||
{ok, Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req),
|
||||
{ok, Req1, Case};
|
||||
|
||||
case_init(<<"handle_before_reply">> = Case, Req) ->
|
||||
|
@ -33,7 +33,7 @@ handle(_Req, <<"handle_before_reply">> = Case) ->
|
|||
erlang:error(Case);
|
||||
|
||||
handle(Req, <<"handle_after_reply">> = Case) ->
|
||||
{ok, _Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req),
|
||||
{ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req),
|
||||
erlang:error(Case).
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -5,12 +5,12 @@
|
|||
-export([init/3, handle/2, terminate/2]).
|
||||
|
||||
init({_Transport, http}, Req, _Opts) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(<<"666 Init Shutdown Testing">>,
|
||||
{ok, Req2} = cowboy_req:reply(<<"666 Init Shutdown Testing">>,
|
||||
[{'Connection', <<"close">>}], Req),
|
||||
{shutdown, Req2, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(200, [], "Hello world!", Req),
|
||||
{ok, Req2} = cowboy_req:reply(200, [], "Hello world!", Req),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -12,7 +12,7 @@ handle(_Req, _State) ->
|
|||
exit(badarg).
|
||||
|
||||
info(timeout, Req, 0) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(102, Req),
|
||||
{ok, Req2} = cowboy_req:reply(102, Req),
|
||||
{ok, Req2, 0};
|
||||
info(timeout, Req, State) ->
|
||||
erlang:send_after(500, self(), timeout),
|
||||
|
|
|
@ -9,7 +9,7 @@ init({_, http}, Req, _) ->
|
|||
{loop, Req, undefined, 500, hibernate}.
|
||||
|
||||
info(error_timeout, Req, State) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(500, Req),
|
||||
{ok, Req2} = cowboy_req:reply(500, Req),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(_, _) ->
|
||||
|
|
|
@ -9,14 +9,14 @@ init({_Transport, http}, Req, []) ->
|
|||
|
||||
handle(Req, State) ->
|
||||
{Result, Req2} = acc_multipart(Req, []),
|
||||
{ok, Req3} = cowboy_http_req:reply(200, [], term_to_binary(Result), Req2),
|
||||
{ok, Req3} = cowboy_req:reply(200, [], term_to_binary(Result), Req2),
|
||||
{ok, Req3, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
ok.
|
||||
|
||||
acc_multipart(Req, Acc) ->
|
||||
{Result, Req2} = cowboy_http_req:multipart_data(Req),
|
||||
{Result, Req2} = cowboy_req:multipart_data(Req),
|
||||
acc_multipart(Req2, Acc, Result).
|
||||
|
||||
acc_multipart(Req, Acc, {headers, Headers}) ->
|
||||
|
|
|
@ -8,23 +8,23 @@ init({_Transport, http}, Req, Opts) ->
|
|||
Headers = proplists:get_value(headers, Opts, []),
|
||||
Body = proplists:get_value(body, Opts, <<"http_handler_set_resp">>),
|
||||
{ok, Req2} = lists:foldl(fun({Name, Value}, {ok, R}) ->
|
||||
cowboy_http_req:set_resp_header(Name, Value, R)
|
||||
cowboy_req:set_resp_header(Name, Value, R)
|
||||
end, {ok, Req}, Headers),
|
||||
{ok, Req3} = cowboy_http_req:set_resp_body(Body, Req2),
|
||||
{ok, Req4} = cowboy_http_req:set_resp_header(
|
||||
{ok, Req3} = cowboy_req:set_resp_body(Body, Req2),
|
||||
{ok, Req4} = cowboy_req:set_resp_header(
|
||||
<<"X-Cowboy-Test">>, <<"ok">>, Req3),
|
||||
{ok, Req5} = cowboy_http_req:set_resp_cookie(
|
||||
{ok, Req5} = cowboy_req:set_resp_cookie(
|
||||
<<"cake">>, <<"lie">>, [], Req4),
|
||||
{ok, Req5, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
case cowboy_http_req:has_resp_header(<<"X-Cowboy-Test">>, Req) of
|
||||
case cowboy_req:has_resp_header(<<"X-Cowboy-Test">>, Req) of
|
||||
false -> {ok, Req, State};
|
||||
true ->
|
||||
case cowboy_http_req:has_resp_body(Req) of
|
||||
case cowboy_req:has_resp_body(Req) of
|
||||
false -> {ok, Req, State};
|
||||
true ->
|
||||
{ok, Req2} = cowboy_http_req:reply(200, Req),
|
||||
{ok, Req2} = cowboy_req:reply(200, Req),
|
||||
{ok, Req2, State}
|
||||
end
|
||||
end.
|
||||
|
|
|
@ -13,11 +13,11 @@ init({_Transport, http}, Req, Opts) ->
|
|||
{ok, Req, #state{headers=Headers, body=Body, reply=Reply}}.
|
||||
|
||||
handle(Req, State=#state{headers=_Headers, body=Body, reply=set_resp}) ->
|
||||
{ok, Transport, Socket} = cowboy_http_req:transport(Req),
|
||||
{ok, Transport, Socket} = cowboy_req:transport(Req),
|
||||
SFun = fun() -> Transport:send(Socket, Body), sent end,
|
||||
SLen = iolist_size(Body),
|
||||
{ok, Req2} = cowboy_http_req:set_resp_body_fun(SLen, SFun, Req),
|
||||
{ok, Req3} = cowboy_http_req:reply(200, Req2),
|
||||
{ok, Req2} = cowboy_req:set_resp_body_fun(SLen, SFun, Req),
|
||||
{ok, Req3} = cowboy_req:reply(200, Req2),
|
||||
{ok, Req3, State}.
|
||||
|
||||
terminate(_Req, _State) ->
|
||||
|
|
|
@ -27,7 +27,7 @@ post_is_create(Req, State) ->
|
|||
{true, Req, State}.
|
||||
|
||||
create_path(Req, State) ->
|
||||
{Path, Req2} = cowboy_http_req:raw_path(Req),
|
||||
{Path, Req2} = cowboy_req:raw_path(Req),
|
||||
{Path, Req2, State}.
|
||||
|
||||
to_text(Req, State) ->
|
||||
|
|
|
@ -5,7 +5,7 @@ init(_Transport, _Req, _Opts) ->
|
|||
{upgrade, protocol, cowboy_http_rest}.
|
||||
|
||||
generate_etag(Req, State) ->
|
||||
case cowboy_http_req:qs_val(<<"type">>, Req) of
|
||||
case cowboy_req:qs_val(<<"type">>, Req) of
|
||||
%% Correct return values from generate_etag/2.
|
||||
{<<"tuple-weak">>, Req2} ->
|
||||
{{weak, <<"etag-header-value">>}, Req2, State};
|
||||
|
|
|
@ -17,7 +17,7 @@ terminate(_Req, _State) ->
|
|||
exit(badarg).
|
||||
|
||||
websocket_init(_TransportName, Req, _Opts) ->
|
||||
Req2 = cowboy_http_req:compact(Req),
|
||||
Req2 = cowboy_req:compact(Req),
|
||||
{ok, Req2, undefined}.
|
||||
|
||||
websocket_handle({text, Data}, Req, State) ->
|
||||
|
|
|
@ -18,7 +18,7 @@ terminate(_Req, _State) ->
|
|||
|
||||
websocket_init(_TransportName, Req, _Opts) ->
|
||||
erlang:start_timer(1000, self(), <<"websocket_init">>),
|
||||
Req2 = cowboy_http_req:compact(Req),
|
||||
Req2 = cowboy_req:compact(Req),
|
||||
{ok, Req2, undefined}.
|
||||
|
||||
websocket_handle({text, Data}, Req, State) ->
|
||||
|
|
|
@ -17,7 +17,7 @@ terminate(_Req, _State) ->
|
|||
exit(badarg).
|
||||
|
||||
websocket_init(_TransportName, Req, _Opts) ->
|
||||
{ok, Req2} = cowboy_http_req:reply(403, Req),
|
||||
{ok, Req2} = cowboy_req:reply(403, Req),
|
||||
{shutdown, Req2}.
|
||||
|
||||
websocket_handle(_Frame, _Req, _State) ->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue