@@ -160,16 +160,19 @@ impl SimpleHttpTransport {
160160 if http_response. len ( ) < 12 {
161161 return Err ( Error :: HttpResponseTooShort { actual : http_response. len ( ) , needed : 12 } ) ;
162162 }
163+ if !http_response. as_bytes ( ) [ ..12 ] . is_ascii ( ) {
164+ return Err ( Error :: HttpResponseNonAsciiHello ( http_response. as_bytes ( ) [ ..12 ] . to_vec ( ) ) ) ;
165+ }
163166 if !http_response. starts_with ( "HTTP/1.1 " ) {
164167 return Err ( Error :: HttpResponseBadHello {
165- actual : String :: from_utf8_lossy ( & http_response. as_bytes ( ) [ 0 ..9 ] ) . into ( ) ,
168+ actual : http_response[ 0 ..9 ] . into ( ) ,
166169 expected : "HTTP/1.1 " . into ( ) ,
167170 } ) ;
168171 }
169172 let response_code = match http_response[ 9 ..12 ] . parse :: < u16 > ( ) {
170173 Ok ( n) => n,
171174 Err ( e) => return Err ( Error :: HttpResponseBadStatus (
172- String :: from_utf8_lossy ( & http_response. as_bytes ( ) [ 9 ..12 ] ) . into ( ) ,
175+ http_response[ 9 ..12 ] . into ( ) ,
173176 e,
174177 ) ) ,
175178 } ;
@@ -240,6 +243,8 @@ pub enum Error {
240243 /// Minimum length we can parse
241244 needed : usize ,
242245 } ,
246+ /// The HTTP response started with a HTTP/1.1 line which was not ASCII
247+ HttpResponseNonAsciiHello ( Vec < u8 > ) ,
243248 /// The HTTP response did not start with HTTP/1.1
244249 HttpResponseBadHello {
245250 /// Actual HTTP-whatever string
@@ -282,6 +287,9 @@ impl fmt::Display for Error {
282287 Error :: HttpResponseTooShort { ref actual, ref needed } => {
283288 write ! ( f, "HTTP response too short: length {}, needed {}." , actual, needed)
284289 } ,
290+ Error :: HttpResponseNonAsciiHello ( ref bytes) => {
291+ write ! ( f, "HTTP response started with non-ASCII {:?}" , bytes)
292+ } ,
285293 Error :: HttpResponseBadHello { ref actual, ref expected } => {
286294 write ! ( f, "HTTP response started with `{}`; expected `{}`." , actual, expected)
287295 } ,
@@ -308,6 +316,7 @@ impl error::Error for Error {
308316 ..
309317 }
310318 | HttpResponseTooShort { .. }
319+ | HttpResponseNonAsciiHello ( ..)
311320 | HttpResponseBadHello { .. }
312321 | HttpResponseBadStatus ( ..)
313322 | HttpResponseBadContentLength ( ..)
0 commit comments