Struct attohttpc::StatusCode

source ·
pub struct StatusCode(_);
Expand description

An HTTP status code (status-code in RFC 7230 et al.).

Constants are provided for known status codes, including those in the IANA HTTP Status Code Registry.

Status code values in the range 100-999 (inclusive) are supported by this type. Values in the range 100-599 are semantically classified by the most significant digit. See StatusCode::is_success, etc. Values above 599 are unclassified but allowed for legacy compatibility, though their use is discouraged. Applications may interpret such values as protocol errors.

Examples

use http::StatusCode;

assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK);
assert_eq!(StatusCode::NOT_FOUND.as_u16(), 404);
assert!(StatusCode::OK.is_success());

Implementations§

source§

impl StatusCode

source

pub fn from_u16(src: u16) -> Result<StatusCode, InvalidStatusCode>

Converts a u16 to a status code.

The function validates the correctness of the supplied u16. It must be greater or equal to 100 and less than 1000.

Example
use http::StatusCode;

let ok = StatusCode::from_u16(200).unwrap();
assert_eq!(ok, StatusCode::OK);

let err = StatusCode::from_u16(99);
assert!(err.is_err());
source

pub fn from_bytes(src: &[u8]) -> Result<StatusCode, InvalidStatusCode>

Converts a &u8 to a status code

source

pub fn as_u16(&self) -> u16

Returns the u16 corresponding to this StatusCode.

Note

This is the same as the From<StatusCode> implementation, but included as an inherent method because that implementation doesn’t appear in rustdocs, as well as a way to force the type instead of relying on inference.

Example
let status = http::StatusCode::OK;
assert_eq!(status.as_u16(), 200);
source

pub fn as_str(&self) -> &str

Returns a &str representation of the StatusCode

The return value only includes a numerical representation of the status code. The canonical reason is not included.

Example
let status = http::StatusCode::OK;
assert_eq!(status.as_str(), "200");
source

pub fn canonical_reason(&self) -> Option<&'static str>

Get the standardised reason-phrase for this status code.

This is mostly here for servers writing responses, but could potentially have application at other times.

The reason phrase is defined as being exclusively for human readers. You should avoid deriving any meaning from it at all costs.

Bear in mind also that in HTTP/2.0 and HTTP/3.0 the reason phrase is abolished from transmission, and so this canonical reason phrase really is the only reason phrase you’ll find.

Example
let status = http::StatusCode::OK;
assert_eq!(status.canonical_reason(), Some("OK"));
source

pub fn is_informational(&self) -> bool

Check if status is within 100-199.

source

pub fn is_success(&self) -> bool

Check if status is within 200-299.

source

pub fn is_redirection(&self) -> bool

Check if status is within 300-399.

source

pub fn is_client_error(&self) -> bool

Check if status is within 400-499.

source

pub fn is_server_error(&self) -> bool

Check if status is within 500-599.

source§

impl StatusCode

source

pub const CONTINUE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(100) })

100 Continue [RFC7231, Section 6.2.1]

source

pub const SWITCHING_PROTOCOLS: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(101) })

101 Switching Protocols [RFC7231, Section 6.2.2]

source

pub const PROCESSING: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(102) })

102 Processing [RFC2518]

source

pub const OK: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(200) })

source

pub const CREATED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(201) })

201 Created [RFC7231, Section 6.3.2]

source

pub const ACCEPTED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(202) })

202 Accepted [RFC7231, Section 6.3.3]

source

pub const NON_AUTHORITATIVE_INFORMATION: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(203) })

203 Non-Authoritative Information [RFC7231, Section 6.3.4]

source

pub const NO_CONTENT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(204) })

204 No Content [RFC7231, Section 6.3.5]

source

pub const RESET_CONTENT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(205) })

205 Reset Content [RFC7231, Section 6.3.6]

source

pub const PARTIAL_CONTENT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(206) })

206 Partial Content [RFC7233, Section 4.1]

source

pub const MULTI_STATUS: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(207) })

