use crate::api::error::NetError;
use crate::api::http::http_response::HttpRequestId;
use bytes::Bytes;
use futures_util::Stream;
use http::{HeaderMap, StatusCode};
use std::pin::Pin;
#[cfg(feature = "http-client-http2")]
use std::sync::{Arc, Mutex};
pub type HttpByteStream = Pin<Box<dyn Stream<Item = Result<Bytes, NetError>> + Send + 'static>>;
pub struct HttpStreamResponse {
pub status: StatusCode,
pub headers: HeaderMap,
pub request_id: HttpRequestId,
pub attempts: u32,
pub stream: HttpByteStream,
#[cfg(feature = "http-client-http2")]
info: crate::api::http::HttpResponseInfo,
#[cfg(feature = "http-client-http2")]
flow_control: Arc<Mutex<crate::api::http::Http2FlowControlSnapshot>>,
}
impl HttpStreamResponse {
pub fn new(
status: StatusCode,
headers: HeaderMap,
request_id: HttpRequestId,
attempts: u32,
stream: HttpByteStream,
) -> Self {
#[cfg(feature = "http-client-http2")]
let flow_control = Arc::new(Mutex::new(
crate::api::http::Http2FlowControlSnapshot::default(),
));
#[cfg(feature = "http-client-http2")]
let stream = counted_stream(stream, Arc::clone(&flow_control));
Self {
status,
headers,
request_id,
attempts,
stream,
#[cfg(feature = "http-client-http2")]
info: crate::api::http::HttpResponseInfo::default(),
#[cfg(feature = "http-client-http2")]
flow_control,
}
}
#[cfg(feature = "http-client-http2")]
pub(crate) fn with_info(mut self, info: crate::api::http::HttpResponseInfo) -> Self {
if let Some(snapshot) = info.flow_control {
if let Ok(mut current) = self.flow_control.lock() {
*current = snapshot;
}
}
self.info = info;
self
}
#[cfg(feature = "http-client-http2")]
pub fn info(&self) -> &crate::api::http::HttpResponseInfo {
&self.info
}
#[cfg(feature = "http-client-http2")]
pub fn flow_control(&self) -> Option<crate::api::http::Http2FlowControlSnapshot> {
self.info.flow_control.map(|_| {
self.flow_control
.lock()
.map(|snapshot| *snapshot)
.unwrap_or_default()
})
}
pub fn content_type(&self) -> Option<&str> {
self.headers
.get(http::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
}
pub async fn next_chunk(&mut self) -> Option<Result<Bytes, NetError>> {
use futures_util::StreamExt;
self.stream.next().await
}
pub fn check_status(&self, policy: crate::api::http::HttpStatusPolicy) -> Result<(), NetError> {
if policy.accepts(self.status) {
Ok(())
} else {
Err(NetError::from_http_status(self.status))
}
}
}
#[cfg(feature = "http-client-http2")]
struct CountedStream {
inner: HttpByteStream,
state: Arc<Mutex<crate::api::http::Http2FlowControlSnapshot>>,
}
#[cfg(feature = "http-client-http2")]
impl futures_util::Stream for CountedStream {
type Item = Result<Bytes, NetError>;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
let this = self.get_mut();
let result = this.inner.as_mut().poll_next(cx);
if let std::task::Poll::Ready(Some(Ok(bytes))) = &result {
if let Ok(mut snapshot) = this.state.lock() {
snapshot.body_bytes_delivered = snapshot
.body_bytes_delivered
.saturating_add(bytes.len() as u64);
snapshot.body_chunks_delivered = snapshot.body_chunks_delivered.saturating_add(1);
}
}
result
}
}
#[cfg(feature = "http-client-http2")]
fn counted_stream(
stream: HttpByteStream,
state: Arc<Mutex<crate::api::http::Http2FlowControlSnapshot>>,
) -> HttpByteStream {
Box::pin(CountedStream {
inner: stream,
state,
})
}