Handle offset-from-end ranges and make range handling more robust - #803
Merged
Conversation
This PR adds support for offset-from-end ranges. An offset-from-end range header looks like "Range: bytes=-10". This returns content starting 10 bytes from the end of the content to the end of the content. The PR also updates other range responses to align with the real GCS server. Specifically, the range handling code now detects when the start of range is beyond the content bounds and returns a 416 response. Most other invalid ranges are ignored, except for a few cases where bounds are automatically adjusted. All of the cases were verified against the GCS server using requests like: ``` curl -D - https://storage.googleapis.com/marksandstrom-test/test.txt -H"Range: bytes=0-4" ``` Fixes fsouza#536.
Contributor
|
@fsouza Looks good to me. Anything else you'd like here? |
fsouza
approved these changes
Jun 4, 2022
fsouza
left a comment
Owner
There was a problem hiding this comment.
Thank you very much for contributing!
| // doesn't validate that the start and end indices fall within the content | ||
| // bounds. The content length is only used to handle "suffix length" and | ||
| // range-to-end ranges. | ||
| func parseRange(rangeHeaderValue string, contentLength int64) (start int64, end int64, err error) { |
Owner
There was a problem hiding this comment.
probably worth it adding some tests specifically for this function, specially for the edge cases. I can take care of it later if you prefer though.
Contributor
There was a problem hiding this comment.
@fsouza If you are adding these tests, the std lib has (unexported unfortunately for our needs): https://github.com/golang/go/blob/085529bd5fa3ab508784f6a3d42f15d2dafddc65/src/net/http/fs.go#L870
which is tested here:
https://github.com/golang/go/blob/085529bd5fa3ab508784f6a3d42f15d2dafddc65/src/net/http/fs_test.go#L102
So you might be able to crib some test cases and save some time.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds support for offset-from-end ranges. An offset-from-end
range header looks like "Range: bytes=-10". This returns content
starting 10 bytes from the end of the content to the end of the content.
The PR also updates other range responses to align with the real GCS
server. Specifically, the range handling code now detects when the start
of range is beyond the content bounds and returns a 416 response. Most
other invalid ranges are ignored, except for a few cases where bounds
are automatically adjusted.
All of the cases were verified against the GCS server using requests
like:
Fixes #536.