@@ -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 \n Accept: text/plain\r \n Content-Length: 49\r \n \r \n "
384+ b"Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT\r \n Accept:text/plain\r \n Content-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