Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Header trait #315

Merged
merged 4 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/auth/authorization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::auth::AuthenticationScheme;
use crate::bail_status as bail;
use crate::headers::{HeaderName, HeaderValue, Headers, AUTHORIZATION};
use crate::headers::{Header, HeaderName, HeaderValue, Headers, AUTHORIZATION};

/// Credentials to authenticate a user agent with a server.
///
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Authorization {

/// Get the `HeaderName`.
pub fn name(&self) -> HeaderName {
AUTHORIZATION
self.header_name()
}

/// Get the `HeaderValue`.
Expand Down Expand Up @@ -110,6 +110,16 @@ impl Authorization {
}
}

impl Header for Authorization {
fn header_name(&self) -> HeaderName {
AUTHORIZATION
}

fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions src/auth/basic_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ impl BasicAuth {
}
}

impl crate::headers::Header for BasicAuth {
fn header_name(&self) -> HeaderName {
AUTHORIZATION
}

fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions src/auth/www_authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ impl WwwAuthenticate {
}
}

impl crate::headers::Header for WwwAuthenticate {
fn header_name(&self) -> HeaderName {
WWW_AUTHENTICATE
}

fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions src/cache/age.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ impl ToHeaderValues for Age {
}
}

impl crate::headers::Header for Age {
fn header_name(&self) -> HeaderName {
AGE
}

fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions src/cache/cache_control/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ impl CacheControl {
}
}

