1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-02-17 10:13:30 +01:00

use modern signatures for awc send_* and header methods (#2553)

This commit is contained in:
Rob Ede 2021-12-28 03:22:22 +00:00 committed by GitHub
parent 4616ca8ee6
commit 96a4dc9dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 162 deletions

View File

@ -1,6 +1,11 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
- `*::send_json` and `*::send_form` methods now receive `impl Serialize`. [#2553]
- `FrozenClientRequest::extra_header` now uses receives an `impl TryIntoHeaderPair`. [#2553]
- Remove unnecessary `Unpin` bounds on `*::send_stream`. [#2553]
[#2553]: https://github.com/actix/actix-web/pull/2553
## 3.0.0-beta.15 - 2021-12-27 ## 3.0.0-beta.15 - 2021-12-27

View File

@ -1,17 +1,13 @@
use std::{ use std::{
borrow::Cow,
fmt, mem, fmt, mem,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
}; };
use bytes::{Bytes, BytesMut}; use bytes::Bytes;
use futures_core::Stream;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use actix_http::body::{BodySize, BodyStream, BoxBody, MessageBody, SizedStream}; use actix_http::body::{BodySize, BoxBody, MessageBody};
use crate::BoxError;
pin_project! { pin_project! {
/// Represents various types of HTTP message body. /// Represents various types of HTTP message body.
@ -160,91 +156,6 @@ impl<S: fmt::Debug> fmt::Debug for AnyBody<S> {
} }
} }
impl<B> From<&'static str> for AnyBody<B> {
fn from(string: &'static str) -> Self {
Self::Bytes {
body: Bytes::from_static(string.as_ref()),
}
}
}
impl<B> From<&'static [u8]> for AnyBody<B> {
fn from(bytes: &'static [u8]) -> Self {
Self::Bytes {
body: Bytes::from_static(bytes),
}
}
}
impl<B> From<Vec<u8>> for AnyBody<B> {
fn from(vec: Vec<u8>) -> Self {
Self::Bytes {
body: Bytes::from(vec),
}
}
}
impl<B> From<String> for AnyBody<B> {
fn from(string: String) -> Self {
Self::Bytes {
body: Bytes::from(string),
}
}
}
impl<B> From<&'_ String> for AnyBody<B> {
fn from(string: &String) -> Self {
Self::Bytes {
body: Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(&string)),
}
}
}
impl<B> From<Cow<'_, str>> for AnyBody<B> {
fn from(string: Cow<'_, str>) -> Self {
match string {
Cow::Owned(s) => Self::from(s),
Cow::Borrowed(s) => Self::Bytes {
body: Bytes::copy_from_slice(AsRef::<[u8]>::as_ref(s)),
},
}
}
}
impl<B> From<Bytes> for AnyBody<B> {
fn from(bytes: Bytes) -> Self {
Self::Bytes { body: bytes }
}
}
impl<B> From<BytesMut> for AnyBody<B> {
fn from(bytes: BytesMut) -> Self {
Self::Bytes {
body: bytes.freeze(),
}
}
}
impl<S, E> From<SizedStream<S>> for AnyBody
where
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
fn from(stream: SizedStream<S>) -> Self {
AnyBody::new_boxed(stream)
}
}
impl<S, E> From<BodyStream<S>> for AnyBody
where
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static,
{
fn from(stream: BodyStream<S>) -> Self {
AnyBody::new_boxed(stream)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::marker::PhantomPinned; use std::marker::PhantomPinned;

View File

@ -1,4 +1,4 @@
use std::{convert::TryFrom, net, rc::Rc, time::Duration}; use std::{net, rc::Rc, time::Duration};
use bytes::Bytes; use bytes::Bytes;
use futures_core::Stream; use futures_core::Stream;
@ -7,7 +7,7 @@ use serde::Serialize;
use actix_http::{ use actix_http::{
body::MessageBody, body::MessageBody,
error::HttpError, error::HttpError,
header::{HeaderMap, HeaderName, TryIntoHeaderValue}, header::{HeaderMap, TryIntoHeaderPair},
Method, RequestHead, Uri, Method, RequestHead, Uri,
}; };
@ -18,6 +18,7 @@ use crate::{
}; };
/// `FrozenClientRequest` struct represents cloneable client request. /// `FrozenClientRequest` struct represents cloneable client request.
///
/// It could be used to send same request multiple times. /// It could be used to send same request multiple times.
#[derive(Clone)] #[derive(Clone)]
pub struct FrozenClientRequest { pub struct FrozenClientRequest {
@ -83,7 +84,7 @@ impl FrozenClientRequest {
/// Send a streaming body. /// Send a streaming body.
pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest pub fn send_stream<S, E>(&self, stream: S) -> SendClientRequest
where where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static, S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static, E: Into<BoxError> + 'static,
{ {
RequestSender::Rc(self.head.clone(), None).send_stream( RequestSender::Rc(self.head.clone(), None).send_stream(
@ -105,20 +106,14 @@ impl FrozenClientRequest {
) )
} }
/// Create a `FrozenSendBuilder` with extra headers /// Clones this `FrozenClientRequest`, returning a new one with extra headers added.
pub fn extra_headers(&self, extra_headers: HeaderMap) -> FrozenSendBuilder { pub fn extra_headers(&self, extra_headers: HeaderMap) -> FrozenSendBuilder {
FrozenSendBuilder::new(self.clone(), extra_headers) FrozenSendBuilder::new(self.clone(), extra_headers)
} }
/// Create a `FrozenSendBuilder` with an extra header /// Clones this `FrozenClientRequest`, returning a new one with the extra header added.
pub fn extra_header<K, V>(&self, key: K, value: V) -> FrozenSendBuilder pub fn extra_header(&self, header: impl TryIntoHeaderPair) -> FrozenSendBuilder {
where self.extra_headers(HeaderMap::new()).extra_header(header)
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: TryIntoHeaderValue,
{
self.extra_headers(HeaderMap::new())
.extra_header(key, value)
} }
} }
@ -139,29 +134,20 @@ impl FrozenSendBuilder {
} }
/// Insert a header, it overrides existing header in `FrozenClientRequest`. /// Insert a header, it overrides existing header in `FrozenClientRequest`.
pub fn extra_header<K, V>(mut self, key: K, value: V) -> Self pub fn extra_header(mut self, header: impl TryIntoHeaderPair) -> Self {
where match header.try_into_pair() {
HeaderName: TryFrom<K>, Ok((key, value)) => {
<HeaderName as TryFrom<K>>::Error: Into<HttpError>, self.extra_headers.insert(key, value);
V: TryIntoHeaderValue, }
{
match HeaderName::try_from(key) { Err(err) => self.err = Some(err.into()),
Ok(key) => match value.try_into_value() {
Ok(value) => {
self.extra_headers.insert(key, value);
}
Err(e) => self.err = Some(e.into()),
},
Err(e) => self.err = Some(e.into()),
} }
self self
} }
/// 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(self, body: impl MessageBody + 'static) -> SendClientRequest {
where
B: MessageBody + 'static,
{
if let Some(e) = self.err { if let Some(e) = self.err {
return e.into(); return e.into();
} }
@ -176,9 +162,9 @@ impl FrozenSendBuilder {
} }
/// Complete request construction and send a json body. /// Complete request construction and send a json body.
pub fn send_json<T: Serialize>(self, value: &T) -> SendClientRequest { pub fn send_json(self, value: impl Serialize) -> SendClientRequest {
if let Some(e) = self.err { if let Some(err) = self.err {
return e.into(); return err.into();
} }
RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_json( RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_json(
@ -191,7 +177,7 @@ impl FrozenSendBuilder {
} }
/// Complete request construction and send an urlencoded body. /// Complete request construction and send an urlencoded body.
pub fn send_form<T: Serialize>(self, value: &T) -> SendClientRequest { pub fn send_form(self, value: impl Serialize) -> SendClientRequest {
if let Some(e) = self.err { if let Some(e) = self.err {
return e.into(); return e.into();
} }
@ -208,7 +194,7 @@ impl FrozenSendBuilder {
/// Complete request construction and send a streaming body. /// Complete request construction and send a streaming body.
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
where where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static, S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static, E: Into<BoxError> + 'static,
{ {
if let Some(e) = self.err { if let Some(e) = self.err {

View File

@ -190,7 +190,9 @@ 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::from(bytes.clone()), Some(ref bytes) => AnyBody::Bytes {
body: bytes.clone(),
},
// TODO: should this be AnyBody::Empty or AnyBody::None. // TODO: should this be AnyBody::Empty or AnyBody::None.
_ => AnyBody::empty(), _ => AnyBody::empty(),
} }

View File

@ -385,7 +385,7 @@ impl ClientRequest {
/// Set an streaming body and generate `ClientRequest`. /// Set an streaming body and generate `ClientRequest`.
pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest pub fn send_stream<S, E>(self, stream: S) -> SendClientRequest
where where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static, S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static, E: Into<BoxError> + 'static,
{ {
let slf = match self.prep_for_sending() { let slf = match self.prep_for_sending() {

View File

@ -7,8 +7,8 @@ use std::{
}; };
use actix_http::{ use actix_http::{
error::PayloadError, header, header::HeaderMap, BoxedPayloadStream, Extensions, error::PayloadError, header::HeaderMap, BoxedPayloadStream, Extensions, HttpMessage,
HttpMessage, Payload, ResponseHead, StatusCode, Version, Payload, ResponseHead, StatusCode, Version,
}; };
use actix_rt::time::{sleep, Sleep}; use actix_rt::time::{sleep, Sleep};
use bytes::Bytes; use bytes::Bytes;
@ -119,12 +119,13 @@ impl<S> ClientResponse<S> {
if self.extensions().get::<Cookies>().is_none() { if self.extensions().get::<Cookies>().is_none() {
let mut cookies = Vec::new(); let mut cookies = Vec::new();
for hdr in self.headers().get_all(&header::SET_COOKIE) { for hdr in self.headers().get_all(&actix_http::header::SET_COOKIE) {
let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?; let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
cookies.push(Cookie::parse_encoded(s)?.into_owned()); cookies.push(Cookie::parse_encoded(s)?.into_owned());
} }
self.extensions_mut().insert(Cookies(cookies)); self.extensions_mut().insert(Cookies(cookies));
} }
Ok(Ref::map(self.extensions(), |ext| { Ok(Ref::map(self.extensions(), |ext| {
&ext.get::<Cookies>().unwrap().0 &ext.get::<Cookies>().unwrap().0
})) }))

View File

@ -181,17 +181,14 @@ pub(crate) enum RequestSender {
} }
impl RequestSender { impl RequestSender {
pub(crate) fn send_body<B>( pub(crate) fn send_body(
self, self,
addr: Option<net::SocketAddr>, addr: Option<net::SocketAddr>,
response_decompress: bool, response_decompress: bool,
timeout: Option<Duration>, timeout: Option<Duration>,
config: &ClientConfig, config: &ClientConfig,
body: B, body: impl MessageBody + 'static,
) -> SendClientRequest ) -> SendClientRequest {
where
B: MessageBody + 'static,
{
let req = match self { let req = match self {
RequestSender::Owned(head) => ConnectRequest::Client( RequestSender::Owned(head) => ConnectRequest::Client(
RequestHeadType::Owned(head), RequestHeadType::Owned(head),
@ -210,15 +207,15 @@ impl RequestSender {
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout)) SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
} }
pub(crate) fn send_json<T: Serialize>( pub(crate) fn send_json(
mut self, mut self,
addr: Option<net::SocketAddr>, addr: Option<net::SocketAddr>,
response_decompress: bool, response_decompress: bool,
timeout: Option<Duration>, timeout: Option<Duration>,
config: &ClientConfig, config: &ClientConfig,
value: &T, value: impl Serialize,
) -> SendClientRequest { ) -> SendClientRequest {
let body = match serde_json::to_string(value) { let body = match serde_json::to_string(&value) {
Ok(body) => body, Ok(body) => body,
Err(err) => return PrepForSendingError::Json(err).into(), Err(err) => return PrepForSendingError::Json(err).into(),
}; };
@ -227,22 +224,16 @@ impl RequestSender {
return e.into(); return e.into();
} }
self.send_body( self.send_body(addr, response_decompress, timeout, config, body)
addr,
response_decompress,
timeout,
config,
AnyBody::from_message_body(body.into_bytes()),
)
} }
pub(crate) fn send_form<T: Serialize>( pub(crate) fn send_form(
mut self, mut self,
addr: Option<net::SocketAddr>, addr: Option<net::SocketAddr>,
response_decompress: bool, response_decompress: bool,
timeout: Option<Duration>, timeout: Option<Duration>,
config: &ClientConfig, config: &ClientConfig,
value: &T, value: impl Serialize,
) -> SendClientRequest { ) -> SendClientRequest {
let body = match serde_urlencoded::to_string(value) { let body = match serde_urlencoded::to_string(value) {
Ok(body) => body, Ok(body) => body,
@ -250,19 +241,13 @@ impl RequestSender {
}; };
// set content-type // set content-type
if let Err(e) = if let Err(err) =
self.set_header_if_none(header::CONTENT_TYPE, "application/x-www-form-urlencoded") self.set_header_if_none(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
{ {
return e.into(); return err.into();
} }
self.send_body( self.send_body(addr, response_decompress, timeout, config, body)
addr,
response_decompress,
timeout,
config,
AnyBody::from_message_body(body.into_bytes()),
)
} }
pub(crate) fn send_stream<S, E>( pub(crate) fn send_stream<S, E>(
@ -274,7 +259,7 @@ impl RequestSender {
stream: S, stream: S,
) -> SendClientRequest ) -> SendClientRequest
where where
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static, S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<BoxError> + 'static, E: Into<BoxError> + 'static,
{ {
self.send_body( self.send_body(
@ -282,7 +267,7 @@ impl RequestSender {
response_decompress, response_decompress,
timeout, timeout,
config, config,
AnyBody::new_boxed(BodyStream::new(stream)), BodyStream::new(stream),
) )
} }
@ -293,7 +278,7 @@ impl RequestSender {
timeout: Option<Duration>, timeout: Option<Duration>,
config: &ClientConfig, config: &ClientConfig,
) -> SendClientRequest { ) -> SendClientRequest {
self.send_body(addr, response_decompress, timeout, config, AnyBody::empty()) self.send_body(addr, response_decompress, timeout, config, ())
} }
fn set_header_if_none<V>(&mut self, key: HeaderName, value: V) -> Result<(), HttpError> fn set_header_if_none<V>(&mut self, key: HeaderName, value: V) -> Result<(), HttpError>