@@ -5,9 +5,9 @@ use mime::Mime;
55use serde:: Serialize ;
66
77use crate :: http:: cookies:: Cookie ;
8- use crate :: http:: headers:: { HeaderName , HeaderValue } ;
8+ use crate :: http:: headers:: { HeaderName , HeaderValues , ToHeaderValues , CONTENT_TYPE } ;
99use crate :: http:: { self , Body , StatusCode } ;
10- use crate :: Redirect ;
10+ use crate :: redirect :: Redirect ;
1111
1212#[ derive( Debug ) ]
1313pub ( crate ) enum CookieEvent {
@@ -99,38 +99,24 @@ impl Response {
9999
100100 /// Get an HTTP header.
101101 #[ must_use]
102- pub fn header ( & self , name : & HeaderName ) -> Option < & Vec < HeaderValue > > {
102+ pub fn header ( & self , name : impl Into < HeaderName > ) -> Option < & HeaderValues > {
103103 self . res . header ( name)
104104 }
105105
106106 /// Remove a header.
107- pub fn remove_header ( & mut self , name : & HeaderName ) -> Option < Vec < HeaderValue > > {
107+ pub fn remove_header ( & mut self , name : impl Into < HeaderName > ) -> Option < HeaderValues > {
108108 self . res . remove_header ( name)
109109 }
110110
111111 /// Insert an HTTP header.
112- pub fn set_header (
113- mut self ,
114- key : http_types:: headers:: HeaderName ,
115- value : impl AsRef < str > ,
116- ) -> Self {
117- let value = value. as_ref ( ) . to_owned ( ) ;
118- self . res
119- . insert_header ( key, & [ value. parse ( ) . unwrap ( ) ] [ ..] )
120- . expect ( "invalid header" ) ;
112+ pub fn set_header ( mut self , key : impl Into < HeaderName > , value : impl ToHeaderValues ) -> Self {
113+ self . res . insert_header ( key, value) ;
121114 self
122115 }
123116
124117 /// Append an HTTP header.
125- pub fn append_header (
126- mut self ,
127- key : http_types:: headers:: HeaderName ,
128- value : impl AsRef < str > ,
129- ) -> Self {
130- let value = value. as_ref ( ) . to_owned ( ) ;
131- self . res
132- . append_header ( key, & [ value. parse ( ) . unwrap ( ) ] [ ..] )
133- . expect ( "invalid header" ) ;
118+ pub fn append_header ( mut self , key : impl Into < HeaderName > , value : impl ToHeaderValues ) -> Self {
119+ self . res . append_header ( key, value) ;
134120 self
135121 }
136122
@@ -139,7 +125,7 @@ impl Response {
139125 /// [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)
140126 #[ must_use]
141127 pub fn set_mime ( self , mime : Mime ) -> Self {
142- self . set_header ( http_types :: headers :: CONTENT_TYPE , format ! ( "{}" , mime) )
128+ self . set_header ( CONTENT_TYPE , mime. to_string ( ) )
143129 }
144130
145131 /// Pass a string as the request body.
@@ -233,15 +219,15 @@ impl Response {
233219 self . cookie_events . push ( CookieEvent :: Removed ( cookie) ) ;
234220 }
235221
236- /// Get a local value.
222+ /// Get a response extension value.
237223 #[ must_use]
238- pub fn local < T : Send + Sync + ' static > ( & self ) -> Option < & T > {
239- self . res . local ( ) . get ( )
224+ pub fn ext < T : Send + Sync + ' static > ( & self ) -> Option < & T > {
225+ self . res . ext ( ) . get ( )
240226 }
241227
242228 /// Set a local value.
243- pub fn set_local < T : Send + Sync + ' static > ( mut self , val : T ) -> Self {
244- self . res . local_mut ( ) . insert ( val) ;
229+ pub fn set_ext < T : Send + Sync + ' static > ( mut self , val : T ) -> Self {
230+ self . res . ext_mut ( ) . insert ( val) ;
245231 self
246232 }
247233
@@ -309,7 +295,7 @@ impl<'a> From<&'a str> for Response {
309295}
310296
311297impl IntoIterator for Response {
312- type Item = ( HeaderName , Vec < HeaderValue > ) ;
298+ type Item = ( HeaderName , HeaderValues ) ;
313299 type IntoIter = http_types:: headers:: IntoIter ;
314300
315301 /// Returns a iterator of references over the remaining items.
@@ -320,7 +306,7 @@ impl IntoIterator for Response {
320306}
321307
322308impl < ' a > IntoIterator for & ' a Response {
323- type Item = ( & ' a HeaderName , & ' a Vec < HeaderValue > ) ;
309+ type Item = ( & ' a HeaderName , & ' a HeaderValues ) ;
324310 type IntoIter = http_types:: headers:: Iter < ' a > ;
325311
326312 #[ inline]
@@ -330,7 +316,7 @@ impl<'a> IntoIterator for &'a Response {
330316}
331317
332318impl < ' a > IntoIterator for & ' a mut Response {
333- type Item = ( & ' a HeaderName , & ' a mut Vec < HeaderValue > ) ;
319+ type Item = ( & ' a HeaderName , & ' a mut HeaderValues ) ;
334320 type IntoIter = http_types:: headers:: IterMut < ' a > ;
335321
336322 #[ inline]
0 commit comments