mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add meta/2 and meta/3 to cowboy_http_req to save useful protocol information
* cowboy_http_protocol now defines 'websocket_version' as metadata. * cowboy_http_rest now defines 'media_type', 'language', 'charset' as metadata.
This commit is contained in:
parent
f9bd5d1061
commit
f390dbd606
4 changed files with 40 additions and 17 deletions
|
@ -60,6 +60,7 @@
|
||||||
headers = [] :: http_headers(),
|
headers = [] :: http_headers(),
|
||||||
p_headers = [] :: [any()], %% @todo Improve those specs.
|
p_headers = [] :: [any()], %% @todo Improve those specs.
|
||||||
cookies = undefined :: undefined | http_cookies(),
|
cookies = undefined :: undefined | http_cookies(),
|
||||||
|
meta = [] :: [{atom(), any()}],
|
||||||
|
|
||||||
%% Request body.
|
%% Request body.
|
||||||
body_state = waiting :: waiting | done,
|
body_state = waiting :: waiting | done,
|
||||||
|
|
|
@ -29,7 +29,8 @@
|
||||||
binding/2, binding/3, bindings/1,
|
binding/2, binding/3, bindings/1,
|
||||||
header/2, header/3, headers/1,
|
header/2, header/3, headers/1,
|
||||||
parse_header/2, parse_header/3,
|
parse_header/2, parse_header/3,
|
||||||
cookie/2, cookie/3, cookies/1
|
cookie/2, cookie/3, cookies/1,
|
||||||
|
meta/2, meta/3
|
||||||
]). %% Request API.
|
]). %% Request API.
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
|
@ -341,6 +342,23 @@ cookies(Req=#http_req{cookies=undefined}) ->
|
||||||
cookies(Req=#http_req{cookies=Cookies}) ->
|
cookies(Req=#http_req{cookies=Cookies}) ->
|
||||||
{Cookies, Req}.
|
{Cookies, Req}.
|
||||||
|
|
||||||
|
%% @equiv meta(Name, Req, undefined)
|
||||||
|
-spec meta(atom(), #http_req{}) -> {any() | undefined, #http_req{}}.
|
||||||
|
meta(Name, Req) ->
|
||||||
|
meta(Name, Req, undefined).
|
||||||
|
|
||||||
|
%% @doc Return metadata information about the request.
|
||||||
|
%%
|
||||||
|
%% Metadata information varies from one protocol to another. Websockets
|
||||||
|
%% would define the protocol version here, while REST would use it to
|
||||||
|
%% indicate which media type, language and charset were retained.
|
||||||
|
-spec meta(atom(), #http_req{}, any()) -> {any(), #http_req{}}.
|
||||||
|
meta(Name, Req, Default) ->
|
||||||
|
case lists:keyfind(Name, 1, Req#http_req.meta) of
|
||||||
|
{Name, Value} -> {Value, Req};
|
||||||
|
false -> {Default, Req}
|
||||||
|
end.
|
||||||
|
|
||||||
%% Request Body API.
|
%% Request Body API.
|
||||||
|
|
||||||
%% @doc Return the full body sent with the request, or <em>{error, badarg}</em>
|
%% @doc Return the full body sent with the request, or <em>{error, badarg}</em>
|
||||||
|
|
|
@ -182,7 +182,7 @@ options(Req, State) ->
|
||||||
%% resources a little more readable, this is a lot less efficient. An example
|
%% resources a little more readable, this is a lot less efficient. An example
|
||||||
%% of such a return value would be:
|
%% of such a return value would be:
|
||||||
%% {<<"text/html">>, to_html}
|
%% {<<"text/html">>, to_html}
|
||||||
content_types_provided(Req, State) ->
|
content_types_provided(Req=#http_req{meta=Meta}, State) ->
|
||||||
case call(Req, State, content_types_provided) of
|
case call(Req, State, content_types_provided) of
|
||||||
no_call ->
|
no_call ->
|
||||||
not_acceptable(Req, State);
|
not_acceptable(Req, State);
|
||||||
|
@ -195,8 +195,10 @@ content_types_provided(Req, State) ->
|
||||||
{Accept, Req3} = cowboy_http_req:parse_header('Accept', Req2),
|
{Accept, Req3} = cowboy_http_req:parse_header('Accept', Req2),
|
||||||
case Accept of
|
case Accept of
|
||||||
undefined ->
|
undefined ->
|
||||||
languages_provided(Req3,
|
{PMT, _Fun} = HeadCTP = hd(CTP2),
|
||||||
State2#state{content_type_a=hd(CTP2)});
|
languages_provided(
|
||||||
|
Req3#http_req{meta=[{media_type, PMT}|Meta]},
|
||||||
|
State2#state{content_type_a=HeadCTP});
|
||||||
Accept ->
|
Accept ->
|
||||||
Accept2 = prioritize_accept(Accept),
|
Accept2 = prioritize_accept(Accept),
|
||||||
choose_media_type(Req3, State2, Accept2)
|
choose_media_type(Req3, State2, Accept2)
|
||||||
|
@ -258,12 +260,13 @@ match_media_type(Req, State, Accept,
|
||||||
match_media_type(Req, State, Accept, [_Any|Tail], MediaType) ->
|
match_media_type(Req, State, Accept, [_Any|Tail], MediaType) ->
|
||||||
match_media_type(Req, State, Accept, Tail, MediaType).
|
match_media_type(Req, State, Accept, Tail, MediaType).
|
||||||
|
|
||||||
match_media_type_params(Req, State, Accept,
|
match_media_type_params(Req=#http_req{meta=Meta}, State, Accept,
|
||||||
[Provided = {{_TP, _STP, Params_P}, _Fun}|Tail],
|
[Provided = {PMT = {_TP, _STP, Params_P}, _Fun}|Tail],
|
||||||
MediaType = {{_TA, _STA, Params_A}, _QA, _APA}) ->
|
MediaType = {{_TA, _STA, Params_A}, _QA, _APA}) ->
|
||||||
case lists:sort(Params_P) =:= lists:sort(Params_A) of
|
case lists:sort(Params_P) =:= lists:sort(Params_A) of
|
||||||
true ->
|
true ->
|
||||||
languages_provided(Req, State#state{content_type_a=Provided});
|
languages_provided(Req#http_req{meta=[{media_type, PMT}|Meta]},
|
||||||
|
State#state{content_type_a=Provided});
|
||||||
false ->
|
false ->
|
||||||
match_media_type(Req, State, Accept, Tail, MediaType)
|
match_media_type(Req, State, Accept, Tail, MediaType)
|
||||||
end.
|
end.
|
||||||
|
@ -327,10 +330,10 @@ match_language(Req, State, Accept, [Provided|Tail],
|
||||||
match_language(Req, State, Accept, Tail, Language)
|
match_language(Req, State, Accept, Tail, Language)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
set_language(Req, State=#state{language_a=Language}) ->
|
set_language(Req=#http_req{meta=Meta}, State=#state{language_a=Language}) ->
|
||||||
{ok, Req2} = cowboy_http_req:set_resp_header(
|
{ok, Req2} = cowboy_http_req:set_resp_header(
|
||||||
<<"Content-Language">>, Language, Req),
|
<<"Content-Language">>, Language, Req),
|
||||||
charsets_provided(Req2, State).
|
charsets_provided(Req2#http_req{meta=[{language, Language}|Meta]}, State).
|
||||||
|
|
||||||
%% charsets_provided should return a list of binary values indicating
|
%% charsets_provided should return a list of binary values indicating
|
||||||
%% which charsets are accepted by the resource.
|
%% which charsets are accepted by the resource.
|
||||||
|
@ -382,7 +385,7 @@ match_charset(Req, State, _Accept, [Provided|_Tail],
|
||||||
match_charset(Req, State, Accept, [_Provided|Tail], Charset) ->
|
match_charset(Req, State, Accept, [_Provided|Tail], Charset) ->
|
||||||
match_charset(Req, State, Accept, Tail, Charset).
|
match_charset(Req, State, Accept, Tail, Charset).
|
||||||
|
|
||||||
set_content_type(Req, State=#state{
|
set_content_type(Req=#http_req{meta=Meta}, State=#state{
|
||||||
content_type_a={{Type, SubType, Params}, _Fun},
|
content_type_a={{Type, SubType, Params}, _Fun},
|
||||||
charset_a=Charset}) ->
|
charset_a=Charset}) ->
|
||||||
ParamsBin = set_content_type_build_params(Params, []),
|
ParamsBin = set_content_type_build_params(Params, []),
|
||||||
|
@ -393,7 +396,7 @@ set_content_type(Req, State=#state{
|
||||||
end,
|
end,
|
||||||
{ok, Req2} = cowboy_http_req:set_resp_header(
|
{ok, Req2} = cowboy_http_req:set_resp_header(
|
||||||
<<"Content-Type">>, ContentType2, Req),
|
<<"Content-Type">>, ContentType2, Req),
|
||||||
encodings_provided(Req2, State).
|
encodings_provided(Req2#http_req{meta=[{charset, Charset}|Meta]}, State).
|
||||||
|
|
||||||
set_content_type_build_params([], []) ->
|
set_content_type_build_params([], []) ->
|
||||||
<<>>;
|
<<>>;
|
||||||
|
|
|
@ -30,9 +30,9 @@
|
||||||
%% <li>Firefox 6</li>
|
%% <li>Firefox 6</li>
|
||||||
%% </ul>
|
%% </ul>
|
||||||
%%
|
%%
|
||||||
%% Version 8 is supported by the following browsers:
|
%% Version 8+ is supported by the following browsers:
|
||||||
%% <ul>
|
%% <ul>
|
||||||
%% <li>Firefox 7</li>
|
%% <li>Firefox 7+</li>
|
||||||
%% <li>Chrome 14+</li>
|
%% <li>Chrome 14+</li>
|
||||||
%% </ul>
|
%% </ul>
|
||||||
-module(cowboy_http_websocket).
|
-module(cowboy_http_websocket).
|
||||||
|
@ -95,23 +95,24 @@ websocket_upgrade(State, Req) ->
|
||||||
%% third part of the challenge key, because proxies will wait for
|
%% third part of the challenge key, because proxies will wait for
|
||||||
%% a reply before sending it. Therefore we calculate the challenge
|
%% a reply before sending it. Therefore we calculate the challenge
|
||||||
%% key only in websocket_handshake/3.
|
%% key only in websocket_handshake/3.
|
||||||
websocket_upgrade(undefined, State, Req) ->
|
websocket_upgrade(undefined, State, Req=#http_req{meta=Meta}) ->
|
||||||
{Origin, Req2} = cowboy_http_req:header(<<"Origin">>, Req),
|
{Origin, Req2} = cowboy_http_req:header(<<"Origin">>, Req),
|
||||||
{Key1, Req3} = cowboy_http_req:header(<<"Sec-Websocket-Key1">>, Req2),
|
{Key1, Req3} = cowboy_http_req:header(<<"Sec-Websocket-Key1">>, Req2),
|
||||||
{Key2, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Key2">>, Req3),
|
{Key2, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Key2">>, Req3),
|
||||||
false = lists:member(undefined, [Origin, Key1, Key2]),
|
false = lists:member(undefined, [Origin, Key1, Key2]),
|
||||||
EOP = binary:compile_pattern(<< 255 >>),
|
EOP = binary:compile_pattern(<< 255 >>),
|
||||||
{ok, State#state{version=0, origin=Origin, challenge={Key1, Key2},
|
{ok, State#state{version=0, origin=Origin, challenge={Key1, Key2},
|
||||||
eop=EOP}, Req4};
|
eop=EOP}, Req4#http_req{meta=[{websocket_version, 0}|Meta]}};
|
||||||
%% Versions 7 and 8. Implementation follows the hybi 7 through 17 drafts.
|
%% Versions 7 and 8. Implementation follows the hybi 7 through 17 drafts.
|
||||||
websocket_upgrade(Version, State, Req)
|
websocket_upgrade(Version, State, Req=#http_req{meta=Meta})
|
||||||
when Version =:= <<"7">>; Version =:= <<"8">>;
|
when Version =:= <<"7">>; Version =:= <<"8">>;
|
||||||
Version =:= <<"13">> ->
|
Version =:= <<"13">> ->
|
||||||
{Key, Req2} = cowboy_http_req:header(<<"Sec-Websocket-Key">>, Req),
|
{Key, Req2} = cowboy_http_req:header(<<"Sec-Websocket-Key">>, Req),
|
||||||
false = Key =:= undefined,
|
false = Key =:= undefined,
|
||||||
Challenge = hybi_challenge(Key),
|
Challenge = hybi_challenge(Key),
|
||||||
IntVersion = list_to_integer(binary_to_list(Version)),
|
IntVersion = list_to_integer(binary_to_list(Version)),
|
||||||
{ok, State#state{version=IntVersion, challenge=Challenge}, Req2}.
|
{ok, State#state{version=IntVersion, challenge=Challenge},
|
||||||
|
Req2#http_req{meta=[{websocket_version, IntVersion}|Meta]}}.
|
||||||
|
|
||||||
-spec handler_init(#state{}, #http_req{}) -> closed | none().
|
-spec handler_init(#state{}, #http_req{}) -> closed | none().
|
||||||
handler_init(State=#state{handler=Handler, opts=Opts},
|
handler_init(State=#state{handler=Handler, opts=Opts},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue