mirror of https://github.com/fafhrd91/actix-web
send_body takes impl MessageBody
This commit is contained in:
parent
1296e07c48
commit
48c8f8433f
|
@ -3,8 +3,10 @@
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
- Rename `Connector::{ssl => openssl}`. [#2503]
|
- Rename `Connector::{ssl => openssl}`. [#2503]
|
||||||
- Improve `Client` instantiation efficiency when using `openssl` by only building connectors once. [#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
|
[#2503]: https://github.com/actix/actix-web/pull/2503
|
||||||
|
[#????]: https://github.com/actix/actix-web/pull/????
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.14 - 2021-12-17
|
## 3.0.0-beta.14 - 2021-12-17
|
||||||
|
|
|
@ -77,10 +77,27 @@ impl<B> AnyBody<B>
|
||||||
where
|
where
|
||||||
B: MessageBody + 'static,
|
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 {
|
pub fn into_boxed(self) -> AnyBody {
|
||||||
match self {
|
match self {
|
||||||
Self::None => AnyBody::None,
|
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),
|
Self::Body { body } => AnyBody::new_boxed(body),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,13 @@ use futures_core::Stream;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
|
body::MessageBody,
|
||||||
error::HttpError,
|
error::HttpError,
|
||||||
header::{HeaderMap, HeaderName, TryIntoHeaderValue},
|
header::{HeaderMap, HeaderName, TryIntoHeaderValue},
|
||||||
Method, RequestHead, Uri,
|
Method, RequestHead, Uri,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
any_body::AnyBody,
|
|
||||||
sender::{RequestSender, SendClientRequest},
|
sender::{RequestSender, SendClientRequest},
|
||||||
BoxError, ClientConfig,
|
BoxError, ClientConfig,
|
||||||
};
|
};
|
||||||
|
@ -46,7 +46,7 @@ impl FrozenClientRequest {
|
||||||
/// Send a body.
|
/// Send a body.
|
||||||
pub fn send_body<B>(&self, body: B) -> SendClientRequest
|
pub fn send_body<B>(&self, body: B) -> SendClientRequest
|
||||||
where
|
where
|
||||||
B: Into<AnyBody>,
|
B: actix_http::body::MessageBody + 'static,
|
||||||
{
|
{
|
||||||
RequestSender::Rc(self.head.clone(), None).send_body(
|
RequestSender::Rc(self.head.clone(), None).send_body(
|
||||||
self.addr,
|
self.addr,
|
||||||
|
@ -159,7 +159,7 @@ impl FrozenSendBuilder {
|
||||||
/// Complete request construction and send a body.
|
/// Complete request construction and send a body.
|
||||||
pub fn send_body<B>(self, body: B) -> SendClientRequest
|
pub fn send_body<B>(self, body: B) -> SendClientRequest
|
||||||
where
|
where
|
||||||
B: Into<AnyBody>,
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
if let Some(e) = self.err {
|
if let Some(e) = self.err {
|
||||||
return e.into();
|
return e.into();
|
||||||
|
|
|
@ -190,10 +190,7 @@ where
|
||||||
let body_new = if is_redirect {
|
let body_new = if is_redirect {
|
||||||
// try to reuse body
|
// try to reuse body
|
||||||
match body {
|
match body {
|
||||||
Some(ref bytes) => AnyBody::Bytes {
|
Some(ref bytes) => AnyBody::from(bytes.clone()),
|
||||||
body: bytes.clone(),
|
|
||||||
},
|
|
||||||
// TODO: should this be AnyBody::Empty or AnyBody::None.
|
|
||||||
_ => AnyBody::empty(),
|
_ => AnyBody::empty(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -5,13 +5,13 @@ use futures_core::Stream;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
|
body::MessageBody,
|
||||||
error::HttpError,
|
error::HttpError,
|
||||||
header::{self, HeaderMap, HeaderValue, TryIntoHeaderPair},
|
header::{self, HeaderMap, HeaderValue, TryIntoHeaderPair},
|
||||||
ConnectionType, Method, RequestHead, Uri, Version,
|
ConnectionType, Method, RequestHead, Uri, Version,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
any_body::AnyBody,
|
|
||||||
error::{FreezeRequestError, InvalidUrl},
|
error::{FreezeRequestError, InvalidUrl},
|
||||||
frozen::FrozenClientRequest,
|
frozen::FrozenClientRequest,
|
||||||
sender::{PrepForSendingError, RequestSender, SendClientRequest},
|
sender::{PrepForSendingError, RequestSender, SendClientRequest},
|
||||||
|
@ -340,7 +340,7 @@ impl ClientRequest {
|
||||||
/// Complete request construction and send body.
|
/// Complete request construction and send body.
|
||||||
pub fn send_body<B>(self, body: B) -> SendClientRequest
|
pub fn send_body<B>(self, body: B) -> SendClientRequest
|
||||||
where
|
where
|
||||||
B: Into<AnyBody>,
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
let slf = match self.prep_for_sending() {
|
let slf = match self.prep_for_sending() {
|
||||||
Ok(slf) => slf,
|
Ok(slf) => slf,
|
||||||
|
|
|
@ -8,7 +8,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::BodyStream,
|
body::{BodyStream, MessageBody},
|
||||||
error::HttpError,
|
error::HttpError,
|
||||||
header::{self, HeaderMap, HeaderName, TryIntoHeaderValue},
|
header::{self, HeaderMap, HeaderName, TryIntoHeaderValue},
|
||||||
RequestHead, RequestHeadType,
|
RequestHead, RequestHeadType,
|
||||||
|
@ -189,15 +189,17 @@ impl RequestSender {
|
||||||
body: B,
|
body: B,
|
||||||
) -> SendClientRequest
|
) -> SendClientRequest
|
||||||
where
|
where
|
||||||
B: Into<AnyBody>,
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
let req = match self {
|
let req = match self {
|
||||||
RequestSender::Owned(head) => {
|
RequestSender::Owned(head) => ConnectRequest::Client(
|
||||||
ConnectRequest::Client(RequestHeadType::Owned(head), body.into(), addr)
|
RequestHeadType::Owned(head),
|
||||||
}
|
AnyBody::from_message_body(body).into_boxed(),
|
||||||
|
addr,
|
||||||
|
),
|
||||||
RequestSender::Rc(head, extra_headers) => ConnectRequest::Client(
|
RequestSender::Rc(head, extra_headers) => ConnectRequest::Client(
|
||||||
RequestHeadType::Rc(head, extra_headers),
|
RequestHeadType::Rc(head, extra_headers),
|
||||||
body.into(),
|
AnyBody::from_message_body(body).into_boxed(),
|
||||||
addr,
|
addr,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
@ -229,9 +231,7 @@ impl RequestSender {
|
||||||
response_decompress,
|
response_decompress,
|
||||||
timeout,
|
timeout,
|
||||||
config,
|
config,
|
||||||
AnyBody::Bytes {
|
AnyBody::copy_from_slice(body.as_bytes()),
|
||||||
body: Bytes::from(body),
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,9 +260,7 @@ impl RequestSender {
|
||||||
response_decompress,
|
response_decompress,
|
||||||
timeout,
|
timeout,
|
||||||
config,
|
config,
|
||||||
AnyBody::Bytes {
|
AnyBody::copy_from_slice(body.as_bytes()),
|
||||||
body: Bytes::from(body),
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue