| 1 | use std::pin::Pin; |
|---|---|
| 2 | use std::task::{self, Poll}; |
| 3 | use std::{fmt, io}; |
| 4 | |
| 5 | use futures_util::TryFutureExt; |
| 6 | use hyper_util::rt::TokioIo; |
| 7 | use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; |
| 8 | |
| 9 | /// An upgraded HTTP connection. |
| 10 | pub struct Upgraded { |
| 11 | inner: TokioIo<hyper::upgrade::Upgraded>, |
| 12 | } |
| 13 | |
| 14 | impl AsyncRead for Upgraded { |
| 15 | fn poll_read( |
| 16 | mut self: Pin<&mut Self>, |
| 17 | cx: &mut task::Context<'_>, |
| 18 | buf: &mut ReadBuf<'_>, |
| 19 | ) -> Poll<io::Result<()>> { |
| 20 | Pin::new(&mut self.inner).poll_read(cx, buf) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl AsyncWrite for Upgraded { |
| 25 | fn poll_write( |
| 26 | mut self: Pin<&mut Self>, |
| 27 | cx: &mut task::Context<'_>, |
| 28 | buf: &[u8], |
| 29 | ) -> Poll<io::Result<usize>> { |
| 30 | Pin::new(&mut self.inner).poll_write(cx, buf) |
| 31 | } |
| 32 | |
| 33 | fn poll_write_vectored( |
| 34 | mut self: Pin<&mut Self>, |
| 35 | cx: &mut task::Context<'_>, |
| 36 | bufs: &[io::IoSlice<'_>], |
| 37 | ) -> Poll<io::Result<usize>> { |
| 38 | Pin::new(&mut self.inner).poll_write_vectored(cx, bufs) |
| 39 | } |
| 40 | |
| 41 | fn poll_flush(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> { |
| 42 | Pin::new(&mut self.inner).poll_flush(cx) |
| 43 | } |
| 44 | |
| 45 | fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> { |
| 46 | Pin::new(&mut self.inner).poll_shutdown(cx) |
| 47 | } |
| 48 | |
| 49 | fn is_write_vectored(&self) -> bool { |
| 50 | self.inner.is_write_vectored() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl fmt::Debug for Upgraded { |
| 55 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 56 | f.debug_struct(name:"Upgraded").finish() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl From<hyper::upgrade::Upgraded> for Upgraded { |
| 61 | fn from(inner: hyper::upgrade::Upgraded) -> Self { |
| 62 | Upgraded { |
| 63 | inner: TokioIo::new(inner), |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl super::response::Response { |
| 69 | /// Consumes the response and returns a future for a possible HTTP upgrade. |
| 70 | pub async fn upgrade(self) -> crate::Result<Upgraded> { |
| 71 | hyperMapErr |
| 72 | .map_ok(Upgraded::from) |
| 73 | .map_err(crate::error::upgrade) |
| 74 | .await |
| 75 | } |
| 76 | } |
| 77 |
