Browse Source

Introduce cowboy_req:set_meta/3 to set request metadata

Loïc Hoguin 12 years ago
parent
commit
9ef94fb64e
3 changed files with 27 additions and 19 deletions
  1. 10 0
      src/cowboy_req.erl
  2. 13 13
      src/cowboy_rest.erl
  3. 4 6
      src/cowboy_websocket.erl

+ 10 - 0
src/cowboy_req.erl

@@ -49,6 +49,7 @@
 -export([cookies/1]).
 -export([cookies/1]).
 -export([meta/2]).
 -export([meta/2]).
 -export([meta/3]).
 -export([meta/3]).
+-export([set_meta/3]).
 
 
 %% Request body API.
 %% Request body API.
 -export([has_body/1]).
 -export([has_body/1]).
@@ -429,6 +430,15 @@ meta(Name, Req, Default) ->
 		false -> {Default, Req}
 		false -> {Default, Req}
 	end.
 	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.
 %% Request Body API.
 
 
 %% @doc Return whether the request message has a body.
 %% @doc Return whether the request message has a body.

+ 13 - 13
src/cowboy_rest.erl

@@ -195,7 +195,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=#http_req{meta=Meta}, State) ->
+content_types_provided(Req, 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);
@@ -212,7 +212,7 @@ content_types_provided(Req=#http_req{meta=Meta}, State) ->
 				undefined ->
 				undefined ->
 					{PMT, _Fun} = HeadCTP = hd(CTP2),
 					{PMT, _Fun} = HeadCTP = hd(CTP2),
 					languages_provided(
 					languages_provided(
-						Req3#http_req{meta=[{media_type, PMT}|Meta]},
+						cowboy_req:set_meta(media_type, PMT, Req3),
 						State2#state{content_type_a=HeadCTP});
 						State2#state{content_type_a=HeadCTP});
 				Accept ->
 				Accept ->
 					Accept2 = prioritize_accept(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, [_Any|Tail], MediaType) ->
 	match_media_type(Req, State, Accept, 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],
 		[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#http_req{meta=[{media_type, PMT}|Meta]},
+			languages_provided(cowboy_req:set_meta(media_type, PMT, Req),
 				State#state{content_type_a=Provided});
 				State#state{content_type_a=Provided});
 		false ->
 		false ->
 			match_media_type(Req, State, Accept, Tail, MediaType)
 			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)
 			match_language(Req, State, Accept, Tail, Language)
 	end.
 	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(
 	{ok, Req2} = cowboy_req:set_resp_header(
 		<<"Content-Language">>, Language, Req),
 		<<"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
 %% charsets_provided should return a list of binary values indicating
 %% which charsets are accepted by the resource.
 %% 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) ->
 	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},
 		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, []),
@@ -415,7 +415,7 @@ set_content_type(Req=#http_req{meta=Meta}, State=#state{
 	end,
 	end,
 	{ok, Req2} = cowboy_req:set_resp_header(
 	{ok, Req2} = cowboy_req:set_resp_header(
 		<<"Content-Type">>, ContentType2, Req),
 		<<"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([], []) ->
 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
 %% 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
 %% and is expected to return the full path to the new resource
 %% (including the leading /).
 %% (including the leading /).
-create_path(Req=#http_req{meta=Meta}, State) ->
+create_path(Req, State) ->
 	case call(Req, State, create_path) of
 	case call(Req, State, create_path) of
 		{halt, Req2, HandlerState} ->
 		{halt, Req2, HandlerState} ->
 			terminate(Req2, State#state{handler_state=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},
 			State2 = State#state{handler_state=HandlerState},
 			{ok, Req3} = cowboy_req:set_resp_header(
 			{ok, Req3} = cowboy_req:set_resp_header(
 				<<"Location">>, Location, Req2),
 				<<"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)
 				State2, 303)
 	end.
 	end.
 
 
@@ -706,9 +706,9 @@ process_post(Req, State) ->
 is_conflict(Req, State) ->
 is_conflict(Req, State) ->
 	expect(Req, State, is_conflict, false, fun put_resource/2, 409).
 	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
 %% content_types_accepted should return a list of media types and their
 %% associated callback functions in the same format as content_types_provided.
 %% associated callback functions in the same format as content_types_provided.

+ 4 - 6
src/cowboy_websocket.erl

@@ -25,8 +25,6 @@
 %% Internal.
 %% Internal.
 -export([handler_loop/4]).
 -export([handler_loop/4]).
 
 
--include("http.hrl").
-
 -type opcode() :: 0 | 1 | 2 | 8 | 9 | 10.
 -type opcode() :: 0 | 1 | 2 | 8 | 9 | 10.
 -type mask_key() :: 0..16#ffffffff.
 -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
 %% 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=#http_req{meta=Meta}) ->
+websocket_upgrade(undefined, State, Req) ->
 	{Origin, Req2} = cowboy_req:header(<<"Origin">>, Req),
 	{Origin, Req2} = cowboy_req:header(<<"Origin">>, Req),
 	{Key1, Req3} = cowboy_req:header(<<"Sec-Websocket-Key1">>, Req2),
 	{Key1, Req3} = cowboy_req:header(<<"Sec-Websocket-Key1">>, Req2),
 	{Key2, Req4} = cowboy_req:header(<<"Sec-Websocket-Key2">>, Req3),
 	{Key2, Req4} = cowboy_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#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.
 %% 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">>;
 		when Version =:= <<"7">>; Version =:= <<"8">>;
 			Version =:= <<"13">> ->
 			Version =:= <<"13">> ->
 	{Key, Req2} = cowboy_req:header(<<"Sec-Websocket-Key">>, Req),
 	{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),
 	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},
 	{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.
 -spec handler_init(#state{}, cowboy_req:req()) -> closed.
 handler_init(State=#state{transport=Transport, handler=Handler, opts=Opts},
 handler_init(State=#state{transport=Transport, handler=Handler, opts=Opts},