From a7be6bc8ea232e965691972dc45468be2785393d Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 2 Mar 2016 17:10:50 -0700 Subject: [PATCH 1/2] Issue #124 - Don't produce text/html if the request doesn't accept it + If request has no 'Accept' header, produce text/html as before. + If request has 'Accept' header, then test for 'text/html' or '*/*' entries before producing html output. Signed-off-by: Joakim Erdfelt --- .../jetty/server/handler/ErrorHandler.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java index ba3dc7bb056..415de77a8f8 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java @@ -110,16 +110,22 @@ public class ErrorHandler extends AbstractHandler } baseRequest.setHandled(true); - response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString()); - if (_cacheControl!=null) - response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl); - ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); - String reason=(response instanceof Response)?((Response)response).getReason():null; - handleErrorPage(request, writer, response.getStatus(), reason); - writer.flush(); - response.setContentLength(writer.size()); - writer.writeTo(response.getOutputStream()); - writer.destroy(); + + // Issue #124 - Don't produce text/html if the request doesn't accept it + String reqAccept = request.getHeader("Accept"); + if (reqAccept == null || reqAccept.contains("text/html") || reqAccept.contains("*/*")) + { + response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString()); + if (_cacheControl != null) + response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl); + ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096); + String reason = (response instanceof Response) ? ((Response) response).getReason() : null; + handleErrorPage(request, writer, response.getStatus(), reason); + writer.flush(); + response.setContentLength(writer.size()); + writer.writeTo(response.getOutputStream()); + writer.destroy(); + } } /* ------------------------------------------------------------ */ From 0f0eb569e9da006867398538a6a68a55d65ee686 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 4 Mar 2016 11:28:34 -0700 Subject: [PATCH 2/2] Issue #124 - Don't produce text/html if the request doesn't accept it + Using cleaner HttpFields access to 'Accept' header Signed-off-by: Joakim Erdfelt --- .../java/org/eclipse/jetty/server/handler/ErrorHandler.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java index 415de77a8f8..73cd498ac05 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java @@ -29,6 +29,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; @@ -112,8 +113,8 @@ public class ErrorHandler extends AbstractHandler baseRequest.setHandled(true); // Issue #124 - Don't produce text/html if the request doesn't accept it - String reqAccept = request.getHeader("Accept"); - if (reqAccept == null || reqAccept.contains("text/html") || reqAccept.contains("*/*")) + HttpField accept = baseRequest.getHttpFields().getField(HttpHeader.ACCEPT); + if (accept == null || accept.contains("text/html") || accept.contains("*/*")) { response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString()); if (_cacheControl != null)