use bytes::Bytes;
use http_body_util::combinators::UnsyncBoxBody;
use http_body_util::{BodyExt, Full};
use osproxy_core::EndpointKind;
use osproxy_spi::{HttpMethod, Protocol};
pub type ResponseBody = UnsyncBoxBody<Bytes, Box<dyn std::error::Error + Send + Sync>>;
#[must_use]
pub fn buffered_response(body: Vec<u8>) -> ResponseBody {
Full::new(Bytes::from(body))
.map_err(|never| match never {})
.boxed_unsync()
}
pub struct StreamingResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: ResponseBody,
}
impl std::fmt::Debug for StreamingResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StreamingResponse")
.field("status", &self.status)
.field("headers", &self.headers)
.finish_non_exhaustive()
}
}
impl StreamingResponse {
#[must_use]
pub fn stream(status: u16, body: ResponseBody) -> Self {
Self {
status,
headers: Vec::new(),
body,
}
}
#[must_use]
pub fn buffered(status: u16, body: Vec<u8>) -> Self {
Self {
status,
headers: Vec::new(),
body: buffered_response(body),
}
}
#[must_use]
pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.push((name.into(), value.into()));
self
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct IngressRequest {
pub method: HttpMethod,
pub protocol: Protocol,
pub path: String,
pub endpoint: EndpointKind,
pub logical_index: String,
pub doc_id: Option<String>,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
pub query: Option<String>,
pub client_cert_subject: Option<String>,
pub secure: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct IngressResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
impl IngressResponse {
#[must_use]
pub fn json(status: u16, body: Vec<u8>) -> Self {
Self {
status,
headers: Vec::new(),
body,
}
}
#[must_use]
pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.push((name.into(), value.into()));
self
}
}