Skip to content

Commit

Permalink
Fixed bug in output stream that was causing large files to be written…
Browse files Browse the repository at this point in the history
… incorrectly.
  • Loading branch information
voidmain committed Sep 9, 2024
1 parent b7c8320 commit a68c379
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/fusionauth/http/io/ChunkedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void write(byte[] b) throws IOException {
public void write(byte[] b, int offset, int length) throws IOException {
int index = offset;
while (index < length) {
int wrote = Math.min(buffer.length - bufferIndex, length);
System.arraycopy(b, 0, buffer, bufferIndex, wrote);
int wrote = Math.min(buffer.length - bufferIndex, length - index);
System.arraycopy(b, index, buffer, bufferIndex, wrote);
bufferIndex += wrote;
index += wrote;

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/io/fusionauth/http/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodySubscribers;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.time.Duration;
Expand Down Expand Up @@ -319,6 +321,35 @@ public void keepAliveTimeout() {
}
}

@Test(dataProvider = "schemes")
public void largeCSS(String scheme) throws Exception {
var css = Files.readString(Paths.get("src/test/resources/fontawesome-6.0.0.min.css"), StandardCharsets.UTF_8);

HTTPHandler handler = (req, res) -> {
res.setStatus(200);

try {
var out = res.getOutputStream();
out.write(css.getBytes(StandardCharsets.UTF_8));
out.close();
} catch (Throwable t) {
System.out.println(t);
}
};

try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();

var response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), css);
}
}

@Test
public void logger() {
// Test replacement values and ensure we are handling special regex characters.
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/fontawesome-6.0.0.min.css

Large diffs are not rendered by default.

0 comments on commit a68c379

Please sign in to comment.