gproxy_channel_api/
transport.rs1use bytes::Bytes;
4
5#[derive(Debug, Clone, Copy, Default)]
21pub struct TransportOptions {
22 pub total_timeout: Option<std::time::Duration>,
23 pub max_redirects: Option<usize>,
24 pub http_version: Option<http::Version>,
25 pub omit_body: bool,
26}
27
28pub trait ByteStreamDecoder: Send {
30 fn push(&mut self, chunk: &[u8]) -> Vec<u8>;
32
33 fn finish(&mut self) -> Vec<u8>;
35}
36
37#[derive(Debug, thiserror::Error)]
39pub enum ClientError {
40 #[error("upstream transport error: {0}")]
41 Transport(String),
42 #[error("upstream client config error: {0}")]
45 Config(String),
46}
47
48#[cfg(not(target_arch = "wasm32"))]
51pub type RespStream =
52 std::pin::Pin<Box<dyn futures_core::Stream<Item = Result<Bytes, ClientError>> + Send>>;
53#[cfg(target_arch = "wasm32")]
54pub type RespStream =
55 std::pin::Pin<Box<dyn futures_core::Stream<Item = Result<Bytes, ClientError>>>>;
56
57#[cfg(not(target_arch = "wasm32"))]
59#[async_trait::async_trait]
60pub trait ConduitSocket: Send {
61 async fn send_text(&mut self, text: String) -> Result<(), ClientError>;
62 async fn recv_text(&mut self) -> Option<Result<String, ClientError>>;
63}
64
65#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
69#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
70pub trait UpstreamClient: Send + Sync {
71 async fn send(&self, req: http::Request<Bytes>) -> Result<http::Response<Bytes>, ClientError>;
72
73 async fn send_websocket(
74 &self,
75 _req: http::Request<Bytes>,
76 ) -> Result<http::Response<Bytes>, ClientError> {
77 Err(ClientError::Config(
78 "upstream websocket not supported by this client".into(),
79 ))
80 }
81
82 async fn send_streaming(
83 &self,
84 req: http::Request<Bytes>,
85 ) -> Result<(http::StatusCode, http::HeaderMap, RespStream), ClientError> {
86 let resp = self.send(req).await?;
87 let (parts, body) = resp.into_parts();
88 let once = futures_util::stream::once(async move { Ok::<Bytes, ClientError>(body) });
89 Ok((parts.status, parts.headers, Box::pin(once)))
90 }
91
92 #[cfg(not(target_arch = "wasm32"))]
93 async fn open_websocket(
94 &self,
95 _req: http::Request<Bytes>,
96 ) -> Result<Box<dyn ConduitSocket>, ClientError> {
97 Err(ClientError::Config(
98 "upstream websocket not supported by this client".into(),
99 ))
100 }
101}