Skip to content

Commit

Permalink
Fix #6869 HTML Error page charset
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 20, 2021
1 parent 4de5fdf commit 87f93ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Expand Up @@ -64,6 +64,7 @@ public class ErrorHandler extends AbstractHandler
private static final Logger LOG = Log.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 @@ -314,6 +315,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 @@ -374,7 +376,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 @@ -160,6 +160,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 @@ -217,7 +218,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 @@ -235,7 +236,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 @@ -256,6 +257,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 @@ -277,6 +279,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 @@ -298,6 +301,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 @@ -319,6 +323,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 @@ -337,6 +342,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 @@ -356,6 +362,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 @@ -375,6 +382,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 @@ -396,6 +404,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 @@ -432,6 +441,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 @@ -451,6 +461,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 @@ -505,6 +516,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 @@ -528,6 +540,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 @@ -561,6 +574,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 87f93ac

Please sign in to comment.