1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-31 08:57:00 +02:00

port hyper header

This commit is contained in:
Nikolay Kim
2018-03-06 00:43:25 -08:00
parent e182ed33b1
commit 32b5544ad9
36 changed files with 3639 additions and 166 deletions

View File

@@ -1,22 +1,32 @@
//! Various http headers
// A lot of code is inspired by hyper
// This is mostly copy of [hyper](https://github.com/hyperium/hyper/tree/master/src/header)
use bytes::Bytes;
use std::fmt;
use std::str::FromStr;
use bytes::{Bytes, BytesMut};
use http::{Error as HttpError};
use http::header::{InvalidHeaderValue, InvalidHeaderValueBytes};
use http::header::GetAll;
use mime::Mime;
pub use cookie::{Cookie, CookieBuilder};
pub use http_range::HttpRange;
pub use http::header::{HeaderName, HeaderValue};
#[doc(hidden)]
pub mod http {
pub use http::header::*;
}
use error::ParseError;
use httpmessage::HttpMessage;
pub use httpresponse::ConnectionType;
mod common;
mod httpdate;
mod shared;
#[doc(hidden)]
pub use self::common::*;
pub use self::httpdate::HttpDate;
#[doc(hidden)]
pub use self::shared::*;
#[doc(hidden)]
@@ -24,7 +34,7 @@ pub use self::httpdate::HttpDate;
pub trait Header where Self: IntoHeaderValue {
/// Returns the name of the header field
fn name() -> HeaderName;
fn name() -> http::HeaderName;
/// Parse a header
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError>;
@@ -37,42 +47,69 @@ pub trait IntoHeaderValue: Sized {
type Error: Into<HttpError>;
/// Cast from PyObject to a concrete Python object type.
fn try_into(self) -> Result<HeaderValue, Self::Error>;
fn try_into(self) -> Result<http::HeaderValue, Self::Error>;
}
impl IntoHeaderValue for HeaderValue {
type Error = InvalidHeaderValue;
impl IntoHeaderValue for http::HeaderValue {
type Error = http::InvalidHeaderValue;
#[inline]
fn try_into(self) -> Result<HeaderValue, Self::Error> {
fn try_into(self) -> Result<http::HeaderValue, Self::Error> {
Ok(self)
}
}
impl<'a> IntoHeaderValue for &'a str {
type Error = InvalidHeaderValue;
type Error = http::InvalidHeaderValue;
#[inline]
fn try_into(self) -> Result<HeaderValue, Self::Error> {
fn try_into(self) -> Result<http::HeaderValue, Self::Error> {
self.parse()
}
}
impl<'a> IntoHeaderValue for &'a [u8] {
type Error = InvalidHeaderValue;
type Error = http::InvalidHeaderValue;
#[inline]
fn try_into(self) -> Result<HeaderValue, Self::Error> {
HeaderValue::from_bytes(self)
fn try_into(self) -> Result<http::HeaderValue, Self::Error> {
http::HeaderValue::from_bytes(self)
}
}
impl IntoHeaderValue for Bytes {
type Error = InvalidHeaderValueBytes;
type Error = http::InvalidHeaderValueBytes;
#[inline]
fn try_into(self) -> Result<HeaderValue, Self::Error> {
HeaderValue::from_shared(self)
fn try_into(self) -> Result<http::HeaderValue, Self::Error> {
http::HeaderValue::from_shared(self)
}
}
impl IntoHeaderValue for Vec<u8> {
type Error = http::InvalidHeaderValueBytes;
#[inline]
fn try_into(self) -> Result<http::HeaderValue, Self::Error> {
http::HeaderValue::from_shared(Bytes::from(self))
}
}
impl IntoHeaderValue for String {
type Error = http::InvalidHeaderValueBytes;
#[inline]
fn try_into(self) -> Result<http::HeaderValue, Self::Error> {
http::HeaderValue::from_shared(Bytes::from(self))
}
}
impl IntoHeaderValue for Mime {
type Error = http::InvalidHeaderValueBytes;
#[inline]
fn try_into(self) -> Result<http::HeaderValue, Self::Error> {
http::HeaderValue::from_shared(Bytes::from(format!("{}", self)))
}
}
@@ -133,3 +170,81 @@ impl<'a> From<&'a str> for ContentEncoding {
}
}
}
#[doc(hidden)]
pub(crate) struct Writer {
buf: BytesMut,
}
impl Writer {
fn new() -> Writer {
Writer{buf: BytesMut::new()}
}
fn take(&mut self) -> Bytes {
self.buf.take().freeze()
}
}
impl fmt::Write for Writer {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
self.buf.extend_from_slice(s.as_bytes());
Ok(())
}
#[inline]
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
fmt::write(self, args)
}
}
#[inline]
#[doc(hidden)]
/// Reads a comma-delimited raw header into a Vec.
pub fn from_comma_delimited<T: FromStr>(all: GetAll<http::HeaderValue>)
-> Result<Vec<T>, ParseError>
{
let mut result = Vec::new();
for h in all {
let s = h.to_str().map_err(|_| ParseError::Header)?;
result.extend(s.split(',')
.filter_map(|x| match x.trim() {
"" => None,
y => Some(y)
})
.filter_map(|x| x.trim().parse().ok()))
}
Ok(result)
}
#[inline]
#[doc(hidden)]
/// Reads a single string when parsing a header.
pub fn from_one_raw_str<T: FromStr>(val: Option<&http::HeaderValue>)
-> Result<T, ParseError>
{
if let Some(line) = val {
let line = line.to_str().map_err(|_| ParseError::Header)?;
if !line.is_empty() {
return T::from_str(line).or(Err(ParseError::Header))
}
}
Err(ParseError::Header)
}
#[inline]
#[doc(hidden)]
/// Format an array into a comma-delimited string.
pub fn fmt_comma_delimited<T>(f: &mut fmt::Formatter, parts: &[T]) -> fmt::Result
where T: fmt::Display
{
let mut iter = parts.iter();
if let Some(part) = iter.next() {
fmt::Display::fmt(part, f)?;
}
for part in iter {
f.write_str(", ")?;
fmt::Display::fmt(part, f)?;
}
Ok(())
}