Skip to content

Commit

Permalink
Make :status in requests be a stream error
Browse files Browse the repository at this point in the history
  • Loading branch information
nox authored and seanmonstar committed Oct 19, 2021
1 parent 8520f06 commit c38c94c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/frame/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@ impl Pseudo {
}
}

#[cfg(feature = "unstable")]
pub fn set_status(&mut self, value: StatusCode) {
self.status = Some(value);
}

pub fn set_scheme(&mut self, scheme: uri::Scheme) {
let bytes_str = match scheme.as_str() {
"http" => BytesStr::from_static("http"),
Expand Down
4 changes: 1 addition & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,10 +1410,8 @@ impl proto::Peer for Peer {
malformed!("malformed headers: missing method");
}

// Specifying :status for a request is a protocol error
if pseudo.status.is_some() {
tracing::trace!("malformed headers: :status field on request; PROTOCOL_ERROR");
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
malformed!("malformed headers: :status field on request");
}

// Convert the URI
Expand Down
10 changes: 9 additions & 1 deletion tests/h2-support/src/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryInto;
use std::fmt;

use bytes::Bytes;
use http::{self, HeaderMap};
use http::{self, HeaderMap, StatusCode};

use h2::frame::{self, Frame, StreamId};

Expand Down Expand Up @@ -162,6 +162,14 @@ impl Mock<frame::Headers> {
Mock(frame)
}

pub fn status(self, value: StatusCode) -> Self {
let (id, mut pseudo, fields) = self.into_parts();

pseudo.set_status(value);

Mock(frame::Headers::new(id, pseudo, fields))
}

pub fn scheme(self, value: &str) -> Self {
let (id, mut pseudo, fields) = self.into_parts();
let value = value.parse().unwrap();
Expand Down
24 changes: 24 additions & 0 deletions tests/h2-tests/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,30 @@ async fn server_error_on_unclean_shutdown() {
srv.await.expect_err("should error");
}

#[tokio::test]
async fn server_error_on_status_in_request() {
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).status(StatusCode::OK))
.await;
client.recv_frame(frames::reset(1).protocol_error()).await;
};

let srv = async move {
let mut srv = server::handshake(io).await.expect("handshake");

assert!(srv.next().await.is_none());
};

join(client, srv).await;
}

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

0 comments on commit c38c94c

Please sign in to comment.