Merge remote-tracking branch 'origin/jetty-11.0.x' into jetty-12.0.x

# Conflicts:
#	jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceService.java
#	jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java
This commit is contained in:
Greg Wilkins 2022-11-17 13:01:45 +11:00
commit f7d8ea67f6
3 changed files with 71 additions and 23 deletions

View File

@ -17,7 +17,6 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.util.ArrayList;
import java.util.Collection;
@ -326,7 +325,7 @@ public class ResourceService
String ifm = null;
String ifnm = null;
String ifms = null;
long ifums = -1;
String ifums = null;
// Find multiple fields by iteration as an optimization
for (HttpField field : request.getHeaders())
@ -338,7 +337,7 @@ public class ResourceService
case IF_MATCH -> ifm = field.getValue();
case IF_NONE_MATCH -> ifnm = field.getValue();
case IF_MODIFIED_SINCE -> ifms = field.getValue();
case IF_UNMODIFIED_SINCE -> ifums = DateParser.parseDate(field.getValue());
case IF_UNMODIFIED_SINCE -> ifums = field.getValue();
default ->
{
}
@ -348,7 +347,6 @@ public class ResourceService
if (_etags)
{
String etag = content.getETagValue();
if (etag != null)
{
@ -381,7 +379,7 @@ public class ResourceService
}
// Handle if modified since
if (ifms != null)
if (ifms != null && ifnm == null)
{
//Get jetty's Response impl
String mdlm = content.getLastModifiedValue();
@ -391,21 +389,33 @@ public class ResourceService
return true;
}
long ifmsl = request.getHeaders().getDateField(HttpHeader.IF_MODIFIED_SINCE);
if (ifmsl != -1 && Files.getLastModifiedTime(content.getResource().getPath()).toMillis() / 1000 <= ifmsl / 1000)
long ifmsl = DateParser.parseDate(ifms);
if (ifmsl != -1)
{
long lm = content.getResource().lastModified().toEpochMilli();
if (lm != -1 && lm / 1000 <= ifmsl / 1000)
{
writeHttpError(request, response, callback, HttpStatus.NOT_MODIFIED_304);
return true;
}
}
}
// Parse the if[un]modified dates and compare to resource
if (ifums != -1 && Files.getLastModifiedTime(content.getResource().getPath()).toMillis() / 1000 > ifums / 1000)
if (ifums != null && ifm == null)
{
long ifumsl = DateParser.parseDate(ifums);
if (ifumsl != -1)
{
long lm = content.getResource().lastModified().toEpochMilli();
if (lm != -1 && lm / 1000 > ifumsl / 1000)
{
writeHttpError(request, response, callback, HttpStatus.PRECONDITION_FAILED_412);
return true;
}
}
}
}
catch (IllegalArgumentException iae)
{
if (!response.isCommitted())

View File

@ -2539,6 +2539,20 @@ public class ResourceHandlerTest
assertThat(response.getStatus(), equalTo(304));
assertThat(response.getContent(), is(""));
response = HttpTester.parseResponse(
_local.getResponse("""
GET /context/simple.txt HTTP/1.1\r
Host: local\r
Connection: close\r
If-Modified-Since: %s\r
If-None-Match: XYZ \r
\r
""".formatted(lastModified)));
assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
assertThat(response.get(LAST_MODIFIED), notNullValue());
assertThat(response.getContent(), containsString("simple text"));
}
@Test
@ -2572,6 +2586,18 @@ public class ResourceHandlerTest
\r
""".formatted(lastModified)));
assertThat(response.getStatus(), is(HttpStatus.PRECONDITION_FAILED_412));
response = HttpTester.parseResponse(_local.getResponse("""
GET /context/test-unmodified-since-file.txt HTTP/1.1\r
Host: local\r
Connection: close\r
If-Unmodified-Since: %s \r
If-Match: XYZ\r
\r
""".formatted(lastModified)));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), equalTo("some content\nsome more content\n"));
assertThat(response.get(LAST_MODIFIED), notNullValue());
}
@Test

View File

@ -517,7 +517,7 @@ public class ResourceService
String ifm = null;
String ifnm = null;
String ifms = null;
long ifums = -1;
String ifums = null;
if (request instanceof Request)
{
@ -538,7 +538,7 @@ public class ResourceService
ifms = field.getValue();
break;
case IF_UNMODIFIED_SINCE:
ifums = DateParser.parseDate(field.getValue());
ifums = field.getValue();
break;
default:
}
@ -550,7 +550,7 @@ public class ResourceService
ifm = request.getHeader(HttpHeader.IF_MATCH.asString());
ifnm = request.getHeader(HttpHeader.IF_NONE_MATCH.asString());
ifms = request.getHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
ifums = request.getDateHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString());
ifums = request.getHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString());
}
if (_etags)
@ -605,7 +605,7 @@ public class ResourceService
}
// Handle if modified since
if (ifms != null)
if (ifms != null && ifnm == null)
{
//Get jetty's Response impl
String mdlm = content.getLastModifiedValue();
@ -615,21 +615,33 @@ public class ResourceService
return false;
}
long ifmsl = request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
if (ifmsl != -1 && content.getResource().lastModified().toEpochMilli() <= ifmsl)
long ifmsl = DateParser.parseDate(ifms);
if (ifmsl != -1)
{
long lm = content.getResource().lastModified().toEpochMilli();
if (lm != -1 && lm / 1000 <= ifmsl / 1000)
{
sendStatus(response, HttpServletResponse.SC_NOT_MODIFIED, content::getETagValue);
return false;
}
}
}
// Parse the if[un]modified dates and compare to resource
if (ifums != -1 && content.getResource().lastModified().toEpochMilli() > ifums)
if (ifums != null && ifm == null)
{
long ifumsl = DateParser.parseDate(ifums);
if (ifumsl != -1)
{
long lm = content.getResource().lastModified().toEpochMilli();
if (lm != -1 && lm / 1000 > ifumsl / 1000)
{
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return false;
}
}
}
}
catch (IllegalArgumentException iae)
{
if (!response.isCommitted())