impl crate::headers::Header for CacheControl {
fn header_name(&self) -> HeaderName {
CACHE_CONTROL
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl IntoIterator for CacheControl {
type Item = CacheDirective;
type IntoIter = IntoIter;
Expand Down
9 changes: 9 additions & 0 deletions src/cache/clear_site_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ impl Debug for ClearSiteData {
}
}

impl crate::headers::Header for ClearSiteData {
fn header_name(&self) -> HeaderName {
CLEAR_SITE_DATA
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use crate::cache::{ClearDirective, ClearSiteData};
Expand Down
9 changes: 9 additions & 0 deletions src/cache/expires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ impl Expires {
}
}

impl crate::headers::Header for Expires {
fn header_name(&self) -> HeaderName {
EXPIRES
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl ToHeaderValues for Expires {
type Iter = option::IntoIter<HeaderValue>;
fn to_header_values(&self) -> crate::Result<Self::Iter> {
Expand Down
9 changes: 9 additions & 0 deletions src/conditional/etag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ impl ETag {
}
}

impl crate::headers::Header for ETag {
fn header_name(&self) -> HeaderName {
ETAG
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl Display for ETag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
9 changes: 9 additions & 0 deletions src/conditional/if_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ impl IfMatch {
}
}

impl crate::headers::Header for IfMatch {
fn header_name(&self) -> HeaderName {
IF_MATCH
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl IntoIterator for IfMatch {
type Item = ETag;
type IntoIter = IntoIter;
Expand Down
9 changes: 9 additions & 0 deletions src/conditional/if_modified_since.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ impl ToHeaderValues for IfModifiedSince {
}
}

impl crate::headers::Header for IfModifiedSince {
fn header_name(&self) -> HeaderName {
IF_MODIFIED_SINCE
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions src/conditional/if_none_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ impl IfNoneMatch {
}
}

impl crate::headers::Header for IfNoneMatch {
fn header_name(&self) -> HeaderName {
IF_NONE_MATCH
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl IntoIterator for IfNoneMatch {
type Item = ETag;
type IntoIter = IntoIter;
Expand Down
9 changes: 9 additions & 0 deletions src/conditional/if_unmodified_since.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ impl IfUnmodifiedSince {
}
}

impl crate::headers::Header for IfUnmodifiedSince {
fn header_name(&self) -> HeaderName {
IF_UNMODIFIED_SINCE
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl ToHeaderValues for IfUnmodifiedSince {
type Iter = option::IntoIter<HeaderValue>;
fn to_header_values(&self) -> crate::Result<Self::Iter> {
Expand Down
9 changes: 9 additions & 0 deletions src/conditional/last_modified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ impl LastModified {
}
}

impl crate::headers::Header for LastModified {
fn header_name(&self) -> HeaderName {
LAST_MODIFIED
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl ToHeaderValues for LastModified {
type Iter = option::IntoIter<HeaderValue>;
fn to_header_values(&self) -> crate::Result<Self::Iter> {
Expand Down
9 changes: 9 additions & 0 deletions src/conditional/vary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ impl Vary {
}
}

impl crate::headers::Header for Vary {
fn header_name(&self) -> HeaderName {
VARY
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl IntoIterator for Vary {
type Item = HeaderName;
type IntoIter = IntoIter;
Expand Down
9 changes: 9 additions & 0 deletions src/content/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ impl Accept {
}
}

impl crate::headers::Header for Accept {
fn header_name(&self) -> HeaderName {
ACCEPT
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl IntoIterator for Accept {
type Item = MediaTypeProposal;
type IntoIter = IntoIter;
Expand Down
9 changes: 9 additions & 0 deletions src/content/accept_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ impl AcceptEncoding {
}
}

impl crate::headers::Header for AcceptEncoding {
fn header_name(&self) -> HeaderName {
ACCEPT_ENCODING
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl IntoIterator for AcceptEncoding {
type Item = EncodingProposal;
type IntoIter = IntoIter;
Expand Down
9 changes: 9 additions & 0 deletions src/content/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ impl ContentEncoding {
}
}

impl crate::headers::Header for ContentEncoding {
fn header_name(&self) -> HeaderName {
CONTENT_ENCODING
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl ToHeaderValues for ContentEncoding {
type Iter = option::IntoIter<HeaderValue>;
fn to_header_values(&self) -> crate::Result<Self::Iter> {
Expand Down
9 changes: 9 additions & 0 deletions src/content/content_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ impl ContentLength {
}
}

impl crate::headers::Header for ContentLength {
fn header_name(&self) -> HeaderName {
CONTENT_LENGTH
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions src/content/content_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ impl ContentLocation {
}
}

impl crate::headers::Header for ContentLocation {
fn header_name(&self) -> HeaderName {
CONTENT_LOCATION
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions src/content/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ impl ContentType {
}
}

impl crate::headers::Header for ContentType {
fn header_name(&self) -> HeaderName {
CONTENT_TYPE
}
fn header_value(&self) -> HeaderValue {
self.value()
}
}

impl PartialEq<Mime> for ContentType {
fn eq(&self, other: &Mime) -> bool {
&self.media_type == other
Expand Down
41 changes: 41 additions & 0 deletions src/headers/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::headers::{HeaderName, HeaderValue, Headers};

/// A trait representing a [`HeaderName`] and [`HeaderValue`] pair.
pub trait Header {
/// Access the header's name.
fn header_name(&self) -> HeaderName;

/// Access the header's value.
fn header_value(&self) -> HeaderValue;

/// Insert the header name and header value into something that looks like a
/// [`Headers`] map.
fn apply_header<H: AsMut<Headers>>(&self, mut headers: H) {
let name = self.header_name();
let value = self.header_value();
headers.as_mut().insert(name, value);
}
}

impl<'a, 'b> Header for (&'a str, &'b str) {
fn header_name(&self) -> HeaderName {
HeaderName::from(self.0)
}

fn header_value(&self) -> HeaderValue {
HeaderValue::from_bytes(self.1.to_owned().into_bytes())
.expect("String slice should be valid ASCII")
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn header_from_strings() {
let strings = ("Content-Length", "12");
assert_eq!(strings.header_name(), "Content-Length");
assert_eq!(strings.header_value(), "12");
}
}
2 changes: 2 additions & 0 deletions src/headers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! HTTP headers.

mod constants;
mod header;
mod header_name;
mod header_value;
mod header_values;
Expand All @@ -14,6 +15,7 @@ mod to_header_values;
mod values;

pub use constants::*;
pub use header::Header;
pub use header_name::HeaderName;
pub use header_value::HeaderValue;
pub use header_values::HeaderValues;
Expand Down
Loading