207 Multi-Status [RFC4918]

source

pub const ALREADY_REPORTED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(208) })

208 Already Reported [RFC5842]

source

pub const IM_USED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(226) })

226 IM Used [RFC3229]

source

pub const MULTIPLE_CHOICES: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(300) })

300 Multiple Choices [RFC7231, Section 6.4.1]

source

pub const MOVED_PERMANENTLY: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(301) })

301 Moved Permanently [RFC7231, Section 6.4.2]

source

pub const FOUND: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(302) })

source

pub const SEE_OTHER: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(303) })

303 See Other [RFC7231, Section 6.4.4]

source

pub const NOT_MODIFIED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(304) })

304 Not Modified [RFC7232, Section 4.1]

source

pub const USE_PROXY: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(305) })

305 Use Proxy [RFC7231, Section 6.4.5]

source

pub const TEMPORARY_REDIRECT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(307) })

307 Temporary Redirect [RFC7231, Section 6.4.7]

source

pub const PERMANENT_REDIRECT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(308) })

308 Permanent Redirect [RFC7238]

source

pub const BAD_REQUEST: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(400) })

400 Bad Request [RFC7231, Section 6.5.1]

source

pub const UNAUTHORIZED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(401) })

401 Unauthorized [RFC7235, Section 3.1]

source

pub const PAYMENT_REQUIRED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(402) })

402 Payment Required [RFC7231, Section 6.5.2]

source

pub const FORBIDDEN: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(403) })

403 Forbidden [RFC7231, Section 6.5.3]

source

pub const NOT_FOUND: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(404) })

404 Not Found [RFC7231, Section 6.5.4]

source

pub const METHOD_NOT_ALLOWED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(405) })

405 Method Not Allowed [RFC7231, Section 6.5.5]

source

pub const NOT_ACCEPTABLE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(406) })

406 Not Acceptable [RFC7231, Section 6.5.6]

source

pub const PROXY_AUTHENTICATION_REQUIRED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(407) })

407 Proxy Authentication Required [RFC7235, Section 3.2]

source

pub const REQUEST_TIMEOUT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(408) })

408 Request Timeout [RFC7231, Section 6.5.7]

source

pub const CONFLICT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(409) })

409 Conflict [RFC7231, Section 6.5.8]

source

pub const GONE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(410) })

source

pub const LENGTH_REQUIRED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(411) })

411 Length Required [RFC7231, Section 6.5.10]

source

pub const PRECONDITION_FAILED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(412) })

412 Precondition Failed [RFC7232, Section 4.2]

source

pub const PAYLOAD_TOO_LARGE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(413) })

413 Payload Too Large [RFC7231, Section 6.5.11]

source

pub const URI_TOO_LONG: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(414) })

414 URI Too Long [RFC7231, Section 6.5.12]

source

pub const UNSUPPORTED_MEDIA_TYPE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(415) })

415 Unsupported Media Type [RFC7231, Section 6.5.13]

source

pub const RANGE_NOT_SATISFIABLE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(416) })

416 Range Not Satisfiable [RFC7233, Section 4.4]

source

pub const EXPECTATION_FAILED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(417) })

417 Expectation Failed [RFC7231, Section 6.5.14]

source

pub const IM_A_TEAPOT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(418) })

418 I’m a teapot [curiously not registered by IANA but RFC2324]

source

pub const MISDIRECTED_REQUEST: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(421) })

421 Misdirected Request RFC7540, Section 9.1.2

source

pub const UNPROCESSABLE_ENTITY: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(422) })

422 Unprocessable Entity [RFC4918]

source

pub const LOCKED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(423) })

423 Locked [RFC4918]

source

pub const FAILED_DEPENDENCY: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(424) })

424 Failed Dependency [RFC4918]

source

pub const UPGRADE_REQUIRED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(426) })

426 Upgrade Required [RFC7231, Section 6.5.15]

source

pub const PRECONDITION_REQUIRED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(428) })

