From 3627f5e07b6f510ad8c3cb56d5fe829f7e5f5aaa Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 8 Jan 2025 13:38:57 -0600 Subject: [PATCH] Issue #12680 - Test of 2GB+ files on ResourceHandler. --- .../server/handler/ResourceHandlerTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java index 2fda299cec03..ea8ff94109ff 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java @@ -18,6 +18,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.SeekableByteChannel; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; @@ -25,6 +27,7 @@ import java.nio.file.StandardOpenOption; import java.nio.file.attribute.FileTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.function.Consumer; import java.util.regex.Matcher; @@ -760,6 +763,28 @@ public void testBigger() throws Exception assertThat(response.getContent(), containsString(" 400\tThis is a big file\n")); } + @Test + public void testOver2GBFile() throws Exception + { + long hugeLength = (long)Integer.MAX_VALUE + 10L; + + generateFile(docRoot.resolve("huge.mkv"), hugeLength); + + HttpTester.Response response = HttpTester.parseResponse( + _local.getResponse(""" + GET /context/huge.mkv HTTP/1.1\r + Host: local\r + Connection: close\r + \r + """)); + + System.err.println(response); + + assertThat(response.getStatus(), is(HttpStatus.OK_200)); + long responseContentLength = response.getLongField(CONTENT_LENGTH); + assertThat(responseContentLength, is(hugeLength)); + } + @Test public void testBrotliInitialCompressed() throws Exception { @@ -3939,6 +3964,40 @@ private void setupBigFiles(Path base) throws Exception } } + private void generateFile(Path staticFile, long size) throws Exception + { + byte[] buf = new byte[(int)(1024 * 1024)]; // about 1 MB + Arrays.fill(buf, (byte)'x'); + ByteBuffer src = ByteBuffer.wrap(buf); + + if (Files.exists(staticFile) && Files.size(staticFile) == size) + { + // all done, nothing left to do. + System.err.printf("File Exists Already: %s (%,d bytes)%n", staticFile, Files.size(staticFile)); + return; + } + + System.err.printf("Creating %,d byte file: %s ...%n", size, staticFile); + try (SeekableByteChannel channel = Files.newByteChannel(staticFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) + { + long remaining = size; + while (remaining > 0) + { + ByteBuffer slice = src.slice(); + int len = buf.length; + if (remaining < Integer.MAX_VALUE) + { + len = Math.min(buf.length, (int)remaining); + slice.limit(len); + } + + channel.write(slice); + remaining -= len; + } + } + System.err.println(" Done"); + } + private void setupQuestionMarkDir(Path base) throws IOException { boolean filesystemSupportsQuestionMarkDir = false;