@@ -160,16 +160,19 @@ impl SimpleHttpTransport {
160
160
if http_response. len ( ) < 12 {
161
161
return Err ( Error :: HttpResponseTooShort { actual : http_response. len ( ) , needed : 12 } ) ;
162
162
}
163
+ if !http_response. as_bytes ( ) [ ..12 ] . is_ascii ( ) {
164
+ return Err ( Error :: HttpResponseNonAsciiHello ( http_response. as_bytes ( ) [ ..12 ] . to_vec ( ) ) ) ;
165
+ }
163
166
if !http_response. starts_with ( "HTTP/1.1 " ) {
164
167
return Err ( Error :: HttpResponseBadHello {
165
- actual : String :: from_utf8_lossy ( & http_response. as_bytes ( ) [ 0 ..9 ] ) . into ( ) ,
168
+ actual : http_response[ 0 ..9 ] . into ( ) ,
166
169
expected : "HTTP/1.1 " . into ( ) ,
167
170
} ) ;
168
171
}
169
172
let response_code = match http_response[ 9 ..12 ] . parse :: < u16 > ( ) {
170
173
Ok ( n) => n,
171
174
Err ( e) => return Err ( Error :: HttpResponseBadStatus (
172
- String :: from_utf8_lossy ( & http_response. as_bytes ( ) [ 9 ..12 ] ) . into ( ) ,
175
+ http_response[ 9 ..12 ] . into ( ) ,
173
176
e,
174
177
) ) ,
175
178
} ;
@@ -240,6 +243,8 @@ pub enum Error {
240
243
/// Minimum length we can parse
241
244
needed : usize ,
242
245
} ,
246
+ /// The HTTP response started with a HTTP/1.1 line which was not ASCII
247
+ HttpResponseNonAsciiHello ( Vec < u8 > ) ,
243
248
/// The HTTP response did not start with HTTP/1.1
244
249
HttpResponseBadHello {
245
250
/// Actual HTTP-whatever string
@@ -282,6 +287,9 @@ impl fmt::Display for Error {
282
287
Error :: HttpResponseTooShort { ref actual, ref needed } => {
283
288
write ! ( f, "HTTP response too short: length {}, needed {}." , actual, needed)
284
289
} ,
290
+ Error :: HttpResponseNonAsciiHello ( ref bytes) => {
291
+ write ! ( f, "HTTP response started with non-ASCII {:?}" , bytes)
292
+ } ,
285
293
Error :: HttpResponseBadHello { ref actual, ref expected } => {
286
294
write ! ( f, "HTTP response started with `{}`; expected `{}`." , actual, expected)
287
295
} ,
@@ -308,6 +316,7 @@ impl error::Error for Error {
308
316
..
309
317
}
310
318
| HttpResponseTooShort { .. }
319
+ | HttpResponseNonAsciiHello ( ..)
311
320
| HttpResponseBadHello { .. }
312
321
| HttpResponseBadStatus ( ..)
313
322
| HttpResponseBadContentLength ( ..)
0 commit comments