428 Precondition Required [RFC6585]

source

pub const TOO_MANY_REQUESTS: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(429) })

429 Too Many Requests [RFC6585]

source

pub const REQUEST_HEADER_FIELDS_TOO_LARGE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(431) })

431 Request Header Fields Too Large [RFC6585]

451 Unavailable For Legal Reasons [RFC7725]

source

pub const INTERNAL_SERVER_ERROR: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(500) })

500 Internal Server Error [RFC7231, Section 6.6.1]

source

pub const NOT_IMPLEMENTED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(501) })

501 Not Implemented [RFC7231, Section 6.6.2]

source

pub const BAD_GATEWAY: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(502) })

502 Bad Gateway [RFC7231, Section 6.6.3]

source

pub const SERVICE_UNAVAILABLE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(503) })

503 Service Unavailable [RFC7231, Section 6.6.4]

source

pub const GATEWAY_TIMEOUT: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(504) })

504 Gateway Timeout [RFC7231, Section 6.6.5]

source

pub const HTTP_VERSION_NOT_SUPPORTED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(505) })

505 HTTP Version Not Supported [RFC7231, Section 6.6.6]

source

pub const VARIANT_ALSO_NEGOTIATES: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(506) })

506 Variant Also Negotiates [RFC2295]

source

pub const INSUFFICIENT_STORAGE: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(507) })

507 Insufficient Storage [RFC4918]

source

pub const LOOP_DETECTED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(508) })

508 Loop Detected [RFC5842]

source

pub const NOT_EXTENDED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(510) })

510 Not Extended [RFC2774]

source

pub const NETWORK_AUTHENTICATION_REQUIRED: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked(511) })

511 Network Authentication Required [RFC6585]

Trait Implementations§

source§

impl Clone for StatusCode

source§

fn clone(&self) -> StatusCode

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for StatusCode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for StatusCode

source§

fn default() -> StatusCode

Returns the “default value” for a type. Read more
source§

impl Display for StatusCode

Formats the status code, including the canonical reason.

Example

assert_eq!(format!("{}", StatusCode::OK), "200 OK");
source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a StatusCode> for StatusCode

source§

fn from(t: &'a StatusCode) -> StatusCode

Converts to this type from the input type.
source§

impl FromStr for StatusCode

§

type Err = InvalidStatusCode

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<StatusCode, InvalidStatusCode>

Parses a string s to return a value of this type. Read more
source§

impl Hash for StatusCode

source§

fn hash<__H>(&self, state: &mut __H)where
    __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where
    H: Hasher,
    Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for StatusCode

source§

fn cmp(&self, other: &StatusCode) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere
    Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<StatusCode> for StatusCode

source§

fn eq(&self, other: &StatusCode) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<u16> for StatusCode

source§

fn eq(&self, other: &u16) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<StatusCode> for StatusCode

source§

fn partial_cmp(&self, other: &StatusCode) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a> TryFrom<&'a [u8]> for StatusCode

§

type Error = InvalidStatusCode

The type returned in the event of a conversion error.
source§

fn try_from(
    t: &'a [u8]
) -> Result<StatusCode, <StatusCode as TryFrom<&'a [u8]>>::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a str> for StatusCode

§

type Error = InvalidStatusCode

The type returned in the event of a conversion error.
source§

fn try_from(
    t: &'a str
) -> Result<StatusCode, <StatusCode as TryFrom<&'a str>>::Error>

Performs the conversion.
source§

impl TryFrom<u16> for StatusCode

§

type Error = InvalidStatusCode

The type returned in the event of a conversion error.
source§

fn try_from(t: u16) -> Result<StatusCode, <StatusCode as TryFrom<u16>>::Error>

Performs the conversion.
source§

impl Copy for StatusCode

source§

impl Eq for StatusCode

source§

impl StructuralEq for StatusCode

source§

impl StructuralPartialEq for StatusCode

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere
    T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere
    V: MultiLane<T>,

§

fn vzip(self) -> V