mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 06:39:22 +02:00
remove responsebody indirection from response (#2201)
This commit is contained in:
@ -86,15 +86,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseBody<Body> {
|
||||
pub(crate) fn get_ref(&self) -> &[u8] {
|
||||
match *self {
|
||||
ResponseBody::Body(ref b) => b.get_ref(),
|
||||
ResponseBody::Other(ref b) => b.get_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_static_str() {
|
||||
assert_eq!(Body::from("").size(), BodySize::Sized(0));
|
||||
|
@ -18,12 +18,6 @@ use crate::{body::Body, helpers::Writer, Response, ResponseBuilder};
|
||||
|
||||
pub use http::Error as HttpError;
|
||||
|
||||
/// A specialized [`std::result::Result`] for Actix Web operations.
|
||||
///
|
||||
/// This typedef is generally used to avoid writing out `actix_http::error::Error` directly and is
|
||||
/// otherwise a direct mapping to `Result`.
|
||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
/// General purpose actix web error.
|
||||
///
|
||||
/// An actix web error is used to carry errors from `std::error`
|
||||
@ -470,9 +464,8 @@ impl ResponseError for ContentTypeError {
|
||||
///
|
||||
/// ```
|
||||
/// # use std::io;
|
||||
/// # use actix_http::*;
|
||||
///
|
||||
/// fn index(req: Request) -> Result<&'static str> {
|
||||
/// # use actix_http::{error, Request};
|
||||
/// fn index(req: Request) -> Result<&'static str, actix_http::Error> {
|
||||
/// Err(error::ErrorBadRequest(io::Error::new(io::ErrorKind::Other, "error")))
|
||||
/// }
|
||||
/// ```
|
||||
|
@ -17,7 +17,7 @@ use futures_core::ready;
|
||||
use log::{error, trace};
|
||||
use pin_project::pin_project;
|
||||
|
||||
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
|
||||
use crate::body::{Body, BodySize, MessageBody};
|
||||
use crate::config::ServiceConfig;
|
||||
use crate::error::{DispatchError, Error};
|
||||
use crate::error::{ParseError, PayloadError};
|
||||
@ -141,7 +141,8 @@ where
|
||||
None,
|
||||
ExpectCall(#[pin] X::Future),
|
||||
ServiceCall(#[pin] S::Future),
|
||||
SendPayload(#[pin] ResponseBody<B>),
|
||||
SendPayload(#[pin] B),
|
||||
SendErrorPayload(#[pin] Body),
|
||||
}
|
||||
|
||||
impl<S, B, X> State<S, B, X>
|
||||
@ -295,11 +296,11 @@ where
|
||||
io.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn send_response(
|
||||
fn send_response_inner(
|
||||
self: Pin<&mut Self>,
|
||||
message: Response<()>,
|
||||
body: ResponseBody<B>,
|
||||
) -> Result<(), DispatchError> {
|
||||
body: &impl MessageBody,
|
||||
) -> Result<BodySize, DispatchError> {
|
||||
let size = body.size();
|
||||
let mut this = self.project();
|
||||
this.codec
|
||||
@ -312,10 +313,35 @@ where
|
||||
})?;
|
||||
|
||||
this.flags.set(Flags::KEEPALIVE, this.codec.keepalive());
|
||||
match size {
|
||||
BodySize::None | BodySize::Empty => this.state.set(State::None),
|
||||
_ => this.state.set(State::SendPayload(body)),
|
||||
|
||||
Ok(size)
|
||||
}
|
||||
|
||||
fn send_response(
|
||||
mut self: Pin<&mut Self>,
|
||||
message: Response<()>,
|
||||
body: B,
|
||||
) -> Result<(), DispatchError> {
|
||||
let size = self.as_mut().send_response_inner(message, &body)?;
|
||||
let state = match size {
|
||||
BodySize::None | BodySize::Empty => State::None,
|
||||
_ => State::SendPayload(body),
|
||||
};
|
||||
self.project().state.set(state);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_error_response(
|
||||
mut self: Pin<&mut Self>,
|
||||
message: Response<()>,
|
||||
body: Body,
|
||||
) -> Result<(), DispatchError> {
|
||||
let size = self.as_mut().send_response_inner(message, &body)?;
|
||||
let state = match size {
|
||||
BodySize::None | BodySize::Empty => State::None,
|
||||
_ => State::SendErrorPayload(body),
|
||||
};
|
||||
self.project().state.set(state);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -353,8 +379,7 @@ where
|
||||
// send_response would update InnerDispatcher state to SendPayload or
|
||||
// None(If response body is empty).
|
||||
// continue loop to poll it.
|
||||
self.as_mut()
|
||||
.send_response(res, ResponseBody::Other(Body::Empty))?;
|
||||
self.as_mut().send_error_response(res, Body::Empty)?;
|
||||
}
|
||||
|
||||
// return with upgrade request and poll it exclusively.
|
||||
@ -376,7 +401,7 @@ where
|
||||
Poll::Ready(Err(err)) => {
|
||||
let res = Response::from_error(err.into());
|
||||
let (res, body) = res.replace_body(());
|
||||
self.as_mut().send_response(res, body.into_body())?;
|
||||
self.as_mut().send_error_response(res, body)?;
|
||||
}
|
||||
|
||||
// service call pending and could be waiting for more chunk messages.
|
||||
@ -392,6 +417,41 @@ where
|
||||
},
|
||||
|
||||
StateProj::SendPayload(mut stream) => {
|
||||
// keep populate writer buffer until buffer size limit hit,
|
||||
// get blocked or finished.
|
||||
while this.write_buf.len() < super::payload::MAX_BUFFER_SIZE {
|
||||
match stream.as_mut().poll_next(cx) {
|
||||
Poll::Ready(Some(Ok(item))) => {
|
||||
this.codec.encode(
|
||||
Message::Chunk(Some(item)),
|
||||
&mut this.write_buf,
|
||||
)?;
|
||||
}
|
||||
|
||||
Poll::Ready(None) => {
|
||||
this.codec
|
||||
.encode(Message::Chunk(None), &mut this.write_buf)?;
|
||||
// payload stream finished.
|
||||
// set state to None and handle next message
|
||||
this.state.set(State::None);
|
||||
continue 'res;
|
||||
}
|
||||
|
||||
Poll::Ready(Some(Err(err))) => {
|
||||
return Err(DispatchError::Service(err.into()))
|
||||
}
|
||||
|
||||
Poll::Pending => return Ok(PollResponse::DoNothing),
|
||||
}
|
||||
}
|
||||
// buffer is beyond max size.
|
||||
// return and try to write the whole buffer to io stream.
|
||||
return Ok(PollResponse::DrainWriteBuf);
|
||||
}
|
||||
|
||||
StateProj::SendErrorPayload(mut stream) => {
|
||||
// TODO: de-dupe impl with SendPayload
|
||||
|
||||
// keep populate writer buffer until buffer size limit hit,
|
||||
// get blocked or finished.
|
||||
while this.write_buf.len() < super::payload::MAX_BUFFER_SIZE {
|
||||
@ -433,12 +493,14 @@ where
|
||||
let fut = this.flow.service.call(req);
|
||||
this.state.set(State::ServiceCall(fut));
|
||||
}
|
||||
|
||||
// send expect error as response
|
||||
Poll::Ready(Err(err)) => {
|
||||
let res = Response::from_error(err.into());
|
||||
let (res, body) = res.replace_body(());
|
||||
self.as_mut().send_response(res, body.into_body())?;
|
||||
self.as_mut().send_error_response(res, body)?;
|
||||
}
|
||||
|
||||
// expect must be solved before progress can be made.
|
||||
Poll::Pending => return Ok(PollResponse::DoNothing),
|
||||
},
|
||||
@ -486,7 +548,7 @@ where
|
||||
Poll::Ready(Err(err)) => {
|
||||
let res = Response::from_error(err.into());
|
||||
let (res, body) = res.replace_body(());
|
||||
return self.send_response(res, body.into_body());
|
||||
return self.send_error_response(res, body);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -506,7 +568,7 @@ where
|
||||
Poll::Ready(Err(err)) => {
|
||||
let res = Response::from_error(err.into());
|
||||
let (res, body) = res.replace_body(());
|
||||
self.send_response(res, body.into_body())
|
||||
self.send_error_response(res, body)
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -626,8 +688,10 @@ where
|
||||
}
|
||||
// Requests overflow buffer size should be responded with 431
|
||||
this.messages.push_back(DispatcherMessage::Error(
|
||||
Response::new(StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE)
|
||||
.drop_body(),
|
||||
Response::with_body(
|
||||
StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE,
|
||||
(),
|
||||
),
|
||||
));
|
||||
this.flags.insert(Flags::READ_DISCONNECT);
|
||||
*this.error = Some(ParseError::TooLarge.into());
|
||||
@ -706,10 +770,9 @@ where
|
||||
} else {
|
||||
// timeout on first request (slow request) return 408
|
||||
trace!("Slow request timeout");
|
||||
let _ = self.as_mut().send_response(
|
||||
Response::new(StatusCode::REQUEST_TIMEOUT)
|
||||
.drop_body(),
|
||||
ResponseBody::Other(Body::Empty),
|
||||
let _ = self.as_mut().send_error_response(
|
||||
Response::with_body(StatusCode::REQUEST_TIMEOUT, ()),
|
||||
Body::Empty,
|
||||
);
|
||||
this = self.project();
|
||||
this.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
|
||||
|
@ -630,8 +630,7 @@ mod tests {
|
||||
async fn test_no_content_length() {
|
||||
let mut bytes = BytesMut::with_capacity(2048);
|
||||
|
||||
let mut res: Response<()> =
|
||||
Response::new(StatusCode::SWITCHING_PROTOCOLS).into_body::<()>();
|
||||
let mut res = Response::with_body(StatusCode::SWITCHING_PROTOCOLS, ());
|
||||
res.headers_mut().insert(DATE, HeaderValue::from_static(""));
|
||||
res.headers_mut()
|
||||
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
||||
|
@ -4,7 +4,7 @@ use std::task::{Context, Poll};
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
||||
|
||||
use crate::body::{BodySize, MessageBody, ResponseBody};
|
||||
use crate::body::{BodySize, MessageBody};
|
||||
use crate::error::Error;
|
||||
use crate::h1::{Codec, Message};
|
||||
use crate::response::Response;
|
||||
@ -14,7 +14,7 @@ use crate::response::Response;
|
||||
pub struct SendResponse<T, B> {
|
||||
res: Option<Message<(Response<()>, BodySize)>>,
|
||||
#[pin]
|
||||
body: Option<ResponseBody<B>>,
|
||||
body: Option<B>,
|
||||
#[pin]
|
||||
framed: Option<Framed<T, Codec>>,
|
||||
}
|
||||
@ -62,7 +62,18 @@ where
|
||||
.unwrap()
|
||||
.is_write_buf_full()
|
||||
{
|
||||
match this.body.as_mut().as_pin_mut().unwrap().poll_next(cx)? {
|
||||
let next =
|
||||
// TODO: MSRV 1.51: poll_map_err
|
||||
match this.body.as_mut().as_pin_mut().unwrap().poll_next(cx) {
|
||||
Poll::Ready(Some(Ok(item))) => Poll::Ready(Some(item)),
|
||||
Poll::Ready(Some(Err(err))) => {
|
||||
return Poll::Ready(Err(err.into()))
|
||||
}
|
||||
Poll::Ready(None) => Poll::Ready(None),
|
||||
Poll::Pending => Poll::Pending,
|
||||
};
|
||||
|
||||
match next {
|
||||
Poll::Ready(item) => {
|
||||
// body is done when item is None
|
||||
body_done = item.is_none();
|
||||
|
@ -12,7 +12,7 @@ use h2::{
|
||||
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
||||
use log::{error, trace};
|
||||
|
||||
use crate::body::{BodySize, MessageBody, ResponseBody};
|
||||
use crate::body::{Body, BodySize, MessageBody};
|
||||
use crate::config::ServiceConfig;
|
||||
use crate::error::{DispatchError, Error};
|
||||
use crate::message::ResponseHead;
|
||||
@ -135,7 +135,8 @@ struct ServiceResponse<F, I, E, B> {
|
||||
#[pin_project::pin_project(project = ServiceResponseStateProj)]
|
||||
enum ServiceResponseState<F, B> {
|
||||
ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
|
||||
SendPayload(SendStream<Bytes>, #[pin] ResponseBody<B>),
|
||||
SendPayload(SendStream<Bytes>, #[pin] B),
|
||||
SendErrorPayload(SendStream<Bytes>, #[pin] Body),
|
||||
}
|
||||
|
||||
impl<F, I, E, B> ServiceResponse<F, I, E, B>
|
||||
@ -280,9 +281,8 @@ where
|
||||
if size.is_eof() {
|
||||
Poll::Ready(())
|
||||
} else {
|
||||
this.state.set(ServiceResponseState::SendPayload(
|
||||
stream,
|
||||
body.into_body(),
|
||||
this.state.set(ServiceResponseState::SendErrorPayload(
|
||||
stream, body,
|
||||
));
|
||||
self.poll(cx)
|
||||
}
|
||||
@ -331,8 +331,65 @@ where
|
||||
*this.buffer = Some(chunk);
|
||||
}
|
||||
|
||||
Some(Err(err)) => {
|
||||
error!(
|
||||
"Response payload stream error: {:?}",
|
||||
err.into()
|
||||
);
|
||||
|
||||
return Poll::Ready(());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ServiceResponseStateProj::SendErrorPayload(ref mut stream, ref mut body) => {
|
||||
// TODO: de-dupe impl with SendPayload
|
||||
|
||||
loop {
|
||||
match this.buffer {
|
||||
Some(ref mut buffer) => match ready!(stream.poll_capacity(cx)) {
|
||||
None => return Poll::Ready(()),
|
||||
|
||||
Some(Ok(cap)) => {
|
||||
let len = buffer.len();
|
||||
let bytes = buffer.split_to(cmp::min(cap, len));
|
||||
|
||||
if let Err(e) = stream.send_data(bytes, false) {
|
||||
warn!("{:?}", e);
|
||||
return Poll::Ready(());
|
||||
} else if !buffer.is_empty() {
|
||||
let cap = cmp::min(buffer.len(), CHUNK_SIZE);
|
||||
stream.reserve_capacity(cap);
|
||||
} else {
|
||||
this.buffer.take();
|
||||
}
|
||||
}
|
||||
|
||||
Some(Err(e)) => {
|
||||
error!("Response payload stream error: {:?}", e);
|
||||
warn!("{:?}", e);
|
||||
return Poll::Ready(());
|
||||
}
|
||||
},
|
||||
|
||||
None => match ready!(body.as_mut().poll_next(cx)) {
|
||||
None => {
|
||||
if let Err(e) = stream.send_data(Bytes::new(), true) {
|
||||
warn!("{:?}", e);
|
||||
}
|
||||
return Poll::Ready(());
|
||||
}
|
||||
|
||||
Some(Ok(chunk)) => {
|
||||
stream
|
||||
.reserve_capacity(cmp::min(chunk.len(), CHUNK_SIZE));
|
||||
*this.buffer = Some(chunk);
|
||||
}
|
||||
|
||||
Some(Err(err)) => {
|
||||
error!("Response payload stream error: {:?}", err);
|
||||
|
||||
return Poll::Ready(());
|
||||
}
|
||||
},
|
||||
|
@ -104,7 +104,7 @@ impl Display for Charset {
|
||||
impl FromStr for Charset {
|
||||
type Err = crate::Error;
|
||||
|
||||
fn from_str(s: &str) -> crate::Result<Charset> {
|
||||
fn from_str(s: &str) -> Result<Charset, crate::Error> {
|
||||
Ok(match s.to_ascii_uppercase().as_ref() {
|
||||
"US-ASCII" => Us_Ascii,
|
||||
"ISO-8859-1" => Iso_8859_1,
|
||||
|
@ -54,7 +54,7 @@ pub mod ws;
|
||||
|
||||
pub use self::builder::HttpServiceBuilder;
|
||||
pub use self::config::{KeepAlive, ServiceConfig};
|
||||
pub use self::error::{Error, ResponseError, Result};
|
||||
pub use self::error::{Error, ResponseError};
|
||||
pub use self::extensions::Extensions;
|
||||
pub use self::header::ContentEncoding;
|
||||
pub use self::http_message::HttpMessage;
|
||||
|
@ -293,14 +293,14 @@ impl ResponseHead {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Check if keep-alive is enabled
|
||||
#[inline]
|
||||
pub fn keep_alive(&self) -> bool {
|
||||
self.connection_type() == ConnectionType::KeepAlive
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Check upgrade status of this message
|
||||
#[inline]
|
||||
pub fn upgrade(&self) -> bool {
|
||||
self.connection_type() == ConnectionType::Upgrade
|
||||
}
|
||||
@ -389,12 +389,6 @@ impl BoxedResponseHead {
|
||||
pub fn new(status: StatusCode) -> Self {
|
||||
RESPONSE_POOL.with(|p| p.get_message(status))
|
||||
}
|
||||
|
||||
pub(crate) fn take(&mut self) -> Self {
|
||||
BoxedResponseHead {
|
||||
head: self.head.take(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for BoxedResponseHead {
|
||||
|
@ -2,17 +2,13 @@
|
||||
|
||||
use std::{
|
||||
cell::{Ref, RefMut},
|
||||
fmt,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
str,
|
||||
task::{Context, Poll},
|
||||
fmt, str,
|
||||
};
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
|
||||
use crate::{
|
||||
body::{Body, MessageBody, ResponseBody},
|
||||
body::{Body, MessageBody},
|
||||
error::Error,
|
||||
extensions::Extensions,
|
||||
http::{HeaderMap, StatusCode},
|
||||
@ -23,22 +19,22 @@ use crate::{
|
||||
/// An HTTP response.
|
||||
pub struct Response<B> {
|
||||
pub(crate) head: BoxedResponseHead,
|
||||
pub(crate) body: ResponseBody<B>,
|
||||
pub(crate) body: B,
|
||||
pub(crate) error: Option<Error>,
|
||||
}
|
||||
|
||||
impl Response<Body> {
|
||||
/// Constructs a response
|
||||
/// Constructs a new response with default body.
|
||||
#[inline]
|
||||
pub fn new(status: StatusCode) -> Response<Body> {
|
||||
Response {
|
||||
head: BoxedResponseHead::new(status),
|
||||
body: ResponseBody::Body(Body::Empty),
|
||||
body: Body::Empty,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create HTTP response builder with specific status.
|
||||
/// Constructs a new response builder.
|
||||
#[inline]
|
||||
pub fn build(status: StatusCode) -> ResponseBuilder {
|
||||
ResponseBuilder::new(status)
|
||||
@ -47,25 +43,25 @@ impl Response<Body> {
|
||||
// just a couple frequently used shortcuts
|
||||
// this list should not grow larger than a few
|
||||
|
||||
/// Creates a new response with status 200 OK.
|
||||
/// Constructs a new response with status 200 OK.
|
||||
#[inline]
|
||||
pub fn ok() -> Response<Body> {
|
||||
Response::new(StatusCode::OK)
|
||||
}
|
||||
|
||||
/// Creates a new response with status 400 Bad Request.
|
||||
/// Constructs a new response with status 400 Bad Request.
|
||||
#[inline]
|
||||
pub fn bad_request() -> Response<Body> {
|
||||
Response::new(StatusCode::BAD_REQUEST)
|
||||
}
|
||||
|
||||
/// Creates a new response with status 404 Not Found.
|
||||
/// Constructs a new response with status 404 Not Found.
|
||||
#[inline]
|
||||
pub fn not_found() -> Response<Body> {
|
||||
Response::new(StatusCode::NOT_FOUND)
|
||||
}
|
||||
|
||||
/// Creates a new response with status 500 Internal Server Error.
|
||||
/// Constructs a new response with status 500 Internal Server Error.
|
||||
#[inline]
|
||||
pub fn internal_server_error() -> Response<Body> {
|
||||
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
@ -73,7 +69,7 @@ impl Response<Body> {
|
||||
|
||||
// end shortcuts
|
||||
|
||||
/// Constructs an error response
|
||||
/// Constructs a new response from an error.
|
||||
#[inline]
|
||||
pub fn from_error(error: Error) -> Response<Body> {
|
||||
let mut resp = error.as_response_error().error_response();
|
||||
@ -83,162 +79,142 @@ impl Response<Body> {
|
||||
resp.error = Some(error);
|
||||
resp
|
||||
}
|
||||
|
||||
/// Convert response to response with body
|
||||
pub fn into_body<B>(self) -> Response<B> {
|
||||
let b = match self.body {
|
||||
ResponseBody::Body(b) => b,
|
||||
ResponseBody::Other(b) => b,
|
||||
};
|
||||
Response {
|
||||
head: self.head,
|
||||
error: self.error,
|
||||
body: ResponseBody::Other(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> Response<B> {
|
||||
/// Constructs a response with body
|
||||
/// Constructs a new response with given body.
|
||||
#[inline]
|
||||
pub fn with_body(status: StatusCode, body: B) -> Response<B> {
|
||||
Response {
|
||||
head: BoxedResponseHead::new(status),
|
||||
body: ResponseBody::Body(body),
|
||||
body: body,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the head of this response.
|
||||
#[inline]
|
||||
/// Http message part of the response
|
||||
pub fn head(&self) -> &ResponseHead {
|
||||
&*self.head
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the head of this response.
|
||||
#[inline]
|
||||
/// Mutable reference to a HTTP message part of the response
|
||||
pub fn head_mut(&mut self) -> &mut ResponseHead {
|
||||
&mut *self.head
|
||||
}
|
||||
|
||||
/// The source `error` for this response
|
||||
/// Returns the source `error` for this response, if one is set.
|
||||
#[inline]
|
||||
pub fn error(&self) -> Option<&Error> {
|
||||
self.error.as_ref()
|
||||
}
|
||||
|
||||
/// Get the response status code
|
||||
/// Returns the status code of this response.
|
||||
#[inline]
|
||||
pub fn status(&self) -> StatusCode {
|
||||
self.head.status
|
||||
}
|
||||
|
||||
/// Set the `StatusCode` for this response
|
||||
/// Returns a mutable reference the status code of this response.
|
||||
#[inline]
|
||||
pub fn status_mut(&mut self) -> &mut StatusCode {
|
||||
&mut self.head.status
|
||||
}
|
||||
|
||||
/// Get the headers from the response
|
||||
/// Returns a reference to response headers.
|
||||
#[inline]
|
||||
pub fn headers(&self) -> &HeaderMap {
|
||||
&self.head.headers
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the headers
|
||||
/// Returns a mutable reference to response headers.
|
||||
#[inline]
|
||||
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
&mut self.head.headers
|
||||
}
|
||||
|
||||
/// Connection upgrade status
|
||||
/// Returns true if connection upgrade is enabled.
|
||||
#[inline]
|
||||
pub fn upgrade(&self) -> bool {
|
||||
self.head.upgrade()
|
||||
}
|
||||
|
||||
/// Keep-alive status for this connection
|
||||
/// Returns true if keep-alive is enabled.
|
||||
pub fn keep_alive(&self) -> bool {
|
||||
self.head.keep_alive()
|
||||
}
|
||||
|
||||
/// Responses extensions
|
||||
/// Returns a reference to the extensions of this response.
|
||||
#[inline]
|
||||
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
||||
self.head.extensions.borrow()
|
||||
}
|
||||
|
||||
/// Mutable reference to a the response's extensions
|
||||
/// Returns a mutable reference to the extensions of this response.
|
||||
#[inline]
|
||||
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
||||
self.head.extensions.borrow_mut()
|
||||
}
|
||||
|
||||
/// Get body of this response
|
||||
/// Returns a reference to the body of this response.
|
||||
#[inline]
|
||||
pub fn body(&self) -> &ResponseBody<B> {
|
||||
pub fn body(&self) -> &B {
|
||||
&self.body
|
||||
}
|
||||
|
||||
/// Set a body
|
||||
/// Sets new body.
|
||||
pub fn set_body<B2>(self, body: B2) -> Response<B2> {
|
||||
Response {
|
||||
head: self.head,
|
||||
body: ResponseBody::Body(body),
|
||||
body,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Split response and body
|
||||
pub fn into_parts(self) -> (Response<()>, ResponseBody<B>) {
|
||||
(
|
||||
Response {
|
||||
head: self.head,
|
||||
body: ResponseBody::Body(()),
|
||||
error: self.error,
|
||||
},
|
||||
self.body,
|
||||
)
|
||||
}
|
||||
|
||||
/// Drop request's body
|
||||
/// Drops body and returns new response.
|
||||
pub fn drop_body(self) -> Response<()> {
|
||||
Response {
|
||||
head: self.head,
|
||||
body: ResponseBody::Body(()),
|
||||
error: None,
|
||||
}
|
||||
self.set_body(())
|
||||
}
|
||||
|
||||
/// Set a body and return previous body value
|
||||
pub(crate) fn replace_body<B2>(self, body: B2) -> (Response<B2>, ResponseBody<B>) {
|
||||
/// Sets new body, returning new response and previous body value.
|
||||
pub(crate) fn replace_body<B2>(self, body: B2) -> (Response<B2>, B) {
|
||||
(
|
||||
Response {
|
||||
head: self.head,
|
||||
body: ResponseBody::Body(body),
|
||||
body,
|
||||
error: self.error,
|
||||
},
|
||||
self.body,
|
||||
)
|
||||
}
|
||||
|
||||
/// Set a body and return previous body value
|
||||
/// Returns split head and body.
|
||||
///
|
||||
/// # Implementation Notes
|
||||
/// Due to internal performance optimisations, the first element of the returned tuple is a
|
||||
/// `Response` as well but only contains the head of the response this was called on.
|
||||
pub fn into_parts(self) -> (Response<()>, B) {
|
||||
self.replace_body(())
|
||||
}
|
||||
|
||||
/// Returns new response with mapped body.
|
||||
pub fn map_body<F, B2>(mut self, f: F) -> Response<B2>
|
||||
where
|
||||
F: FnOnce(&mut ResponseHead, ResponseBody<B>) -> ResponseBody<B2>,
|
||||
F: FnOnce(&mut ResponseHead, B) -> B2,
|
||||
{
|
||||
let body = f(&mut self.head, self.body);
|
||||
|
||||
Response {
|
||||
body,
|
||||
head: self.head,
|
||||
body,
|
||||
error: self.error,
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract response body
|
||||
pub fn take_body(&mut self) -> ResponseBody<B> {
|
||||
self.body.take_body()
|
||||
/// Returns body, consuming this response.
|
||||
pub fn into_body(self) -> B {
|
||||
self.body
|
||||
}
|
||||
}
|
||||
|
||||
@ -264,19 +240,13 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Unpin> Future for Response<B> {
|
||||
type Output = Result<Response<B>, Error>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
Poll::Ready(Ok(Response {
|
||||
head: self.head.take(),
|
||||
body: self.body.take_body(),
|
||||
error: self.error.take(),
|
||||
}))
|
||||
impl<B: Default> Default for Response<B> {
|
||||
#[inline]
|
||||
fn default() -> Response<B> {
|
||||
Response::with_body(StatusCode::default(), B::default())
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper converters
|
||||
impl<I: Into<Response<Body>>, E: Into<Error>> From<Result<I, E>> for Response<Body> {
|
||||
fn from(res: Result<I, E>) -> Self {
|
||||
match res {
|
||||
|
@ -13,7 +13,7 @@ use bytes::Bytes;
|
||||
use futures_core::Stream;
|
||||
|
||||
use crate::{
|
||||
body::{Body, BodyStream, ResponseBody},
|
||||
body::{Body, BodyStream},
|
||||
error::{Error, HttpError},
|
||||
header::{self, IntoHeaderPair, IntoHeaderValue},
|
||||
message::{BoxedResponseHead, ConnectionType, ResponseHead},
|
||||
@ -38,10 +38,11 @@ use crate::{
|
||||
/// .body("1234");
|
||||
///
|
||||
/// assert_eq!(res.status(), StatusCode::OK);
|
||||
/// assert_eq!(body::to_bytes(res.take_body()).await.unwrap(), &b"1234"[..]);
|
||||
///
|
||||
/// assert!(res.headers().contains_key("server"));
|
||||
/// assert_eq!(res.headers().get_all("set-cookie").count(), 2);
|
||||
///
|
||||
/// assert_eq!(body::to_bytes(res.into_body()).await.unwrap(), &b"1234"[..]);
|
||||
/// # })
|
||||
/// ```
|
||||
pub struct ResponseBuilder {
|
||||
@ -236,23 +237,24 @@ impl ResponseBuilder {
|
||||
#[inline]
|
||||
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response<Body> {
|
||||
self.message_body(body.into())
|
||||
.unwrap_or_else(Response::from_error)
|
||||
}
|
||||
|
||||
/// Generate response with a body.
|
||||
///
|
||||
/// This `ResponseBuilder` will be left in a useless state.
|
||||
pub fn message_body<B>(&mut self, body: B) -> Response<B> {
|
||||
if let Some(e) = self.err.take() {
|
||||
return Response::from(Error::from(e)).into_body();
|
||||
pub fn message_body<B>(&mut self, body: B) -> Result<Response<B>, Error> {
|
||||
if let Some(err) = self.err.take() {
|
||||
return Err(err.into());
|
||||
}
|
||||
|
||||
let response = self.head.take().expect("cannot reuse response builder");
|
||||
|
||||
Response {
|
||||
Ok(Response {
|
||||
head: response,
|
||||
body: ResponseBody::Body(body),
|
||||
body,
|
||||
error: None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate response with a streaming body.
|
||||
|
Reference in New Issue
Block a user