Permalink
Browse files

Fix checks on the response content type when a charset is present

When we test if a response is compressible or not, or when 'Expires' and
'Cache-Control' headers are set, the charset must be removed from the
response content-type to only keep the response mime-type.
  • Loading branch information...
1 parent 89f3ea2 commit f5f62b608839339e356b60c149853240fdad4d33 @capflam capflam committed Nov 29, 2012
Showing with 31 additions and 14 deletions.
  1. +11 −7 src/yaws.erl
  2. +9 −7 src/yaws_server.erl
  3. +11 −0 test/t7/app_test.erl
View
@@ -931,7 +931,6 @@ outh_set_non_cacheable(_Version) ->
ok.
outh_set_content_type(Mime) ->
- put(outh_mime_type, Mime),
put(outh, (get(outh))#outh{content_type = make_content_type_header(Mime)}),
ok.
@@ -970,14 +969,18 @@ outh_set_static_headers(Req, UT, Headers, Range) ->
all ->
case UT#urltype.deflate of
DB when is_binary(DB) -> % cached
- case accepts_gzip(Headers, UT#urltype.mime) of
+ %% Remove charset
+ [Mime|_] = yaws:split_sep(UT#urltype.mime, $;),
+ case accepts_gzip(Headers, Mime) of
true -> {true, size(DB)};
false -> {false, FIL}
end;
undefined ->
{false, FIL};
dynamic ->
- case accepts_gzip(Headers, UT#urltype.mime) of
+ %% Remove charset
+ [Mime|_] = yaws:split_sep(UT#urltype.mime, $;),
+ case accepts_gzip(Headers, Mime) of
true -> {true, undefined};
false -> {false, FIL}
end
@@ -1196,11 +1199,12 @@ make_last_modified_header(FI) ->
["Last-Modified: ", local_time_as_gmt_string(Then), "\r\n"].
-make_expires_header(MimeType, FI) ->
+make_expires_header(MimeType0, FI) ->
SC = get(sc),
- case lists:keyfind(MimeType, 1, SC#sconf.expires) of
- {MimeType, Type, TTL} -> make_expires_header(Type, TTL, FI);
- false -> {undefined, undefined}
+ [MimeType1|_] = yaws:split_sep(MimeType0, $;), %% Remove charset
+ case lists:keyfind(MimeType1, 1, SC#sconf.expires) of
+ {MimeType1, Type, TTL} -> make_expires_header(Type, TTL, FI);
+ false -> {undefined, undefined}
end.
View
@@ -3645,13 +3645,14 @@ decide_deflate(true, SC, Arg, Data, decide, Mode) ->
false;
true ->
- Mime = yaws:outh_get_content_type(),
- ?Debug("Check compression support: Mime-Type=~p~n", [Mime]),
- case compressible_mime_type(Mime, DOpts) of
+ Mime0 = yaws:outh_get_content_type(),
+ [Mime1|_] = yaws:split_sep(Mime0, $;), %% Remove charset
+ ?Debug("Check compression support: Mime-Type=~p~n", [Mime1]),
+ case compressible_mime_type(Mime1, DOpts) of
true ->
case (Arg =:= undefined
orelse
- yaws:accepts_gzip(Arg#arg.headers, Mime)) of
+ yaws:accepts_gzip(Arg#arg.headers, Mime1)) of
true when Mode =:= final ->
?Debug("Compress data~n", []),
yaws:outh_set_content_encoding(deflate),
@@ -3668,7 +3669,7 @@ decide_deflate(true, SC, Arg, Data, decide, Mode) ->
false
end;
false ->
- ?Debug("~p is not compressible~n", [Mime]),
+ ?Debug("~p is not compressible~n", [Mime1]),
yaws:outh_set_content_encoding(identity),
false
end
@@ -4753,8 +4754,9 @@ parse_user_path(DR, [H|T], User) ->
parse_user_path(DR, T, [H|User]).
-deflate_q(true, SC, regular, Mime) ->
- case compressible_mime_type(Mime, SC#sconf.deflate_options) of
+deflate_q(true, SC, regular, Mime0) ->
+ [Mime1|_] = yaws:split_sep(Mime0, $;), %% Remove charset
+ case compressible_mime_type(Mime1, SC#sconf.deflate_options) of
true -> dynamic;
false -> undefined
end;
View
@@ -17,6 +17,7 @@ start() ->
test_default_type(),
test_yaws_type(),
test_erlang_type(),
+ test_gzip_with_charset(),
ibrowse:stop().
@@ -116,3 +117,13 @@ test_erlang_type() ->
?line "text/html" = proplists:get_value("Content-Type", Hdrs1),
?line "text/x-erlang; charset=UTF-8" = proplists:get_value("Content-Type", Hdrs2),
ok.
+
+
+test_gzip_with_charset() ->
+ io:format("test_gzip_with_charset\n", []),
+ Uri = "http://localhost:8001/index.yaws",
+ ?line {ok, "200", Hdrs, _} =
+ ibrowse:send_req(Uri, [{"Accept-Encoding", "gzip, deflate"}], get),
+ ?line "text/xhtml; charset=ISO-8859-1" = proplists:get_value("Content-Type", Hdrs),
+ ?line "gzip" = proplists:get_value("Content-Encoding", Hdrs),
+ ok.

0 comments on commit f5f62b6

Please sign in to comment.