Skip to content

Commit

Permalink
Issue #5214 - Use known content_length when in HEAD mode
Browse files Browse the repository at this point in the history
+ Adding DefaultServlet.doHead() to avoid servlet wrapping
+ Making ResourceService HEAD aware

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Aug 31, 2020
1 parent eba360f commit dcb06d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,16 @@ protected boolean sendData(HttpServletRequest request,
// write without headers
content.getResource().writeTo(out, 0, content_length);
}
// we are working with a HEAD request
else if (isHead(request) && content_length > 0)
{
putHeaders(response, content, content_length);
}
// else if we can't do a bypass write because of wrapping
else if (written || !(out instanceof HttpOutput))
{
// write normally
putHeaders(response, content, content_length);
putHeaders(response, content, written ? -1 : 0);
ByteBuffer buffer = content.getIndirectBuffer();
if (buffer != null)
BufferUtil.writeTo(buffer, out);
Expand Down Expand Up @@ -846,6 +851,11 @@ protected void putHeaders(HttpServletResponse response, HttpContent content, lon
}
}

private boolean isHead(HttpServletRequest request)
{
return "HEAD".equalsIgnoreCase(request.getMethod());
}

public interface WelcomeFactory
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,20 +462,28 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
doGet(request, response);
}

@Override
protected void doHead(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doTrace(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doTrace(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
protected void doOptions(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
resp.setHeader("Allow", "GET,HEAD,POST,OPTIONS");
response.setHeader("Allow", "GET,HEAD,POST,OPTIONS");
}

/*
Expand Down

0 comments on commit dcb06d3

Please sign in to comment.