Skip to content

Commit 385afca

Browse files
wt-l00dianpopa
authored andcommitted
Add more flexibility when parsing the headers
Any number of whitespace characters are accepted after ":" when parsing HTTP headers. Create a test function (Add test_parse_header_whitespace). This test addresses parsing header and allow no space and any amount of linear white space after ":". And modified CHANGELOG.md. Signed-off-by: Eisuke Matsushita <[email protected]>
1 parent cf29e0f commit 385afca

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
- Added metrics for the vsock device.
2020
- Added devtool strip command which removes debug symbols from the release
2121
binaries.
22+
- Any number of whitespace characters are accepted after ":" when parsing HTTP
23+
headers.
2224

2325
### Fixed
2426

src/micro_http/src/common/headers.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Headers {
128128
// Headers must be ASCII, so also UTF-8 valid.
129129
match std::str::from_utf8(header_line) {
130130
Ok(headers_str) => {
131-
let entry = headers_str.split(": ").collect::<Vec<&str>>();
131+
let entry = headers_str.splitn(2, ':').collect::<Vec<&str>>();
132132
if entry.len() != 2 {
133133
return Err(RequestError::InvalidHeader);
134134
}
@@ -378,9 +378,10 @@ mod tests {
378378
assert_eq!(headers.content_length, 55);
379379
assert_eq!(headers.accept, MediaType::ApplicationJson);
380380

381-
// Valid headers.
381+
// Valid headers. (${HEADER_NAME} : WHITESPACE ${HEADER_VALUE})
382+
// Any number of whitespace characters should be accepted including zero.
382383
let headers = Headers::try_from(
383-
b"Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT\r\nAccept: text/plain\r\nContent-Length: 49\r\n\r\n"
384+
b"Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT\r\nAccept:text/plain\r\nContent-Length: 49\r\n\r\n"
384385
)
385386
.unwrap();
386387
assert_eq!(headers.content_length, 49);
@@ -472,6 +473,47 @@ mod tests {
472473
.is_err());
473474
}
474475

476+
#[test]
477+
fn test_parse_header_whitespace() {
478+
let mut header = Headers::default();
479+
// Test that any number of whitespace characters are accepted before the header value.
480+
// For Content-Length
481+
assert!(header.parse_header_line(b"Content-Length:24").is_ok());
482+
assert!(header.parse_header_line(b"Content-Length: 24").is_ok());
483+
484+
// For ContentType
485+
assert!(header
486+
.parse_header_line(b"Content-Type:application/json")
487+
.is_ok());
488+
assert!(header
489+
.parse_header_line(b"Content-Type: application/json")
490+
.is_ok());
491+
492+
// For Accept
493+
assert!(header.parse_header_line(b"Accept:application/json").is_ok());
494+
assert!(header
495+
.parse_header_line(b"Accept: application/json")
496+
.is_ok());
497+
498+
// For Transfer-Encoding
499+
assert!(header
500+
.parse_header_line(b"Transfer-Encoding:chunked")
501+
.is_ok());
502+
assert!(header.chunked());
503+
assert!(header
504+
.parse_header_line(b"Transfer-Encoding: chunked")
505+
.is_ok());
506+
assert!(header.chunked());
507+
508+
// For Server
509+
assert!(header.parse_header_line(b"Server:xxx.yyy.zzz").is_ok());
510+
assert!(header.parse_header_line(b"Server: xxx.yyy.zzz").is_ok());
511+
512+
// For Expect
513+
assert!(header.parse_header_line(b"Expect:100-continue").is_ok());
514+
assert!(header.parse_header_line(b"Expect: 100-continue").is_ok());
515+
}
516+
475517
#[test]
476518
fn test_header_try_from() {
477519
// Bad header.

0 commit comments

Comments
 (0)