-
-
Notifications
You must be signed in to change notification settings - Fork 288
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 server support for push #327
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e116ed6
Add server support for push
michaelbeaumont 287c36b
Properly queue push promise frames
michaelbeaumont f2e0241
Revert "Properly queue push promise frames"
michaelbeaumont 151c0bf
Don't send for stream if a PushPromise is pending
michaelbeaumont 8a8c6ac
Add additional tests
michaelbeaumont 09a9ab6
Add SendPushedResponse wrapper type
michaelbeaumont f49a24f
Code cleanup
michaelbeaumont 810e56e
Merge remote-tracking branch 'upstream/master' into server_push
michaelbeaumont c118a52
Update push promise based tests
michaelbeaumont 51c94dc
Fix and add comments, add debug messages
michaelbeaumont a1ca693
Merge remote-tracking branch 'upstream/master' into server_push
michaelbeaumont File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ use super::{StreamDependency, StreamId}; | |
use frame::{Error, Frame, Head, Kind}; | ||
use hpack; | ||
|
||
use http::{uri, HeaderMap, Method, StatusCode, Uri}; | ||
use http::{uri, Request, HeaderMap, Method, StatusCode, Uri}; | ||
use http::header::{self, HeaderName, HeaderValue}; | ||
|
||
use byteorder::{BigEndian, ByteOrder}; | ||
|
@@ -283,9 +283,86 @@ impl fmt::Debug for Headers { | |
} | ||
} | ||
|
||
// ===== util ===== | ||
|
||
pub fn parse_u64(src: &[u8]) -> Result<u64, ()> { | ||
if src.len() > 19 { | ||
// At danger for overflow... | ||
return Err(()); | ||
} | ||
|
||
let mut ret = 0; | ||
|
||
for &d in src { | ||
if d < b'0' || d > b'9' { | ||
return Err(()); | ||
} | ||
|
||
ret *= 10; | ||
ret += (d - b'0') as u64; | ||
} | ||
|
||
Ok(ret) | ||
} | ||
|
||
// ===== impl PushPromise ===== | ||
|
||
impl PushPromise { | ||
pub fn new( | ||
stream_id: StreamId, | ||
promised_id: StreamId, | ||
pseudo: Pseudo, | ||
fields: HeaderMap, | ||
) -> Self { | ||
PushPromise { | ||
flags: PushPromiseFlag::default(), | ||
header_block: HeaderBlock { | ||
fields, | ||
is_over_size: false, | ||
pseudo, | ||
}, | ||
promised_id, | ||
stream_id, | ||
} | ||
} | ||
|
||
pub fn validate_request(req: &Request<()>) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cleanup work is good standalone. It might be worth extracting it to a dedicated PR in order to simplify this PR (and make reviewing easier). Up to you though! Whatever makes it easier for you. |
||
// The spec has some requirements for promised request headers | ||
// [https://httpwg.org/specs/rfc7540.html#PushRequests] | ||
|
||
// A promised request "that indicates the presence of a request body | ||
// MUST reset the promised stream with a stream error" | ||
if let Some(content_length) = req.headers().get(header::CONTENT_LENGTH) { | ||
match parse_u64(content_length.as_bytes()) { | ||
michaelbeaumont marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ok(0) => {}, | ||
_ => return false, | ||
} | ||
} | ||
// "The server MUST include a method in the :method pseudo-header field | ||
// that is safe and cacheable" | ||
if !Self::safe_and_cacheable(req.method()) { | ||
return false; | ||
} | ||
|
||
true | ||
} | ||
|
||
fn safe_and_cacheable(method: &Method) -> bool { | ||
// Cacheable: https://httpwg.org/specs/rfc7231.html#cacheable.methods | ||
// Safe: https://httpwg.org/specs/rfc7231.html#safe.methods | ||
return method == Method::GET || method == Method::HEAD; | ||
} | ||
|
||
|
||
pub fn fields(&self) -> &HeaderMap { | ||
&self.header_block.fields | ||
} | ||
|
||
#[cfg(feature = "unstable")] | ||
pub fn into_fields(self) -> HeaderMap { | ||
self.header_block.fields | ||
} | ||
|
||
/// Loads the push promise frame but doesn't actually do HPACK decoding. | ||
/// | ||
/// HPACK decoding is done in the `load_hpack` step. | ||
|
@@ -378,44 +455,13 @@ impl PushPromise { | |
fn head(&self) -> Head { | ||
Head::new(Kind::PushPromise, self.flags.into(), self.stream_id) | ||
} | ||
} | ||
|
||
impl PushPromise { | ||
/// Consume `self`, returning the parts of the frame | ||
pub fn into_parts(self) -> (Pseudo, HeaderMap) { | ||
(self.header_block.pseudo, self.header_block.fields) | ||
} | ||
} | ||
|
||
#[cfg(feature = "unstable")] | ||
impl PushPromise { | ||
pub fn new( | ||
stream_id: StreamId, | ||
promised_id: StreamId, | ||
pseudo: Pseudo, | ||
fields: HeaderMap, | ||
) -> Self { | ||
PushPromise { | ||
flags: PushPromiseFlag::default(), | ||
header_block: HeaderBlock { | ||
fields, | ||
is_over_size: false, | ||
pseudo, | ||
}, | ||
promised_id, | ||
stream_id, | ||
} | ||
} | ||
|
||
pub fn fields(&self) -> &HeaderMap { | ||
&self.header_block.fields | ||
} | ||
|
||
pub fn into_fields(self) -> HeaderMap { | ||
self.header_block.fields | ||
} | ||
} | ||
|
||
impl<T> From<PushPromise> for Frame<T> { | ||
fn from(src: PushPromise) -> Self { | ||
Frame::PushPromise(src) | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is used in multiple locations now (I believe), I would consider creating a private
util
module at the root and moving this there.