1
- //! Re-exported hyper HTTP library types.
1
+ //! Re-exported hyper HTTP library types and hyperx typed headers .
2
2
//!
3
3
//! All types that are re-exported from Hyper reside inside of this module.
4
4
//! These types will, with certainty, be removed with time, but they reside here
21
21
22
22
/// Reexported http header types.
23
23
pub mod header {
24
+ use super :: super :: header:: Header ;
25
+ pub use hyperx:: header:: Header as HyperxHeaderTrait ;
26
+
24
27
macro_rules! import_http_headers {
25
28
( $( $name: ident) ,* ) => ( $(
26
29
pub use http:: header:: $name as $name;
@@ -43,4 +46,98 @@ pub mod header {
43
46
STRICT_TRANSPORT_SECURITY , TE , TRANSFER_ENCODING , UPGRADE , USER_AGENT ,
44
47
VARY
45
48
}
49
+
50
+ macro_rules! import_hyperx_items {
51
+ ( $( $item: ident) ,* ) => ( $( pub use hyperx:: header:: $item as $item; ) * )
52
+ }
53
+
54
+ macro_rules! import_hyperx_headers {
55
+ ( $( $name: ident) ,* ) => ( $(
56
+ impl :: std:: convert:: From <self :: $name> for Header <' static > {
57
+ fn from( header: self :: $name) -> Header <' static > {
58
+ Header :: new( $name:: header_name( ) , header. to_string( ) )
59
+ }
60
+ }
61
+ ) * )
62
+ }
63
+
64
+ macro_rules! import_generic_hyperx_headers {
65
+ ( $( $name: ident<$bound: ident>) ,* ) => ( $(
66
+ impl <T1 : ' static + $bound> :: std:: convert:: From <self :: $name<T1 >>
67
+ for Header <' static > {
68
+ fn from( header: self :: $name<T1 >) -> Header <' static > {
69
+ Header :: new( $name:: <T1 >:: header_name( ) , header. to_string( ) )
70
+ }
71
+ }
72
+ ) * )
73
+ }
74
+
75
+ import_hyperx_items ! {
76
+ Accept , AcceptCharset , AcceptEncoding , AcceptLanguage , AcceptRanges ,
77
+ AccessControlAllowCredentials , AccessControlAllowHeaders ,
78
+ AccessControlAllowMethods , AccessControlAllowOrigin ,
79
+ AccessControlExposeHeaders , AccessControlMaxAge ,
80
+ AccessControlRequestHeaders , AccessControlRequestMethod , Allow ,
81
+ Authorization , Basic , Bearer , ByteRangeSpec , CacheControl ,
82
+ CacheDirective , Charset , Connection , ConnectionOption ,
83
+ ContentDisposition , ContentEncoding , ContentLanguage , ContentLength ,
84
+ ContentLocation , ContentRange , ContentRangeSpec , ContentType , Cookie ,
85
+ Date , DispositionParam , DispositionType , Encoding , EntityTag , ETag ,
86
+ Expect , Expires , From , Host , HttpDate , IfMatch , IfModifiedSince ,
87
+ IfNoneMatch , IfRange , IfUnmodifiedSince , LastEventId , LastModified ,
88
+ Link , LinkValue , Location , Origin , Pragma , Prefer , Preference ,
89
+ PreferenceApplied , Protocol , ProtocolName , ProxyAuthorization , Quality ,
90
+ QualityItem , Range , RangeUnit , Referer , ReferrerPolicy , RetryAfter ,
91
+ Scheme , Server , SetCookie , StrictTransportSecurity ,
92
+ Te , TransferEncoding , Upgrade , UserAgent , Vary , Warning , q, qitem
93
+ }
94
+
95
+ import_hyperx_headers ! {
96
+ Accept , AcceptCharset , AcceptEncoding , AcceptLanguage , AcceptRanges ,
97
+ AccessControlAllowCredentials , AccessControlAllowHeaders ,
98
+ AccessControlAllowMethods , AccessControlAllowOrigin ,
99
+ AccessControlExposeHeaders , AccessControlMaxAge ,
100
+ AccessControlRequestHeaders , AccessControlRequestMethod , Allow ,
101
+ CacheControl , Connection , ContentDisposition , ContentEncoding ,
102
+ ContentLanguage , ContentLength , ContentLocation , ContentRange ,
103
+ ContentType , Cookie , Date , ETag , Expires , Expect , From , Host , IfMatch ,
104
+ IfModifiedSince , IfNoneMatch , IfUnmodifiedSince , IfRange , LastEventId ,
105
+ LastModified , Link , Location , Origin , Pragma , Prefer , PreferenceApplied ,
106
+ Range , Referer , ReferrerPolicy , RetryAfter , Server ,
107
+ StrictTransportSecurity , Te , TransferEncoding , Upgrade , UserAgent , Vary ,
108
+ Warning
109
+ }
110
+ import_generic_hyperx_headers ! {
111
+ Authorization <Scheme >,
112
+ ProxyAuthorization <Scheme >
113
+ }
114
+ // Note: SetCookie is missing, since it must be formatted as separate header lines...
115
+ }
116
+
117
+ #[ cfg( test) ]
118
+ mod tests {
119
+ use crate :: header:: HeaderMap ;
120
+ use super :: header:: HyperxHeaderTrait ; // Needed for Accept::header_name() below?!?!
121
+
122
+ #[ test]
123
+ fn add_typed_header ( ) {
124
+ use super :: header:: { Accept , QualityItem , q, qitem} ;
125
+ let mut map = HeaderMap :: new ( ) ;
126
+ map. add ( Accept ( vec ! [
127
+ QualityItem :: new( "audio/*" . parse( ) . unwrap( ) , q( 200 ) ) ,
128
+ qitem( "audio/basic" . parse( ) . unwrap( ) ) ,
129
+ ] ) ) ;
130
+ assert_eq ! ( map. get_one( Accept :: header_name( ) ) , Some ( "audio/*; q=0.2, audio/basic" ) ) ;
131
+ }
132
+
133
+ #[ test]
134
+ fn add_typed_header_with_type_params ( ) {
135
+ use super :: header:: { Authorization , Basic } ;
136
+ let mut map = HeaderMap :: new ( ) ;
137
+ map. add ( Authorization ( Basic {
138
+ username : "admin" . to_owned ( ) ,
139
+ password : Some ( "12345678" . to_owned ( ) ) } ) ) ;
140
+ assert_eq ! ( map. get_one( "Authorization" ) , Some ( "Basic YWRtaW46MTIzNDU2Nzg=" ) ) ;
141
+ }
142
+
46
143
}
0 commit comments