fix(lib): properly handle HTTP/1.0 remotes

- Downgrades internal semantics to HTTP/1.0 if peer sends a message with
  1.0 version.
- If downgraded, chunked writers become EOF writers, with the connection
  closing once the writing is complete.
- When downgraded, if keep-alive was wanted, the `Connection: keep-alive`
  header is added.

Closes #1304
This commit is contained in:
Sean McArthur
2018-01-22 10:08:27 -08:00
parent 7d493aafce
commit 36e66a5054
4 changed files with 258 additions and 44 deletions

View File

@@ -17,6 +17,11 @@ enum Kind {
///
/// Enforces that the body is not longer than the Content-Length header.
Length(u64),
/// An Encoder for when neither Content-Length nore Chunked encoding is set.
///
/// This is mostly only used with HTTP/1.0 with a length. This kind requires
/// the connection to be closed when the body is finished.
Eof
}
impl Encoder {
@@ -32,6 +37,12 @@ impl Encoder {
}
}
pub fn eof() -> Encoder {
Encoder {
kind: Kind::Eof,
}
}
pub fn is_eof(&self) -> bool {
match self.kind {
Kind::Length(0) |
@@ -40,7 +51,7 @@ impl Encoder {
}
}
pub fn eof(&self) -> Result<Option<&'static [u8]>, NotEof> {
pub fn end(&self) -> Result<Option<&'static [u8]>, NotEof> {
match self.kind {
Kind::Length(0) => Ok(None),
Kind::Chunked(Chunked::Init) => Ok(Some(b"0\r\n\r\n")),
@@ -73,6 +84,12 @@ impl Encoder {
trace!("encoded {} bytes, remaining = {}", n, remaining);
Ok(n)
},
Kind::Eof => {
if msg.is_empty() {
return Ok(0);
}
w.write_atomic(&[msg])
}
}
}
}