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 <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2016-03-02 17:10:50 -07:00
parent 8f8fcd1b1a
commit a7be6bc8ea
1 changed files with 16 additions and 10 deletions

View File

@ -110,17 +110,23 @@ public class ErrorHandler extends AbstractHandler
} }
baseRequest.setHandled(true); 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("*/*"))
{
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString()); response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
if (_cacheControl!=null) if (_cacheControl != null)
response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl); response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl);
ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096);
String reason=(response instanceof Response)?((Response)response).getReason():null; String reason = (response instanceof Response) ? ((Response) response).getReason() : null;
handleErrorPage(request, writer, response.getStatus(), reason); handleErrorPage(request, writer, response.getStatus(), reason);
writer.flush(); writer.flush();
response.setContentLength(writer.size()); response.setContentLength(writer.size());
writer.writeTo(response.getOutputStream()); writer.writeTo(response.getOutputStream());
writer.destroy(); writer.destroy();
} }
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)