2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-12-30 08:57:17 -05:00
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::Stream;
|
|
|
|
use futures::StreamExt;
|
|
|
|
use reqwest;
|
2018-10-25 19:14:04 -04:00
|
|
|
use std::cmp::min;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Read;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2019-12-30 08:57:17 -05:00
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
|
|
|
|
// TODO(bartlomieju): most of this stuff can be moved to `cli/ops/fetch.rs`
|
|
|
|
type ReqwestStream = Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send>>;
|
2018-10-25 19:14:04 -04:00
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
/// Wraps `ReqwestStream` so that it can be exposed as an `AsyncRead` and integrated
|
2018-10-25 19:14:04 -04:00
|
|
|
/// into resources more easily.
|
|
|
|
pub struct HttpBody {
|
2019-12-30 08:57:17 -05:00
|
|
|
stream: ReqwestStream,
|
|
|
|
chunk: Option<Bytes>,
|
2018-10-25 19:14:04 -04:00
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpBody {
|
2019-12-30 08:57:17 -05:00
|
|
|
pub fn from(body: ReqwestStream) -> Self {
|
2018-11-30 03:30:49 -05:00
|
|
|
Self {
|
2019-12-30 08:57:17 -05:00
|
|
|
stream: body,
|
2018-10-25 19:14:04 -04:00
|
|
|
chunk: None,
|
|
|
|
pos: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Read for HttpBody {
|
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncRead for HttpBody {
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll_read(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context,
|
|
|
|
buf: &mut [u8],
|
|
|
|
) -> Poll<Result<usize, io::Error>> {
|
|
|
|
let mut inner = self.get_mut();
|
|
|
|
if let Some(chunk) = inner.chunk.take() {
|
2018-11-30 03:30:49 -05:00
|
|
|
debug!(
|
|
|
|
"HttpBody Fake Read buf {} chunk {} pos {}",
|
|
|
|
buf.len(),
|
|
|
|
chunk.len(),
|
2019-11-16 19:17:47 -05:00
|
|
|
inner.pos
|
2018-11-30 03:30:49 -05:00
|
|
|
);
|
2019-11-16 19:17:47 -05:00
|
|
|
let n = min(buf.len(), chunk.len() - inner.pos);
|
2018-11-30 03:30:49 -05:00
|
|
|
{
|
2019-11-16 19:17:47 -05:00
|
|
|
let rest = &chunk[inner.pos..];
|
2018-11-30 03:30:49 -05:00
|
|
|
buf[..n].clone_from_slice(&rest[..n]);
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
inner.pos += n;
|
|
|
|
if inner.pos == chunk.len() {
|
|
|
|
inner.pos = 0;
|
2018-11-30 03:30:49 -05:00
|
|
|
} else {
|
2019-11-16 19:17:47 -05:00
|
|
|
inner.chunk = Some(chunk);
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
return Poll::Ready(Ok(n));
|
2018-11-30 03:30:49 -05:00
|
|
|
} else {
|
2019-11-16 19:17:47 -05:00
|
|
|
assert_eq!(inner.pos, 0);
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
let p = inner.stream.poll_next_unpin(cx);
|
2018-10-25 19:14:04 -04:00
|
|
|
match p {
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Ready(Some(Err(e))) => Poll::Ready(Err(
|
2019-12-30 08:57:17 -05:00
|
|
|
// TODO(bartlomieju): rewrite it to use ErrBox
|
2018-10-25 19:14:04 -04:00
|
|
|
io::Error::new(io::ErrorKind::Other, e),
|
2019-11-16 19:17:47 -05:00
|
|
|
)),
|
|
|
|
Poll::Ready(Some(Ok(chunk))) => {
|
|
|
|
debug!(
|
|
|
|
"HttpBody Real Read buf {} chunk {} pos {}",
|
|
|
|
buf.len(),
|
|
|
|
chunk.len(),
|
|
|
|
inner.pos
|
|
|
|
);
|
|
|
|
let n = min(buf.len(), chunk.len());
|
|
|
|
buf[..n].clone_from_slice(&chunk[..n]);
|
|
|
|
if buf.len() < chunk.len() {
|
|
|
|
inner.pos = n;
|
|
|
|
inner.chunk = Some(chunk);
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Ready(Ok(n))
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => Poll::Ready(Ok(0)),
|
|
|
|
Poll::Pending => Poll::Pending,
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|