MultipartParser { public static Parameters parse(InputStream in, Long contentLength, String contentType, int bufferSize) throws IOException { if (contentLength != null && contentLength == 0) return Parameters.empty(); String boundary = parseBoundary(contentType); if (boundary == null) return Parameters.empty(); if (bufferSize == 0) bufferSize = DEFAULT_BUFFER_SIZE; byte[] buffer = new byte[bufferSize]; MultipartParser parser = new MultipartParser(boundary, bufferSize); int readed = in.read(buffer); parser.onRead(buffer, readed); while (parser.state != ParseState.DONE) { readed = in.read(buffer, 0, parser.buf.remaining()); parser.onRead(buffer, readed); } return parser.result(); } MultipartParser(String boundary, int bufferSize); void onRead(byte[] src, int len); Parameters result(); static String parseBoundary(String contentType); static Parameters parse(InputStream in, Long contentLength, String contentType, int bufferSize);  }
@Test void testIE() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/ie"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); Optional<String> content = getFileContents(params, "files"); assertThat(content) .isPresent() .contains("contents"); Files.deleteIfExists(((File) params.getIn("files", "tempfile")).toPath()); }
@Test void testContentTypeAndNoFilename() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/contentTypeAndNoFilename"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); assertThat(params.get("text")).isEqualTo("contents"); }
@Test void testEmpty() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/empty"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); assertThat(params.get("submit-name")).isEqualTo("Larry"); Optional<String> content = getFileContents(params, "files"); assertThat(content) .isPresent() .contains(""); Files.deleteIfExists(((File) params.getIn("files", "tempfile")).toPath()); }
@Test void testFilenameWithEncodedWords() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/filenameWithEncodedWords"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); Optional<String> content = getFileContents(params, "files"); assertThat(content) .isPresent() .contains("contents"); assertThat(params.getIn("files", "filename")).isEqualTo("файл"); Files.deleteIfExists(((File) params.getIn("files", "tempfile")).toPath()); }
@Test void testFilenameWithEscapeQuotes() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/filenameWithEscapedQuotes"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); Optional<String> content = getFileContents(params, "files"); assertThat(content) .isPresent() .contains("contents"); assertThat(params.getIn("files", "filename")) .isEqualTo("escape \"quotes"); assertThat(params.getIn("files", "type")) .isEqualTo("application/octet-stream"); assertThat(params.getIn("files", "name")) .isEqualTo("files"); Files.deleteIfExists(((File) params.getIn("files", "tempfile")).toPath()); }
@Test void testFilenameWithEscapeQuotesAndModificationParam() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/filenameWithEscapedQuotesAndModificationParam"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); Optional<String> content = getFileContents(params, "files"); assertThat(content) .isPresent() .contains("contents"); assertThat(params.getIn("files", "filename")) .isEqualTo("\"human\" genome.jpeg"); assertThat(params.getIn("files", "type")) .isEqualTo("image/jpeg"); assertThat(params.getIn("files", "name")) .isEqualTo("files"); Files.deleteIfExists(((File) params.getIn("files", "tempfile")).toPath()); }
@Test void testFilenameWithPercentEscapedQuotes() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/filenameWithPercentEscapedQuotes"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); Optional<String> content = getFileContents(params, "files"); assertThat(content) .isPresent() .contains("contents"); assertThat(params.getIn("files", "filename")) .isEqualTo("escape \"quotes"); assertThat(params.getIn("files", "type")) .isEqualTo("application/octet-stream"); assertThat(params.getIn("files", "name")) .isEqualTo("files"); Files.deleteIfExists(((File) params.getIn("files", "tempfile")).toPath()); }
@Test void testFilenameWithSingleQuote() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/filenameWithSingleQuote"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=AaB03x", 0); Optional<String> content = getFileContents(params, "files"); assertThat(content) .isPresent() .contains("contents"); assertThat(params.getIn("files", "filename")) .isEqualTo("bob's flowers.jpg"); Files.deleteIfExists(((File) params.getIn("files", "tempfile")).toPath()); }
@Test void testFilenameWithUnescapedPercentages() throws IOException { InputStream is = getClass().getResourceAsStream("/multipart/filenameWithUnescapedPercentages"); Parameters params = MultipartParser.parse(is, null, "multipart/form-data; boundary=----WebKitFormBoundary2NHc7OhsgU68l3Al", 0); Optional<String> content = getFileContents(params, "document[attachment]"); assertThat(content) .isPresent() .contains("contents"); assertThat(params.getIn("document[attachment]", "filename")) .isEqualTo("100% of a photo.jpeg"); assertThat(params.getIn("document[attachment]", "type")) .isEqualTo("image/jpeg"); assertThat(params.getIn("document[attachment]", "name")) .isEqualTo("document[attachment]"); Files.deleteIfExists(((File) params.getIn("document[attachment]", "tempfile")).toPath()); }