aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_rest.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-11-02 13:54:19 +0100
committerLoïc Hoguin <[email protected]>2018-11-02 13:54:19 +0100
commite0b036fe68579b3ffb69b450acf2d17bb7162cf3 (patch)
tree4be263290f0ae3bef6b9bd940f6624cf035607d3 /src/cowboy_rest.erl
parent399b6a16b4a571e293437dcc8f85808f83b32ff6 (diff)
downloadcowboy-e0b036fe68579b3ffb69b450acf2d17bb7162cf3.tar.gz
cowboy-e0b036fe68579b3ffb69b450acf2d17bb7162cf3.tar.bz2
cowboy-e0b036fe68579b3ffb69b450acf2d17bb7162cf3.zip
Add tests for charsets_provided
Fix cases where the q-value is 0 and where a wildcard was sent in the accept-charset header. Also don't send a charset in the content-type of the response if the media type is not text. Thanks to Philip Witty for help figuring this out.
Diffstat (limited to 'src/cowboy_rest.erl')
-rw-r--r--src/cowboy_rest.erl13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl
index 63f3d99..24c0fb0 100644
--- a/src/cowboy_rest.erl
+++ b/src/cowboy_rest.erl
@@ -680,11 +680,16 @@ prioritize_charsets(AcceptCharsets) ->
choose_charset(Req, State, []) ->
not_acceptable(Req, State);
+%% A q-value of 0 means not acceptable.
+choose_charset(Req, State, [{_, 0}|Tail]) ->
+ choose_charset(Req, State, Tail);
choose_charset(Req, State=#state{charsets_p=CP}, [Charset|Tail]) ->
match_charset(Req, State, Tail, CP, Charset).
match_charset(Req, State, Accept, [], _Charset) ->
choose_charset(Req, State, Accept);
+match_charset(Req, State, _Accept, [Provided|_], {<<"*">>, _}) ->
+ set_content_type(Req, State#state{charset_a=Provided});
match_charset(Req, State, _Accept, [Provided|_], {Provided, _}) ->
set_content_type(Req, State#state{charset_a=Provided});
match_charset(Req, State, Accept, [_|Tail], Charset) ->
@@ -695,9 +700,11 @@ set_content_type(Req, State=#state{
charset_a=Charset}) ->
ParamsBin = set_content_type_build_params(Params, []),
ContentType = [Type, <<"/">>, SubType, ParamsBin],
- ContentType2 = case Charset of
- undefined -> ContentType;
- Charset -> [ContentType, <<"; charset=">>, Charset]
+ ContentType2 = case {Type, Charset} of
+ {<<"text">>, Charset} when Charset =/= undefined ->
+ [ContentType, <<"; charset=">>, Charset];
+ _ ->
+ ContentType
end,
Req2 = cowboy_req:set_resp_header(<<"content-type">>, ContentType2, Req),
encodings_provided(Req2#{charset => Charset}, State).