Skip to content
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

updated logic for making a range request with no end value #156

Merged
merged 6 commits into from
Mar 6, 2023

Conversation

danielRicaud
Copy link
Contributor

No description provided.

@danielRicaud danielRicaud self-assigned this Mar 2, 2023
@danielRicaud danielRicaud requested a review from a team as a code owner March 2, 2023 20:12
@danielRicaud danielRicaud changed the title updated logic for making a range request with a negative value updated logic for making a range request with no end value Mar 2, 2023
@@ -104,7 +104,7 @@ export const fromUrl = (location: string): Chunker => {
if (byteEnd && byteEnd < 0) {
// NOTE: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
throw Error('negative end unsupported');
} else {
} else if (byteEnd !== undefined) {
rangeHeader += `-${(byteEnd || 0) - 1}`;
}
Copy link
Member

@elizabethhealy elizabethhealy Mar 2, 2023

Choose a reason for hiding this comment

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

you might want to add an extra else for when byteEnd is undefined and throw an error there, or log the error?

Copy link
Member

Choose a reason for hiding this comment

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

Required fix: Update line 103 to end with - and remove the - from line 108. Also, 0 is always an invalid byteEnd for the current implementation, the guard on 104 should be !byteEnd || byteEnd < 0.

Actually reading the range header docs was informative. Notably, it does not implement the full slice API, so we'd need to update this to first run a request to see how big the file is, allowing us to implement the negative end output. For now we can throw an Unsupported error as above and when/if we start seeing that we can go back and add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure we should automatically add - to line 103 since negative range's shouldn't append the -. I've changed 107 to not allow byteEnd to be a zero value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also in the case of byteEnd being undefined that's ok in the case of negative range headers like -1000

Copy link
Member

Choose a reason for hiding this comment

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

So if byteEnd exists, it must:

  • not be negative
  • byteStart can't be negative either, otherwise we need to use the suffix format (last N bytes, -N)
  • must be strictly greater than byteStart

So it should be like:

  if (byteEnd) {
    if (byteStart < 0 || byteEnd <= byteStart) {
      throw Error(`invalid HTTP range [${byteStart}-${byteEnd}]`);
    }
    rangeHeader += `-${(byteEnd || 0) - 1}`;
  }

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: Pull this out as an exported function, just so you can add some unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dmihalcik-virtru implemented the changes. I'll make a quick Jira ticket to pull this function out and add unit tests just because it needs to be released as soon as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this logic back to the original implementation because throwing an error that early was causing a unit test to fail that's already making sure the Range is satisfiable. On line 92 if an Unsatisfiable range is encountered with an HTTP response code of 416 the test catches the issue correctly

@@ -104,7 +104,7 @@ export const fromUrl = (location: string): Chunker => {
if (byteEnd && byteEnd < 0) {
// NOTE: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
throw Error('negative end unsupported');
} else {
} else if (byteEnd !== undefined) {
rangeHeader += `-${(byteEnd || 0) - 1}`;
}
Copy link
Member

Choose a reason for hiding this comment

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

Required fix: Update line 103 to end with - and remove the - from line 108. Also, 0 is always an invalid byteEnd for the current implementation, the guard on 104 should be !byteEnd || byteEnd < 0.

Actually reading the range header docs was informative. Notably, it does not implement the full slice API, so we'd need to update this to first run a request to see how big the file is, allowing us to implement the negative end output. For now we can throw an Unsupported error as above and when/if we start seeing that we can go back and add it.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@danielRicaud danielRicaud merged commit 33e7334 into main Mar 6, 2023
@danielRicaud danielRicaud deleted the feature/fix-negative-range-header-logic branch March 6, 2023 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants