1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

align remaining header map terminology (#2510)

This commit is contained in:
Rob Ede
2021-12-13 16:08:08 +00:00
committed by GitHub
parent 551a0d973c
commit 11ee8ec3ab
47 changed files with 608 additions and 503 deletions

View File

@ -270,10 +270,10 @@ where
type Future = H2ServiceHandlerResponse<T, S, B>;
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.flow.service.poll_ready(cx).map_err(|e| {
let e = e.into();
error!("Service readiness error: {:?}", e);
DispatchError::Service(e)
self.flow.service.poll_ready(cx).map_err(|err| {
let err = err.into();
error!("Service readiness error: {:?}", err);
DispatchError::Service(err)
})
}
@ -297,7 +297,6 @@ where
T: AsyncRead + AsyncWrite + Unpin,
S::Future: 'static,
{
Incoming(Dispatcher<T, S, B, (), ()>),
Handshake(
Option<Rc<HttpFlow<S, (), ()>>>,
Option<ServiceConfig>,
@ -305,6 +304,7 @@ where
OnConnectData,
HandshakeWithTimeout<T>,
),
Established(Dispatcher<T, S, B, (), ()>),
}
pub struct H2ServiceHandlerResponse<T, S, B>
@ -332,7 +332,6 @@ where
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.state {
State::Incoming(ref mut disp) => Pin::new(disp).poll(cx),
State::Handshake(
ref mut srv,
ref mut config,
@ -343,7 +342,7 @@ where
Ok((conn, timer)) => {
let on_connect_data = mem::take(conn_data);
self.state = State::Incoming(Dispatcher::new(
self.state = State::Established(Dispatcher::new(
conn,
srv.take().unwrap(),
config.take().unwrap(),
@ -360,6 +359,8 @@ where
Poll::Ready(Err(err))
}
},
State::Established(ref mut disp) => Pin::new(disp).poll(cx),
}
}
}

View File

@ -16,6 +16,7 @@ pub trait Sealed {
}
impl Sealed for HeaderName {
#[inline]
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
Ok(Cow::Borrowed(self))
}
@ -23,6 +24,7 @@ impl Sealed for HeaderName {
impl AsHeaderName for HeaderName {}
impl Sealed for &HeaderName {
#[inline]
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
Ok(Cow::Borrowed(*self))
}
@ -30,6 +32,7 @@ impl Sealed for &HeaderName {
impl AsHeaderName for &HeaderName {}
impl Sealed for &str {
#[inline]
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
HeaderName::from_str(self).map(Cow::Owned)
}
@ -37,6 +40,7 @@ impl Sealed for &str {
impl AsHeaderName for &str {}
impl Sealed for String {
#[inline]
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
HeaderName::from_str(self).map(Cow::Owned)
}
@ -44,6 +48,7 @@ impl Sealed for String {
impl AsHeaderName for String {}
impl Sealed for &String {
#[inline]
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
HeaderName::from_str(self).map(Cow::Owned)
}

View File

@ -1,22 +1,20 @@
//! [`IntoHeaderPair`] trait and implementations.
//! [`TryIntoHeaderPair`] trait and implementations.
use std::convert::TryFrom as _;
use http::{
header::{HeaderName, InvalidHeaderName, InvalidHeaderValue},
Error as HttpError, HeaderValue,
use super::{
Header, HeaderName, HeaderValue, InvalidHeaderName, InvalidHeaderValue, TryIntoHeaderValue,
};
use crate::error::HttpError;
use super::{Header, IntoHeaderValue};
/// An interface for types that can be converted into a [`HeaderName`]/[`HeaderValue`] pair for
/// An interface for types that can be converted into a [`HeaderName`] + [`HeaderValue`] pair for
/// insertion into a [`HeaderMap`].
///
/// [`HeaderMap`]: super::HeaderMap
pub trait IntoHeaderPair: Sized {
pub trait TryIntoHeaderPair: Sized {
type Error: Into<HttpError>;
fn try_into_header_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error>;
fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error>;
}
#[derive(Debug)]
@ -34,14 +32,14 @@ impl From<InvalidHeaderPart> for HttpError {
}
}
impl<V> IntoHeaderPair for (HeaderName, V)
impl<V> TryIntoHeaderPair for (HeaderName, V)
where
V: IntoHeaderValue,
V: TryIntoHeaderValue,
V::Error: Into<InvalidHeaderValue>,
{
type Error = InvalidHeaderPart;
fn try_into_header_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
let (name, value) = self;
let value = value
.try_into_value()
@ -50,14 +48,14 @@ where
}
}
impl<V> IntoHeaderPair for (&HeaderName, V)
impl<V> TryIntoHeaderPair for (&HeaderName, V)
where
V: IntoHeaderValue,
V: TryIntoHeaderValue,
V::Error: Into<InvalidHeaderValue>,
{
type Error = InvalidHeaderPart;
fn try_into_header_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
let (name, value) = self;
let value = value
.try_into_value()
@ -66,14 +64,14 @@ where
}
}
impl<V> IntoHeaderPair for (&[u8], V)
impl<V> TryIntoHeaderPair for (&[u8], V)
where
V: IntoHeaderValue,
V: TryIntoHeaderValue,
V::Error: Into<InvalidHeaderValue>,
{
type Error = InvalidHeaderPart;
fn try_into_header_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
let (name, value) = self;
let name = HeaderName::try_from(name).map_err(InvalidHeaderPart::Name)?;
let value = value
@ -83,14 +81,14 @@ where
}
}
impl<V> IntoHeaderPair for (&str, V)
impl<V> TryIntoHeaderPair for (&str, V)
where
V: IntoHeaderValue,
V: TryIntoHeaderValue,
V::Error: Into<InvalidHeaderValue>,
{
type Error = InvalidHeaderPart;
fn try_into_header_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
let (name, value) = self;
let name = HeaderName::try_from(name).map_err(InvalidHeaderPart::Name)?;
let value = value
@ -100,23 +98,25 @@ where
}
}
impl<V> IntoHeaderPair for (String, V)
impl<V> TryIntoHeaderPair for (String, V)
where
V: IntoHeaderValue,
V: TryIntoHeaderValue,
V::Error: Into<InvalidHeaderValue>,
{
type Error = InvalidHeaderPart;
fn try_into_header_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
#[inline]
fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
let (name, value) = self;
(name.as_str(), value).try_into_header_pair()
(name.as_str(), value).try_into_pair()
}
}
impl<T: Header> IntoHeaderPair for T {
type Error = <T as IntoHeaderValue>::Error;
impl<T: Header> TryIntoHeaderPair for T {
type Error = <T as TryIntoHeaderValue>::Error;
fn try_into_header_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
#[inline]
fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> {
Ok((T::name(), self.try_into_value()?))
}
}

View File

@ -1,4 +1,4 @@
//! [`IntoHeaderValue`] trait and implementations.
//! [`TryIntoHeaderValue`] trait and implementations.
use std::convert::TryFrom as _;
@ -7,7 +7,7 @@ use http::{header::InvalidHeaderValue, Error as HttpError, HeaderValue};
use mime::Mime;
/// An interface for types that can be converted into a [`HeaderValue`].
pub trait IntoHeaderValue: Sized {
pub trait TryIntoHeaderValue: Sized {
/// The type returned in the event of a conversion error.
type Error: Into<HttpError>;
@ -15,7 +15,7 @@ pub trait IntoHeaderValue: Sized {
fn try_into_value(self) -> Result<HeaderValue, Self::Error>;
}
impl IntoHeaderValue for HeaderValue {
impl TryIntoHeaderValue for HeaderValue {
type Error = InvalidHeaderValue;
#[inline]
@ -24,7 +24,7 @@ impl IntoHeaderValue for HeaderValue {
}
}
impl IntoHeaderValue for &HeaderValue {
impl TryIntoHeaderValue for &HeaderValue {
type Error = InvalidHeaderValue;
#[inline]
@ -33,7 +33,7 @@ impl IntoHeaderValue for &HeaderValue {
}
}
impl IntoHeaderValue for &str {
impl TryIntoHeaderValue for &str {
type Error = InvalidHeaderValue;
#[inline]
@ -42,7 +42,7 @@ impl IntoHeaderValue for &str {
}
}
impl IntoHeaderValue for &[u8] {
impl TryIntoHeaderValue for &[u8] {
type Error = InvalidHeaderValue;
#[inline]
@ -51,7 +51,7 @@ impl IntoHeaderValue for &[u8] {
}
}
impl IntoHeaderValue for Bytes {
impl TryIntoHeaderValue for Bytes {
type Error = InvalidHeaderValue;
#[inline]
@ -60,7 +60,7 @@ impl IntoHeaderValue for Bytes {
}
}
impl IntoHeaderValue for Vec<u8> {
impl TryIntoHeaderValue for Vec<u8> {
type Error = InvalidHeaderValue;
#[inline]
@ -69,7 +69,7 @@ impl IntoHeaderValue for Vec<u8> {
}
}
impl IntoHeaderValue for String {
impl TryIntoHeaderValue for String {
type Error = InvalidHeaderValue;
#[inline]
@ -78,7 +78,7 @@ impl IntoHeaderValue for String {
}
}
impl IntoHeaderValue for usize {
impl TryIntoHeaderValue for usize {
type Error = InvalidHeaderValue;
#[inline]
@ -87,7 +87,7 @@ impl IntoHeaderValue for usize {
}
}
impl IntoHeaderValue for i64 {
impl TryIntoHeaderValue for i64 {
type Error = InvalidHeaderValue;
#[inline]
@ -96,7 +96,7 @@ impl IntoHeaderValue for i64 {
}
}
impl IntoHeaderValue for u64 {
impl TryIntoHeaderValue for u64 {
type Error = InvalidHeaderValue;
#[inline]
@ -105,7 +105,7 @@ impl IntoHeaderValue for u64 {
}
}
impl IntoHeaderValue for i32 {
impl TryIntoHeaderValue for i32 {
type Error = InvalidHeaderValue;
#[inline]
@ -114,7 +114,7 @@ impl IntoHeaderValue for i32 {
}
}
impl IntoHeaderValue for u32 {
impl TryIntoHeaderValue for u32 {
type Error = InvalidHeaderValue;
#[inline]
@ -123,7 +123,7 @@ impl IntoHeaderValue for u32 {
}
}
impl IntoHeaderValue for Mime {
impl TryIntoHeaderValue for Mime {
type Error = InvalidHeaderValue;
#[inline]

View File

@ -333,7 +333,7 @@ impl HeaderMap {
}
}
/// Inserts a name-value pair into the map.
/// Inserts (overrides) a name-value pair in the map.
///
/// If the map already contained this key, the new value is associated with the key and all
/// previous values are removed and returned as a `Removed` iterator. The key is not updated;
@ -372,7 +372,7 @@ impl HeaderMap {
Removed::new(value)
}
/// Inserts a name-value pair into the map.
/// Appends a name-value pair to the map.
///
/// If the map already contained this key, the new value is added to the list of values
/// currently associated with the key. The key is not updated; this matters for types that can

View File

@ -37,8 +37,8 @@ mod shared;
mod utils;
pub use self::as_name::AsHeaderName;
pub use self::into_pair::IntoHeaderPair;
pub use self::into_value::IntoHeaderValue;
pub use self::into_pair::TryIntoHeaderPair;
pub use self::into_value::TryIntoHeaderValue;
pub use self::map::HeaderMap;
pub use self::shared::{
parse_extended_value, q, Charset, ContentEncoding, ExtendedValue, HttpDate, LanguageTag,
@ -49,7 +49,7 @@ pub use self::utils::{
};
/// An interface for types that already represent a valid header.
pub trait Header: IntoHeaderValue {
pub trait Header: TryIntoHeaderValue {
/// Returns the name of the header field
fn name() -> HeaderName;

View File

@ -5,7 +5,7 @@ use http::header::InvalidHeaderValue;
use crate::{
error::ParseError,
header::{self, from_one_raw_str, Header, HeaderName, HeaderValue, IntoHeaderValue},
header::{self, from_one_raw_str, Header, HeaderName, HeaderValue, TryIntoHeaderValue},
HttpMessage,
};
@ -96,7 +96,7 @@ impl TryFrom<&str> for ContentEncoding {
}
}
impl IntoHeaderValue for ContentEncoding {
impl TryIntoHeaderValue for ContentEncoding {
type Error = InvalidHeaderValue;
fn try_into_value(self) -> Result<http::HeaderValue, Self::Error> {

View File

@ -4,7 +4,8 @@ use bytes::BytesMut;
use http::header::{HeaderValue, InvalidHeaderValue};
use crate::{
config::DATE_VALUE_LENGTH, error::ParseError, header::IntoHeaderValue, helpers::MutWriter,
config::DATE_VALUE_LENGTH, error::ParseError, header::TryIntoHeaderValue,
helpers::MutWriter,
};
/// A timestamp with HTTP-style formatting and parsing.
@ -29,7 +30,7 @@ impl fmt::Display for HttpDate {
}
}
impl IntoHeaderValue for HttpDate {
impl TryIntoHeaderValue for HttpDate {
type Error = InvalidHeaderValue;
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {

View File

@ -11,7 +11,7 @@ use bytestring::ByteString;
use crate::{
body::{BoxBody, MessageBody},
extensions::Extensions,
header::{self, HeaderMap, IntoHeaderValue},
header::{self, HeaderMap, TryIntoHeaderValue},
message::{BoxedResponseHead, ResponseHead},
Error, ResponseBuilder, StatusCode,
};

View File

@ -8,7 +8,7 @@ use std::{
use crate::{
body::{EitherBody, MessageBody},
error::{Error, HttpError},
header::{self, IntoHeaderPair, IntoHeaderValue},
header::{self, TryIntoHeaderPair, TryIntoHeaderValue},
message::{BoxedResponseHead, ConnectionType, ResponseHead},
Extensions, Response, StatusCode,
};
@ -90,12 +90,9 @@ impl ResponseBuilder {
/// assert!(res.headers().contains_key("content-type"));
/// assert!(res.headers().contains_key("x-test"));
/// ```
pub fn insert_header<H>(&mut self, header: H) -> &mut Self
where
H: IntoHeaderPair,
{
pub fn insert_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self {
if let Some(parts) = self.inner() {
match header.try_into_header_pair() {
match header.try_into_pair() {
Ok((key, value)) => {
parts.headers.insert(key, value);
}
@ -121,12 +118,9 @@ impl ResponseBuilder {
/// assert_eq!(res.headers().get_all("content-type").count(), 1);
/// assert_eq!(res.headers().get_all("x-test").count(), 2);
/// ```
pub fn append_header<H>(&mut self, header: H) -> &mut Self
where
H: IntoHeaderPair,
{
pub fn append_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self {
if let Some(parts) = self.inner() {
match header.try_into_header_pair() {
match header.try_into_pair() {
Ok((key, value)) => parts.headers.append(key, value),
Err(e) => self.err = Some(e.into()),
};
@ -157,7 +151,7 @@ impl ResponseBuilder {
#[inline]
pub fn upgrade<V>(&mut self, value: V) -> &mut Self
where
V: IntoHeaderValue,
V: TryIntoHeaderValue,
{
if let Some(parts) = self.inner() {
parts.set_connection_type(ConnectionType::Upgrade);
@ -195,7 +189,7 @@ impl ResponseBuilder {
#[inline]
pub fn content_type<V>(&mut self, value: V) -> &mut Self
where
V: IntoHeaderValue,
V: TryIntoHeaderValue,
{
if let Some(parts) = self.inner() {
match value.try_into_value() {

View File

@ -14,7 +14,7 @@ use bytes::{Bytes, BytesMut};
use http::{Method, Uri, Version};
use crate::{
header::{HeaderMap, IntoHeaderPair},
header::{HeaderMap, TryIntoHeaderPair},
payload::Payload,
Request,
};
@ -92,11 +92,8 @@ impl TestRequest {
}
/// Insert a header, replacing any that were set with an equivalent field name.
pub fn insert_header<H>(&mut self, header: H) -> &mut Self
where
H: IntoHeaderPair,
{
match header.try_into_header_pair() {
pub fn insert_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self {
match header.try_into_pair() {
Ok((key, value)) => {
parts(&mut self.0).headers.insert(key, value);
}
@ -109,11 +106,8 @@ impl TestRequest {
}
/// Append a header, keeping any that were set with an equivalent field name.
pub fn append_header<H>(&mut self, header: H) -> &mut Self
where
H: IntoHeaderPair,
{
match header.try_into_header_pair() {
pub fn append_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self {
match header.try_into_pair() {
Ok((key, value)) => {
parts(&mut self.0).headers.append(key, value);
}