Skip to content

Commit 91e83b7

Browse files
authored
refactor(server): server::conn::http1::Connection always holds a proto (#3018)
* server::conn::http1::Connection always holds a proto * Remove `Connection::try_into_parts` as it can't fail
1 parent 0766d3f commit 91e83b7

File tree

4 files changed

+24
-55
lines changed

4 files changed

+24
-55
lines changed

src/body/body.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ impl Sender {
342342
/// This is mostly useful for when trying to send from some other thread
343343
/// that doesn't have an async context. If in an async context, prefer
344344
/// `send_data()` instead.
345+
#[cfg(feature = "http1")]
345346
pub(crate) fn try_send_data(&mut self, chunk: Bytes) -> Result<(), Bytes> {
346347
self.data_tx
347348
.try_send(Ok(chunk))
@@ -447,7 +448,7 @@ mod tests {
447448
assert!(err.is_body_write_aborted(), "{:?}", err);
448449
}
449450

450-
#[cfg(not(miri))]
451+
#[cfg(all(not(miri), feature = "http1"))]
451452
#[tokio::test]
452453
async fn channel_abort_when_buffer_is_full() {
453454
let (mut tx, mut rx) = Recv::channel();
@@ -463,6 +464,7 @@ mod tests {
463464
assert!(err.is_body_write_aborted(), "{:?}", err);
464465
}
465466

467+
#[cfg(feature = "http1")]
466468
#[test]
467469
fn channel_buffers_one() {
468470
let (mut tx, _rx) = Recv::channel();

src/body/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub use http_body::SizeHint;
2121

2222
pub use self::aggregate::aggregate;
2323
pub use self::body::Recv;
24+
#[cfg(feature = "http1")]
2425
pub(crate) use self::body::Sender;
2526
pub(crate) use self::length::DecodedLength;
2627
pub use self::to_bytes::to_bytes;

src/error.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ pub(super) enum User {
110110
#[cfg(feature = "http1")]
111111
ManualUpgrade,
112112

113-
/// User called `server::Connection::without_shutdown()` on an HTTP/2 conn.
114-
#[cfg(feature = "server")]
115-
WithoutShutdownNonHttp1,
116-
117113
/// User aborted in an FFI callback.
118114
#[cfg(feature = "ffi")]
119115
AbortedByCallback,
@@ -308,11 +304,6 @@ impl Error {
308304
Error::new_user(User::Body).with(cause)
309305
}
310306

311-
#[cfg(feature = "server")]
312-
pub(super) fn new_without_shutdown_not_h1() -> Error {
313-
Error::new(Kind::User(User::WithoutShutdownNonHttp1))
314-
}
315-
316307
#[cfg(feature = "http1")]
317308
pub(super) fn new_shutdown(cause: std::io::Error) -> Error {
318309
Error::new(Kind::Shutdown).with(cause)
@@ -399,10 +390,6 @@ impl Error {
399390
Kind::User(User::NoUpgrade) => "no upgrade available",
400391
#[cfg(feature = "http1")]
401392
Kind::User(User::ManualUpgrade) => "upgrade expected but low level API in use",
402-
#[cfg(feature = "server")]
403-
Kind::User(User::WithoutShutdownNonHttp1) => {
404-
"without_shutdown() called on a non-HTTP/1 connection"
405-
}
406393
#[cfg(feature = "ffi")]
407394
Kind::User(User::AbortedByCallback) => "operation aborted by an application callback",
408395
}

src/server/conn/http1.rs

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pin_project_lite::pin_project! {
2727
where
2828
S: HttpService<Recv>,
2929
{
30-
conn: Option<Http1Dispatcher<T, S::ResBody, S>>,
30+
conn: Http1Dispatcher<T, S::ResBody, S>,
3131
}
3232
}
3333

@@ -98,12 +98,7 @@ where
9898
/// pending. If called after `Connection::poll` has resolved, this does
9999
/// nothing.
100100
pub fn graceful_shutdown(mut self: Pin<&mut Self>) {
101-
match self.conn {
102-
Some(ref mut h1) => {
103-
h1.disable_keep_alive();
104-
}
105-
None => (),
106-
}
101+
self.conn.disable_keep_alive();
107102
}
108103

109104
/// Return the inner IO object, and additional information.
@@ -116,25 +111,13 @@ where
116111
/// # Panics
117112
/// This method will panic if this connection is using an h2 protocol.
118113
pub fn into_parts(self) -> Parts<I, S> {
119-
self.try_into_parts()
120-
.unwrap_or_else(|| panic!("h2 cannot into_inner"))
121-
}
122-
123-
/// Return the inner IO object, and additional information, if available.
124-
///
125-
///
126-
/// TODO:(mike) does this need to return none for h1 or is it expected to always be present? previously used an "unwrap"
127-
/// This method will return a `None` if this connection is using an h2 protocol.
128-
pub fn try_into_parts(self) -> Option<Parts<I, S>> {
129-
self.conn.map(|h1| {
130-
let (io, read_buf, dispatch) = h1.into_inner();
131-
Parts {
132-
io,
133-
read_buf,
134-
service: dispatch.into_service(),
135-
_inner: (),
136-
}
137-
})
114+
let (io, read_buf, dispatch) = self.conn.into_inner();
115+
Parts {
116+
io,
117+
read_buf,
118+
service: dispatch.into_service(),
119+
_inner: (),
120+
}
138121
}
139122

140123
/// Poll the connection for completion, but without calling `shutdown`
@@ -150,7 +133,7 @@ where
150133
S::Future: Unpin,
151134
B: Unpin,
152135
{
153-
self.conn.as_mut().unwrap().poll_without_shutdown(cx)
136+
self.conn.poll_without_shutdown(cx)
154137
}
155138

156139
/// Prevent shutdown of the underlying IO object at the end of service the request,
@@ -165,15 +148,11 @@ where
165148
S::Future: Unpin,
166149
B: Unpin,
167150
{
168-
// TODO(mike): "new_without_shutdown_not_h1" is not possible here
169-
let mut conn = Some(self);
151+
let mut zelf = Some(self);
170152
futures_util::future::poll_fn(move |cx| {
171-
ready!(conn.as_mut().unwrap().poll_without_shutdown(cx))?;
153+
ready!(zelf.as_mut().unwrap().conn.poll_without_shutdown(cx))?;
172154
Poll::Ready(
173-
conn.take()
174-
.unwrap()
175-
.try_into_parts()
176-
.ok_or_else(crate::Error::new_without_shutdown_not_h1),
155+
Ok(zelf.take().unwrap().into_parts())
177156
)
178157
})
179158
}
@@ -185,7 +164,7 @@ where
185164
where
186165
I: Send,
187166
{
188-
upgrades::UpgradeableConnection { inner: self }
167+
upgrades::UpgradeableConnection { inner: Some(self) }
189168
}
190169
}
191170

@@ -201,7 +180,7 @@ where
201180
type Output = crate::Result<()>;
202181

203182
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
204-
match ready!(Pin::new(self.conn.as_mut().unwrap()).poll(cx)) {
183+
match ready!(Pin::new(&mut self.conn).poll(cx)) {
205184
Ok(done) => {
206185
match done {
207186
proto::Dispatched::Shutdown => {}
@@ -417,7 +396,7 @@ impl Builder {
417396
let sd = proto::h1::dispatch::Server::new(service);
418397
let proto = proto::h1::Dispatcher::new(sd, conn);
419398
Connection {
420-
conn: Some(proto),
399+
conn: proto,
421400
}
422401
}
423402
}
@@ -436,7 +415,7 @@ mod upgrades {
436415
where
437416
S: HttpService<Recv>,
438417
{
439-
pub(super) inner: Connection<T, S>,
418+
pub(super) inner: Option<Connection<T, S>>,
440419
}
441420

442421
impl<I, B, S> UpgradeableConnection<I, S>
@@ -452,7 +431,7 @@ mod upgrades {
452431
/// This `Connection` should continue to be polled until shutdown
453432
/// can finish.
454433
pub fn graceful_shutdown(mut self: Pin<&mut Self>) {
455-
Pin::new(&mut self.inner).graceful_shutdown()
434+
Pin::new(self.inner.as_mut().unwrap()).graceful_shutdown()
456435
}
457436
}
458437

@@ -467,10 +446,10 @@ mod upgrades {
467446
type Output = crate::Result<()>;
468447

469448
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
470-
match ready!(Pin::new(self.inner.conn.as_mut().unwrap()).poll(cx)) {
449+
match ready!(Pin::new(&mut self.inner.as_mut().unwrap().conn).poll(cx)) {
471450
Ok(proto::Dispatched::Shutdown) => Poll::Ready(Ok(())),
472451
Ok(proto::Dispatched::Upgrade(pending)) => {
473-
let (io, buf, _) = self.inner.conn.take().unwrap().into_inner();
452+
let (io, buf, _) = self.inner.take().unwrap().conn.into_inner();
474453
pending.fulfill(Upgraded::new(io, buf));
475454
Poll::Ready(Ok(()))
476455
}

0 commit comments

Comments
 (0)