0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 20:50:24 +00:00

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.
This commit is contained in:
Loïc Hoguin 2018-11-02 13:54:19 +01:00
parent 399b6a16b4
commit e0b036fe68
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
5 changed files with 203 additions and 3 deletions

View file

@ -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).