@@ -52,40 +52,6 @@ pub enum Method {
5252 Extension ( String ) ,
5353}
5454
55- impl From < http:: Method > for Method {
56- fn from ( method : http:: Method ) -> Method {
57- match method {
58- http:: Method :: GET => Method :: Get ,
59- http:: Method :: HEAD => Method :: Head ,
60- http:: Method :: POST => Method :: Post ,
61- http:: Method :: PUT => Method :: Put ,
62- http:: Method :: DELETE => Method :: Delete ,
63- http:: Method :: CONNECT => Method :: Connect ,
64- http:: Method :: OPTIONS => Method :: Options ,
65- http:: Method :: TRACE => Method :: Trace ,
66- http:: Method :: PATCH => Method :: Patch ,
67- _ => Method :: Extension ( method. to_string ( ) ) ,
68- }
69- }
70- }
71-
72- impl From < Method > for http:: Method {
73- fn from ( method : Method ) -> http:: Method {
74- match method {
75- Method :: Get => http:: Method :: GET ,
76- Method :: Head => http:: Method :: HEAD ,
77- Method :: Post => http:: Method :: POST ,
78- Method :: Put => http:: Method :: PUT ,
79- Method :: Delete => http:: Method :: DELETE ,
80- Method :: Connect => http:: Method :: CONNECT ,
81- Method :: Options => http:: Method :: OPTIONS ,
82- Method :: Trace => http:: Method :: TRACE ,
83- Method :: Patch => http:: Method :: PATCH ,
84- Method :: Extension ( method) => http:: Method :: from_bytes ( method. as_bytes ( ) ) . expect ( "Invalid HTTP method" ) ,
85- }
86- }
87- }
88-
8955/// An HTTP version.
9056#[ derive( Clone , SpacetimeType , PartialEq , Eq ) ]
9157#[ sats( crate = crate , name = "HttpVersion" ) ]
@@ -97,31 +63,6 @@ pub enum Version {
9763 Http3 ,
9864}
9965
100- impl From < http:: Version > for Version {
101- fn from ( version : http:: Version ) -> Version {
102- match version {
103- http:: Version :: HTTP_09 => Version :: Http09 ,
104- http:: Version :: HTTP_10 => Version :: Http10 ,
105- http:: Version :: HTTP_11 => Version :: Http11 ,
106- http:: Version :: HTTP_2 => Version :: Http2 ,
107- http:: Version :: HTTP_3 => Version :: Http3 ,
108- _ => unreachable ! ( "Unknown HTTP version: {version:?}" ) ,
109- }
110- }
111- }
112-
113- impl From < Version > for http:: Version {
114- fn from ( version : Version ) -> http:: Version {
115- match version {
116- Version :: Http09 => http:: Version :: HTTP_09 ,
117- Version :: Http10 => http:: Version :: HTTP_10 ,
118- Version :: Http11 => http:: Version :: HTTP_11 ,
119- Version :: Http2 => http:: Version :: HTTP_2 ,
120- Version :: Http3 => http:: Version :: HTTP_3 ,
121- }
122- }
123- }
124-
12566/// A set of HTTP headers.
12667///
12768/// Construct this by converting from a [`http::HeaderMap`].
@@ -134,26 +75,22 @@ pub struct Headers {
13475
13576// `http::header::IntoIter` only returns the `HeaderName` for the first
13677// `HeaderValue` with that name, so we have to manually assign the names.
137- struct HeaderMapIntoIter {
138- prev : Option < ( http :: HeaderName , http :: HeaderValue ) > ,
139- inner : http :: header :: IntoIter < http :: HeaderValue > ,
78+ struct HeaderIter < I , T > {
79+ prev : Option < ( String , T ) > ,
80+ inner : I ,
14081}
14182
142- impl From < http:: header:: HeaderMap > for HeaderMapIntoIter {
143- fn from ( map : http:: header:: HeaderMap ) -> Self {
144- let mut inner = map. into_iter ( ) ;
145- Self {
146- prev : inner. next ( ) . map ( |( k, v) | ( k. unwrap ( ) , v) ) ,
147- inner,
148- }
149- }
150- }
151-
152- impl Iterator for HeaderMapIntoIter {
153- type Item = ( http:: HeaderName , http:: HeaderValue ) ;
83+ impl < I , T > Iterator for HeaderIter < I , T >
84+ where
85+ I : Iterator < Item = ( Option < String > , T ) > ,
86+ {
87+ type Item = ( String , T ) ;
15488
15589 fn next ( & mut self ) -> Option < Self :: Item > {
156- let ( prev_k, prev_v) = self . prev . take ( ) ?;
90+ let ( prev_k, prev_v) = self
91+ . prev
92+ . take ( )
93+ . or_else ( || self . inner . next ( ) . map ( |( k, v) | ( k. unwrap ( ) , v) ) ) ?;
15794 self . prev = self
15895 . inner
15996 . next ( )
@@ -166,28 +103,20 @@ impl Iterator for HeaderMapIntoIter {
166103 }
167104}
168105
169- impl From < http:: HeaderMap > for Headers {
170- fn from ( value : http:: HeaderMap ) -> Headers {
171- Headers {
172- entries : HeaderMapIntoIter :: from ( value)
173- . map ( |( name, value) | HttpHeaderPair {
174- name : name. to_string ( ) ,
175- value : value. into ( ) ,
176- } )
177- . collect ( ) ,
178- }
106+ impl FromIterator < ( Option < String > , HeaderValue ) > for Headers {
107+ fn from_iter < T : IntoIterator < Item = ( Option < String > , HeaderValue ) > > ( iter : T ) -> Self {
108+ let inner = iter. into_iter ( ) ;
109+ let entries = HeaderIter { prev : None , inner }
110+ . map ( |( name, value) | HttpHeaderPair { name, value } )
111+ . collect ( ) ;
112+ Self { entries }
179113 }
180114}
181115
182- impl TryFrom < Headers > for http:: HeaderMap {
183- type Error = http:: Error ;
184- fn try_from ( headers : Headers ) -> http:: Result < Self > {
185- let Headers { entries } = headers;
186- let mut new_headers = http:: HeaderMap :: with_capacity ( entries. len ( ) / 2 ) ;
187- for HttpHeaderPair { name, value } in entries {
188- new_headers. insert ( http:: HeaderName :: try_from ( name) ?, value. try_into ( ) ?) ;
189- }
190- Ok ( new_headers)
116+ impl Headers {
117+ #[ allow( clippy:: should_implement_trait) ]
118+ pub fn into_iter ( self ) -> impl Iterator < Item = ( String , HeaderValue ) > {
119+ IntoIterator :: into_iter ( self . entries ) . map ( |HttpHeaderPair { name, value } | ( name, value) )
191120 }
192121}
193122
@@ -202,27 +131,9 @@ struct HttpHeaderPair {
202131/// A valid HTTP header value, sourced from an already-validated [`http::HeaderValue`].
203132#[ derive( Clone , SpacetimeType ) ]
204133#[ sats( crate = crate , name = "HttpHeaderValue" ) ]
205- struct HeaderValue {
206- bytes : Box < [ u8 ] > ,
207- is_sensitive : bool ,
208- }
209-
210- impl From < http:: HeaderValue > for HeaderValue {
211- fn from ( value : http:: HeaderValue ) -> HeaderValue {
212- HeaderValue {
213- is_sensitive : value. is_sensitive ( ) ,
214- bytes : value. as_bytes ( ) . into ( ) ,
215- }
216- }
217- }
218-
219- impl TryFrom < HeaderValue > for http:: HeaderValue {
220- type Error = http:: Error ;
221- fn try_from ( value : HeaderValue ) -> http:: Result < http:: HeaderValue > {
222- let mut new_value = http:: HeaderValue :: from_bytes ( & value. bytes ) ?;
223- new_value. set_sensitive ( value. is_sensitive ) ;
224- Ok ( new_value)
225- }
134+ pub struct HeaderValue {
135+ pub bytes : Box < [ u8 ] > ,
136+ pub is_sensitive : bool ,
226137}
227138
228139#[ derive( Clone , SpacetimeType ) ]
0 commit comments