|
1 | 1 | //! A collection of traits abstracting over Listeners and Streams.
|
2 | 2 | use std::any::{Any, TypeId};
|
3 | 3 | use std::fmt;
|
4 |
| -use std::io::{self, Read, Write}; |
| 4 | +use std::io::{self, ErrorKind, Read, Write}; |
5 | 5 | use std::net::{SocketAddr, ToSocketAddrs, TcpStream, TcpListener, Shutdown};
|
6 | 6 | use std::mem;
|
7 | 7 | use std::path::Path;
|
@@ -292,10 +292,20 @@ impl NetworkStream for HttpStream {
|
292 | 292 |
|
293 | 293 | #[inline]
|
294 | 294 | fn close(&mut self, how: Shutdown) -> io::Result<()> {
|
| 295 | + #[inline] |
| 296 | + fn shutdown(tcp: &mut TcpStream, how: Shutdown) -> io::Result<()> { |
| 297 | + match tcp.shutdown(how) { |
| 298 | + Ok(_) => Ok(()), |
| 299 | + Err(ref e) if e.kind() == ErrorKind::NotConnected => Ok(()), |
| 300 | + err => err |
| 301 | + } |
| 302 | + } |
| 303 | + |
295 | 304 | match *self {
|
296 |
| - HttpStream::Http(ref mut inner) => inner.0.shutdown(how), |
297 |
| - HttpStream::Https(ref mut inner) => inner.get_mut().0.shutdown(how) |
| 305 | + HttpStream::Http(ref mut inner) => shutdown(&mut inner.0, how), |
| 306 | + HttpStream::Https(ref mut inner) => shutdown(&mut inner.get_mut().0, how) |
298 | 307 | }
|
| 308 | + |
299 | 309 | }
|
300 | 310 | }
|
301 | 311 |
|
@@ -347,8 +357,19 @@ fn lift_ssl_error(ssl: SslError) -> io::Error {
|
347 | 357 |
|
348 | 358 | #[cfg(test)]
|
349 | 359 | mod tests {
|
| 360 | + use std::net::{Shutdown, TcpStream}; |
| 361 | + |
350 | 362 | use mock::MockStream;
|
351 |
| - use super::NetworkStream; |
| 363 | + use super::{HttpStream, NetworkStream, CloneTcpStream}; |
| 364 | + |
| 365 | + #[test] |
| 366 | + fn test_shutdown() { |
| 367 | + let tcp = CloneTcpStream(TcpStream::connect("0.0.0.0:1111").unwrap()); |
| 368 | + let mut stream = HttpStream::Http(tcp); |
| 369 | + // see https://github.com/hyperium/hyper/issues/508 |
| 370 | + stream.close(Shutdown::Write).unwrap(); |
| 371 | + stream.close(Shutdown::Write).unwrap(); |
| 372 | + } |
352 | 373 |
|
353 | 374 | #[test]
|
354 | 375 | fn test_downcast_box_stream() {
|
|
0 commit comments