mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 02:19:22 +02:00
refactor HttpRequest mutability
This commit is contained in:
@ -35,6 +35,7 @@ pub use self::connector::{
|
||||
Pause, Resume,
|
||||
};
|
||||
pub(crate) use self::parser::{HttpResponseParser, HttpResponseParserError};
|
||||
pub(crate) use self::pipeline::Pipeline;
|
||||
pub use self::pipeline::{SendRequest, SendRequestError};
|
||||
pub use self::request::{ClientRequest, ClientRequestBuilder};
|
||||
pub use self::response::ClientResponse;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::sync::oneshot;
|
||||
use futures::{Async, Future, Poll};
|
||||
use futures::{Async, Future, Poll, Stream};
|
||||
use http::header::CONTENT_ENCODING;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::{io, mem};
|
||||
@ -230,7 +230,7 @@ impl Future for SendRequest {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Pipeline {
|
||||
pub struct Pipeline {
|
||||
body: IoBody,
|
||||
body_completed: bool,
|
||||
conn: Option<Connection>,
|
||||
@ -315,7 +315,7 @@ impl Pipeline {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn poll(&mut self) -> Poll<Option<Bytes>, PayloadError> {
|
||||
pub(crate) fn poll(&mut self) -> Poll<Option<Bytes>, PayloadError> {
|
||||
if self.conn.is_none() {
|
||||
return Ok(Async::Ready(None));
|
||||
}
|
||||
@ -522,3 +522,13 @@ impl Drop for Pipeline {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Future that resolves to a complete request body.
|
||||
impl Stream for Box<Pipeline> {
|
||||
type Item = Bytes;
|
||||
type Error = PayloadError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
Pipeline::poll(self)
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use std::cell::RefCell;
|
||||
use std::{fmt, str};
|
||||
|
||||
use bytes::Bytes;
|
||||
@ -30,23 +31,33 @@ impl Default for ClientMessage {
|
||||
}
|
||||
|
||||
/// An HTTP Client response
|
||||
pub struct ClientResponse(ClientMessage, Option<Box<Pipeline>>);
|
||||
pub struct ClientResponse(ClientMessage, RefCell<Option<Box<Pipeline>>>);
|
||||
|
||||
impl HttpMessage for ClientResponse {
|
||||
type Stream = Box<Pipeline>;
|
||||
|
||||
/// Get the headers from the response.
|
||||
#[inline]
|
||||
fn headers(&self) -> &HeaderMap {
|
||||
&self.0.headers
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn payload(&self) -> Box<Pipeline> {
|
||||
self.1
|
||||
.borrow_mut()
|
||||
.take()
|
||||
.expect("Payload is already consumed.")
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientResponse {
|
||||
pub(crate) fn new(msg: ClientMessage) -> ClientResponse {
|
||||
ClientResponse(msg, None)
|
||||
ClientResponse(msg, RefCell::new(None))
|
||||
}
|
||||
|
||||
pub(crate) fn set_pipeline(&mut self, pl: Box<Pipeline>) {
|
||||
self.1 = Some(pl);
|
||||
*self.1.borrow_mut() = Some(pl);
|
||||
}
|
||||
|
||||
/// Get the HTTP version of this response.
|
||||
@ -95,20 +106,6 @@ impl fmt::Debug for ClientResponse {
|
||||
}
|
||||
}
|
||||
|
||||
/// Future that resolves to a complete request body.
|
||||
impl Stream for ClientResponse {
|
||||
type Item = Bytes;
|
||||
type Error = PayloadError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
if let Some(ref mut pl) = self.1 {
|
||||
pl.poll()
|
||||
} else {
|
||||
Ok(Async::Ready(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
Reference in New Issue
Block a user