Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(s3): add conditional request headers (If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since) (#48)

Roberto Perez Alcolea66af54505359
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
index 9657f16148..25cb3ec6c0 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
@@ -364,6 +364,10 @@ public class S3Controller {
@HeaderParam("x-amz-object-attributes") String objectAttributesHeader,
@HeaderParam("x-amz-max-parts") Integer maxParts,
@HeaderParam("x-amz-part-number-marker") Integer partNumberMarker,
+ @HeaderParam("If-Match") String ifMatch,
+ @HeaderParam("If-None-Match") String ifNoneMatch,
+ @HeaderParam("If-Modified-Since") String ifModifiedSince,
+ @HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince,
@Context UriInfo uriInfo) {
try {
if (hasQueryParam(uriInfo, "tagging")) {
@@ -382,6 +386,14 @@ public class S3Controller {
return handleGetObjectAttributes(bucket, key, versionId,
objectAttributesHeader, maxParts, partNumberMarker);
}
+ if (hasPreconditions(ifMatch, ifNoneMatch, ifModifiedSince, ifUnmodifiedSince)) {
+ // Fetch metadata only to evaluate preconditions, avoiding loading the full object unnecessarily.
+ S3Object metadata = s3Service.headObject(bucket, key, versionId);
+ Response preconditionResponse = checkPreconditions(metadata, ifMatch, ifNoneMatch, ifModifiedSince, ifUnmodifiedSince);
+ if (preconditionResponse != null) {
+ return preconditionResponse;
+ }
+ }
S3Object obj = s3Service.getObject(bucket, key, versionId);
var resp = Response.ok(obj.getData())
.header("Content-Type", obj.getContentType())
@@ -402,9 +414,17 @@ public class S3Controller {
@Path("/{bucket}/{key:.+}")
public Response headObject(@PathParam("bucket") String bucket,
@PathParam("key") String key,
- @QueryParam("versionId") String versionId) {
+ @QueryParam("versionId") String versionId,
+ @HeaderParam("If-Match") String ifMatch,
+ @HeaderParam("If-None-Match") String ifNoneMatch,
+ @HeaderParam("If-Modified-Since") String ifModifiedSince,
+ @HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince) {
try {
S3Object obj = s3Service.headObject(bucket, key, versionId);
+ Response preconditionResponse = checkPreconditions(obj, ifMatch, ifNoneMatch, ifModifiedSince, ifUnmodifiedSince);
+ if (preconditionResponse != null) {
+ return preconditionResponse;
+ }
var resp = Response.ok()
.header("Content-Type", obj.getContentType())
.header("Content-Length", obj.getSize())
@@ -1090,6 +1110,77 @@ public class S3Controller {
return Response.status(e.getHttpStatus()).entity(xml).type(MediaType.APPLICATION_XML).build();
}
+ private Response checkPreconditions(S3Object obj, String ifMatch, String ifNoneMatch,
+ String ifModifiedSince, String ifUnmodifiedSince) {
+ String eTag = obj.getETag();
+ Instant lastModified = obj.getLastModified();
+
+ if (ifMatch != null && !eTagMatches(ifMatch, eTag)) {
+ return preconditionFailedResponse();
+ }
+
+ if (ifUnmodifiedSince != null && ifMatch == null) {
+ Instant since = parseHttpDate(ifUnmodifiedSince);
+ if (since != null && lastModified.isAfter(since)) {
+ return preconditionFailedResponse();
+ }
+ }
+
+ if (ifNoneMatch != null && eTagMatches(ifNoneMatch, eTag)) {
+ return notModifiedResponse(eTag, lastModified);
+ }
+
+ if (ifModifiedSince != null && ifNoneMatch == null) {
+ Instant since = parseHttpDate(ifModifiedSince);
+ if (since != null && !lastModified.isAfter(since)) {
+ return notModifiedResponse(eTag, lastModified);
+ }
+ }
+
+ return null;
+ }
+
+ private boolean hasPreconditions(String ifMatch, String ifNoneMatch,
+ String ifModifiedSince, String ifUnmodifiedSince) {
+ return ifMatch != null || ifNoneMatch != null || ifModifiedSince != null || ifUnmodifiedSince != null;
+ }
+
+ private Response notModifiedResponse(String eTag, Instant lastModified) {
+ return Response.notModified()
+ .header("ETag", eTag)
+ .header("Last-Modified", RFC_822.format(lastModified))
+ .build();
+ }
+
+ private Response preconditionFailedResponse() {
+ return xmlErrorResponse(new AwsException("PreconditionFailed",
+ "At least one of the pre-conditions you specified did not hold.", 412));
+ }
+
+ private boolean eTagMatches(String headerValue, String eTag) {
+ if ("*".equals(headerValue.trim())) {
+ return true;
+ }
+ for (String candidate : headerValue.split(",")) {
+ if (candidate.trim().equals(eTag)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private Instant parseHttpDate(String dateStr) {
+ try {
+ return RFC_822.parse(dateStr.trim(), Instant::from);
+ } catch (Exception e) {
+ try {
+ return Instant.parse(dateStr.trim());
+ } catch (Exception e2) {
+ return null;
+ }
+ }
+ }
+
private boolean hasQueryParam(UriInfo uriInfo, String param) {
if (uriInfo.getQueryParameters().containsKey(param)) return true;
String query = uriInfo.getRequestUri().getQuery();
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
index 9d3da6de8b..82a1c246b2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
@@ -210,7 +210,216 @@ class S3IntegrationTest {
}
@Test
- @Order(16)
+ @Order(50)
+ void getObjectIfNoneMatchReturns304() {
+ String eTag = given()
+ .when().head("/test-bucket/greeting.txt")
+ .then().statusCode(200)
+ .extract().header("ETag");
+
+ given()
+ .header("If-None-Match", eTag)
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(304)
+ .header("ETag", equalTo(eTag));
+ }
+
+ @Test
+ @Order(51)
+ void getObjectIfNoneMatchNonMatchingReturns200() {
+ given()
+ .header("If-None-Match", "\"wrong-etag\"")
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(200)
+ .body(equalTo("Hello World from S3!"));
+ }
+
+ @Test
+ @Order(52)
+ void getObjectIfMatchReturns200() {
+ String eTag = given()
+ .when().head("/test-bucket/greeting.txt")
+ .then().statusCode(200)
+ .extract().header("ETag");
+
+ given()
+ .header("If-Match", eTag)
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(200)
+ .body(equalTo("Hello World from S3!"));
+ }
+
+ @Test
+ @Order(53)
+ void getObjectIfMatchWrongEtagReturns412() {
+ given()
+ .header("If-Match", "\"wrong-etag\"")
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(412)
+ .body(containsString("PreconditionFailed"));
+ }
+
+ @Test
+ @Order(54)
+ void headObjectIfNoneMatchReturns304() {
+ String eTag = given()
+ .when().head("/test-bucket/greeting.txt")
+ .then().statusCode(200)
+ .extract().header("ETag");
+
+ given()
+ .header("If-None-Match", eTag)
+ .when()
+ .head("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(304);
+ }
+
+ @Test
+ @Order(55)
+ void headObjectIfMatchReturns200() {
+ String eTag = given()
+ .when().head("/test-bucket/greeting.txt")
+ .then().statusCode(200)
+ .extract().header("ETag");
+
+ given()
+ .header("If-Match", eTag)
+ .when()
+ .head("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ @Order(56)
+ void headObjectIfMatchWrongEtagReturns412() {
+ given()
+ .header("If-Match", "\"wrong-etag\"")
+ .when()
+ .head("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(412);
+ }
+
+ @Test
+ @Order(57)
+ void headObjectIfModifiedSinceReturns304() {
+ given()
+ .header("If-Modified-Since", "Sun, 24 Mar 2030 00:00:00 GMT")
+ .when()
+ .head("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(304);
+ }
+
+ @Test
+ @Order(58)
+ void headObjectIfUnmodifiedSinceReturns412() {
+ given()
+ .header("If-Unmodified-Since", "Tue, 24 Mar 2020 00:00:00 GMT")
+ .when()
+ .head("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(412);
+ }
+
+ @Test
+ @Order(61)
+ void getObjectIfModifiedSinceReturns304() {
+ given()
+ .header("If-Modified-Since", "Sun, 24 Mar 2030 00:00:00 GMT")
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(304);
+ }
+
+ @Test
+ @Order(62)
+ void getObjectIfUnmodifiedSinceReturns412() {
+ given()
+ .header("If-Unmodified-Since", "Tue, 24 Mar 2020 00:00:00 GMT")
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(412)
+ .body(containsString("PreconditionFailed"));
+ }
+
+ @Test
+ @Order(63)
+ void getObjectIfMatchWildcardReturns200() {
+ given()
+ .header("If-Match", "*")
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(200)
+ .body(equalTo("Hello World from S3!"));
+ }
+
+ @Test
+ @Order(64)
+ void getObjectIfNoneMatchCommaListReturns304() {
+ String eTag = given()
+ .when().head("/test-bucket/greeting.txt")
+ .then().statusCode(200)
+ .extract().header("ETag");
+
+ given()
+ .header("If-None-Match", "\"wrong-etag\", " + eTag + ", \"other\"")
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(304);
+ }
+
+ @Test
+ @Order(65)
+ void ifNoneMatchTakesPrecedenceOverIfModifiedSince() {
+ String eTag = given()
+ .when().head("/test-bucket/greeting.txt")
+ .then().statusCode(200)
+ .extract().header("ETag");
+
+ given()
+ .header("If-None-Match", eTag)
+ .header("If-Modified-Since", "Tue, 24 Mar 2020 00:00:00 GMT")
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(304);
+ }
+
+ @Test
+ @Order(66)
+ void notModifiedResponseIncludesLastModified() {
+ String eTag = given()
+ .when().head("/test-bucket/greeting.txt")
+ .then().statusCode(200)
+ .extract().header("ETag");
+
+ given()
+ .header("If-None-Match", eTag)
+ .when()
+ .get("/test-bucket/greeting.txt")
+ .then()
+ .statusCode(304)
+ .header("ETag", equalTo(eTag))
+ .header("Last-Modified", notNullValue());
+ }
+
+ @Test
+ @Order(70)
void cleanupAndDeleteBucket() {
// Delete all objects
given().delete("/test-bucket/greeting.txt");