Skip to content

Commit fee2df9

Browse files
committed
Also handle disabling keep-alive while in keep-alive
1 parent d6848e7 commit fee2df9

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

src/proto/conn.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,11 @@ where I: AsyncRead + AsyncWrite,
455455
}
456456

457457
pub fn disable_keep_alive(&mut self) {
458-
self.state.disable_keep_alive();
458+
if self.state.is_idle() {
459+
self.state.close_read();
460+
} else {
461+
self.state.disable_keep_alive();
462+
}
459463
}
460464
}
461465

tests/server.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,10 @@ fn disable_keep_alive_mid_request() {
562562

563563
let child = thread::spawn(move || {
564564
let mut req = connect(&addr);
565-
req.write_all(b"GET / HTTP/1.1\r\nConnection: keep-alive\r\n").unwrap();
565+
req.write_all(b"GET / HTTP/1.1\r\n").unwrap();
566566
tx1.send(()).unwrap();
567567
rx2.wait().unwrap();
568-
req.write_all(b"Host: localhost\r\nContent-Length: 0\r\n\r\n").unwrap();
568+
req.write_all(b"Host: localhost\r\n\r\n").unwrap();
569569
let mut buf = vec![];
570570
req.read_to_end(&mut buf).unwrap();
571571
});
@@ -595,6 +595,62 @@ fn disable_keep_alive_mid_request() {
595595
child.join().unwrap();
596596
}
597597

598+
#[test]
599+
fn disable_keep_alive_post_request() {
600+
let mut core = Core::new().unwrap();
601+
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
602+
let addr = listener.local_addr().unwrap();
603+
604+
let (tx1, rx1) = oneshot::channel();
605+
606+
let child = thread::spawn(move || {
607+
let mut req = connect(&addr);
608+
req.write_all(b"\
609+
GET / HTTP/1.1\r\n\
610+
Host: localhost\r\n\
611+
\r\n\
612+
").unwrap();
613+
614+
let mut buf = [0; 1024 * 8];
615+
loop {
616+
let n = req.read(&mut buf).expect("reading 1");
617+
if n < buf.len() {
618+
if &buf[n - HELLO.len()..n] == HELLO.as_bytes() {
619+
break;
620+
}
621+
}
622+
}
623+
624+
tx1.send(()).unwrap();
625+
626+
let nread = req.read(&mut buf).unwrap();
627+
assert_eq!(nread, 0);
628+
});
629+
630+
let fut = listener.incoming()
631+
.into_future()
632+
.map_err(|_| unreachable!())
633+
.and_then(|(item, _incoming)| {
634+
let (socket, _) = item.unwrap();
635+
Http::<hyper::Chunk>::new().serve_connection(socket, HelloWorld)
636+
.select2(rx1)
637+
.then(|r| {
638+
match r {
639+
Ok(Either::A(_)) => panic!("expected rx first"),
640+
Ok(Either::B(((), mut conn))) => {
641+
conn.disable_keep_alive();
642+
conn
643+
}
644+
Err(Either::A((e, _))) => panic!("unexpected error {}", e),
645+
Err(Either::B((e, _))) => panic!("unexpected error {}", e),
646+
}
647+
})
648+
});
649+
650+
core.run(fut).unwrap();
651+
child.join().unwrap();
652+
}
653+
598654
#[test]
599655
fn no_proto_empty_parse_eof_does_not_return_error() {
600656
let mut core = Core::new().unwrap();
@@ -763,6 +819,8 @@ impl Service for TestService {
763819

764820
}
765821

822+
const HELLO: &'static str = "hello";
823+
766824
struct HelloWorld;
767825

768826
impl Service for HelloWorld {
@@ -772,7 +830,10 @@ impl Service for HelloWorld {
772830
type Future = FutureResult<Self::Response, Self::Error>;
773831

774832
fn call(&self, _req: Request) -> Self::Future {
775-
future::ok(Response::new())
833+
let mut response = Response::new();
834+
response.headers_mut().set(hyper::header::ContentLength(HELLO.len() as u64));
835+
response.set_body(HELLO);
836+
future::ok(response)
776837
}
777838
}
778839

0 commit comments

Comments
 (0)