Skip to content

Commit

Permalink
Issue #4760 Response.setLocale should override previous
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Apr 8, 2020
1 parent 3981ec1 commit 7b5dacc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mapping of mime type to inferred or assumed charset
# inferred charsets are used for encoding/decoding and explicitly set in associated metadata
# assumed charsets are used for encoding/decoding, but are not set in associated metadata
# inferred charsets are used for encoding/decoding and explicitly set in Content-Type
# assumed charsets are used for encoding/decoding, but are not set in Content-Type
# In this file, assumed charsets are indicated with a leading '-'

text/html=utf-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private enum EncodingFrom
NOT_SET, INFERRED, SET_LOCALE, SET_CONTENT_TYPE, SET_CHARACTER_ENCODING
}

private static final EnumSet<EncodingFrom> __localeOverride = EnumSet.of(EncodingFrom.NOT_SET, EncodingFrom.INFERRED);
private static final EnumSet<EncodingFrom> __localeOverride = EnumSet.of(EncodingFrom.NOT_SET, EncodingFrom.INFERRED, EncodingFrom.SET_LOCALE);
private static final EnumSet<EncodingFrom> __explicitCharset = EnumSet.of(EncodingFrom.SET_LOCALE, EncodingFrom.SET_CHARACTER_ENCODING);

public Response(HttpChannel channel, HttpOutput out)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.AbstractEndPoint;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.server.handler.AbstractHandler;
Expand Down Expand Up @@ -473,6 +474,67 @@ public void testResponseCharacterEncoding() throws Exception
response.getWriter();
assertThat("iso-8859-1", Matchers.equalTo(response.getCharacterEncoding()));
}

@Test
public void testLocaleAndContentTypeEncoding() throws Exception
{
_server.stop();
MimeTypes.getInferredEncodings().put("text/html", "iso-8859-1");
ContextHandler handler = new ContextHandler();
handler.addLocaleEncoding("ja", "euc-jp");
handler.addLocaleEncoding("zh_CN", "gb18030");
_server.setHandler(handler);
handler.setHandler(new DumpHandler());
_server.start();

Response response = getResponse();
response.getHttpChannel().getRequest().setContext(handler.getServletContext());

response.setContentType("text/html");
assertEquals("iso-8859-1", response.getCharacterEncoding());

// setLocale should change character encoding based on
// locale-encoding-mapping-list
response.setLocale(Locale.JAPAN);
assertEquals("euc-jp", response.getCharacterEncoding());

// setLocale should change character encoding based on
// locale-encoding-mapping-list
response.setLocale(Locale.CHINA);
assertEquals("gb18030", response.getCharacterEncoding());

// setContentType here doesn't define character encoding
response.setContentType("text/html");
assertEquals("gb18030", response.getCharacterEncoding());

// setCharacterEncoding should still be able to change encoding
response.setCharacterEncoding("utf-8");
assertEquals("utf-8", response.getCharacterEncoding());

// setLocale should not override explicit character encoding request
response.setLocale(Locale.JAPAN);
assertEquals("utf-8", response.getCharacterEncoding());

// setContentType should still be able to change encoding
response.setContentType("text/html;charset=gb18030");
assertEquals("gb18030", response.getCharacterEncoding());

// setCharacterEncoding should still be able to change encoding
response.setCharacterEncoding("utf-8");
assertEquals("utf-8", response.getCharacterEncoding());

// getWriter should freeze the character encoding
PrintWriter pw = response.getWriter();
assertEquals("utf-8", response.getCharacterEncoding());

// setCharacterEncoding should no longer be able to change the encoding
response.setCharacterEncoding("iso-8859-1");
assertEquals("utf-8", response.getCharacterEncoding());

// setLocale should not override explicit character encoding request
response.setLocale(Locale.JAPAN);
assertEquals("utf-8", response.getCharacterEncoding());
}

@Test
public void testContentTypeCharacterEncoding() throws Exception
Expand Down

0 comments on commit 7b5dacc

Please sign in to comment.