324679 dont set content length if writer is gotten

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2268 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-09-10 06:03:45 +00:00
parent f919f78f1f
commit f815555349
2 changed files with 44 additions and 12 deletions

View File

@ -761,8 +761,21 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
// Get the output stream (or writer)
OutputStream out =null;
try{out = response.getOutputStream();}
catch(IllegalStateException e) {out = new WriterOutputStream(response.getWriter());}
boolean written;
try
{
out = response.getOutputStream();
// has a filter already written to the response?
written = out instanceof HttpOutput
? !((HttpOutput)out).isWritten()
: HttpConnection.getCurrentConnection().getGenerator().isContentWritten();
}
catch(IllegalStateException e)
{
out = new WriterOutputStream(response.getWriter());
written=true; // there may be data in writer buffer, so assume written
}
if ( reqRanges == null || !reqRanges.hasMoreElements() || content_length<0)
{
@ -773,10 +786,6 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
}
else
{
// has a filter already written to the response?
boolean written = out instanceof HttpOutput
? !((HttpOutput)out).isWritten()
: HttpConnection.getCurrentConnection().getGenerator().isContentWritten();
// See if a direct methods can be used?
if (content!=null && !written && out instanceof HttpOutput)

View File

@ -524,21 +524,27 @@ public class DefaultServletTest
defholder.setInitParameter("gzip","false");
defholder.setInitParameter("resourceBase",resBasePath);
ServletHolder jspholder = context.addServlet(NoJspServlet.class,"*.jsp");
String response = connector.getResponses("GET /context/data0.txt HTTP/1.1\r\nHost:localhost:8080\r\n\r\n");
assertResponseContains("Content-Length: 12",response);
assertResponseNotContains("Extra Info",response);
context.addFilter(AddingFilter.class,"/*",0);
context.addFilter(OutputFilter.class,"/*",0);
response = connector.getResponses("GET /context/data0.txt HTTP/1.1\r\nHost:localhost:8080\r\n\r\n");
System.err.println(response);
assertResponseContains("Content-Length: 24",response);
assertResponseContains("Content-Length: 2",response); // 20 something long
assertResponseContains("Extra Info",response);
assertResponseNotContains("Content-Length: 12",response);
context.getServletHandler().setFilterMappings(new FilterMapping[]{});
context.getServletHandler().setFilters(new FilterHolder[]{});
context.addFilter(WriterFilter.class,"/*",0);
response = connector.getResponses("GET /context/data0.txt HTTP/1.1\r\nHost:localhost:8080\r\n\r\n");
assertResponseContains("Content-Length: 2",response); // 20 something long
assertResponseContains("Extra Info",response);
assertResponseNotContains("Content-Length: 12",response);
}
public static class AddingFilter implements Filter
public static class OutputFilter implements Filter
{
public void init(FilterConfig filterConfig) throws ServletException
{
@ -554,6 +560,23 @@ public class DefaultServletTest
{
}
}
public static class WriterFilter implements Filter
{
public void init(FilterConfig filterConfig) throws ServletException
{
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
response.getWriter().println("Extra Info");
chain.doFilter(request,response);
}
public void destroy()
{
}
}
private void createFile(File file, String str) throws IOException
{