diff options
| author | Kostya <mail@sartin.in> | 2026-10-10 14:12:55 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-10 14:13:20 +0300 |
| commit | 6f0ab9c9a20b96890ed60bc168b28d29f5db39e5 (patch) | |
| tree | 51b85eab9c6b9995b8836544f54ddae861b18b06 /server | |
| parent | 33d313e17b841db3736c465447913df0cc73d5df (diff) | |
| download | guess-market-6f0ab9c9a20b96890ed60bc168b28d29f5db39e5.tar.gz guess-market-6f0ab9c9a20b96890ed60bc168b28d29f5db39e5.tar.xz guess-market-6f0ab9c9a20b96890ed60bc168b28d29f5db39e5.zip | |
server: accept event file uploads held entirely in memory
Add POST /api/upload: a logged-in user sends a Schema v3 file as a
multipart part, its events join the market for everyone, and the
uploader becomes their Market Maker. It uses the Servlet API's own
multipart support, as the spec asks, and hands the part's stream to
the v3 loader, so validation errors (bad XML, broken Exercise 1 rules,
a name already in the market) come back as 400 with their reason.
As in Exercise 1, a file whose name does not end in .xml is refused
before it is read, since a request need not come from our client.
The spec warns that the grading server cannot write files, and Tomcat
writes any multipart part larger than @MultipartConfig's
fileSizeThreshold to a temp file. That threshold defaults to 0, so a
plain upload servlet always touches the disk. Cap uploads at 1 MB and
put the threshold above the whole request limit, so every upload stays
in memory. Bigger files get 413, requests without a session 401, and
non-multipart bodies 400.
Tomcat deletes spooled parts once the request ends, so comparing the
files before and after an upload cannot catch spooling. The test
instead watches the webapp's temp directory for any file being
created while a near-limit upload runs. It fails if the threshold is
set back to 0.
Diffstat (limited to 'server')
3 files changed, 226 insertions, 1 deletions
diff --git a/server/src/main/java/market/guess/server/UploadServlet.java b/server/src/main/java/market/guess/server/UploadServlet.java new file mode 100644 index 0000000..5fe69ad --- /dev/null +++ b/server/src/main/java/market/guess/server/UploadServlet.java | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import jakarta.servlet.ServletException; | ||
| 4 | import jakarta.servlet.annotation.MultipartConfig; | ||
| 5 | import jakarta.servlet.annotation.WebServlet; | ||
| 6 | import jakarta.servlet.http.HttpServlet; | ||
| 7 | import jakarta.servlet.http.HttpServletRequest; | ||
| 8 | import jakarta.servlet.http.HttpServletResponse; | ||
| 9 | import jakarta.servlet.http.Part; | ||
| 10 | import java.io.IOException; | ||
| 11 | import java.util.Locale; | ||
| 12 | |||
| 13 | /** | ||
| 14 | * POST /api/upload, multipart with one file part. The logged-in user becomes the Market Maker of | ||
| 15 | * | ||
| 16 | * <p>The grading server forbids disk writes, and Tomcat spools any part larger than {@code | ||
| 17 | * fileSizeThreshold} to a temp file (the default threshold is 0, i.e. always). Keeping the threshold | ||
| 18 | * above the request limit holds every upload in memory. | ||
| 19 | */ | ||
| 20 | @WebServlet("/api/upload") | ||
| 21 | @MultipartConfig( | ||
| 22 | maxFileSize = UploadServlet.MAX_BYTES, | ||
| 23 | maxRequestSize = UploadServlet.MAX_BYTES + 64 * 1024, | ||
| 24 | fileSizeThreshold = (int) UploadServlet.MAX_BYTES + 128 * 1024) | ||
| 25 | public final class UploadServlet extends HttpServlet { | ||
| 26 | static final long MAX_BYTES = 1024 * 1024; | ||
| 27 | |||
| 28 | @Override | ||
| 29 | protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { | ||
| 30 | var user = Sessions.currentUser(req); | ||
| 31 | if (user == null) { | ||
| 32 | Json.error(res, HttpServletResponse.SC_UNAUTHORIZED, "NOT_LOGGED_IN", "Please log in first."); | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | Part file; | ||
| 37 | try { | ||
| 38 | file = | ||
| 39 | req.getParts().stream() | ||
| 40 | .filter(p -> p.getSubmittedFileName() != null) | ||
| 41 | .findFirst() | ||
| 42 | .orElse(null); | ||
| 43 | } catch (IllegalStateException e) { | ||
| 44 | Json.error( | ||
| 45 | res, | ||
| 46 | HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, | ||
| 47 | "FILE_TOO_LARGE", | ||
| 48 | "The file is larger than " + MAX_BYTES / 1024 + " KB."); | ||
| 49 | return; | ||
| 50 | } catch (ServletException e) { | ||
| 51 | Json.error( | ||
| 52 | res, HttpServletResponse.SC_BAD_REQUEST, "NOT_MULTIPART", "Send the file as multipart."); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | if (file == null) { | ||
| 56 | Json.error(res, HttpServletResponse.SC_BAD_REQUEST, "NO_FILE", "No file was attached."); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | var name = file.getSubmittedFileName(); | ||
| 60 | if (!name.toLowerCase(Locale.ROOT).endsWith(".xml")) { | ||
| 61 | Json.error( | ||
| 62 | res, | ||
| 63 | HttpServletResponse.SC_BAD_REQUEST, | ||
| 64 | "NOT_XML", | ||
| 65 | "'" + name + "' is not an XML file. Choose a file whose name ends in .xml."); | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | var engine = ServerEngine.from(getServletContext()); | ||
| 70 | try (var in = file.getInputStream()) { | ||
| 71 | var result = engine.uploads().load(in, name, user); | ||
| 72 | if (!result.isSuccess()) { | ||
| 73 | Json.error(res, HttpServletResponse.SC_BAD_REQUEST, result); | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | Json.send(res, HttpServletResponse.SC_OK, result.getData()); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
diff --git a/server/src/test/java/market/guess/server/TestServer.java b/server/src/test/java/market/guess/server/TestServer.java index 9b77819..1ab87ae 100644 --- a/server/src/test/java/market/guess/server/TestServer.java +++ b/server/src/test/java/market/guess/server/TestServer.java | |||
| @@ -9,6 +9,7 @@ import java.net.http.HttpResponse; | |||
| 9 | import java.nio.charset.StandardCharsets; | 9 | import java.nio.charset.StandardCharsets; |
| 10 | import java.nio.file.Files; | 10 | import java.nio.file.Files; |
| 11 | import java.nio.file.Path; | 11 | import java.nio.file.Path; |
| 12 | import org.apache.catalina.core.StandardContext; | ||
| 12 | import org.apache.catalina.startup.Tomcat; | 13 | import org.apache.catalina.startup.Tomcat; |
| 13 | import org.apache.catalina.webresources.DirResourceSet; | 14 | import org.apache.catalina.webresources.DirResourceSet; |
| 14 | import org.apache.catalina.webresources.StandardRoot; | 15 | import org.apache.catalina.webresources.StandardRoot; |
| @@ -16,6 +17,7 @@ import org.apache.tomcat.util.scan.StandardJarScanner; | |||
| 16 | 17 | ||
| 17 | final class TestServer implements AutoCloseable { | 18 | final class TestServer implements AutoCloseable { |
| 18 | private final Tomcat tomcat; | 19 | private final Tomcat tomcat; |
| 20 | private final StandardContext ctx; | ||
| 19 | final String api; | 21 | final String api; |
| 20 | 22 | ||
| 21 | TestServer(Path base) throws Exception { | 23 | TestServer(Path base) throws Exception { |
| @@ -25,7 +27,7 @@ final class TestServer implements AutoCloseable { | |||
| 25 | tomcat.getConnector(); | 27 | tomcat.getConnector(); |
| 26 | 28 | ||
| 27 | var docBase = Files.createDirectories(base.resolve("webapp")); | 29 | var docBase = Files.createDirectories(base.resolve("webapp")); |
| 28 | var ctx = tomcat.addWebapp("/guess-market", docBase.toString()); | 30 | ctx = (StandardContext) tomcat.addWebapp("/guess-market", docBase.toString()); |
| 29 | ctx.setParentClassLoader(TestServer.class.getClassLoader()); | 31 | ctx.setParentClassLoader(TestServer.class.getClassLoader()); |
| 30 | ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); | 32 | ((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(false); |
| 31 | var resources = new StandardRoot(ctx); | 33 | var resources = new StandardRoot(ctx); |
| @@ -41,6 +43,10 @@ final class TestServer implements AutoCloseable { | |||
| 41 | api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; | 43 | api = "http://localhost:" + tomcat.getConnector().getLocalPort() + "/guess-market/api"; |
| 42 | } | 44 | } |
| 43 | 45 | ||
| 46 | Path tempDir() { | ||
| 47 | return Path.of(ctx.getWorkPath()); | ||
| 48 | } | ||
| 49 | |||
| 44 | static HttpClient client() { | 50 | static HttpClient client() { |
| 45 | return HttpClient.newBuilder().cookieHandler(new CookieManager()).build(); | 51 | return HttpClient.newBuilder().cookieHandler(new CookieManager()).build(); |
| 46 | } | 52 | } |
diff --git a/server/src/test/java/market/guess/server/UploadTest.java b/server/src/test/java/market/guess/server/UploadTest.java new file mode 100644 index 0000000..2602b7f --- /dev/null +++ b/server/src/test/java/market/guess/server/UploadTest.java | |||
| @@ -0,0 +1,140 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 4 | import static org.junit.jupiter.api.Assertions.assertNull; | ||
| 5 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 6 | |||
| 7 | import java.io.ByteArrayOutputStream; | ||
| 8 | import java.net.http.HttpClient; | ||
| 9 | import java.net.http.HttpRequest; | ||
| 10 | import java.net.http.HttpResponse; | ||
| 11 | import java.nio.charset.StandardCharsets; | ||
| 12 | import java.nio.file.Files; | ||
| 13 | import java.nio.file.Path; | ||
| 14 | import java.nio.file.FileSystems; | ||
| 15 | import java.nio.file.StandardWatchEventKinds; | ||
| 16 | import java.util.concurrent.TimeUnit; | ||
| 17 | import org.junit.jupiter.api.AfterAll; | ||
| 18 | import org.junit.jupiter.api.BeforeAll; | ||
| 19 | import org.junit.jupiter.api.Test; | ||
| 20 | import org.junit.jupiter.api.io.TempDir; | ||
| 21 | |||
| 22 | class UploadTest { | ||
| 23 | @TempDir static Path base; | ||
| 24 | private static TestServer server; | ||
| 25 | |||
| 26 | @BeforeAll | ||
| 27 | static void start() throws Exception { | ||
| 28 | server = new TestServer(base); | ||
| 29 | } | ||
| 30 | |||
| 31 | @AfterAll | ||
| 32 | static void stop() throws Exception { | ||
| 33 | server.close(); | ||
| 34 | } | ||
| 35 | |||
| 36 | private static HttpClient loggedIn(String name) throws Exception { | ||
| 37 | var client = TestServer.client(); | ||
| 38 | assertEquals(200, server.post(client, "/login", "username", name).statusCode()); | ||
| 39 | return client; | ||
| 40 | } | ||
| 41 | |||
| 42 | private static HttpResponse<String> upload(HttpClient client, String fileName, byte[] content) | ||
| 43 | throws Exception { | ||
| 44 | var boundary = "gm-test-boundary"; | ||
| 45 | var body = new ByteArrayOutputStream(); | ||
| 46 | body.writeBytes( | ||
| 47 | ("--" | ||
| 48 | + boundary | ||
| 49 | + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" | ||
| 50 | + fileName | ||
| 51 | + "\"\r\nContent-Type: application/xml\r\n\r\n") | ||
| 52 | .getBytes(StandardCharsets.UTF_8)); | ||
| 53 | body.writeBytes(content); | ||
| 54 | body.writeBytes(("\r\n--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8)); | ||
| 55 | var req = | ||
| 56 | server | ||
| 57 | .request("/upload") | ||
| 58 | .header("Content-Type", "multipart/form-data; boundary=" + boundary) | ||
| 59 | .POST(HttpRequest.BodyPublishers.ofByteArray(body.toByteArray())) | ||
| 60 | .build(); | ||
| 61 | return client.send(req, HttpResponse.BodyHandlers.ofString()); | ||
| 62 | } | ||
| 63 | |||
| 64 | private static byte[] sample(String name) throws Exception { | ||
| 65 | return Files.readAllBytes(Path.of("test-data/ex-3", name)); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | private static int eventCount() throws Exception { | ||
| 70 | var body = server.get(TestServer.client(), "/health").body(); | ||
| 71 | return Integer.parseInt(body.replaceAll(".*\"events\":([0-9]+).*", "$1")); | ||
| 72 | } | ||
| 73 | |||
| 74 | @Test | ||
| 75 | void uploadsAccumulateWithUploaderAsMarketMaker() throws Exception { | ||
| 76 | var startCount = eventCount(); | ||
| 77 | var dana = loggedIn("Dana"); | ||
| 78 | var first = upload(dana, "small.xml", sample("small.xml")); | ||
| 79 | assertEquals(200, first.statusCode(), first.body()); | ||
| 80 | assertTrue(first.body().contains("\"eventsLoaded\":1"), first.body()); | ||
| 81 | |||
| 82 | var eli = loggedIn("Eli"); | ||
| 83 | var second = upload(eli, "multiple.xml", sample("multiple.xml")); | ||
| 84 | assertEquals(200, second.statusCode(), second.body()); | ||
| 85 | assertTrue(second.body().contains("\"eventsLoaded\":3"), second.body()); | ||
| 86 | |||
| 87 | assertEquals(startCount + 4, eventCount()); | ||
| 88 | |||
| 89 | var again = upload(eli, "small.xml", sample("small.xml")); | ||
| 90 | assertEquals(400, again.statusCode()); | ||
| 91 | assertTrue(again.body().contains("NAME_TAKEN"), again.body()); | ||
| 92 | assertTrue(again.body().contains("Mujtaba is Dead"), again.body()); | ||
| 93 | } | ||
| 94 | |||
| 95 | @Test | ||
| 96 | void uploadNeverTouchesTheDisk() throws Exception { | ||
| 97 | var client = loggedIn("Fay"); | ||
| 98 | var padding = "<!--" + "x".repeat((int) UploadServlet.MAX_BYTES - 4096) + "-->"; | ||
| 99 | var xml = | ||
| 100 | new String(sample("small.xml"), StandardCharsets.UTF_8) | ||
| 101 | .replace("Mujtaba is Dead", "Big Upload") | ||
| 102 | .replace("<GM-events>", padding + "<GM-events>"); | ||
| 103 | |||
| 104 | try (var watch = FileSystems.getDefault().newWatchService()) { | ||
| 105 | server.tempDir().register(watch, StandardWatchEventKinds.ENTRY_CREATE); | ||
| 106 | var res = upload(client, "big.xml", xml.getBytes(StandardCharsets.UTF_8)); | ||
| 107 | assertEquals(200, res.statusCode(), res.body()); | ||
| 108 | var key = watch.poll(1, TimeUnit.SECONDS); | ||
| 109 | assertNull(key == null ? null : key.pollEvents(), "upload wrote to " + server.tempDir()); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | @Test | ||
| 114 | void rejectsAnonymousOversizedAndEmptyUploads() throws Exception { | ||
| 115 | var anon = upload(TestServer.client(), "small.xml", sample("small.xml")); | ||
| 116 | assertEquals(401, anon.statusCode()); | ||
| 117 | |||
| 118 | var client = loggedIn("Gil"); | ||
| 119 | var huge = upload(client, "huge.xml", new byte[(int) UploadServlet.MAX_BYTES + 1]); | ||
| 120 | assertEquals(413, huge.statusCode(), huge.body()); | ||
| 121 | |||
| 122 | var notMultipart = server.post(client, "/upload", "file", "<Guess-Market/>"); | ||
| 123 | assertEquals(400, notMultipart.statusCode()); | ||
| 124 | assertTrue(notMultipart.body().contains("NOT_MULTIPART"), notMultipart.body()); | ||
| 125 | |||
| 126 | var malformed = upload(client, "bad.xml", "<Guess-Market>".getBytes(StandardCharsets.UTF_8)); | ||
| 127 | assertEquals(400, malformed.statusCode()); | ||
| 128 | assertTrue(malformed.body().contains("XML_PARSE_ERROR"), malformed.body()); | ||
| 129 | } | ||
| 130 | |||
| 131 | @Test | ||
| 132 | void refusesFilesNotNamedXml() throws Exception { | ||
| 133 | var client = loggedIn("Hila"); | ||
| 134 | var before = eventCount(); | ||
| 135 | var txt = upload(client, "small.txt", sample("small.xml")); | ||
| 136 | assertEquals(400, txt.statusCode(), txt.body()); | ||
| 137 | assertTrue(txt.body().contains("NOT_XML"), txt.body()); | ||
| 138 | assertEquals(before, eventCount()); | ||
| 139 | } | ||
| 140 | } | ||