Skip to content

Commit 1448e40

Browse files
lambdasqdseanmonstar
authored andcommitted
fix(server): properly handle keep-alive for HTTP/1.0
Change behaviour of connection or server response when the request is version 1.0 and the Connection: keep-alive header is not present. 1. If the response is also version 1.0, then connection is closed if the server keep-alive header is not present. 2. If the response is version 1.1, then the keep-alive header is added when downgrading to version 1.0. Closes #1614
1 parent bdbaa85 commit 1448e40

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/proto/h1/conn.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use std::marker::PhantomData;
55
use bytes::{Buf, Bytes};
66
use futures::{Async, Poll};
77
use http::{HeaderMap, Method, Version};
8+
use http::header::{HeaderValue, CONNECTION};
89
use tokio_io::{AsyncRead, AsyncWrite};
910

1011
use ::Chunk;
1112
use proto::{BodyLength, DecodedLength, MessageHead};
13+
use headers::connection_keep_alive;
1214
use super::io::{Buffered};
1315
use super::{EncodedBuf, Encode, Encoder, /*Decode,*/ Decoder, Http1Transaction, ParseContext};
1416

@@ -438,12 +440,38 @@ where I: AsyncRead + AsyncWrite,
438440
}
439441
}
440442

443+
// Fix keep-alives when Connection: keep-alive header is not present
444+
fn fix_keep_alive(&mut self, head: &mut MessageHead<T::Outgoing>) {
445+
let outgoing_is_keep_alive = head
446+
.headers
447+
.get(CONNECTION)
448+
.and_then(|value| Some(connection_keep_alive(value)))
449+
.unwrap_or(false);
450+
451+
if !outgoing_is_keep_alive {
452+
match head.version {
453+
// If response is version 1.0 and keep-alive is not present in the response,
454+
// disable keep-alive so the server closes the connection
455+
Version::HTTP_10 => self.state.disable_keep_alive(),
456+
// If response is version 1.1 and keep-alive is wanted, add
457+
// Connection: keep-alive header when not present
458+
Version::HTTP_11 => if self.state.wants_keep_alive() {
459+
head.headers
460+
.insert(CONNECTION, HeaderValue::from_static("keep-alive"));
461+
},
462+
_ => (),
463+
}
464+
}
465+
}
466+
441467
// If we know the remote speaks an older version, we try to fix up any messages
442468
// to work with our older peer.
443469
fn enforce_version(&mut self, head: &mut MessageHead<T::Outgoing>) {
444470

445471
match self.state.version {
446472
Version::HTTP_10 => {
473+
// Fixes response or connection when keep-alive header is not present
474+
self.fix_keep_alive(head);
447475
// If the remote only knows HTTP/1.0, we should force ourselves
448476
// to do only speak HTTP/1.0 as well.
449477
head.version = Version::HTTP_10;

tests/server.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tokio::reactor::Handle;
3030
use tokio_io::{AsyncRead, AsyncWrite};
3131

3232

33-
use hyper::{Body, Request, Response, StatusCode};
33+
use hyper::{Body, Request, Response, StatusCode, Version};
3434
use hyper::client::Client;
3535
use hyper::server::conn::Http;
3636
use hyper::server::Server;
@@ -637,6 +637,7 @@ fn keep_alive() {
637637
fn http_10_keep_alive() {
638638
let foo_bar = b"foo bar baz";
639639
let server = serve();
640+
// Response version 1.1 with no keep-alive header will downgrade to 1.0 when served
640641
server.reply()
641642
.header("content-length", foo_bar.len().to_string())
642643
.body(foo_bar);
@@ -658,6 +659,10 @@ fn http_10_keep_alive() {
658659
}
659660
}
660661

662+
// Connection: keep-alive header should be added when downgrading to a 1.0 response
663+
let response = String::from_utf8(buf.to_vec()).unwrap();
664+
response.contains("Connection: keep-alive\r\n");
665+
661666
// try again!
662667

663668
let quux = b"zar quux";
@@ -682,6 +687,69 @@ fn http_10_keep_alive() {
682687
}
683688
}
684689

690+
#[test]
691+
fn http_10_close_on_no_ka() {
692+
let foo_bar = b"foo bar baz";
693+
let server = serve();
694+
695+
// A server response with version 1.0 but no keep-alive header
696+
server
697+
.reply()
698+
.version(Version::HTTP_10)
699+
.header("content-length", foo_bar.len().to_string())
700+
.body(foo_bar);
701+
let mut req = connect(server.addr());
702+
703+
// The client request with version 1.0 that may have the keep-alive header
704+
req.write_all(
705+
b"\
706+
GET / HTTP/1.0\r\n\
707+
Host: example.domain\r\n\
708+
Connection: keep-alive\r\n\
709+
\r\n\
710+
",
711+
).expect("writing 1");
712+
713+
let mut buf = [0; 1024 * 8];
714+
loop {
715+
let n = req.read(&mut buf[..]).expect("reading 1");
716+
if n < buf.len() {
717+
if &buf[n - foo_bar.len()..n] == foo_bar {
718+
break;
719+
} else {
720+
}
721+
}
722+
}
723+
724+
// try again!
725+
726+
let quux = b"zar quux";
727+
server
728+
.reply()
729+
.header("content-length", quux.len().to_string())
730+
.body(quux);
731+
732+
// the write can possibly succeed, since it fills the kernel buffer on the first write
733+
let _ = req.write_all(
734+
b"\
735+
GET /quux HTTP/1.1\r\n\
736+
Host: example.domain\r\n\
737+
Connection: close\r\n\
738+
\r\n\
739+
",
740+
);
741+
742+
let mut buf = [0; 1024 * 8];
743+
match req.read(&mut buf[..]) {
744+
// Ok(0) means EOF, so a proper shutdown
745+
// Err(_) could mean ConnReset or something, also fine
746+
Ok(0) | Err(_) => {}
747+
Ok(n) => {
748+
panic!("read {} bytes on a disabled keep-alive socket", n);
749+
}
750+
}
751+
}
752+
685753
#[test]
686754
fn disable_keep_alive() {
687755
let foo_bar = b"foo bar baz";

0 commit comments

Comments
 (0)