aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-12-12 07:46:53 +0100
committerLoïc Hoguin <[email protected]>2011-12-12 07:47:08 +0100
commit7c1372df2077e5c4b81c946c813235a77a399b63 (patch)
tree796cee4b66659ff8a834ac850970786a4f2c1d18
parent57eb99fa424a259471402f5d8dbe4902bc0a0fb5 (diff)
parent62bdb7d9a236ebc8d3d56de8da09269a8a460c57 (diff)
downloadcowboy-7c1372df2077e5c4b81c946c813235a77a399b63.tar.gz
cowboy-7c1372df2077e5c4b81c946c813235a77a399b63.tar.bz2
cowboy-7c1372df2077e5c4b81c946c813235a77a399b63.zip
Merge branch 'master' of github.com:talentdeficit/cowboy
-rw-r--r--src/cowboy_http_rest.erl24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/cowboy_http_rest.erl b/src/cowboy_http_rest.erl
index ef6e92c..a5ae964 100644
--- a/src/cowboy_http_rest.erl
+++ b/src/cowboy_http_rest.erl
@@ -176,25 +176,39 @@ options(Req, State) ->
%%
%% An example of such return value would be:
%% {{<<"text">>, <<"html">>, []}, to_html}
+%%
+%% Note that it is also possible to return a binary content type that will
+%% then be parsed by Cowboy. However note that while this may make your
+%% 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) ->
case call(Req, State, content_types_provided) of
no_call ->
not_acceptable(Req, State);
- {[], Req2, HandlerState2} ->
- not_acceptable(Req2, State#state{handler_state=HandlerState2});
- {CTP, Req2, HandlerState2} ->
- State2 = State#state{handler_state=HandlerState2, content_types_p=CTP},
+ {[], Req2, HandlerState} ->
+ not_acceptable(Req2, State#state{handler_state=HandlerState});
+ {CTP, Req2, HandlerState} ->
+ CTP2 = [normalize_content_types_provided(P) || P <- CTP],
+ State2 = State#state{
+ handler_state=HandlerState, content_types_p=CTP2},
{Accept, Req3} = cowboy_http_req:parse_header('Accept', Req2),
case Accept of
undefined ->
languages_provided(Req3,
- State2#state{content_type_a=hd(CTP)});
+ State2#state{content_type_a=hd(CTP2)});
Accept ->
Accept2 = prioritize_accept(Accept),
choose_media_type(Req3, State2, Accept2)
end
end.
+normalize_content_types_provided({ContentType, Handler})
+ when is_binary(ContentType) ->
+ {cowboy_http:content_type(ContentType), Handler};
+normalize_content_types_provided(Provided) ->
+ Provided.
+
prioritize_accept(Accept) ->
lists:sort(
fun ({MediaTypeA, Quality, _AcceptParamsA},