aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cowboy_req.erl10
-rw-r--r--src/cowboy_rest.erl26
-rw-r--r--src/cowboy_websocket.erl10
3 files changed, 27 insertions, 19 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index ad6c1a4..2625c58 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -49,6 +49,7 @@
-export([cookies/1]).
-export([meta/2]).
-export([meta/3]).
+-export([set_meta/3]).
%% Request body API.
-export([has_body/1]).
@@ -429,6 +430,15 @@ meta(Name, Req, Default) ->
false -> {Default, Req}
end.
+%% @doc Set metadata information.
+%%
+%% You can use this function to attach information about the request.
+%%
+%% If the value already exists it will be overwritten.
+-spec set_meta(atom(), any(), Req) -> Req when Req::req().
+set_meta(Name, Value, Req=#http_req{meta=Meta}) ->
+ Req#http_req{meta=[{Name, Value}|lists:keydelete(Name, 1, Meta)]}.
+
%% Request Body API.
%% @doc Return whether the request message has a body.
diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl
index fc4279b..382814d 100644
--- a/src/cowboy_rest.erl
+++ b/src/cowboy_rest.erl
@@ -195,7 +195,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=#http_req{meta=Meta}, State) ->
+content_types_provided(Req, State) ->
case call(Req, State, content_types_provided) of
no_call ->
not_acceptable(Req, State);
@@ -212,7 +212,7 @@ content_types_provided(Req=#http_req{meta=Meta}, State) ->
undefined ->
{PMT, _Fun} = HeadCTP = hd(CTP2),
languages_provided(
- Req3#http_req{meta=[{media_type, PMT}|Meta]},
+ cowboy_req:set_meta(media_type, PMT, Req3),
State2#state{content_type_a=HeadCTP});
Accept ->
Accept2 = prioritize_accept(Accept),
@@ -275,12 +275,12 @@ 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=#http_req{meta=Meta}, State, Accept,
+match_media_type_params(Req, 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#http_req{meta=[{media_type, PMT}|Meta]},
+ languages_provided(cowboy_req:set_meta(media_type, PMT, Req),
State#state{content_type_a=Provided});
false ->
match_media_type(Req, State, Accept, Tail, MediaType)
@@ -347,10 +347,10 @@ match_language(Req, State, Accept, [Provided|Tail],
match_language(Req, State, Accept, Tail, Language)
end.
-set_language(Req=#http_req{meta=Meta}, State=#state{language_a=Language}) ->
+set_language(Req, State=#state{language_a=Language}) ->
{ok, Req2} = cowboy_req:set_resp_header(
<<"Content-Language">>, Language, Req),
- charsets_provided(Req2#http_req{meta=[{language, Language}|Meta]}, State).
+ charsets_provided(cowboy_req:set_meta(language, Language, Req2), State).
%% charsets_provided should return a list of binary values indicating
%% which charsets are accepted by the resource.
@@ -404,7 +404,7 @@ match_charset(Req, State, _Accept, [{Provided, _}|_], {Provided, _}) ->
match_charset(Req, State, Accept, [_|Tail], Charset) ->
match_charset(Req, State, Accept, Tail, Charset).
-set_content_type(Req=#http_req{meta=Meta}, State=#state{
+set_content_type(Req, State=#state{
content_type_a={{Type, SubType, Params}, _Fun},
charset_a=Charset}) ->
ParamsBin = set_content_type_build_params(Params, []),
@@ -415,7 +415,7 @@ set_content_type(Req=#http_req{meta=Meta}, State=#state{
end,
{ok, Req2} = cowboy_req:set_resp_header(
<<"Content-Type">>, ContentType2, Req),
- encodings_provided(Req2#http_req{meta=[{charset, Charset}|Meta]}, State).
+ encodings_provided(cowboy_req:set_meta(charset, Charset, Req2), State).
set_content_type_build_params([], []) ->
<<>>;
@@ -659,7 +659,7 @@ post_is_create(Req, State) ->
%% When the POST method can create new resources, create_path/2 will be called
%% and is expected to return the full path to the new resource
%% (including the leading /).
-create_path(Req=#http_req{meta=Meta}, State) ->
+create_path(Req, State) ->
case call(Req, State, create_path) of
{halt, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState});
@@ -668,7 +668,7 @@ create_path(Req=#http_req{meta=Meta}, State) ->
State2 = State#state{handler_state=HandlerState},
{ok, Req3} = cowboy_req:set_resp_header(
<<"Location">>, Location, Req2),
- put_resource(Req3#http_req{meta=[{put_path, Path}|Meta]},
+ put_resource(cowboy_req:set_meta(put_path, Path, Req3),
State2, 303)
end.
@@ -706,9 +706,9 @@ process_post(Req, State) ->
is_conflict(Req, State) ->
expect(Req, State, is_conflict, false, fun put_resource/2, 409).
-put_resource(Req=#http_req{path=RawPath, meta=Meta}, State) ->
- Req2 = Req#http_req{meta=[{put_path, RawPath}|Meta]},
- put_resource(Req2, State, fun is_new_resource/2).
+put_resource(Req=#http_req{path=RawPath}, State) ->
+ put_resource(cowboy_req:set_meta(put_path, RawPath, Req),
+ State, fun is_new_resource/2).
%% content_types_accepted should return a list of media types and their
%% associated callback functions in the same format as content_types_provided.
diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl
index 55855c7..7382344 100644
--- a/src/cowboy_websocket.erl
+++ b/src/cowboy_websocket.erl
@@ -25,8 +25,6 @@
%% Internal.
-export([handler_loop/4]).
--include("http.hrl").
-
-type opcode() :: 0 | 1 | 2 | 8 | 9 | 10.
-type mask_key() :: 0..16#ffffffff.
@@ -91,16 +89,16 @@ 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=#http_req{meta=Meta}) ->
+websocket_upgrade(undefined, State, Req) ->
{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},
- eop=EOP}, Req4#http_req{meta=[{websocket_version, 0}|Meta]}};
+ eop=EOP}, cowboy_req:set_meta(websocket_version, 0, Req4)};
%% Versions 7 and 8. Implementation follows the hybi 7 through 17 drafts.
-websocket_upgrade(Version, State, Req=#http_req{meta=Meta})
+websocket_upgrade(Version, State, Req)
when Version =:= <<"7">>; Version =:= <<"8">>;
Version =:= <<"13">> ->
{Key, Req2} = cowboy_req:header(<<"Sec-Websocket-Key">>, Req),
@@ -108,7 +106,7 @@ websocket_upgrade(Version, State, Req=#http_req{meta=Meta})
Challenge = hybi_challenge(Key),
IntVersion = list_to_integer(binary_to_list(Version)),
{ok, State#state{version=IntVersion, challenge=Challenge},
- Req2#http_req{meta=[{websocket_version, IntVersion}|Meta]}}.
+ cowboy_req:set_meta(websocket_version, IntVersion, Req2)}.
-spec handler_init(#state{}, cowboy_req:req()) -> closed.
handler_init(State=#state{transport=Transport, handler=Handler, opts=Opts},