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/src/main/java | |
| 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/src/main/java')
| -rw-r--r-- | server/src/main/java/market/guess/server/UploadServlet.java | 79 |
1 files changed, 79 insertions, 0 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 | } | ||