-
Notifications
You must be signed in to change notification settings - Fork 1
Support Range Requests #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
avaje-jex/src/main/java/io/avaje/jex/core/RangeWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package io.avaje.jex.core; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import io.avaje.jex.http.Context; | ||
| import io.avaje.jex.http.HttpStatus; | ||
|
|
||
| class RangeWriter { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be final |
||
|
|
||
| private static final int DEFAULT_BUFFER_SIZE = 16384; | ||
|
|
||
| static void write(Context ctx, InputStream inputStream, long totalBytes, long chunkSize) { | ||
|
|
||
| ctx.header(Constants.ACCEPT_RANGES, "bytes"); | ||
| final String rangeHeader = ctx.header(Constants.RANGE); | ||
| if (rangeHeader == null) { | ||
| ctx.contentLength(totalBytes).write(inputStream); | ||
| return; | ||
| } | ||
| final List<String> requestedRange = | ||
| Arrays.stream(rangeHeader.split("=")[1].split("-")).filter(s -> !s.isEmpty()).toList(); | ||
| final long from = Long.parseLong(requestedRange.get(0)); | ||
| final long to; | ||
|
|
||
| final boolean audioOrVideo = isAudioOrVideo(ctx.responseHeader(Constants.CONTENT_TYPE)); | ||
|
|
||
| if (!audioOrVideo || from + chunkSize > totalBytes) { | ||
| to = totalBytes - 1; // chunk bigger than file, write all | ||
| } else if (requestedRange.size() == 2) { | ||
| to = Long.parseLong(requestedRange.get(1)); // chunk smaller than file, to/from specified | ||
| } else { | ||
| to = from + chunkSize - 1; | ||
| } | ||
|
|
||
| long contentLength; | ||
| if (audioOrVideo) { | ||
| contentLength = Math.min(to - from + 1, totalBytes); | ||
| } else { | ||
| contentLength = totalBytes - from; | ||
| } | ||
|
|
||
| final HttpStatus status; | ||
| if (audioOrVideo) { | ||
| status = HttpStatus.PARTIAL_CONTENT_206; | ||
| } else { | ||
| status = HttpStatus.OK_200; | ||
| } | ||
|
|
||
| ctx.status(status); | ||
| ctx.header(Constants.ACCEPT_RANGES, "bytes"); | ||
| ctx.header(Constants.CONTENT_RANGE, "bytes " + from + "-" + to + "/" + totalBytes); | ||
| ctx.contentLength(contentLength); | ||
| try (var os = ctx.outputStream()) { | ||
| write(os, inputStream, from, to); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
|
|
||
| private static void write(OutputStream outputStream, InputStream inputStream, long from, long to) | ||
| throws IOException { | ||
| byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; | ||
| long toSkip = from; | ||
| while (toSkip > 0) { | ||
| toSkip -= inputStream.skip(toSkip); | ||
| } | ||
| long bytesLeft = to - from + 1; | ||
| while (bytesLeft > 0) { | ||
| int read = inputStream.read(buffer, 0, (int) Math.min(DEFAULT_BUFFER_SIZE, bytesLeft)); | ||
| if (read == -1) { | ||
| break; // End of stream reached unexpectedly | ||
| } | ||
| outputStream.write(buffer, 0, read); | ||
| bytesLeft -= read; | ||
| } | ||
| } | ||
|
|
||
| private static boolean isAudioOrVideo(String contentType) { | ||
| return contentType.startsWith("audio/") || contentType.startsWith("video/"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
avaje-jex/src/test/java/io/avaje/jex/http/LargeSeekableInput.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package io.avaje.jex.http; | ||
|
|
||
| import java.io.InputStream; | ||
|
|
||
| class LargeSeekableInput extends InputStream { | ||
| private final long prefixSize; | ||
| private final long contentSize; | ||
| private long alreadyRead = 0L; | ||
|
|
||
| public LargeSeekableInput(long prefixSize, long contentSize) { | ||
| this.prefixSize = prefixSize; | ||
| this.contentSize = contentSize; | ||
| } | ||
|
|
||
| private long remaining() { | ||
| return prefixSize + contentSize - alreadyRead; | ||
| } | ||
|
|
||
| @Override | ||
| public int available() { | ||
| long rem = remaining(); | ||
| if (rem >= 0 && rem <= Integer.MAX_VALUE) { | ||
| return (int) rem; | ||
| } else { | ||
| return Integer.MAX_VALUE; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long skip(long toSkip) { | ||
| if (toSkip <= 0) { | ||
| return 0; | ||
| } else { | ||
| long rem = remaining(); | ||
| if (rem >= 0 && rem <= toSkip) { | ||
| alreadyRead += rem; | ||
| return rem; | ||
| } else { | ||
| alreadyRead += toSkip; | ||
| return toSkip; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int read() { | ||
| if (remaining() == 0L) { | ||
| return -1; | ||
| } else if (alreadyRead < prefixSize) { | ||
| alreadyRead++; | ||
| return ' '; | ||
| } else { | ||
| alreadyRead++; | ||
| return 'J'; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if this should be initialised at all.