2021-01-15 03:11:10 +01:00
|
|
|
//! Typed HTTP headers, pre-defined `HeaderName`s, traits for parsing/conversion and other
|
|
|
|
//! header utility methods.
|
2019-02-07 22:24:24 +01:00
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
use std::fmt;
|
2019-02-07 22:24:24 +01:00
|
|
|
|
|
|
|
use bytes::{Bytes, BytesMut};
|
2019-08-13 19:48:11 +02:00
|
|
|
use percent_encoding::{AsciiSet, CONTROLS};
|
2019-02-07 22:24:24 +01:00
|
|
|
|
|
|
|
pub use http::header::*;
|
|
|
|
|
|
|
|
use crate::error::ParseError;
|
|
|
|
use crate::httpmessage::HttpMessage;
|
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
mod into_pair;
|
|
|
|
mod into_value;
|
|
|
|
mod utils;
|
|
|
|
|
2019-02-07 22:24:24 +01:00
|
|
|
mod common;
|
2019-04-07 00:02:02 +02:00
|
|
|
pub(crate) mod map;
|
2019-02-07 22:24:24 +01:00
|
|
|
mod shared;
|
2021-01-15 03:11:10 +01:00
|
|
|
|
2019-02-07 22:24:24 +01:00
|
|
|
pub use self::common::*;
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub use self::shared::*;
|
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
pub use self::into_pair::IntoHeaderPair;
|
|
|
|
pub use self::into_value::IntoHeaderValue;
|
2019-04-07 00:02:02 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub use self::map::GetAll;
|
|
|
|
pub use self::map::HeaderMap;
|
2021-01-15 03:11:10 +01:00
|
|
|
pub use self::utils::*;
|
2019-04-07 00:02:02 +02:00
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
/// A trait for any object that already represents a valid header field and value.
|
|
|
|
pub trait Header: IntoHeaderValue {
|
2019-02-07 22:24:24 +01:00
|
|
|
/// Returns the name of the header field
|
|
|
|
fn name() -> HeaderName;
|
|
|
|
|
|
|
|
/// Parse a header
|
|
|
|
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub(crate) struct Writer {
|
|
|
|
buf: BytesMut,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer {
|
|
|
|
fn new() -> Writer {
|
|
|
|
Writer {
|
|
|
|
buf: BytesMut::new(),
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 03:11:10 +01:00
|
|
|
|
2019-02-07 22:24:24 +01:00
|
|
|
fn take(&mut self) -> Bytes {
|
2019-12-05 18:35:43 +01:00
|
|
|
self.buf.split().freeze()
|
2019-02-07 22:24:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
2019-12-07 19:46:51 +01:00
|
|
|
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
|
2019-02-07 22:24:24 +01:00
|
|
|
fmt::write(self, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
/// Convert `http::HeaderMap` to our `HeaderMap`.
|
2019-04-07 00:02:02 +02:00
|
|
|
impl From<http::HeaderMap> for HeaderMap {
|
|
|
|
fn from(map: http::HeaderMap) -> HeaderMap {
|
|
|
|
let mut new_map = HeaderMap::with_capacity(map.capacity());
|
|
|
|
for (h, v) in map.iter() {
|
|
|
|
new_map.append(h.clone(), v.clone());
|
|
|
|
}
|
|
|
|
new_map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:48:11 +02:00
|
|
|
// This encode set is used for HTTP header values and is defined at
|
2021-01-15 03:11:10 +01:00
|
|
|
// https://tools.ietf.org/html/rfc5987#section-3.2.
|
2019-08-13 19:48:11 +02:00
|
|
|
pub(crate) const HTTP_VALUE: &AsciiSet = &CONTROLS
|
|
|
|
.add(b' ')
|
|
|
|
.add(b'"')
|
|
|
|
.add(b'%')
|
|
|
|
.add(b'\'')
|
|
|
|
.add(b'(')
|
|
|
|
.add(b')')
|
|
|
|
.add(b'*')
|
|
|
|
.add(b',')
|
|
|
|
.add(b'/')
|
|
|
|
.add(b':')
|
|
|
|
.add(b';')
|
|
|
|
.add(b'<')
|
|
|
|
.add(b'-')
|
|
|
|
.add(b'>')
|
|
|
|
.add(b'?')
|
|
|
|
.add(b'[')
|
|
|
|
.add(b'\\')
|
|
|
|
.add(b']')
|
|
|
|
.add(b'{')
|
|
|
|
.add(b'}');
|