2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-25 19:14:04 -04:00
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
use futures::stream::Stream;
|
2018-10-25 19:14:04 -04:00
|
|
|
use futures::Async;
|
|
|
|
use futures::Poll;
|
2019-04-25 13:29:21 -04:00
|
|
|
use reqwest::r#async::Chunk;
|
|
|
|
use reqwest::r#async::Decoder;
|
2018-10-25 19:14:04 -04:00
|
|
|
use std::cmp::min;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Read;
|
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
/// Wraps `reqwest::Decoder` 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-04-25 13:29:21 -04:00
|
|
|
decoder: Decoder,
|
2018-10-25 19:14:04 -04:00
|
|
|
chunk: Option<Chunk>,
|
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpBody {
|
2019-04-25 13:29:21 -04:00
|
|
|
pub fn from(body: Decoder) -> Self {
|
2018-11-30 03:30:49 -05:00
|
|
|
Self {
|
2019-04-25 13:29:21 -04:00
|
|
|
decoder: 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 {
|
|
|
|
fn poll_read(&mut self, buf: &mut [u8]) -> Poll<usize, io::Error> {
|
2018-11-30 03:30:49 -05:00
|
|
|
if let Some(chunk) = self.chunk.take() {
|
|
|
|
debug!(
|
|
|
|
"HttpBody Fake Read buf {} chunk {} pos {}",
|
|
|
|
buf.len(),
|
|
|
|
chunk.len(),
|
|
|
|
self.pos
|
|
|
|
);
|
|
|
|
let n = min(buf.len(), chunk.len() - self.pos);
|
|
|
|
{
|
|
|
|
let rest = &chunk[self.pos..];
|
|
|
|
buf[..n].clone_from_slice(&rest[..n]);
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
2018-11-30 03:30:49 -05:00
|
|
|
self.pos += n;
|
|
|
|
if self.pos == chunk.len() {
|
|
|
|
self.pos = 0;
|
|
|
|
} else {
|
|
|
|
self.chunk = Some(chunk);
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
2018-11-30 03:30:49 -05:00
|
|
|
return Ok(Async::Ready(n));
|
|
|
|
} else {
|
|
|
|
assert_eq!(self.pos, 0);
|
2018-10-25 19:14:04 -04:00
|
|
|
}
|
|
|
|
|
2019-04-25 13:29:21 -04:00
|
|
|
let p = self.decoder.poll();
|
2018-10-25 19:14:04 -04:00
|
|
|
match p {
|
|
|
|
Err(e) => Err(
|
|
|
|
// TODO Need to map hyper::Error into std::io::Error.
|
|
|
|
io::Error::new(io::ErrorKind::Other, e),
|
|
|
|
),
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(maybe_chunk)) => match maybe_chunk {
|
|
|
|
None => Ok(Async::Ready(0)),
|
|
|
|
Some(chunk) => {
|
|
|
|
debug!(
|
|
|
|
"HttpBody Real Read buf {} chunk {} pos {}",
|
|
|
|
buf.len(),
|
|
|
|
chunk.len(),
|
|
|
|
self.pos
|
|
|
|
);
|
|
|
|
let n = min(buf.len(), chunk.len());
|
|
|
|
buf[..n].clone_from_slice(&chunk[..n]);
|
|
|
|
if buf.len() < chunk.len() {
|
|
|
|
self.pos = n;
|
|
|
|
self.chunk = Some(chunk);
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(n))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|