Skip to content

Commit

Permalink
Add precondition support to DELETE requests
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Dec 13, 2024
1 parent 1aa97da commit 4d7c896
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
5 changes: 4 additions & 1 deletion java/org/apache/catalina/servlets/DefaultServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,10 @@ protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws

WebResource resource = resources.getResource(path);

if (!checkIfHeaders(req, resp, resource)) {
return;
}

if (resource.exists()) {
if (resource.delete()) {
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
Expand All @@ -735,7 +739,6 @@ protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws
} else {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ public static Collection<Object[]> parameters() {
null, null, null, Boolean.FALSE, SC_400 });
parameterSets.add(new Object[] { useStrongEtag, Task.PUT_NEW_TXT, EtagPrecondition.INVALID_ALL_PLUS_OTHER,
null, null, null, null, null, Boolean.FALSE, SC_400 });

// DELETE TESTS
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_EXIST_TXT, null, null, null, null, null, null,
Boolean.FALSE, SC_204 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_EXIST_TXT, EtagPrecondition.ALL, null, null, null, null, null,
Boolean.FALSE, SC_204 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_EXIST_TXT, EtagPrecondition.EXACTLY, null, null, null, null, null,
Boolean.FALSE, useStrongEtag.booleanValue() ? SC_204 : SC_412 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_EXIST_TXT, EtagPrecondition.IN, null, null, null, null, null,
Boolean.FALSE, useStrongEtag.booleanValue() ? SC_204 : SC_412 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_EXIST_TXT, EtagPrecondition.NOT_IN, null, null, null, null, null,
Boolean.FALSE, SC_412 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_EXIST_TXT, EtagPrecondition.INVALID, null, null, null, null, null,
Boolean.FALSE, SC_400 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_EXIST_TXT, EtagPrecondition.INVALID_ALL_PLUS_OTHER, null, null, null, null, null,
Boolean.FALSE, SC_400 });

parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_NOT_EXIST_TXT, null, null, null, null, null, null,
Boolean.FALSE, SC_404 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_NOT_EXIST_TXT, EtagPrecondition.ALL, null, null, null, null, null,
Boolean.FALSE, SC_412 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_NOT_EXIST_TXT, EtagPrecondition.IN, null, null, null, null, null,
Boolean.FALSE, SC_412 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_NOT_EXIST_TXT, EtagPrecondition.NOT_IN, null, null, null, null, null,
Boolean.FALSE, SC_412 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_NOT_EXIST_TXT, EtagPrecondition.INVALID, null, null, null, null, null,
Boolean.FALSE, SC_400 });
parameterSets.add(new Object[] { useStrongEtag, Task.DELETE_NOT_EXIST_TXT, EtagPrecondition.INVALID_ALL_PLUS_OTHER, null, null, null, null, null,
Boolean.FALSE, SC_400 });
}

return parameterSets;
Expand All @@ -269,6 +298,7 @@ public static Collection<Object[]> parameters() {
private static Integer SC_206 = Integer.valueOf(HttpServletResponse.SC_PARTIAL_CONTENT);
private static Integer SC_304 = Integer.valueOf(HttpServletResponse.SC_NOT_MODIFIED);
private static Integer SC_400 = Integer.valueOf(HttpServletResponse.SC_BAD_REQUEST);
private static Integer SC_404 = Integer.valueOf(HttpServletResponse.SC_NOT_FOUND);
private static Integer SC_412 = Integer.valueOf(HttpServletResponse.SC_PRECONDITION_FAILED);


Expand All @@ -295,10 +325,6 @@ private enum Task {
PUT_NEW_TXT(HTTP_METHOD.PUT, "/put_new.txt"),

DELETE_EXIST_TXT(HTTP_METHOD.DELETE, "/delete_exist.txt"),
DELETE_EXIST1_TXT(HTTP_METHOD.DELETE, "/delete_exist1.txt"),
DELETE_EXIST2_TXT(HTTP_METHOD.DELETE, "/delete_exist2.txt"),
DELETE_EXIST3_TXT(HTTP_METHOD.DELETE, "/delete_exist3.txt"),
DELETE_EXIST4_TXT(HTTP_METHOD.DELETE, "/delete_exist4.txt"),
DELETE_NOT_EXIST_TXT(HTTP_METHOD.DELETE, "/delete_404.txt");

HTTP_METHOD m;
Expand Down Expand Up @@ -480,22 +506,6 @@ public void setUp() throws Exception {
Files.write((new File(tempDocBase.getAbsolutePath(), "delete_exist.txt")).toPath(), "delete_exist_v0".getBytes(),
StandardOpenOption.CREATE);
(new File(tempDocBase.getAbsolutePath(), "delete_exist.txt")).setLastModified(lastModified);

Files.write((new File(tempDocBase.getAbsolutePath(), "delete_exist1.txt")).toPath(), "delete_exist1_v0".getBytes(),
StandardOpenOption.CREATE);
(new File(tempDocBase.getAbsolutePath(), "delete_exist1.txt")).setLastModified(lastModified);

Files.write((new File(tempDocBase.getAbsolutePath(), "delete_exist2.txt")).toPath(), "delete_exist2_v0".getBytes(),
StandardOpenOption.CREATE);
(new File(tempDocBase.getAbsolutePath(), "delete_exist2.txt")).setLastModified(lastModified);

Files.write((new File(tempDocBase.getAbsolutePath(), "delete_exist3.txt")).toPath(), "delete_exist3_v0".getBytes(),
StandardOpenOption.CREATE);
(new File(tempDocBase.getAbsolutePath(), "delete_exist3.txt")).setLastModified(lastModified);

Files.write((new File(tempDocBase.getAbsolutePath(), "delete_exist4.txt")).toPath(), "delete_exist4_v0".getBytes(),
StandardOpenOption.CREATE);
(new File(tempDocBase.getAbsolutePath(), "delete_exist4.txt")).setLastModified(lastModified);
}
}

Expand Down

0 comments on commit 4d7c896

Please sign in to comment.