Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4760 Response.setLocale should override previous #4761

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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