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

Add range request and etag support to file_server.ts #1028

Merged
merged 14 commits into from
Jul 22, 2021

Conversation

pseudosavant
Copy link
Contributor

First pass at adding support for range requests

  • Basic etag support
  • Server name header
  • Date and Last-Modified headers
  • Added ~200 additional mimetypes

- Basic etag support
- Server name header
- Date and Last-Modified headers
- Added ~200 additional mimetypes
@CLAassistant
Copy link

CLAassistant commented Jul 12, 2021

CLA assistant check
All committers have signed the CLA.

@kitsonk
Copy link
Contributor

kitsonk commented Jul 12, 2021

Personal opinion is that this is really getting into the range of what a Web server framework does. file_server is intended to be a minimalist example application. Also it doesn't have tests, but even with exhaustive tests I would feel the same way, that it isn't part of the std library.

- Corrected single-quotes to double-quotes
- Fixed use before assignment for `start` and `end`
@pseudosavant
Copy link
Contributor Author

Personal opinion is that this is really getting into the range of what a Web server framework does. file_server is intended to be a minimalist example application. Also it doesn't have tests, but even with exhaustive tests I would feel the same way, that it isn't part of the std library.

Agreed on the tests. I'm working on them. I wanted to get some feedback on this first. Is file_server.ts not actually part of the std library even though it is in the std repo?

I'm not sure how supporting a basic HTTP 1.1 feature makes it a 'framework'? Without range support, it is basically an HTTP 1.0 server. I know file_server.ts is made for basic non-prod work, but even for a basic 'built-in' web server, it is very limited. If it is to remain this limited, it should advertise itself as HTTP 1.0.

I have a video player app that I work on that does thumbnailing for any video URL. Lack of range support caused it to either: 1) only ever show the very first frame no matter the requested timestamp, or 2) it wouldn't work at all, but also wasn't throwing a visible error in the browser. The issue affected playback too. The browser video engine wouldn't seek (even fully downloaded files) or would have to download the entire file because the MP4 index was at the end.

- Set standard base headers (`server`, `accept-ranges`, `date`) for all responses
- Fixed handling of malformed range requests
- `response.body` is empty for 304 responses
- Added tests for: range requests, etag headers, mime-type headers, date headers
- Populated test `hello.html` and `test file.txt` files with test data
…e test, contentType test

- CORS has its own specific test now
- Excess tests for CORS removed from other tests
- Corrected test for `/testdata/%` URL
- Fixed contentType test
- All tests now pass
@pseudosavant
Copy link
Contributor Author

I have added and fixed all the tests in file_server_ts.ts, and fixed issues in file_server.ts discovered from tests.

@pseudosavant
Copy link
Contributor Author

Of course all of the tests passed on my machine, but not on the CI. The file metadata (length, date) is being returned different, probably due to platform differences. I'm changing them to dynamically pull the metadata instead of it being hard coded.

http/file_server_test.ts Outdated Show resolved Hide resolved
http/file_server.ts Outdated Show resolved Hide resolved
@kt3k
Copy link
Member

kt3k commented Jul 14, 2021

I'm slightly in favor of including this change because according to the file's history we've been accepting the PRs which simply add the usability as a file server, (which therefore reduce the minimality as an example). I think this is one of that types of changes. But I'd like to hear other collaborators opinions as well.

@pseudosavant
Copy link
Contributor Author

Fixed a couple of hardcoded values I missed in 2 tests.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

This falls in the scope of the file_server.ts and looks well-implemented with tests. Thank you for this PR @pseudosavant !

I'm a little concerned that the file_server now depends on createHash. Can crypto.subtle.digest be used instead?

@pseudosavant
Copy link
Contributor Author

I'm a little concerned that the file_server now depends on createHash. Can crypto.subtle.digest be used instead?

Sure, that was a straight-forward change. I just pushed a commit for it.

@pseudosavant pseudosavant changed the title First pass at adding range request support to file_server.ts Add range request and etag support to file_server.ts Jul 16, 2021
"",
);
return hashHex;
};
Copy link
Member

Choose a reason for hiding this comment

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

This code is duplicated... Seems better if you export it.

Also the .join("",) seems odd...

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 can move that out into a module. Is there guidance on where I should put that in the repo?

As for the join. Agreed .join("",) looks odd. But do you mean the trailing comma or joining the array into strings using join? The comma and line break are being added by deno fmt. I don't understand why it is doing that though. That method only accepts 1 argument.

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 pushed a commit to make createEtagHash clearer, and format better. It doesn't address moving it out into a module yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ry Any guidance on where I should put the module? Should I put it in something like _createEtagHash.ts?

Copy link
Member

Choose a reason for hiding this comment

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

That would work. But I question whether the test needs the ability to create the etag header generally - how about just hardcoding some expected etag values into the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My first tests did used hard coded values but it didn't work. I learned that the last modified time, and the system reported file size, aren't consistent across environments. Those are the two inputs, concatenated, for createEtagHash.

@kt3k
Copy link
Member

kt3k commented Jul 20, 2021

LGTM except CLA check error. It seems 2 email addresses are used in the commits and one of them causing the error... @pseudosavant Can you fix this situation?

@pseudosavant
Copy link
Contributor Author

LGTM except CLA check error. It seems 2 email addresses are used in the commits and one of them causing the error... @pseudosavant Can you fix this situation?

I've been trying to get it corrected. I didn't realize my git config was slightly different on one of my other machines I used. I followed the CLA assistant's directions and added and verified the other email address in my GitHub account. The CLA assistant doesn't seem to pick it up though. And when I try doing the assistant again it says I've signed it already.

Do you know of another way I could fix this?

@kt3k
Copy link
Member

kt3k commented Jul 22, 2021

Only the last commit had a different email address. I squashed it to the one before it.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

@pseudosavant Thank you for your contribution!

@kt3k kt3k merged commit 0722cd0 into denoland:main Jul 22, 2021
@pseudosavant
Copy link
Contributor Author

@pseudosavant Thank you for your contribution!

Thanks for the help getting it merged in! Deno is a great project. 🙏

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.

5 participants