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