Skip to content

Commit

Permalink
Fix rwf2#1067 by importing typed headers from hyperx
Browse files Browse the repository at this point in the history
  • Loading branch information
jespersm committed Feb 10, 2021
1 parent c24f15c commit c284eae
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 13 deletions.
33 changes: 21 additions & 12 deletions core/codegen/tests/ui-fail-stable/responder-types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ error[E0277]: the trait bound `u8: Responder<'_, '_>` is not satisfied
|
= note: required by `respond_to`

error[E0277]: the trait bound `Header<'_>: From<u8>` is not satisfied
error[E0277]: the trait bound `Header<'_>: std::convert::From<u8>` is not satisfied
--> $DIR/responder-types.rs:11:5
|
11 | other: u8,
| ^^^^^ the trait `From<u8>` is not implemented for `Header<'_>`
| ^^^^^ the trait `std::convert::From<u8>` is not implemented for `Header<'_>`
|
= help: the following implementations were found:
<Header<'static> as From<&Cookie<'_>>>
<Header<'static> as From<Cookie<'_>>>
<Header<'static> as std::convert::From<&rocket::http::Cookie<'_>>>
<Header<'static> as std::convert::From<AcceptCharset>>
<Header<'static> as std::convert::From<AcceptEncoding>>
<Header<'static> as std::convert::From<AcceptLanguage>>
and 55 others
= note: required because of the requirements on the impl of `Into<Header<'_>>` for `u8`

error[E0277]: the trait bound `u8: Responder<'_, '_>` is not satisfied
Expand All @@ -25,26 +28,32 @@ error[E0277]: the trait bound `u8: Responder<'_, '_>` is not satisfied
|
= note: required by `respond_to`

error[E0277]: the trait bound `Header<'_>: From<u8>` is not satisfied
error[E0277]: the trait bound `Header<'_>: std::convert::From<u8>` is not satisfied
--> $DIR/responder-types.rs:17:5
|
17 | other: u8,
| ^^^^^ the trait `From<u8>` is not implemented for `Header<'_>`
| ^^^^^ the trait `std::convert::From<u8>` is not implemented for `Header<'_>`
|
= help: the following implementations were found:
<Header<'static> as From<&Cookie<'_>>>
<Header<'static> as From<Cookie<'_>>>
<Header<'static> as std::convert::From<&rocket::http::Cookie<'_>>>
<Header<'static> as std::convert::From<AcceptCharset>>
<Header<'static> as std::convert::From<AcceptEncoding>>
<Header<'static> as std::convert::From<AcceptLanguage>>
and 55 others
= note: required because of the requirements on the impl of `Into<Header<'_>>` for `u8`

error[E0277]: the trait bound `Header<'_>: From<std::string::String>` is not satisfied
error[E0277]: the trait bound `Header<'_>: std::convert::From<std::string::String>` is not satisfied
--> $DIR/responder-types.rs:24:5
|
24 | then: String,
| ^^^^ the trait `From<std::string::String>` is not implemented for `Header<'_>`
| ^^^^ the trait `std::convert::From<std::string::String>` is not implemented for `Header<'_>`
|
= help: the following implementations were found:
<Header<'static> as From<&Cookie<'_>>>
<Header<'static> as From<Cookie<'_>>>
<Header<'static> as std::convert::From<&rocket::http::Cookie<'_>>>
<Header<'static> as std::convert::From<AcceptCharset>>
<Header<'static> as std::convert::From<AcceptEncoding>>
<Header<'static> as std::convert::From<AcceptLanguage>>
and 55 others
= note: required because of the requirements on the impl of `Into<Header<'_>>` for `std::string::String`

error[E0277]: the trait bound `usize: Responder<'_, '_>` is not satisfied
Expand Down
1 change: 1 addition & 0 deletions core/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private-cookies = ["cookie/private", "cookie/key-expansion"]
smallvec = "1.0"
percent-encoding = "2"
hyper = { version = "0.14", default-features = false, features = ["http1", "http2", "runtime", "server", "stream"] }
hyperx = { version = "1.3.0" }
http = "0.2"
mime = "0.3.13"
time = "0.2.11"
Expand Down
99 changes: 98 additions & 1 deletion core/http/src/hyper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Re-exported hyper HTTP library types.
//! Re-exported hyper HTTP library types and hyperx typed headers.
//!
//! All types that are re-exported from Hyper reside inside of this module.
//! These types will, with certainty, be removed with time, but they reside here
Expand All @@ -21,6 +21,9 @@

