mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
adds webmachine format content type handling
This commit is contained in:
parent
168405830d
commit
62bdb7d9a2
1 changed files with 19 additions and 5 deletions
|
@ -176,25 +176,39 @@ options(Req, State) ->
|
||||||
%%
|
%%
|
||||||
%% An example of such return value would be:
|
%% An example of such return value would be:
|
||||||
%% {{<<"text">>, <<"html">>, []}, to_html}
|
%% {{<<"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) ->
|
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);
|
||||||
{[], Req2, HandlerState2} ->
|
{[], Req2, HandlerState} ->
|
||||||
not_acceptable(Req2, State#state{handler_state=HandlerState2});
|
not_acceptable(Req2, State#state{handler_state=HandlerState});
|
||||||
{CTP, Req2, HandlerState2} ->
|
{CTP, Req2, HandlerState} ->
|
||||||
State2 = State#state{handler_state=HandlerState2, content_types_p=CTP},
|
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),
|
{Accept, Req3} = cowboy_http_req:parse_header('Accept', Req2),
|
||||||
case Accept of
|
case Accept of
|
||||||
undefined ->
|
undefined ->
|
||||||
languages_provided(Req3,
|
languages_provided(Req3,
|
||||||
State2#state{content_type_a=hd(CTP)});
|
State2#state{content_type_a=hd(CTP2)});
|
||||||
Accept ->
|
Accept ->
|
||||||
Accept2 = prioritize_accept(Accept),
|
Accept2 = prioritize_accept(Accept),
|
||||||
choose_media_type(Req3, State2, Accept2)
|
choose_media_type(Req3, State2, Accept2)
|
||||||
end
|
end
|
||||||
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) ->
|
prioritize_accept(Accept) ->
|
||||||
lists:sort(
|
lists:sort(
|
||||||
fun ({MediaTypeA, Quality, _AcceptParamsA},
|
fun ({MediaTypeA, Quality, _AcceptParamsA},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue