use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::{Bytes, BytesMut};
use http_body::Body as _;
use crate::config::check_limit;
use crate::{Body, Error, Result};
pub(super) struct BodySource {
body: Body,
received: usize,
limit: Option<usize>,
resource: &'static str,
collected: Vec<Bytes>,
}
pub(super) enum BodyRead {
Chunk(Bytes),
Done,
}
impl BodySource {
pub fn new(body: Body, limit: Option<usize>, resource: &'static str) -> Self {
Self {
body,
received: 0,
limit,
resource,
collected: Vec::new(),
}
}
pub fn poll_read_all(&mut self, context: &mut Context<'_>) -> Poll<Result<Bytes>> {
loop {
match self.poll_read(context) {
Poll::Pending => {
return Poll::Pending;
}
Poll::Ready(Err(error)) => {
return Poll::Ready(Err(error));
}
Poll::Ready(Ok(BodyRead::Chunk(chunk))) => {
self.collected.push(chunk);
}
Poll::Ready(Ok(BodyRead::Done)) => {
return Poll::Ready(Ok(concat(std::mem::take(&mut self.collected))));
}
}
}
}
pub fn poll_read(&mut self, context: &mut Context<'_>) -> Poll<Result<BodyRead>> {
loop {
let frame = match Pin::new(&mut self.body).poll_frame(context) {
Poll::Pending => return Poll::Pending,
Poll::Ready(None) => return Poll::Ready(Ok(BodyRead::Done)),
Poll::Ready(Some(Err(error))) => return Poll::Ready(Err(error)),
Poll::Ready(Some(Ok(frame))) => frame,
};
let Ok(chunk) = frame.into_data() else {
continue;
};
if chunk.is_empty() {
continue;
}
return Poll::Ready(self.count(chunk.len()).map(|()| BodyRead::Chunk(chunk)));
}
}
fn count(&mut self, length: usize) -> Result<()> {
self.received = self
.received
.checked_add(length)
.ok_or(Error::LimitExceeded(self.resource))?;
check_limit(self.limit, self.received, self.resource)
}
}
fn concat(mut chunks: Vec<Bytes>) -> Bytes {
if chunks.len() <= 1 {
return chunks.pop().unwrap_or_default();
}
let mut bytes = BytesMut::with_capacity(chunks.iter().map(Bytes::len).sum());
for chunk in chunks {
bytes.extend_from_slice(&chunk);
}
bytes.freeze()
}