feat(server): add upgrade support to lower-level Connection API (#1459)
Closes #1323
This commit is contained in:
@@ -500,6 +500,8 @@ where I: AsyncRead + AsyncWrite,
|
||||
Ok(encoder) => {
|
||||
if !encoder.is_eof() {
|
||||
Writing::Body(encoder)
|
||||
} else if encoder.is_last() {
|
||||
Writing::Closed
|
||||
} else {
|
||||
Writing::KeepAlive
|
||||
}
|
||||
@@ -566,7 +568,11 @@ where I: AsyncRead + AsyncWrite,
|
||||
self.io.buffer(encoded);
|
||||
|
||||
if encoder.is_eof() {
|
||||
Writing::KeepAlive
|
||||
if encoder.is_last() {
|
||||
Writing::Closed
|
||||
} else {
|
||||
Writing::KeepAlive
|
||||
}
|
||||
} else {
|
||||
return Ok(AsyncSink::Ready);
|
||||
}
|
||||
@@ -577,7 +583,11 @@ where I: AsyncRead + AsyncWrite,
|
||||
if let Some(end) = end {
|
||||
self.io.buffer(end);
|
||||
}
|
||||
Writing::KeepAlive
|
||||
if encoder.is_last() {
|
||||
Writing::Closed
|
||||
} else {
|
||||
Writing::KeepAlive
|
||||
}
|
||||
},
|
||||
Err(_not_eof) => Writing::Closed,
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use iovec::IoVec;
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Encoder {
|
||||
kind: Kind,
|
||||
is_last: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -43,22 +44,22 @@ enum BufKind<B> {
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
pub fn chunked() -> Encoder {
|
||||
fn new(kind: Kind) -> Encoder {
|
||||
Encoder {
|
||||
kind: Kind::Chunked,
|
||||
kind: kind,
|
||||
is_last: false,
|
||||
}
|
||||
}
|
||||
pub fn chunked() -> Encoder {
|
||||
Encoder::new(Kind::Chunked)
|
||||
}
|
||||
|
||||
pub fn length(len: u64) -> Encoder {
|
||||
Encoder {
|
||||
kind: Kind::Length(len),
|
||||
}
|
||||
Encoder::new(Kind::Length(len))
|
||||
}
|
||||
|
||||
pub fn eof() -> Encoder {
|
||||
Encoder {
|
||||
kind: Kind::Eof,
|
||||
}
|
||||
Encoder::new(Kind::Eof)
|
||||
}
|
||||
|
||||
pub fn is_eof(&self) -> bool {
|
||||
@@ -68,6 +69,14 @@ impl Encoder {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_last(&mut self) {
|
||||
self.is_last = true;
|
||||
}
|
||||
|
||||
pub fn is_last(&self) -> bool {
|
||||
self.is_last
|
||||
}
|
||||
|
||||
pub fn end<B>(&self) -> Result<Option<EncodedBuf<B>>, NotEof> {
|
||||
match self.kind {
|
||||
Kind::Length(0) => Ok(None),
|
||||
|
||||
@@ -132,7 +132,11 @@ where
|
||||
// replying with the latter status code response.
|
||||
let ret = if ::StatusCode::SwitchingProtocols == head.subject {
|
||||
T::on_encode_upgrade(&mut head)
|
||||
.map(|_| Server::set_length(&mut head, has_body, method.as_ref()))
|
||||
.map(|_| {
|
||||
let mut enc = Server::set_length(&mut head, has_body, method.as_ref());
|
||||
enc.set_last();
|
||||
enc
|
||||
})
|
||||
} else if head.subject.is_informational() {
|
||||
error!("response with 1xx status code not supported");
|
||||
head = MessageHead::default();
|
||||
|
||||
@@ -134,7 +134,9 @@ pub fn expecting_continue(version: HttpVersion, headers: &Headers) -> bool {
|
||||
ret
|
||||
}
|
||||
|
||||
pub type ServerTransaction = h1::role::Server<h1::role::NoUpgrades>;
|
||||
pub type ServerTransaction = h1::role::Server<h1::role::YesUpgrades>;
|
||||
//pub type ServerTransaction = h1::role::Server<h1::role::NoUpgrades>;
|
||||
//pub type ServerUpgradeTransaction = h1::role::Server<h1::role::YesUpgrades>;
|
||||
|
||||
pub type ClientTransaction = h1::role::Client<h1::role::NoUpgrades>;
|
||||
pub type ClientUpgradeTransaction = h1::role::Client<h1::role::YesUpgrades>;
|
||||
|
||||
Reference in New Issue
Block a user