Skip to content

Commit

Permalink
Fix #6869 HTML Error page charset (#6873) (#6878)
Browse files Browse the repository at this point in the history
Fix #6869 HTML Error page charset passed as request attribute

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Sep 21, 2021
1 parent d3bfb61 commit 0dc5371
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit 0dc5371

Please sign in to comment.