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

Content length handling #544

Closed
wants to merge 2 commits into from
Closed

Conversation

amouat
Copy link

@amouat amouat commented Jan 18, 2018

Closes #514.

I moved all logic for setting Content-Length out of write_response and into dispatch.

I expanded the tests in lib/tests/head_handling.rs.

@amouat
Copy link
Author

amouat commented Jan 18, 2018

Test failure is due to out-of-sync libraries. The fix I used is to bump rusqlite to "0.13" in examples/raw_sqlite/Cargo.toml. I didn't include the fix in this PR as it's a different issue.

@SergioBenitez
Copy link
Member

It's failing because of rust-lang/cargo#4902.

@SergioBenitez
Copy link
Member

This changeset is much more complicated than it needs to be. All that needs to happen is that the body needs to be stripped using Response.strip_body() if the user's handler responded successfully to a HEAD request. The code already does the rest. This should be a line or two of changes.

@amouat
Copy link
Author

amouat commented Jan 19, 2018

I'm new to Rust and know very little of Rocket internals, so there may well be a better way of doing this. I'm afraid I can't see it though, and I did play with a few approaches.

I'll take you through my logic. In order to correctly meet the HTTP spec, we need to:

  1. Set the content-length on HEAD requests to the same as the GET equivalent.
  2. Set the content-length on empty body requests to 0 (I'm not sure if this is essential)
  3. Do not set the content-length for streaming responses
  4. Strip the body from HEAD requests

In the original code, the body is stripped before the content-length is set. To correctly set the length, we can either move the code that sets the content-length so it happens before the strip, or save the content-length somewhere so we know what to set it to. I felt it was simpler to move the content-length code to dispatch.

The final match branch can probably be simplified to _ at the loss of some explicitness. It could also be changed to an if/else, but I think the match is cleaner.

I wonder if it would be possible to automatically generate HEAD routes at start-up rather than handle them in dispatch if they aren't set. This would simplify dispatch and get rid of the need to cache the method type for this patch.

@SergioBenitez
Copy link
Member

Unfortunately I don't have a lot of time at the moment, so I went ahead and took care of it, @amouat. Apologies for being a bit short on my previous response.

@SergioBenitez SergioBenitez added the pr: closed This pull request was not merged label Jan 19, 2018
@amouat
Copy link
Author

amouat commented Jan 19, 2018

No worries, I had assumed strip_body set the size to 0, which was wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: closed This pull request was not merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants