Issue #2706 - minor cleanup

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-07-20 16:03:27 +02:00
parent d6b7f8d71f
commit cd0cf3d11f
2 changed files with 35 additions and 22 deletions

View File

@ -32,6 +32,7 @@ import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import javax.servlet.AsyncContext;
import javax.servlet.RequestDispatcher;
@ -464,6 +465,14 @@ public class ResourceService
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
protected void sendStatus(HttpServletResponse response, int status, Supplier<String> etag) throws IOException
{
response.setStatus(status);
if (_etags && etag!=null)
response.setHeader(HttpHeader.ETAG.asString(),etag.get());
response.flushBuffer();
}
/* ------------------------------------------------------------ */
/* Check modification date headers.
*/
@ -534,8 +543,7 @@ public class ResourceService
if (!match)
{
response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
response.flushBuffer();
sendStatus(response,HttpServletResponse.SC_PRECONDITION_FAILED,null);
return false;
}
}
@ -545,9 +553,7 @@ public class ResourceService
// Handle special case of exact match OR gzip exact match
if (CompressedContentFormat.tagEquals(etag, ifnm) && ifnm.indexOf(',')<0)
{
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.setHeader(HttpHeader.ETAG.asString(),ifnm);
response.flushBuffer();
sendStatus(response,HttpServletResponse.SC_NOT_MODIFIED,ifnm::toString);
return false;
}
@ -557,9 +563,7 @@ public class ResourceService
{
if (CompressedContentFormat.tagEquals(etag, tag))
{
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.setHeader(HttpHeader.ETAG.asString(),tag);
response.flushBuffer();
sendStatus(response,HttpServletResponse.SC_NOT_MODIFIED,tag::toString);
return false;
}
}
@ -576,20 +580,14 @@ public class ResourceService
String mdlm=content.getLastModifiedValue();
if (mdlm!=null && ifms.equals(mdlm))
{
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
if (_etags)
response.setHeader(HttpHeader.ETAG.asString(),content.getETagValue());
response.flushBuffer();
sendStatus(response,HttpServletResponse.SC_NOT_MODIFIED,content::getETagValue);
return false;
}
long ifmsl=request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
if (ifmsl!=-1 && content.getResource().lastModified()/1000 <= ifmsl/1000)
{
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
if (_etags)
response.setHeader(HttpHeader.ETAG.asString(),content.getETagValue());
response.flushBuffer();
sendStatus(response,HttpServletResponse.SC_NOT_MODIFIED,content::getETagValue);
return false;
}
}
@ -741,14 +739,12 @@ public class ResourceService
List<InclusiveByteRange> ranges =InclusiveByteRange.satisfiableRanges( reqRanges, content_length);
// if there are no satisfiable ranges, send 416 response
// TODO should we be doing a 416 with a body
if (ranges==null || ranges.size()==0)
{
putHeaders(response,content,0);
response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
response.setHeader(HttpHeader.CONTENT_RANGE.asString(),
InclusiveByteRange.to416HeaderRangeString(content_length));
content.getResource().writeTo(out,0,content_length);
sendStatus(response,HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE,null);
return true;
}

View File

@ -186,6 +186,24 @@ public class ResourceHandlerTest
assertThat(response.get(SERVER),containsString("Jetty"));
assertThat(response.getContent(),containsString("simple text"));
}
@Test
public void testIfModifiedSince() throws Exception
{
HttpTester.Response response = HttpTester.parseResponse(
_local.getResponse("GET /resource/simple.txt HTTP/1.0\r\n\r\n"));
assertThat(response.getStatus(),equalTo(200));
assertThat(response.get(LAST_MODIFIED),Matchers.notNullValue());
assertThat(response.getContent(),containsString("simple text"));
String last_modified = response.get(LAST_MODIFIED);
response = HttpTester.parseResponse(_local.getResponse(
"GET /resource/simple.txt HTTP/1.0\r\n" +
"If-Modified-Since: " + last_modified + "\r\n" +
"\r\n"));
assertThat(response.getStatus(),equalTo(304));
}
@Test
public void testBigFile() throws Exception
@ -267,7 +285,7 @@ public class ResourceHandlerTest
@Slow
public void testSlowBiggest() throws Exception
{
_connector.setIdleTimeout(10000);
_connector.setIdleTimeout(9000);
File dir = MavenTestingUtils.getTargetFile("test-classes/simple");
File biggest = new File(dir,"biggest.txt");
@ -292,7 +310,7 @@ public class ResourceHandlerTest
ByteBuffer buffer=null;
while(true)
{
Thread.sleep(100);
Thread.sleep(25);
int len=in.read(array);
if (len<0)
break;
@ -308,7 +326,6 @@ public class ResourceHandlerTest
}
@Test
public void testConditionalGetResponseCommitted() throws Exception
{