/rust/registry/src/index.crates.io-6f17d22bba15001f/http-1.3.1/src/method.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! The HTTP request method |
2 | | //! |
3 | | //! This module contains HTTP-method related structs and errors and such. The |
4 | | //! main type of this module, `Method`, is also reexported at the root of the |
5 | | //! crate as `http::Method` and is intended for import through that location |
6 | | //! primarily. |
7 | | //! |
8 | | //! # Examples |
9 | | //! |
10 | | //! ``` |
11 | | //! use http::Method; |
12 | | //! |
13 | | //! assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); |
14 | | //! assert!(Method::GET.is_idempotent()); |
15 | | //! assert_eq!(Method::POST.as_str(), "POST"); |
16 | | //! ``` |
17 | | |
18 | | use self::extension::{AllocatedExtension, InlineExtension}; |
19 | | use self::Inner::*; |
20 | | |
21 | | use std::convert::TryFrom; |
22 | | use std::error::Error; |
23 | | use std::str::FromStr; |
24 | | use std::{fmt, str}; |
25 | | |
26 | | /// The Request Method (VERB) |
27 | | /// |
28 | | /// This type also contains constants for a number of common HTTP methods such |
29 | | /// as GET, POST, etc. |
30 | | /// |
31 | | /// Currently includes 8 variants representing the 8 methods defined in |
32 | | /// [RFC 7230](https://tools.ietf.org/html/rfc7231#section-4.1), plus PATCH, |
33 | | /// and an Extension variant for all extensions. |
34 | | /// |
35 | | /// # Examples |
36 | | /// |
37 | | /// ``` |
38 | | /// use http::Method; |
39 | | /// |
40 | | /// assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); |
41 | | /// assert!(Method::GET.is_idempotent()); |
42 | | /// assert_eq!(Method::POST.as_str(), "POST"); |
43 | | /// ``` |
44 | | #[derive(Clone, PartialEq, Eq, Hash)] |
45 | | pub struct Method(Inner); |
46 | | |
47 | | /// A possible error value when converting `Method` from bytes. |
48 | | pub struct InvalidMethod { |
49 | | _priv: (), |
50 | | } |
51 | | |
52 | | #[derive(Clone, PartialEq, Eq, Hash)] |
53 | | enum Inner { |
54 | | Options, |
55 | | Get, |
56 | | Post, |
57 | | Put, |
58 | | Delete, |
59 | | Head, |
60 | | Trace, |
61 | | Connect, |
62 | | Patch, |
63 | | // If the extension is short enough, store it inline |
64 | | ExtensionInline(InlineExtension), |
65 | | // Otherwise, allocate it |
66 | | ExtensionAllocated(AllocatedExtension), |
67 | | } |
68 | | |
69 | | impl Method { |
70 | | /// GET |
71 | | pub const GET: Method = Method(Get); |
72 | | |
73 | | /// POST |
74 | | pub const POST: Method = Method(Post); |
75 | | |
76 | | /// PUT |
77 | | pub const PUT: Method = Method(Put); |
78 | | |
79 | | /// DELETE |
80 | | pub const DELETE: Method = Method(Delete); |
81 | | |
82 | | /// HEAD |
83 | | pub const HEAD: Method = Method(Head); |
84 | | |
85 | | /// OPTIONS |
86 | | pub const OPTIONS: Method = Method(Options); |
87 | | |
88 | | /// CONNECT |
89 | | pub const CONNECT: Method = Method(Connect); |
90 | | |
91 | | /// PATCH |
92 | | pub const PATCH: Method = Method(Patch); |
93 | | |
94 | | /// TRACE |
95 | | pub const TRACE: Method = Method(Trace); |
96 | | |
97 | | /// Converts a slice of bytes to an HTTP method. |
98 | 0 | pub fn from_bytes(src: &[u8]) -> Result<Method, InvalidMethod> { |
99 | 0 | match src.len() { |
100 | 0 | 0 => Err(InvalidMethod::new()), |
101 | 0 | 3 => match src { |
102 | 0 | b"GET" => Ok(Method(Get)), |
103 | 0 | b"PUT" => Ok(Method(Put)), |
104 | 0 | _ => Method::extension_inline(src), |
105 | | }, |
106 | 0 | 4 => match src { |
107 | 0 | b"POST" => Ok(Method(Post)), |
108 | 0 | b"HEAD" => Ok(Method(Head)), |
109 | 0 | _ => Method::extension_inline(src), |
110 | | }, |
111 | 0 | 5 => match src { |
112 | 0 | b"PATCH" => Ok(Method(Patch)), |
113 | 0 | b"TRACE" => Ok(Method(Trace)), |
114 | 0 | _ => Method::extension_inline(src), |
115 | | }, |
116 | 0 | 6 => match src { |
117 | 0 | b"DELETE" => Ok(Method(Delete)), |
118 | 0 | _ => Method::extension_inline(src), |
119 | | }, |
120 | 0 | 7 => match src { |
121 | 0 | b"OPTIONS" => Ok(Method(Options)), |
122 | 0 | b"CONNECT" => Ok(Method(Connect)), |
123 | 0 | _ => Method::extension_inline(src), |
124 | | }, |
125 | | _ => { |
126 | 0 | if src.len() <= InlineExtension::MAX { |
127 | 0 | Method::extension_inline(src) |
128 | | } else { |
129 | 0 | let allocated = AllocatedExtension::new(src)?; |
130 | | |
131 | 0 | Ok(Method(ExtensionAllocated(allocated))) |
132 | | } |
133 | | } |
134 | | } |
135 | 0 | } |
136 | | |
137 | 0 | fn extension_inline(src: &[u8]) -> Result<Method, InvalidMethod> { |
138 | 0 | let inline = InlineExtension::new(src)?; |
139 | | |
140 | 0 | Ok(Method(ExtensionInline(inline))) |
141 | 0 | } |
142 | | |
143 | | /// Whether a method is considered "safe", meaning the request is |
144 | | /// essentially read-only. |
145 | | /// |
146 | | /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.1) |
147 | | /// for more words. |
148 | 0 | pub fn is_safe(&self) -> bool { |
149 | 0 | matches!(self.0, Get | Head | Options | Trace) |
150 | 0 | } |
151 | | |
152 | | /// Whether a method is considered "idempotent", meaning the request has |
153 | | /// the same result if executed multiple times. |
154 | | /// |
155 | | /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.2) for |
156 | | /// more words. |
157 | 0 | pub fn is_idempotent(&self) -> bool { |
158 | 0 | match self.0 { |
159 | 0 | Put | Delete => true, |
160 | 0 | _ => self.is_safe(), |
161 | | } |
162 | 0 | } |
163 | | |
164 | | /// Return a &str representation of the HTTP method |
165 | | #[inline] |
166 | 0 | pub fn as_str(&self) -> &str { |
167 | 0 | match self.0 { |
168 | 0 | Options => "OPTIONS", |
169 | 0 | Get => "GET", |
170 | 0 | Post => "POST", |
171 | 0 | Put => "PUT", |
172 | 0 | Delete => "DELETE", |
173 | 0 | Head => "HEAD", |
174 | 0 | Trace => "TRACE", |
175 | 0 | Connect => "CONNECT", |
176 | 0 | Patch => "PATCH", |
177 | 0 | ExtensionInline(ref inline) => inline.as_str(), |
178 | 0 | ExtensionAllocated(ref allocated) => allocated.as_str(), |
179 | | } |
180 | 0 | } |
181 | | } |
182 | | |
183 | | impl AsRef<str> for Method { |
184 | | #[inline] |
185 | 0 | fn as_ref(&self) -> &str { |
186 | 0 | self.as_str() |
187 | 0 | } |
188 | | } |
189 | | |
190 | | impl<'a> PartialEq<&'a Method> for Method { |
191 | | #[inline] |
192 | 0 | fn eq(&self, other: &&'a Method) -> bool { |
193 | 0 | self == *other |
194 | 0 | } |
195 | | } |
196 | | |
197 | | impl<'a> PartialEq<Method> for &'a Method { |
198 | | #[inline] |
199 | 0 | fn eq(&self, other: &Method) -> bool { |
200 | 0 | *self == other |
201 | 0 | } Unexecuted instantiation: <&http::method::Method as core::cmp::PartialEq<http::method::Method>>::eq Unexecuted instantiation: <&http::method::Method as core::cmp::PartialEq<http::method::Method>>::eq |
202 | | } |
203 | | |
204 | | impl PartialEq<str> for Method { |
205 | | #[inline] |
206 | 0 | fn eq(&self, other: &str) -> bool { |
207 | 0 | self.as_ref() == other |
208 | 0 | } |
209 | | } |
210 | | |
211 | | impl PartialEq<Method> for str { |
212 | | #[inline] |
213 | 0 | fn eq(&self, other: &Method) -> bool { |
214 | 0 | self == other.as_ref() |
215 | 0 | } |
216 | | } |
217 | | |
218 | | impl<'a> PartialEq<&'a str> for Method { |
219 | | #[inline] |
220 | 0 | fn eq(&self, other: &&'a str) -> bool { |
221 | 0 | self.as_ref() == *other |
222 | 0 | } |
223 | | } |
224 | | |
225 | | impl<'a> PartialEq<Method> for &'a str { |
226 | | #[inline] |
227 | 0 | fn eq(&self, other: &Method) -> bool { |
228 | 0 | *self == other.as_ref() |
229 | 0 | } |
230 | | } |
231 | | |
232 | | impl fmt::Debug for Method { |
233 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
234 | 0 | f.write_str(self.as_ref()) |
235 | 0 | } |
236 | | } |
237 | | |
238 | | impl fmt::Display for Method { |
239 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
240 | 0 | fmt.write_str(self.as_ref()) |
241 | 0 | } |
242 | | } |
243 | | |
244 | | impl Default for Method { |
245 | | #[inline] |
246 | 0 | fn default() -> Method { |
247 | 0 | Method::GET |
248 | 0 | } |
249 | | } |
250 | | |
251 | | impl<'a> From<&'a Method> for Method { |
252 | | #[inline] |
253 | 0 | fn from(t: &'a Method) -> Self { |
254 | 0 | t.clone() |
255 | 0 | } |
256 | | } |
257 | | |
258 | | impl<'a> TryFrom<&'a [u8]> for Method { |
259 | | type Error = InvalidMethod; |
260 | | |
261 | | #[inline] |
262 | 0 | fn try_from(t: &'a [u8]) -> Result<Self, Self::Error> { |
263 | 0 | Method::from_bytes(t) |
264 | 0 | } Unexecuted instantiation: <http::method::Method as core::convert::TryFrom<&[u8]>>::try_from Unexecuted instantiation: <http::method::Method as core::convert::TryFrom<&[u8]>>::try_from |
265 | | } |
266 | | |
267 | | impl<'a> TryFrom<&'a str> for Method { |
268 | | type Error = InvalidMethod; |
269 | | |
270 | | #[inline] |
271 | 0 | fn try_from(t: &'a str) -> Result<Self, Self::Error> { |
272 | 0 | TryFrom::try_from(t.as_bytes()) |
273 | 0 | } Unexecuted instantiation: <http::method::Method as core::convert::TryFrom<&str>>::try_from Unexecuted instantiation: <http::method::Method as core::convert::TryFrom<&str>>::try_from |
274 | | } |
275 | | |
276 | | impl FromStr for Method { |
277 | | type Err = InvalidMethod; |
278 | | |
279 | | #[inline] |
280 | 0 | fn from_str(t: &str) -> Result<Self, Self::Err> { |
281 | 0 | TryFrom::try_from(t) |
282 | 0 | } |
283 | | } |
284 | | |
285 | | impl InvalidMethod { |
286 | 0 | fn new() -> InvalidMethod { |
287 | 0 | InvalidMethod { _priv: () } |
288 | 0 | } |
289 | | } |
290 | | |
291 | | impl fmt::Debug for InvalidMethod { |
292 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
293 | 0 | f.debug_struct("InvalidMethod") |
294 | 0 | // skip _priv noise |
295 | 0 | .finish() |
296 | 0 | } |
297 | | } |
298 | | |
299 | | impl fmt::Display for InvalidMethod { |
300 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
301 | 0 | f.write_str("invalid HTTP method") |
302 | 0 | } |
303 | | } |
304 | | |
305 | | impl Error for InvalidMethod {} |
306 | | |
307 | | mod extension { |
308 | | use super::InvalidMethod; |
309 | | use std::str; |
310 | | |
311 | | #[derive(Clone, PartialEq, Eq, Hash)] |
312 | | // Invariant: the first self.1 bytes of self.0 are valid UTF-8. |
313 | | pub struct InlineExtension([u8; InlineExtension::MAX], u8); |
314 | | |
315 | | #[derive(Clone, PartialEq, Eq, Hash)] |
316 | | // Invariant: self.0 contains valid UTF-8. |
317 | | pub struct AllocatedExtension(Box<[u8]>); |
318 | | |
319 | | impl InlineExtension { |
320 | | // Method::from_bytes() assumes this is at least 7 |
321 | | pub const MAX: usize = 15; |
322 | | |
323 | 0 | pub fn new(src: &[u8]) -> Result<InlineExtension, InvalidMethod> { |
324 | 0 | let mut data: [u8; InlineExtension::MAX] = Default::default(); |
325 | 0 |
|
326 | 0 | write_checked(src, &mut data)?; |
327 | | |
328 | | // Invariant: write_checked ensures that the first src.len() bytes |
329 | | // of data are valid UTF-8. |
330 | 0 | Ok(InlineExtension(data, src.len() as u8)) |
331 | 0 | } |
332 | | |
333 | 0 | pub fn as_str(&self) -> &str { |
334 | 0 | let InlineExtension(ref data, len) = self; |
335 | 0 | // Safety: the invariant of InlineExtension ensures that the first |
336 | 0 | // len bytes of data contain valid UTF-8. |
337 | 0 | unsafe { str::from_utf8_unchecked(&data[..*len as usize]) } |
338 | 0 | } |
339 | | } |
340 | | |
341 | | impl AllocatedExtension { |
342 | 0 | pub fn new(src: &[u8]) -> Result<AllocatedExtension, InvalidMethod> { |
343 | 0 | let mut data: Vec<u8> = vec![0; src.len()]; |
344 | 0 |
|
345 | 0 | write_checked(src, &mut data)?; |
346 | | |
347 | | // Invariant: data is exactly src.len() long and write_checked |
348 | | // ensures that the first src.len() bytes of data are valid UTF-8. |
349 | 0 | Ok(AllocatedExtension(data.into_boxed_slice())) |
350 | 0 | } |
351 | | |
352 | 0 | pub fn as_str(&self) -> &str { |
353 | 0 | // Safety: the invariant of AllocatedExtension ensures that self.0 |
354 | 0 | // contains valid UTF-8. |
355 | 0 | unsafe { str::from_utf8_unchecked(&self.0) } |
356 | 0 | } |
357 | | } |
358 | | |
359 | | // From the RFC 9110 HTTP Semantics, section 9.1, the HTTP method is case-sensitive and can |
360 | | // contain the following characters: |
361 | | // |
362 | | // ``` |
363 | | // method = token |
364 | | // token = 1*tchar |
365 | | // tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / |
366 | | // "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA |
367 | | // ``` |
368 | | // |
369 | | // https://datatracker.ietf.org/doc/html/rfc9110#section-9.1 |
370 | | // |
371 | | // Note that this definition means that any &[u8] that consists solely of valid |
372 | | // characters is also valid UTF-8 because the valid method characters are a |
373 | | // subset of the valid 1 byte UTF-8 encoding. |
374 | | #[rustfmt::skip] |
375 | | const METHOD_CHARS: [u8; 256] = [ |
376 | | // 0 1 2 3 4 5 6 7 8 9 |
377 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // x |
378 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 1x |
379 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 2x |
380 | | b'\0', b'\0', b'\0', b'!', b'\0', b'#', b'$', b'%', b'&', b'\'', // 3x |
381 | | b'\0', b'\0', b'*', b'+', b'\0', b'-', b'.', b'\0', b'0', b'1', // 4x |
382 | | b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'\0', b'\0', // 5x |
383 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'A', b'B', b'C', b'D', b'E', // 6x |
384 | | b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', // 7x |
385 | | b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', // 8x |
386 | | b'Z', b'\0', b'\0', b'\0', b'^', b'_', b'`', b'a', b'b', b'c', // 9x |
387 | | b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x |
388 | | b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x |
389 | | b'x', b'y', b'z', b'\0', b'|', b'\0', b'~', b'\0', b'\0', b'\0', // 12x |
390 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 13x |
391 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 14x |
392 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 15x |
393 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 16x |
394 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 17x |
395 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 18x |
396 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 19x |
397 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 20x |
398 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 21x |
399 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 22x |
400 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 23x |
401 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 24x |
402 | | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0' // 25x |
403 | | ]; |
404 | | |
405 | | // write_checked ensures (among other things) that the first src.len() bytes |
406 | | // of dst are valid UTF-8 |
407 | 0 | fn write_checked(src: &[u8], dst: &mut [u8]) -> Result<(), InvalidMethod> { |
408 | 0 | for (i, &b) in src.iter().enumerate() { |
409 | 0 | let b = METHOD_CHARS[b as usize]; |
410 | 0 |
|
411 | 0 | if b == 0 { |
412 | 0 | return Err(InvalidMethod::new()); |
413 | 0 | } |
414 | 0 |
|
415 | 0 | dst[i] = b; |
416 | | } |
417 | | |
418 | 0 | Ok(()) |
419 | 0 | } |
420 | | } |
421 | | |
422 | | #[cfg(test)] |
423 | | mod test { |
424 | | use super::*; |
425 | | |
426 | | #[test] |
427 | | fn test_method_eq() { |
428 | | assert_eq!(Method::GET, Method::GET); |
429 | | assert_eq!(Method::GET, "GET"); |
430 | | assert_eq!(&Method::GET, "GET"); |
431 | | |
432 | | assert_eq!("GET", Method::GET); |
433 | | assert_eq!("GET", &Method::GET); |
434 | | |
435 | | assert_eq!(&Method::GET, Method::GET); |
436 | | assert_eq!(Method::GET, &Method::GET); |
437 | | } |
438 | | |
439 | | #[test] |
440 | | fn test_invalid_method() { |
441 | | assert!(Method::from_str("").is_err()); |
442 | | assert!(Method::from_bytes(b"").is_err()); |
443 | | assert!(Method::from_bytes(&[0xC0]).is_err()); // invalid utf-8 |
444 | | assert!(Method::from_bytes(&[0x10]).is_err()); // invalid method characters |
445 | | } |
446 | | |
447 | | #[test] |
448 | | fn test_is_idempotent() { |
449 | | assert!(Method::OPTIONS.is_idempotent()); |
450 | | assert!(Method::GET.is_idempotent()); |
451 | | assert!(Method::PUT.is_idempotent()); |
452 | | assert!(Method::DELETE.is_idempotent()); |
453 | | assert!(Method::HEAD.is_idempotent()); |
454 | | assert!(Method::TRACE.is_idempotent()); |
455 | | |
456 | | assert!(!Method::POST.is_idempotent()); |
457 | | assert!(!Method::CONNECT.is_idempotent()); |
458 | | assert!(!Method::PATCH.is_idempotent()); |
459 | | } |
460 | | |
461 | | #[test] |
462 | | fn test_extension_method() { |
463 | | assert_eq!(Method::from_str("WOW").unwrap(), "WOW"); |
464 | | assert_eq!(Method::from_str("wOw!!").unwrap(), "wOw!!"); |
465 | | |
466 | | let long_method = "This_is_a_very_long_method.It_is_valid_but_unlikely."; |
467 | | assert_eq!(Method::from_str(long_method).unwrap(), long_method); |
468 | | |
469 | | let longest_inline_method = [b'A'; InlineExtension::MAX]; |
470 | | assert_eq!( |
471 | | Method::from_bytes(&longest_inline_method).unwrap(), |
472 | | Method(ExtensionInline( |
473 | | InlineExtension::new(&longest_inline_method).unwrap() |
474 | | )) |
475 | | ); |
476 | | let shortest_allocated_method = [b'A'; InlineExtension::MAX + 1]; |
477 | | assert_eq!( |
478 | | Method::from_bytes(&shortest_allocated_method).unwrap(), |
479 | | Method(ExtensionAllocated( |
480 | | AllocatedExtension::new(&shortest_allocated_method).unwrap() |
481 | | )) |
482 | | ); |
483 | | } |
484 | | |
485 | | #[test] |
486 | | fn test_extension_method_chars() { |
487 | | const VALID_METHOD_CHARS: &str = |
488 | | "!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
489 | | |
490 | | for c in VALID_METHOD_CHARS.chars() { |
491 | | let c = c.to_string(); |
492 | | |
493 | | assert_eq!( |
494 | | Method::from_str(&c).unwrap(), |
495 | | c.as_str(), |
496 | | "testing {c} is a valid method character" |
497 | | ); |
498 | | } |
499 | | } |
500 | | } |