From f390dbd60671fd49bd99137f539794b1fbe1c718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 19 Dec 2011 09:44:24 +0100 Subject: 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. --- src/cowboy_http_req.erl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/cowboy_http_req.erl') 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 {error, badarg} -- cgit v1.2.3