actix_web/response/
http_codes.rs

1//! Status code based HTTP response builders.
2
3use actix_http::StatusCode;
4
5use crate::{HttpResponse, HttpResponseBuilder};
6
7macro_rules! static_resp {
8    ($name:ident, $status:expr) => {
9        #[allow(non_snake_case)]
10        #[doc = concat!("Creates a new response builder with the status code `", stringify!($status), "`.")]
11        pub fn $name() -> HttpResponseBuilder {
12            HttpResponseBuilder::new($status)
13        }
14    };
15}
16
17impl HttpResponse {
18    static_resp!(Continue, StatusCode::CONTINUE);
19    static_resp!(SwitchingProtocols, StatusCode::SWITCHING_PROTOCOLS);
20    static_resp!(Processing, StatusCode::PROCESSING);
21
22    static_resp!(Ok, StatusCode::OK);
23    static_resp!(Created, StatusCode::CREATED);
24    static_resp!(Accepted, StatusCode::ACCEPTED);
25    static_resp!(
26        NonAuthoritativeInformation,
27        StatusCode::NON_AUTHORITATIVE_INFORMATION
28    );
29    static_resp!(NoContent, StatusCode::NO_CONTENT);
30    static_resp!(ResetContent, StatusCode::RESET_CONTENT);
31    static_resp!(PartialContent, StatusCode::PARTIAL_CONTENT);
32    static_resp!(MultiStatus, StatusCode::MULTI_STATUS);
33    static_resp!(AlreadyReported, StatusCode::ALREADY_REPORTED);
34    static_resp!(ImUsed, StatusCode::IM_USED);
35
36    static_resp!(MultipleChoices, StatusCode::MULTIPLE_CHOICES);
37    static_resp!(MovedPermanently, StatusCode::MOVED_PERMANENTLY);
38    static_resp!(Found, StatusCode::FOUND);
39    static_resp!(SeeOther, StatusCode::SEE_OTHER);
40    static_resp!(NotModified, StatusCode::NOT_MODIFIED);
41    static_resp!(UseProxy, StatusCode::USE_PROXY);
42    static_resp!(TemporaryRedirect, StatusCode::TEMPORARY_REDIRECT);
43    static_resp!(PermanentRedirect, StatusCode::PERMANENT_REDIRECT);
44
45    static_resp!(BadRequest, StatusCode::BAD_REQUEST);
46    static_resp!(Unauthorized, StatusCode::UNAUTHORIZED);
47    static_resp!(PaymentRequired, StatusCode::PAYMENT_REQUIRED);
48    static_resp!(Forbidden, StatusCode::FORBIDDEN);
49    static_resp!(NotFound, StatusCode::NOT_FOUND);
50    static_resp!(MethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED);
51    static_resp!(NotAcceptable, StatusCode::NOT_ACCEPTABLE);
52    static_resp!(
53        ProxyAuthenticationRequired,
54        StatusCode::PROXY_AUTHENTICATION_REQUIRED
55    );
56    static_resp!(RequestTimeout, StatusCode::REQUEST_TIMEOUT);
57    static_resp!(Conflict, StatusCode::CONFLICT);
58    static_resp!(Gone, StatusCode::GONE);
59    static_resp!(LengthRequired, StatusCode::LENGTH_REQUIRED);
60    static_resp!(PreconditionFailed, StatusCode::PRECONDITION_FAILED);
61    static_resp!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE);
62    static_resp!(UriTooLong, StatusCode::URI_TOO_LONG);
63    static_resp!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE);
64    static_resp!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
65    static_resp!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
66    static_resp!(ImATeapot, StatusCode::IM_A_TEAPOT);
67    static_resp!(MisdirectedRequest, StatusCode::MISDIRECTED_REQUEST);
68    static_resp!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY);
69    static_resp!(Locked, StatusCode::LOCKED);
70    static_resp!(FailedDependency, StatusCode::FAILED_DEPENDENCY);
71    static_resp!(UpgradeRequired, StatusCode::UPGRADE_REQUIRED);
72    static_resp!(PreconditionRequired, StatusCode::PRECONDITION_REQUIRED);
73    static_resp!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS);
74    static_resp!(
75        RequestHeaderFieldsTooLarge,
76        StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE
77    );
78    static_resp!(
79        UnavailableForLegalReasons,
80        StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS
81    );
82
83    static_resp!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
84    static_resp!(NotImplemented, StatusCode::NOT_IMPLEMENTED);
85    static_resp!(BadGateway, StatusCode::BAD_GATEWAY);
86    static_resp!(ServiceUnavailable, StatusCode::SERVICE_UNAVAILABLE);
87    static_resp!(GatewayTimeout, StatusCode::GATEWAY_TIMEOUT);
88    static_resp!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED);
89    static_resp!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES);
90    static_resp!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE);
91    static_resp!(LoopDetected, StatusCode::LOOP_DETECTED);
92    static_resp!(NotExtended, StatusCode::NOT_EXTENDED);
93    static_resp!(
94        NetworkAuthenticationRequired,
95        StatusCode::NETWORK_AUTHENTICATION_REQUIRED
96    );
97}
98
99#[cfg(test)]
100mod tests {
101    use crate::{http::StatusCode, HttpResponse};
102
103    #[test]
104    fn test_build() {
105        let resp = HttpResponse::Ok().finish();
106        assert_eq!(resp.status(), StatusCode::OK);
107    }
108}