send_body takes impl MessageBody

This commit is contained in:
Rob Ede 2021-12-24 23:46:35 +00:00
parent 1296e07c48
commit 48c8f8433f
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 36 additions and 22 deletions

View File

@ -3,8 +3,10 @@
## Unreleased - 2021-xx-xx
- Rename `Connector::{ssl => openssl}`. [#2503]
- Improve `Client` instantiation efficiency when using `openssl` by only building connectors once. [#2503]
- `ClientRequest::send_body` now takes an `impl MessageBody`. [#????]
[#2503]: https://github.com/actix/actix-web/pull/2503
[#????]: https://github.com/actix/actix-web/pull/????
## 3.0.0-beta.14 - 2021-12-17

View File

@ -77,10 +77,27 @@ impl<B> AnyBody<B>
where
B: MessageBody + 'static,
{
/// Converts a [`MessageBody`] type into the best possible representation.
///
/// Checks size for `None` and tries to convert to `Bytes`. Otherwise, uses the `Body` variant.
pub fn from_message_body(body: B) -> Self
where
B: MessageBody,
{
if matches!(body.size(), BodySize::None) {
return Self::None;
}
match body.try_into_bytes() {
Ok(body) => Self::Bytes { body },
Err(body) => Self::new(body),
}
}
pub fn into_boxed(self) -> AnyBody {
match self {
Self::None => AnyBody::None,
Self::Bytes { body: bytes } => AnyBody::Bytes { body: bytes },
Self::Bytes { body } => AnyBody::Bytes { body },
Self::Body { body } => AnyBody::new_boxed(body),
}
}

View File

@ -5,13 +5,13 @@ use futures_core::Stream;
use serde::Serialize;
use actix_http::{
body::MessageBody,
error::HttpError,
header::{HeaderMap, HeaderName, TryIntoHeaderValue},
Method, RequestHead, Uri,
};
use crate::{
any_body::AnyBody,
sender::{RequestSender, SendClientRequest},
BoxError, ClientConfig,
};
@ -46,7 +46,7 @@ impl FrozenClientRequest {
/// Send a body.
pub fn send_body<B>(&self, body: B) -> SendClientRequest
where
B: Into<AnyBody>,
B: actix_http::body::MessageBody + 'static,
{
RequestSender::Rc(self.head.clone(), None).send_body(
self.addr,
@ -159,7 +159,7 @@ impl FrozenSendBuilder {
/// Complete request construction and send a body.
pub fn send_body<B>(self, body: B) -> SendClientRequest
where
B: Into<AnyBody>,
B: MessageBody + 'static,
{
if let Some(e) = self.err {
return e.into();

View File

@ -190,10 +190,7 @@ where
let body_new = if is_redirect {
// try to reuse body
match body {
Some(ref bytes) => AnyBody::Bytes {
body: bytes.clone(),
},
// TODO: should this be AnyBody::Empty or AnyBody::None.
Some(ref bytes) => AnyBody::from(bytes.clone()),
_ => AnyBody::empty(),
}
} else {

View File

@ -5,13 +5,13 @@ use futures_core::Stream;
use serde::Serialize;
use actix_http::{
body::MessageBody,
error::HttpError,
header::{self, HeaderMap, HeaderValue, TryIntoHeaderPair},
ConnectionType, Method, RequestHead, Uri, Version,
};
use crate::{
any_body::AnyBody,
error::{FreezeRequestError, InvalidUrl},
frozen::FrozenClientRequest,
sender::{PrepForSendingError, RequestSender, SendClientRequest},
@ -340,7 +340,7 @@ impl ClientRequest {
/// Complete request construction and send body.
pub fn send_body<B>(self, body: B) -> SendClientRequest
where
B: Into<AnyBody>,
B: MessageBody + 'static,
{
let slf = match self.prep_for_sending() {
Ok(slf) => slf,

View File

@ -8,7 +8,7 @@ use std::{
};
use actix_http::{
body::BodyStream,
body::{BodyStream, MessageBody},
error::HttpError,
header::{self, HeaderMap, HeaderName, TryIntoHeaderValue},
RequestHead, RequestHeadType,
@ -189,15 +189,17 @@ impl RequestSender {
body: B,
) -> SendClientRequest
where
B: Into<AnyBody>,
B: MessageBody + 'static,
{
let req = match self {
RequestSender::Owned(head) => {
ConnectRequest::Client(RequestHeadType::Owned(head), body.into(), addr)
}
RequestSender::Owned(head) => ConnectRequest::Client(
RequestHeadType::Owned(head),
AnyBody::from_message_body(body).into_boxed(),
addr,
),
RequestSender::Rc(head, extra_headers) => ConnectRequest::Client(
RequestHeadType::Rc(head, extra_headers),
body.into(),
AnyBody::from_message_body(body).into_boxed(),
addr,
),
};
@ -229,9 +231,7 @@ impl RequestSender {
response_decompress,
timeout,
config,
AnyBody::Bytes {
body: Bytes::from(body),
},
AnyBody::copy_from_slice(body.as_bytes()),
)
}
@ -260,9 +260,7 @@ impl RequestSender {
response_decompress,
timeout,
config,
AnyBody::Bytes {
body: Bytes::from(body),
},
AnyBody::copy_from_slice(body.as_bytes()),
)
}