Skip to content

Commit

Permalink
Ensure authority and/or host fields are not empty (hyperium#232)
Browse files Browse the repository at this point in the history
* Ensure authority and/or host fields are not empty

* Move doc comment

---------

Co-authored-by: ruben <ruben.buehler@t-online.de>
  • Loading branch information
Pi-Cla and Ruben2424 authored Jun 1, 2024
1 parent 793bd17 commit d1ec7c4
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions h3/src/proto/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ impl Header {
//# request MUST contain either an :authority pseudo-header field or a
//# Host header field.

//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//= type=TODO
//# If these fields are present, they MUST NOT be
//# empty.

//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//= type=TODO
//# If the scheme does not have a mandatory authority component and none
Expand All @@ -99,6 +94,10 @@ impl Header {

Ok((
self.pseudo.method.ok_or(HeaderError::MissingMethod)?,
// When empty host field is built into a uri it fails
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//# If these fields are present, they MUST NOT be
//# empty.
uri.build().map_err(HeaderError::InvalidRequest)?,
self.pseudo.protocol,
self.fields,
Expand Down Expand Up @@ -291,6 +290,9 @@ impl Field {

Ok(match name {
b":scheme" => Field::Scheme(try_value(name, value)?),
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//# If these fields are present, they MUST NOT be
//# empty.
b":authority" => Field::Authority(try_value(name, value)?),
b":path" => Field::Path(try_value(name, value)?),
b":method" => Field::Method(
Expand Down Expand Up @@ -498,6 +500,38 @@ mod tests {
);
}

#[test]
fn request_has_empty_authority() {
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//= type=test
//# If these fields are present, they MUST NOT be
//# empty.
assert_matches!(
Header::try_from(vec![
(b":method", Method::GET.as_str()).into(),
(b":authority", b"").into(),
]),
Err(HeaderError::InvalidHeaderValue(_))
);
}

#[test]
fn request_has_empty_host() {
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
//= type=test
//# If these fields are present, they MUST NOT be
//# empty.
let headers = Header::try_from(vec![
(b":method", Method::GET.as_str()).into(),
(b"host", b"").into(),
])
.unwrap();
assert_matches!(
headers.into_request_parts(),
Err(HeaderError::InvalidRequest(_))
);
}

#[test]
fn request_has_authority() {
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.3.1
Expand Down

0 comments on commit d1ec7c4

Please sign in to comment.