Skip to content

Commit

Permalink
Fix gateway handler infinite loop on large assets!
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Jan 19, 2025
1 parent e4d1341 commit 37ffb64
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/peergos/server/net/GatewayHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private Optional<byte[]> serveAsset(AsyncReader reader,
httpExchange.getResponseHeaders().set("permissions-policy", "interest-cohort=()");

if (size < MAX_ASSET_SIZE_CACHE) {
byte[] body = Serialize.readFully(reader, size).join();
byte[] body = Serialize.readFully(reader, size).orTimeout(15, TimeUnit.SECONDS).join();
addContentType(httpExchange, path, props, body);
httpExchange.sendResponseHeaders(200, size);
OutputStream resp = httpExchange.getResponseBody();
Expand All @@ -220,7 +220,7 @@ private Optional<byte[]> serveAsset(AsyncReader reader,
byte[] buf = buffer.get();
int read;
long offset = 0;
while ((read = reader.readIntoArray(buf, 0, (int) Math.min(size - offset, buf.length)).orTimeout(15, TimeUnit.SECONDS).join()) >= 0) {
while (offset < size && (read = reader.readIntoArray(buf, 0, (int) Math.min(size - offset, buf.length)).orTimeout(15, TimeUnit.SECONDS).join()) >= 0) {
resp.write(buf, 0, read);
offset += read;
}
Expand Down

0 comments on commit 37ffb64

Please sign in to comment.