Skip to content

Commit

Permalink
Issue #6520 - Fixing ErrorHandler output of text/html
Browse files Browse the repository at this point in the history
+ Updating tests to ensure that output is xml verified
+ Updating output to use `<hr>` element properly.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Jul 16, 2021
1 parent c6d8bfc commit 22c4855
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -397,7 +397,7 @@ protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int
writeErrorPageStacks(request, writer);

Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
.writePoweredBy(writer, "<hr>", "<hr/>\n");
.writePoweredBy(writer, "<hr/>", "<hr/>\n");
}

protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri)
Expand Down
Expand Up @@ -18,7 +18,9 @@

package org.eclipse.jetty.server;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -27,6 +29,9 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpHeader;
Expand All @@ -43,6 +48,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.w3c.dom.Document;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
Expand Down Expand Up @@ -567,7 +573,7 @@ public void testComplexCauseMessageAcceptUtf8Header(String path) throws Exceptio
}
}

private String assertContent(HttpTester.Response response)
private String assertContent(HttpTester.Response response) throws Exception
{
String contentType = response.get(HttpHeader.CONTENT_TYPE);
String content = response.getContent();
Expand All @@ -578,6 +584,17 @@ private String assertContent(HttpTester.Response response)
assertThat(content, not(containsString("<glossary>")));
assertThat(content, not(containsString("<!DOCTYPE>")));
assertThat(content, not(containsString("&euro;")));

// we expect that our generated output conforms to text/xhtml is well formed
DocumentBuilderFactory xmlDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
xmlDocumentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder db = xmlDocumentBuilderFactory.newDocumentBuilder();
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)))
{
// We consider this content to be XML well formed if these 2 lines do not throw an Exception
Document doc = db.parse(inputStream);
doc.getDocumentElement().normalize();
}
}
else if (contentType.contains("text/json"))
{
Expand Down

0 comments on commit 22c4855

Please sign in to comment.