1- use header:: { Header , HeaderFormat } ;
21use std:: fmt:: { self , Display } ;
32use std:: str:: FromStr ;
4- use header:: parsing:: { from_comma_delimited, fmt_comma_delimited} ;
53use unicase:: UniCase ;
64
75pub use self :: ConnectionOption :: { KeepAlive , Close , ConnectionHeader } ;
86
9- /// `Connection` header, defined in [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.1)
10- ///
11- /// The `Connection` header field allows the sender to indicate desired
12- /// control options for the current connection. In order to avoid
13- /// confusing downstream recipients, a proxy or gateway MUST remove or
14- /// replace any received connection options before forwarding the
15- /// message.
16- ///
17- /// # ABNF
18- /// ```plain
19- /// Connection = 1#connection-option
20- /// connection-option = token
21- /// ```
22- ///
23- /// # Example values
24- /// * `close`
25- /// * `upgrade`
26- /// * `keep-alive`
27- #[ derive( Clone , PartialEq , Debug ) ]
28- pub struct Connection ( pub Vec < ConnectionOption > ) ;
29-
30- deref ! ( Connection => Vec <ConnectionOption >) ;
31-
327/// Values that can be in the `Connection` header.
338#[ derive( Clone , PartialEq , Debug ) ]
349pub enum ConnectionOption {
@@ -50,38 +25,49 @@ pub enum ConnectionOption {
5025impl FromStr for ConnectionOption {
5126 type Err = ( ) ;
5227 fn from_str ( s : & str ) -> Result < ConnectionOption , ( ) > {
53- Ok ( match s {
54- "keep-alive" => KeepAlive ,
55- "close" => Close ,
56- s => ConnectionHeader ( UniCase ( s. to_string ( ) ) ) ,
57- } )
28+ match s {
29+ "keep-alive" => Ok ( KeepAlive ) ,
30+ "close" => Ok ( Close ) ,
31+ s => Ok ( ConnectionHeader ( UniCase ( s. to_string ( ) ) ) )
32+ }
5833 }
5934}
6035
6136impl Display for ConnectionOption {
62- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
63- f . write_str ( match * self {
37+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
38+ write ! ( fmt , "{}" , match * self {
6439 KeepAlive => "keep-alive" ,
6540 Close => "close" ,
66- ConnectionHeader ( UniCase ( ref s) ) => s,
41+ ConnectionHeader ( UniCase ( ref s) ) => s. as_ref ( )
6742 } )
6843 }
6944}
7045
71- impl Header for Connection {
72- fn header_name ( ) -> & ' static str {
73- "Connection"
74- }
75-
76- fn parse_header ( raw : & [ Vec < u8 > ] ) -> Option < Connection > {
77- from_comma_delimited ( raw) . map ( |vec| Connection ( vec) )
78- }
79- }
46+ header ! {
47+ #[ doc="`Connection` header, defined in" ]
48+ #[ doc="[RFC7230](http://tools.ietf.org/html/rfc7230#section-6.1)" ]
49+ #[ doc="" ]
50+ #[ doc="The `Connection` header field allows the sender to indicate desired" ]
51+ #[ doc="control options for the current connection. In order to avoid" ]
52+ #[ doc="confusing downstream recipients, a proxy or gateway MUST remove or" ]
53+ #[ doc="replace any received connection options before forwarding the" ]
54+ #[ doc="message." ]
55+ #[ doc="" ]
56+ #[ doc="# ABNF" ]
57+ #[ doc="```plain" ]
58+ #[ doc="Connection = 1#connection-option" ]
59+ #[ doc="connection-option = token" ]
60+ #[ doc="" ]
61+ #[ doc="# Example values" ]
62+ #[ doc="* `close`" ]
63+ #[ doc="* `keep-alive`" ]
64+ #[ doc="* `upgrade`" ]
65+ ( Connection , "Connection" ) => ( ConnectionOption ) +
8066
81- impl HeaderFormat for Connection {
82- fn fmt_header ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
83- let Connection ( ref parts ) = * self ;
84- fmt_comma_delimited ( f , & parts [ .. ] )
67+ test_connection {
68+ test_header! ( test1 , vec! [ b"close" ] ) ;
69+ test_header! ( test2 , vec! [ b"keep-alive" ] ) ;
70+ test_header! ( test3 , vec! [ b"upgrade" ] ) ;
8571 }
8672}
8773
0 commit comments