@@ -128,7 +128,7 @@ impl Headers {
128
128
// Headers must be ASCII, so also UTF-8 valid.
129
129
match std:: str:: from_utf8 ( header_line) {
130
130
Ok ( headers_str) => {
131
- let entry = headers_str. split ( ": " ) . collect :: < Vec < & str > > ( ) ;
131
+ let entry = headers_str. splitn ( 2 , ':' ) . collect :: < Vec < & str > > ( ) ;
132
132
if entry. len ( ) != 2 {
133
133
return Err ( RequestError :: InvalidHeader ) ;
134
134
}
@@ -378,9 +378,10 @@ mod tests {
378
378
assert_eq ! ( headers. content_length, 55 ) ;
379
379
assert_eq ! ( headers. accept, MediaType :: ApplicationJson ) ;
380
380
381
- // Valid headers.
381
+ // Valid headers. (${HEADER_NAME} : WHITESPACE ${HEADER_VALUE})
382
+ // Any number of whitespace characters should be accepted including zero.
382
383
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 "
384
385
)
385
386
. unwrap ( ) ;
386
387
assert_eq ! ( headers. content_length, 49 ) ;
@@ -481,6 +482,47 @@ mod tests {
481
482
. is_err( ) ) ;
482
483
}
483
484
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
+
484
526
#[ test]
485
527
fn test_header_try_from ( ) {
486
528
// Bad header.
0 commit comments