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

Fix #6869 HTML Error page charset (#6873) #6878

Merged
merged 1 commit into from Sep 21, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -59,6 +59,7 @@ public class ErrorHandler extends AbstractHandler
private static final Logger LOG = LoggerFactory.getLogger(ErrorHandler.class);
public static final String ERROR_PAGE = "org.eclipse.jetty.server.error_page";
public static final String ERROR_CONTEXT = "org.eclipse.jetty.server.error_context";
public static final String ERROR_CHARSET = "org.eclipse.jetty.server.error_charset";

boolean _showServlet = true;
boolean _showStacks = true;
Expand Down Expand Up @@ -302,6 +303,7 @@ protected void generateAcceptableResponse(Request baseRequest, HttpServletReques
case TEXT_HTML:
response.setContentType(MimeTypes.Type.TEXT_HTML.asString());
response.setCharacterEncoding(charset.name());
request.setAttribute(ERROR_CHARSET, charset);
handleErrorPage(request, writer, code, message);
break;
case TEXT_JSON:
Expand Down Expand Up @@ -363,7 +365,13 @@ protected void writeErrorPage(HttpServletRequest request, Writer writer, int cod
protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
throws IOException
{
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n");
Charset charset = (Charset)request.getAttribute(ERROR_CHARSET);
if (charset != null)
{
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=");
writer.write(charset.name());
writer.write("\"/>\n");
}
writer.write("<title>Error ");
// TODO this code is duplicated in writeErrorPageMessage
String status = Integer.toString(code);
Expand Down
Expand Up @@ -154,6 +154,7 @@ public void test404NoAccept() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));

assertContent(response);
}
Expand Down Expand Up @@ -211,7 +212,7 @@ public void test404AllAccept() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));

assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));
assertContent(response);
}

Expand All @@ -229,7 +230,7 @@ public void test404HtmlAccept() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));

assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));
assertContent(response);
}

Expand All @@ -250,6 +251,7 @@ public void test404PostHttp10() throws Exception
assertThat(response.getStatus(), is(404));
assertThat(response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat(response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));
assertThat(response.get(HttpHeader.CONNECTION), is("keep-alive"));
assertContent(response);
}
Expand All @@ -271,6 +273,7 @@ public void test404PostHttp11() throws Exception
assertThat(response.getStatus(), is(404));
assertThat(response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat(response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));
assertThat(response.getField(HttpHeader.CONNECTION), nullValue());
assertContent(response);
}
Expand All @@ -292,6 +295,7 @@ public void test404PostCantConsumeHttp10() throws Exception
assertThat(response.getStatus(), is(404));
assertThat(response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat(response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));
assertThat(response.getField(HttpHeader.CONNECTION), nullValue());
assertContent(response);
}
Expand All @@ -313,6 +317,7 @@ public void test404PostCantConsumeHttp11() throws Exception
assertThat(response.getStatus(), is(404));
assertThat(response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat(response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));
assertThat(response.getField(HttpHeader.CONNECTION).getValue(), is("close"));
assertContent(response);
}
Expand All @@ -331,6 +336,7 @@ public void testMoreSpecificAccept() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));

assertContent(response);
}
Expand All @@ -350,6 +356,7 @@ public void test404HtmlAcceptAnyCharset() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=UTF-8"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=UTF-8\""));

assertContent(response);
}
Expand All @@ -369,6 +376,7 @@ public void test404HtmlAcceptUtf8Charset() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=UTF-8"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=UTF-8\""));

assertContent(response);
}
Expand All @@ -390,6 +398,7 @@ public void test404HtmlAcceptNotUtf8Charset() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));

assertContent(response);
}
Expand Down Expand Up @@ -426,6 +435,7 @@ public void test404HtmlAcceptUnknownUtf8Charset() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=UTF-8"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=UTF-8\""));

assertContent(response);
}
Expand All @@ -445,6 +455,7 @@ public void test404PreferHtml() throws Exception
assertThat("Response status code", response.getStatus(), is(404));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=UTF-8"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=UTF-8\""));

assertContent(response);
}
Expand Down Expand Up @@ -499,6 +510,7 @@ public void testBadMessage() throws Exception
assertThat("Response status code", response.getStatus(), is(444));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));

assertContent(response);
}
Expand All @@ -522,6 +534,7 @@ public void testComplexCauseMessageNoAcceptHeader(String path) throws Exception
assertThat("Response status code", response.getStatus(), is(500));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=ISO-8859-1"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=ISO-8859-1\""));

String content = assertContent(response);

Expand Down Expand Up @@ -555,6 +568,7 @@ public void testComplexCauseMessageAcceptUtf8Header(String path) throws Exceptio
assertThat("Response status code", response.getStatus(), is(500));
assertThat("Response Content-Length", response.getField(HttpHeader.CONTENT_LENGTH).getIntValue(), greaterThan(0));
assertThat("Response Content-Type", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html;charset=UTF-8"));
assertThat(response.getContent(), containsString("content=\"text/html;charset=UTF-8\""));

String content = assertContent(response);

Expand Down