Skip to content

Commit 73dd474

Browse files
noxseanmonstar
authored andcommitted
feat(h1): allow ignoring invalid header lines in requests
1 parent 287d712 commit 73dd474

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ futures-util = { version = "0.3", default-features = false }
2727
http = "0.2"
2828
http-body = "0.4"
2929
httpdate = "1.0"
30-
httparse = "1.6"
30+
httparse = "1.8"
3131
h2 = { version = "0.3.9", optional = true }
3232
itoa = "1"
3333
tracing = { version = "0.1", default-features = false, features = ["std"] }

src/client/client.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,39 @@ impl Builder {
10631063
self
10641064
}
10651065

1066+
/// Sets whether invalid header lines should be silently ignored in HTTP/1 responses.
1067+
///
1068+
/// This mimicks the behaviour of major browsers. You probably don't want this.
1069+
/// You should only want this if you are implementing a proxy whose main
1070+
/// purpose is to sit in front of browsers whose users access arbitrary content
1071+
/// which may be malformed, and they expect everything that works without
1072+
/// the proxy to keep working with the proxy.
1073+
///
1074+
/// This option will prevent Hyper's client from returning an error encountered
1075+
/// when parsing a header, except if the error was caused by the character NUL
1076+
/// (ASCII code 0), as Chrome specifically always reject those.
1077+
///
1078+
/// The ignorable errors are:
1079+
/// * empty header names;
1080+
/// * characters that are not allowed in header names, except for `\0` and `\r`;
1081+
/// * when `allow_spaces_after_header_name_in_responses` is not enabled,
1082+
/// spaces and tabs between the header name and the colon;
1083+
/// * missing colon between header name and colon;
1084+
/// * characters that are not allowed in header values except for `\0` and `\r`.
1085+
///
1086+
/// If an ignorable error is encountered, the parser tries to find the next
1087+
/// line in the input to resume parsing the rest of the headers. An error
1088+
/// will be emitted nonetheless if it finds `\0` or a lone `\r` while
1089+
/// looking for the next line.
1090+
pub fn http1_ignore_invalid_headers_in_responses(
1091+
&mut self,
1092+
val: bool,
1093+
) -> &mut Builder {
1094+
self.conn_builder
1095+
.http1_ignore_invalid_headers_in_responses(val);
1096+
self
1097+
}
1098+
10661099
/// Set whether HTTP/1 connections should try to use vectored writes,
10671100
/// or always flatten into a single buffer.
10681101
///

src/client/conn.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,24 @@ impl Builder {
662662
self
663663
}
664664

665+
/// Set whether HTTP/1 connections will silently ignored malformed header lines.
666+
///
667+
/// If this is enabled and and a header line does not start with a valid header
668+
/// name, or does not include a colon at all, the line will be silently ignored
669+
/// and no error will be reported.
670+
///
671+
/// Note that this setting does not affect HTTP/2.
672+
///
673+
/// Default is false.
674+
pub fn http1_ignore_invalid_headers_in_responses(
675+
&mut self,
676+
enabled: bool,
677+
) -> &mut Builder {
678+
self.h1_parser_config
679+
.ignore_invalid_headers_in_responses(enabled);
680+
self
681+
}
682+
665683
/// Set whether HTTP/1 connections should try to use vectored writes,
666684
/// or always flatten into a single buffer.
667685
///

0 commit comments

Comments
 (0)