2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-04-22 13:48:21 -04:00
|
|
|
use bytes::Bytes;
|
|
|
|
use deno_core::futures::stream::Peekable;
|
|
|
|
use deno_core::futures::Stream;
|
|
|
|
use deno_core::futures::StreamExt;
|
2024-10-18 18:57:12 -04:00
|
|
|
use deno_core::futures::TryFutureExt;
|
2023-04-22 13:48:21 -04:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
use deno_core::AsyncResult;
|
|
|
|
use deno_core::BufView;
|
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2023-12-27 11:59:57 -05:00
|
|
|
use hyper::body::Body;
|
|
|
|
use hyper::body::Incoming;
|
|
|
|
use hyper::body::SizeHint;
|
2023-04-22 13:48:21 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
2023-09-15 10:08:21 -04:00
|
|
|
use std::task::ready;
|
|
|
|
use std::task::Poll;
|
2023-04-22 13:48:21 -04:00
|
|
|
|
|
|
|
/// Converts a hyper incoming body stream into a stream of [`Bytes`] that we can use to read in V8.
|
|
|
|
struct ReadFuture(Incoming);
|
|
|
|
|
|
|
|
impl Stream for ReadFuture {
|
2024-10-18 18:57:12 -04:00
|
|
|
type Item = Result<Bytes, hyper::Error>;
|
2023-04-22 13:48:21 -04:00
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
2023-09-15 10:08:21 -04:00
|
|
|
) -> Poll<Option<Self::Item>> {
|
|
|
|
// Loop until we receive a non-empty frame from Hyper
|
|
|
|
let this = self.get_mut();
|
|
|
|
loop {
|
|
|
|
let res = ready!(Pin::new(&mut this.0).poll_frame(cx));
|
|
|
|
break match res {
|
|
|
|
Some(Ok(frame)) => {
|
|
|
|
if let Ok(data) = frame.into_data() {
|
|
|
|
// Ensure that we never yield an empty frame
|
|
|
|
if !data.is_empty() {
|
2024-10-18 18:57:12 -04:00
|
|
|
break Poll::Ready(Some(Ok(data)));
|
2023-09-15 10:08:21 -04:00
|
|
|
}
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
2023-09-15 10:08:21 -04:00
|
|
|
// Loop again so we don't lose the waker
|
|
|
|
continue;
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
2024-10-18 18:57:12 -04:00
|
|
|
Some(Err(e)) => Poll::Ready(Some(Err(e))),
|
2023-09-15 10:08:21 -04:00
|
|
|
None => Poll::Ready(None),
|
|
|
|
};
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct HttpRequestBody(AsyncRefCell<Peekable<ReadFuture>>, SizeHint);
|
|
|
|
|
|
|
|
impl HttpRequestBody {
|
|
|
|
pub fn new(body: Incoming) -> Self {
|
|
|
|
let size_hint = body.size_hint();
|
|
|
|
Self(AsyncRefCell::new(ReadFuture(body).peekable()), size_hint)
|
|
|
|
}
|
|
|
|
|
2024-10-18 18:57:12 -04:00
|
|
|
async fn read(self: Rc<Self>, limit: usize) -> Result<BufView, hyper::Error> {
|
2023-04-22 13:48:21 -04:00
|
|
|
let peekable = RcRef::map(self, |this| &this.0);
|
|
|
|
let mut peekable = peekable.borrow_mut().await;
|
|
|
|
match Pin::new(&mut *peekable).peek_mut().await {
|
|
|
|
None => Ok(BufView::empty()),
|
|
|
|
Some(Err(_)) => Err(peekable.next().await.unwrap().err().unwrap()),
|
|
|
|
Some(Ok(bytes)) => {
|
|
|
|
if bytes.len() <= limit {
|
|
|
|
// We can safely take the next item since we peeked it
|
|
|
|
return Ok(BufView::from(peekable.next().await.unwrap()?));
|
|
|
|
}
|
|
|
|
let ret = bytes.split_to(limit);
|
|
|
|
Ok(BufView::from(ret))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for HttpRequestBody {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"requestBody".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(self: Rc<Self>, limit: usize) -> AsyncResult<BufView> {
|
2024-10-18 18:57:12 -04:00
|
|
|
Box::pin(HttpRequestBody::read(self, limit).map_err(Into::into))
|
2023-04-22 13:48:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (u64, Option<u64>) {
|
|
|
|
(self.1.lower(), self.1.upper())
|
|
|
|
}
|
|
|
|
}
|