diff options
author | Loïc Hoguin <[email protected]> | 2011-12-19 09:44:24 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2011-12-19 09:44:47 +0100 |
commit | f390dbd60671fd49bd99137f539794b1fbe1c718 (patch) | |
tree | dc38e8f6d9697565cff720e43c4952b6f78d6cd0 /src/cowboy_http_req.erl | |
parent | f9bd5d1061813be1405977af57390ca7af40e39e (diff) | |
download | cowboy-f390dbd60671fd49bd99137f539794b1fbe1c718.tar.gz cowboy-f390dbd60671fd49bd99137f539794b1fbe1c718.tar.bz2 cowboy-f390dbd60671fd49bd99137f539794b1fbe1c718.zip |
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.
Diffstat (limited to 'src/cowboy_http_req.erl')
-rw-r--r-- | src/cowboy_http_req.erl | 20 |
1 files changed, 19 insertions, 1 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> |