Skip to content

Commit c5a3f9f

Browse files
wt-l00acatangiu
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]> Signed-off-by: YUAN LYU <[email protected]>
1 parent 3e88863 commit c5a3f9f

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

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);
@@ -481,6 +482,47 @@ mod tests {
481482
.is_err());
482483
}
483484

485+
#[test]
486+
fn test_parse_header_whitespace() {
487+
let mut header = Headers::default();
488+
// Test that any number of whitespace characters are accepted before the header value.
489+
// For Content-Length
490+
assert!(header.parse_header_line(b"Content-Length:24").is_ok());
491+
assert!(header.parse_header_line(b"Content-Length: 24").is_ok());
492+
493+
// For ContentType
494+
assert!(header
495+
.parse_header_line(b"Content-Type:application/json")
496+
.is_ok());
497+
assert!(header
498+
.parse_header_line(b"Content-Type: application/json")
499+
.is_ok());
500+
501+
// For Accept
502+
assert!(header.parse_header_line(b"Accept:application/json").is_ok());
503+
assert!(header
504+
.parse_header_line(b"Accept: application/json")
505+
.is_ok());
506+
507+
// For Transfer-Encoding
508+
assert!(header
509+
.parse_header_line(b"Transfer-Encoding:chunked")
510+
.is_ok());
511+
assert!(header.chunked());
512+
assert!(header
513+
.parse_header_line(b"Transfer-Encoding: chunked")
514+
.is_ok());
515+
assert!(header.chunked());
516+
517+
// For Server
518+
assert!(header.parse_header_line(b"Server:xxx.yyy.zzz").is_ok());
519+
assert!(header.parse_header_line(b"Server: xxx.yyy.zzz").is_ok());
520+
521+
// For Expect
522+
assert!(header.parse_header_line(b"Expect:100-continue").is_ok());
523+
assert!(header.parse_header_line(b"Expect: 100-continue").is_ok());
524+
}
525+
484526
#[test]
485527
fn test_header_try_from() {
486528
// Bad header.

0 commit comments

Comments
 (0)