aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http_req.erl20
-rw-r--r--src/cowboy_http_rest.erl23
-rw-r--r--src/cowboy_http_websocket.erl13
3 files changed, 39 insertions, 17 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index c302100..d4b7fd3 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -29,7 +29,8 @@
binding/2, binding/3, bindings/1,
header/2, header/3, headers/1,
parse_header/2, parse_header/3,
- cookie/2, cookie/3, cookies/1
+ cookie/2, cookie/3, cookies/1,
+ meta/2, meta/3
]). %% Request API.
-export([
@@ -341,6 +342,23 @@ cookies(Req=#http_req{cookies=undefined}) ->
cookies(Req=#http_req{cookies=Cookies}) ->
{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.
%% @doc Return the full body sent with the request, or <em>{error, badarg}</em>
diff --git a/src/cowboy_http_rest.erl b/src/cowboy_http_rest.erl
index 474964f..6bf256a 100644
--- a/src/cowboy_http_rest.erl
+++ b/src/cowboy_http_rest.erl
@@ -182,7 +182,7 @@ options(Req, State) ->
%% resources a little more readable, this is a lot less efficient. An example
%% of such a return value would be:
%% {<<"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
no_call ->
not_acceptable(Req, State);
@@ -195,8 +195,10 @@ content_types_provided(Req, State) ->
{Accept, Req3} = cowboy_http_req:parse_header('Accept', Req2),
case Accept of
undefined ->
- languages_provided(Req3,
- State2#state{content_type_a=hd(CTP2)});
+ {PMT, _Fun} = HeadCTP = hd(CTP2),
+ languages_provided(
+ Req3#http_req{meta=[{media_type, PMT}|Meta]},
+ State2#state{content_type_a=HeadCTP});
Accept ->
Accept2 = prioritize_accept(Accept),
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, Tail, MediaType).
-match_media_type_params(Req, State, Accept,
- [Provided = {{_TP, _STP, Params_P}, _Fun}|Tail],
+match_media_type_params(Req=#http_req{meta=Meta}, State, Accept,
+ [Provided = {PMT = {_TP, _STP, Params_P}, _Fun}|Tail],
MediaType = {{_TA, _STA, Params_A}, _QA, _APA}) ->
case lists:sort(Params_P) =:= lists:sort(Params_A) of
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 ->
match_media_type(Req, State, Accept, Tail, MediaType)
end.
@@ -327,10 +330,10 @@ match_language(Req, State, Accept, [Provided|Tail],
match_language(Req, State, Accept, Tail, Language)
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(
<<"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
%% 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, 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},
charset_a=Charset}) ->
ParamsBin = set_content_type_build_params(Params, []),
@@ -393,7 +396,7 @@ set_content_type(Req, State=#state{
end,
{ok, Req2} = cowboy_http_req:set_resp_header(
<<"Content-Type">>, ContentType2, Req),
- encodings_provided(Req2, State).
+ encodings_provided(Req2#http_req{meta=[{charset, Charset}|Meta]}, State).
set_content_type_build_params([], []) ->
<<>>;
diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl
index 20060e6..dd1550e 100644
--- a/src/cowboy_http_websocket.erl
+++ b/src/cowboy_http_websocket.erl
@@ -30,9 +30,9 @@
%% <li>Firefox 6</li>
%% </ul>
%%
-%% Version 8 is supported by the following browsers:
+%% Version 8+ is supported by the following browsers:
%% <ul>
-%% <li>Firefox 7</li>
+%% <li>Firefox 7+</li>
%% <li>Chrome 14+</li>
%% </ul>
-module(cowboy_http_websocket).
@@ -95,23 +95,24 @@ websocket_upgrade(State, Req) ->
%% third part of the challenge key, because proxies will wait for
%% a reply before sending it. Therefore we calculate the challenge
%% 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),
{Key1, Req3} = cowboy_http_req:header(<<"Sec-Websocket-Key1">>, Req2),
{Key2, Req4} = cowboy_http_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},
- 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.
-websocket_upgrade(Version, State, Req)
+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),
false = Key =:= undefined,
Challenge = hybi_challenge(Key),
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().
handler_init(State=#state{handler=Handler, opts=Opts},