Skip to content

Handle offset-from-end ranges and make range handling more robust - #803

Merged
fsouza merged 6 commits into
fsouza:mainfrom
dnerdy:range-updates
Jun 4, 2022
Merged

Handle offset-from-end ranges and make range handling more robust#803
fsouza merged 6 commits into
fsouza:mainfrom
dnerdy:range-updates

Conversation

@dnerdy

@dnerdy dnerdy commented Jun 1, 2022

Copy link
Copy Markdown
Contributor

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 #536.

dnerdy added 5 commits June 1, 2022 14:57
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.
@StevenACoffman

Copy link
Copy Markdown
Contributor

@fsouza Looks good to me. Anything else you'd like here?

@fsouza fsouza left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for contributing!

Comment thread fakestorage/object.go Outdated
Comment thread fakestorage/object.go
// 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) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@fsouza
fsouza merged commit 275f44a into fsouza:main Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants