chore(stability): remove core feature gate
This commit is contained in:
318
src/status.rs
318
src/status.rs
@@ -1,6 +1,5 @@
|
||||
//! HTTP status codes
|
||||
use std::fmt;
|
||||
use std::num::{FromPrimitive, ToPrimitive};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
// shamelessly lifted from Teepee. I tried a few schemes, this really
|
||||
@@ -20,11 +19,9 @@ use std::cmp::Ordering;
|
||||
/// `self.class().default_code()`:
|
||||
///
|
||||
/// ```rust
|
||||
/// #![feature(core)]
|
||||
/// # use std::num::FromPrimitive;
|
||||
/// # use hyper::status::StatusCode;
|
||||
/// let statusopt: Option<StatusCode> = FromPrimitive::from_u16(137u16);
|
||||
/// assert_eq!(statusopt.unwrap().class().default_code(), StatusCode::Continue);
|
||||
/// let status = StatusCode::Unregistered(123);
|
||||
/// assert_eq!(status.class().default_code(), StatusCode::Continue);
|
||||
/// ```
|
||||
///
|
||||
/// IANA maintain the [Hypertext Transfer Protocol (HTTP) Status Code
|
||||
@@ -225,6 +222,136 @@ pub enum StatusCode {
|
||||
|
||||
impl StatusCode {
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn from_u16(n: u16) -> StatusCode {
|
||||
match n {
|
||||
100 => StatusCode::Continue,
|
||||
101 => StatusCode::SwitchingProtocols,
|
||||
102 => StatusCode::Processing,
|
||||
200 => StatusCode::Ok,
|
||||
201 => StatusCode::Created,
|
||||
202 => StatusCode::Accepted,
|
||||
203 => StatusCode::NonAuthoritativeInformation,
|
||||
204 => StatusCode::NoContent,
|
||||
205 => StatusCode::ResetContent,
|
||||
206 => StatusCode::PartialContent,
|
||||
207 => StatusCode::MultiStatus,
|
||||
208 => StatusCode::AlreadyReported,
|
||||
226 => StatusCode::ImUsed,
|
||||
300 => StatusCode::MultipleChoices,
|
||||
301 => StatusCode::MovedPermanently,
|
||||
302 => StatusCode::Found,
|
||||
303 => StatusCode::SeeOther,
|
||||
304 => StatusCode::NotModified,
|
||||
305 => StatusCode::UseProxy,
|
||||
307 => StatusCode::TemporaryRedirect,
|
||||
308 => StatusCode::PermanentRedirect,
|
||||
400 => StatusCode::BadRequest,
|
||||
401 => StatusCode::Unauthorized,
|
||||
402 => StatusCode::PaymentRequired,
|
||||
403 => StatusCode::Forbidden,
|
||||
404 => StatusCode::NotFound,
|
||||
405 => StatusCode::MethodNotAllowed,
|
||||
406 => StatusCode::NotAcceptable,
|
||||
407 => StatusCode::ProxyAuthenticationRequired,
|
||||
408 => StatusCode::RequestTimeout,
|
||||
409 => StatusCode::Conflict,
|
||||
410 => StatusCode::Gone,
|
||||
411 => StatusCode::LengthRequired,
|
||||
412 => StatusCode::PreconditionFailed,
|
||||
413 => StatusCode::PayloadTooLarge,
|
||||
414 => StatusCode::UriTooLong,
|
||||
415 => StatusCode::UnsupportedMediaType,
|
||||
416 => StatusCode::RangeNotSatisfiable,
|
||||
417 => StatusCode::ExpectationFailed,
|
||||
418 => StatusCode::ImATeapot,
|
||||
422 => StatusCode::UnprocessableEntity,
|
||||
423 => StatusCode::Locked,
|
||||
424 => StatusCode::FailedDependency,
|
||||
426 => StatusCode::UpgradeRequired,
|
||||
428 => StatusCode::PreconditionRequired,
|
||||
429 => StatusCode::TooManyRequests,
|
||||
431 => StatusCode::RequestHeaderFieldsTooLarge,
|
||||
500 => StatusCode::InternalServerError,
|
||||
501 => StatusCode::NotImplemented,
|
||||
502 => StatusCode::BadGateway,
|
||||
503 => StatusCode::ServiceUnavailable,
|
||||
504 => StatusCode::GatewayTimeout,
|
||||
505 => StatusCode::HttpVersionNotSupported,
|
||||
506 => StatusCode::VariantAlsoNegotiates,
|
||||
507 => StatusCode::InsufficientStorage,
|
||||
508 => StatusCode::LoopDetected,
|
||||
510 => StatusCode::NotExtended,
|
||||
511 => StatusCode::NetworkAuthenticationRequired,
|
||||
_ => StatusCode::Unregistered(n),
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
match *self {
|
||||
StatusCode::Continue => 100,
|
||||
StatusCode::SwitchingProtocols => 101,
|
||||
StatusCode::Processing => 102,
|
||||
StatusCode::Ok => 200,
|
||||
StatusCode::Created => 201,
|
||||
StatusCode::Accepted => 202,
|
||||
StatusCode::NonAuthoritativeInformation => 203,
|
||||
StatusCode::NoContent => 204,
|
||||
StatusCode::ResetContent => 205,
|
||||
StatusCode::PartialContent => 206,
|
||||
StatusCode::MultiStatus => 207,
|
||||
StatusCode::AlreadyReported => 208,
|
||||
StatusCode::ImUsed => 226,
|
||||
StatusCode::MultipleChoices => 300,
|
||||
StatusCode::MovedPermanently => 301,
|
||||
StatusCode::Found => 302,
|
||||
StatusCode::SeeOther => 303,
|
||||
StatusCode::NotModified => 304,
|
||||
StatusCode::UseProxy => 305,
|
||||
StatusCode::TemporaryRedirect => 307,
|
||||
StatusCode::PermanentRedirect => 308,
|
||||
StatusCode::BadRequest => 400,
|
||||
StatusCode::Unauthorized => 401,
|
||||
StatusCode::PaymentRequired => 402,
|
||||
StatusCode::Forbidden => 403,
|
||||
StatusCode::NotFound => 404,
|
||||
StatusCode::MethodNotAllowed => 405,
|
||||
StatusCode::NotAcceptable => 406,
|
||||
StatusCode::ProxyAuthenticationRequired => 407,
|
||||
StatusCode::RequestTimeout => 408,
|
||||
StatusCode::Conflict => 409,
|
||||
StatusCode::Gone => 410,
|
||||
StatusCode::LengthRequired => 411,
|
||||
StatusCode::PreconditionFailed => 412,
|
||||
StatusCode::PayloadTooLarge => 413,
|
||||
StatusCode::UriTooLong => 414,
|
||||
StatusCode::UnsupportedMediaType => 415,
|
||||
StatusCode::RangeNotSatisfiable => 416,
|
||||
StatusCode::ExpectationFailed => 417,
|
||||
StatusCode::ImATeapot => 418,
|
||||
StatusCode::UnprocessableEntity => 422,
|
||||
StatusCode::Locked => 423,
|
||||
StatusCode::FailedDependency => 424,
|
||||
StatusCode::UpgradeRequired => 426,
|
||||
StatusCode::PreconditionRequired => 428,
|
||||
StatusCode::TooManyRequests => 429,
|
||||
StatusCode::RequestHeaderFieldsTooLarge => 431,
|
||||
StatusCode::InternalServerError => 500,
|
||||
StatusCode::NotImplemented => 501,
|
||||
StatusCode::BadGateway => 502,
|
||||
StatusCode::ServiceUnavailable => 503,
|
||||
StatusCode::GatewayTimeout => 504,
|
||||
StatusCode::HttpVersionNotSupported => 505,
|
||||
StatusCode::VariantAlsoNegotiates => 506,
|
||||
StatusCode::InsufficientStorage => 507,
|
||||
StatusCode::LoopDetected => 508,
|
||||
StatusCode::NotExtended => 510,
|
||||
StatusCode::NetworkAuthenticationRequired => 511,
|
||||
StatusCode::Unregistered(n) => n,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the standardised `reason-phrase` for this status code.
|
||||
///
|
||||
/// This is mostly here for servers writing responses, but could potentially have application
|
||||
@@ -312,7 +439,7 @@ impl StatusCode {
|
||||
|
||||
/// Determine the class of a status code, based on its first digit.
|
||||
pub fn class(&self) -> StatusClass {
|
||||
match self.to_u16().unwrap() {
|
||||
match self.to_u16() {
|
||||
100...199 => StatusClass::Informational,
|
||||
200...299 => StatusClass::Success,
|
||||
300...399 => StatusClass::Redirection,
|
||||
@@ -363,19 +490,9 @@ impl Copy for StatusCode {}
|
||||
/// assert_eq!(format!("{}", Unregistered(123)),
|
||||
/// "123 <unknown status code>");
|
||||
/// ```
|
||||
///
|
||||
/// If you wish to just include the number, convert to `u16` instead:
|
||||
///
|
||||
/// ```rust
|
||||
/// #![feature(core)]
|
||||
/// # use std::num::ToPrimitive;
|
||||
/// # use hyper::status::StatusCode::{ImATeapot, Unregistered};
|
||||
/// assert_eq!(format!("{}", ImATeapot.to_u16().unwrap()), "418");
|
||||
/// assert_eq!(format!("{}", Unregistered(123).to_u16().unwrap()), "123");
|
||||
/// ```
|
||||
impl fmt::Display for StatusCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{} {}", self.to_u16().unwrap(),
|
||||
write!(f, "{} {}", self.to_u16(),
|
||||
self.canonical_reason().unwrap_or("<unknown status code>"))
|
||||
}
|
||||
}
|
||||
@@ -396,88 +513,10 @@ impl Clone for StatusCode {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromPrimitive for StatusCode {
|
||||
fn from_i64(n: i64) -> Option<StatusCode> {
|
||||
if n < 0 {
|
||||
None
|
||||
} else {
|
||||
FromPrimitive::from_u64(n as u64)
|
||||
}
|
||||
}
|
||||
|
||||
fn from_u64(n: u64) -> Option<StatusCode> {
|
||||
if n > 65535 {
|
||||
None
|
||||
} else {
|
||||
Some(match n {
|
||||
100 => StatusCode::Continue,
|
||||
101 => StatusCode::SwitchingProtocols,
|
||||
102 => StatusCode::Processing,
|
||||
200 => StatusCode::Ok,
|
||||
201 => StatusCode::Created,
|
||||
202 => StatusCode::Accepted,
|
||||
203 => StatusCode::NonAuthoritativeInformation,
|
||||
204 => StatusCode::NoContent,
|
||||
205 => StatusCode::ResetContent,
|
||||
206 => StatusCode::PartialContent,
|
||||
207 => StatusCode::MultiStatus,
|
||||
208 => StatusCode::AlreadyReported,
|
||||
226 => StatusCode::ImUsed,
|
||||
300 => StatusCode::MultipleChoices,
|
||||
301 => StatusCode::MovedPermanently,
|
||||
302 => StatusCode::Found,
|
||||
303 => StatusCode::SeeOther,
|
||||
304 => StatusCode::NotModified,
|
||||
305 => StatusCode::UseProxy,
|
||||
307 => StatusCode::TemporaryRedirect,
|
||||
308 => StatusCode::PermanentRedirect,
|
||||
400 => StatusCode::BadRequest,
|
||||
401 => StatusCode::Unauthorized,
|
||||
402 => StatusCode::PaymentRequired,
|
||||
403 => StatusCode::Forbidden,
|
||||
404 => StatusCode::NotFound,
|
||||
405 => StatusCode::MethodNotAllowed,
|
||||
406 => StatusCode::NotAcceptable,
|
||||
407 => StatusCode::ProxyAuthenticationRequired,
|
||||
408 => StatusCode::RequestTimeout,
|
||||
409 => StatusCode::Conflict,
|
||||
410 => StatusCode::Gone,
|
||||
411 => StatusCode::LengthRequired,
|
||||
412 => StatusCode::PreconditionFailed,
|
||||
413 => StatusCode::PayloadTooLarge,
|
||||
414 => StatusCode::UriTooLong,
|
||||
415 => StatusCode::UnsupportedMediaType,
|
||||
416 => StatusCode::RangeNotSatisfiable,
|
||||
417 => StatusCode::ExpectationFailed,
|
||||
418 => StatusCode::ImATeapot,
|
||||
422 => StatusCode::UnprocessableEntity,
|
||||
423 => StatusCode::Locked,
|
||||
424 => StatusCode::FailedDependency,
|
||||
426 => StatusCode::UpgradeRequired,
|
||||
428 => StatusCode::PreconditionRequired,
|
||||
429 => StatusCode::TooManyRequests,
|
||||
431 => StatusCode::RequestHeaderFieldsTooLarge,
|
||||
500 => StatusCode::InternalServerError,
|
||||
501 => StatusCode::NotImplemented,
|
||||
502 => StatusCode::BadGateway,
|
||||
503 => StatusCode::ServiceUnavailable,
|
||||
504 => StatusCode::GatewayTimeout,
|
||||
505 => StatusCode::HttpVersionNotSupported,
|
||||
506 => StatusCode::VariantAlsoNegotiates,
|
||||
507 => StatusCode::InsufficientStorage,
|
||||
508 => StatusCode::LoopDetected,
|
||||
510 => StatusCode::NotExtended,
|
||||
511 => StatusCode::NetworkAuthenticationRequired,
|
||||
_ => StatusCode::Unregistered(n as u16),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for StatusCode {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &StatusCode) -> Option<Ordering> {
|
||||
self.to_u16().unwrap().partial_cmp(&(other.to_u16().unwrap()))
|
||||
self.to_u16().partial_cmp(&(other.to_u16()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -494,76 +533,6 @@ impl Ord for StatusCode {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPrimitive for StatusCode {
|
||||
fn to_i64(&self) -> Option<i64> {
|
||||
Some(self.to_u64().unwrap() as i64)
|
||||
}
|
||||
|
||||
fn to_u64(&self) -> Option<u64> {
|
||||
Some(match *self {
|
||||
StatusCode::Continue => 100,
|
||||
StatusCode::SwitchingProtocols => 101,
|
||||
StatusCode::Processing => 102,
|
||||
StatusCode::Ok => 200,
|
||||
StatusCode::Created => 201,
|
||||
StatusCode::Accepted => 202,
|
||||
StatusCode::NonAuthoritativeInformation => 203,
|
||||
StatusCode::NoContent => 204,
|
||||
StatusCode::ResetContent => 205,
|
||||
StatusCode::PartialContent => 206,
|
||||
StatusCode::MultiStatus => 207,
|
||||
StatusCode::AlreadyReported => 208,
|
||||
StatusCode::ImUsed => 226,
|
||||
StatusCode::MultipleChoices => 300,
|
||||
StatusCode::MovedPermanently => 301,
|
||||
StatusCode::Found => 302,
|
||||
StatusCode::SeeOther => 303,
|
||||
StatusCode::NotModified => 304,
|
||||
StatusCode::UseProxy => 305,
|
||||
StatusCode::TemporaryRedirect => 307,
|
||||
StatusCode::PermanentRedirect => 308,
|
||||
StatusCode::BadRequest => 400,
|
||||
StatusCode::Unauthorized => 401,
|
||||
StatusCode::PaymentRequired => 402,
|
||||
StatusCode::Forbidden => 403,
|
||||
StatusCode::NotFound => 404,
|
||||
StatusCode::MethodNotAllowed => 405,
|
||||
StatusCode::NotAcceptable => 406,
|
||||
StatusCode::ProxyAuthenticationRequired => 407,
|
||||
StatusCode::RequestTimeout => 408,
|
||||
StatusCode::Conflict => 409,
|
||||
StatusCode::Gone => 410,
|
||||
StatusCode::LengthRequired => 411,
|
||||
StatusCode::PreconditionFailed => 412,
|
||||
StatusCode::PayloadTooLarge => 413,
|
||||
StatusCode::UriTooLong => 414,
|
||||
StatusCode::UnsupportedMediaType => 415,
|
||||
StatusCode::RangeNotSatisfiable => 416,
|
||||
StatusCode::ExpectationFailed => 417,
|
||||
StatusCode::ImATeapot => 418,
|
||||
StatusCode::UnprocessableEntity => 422,
|
||||
StatusCode::Locked => 423,
|
||||
StatusCode::FailedDependency => 424,
|
||||
StatusCode::UpgradeRequired => 426,
|
||||
StatusCode::PreconditionRequired => 428,
|
||||
StatusCode::TooManyRequests => 429,
|
||||
StatusCode::RequestHeaderFieldsTooLarge => 431,
|
||||
StatusCode::InternalServerError => 500,
|
||||
StatusCode::NotImplemented => 501,
|
||||
StatusCode::BadGateway => 502,
|
||||
StatusCode::ServiceUnavailable => 503,
|
||||
StatusCode::GatewayTimeout => 504,
|
||||
StatusCode::HttpVersionNotSupported => 505,
|
||||
StatusCode::VariantAlsoNegotiates => 506,
|
||||
StatusCode::InsufficientStorage => 507,
|
||||
StatusCode::LoopDetected => 508,
|
||||
StatusCode::NotExtended => 510,
|
||||
StatusCode::NetworkAuthenticationRequired => 511,
|
||||
StatusCode::Unregistered(n) => n,
|
||||
} as u64)
|
||||
}
|
||||
}
|
||||
|
||||
/// The class of an HTTP `status-code`.
|
||||
///
|
||||
/// [RFC 7231, section 6 (Response Status Codes)](https://tools.ietf.org/html/rfc7231#section-6):
|
||||
@@ -669,20 +638,3 @@ impl StatusClass {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPrimitive for StatusClass {
|
||||
fn to_i64(&self) -> Option<i64> {
|
||||
Some(self.to_u64().unwrap() as i64)
|
||||
}
|
||||
|
||||
fn to_u64(&self) -> Option<u64> {
|
||||
Some(match *self {
|
||||
StatusClass::Informational => 100,
|
||||
StatusClass::Success => 200,
|
||||
StatusClass::Redirection => 300,
|
||||
StatusClass::ClientError => 400,
|
||||
StatusClass::ServerError => 500,
|
||||
StatusClass::NoClass => 200,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user