mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-14 11:23:44 +00:00
http: takeover MaybeTlsStream from proxmox_backup
this is just a (rather HTTP specific) wrapper, so put it into a 'wrapper' module for now. Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
parent
caef9a3b20
commit
6c5028512b
@ -18,11 +18,13 @@ futures = { version = "0.3", optional = true }
|
|||||||
hyper = { version = "0.14", features = [ "full" ], optional = true }
|
hyper = { version = "0.14", features = [ "full" ], optional = true }
|
||||||
openssl = { version = "0.10", optional = true }
|
openssl = { version = "0.10", optional = true }
|
||||||
tokio = { version = "1.0", features = [], optional = true }
|
tokio = { version = "1.0", features = [], optional = true }
|
||||||
|
tokio-openssl = { version = "0.6.1", optional = true }
|
||||||
|
|
||||||
proxmox = { path = "../proxmox", optional = true, version = "0.11.3", default-features = false }
|
proxmox = { path = "../proxmox", optional = true, version = "0.11.3", default-features = false }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|
||||||
client = []
|
client = [ "http-helpers" ]
|
||||||
|
http-helpers = [ "hyper", "tokio/io-util", "tokio-openssl" ]
|
||||||
websocket = [ "base64", "futures", "hyper", "openssl", "proxmox/tokio", "tokio/io-util", "tokio/sync" ]
|
websocket = [ "base64", "futures", "hyper", "openssl", "proxmox/tokio", "tokio/io-util", "tokio/sync" ]
|
||||||
|
3
proxmox-http/src/http/mod.rs
Normal file
3
proxmox-http/src/http/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
mod wrapper;
|
||||||
|
|
||||||
|
pub use wrapper::MaybeTlsStream;
|
122
proxmox-http/src/http/wrapper.rs
Normal file
122
proxmox-http/src/http/wrapper.rs
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
use hyper::client::connect::{Connection, Connected};
|
||||||
|
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
||||||
|
use tokio_openssl::SslStream;
|
||||||
|
|
||||||
|
/// Asynchronous stream, possibly encrypted and proxied
|
||||||
|
///
|
||||||
|
/// Usefule for HTTP client implementations using hyper.
|
||||||
|
pub enum MaybeTlsStream<S> {
|
||||||
|
Normal(S),
|
||||||
|
Proxied(S),
|
||||||
|
Secured(SslStream<S>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for MaybeTlsStream<S> {
|
||||||
|
fn poll_read(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context,
|
||||||
|
buf: &mut ReadBuf,
|
||||||
|
) -> Poll<Result<(), io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Normal(ref mut s) => {
|
||||||
|
Pin::new(s).poll_read(cx, buf)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Proxied(ref mut s) => {
|
||||||
|
Pin::new(s).poll_read(cx, buf)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Secured(ref mut s) => {
|
||||||
|
Pin::new(s).poll_read(cx, buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for MaybeTlsStream<S> {
|
||||||
|
fn poll_write(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context,
|
||||||
|
buf: &[u8],
|
||||||
|
) -> Poll<Result<usize, io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Normal(ref mut s) => {
|
||||||
|
Pin::new(s).poll_write(cx, buf)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Proxied(ref mut s) => {
|
||||||
|
Pin::new(s).poll_write(cx, buf)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Secured(ref mut s) => {
|
||||||
|
Pin::new(s).poll_write(cx, buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_write_vectored(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
bufs: &[io::IoSlice<'_>],
|
||||||
|
) -> Poll<Result<usize, io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Normal(ref mut s) => {
|
||||||
|
Pin::new(s).poll_write_vectored(cx, bufs)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Proxied(ref mut s) => {
|
||||||
|
Pin::new(s).poll_write_vectored(cx, bufs)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Secured(ref mut s) => {
|
||||||
|
Pin::new(s).poll_write_vectored(cx, bufs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_write_vectored(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
MaybeTlsStream::Normal(s) => s.is_write_vectored(),
|
||||||
|
MaybeTlsStream::Proxied(s) => s.is_write_vectored(),
|
||||||
|
MaybeTlsStream::Secured(s) => s.is_write_vectored(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Normal(ref mut s) => {
|
||||||
|
Pin::new(s).poll_flush(cx)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Proxied(ref mut s) => {
|
||||||
|
Pin::new(s).poll_flush(cx)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Secured(ref mut s) => {
|
||||||
|
Pin::new(s).poll_flush(cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
MaybeTlsStream::Normal(ref mut s) => {
|
||||||
|
Pin::new(s).poll_shutdown(cx)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Proxied(ref mut s) => {
|
||||||
|
Pin::new(s).poll_shutdown(cx)
|
||||||
|
}
|
||||||
|
MaybeTlsStream::Secured(ref mut s) => {
|
||||||
|
Pin::new(s).poll_shutdown(cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// we need this for the hyper http client
|
||||||
|
impl <S: Connection + AsyncRead + AsyncWrite + Unpin> Connection for MaybeTlsStream<S>
|
||||||
|
{
|
||||||
|
fn connected(&self) -> Connected {
|
||||||
|
match self {
|
||||||
|
MaybeTlsStream::Normal(s) => s.connected(),
|
||||||
|
MaybeTlsStream::Proxied(s) => s.connected().proxy(true),
|
||||||
|
MaybeTlsStream::Secured(s) => s.get_ref().connected(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,5 @@
|
|||||||
#[cfg(feature = "websocket")]
|
#[cfg(feature = "websocket")]
|
||||||
pub mod websocket;
|
pub mod websocket;
|
||||||
|
|
||||||
|
#[cfg(feature = "http-helpers")]
|
||||||
|
pub mod http;
|
||||||
|
Loading…
Reference in New Issue
Block a user