//! The HTTP request method //! //! This module contains HTTP-method related structs and errors and such. The //! main type of this module, `Method`, is also reexported at the root of the //! crate as `http::Method` and is intended for import through that location //! primarily. //! //! # Examples //! //! ``` //! use http::Method; //! //! assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); //! assert!(Method::GET.is_idempotent()); //! assert_eq!(Method::POST.as_str(), "POST"); //! ```
use std::convert::AsRef; use std::error::Error; use std::str::FromStr; use std::convert::TryFrom; use std::{fmt, str};
/// The Request Method (VERB) /// /// This type also contains constants for a number of common HTTP methods such /// as GET, POST, etc. /// /// Currently includes 8 variants representing the 8 methods defined in /// [RFC 7230](https://tools.ietf.org/html/rfc7231#section-4.1), plus PATCH, /// and an Extension variant for all extensions. /// /// # Examples /// /// ``` /// use http::Method; /// /// assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); /// assert!(Method::GET.is_idempotent()); /// assert_eq!(Method::POST.as_str(), "POST"); /// ``` #[derive(Clone, PartialEq, Eq, Hash)] pubstruct Method(Inner);
/// A possible error value when converting `Method` from bytes. pubstruct InvalidMethod {
_priv: (),
}
#[derive(Clone, PartialEq, Eq, Hash)] enum Inner {
Options,
Get,
Post,
Put,
Delete,
Head,
Trace,
Connect,
Patch, // If the extension is short enough, store it inline
ExtensionInline(InlineExtension), // Otherwise, allocate it
ExtensionAllocated(AllocatedExtension),
}
impl Method { /// GET pubconst GET: Method = Method(Get);
/// Whether a method is considered "safe", meaning the request is /// essentially read-only. /// /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.1) /// for more words. pubfn is_safe(&self) -> bool { matchself.0 {
Get | Head | Options | Trace => true,
_ => false,
}
}
/// Whether a method is considered "idempotent", meaning the request has /// the same result if executed multiple times. /// /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.2) for /// more words. pubfn is_idempotent(&self) -> bool { matchself.0 {
Put | Delete => true,
_ => self.is_safe(),
}
}
/// Return a &str representation of the HTTP method #[inline] pubfn as_str(&self) -> &str { matchself.0 {
Options => "OPTIONS",
Get => "GET",
Post => "POST",
Put => "PUT",
Delete => "DELETE",
Head => "HEAD",
Trace => "TRACE",
Connect => "CONNECT",
Patch => "PATCH",
ExtensionInline(ref inline) => inline.as_str(),
ExtensionAllocated(ref allocated) => allocated.as_str(),
}
}
}
mod extension { usesuper::InvalidMethod; use std::str;
#[derive(Clone, PartialEq, Eq, Hash)] // Invariant: the first self.1 bytes of self.0 are valid UTF-8. pubstruct InlineExtension([u8; InlineExtension::MAX], u8);
// Invariant: write_checked ensures that the first src.len() bytes // of data are valid UTF-8.
Ok(InlineExtension(data, src.len() as u8))
}
pubfn as_str(&self) -> &str { let InlineExtension(ref data, len) = self; // Safety: the invariant of InlineExtension ensures that the first // len bytes of data contain valid UTF-8. unsafe {str::from_utf8_unchecked(&data[..*len as usize])}
}
}
// Invariant: data is exactly src.len() long and write_checked // ensures that the first src.len() bytes of data are valid UTF-8.
Ok(AllocatedExtension(data.into_boxed_slice()))
}
pubfn as_str(&self) -> &str { // Safety: the invariant of AllocatedExtension ensures that self.0 // contains valid UTF-8. unsafe {str::from_utf8_unchecked(&self.0)}
}
}
// write_checked ensures (among other things) that the first src.len() bytes // of dst are valid UTF-8 fn write_checked(src: &[u8], dst: &mut [u8]) -> Result<(), InvalidMethod> { for (i, &b) in src.iter().enumerate() { let b = METHOD_CHARS[b as usize];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.