1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

add Read and AsyncRead impl to HttpRequest

This commit is contained in:
Nikolay Kim 2018-02-25 21:26:58 +03:00
parent a2b98b31e8
commit 6ef9c60361
2 changed files with 37 additions and 4 deletions

View File

@ -96,8 +96,7 @@ impl<T: ResponseError> From<T> for Error {
/// Compatibility for `failure::Error`
impl<T> ResponseError for failure::Compat<T>
where T: fmt::Display + fmt::Debug + Sync + Send + 'static
{ }
where T: fmt::Display + fmt::Debug + Sync + Send + 'static { }
impl From<failure::Error> for Error {
fn from(err: failure::Error) -> Error {

View File

@ -1,16 +1,18 @@
//! HTTP Request message related code.
use std::{str, fmt, mem};
use std::{io, cmp, str, fmt, mem};
use std::rc::Rc;
use std::net::SocketAddr;
use std::collections::HashMap;
use bytes::{Bytes, BytesMut};
use cookie::Cookie;
use futures::{Future, Stream, Poll};
use futures::{Async, Future, Stream, Poll};
use http_range::HttpRange;
use serde::de::DeserializeOwned;
use mime::Mime;
use failure;
use url::{Url, form_urlencoded};
use http::{header, Uri, Method, Version, HeaderMap, Extensions};
use tokio_io::AsyncRead;
use info::ConnectionInfo;
use param::Params;
@ -611,6 +613,38 @@ impl<S> Stream for HttpRequest<S> {
}
}
impl<S> io::Read for HttpRequest<S> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.payload_mut().poll() {
Ok(Async::Ready(Some(mut b))) => {
let i = cmp::min(b.len(), buf.len());
buf.copy_from_slice(&b.split_to(i)[..i]);
if !b.is_empty() {
self.payload_mut().unread_data(b);
}
if i < buf.len() {
match self.read(&mut buf[i..]) {
Ok(n) => Ok(i + n),
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => Ok(i),
Err(e) => Err(e),
}
} else {
Ok(i)
}
}
Ok(Async::Ready(None)) => Ok(0),
Ok(Async::NotReady) =>
Err(io::Error::new(io::ErrorKind::WouldBlock, "Not ready")),
Err(e) =>
Err(io::Error::new(io::ErrorKind::Other, failure::Error::from(e).compat())),
}
}
}
impl<S> AsyncRead for HttpRequest<S> {}
impl<S> fmt::Debug for HttpRequest<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = write!(f, "\nHttpRequest {:?} {}:{}\n",