/// Reexported http header types.
pub mod header {
use super::super::header::Header;
pub use hyperx::header::Header as HyperxHeaderTrait;

macro_rules! import_http_headers {
($($name:ident),*) => ($(
pub use http::header::$name as $name;
Expand All @@ -43,4 +46,98 @@ pub mod header {
STRICT_TRANSPORT_SECURITY, TE, TRANSFER_ENCODING, UPGRADE, USER_AGENT,
VARY
}

macro_rules! import_hyperx_items {
($($item:ident),*) => ($(pub use hyperx::header::$item as $item;)*)
}

macro_rules! import_hyperx_headers {
($($name:ident),*) => ($(
impl ::std::convert::From<self::$name> for Header<'static> {
fn from(header: self::$name) -> Header<'static> {
Header::new($name::header_name(), header.to_string())
}
}
)*)
}

macro_rules! import_generic_hyperx_headers {
($($name:ident<$bound:ident>),*) => ($(
impl <T1: 'static + $bound> ::std::convert::From<self::$name<T1>>
for Header<'static> {
fn from(header: self::$name<T1>) -> Header<'static> {
Header::new($name::<T1>::header_name(), header.to_string())
}
}
)*)
}

import_hyperx_items! {
Accept, AcceptCharset, AcceptEncoding, AcceptLanguage, AcceptRanges,
AccessControlAllowCredentials, AccessControlAllowHeaders,
AccessControlAllowMethods, AccessControlAllowOrigin,
AccessControlExposeHeaders, AccessControlMaxAge,
AccessControlRequestHeaders, AccessControlRequestMethod, Allow,
Authorization, Basic, Bearer, ByteRangeSpec, CacheControl,
CacheDirective, Charset, Connection, ConnectionOption,
ContentDisposition, ContentEncoding, ContentLanguage, ContentLength,
ContentLocation, ContentRange, ContentRangeSpec, ContentType, Cookie,
Date, DispositionParam, DispositionType, Encoding, EntityTag, ETag,
Expect, Expires, From, Host, HttpDate, IfMatch, IfModifiedSince,
IfNoneMatch, IfRange, IfUnmodifiedSince, LastEventId, LastModified,
Link, LinkValue, Location, Origin, Pragma, Prefer, Preference,
PreferenceApplied, Protocol, ProtocolName, ProxyAuthorization, Quality,
QualityItem, Range, RangeUnit, Referer, ReferrerPolicy, RetryAfter,
Scheme, Server, SetCookie, StrictTransportSecurity,
Te, TransferEncoding, Upgrade, UserAgent, Vary, Warning, q, qitem
}

import_hyperx_headers! {
Accept, AcceptCharset, AcceptEncoding, AcceptLanguage, AcceptRanges,
AccessControlAllowCredentials, AccessControlAllowHeaders,
AccessControlAllowMethods, AccessControlAllowOrigin,
AccessControlExposeHeaders, AccessControlMaxAge,
AccessControlRequestHeaders, AccessControlRequestMethod, Allow,
CacheControl, Connection, ContentDisposition, ContentEncoding,
ContentLanguage, ContentLength, ContentLocation, ContentRange,
ContentType, Cookie, Date, ETag, Expires, Expect, From, Host, IfMatch,
IfModifiedSince, IfNoneMatch, IfUnmodifiedSince, IfRange, LastEventId,
LastModified, Link, Location, Origin, Pragma, Prefer, PreferenceApplied,
Range, Referer, ReferrerPolicy, RetryAfter, Server,
StrictTransportSecurity, Te, TransferEncoding, Upgrade, UserAgent, Vary,
Warning
}
import_generic_hyperx_headers! {
Authorization<Scheme>,
ProxyAuthorization<Scheme>
}
// Note: SetCookie is missing, since it must be formatted as separate header lines...
}

#[cfg(test)]
mod tests {
use crate::header::HeaderMap;
use super::header::HyperxHeaderTrait; // Needed for Accept::header_name() below?!?!

#[test]
fn add_typed_header() {
use super::header::{Accept, QualityItem, q, qitem};
let mut map = HeaderMap::new();
map.add(Accept(vec![
QualityItem::new("audio/*".parse().unwrap(), q(200)),
qitem("audio/basic".parse().unwrap()),
]));
assert_eq!(map.get_one(Accept::header_name()), Some("audio/*; q=0.2, audio/basic"));
}

#[test]
fn add_typed_header_with_type_params() {
use super::header::{Authorization, Basic};
let mut map = HeaderMap::new();
map.add(Authorization(Basic {
username: "admin".to_owned(),
password: Some("12345678".to_owned())}));
assert_eq!(map.get_one("Authorization"), Some("Basic YWRtaW46MTIzNDU2Nzg="));
}

}

0 comments on commit c284eae

Please sign in to comment.