@@ -136,6 +136,31 @@ impl Method {
136136 }
137137 }
138138
139+ /// Convert static bytes into a `Method`.
140+ ///
141+ /// The method name can only be up to 15 bytes long. Any remaining bytes are ignored.
142+ ///
143+ /// # Panics
144+ ///
145+ /// If the input bytes are not a valid method name.
146+ pub const fn from_static ( src : & ' static [ u8 ] ) -> Method {
147+ match src {
148+ b"OPTIONS" => Method :: OPTIONS ,
149+ b"GET" => Method :: GET ,
150+ b"POST" => Method :: POST ,
151+ b"PUT" => Method :: PUT ,
152+ b"DELETE" => Method :: DELETE ,
153+ b"HEAD" => Method :: HEAD ,
154+ b"TRACE" => Method :: TRACE ,
155+ b"CONNECT" => Method :: CONNECT ,
156+ b"PATCH" => Method :: PATCH ,
157+ _ => {
158+ let inline = InlineExtension :: from_static ( src) ;
159+ Method ( ExtensionInline ( inline) )
160+ }
161+ }
162+ }
163+
139164 fn extension_inline ( src : & [ u8 ] ) -> Result < Method , InvalidMethod > {
140165 let inline = InlineExtension :: new ( src) ?;
141166
@@ -335,6 +360,29 @@ mod extension {
335360 Ok ( InlineExtension ( data, src. len ( ) as u8 ) )
336361 }
337362
363+ /// Convert static bytes into an `InlineExtension`.
364+ ///
365+ /// Only the first 15 characters are used. Any remaining bytes are ignored.
366+ ///
367+ /// # Panics
368+ ///
369+ /// If the input bytes are not a valid method name.
370+ pub const fn from_static ( src : & ' static [ u8 ] ) -> InlineExtension {
371+ let mut i = 0 ;
372+ let mut dst = [ 0u8 ; 15 ] ;
373+ while i < src. len ( ) && i < 15 {
374+ let byte = src[ i] ;
375+ let v = METHOD_CHARS [ byte as usize ] ;
376+ if v == 0 {
377+ panic ! ( "Invalid byte for method name." ) ;
378+ }
379+ dst[ i] = byte;
380+ i += 1 ;
381+ }
382+
383+ InlineExtension ( dst, i as u8 )
384+ }
385+
338386 pub fn as_str ( & self ) -> & str {
339387 let InlineExtension ( ref data, len) = self ;
340388 // Safety: the invariant of InlineExtension ensures that the first
@@ -440,6 +488,19 @@ mod test {
440488 assert_eq ! ( Method :: GET , & Method :: GET ) ;
441489 }
442490
491+ #[ test]
492+ fn test_from_static ( ) {
493+ assert_eq ! ( Method :: from_static( b"PROPFIND" ) , Method :: from_bytes( b"PROPFIND" ) . unwrap( ) ) ;
494+ assert_eq ! ( Method :: from_static( b"GET" ) , Method :: from_bytes( b"GET" ) . unwrap( ) ) ;
495+ assert_eq ! ( Method :: from_static( b"1234567890123456" ) . to_string( ) , "123456789012345" . to_string( ) ) ;
496+ }
497+
498+ #[ test]
499+ #[ should_panic]
500+ fn test_from_static_bad ( ) {
501+ Method :: from_static ( b"\0 " ) ;
502+ }
503+
443504 #[ test]
444505 fn test_invalid_method ( ) {
445506 assert ! ( Method :: from_str( "" ) . is_err( ) ) ;
0 commit comments