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

Support SendResponse::send_continue #601

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/proto/streams/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ impl Send {

Self::check_headers(frame.fields())?;

let final_response = !frame.is_informational();
let end_stream = frame.is_end_stream();

// Update the state
stream.state.send_open(end_stream)?;
stream.state.send_open(final_response, end_stream)?;

if counts.peer().is_local_init(frame.stream_id()) {
// If we're waiting on a PushPromise anyway
Expand Down
10 changes: 7 additions & 3 deletions src/proto/streams/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ enum Cause {

impl State {
/// Opens the send-half of a stream if it is not already open.
pub fn send_open(&mut self, eos: bool) -> Result<(), UserError> {
let local = Streaming;
pub fn send_open(&mut self, final_response: bool, eos: bool) -> Result<(), UserError> {
let local = if final_response {
Streaming
} else {
AwaitingHeaders
};

self.inner = match self.inner {
Idle => {
Expand All @@ -111,7 +115,7 @@ impl State {
Open { local, remote }
}
}
HalfClosedRemote(AwaitingHeaders) | ReservedLocal => {
HalfClosedRemote(AwaitingHeaders | Streaming) | ReservedLocal => {
Copy link
Author

Choose a reason for hiding this comment

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

I spent some time thinking about this in particular. There is no documentation on the struct hinting at how to reason about the state, but if I understood it correctly HalfClosedRemote means that we've gotten an EOS from the remote side. HalfClosedRemote(AwaitingHeaders) this side currently haven't written headers yet (the enum-name Peer is a bit misleading here?). This used to encode the rule that headers should only be sent once. With 100-continue in the picture, however, that doesn't really hold true. The easiest way I could see to model this, is to simply relax this rule as proposed here. Perhaps another approach could be feasible? I.E. something not "Opening the send-half", but instead explicitly "finish sending headers"?

Copy link
Member

Choose a reason for hiding this comment

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

The code comments at the top of file include the flow chart from the spec, but the the spec includes more description of each possible state: https://httpwg.org/specs/rfc7540.html#StreamStates.

It looks like your understanding is correct. I've also found the usage of Peer in this crate to be the opposite of what I usually think, but it is consistently used in this file and elsewhere to mean "me" (I usually think of peer as the remote, but I didn't come up with those names 🤷).

Copy link
Member

Choose a reason for hiding this comment

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

However, I think we don't want to change the state here, since sending a HEADERS frame with a 1xx code shouldn't mean it is now waiting to send DATA frames. It still needs to send the "final" code in another HEADERS frame.

if eos {
Closed(Cause::EndStream)
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,22 @@ impl<B: Buf> SendResponse<B> {
.map_err(Into::into)
}

/// Send a non-final 1xx response to a client request.
///
/// The [`SendResponse`] instance is already associated with a received
/// request. This function may only be called if [`send_reset`] or
/// [`send_response`] has not been previously called.
///
/// [`SendResponse`]: #
/// [`send_reset`]: #method.send_reset
/// [`send_response`]: #method.send_response
pub fn send_info(&mut self, response: Response<()>) -> Result<(), crate::Error> {
assert!(response.status().is_informational());
self.inner
.send_response(response, false)
.map_err(Into::into)
}

/// Push a request and response to the client
///
/// On success, a [`SendResponse`] instance is returned.
Expand Down
55 changes: 55 additions & 0 deletions tests/h2-tests/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,61 @@ async fn serve_request() {
join(client, srv).await;
}

#[tokio::test]
async fn serve_request_expect_continue() {
h2_support::trace_init!();
let (io, mut client) = mock::new();

let client = async move {
let settings = client.assert_server_handshake().await;
assert_default_settings!(settings);
client
.send_frame(
frames::headers(1)
.field(http::header::EXPECT, "100-continue")
.request("POST", "https://example.com/"),
)
.await;
client.recv_frame(frames::headers(1).response(100)).await;
client
.send_frame(frames::data(1, "hello world").eos())
.await;
client
.recv_frame(frames::headers(1).response(200).eos())
.await;
};

let srv = async move {
let mut srv = server::handshake(io).await.expect("handshake");
let (req, mut stream) = srv.next().await.unwrap().unwrap();

assert_eq!(req.method(), &http::Method::POST);
assert_eq!(
req.headers().get(http::header::EXPECT),
Some(&http::HeaderValue::from_static("100-continue"))
);

let connection_fut = poll_fn(|cx| srv.poll_closed(cx).map(Result::ok));
let test_fut = async move {
stream.send_continue().unwrap();

let mut body = req.into_body();
assert_eq!(
body.next().await.unwrap().unwrap(),
Bytes::from_static(b"hello world")
);
assert!(body.next().await.is_none());

let rsp = http::Response::builder().status(200).body(()).unwrap();
stream.send_response(rsp, true).unwrap();
};

join(connection_fut, test_fut).await;
};

join(client, srv).await;
}

#[tokio::test]
async fn serve_connect() {
h2_support::trace_init!();
Expand Down