pillow_http/response/status_code.rs
1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum StatusCode {
5 Information(Information),
6 Successfull(Successfull),
7 Redirection(Redirection),
8 ClientError(ClientError),
9 ServerError(ServerError),
10}
11
12impl AsStr for StatusCode {
13 fn as_str(&self) -> &'static str {
14 match self {
15 StatusCode::Information(info) => info.as_str(),
16 StatusCode::Successfull(success) => success.as_str(),
17 StatusCode::Redirection(redirect) => redirect.as_str(),
18 StatusCode::ClientError(client) => client.as_str(),
19 StatusCode::ServerError(server) => server.as_str(),
20 }
21 }
22}
23
24impl fmt::Display for StatusCode {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(f, "{}", self)
27 }
28}
29
30pub trait AsStr {
31 fn as_str(&self) -> &'static str {
32 match self {
33 _ => "",
34 }
35 }
36}
37
38/// Information Messages
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub enum Information {
41 /// This interim response indicates that the client should continue the request or ignore the response if the request is already finished.
42 Continue,
43 /// This code is sent in response to an Upgrade request header from the client and indicates the protocol the server is switching to.
44 SwitchingProtocols,
45}
46
47impl AsStr for Information {
48 fn as_str(&self) -> &'static str {
49 match self {
50 Information::Continue => "100 Continue",
51 Information::SwitchingProtocols => "101 Switching Protocols",
52 }
53 }
54}
55
56/// Successfull Messages
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum Successfull {
59 /// The request succeeded. The result meaning of "success" depends on the HTTP method:
60 ///
61 /// - GET: The resource has been fetched and transmitted in the message body.
62 ///
63 /// - HEAD: The representation headers are included in the response without any message body.
64 ///
65 /// - PUT or POST: The resource describing the result of the action is transmitted in the message body.
66 ///
67 /// - TRACE: The message body contains the request message as received by the server.
68 OK,
69 /// The request succeeded, and a new resource was created as a result.
70 /// This is typically the response sent after POST requests, or some PUT requests.
71 Created,
72 /// The request has been received but not yet acted upon.
73 /// It is noncommittal, since there is no way in HTTP to later send an asynchronous response indicating the outcome of the request.
74 /// It is intended for cases where another process or server handles the request, or for batch processing.
75 Accepted,
76 /// This response code is used when the Range header is sent from the client to request only part of a resource.
77 PartialContent,
78}
79
80impl AsStr for Successfull {
81 fn as_str(&self) -> &'static str {
82 match self {
83 Successfull::OK => "200 OK",
84 Successfull::Created => "201 Created",
85 Successfull::Accepted => "202 Accepted",
86 Successfull::PartialContent => "206 Partial Content",
87 }
88 }
89}
90
91/// Redirect
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub enum Redirection {
94 /// The request has more than one possible response.
95 /// The user agent or user should choose one of them.
96 /// (There is no standardized way of choosing one of the responses, but HTML links to the possibilities are recommended so the user can pick.)
97 MultipleChoices,
98 /// The URL of the requested resource has been changed permanently. The new URL is given in the response.
99 MovedPermantely,
100 /// This response code means that the URI of requested resource has been changed temporarily.
101 /// Further changes in the URI might be made in the future.
102 /// Therefore, this same URI should be used by the client in future requests.
103 Found,
104 /// The server sent this response to direct the client to get the requested resource at another URI with a GET request.
105 SeeOther,
106 /// This is used for caching purposes.
107 /// It tells the client that the response has not been modified, so the client can continue to use the same cached version of the response.
108 NotModified,
109 /// The server sends this response to direct the client to get the requested resource at another URI with same method that was used in the prior request.
110 /// This has the same semantics as the 302 Found HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request.
111 TemporalyRedirect,
112 /// This means that the resource is now permanently located at another URI, specified by the Location: HTTP Response header.
113 /// This has the same semantics as the 301 Moved Permanently HTTP response code, with the exception that the user agent must not change the HTTP method used:
114 /// if a POST was used in the first request, a POST must be used in the second request.
115 PermanetRedirect,
116}
117
118impl AsStr for Redirection {
119 fn as_str(&self) -> &'static str {
120 match self {
121 Redirection::MultipleChoices => "300 Multiple Choices",
122 Redirection::MovedPermantely => "301 Moved Permantely",
123 Redirection::Found => "302 Found",
124 Redirection::SeeOther => "303 See Other",
125 Redirection::NotModified => "304 Not Modified",
126 Redirection::TemporalyRedirect => "307 Temporaly Redirect",
127 Redirection::PermanetRedirect => "308 Permanet Redirect",
128 }
129 }
130}
131
132/// Client Error Messages
133#[derive(Debug, Clone, PartialEq, Eq)]
134pub enum ClientError {
135 /// The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
136 BadRequest,
137 /// Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated".
138 /// That is, the client must authenticate itself to get the requested response.
139 Unautorized,
140 /// The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource.
141 /// Unlike 401 Unauthorized, the client's identity is known to the server.
142 Forbiden,
143 /// The server cannot find the requested resource.
144 /// In the browser, this means the URL is not recognized.
145 /// In an API, this can also mean that the endpoint is valid but the resource itself does not exist.
146 /// Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client.
147 /// This response code is probably the most well known due to its frequent occurrence on the web.
148 NotFound,
149 /// The request method is known by the server but is not supported by the target resource.
150 /// For example, an API may not allow calling DELETE to remove a resource.
151 MethodNotAllowed,
152 /// This response is sent when the web server, after performing server-driven content negotiation, doesn't find any content that conforms to the criteria given by the user agent.
153 NotAcceptable,
154 /// This response is sent on an idle connection by some servers, even without any previous request by the client.
155 /// It means that the server would like to shut down this unused connection.
156 /// This response is used much more since some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing.
157 /// Also note that some servers merely shut down the connection without sending this message.
158 RequestTimeout,
159}
160
161impl AsStr for ClientError {
162 fn as_str(&self) -> &'static str {
163 match self {
164 ClientError::BadRequest => "400 Bad Request",
165 ClientError::Unautorized => "401 Unautorized",
166 ClientError::Forbiden => "403 Forbiden",
167 ClientError::NotFound => "404 Not Found",
168 ClientError::MethodNotAllowed => "405 Method Not Allowed",
169 ClientError::NotAcceptable => "406 Not Acceptable",
170 ClientError::RequestTimeout => "408 Request Timeout",
171 }
172 }
173}
174
175/// Server Error Messages
176#[derive(Debug, Clone, PartialEq, Eq)]
177pub enum ServerError {
178 /// The server has encountered a situation it does not know how to handle.
179 InternalServerError,
180 /// The request method is not supported by the server and cannot be handled.
181 /// The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD.
182 NotImplemented,
183 /// This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response.
184 BadGateway,
185 /// This error response is given when the server is acting as a gateway and cannot get a response in time.
186 GatewayTimeout,
187}
188
189impl AsStr for ServerError {
190 fn as_str(&self) -> &'static str {
191 match self {
192 ServerError::InternalServerError => "500 Internal Server Error",
193 ServerError::NotImplemented => "501 Not Implemented",
194 ServerError::BadGateway => "502 Bad Gateway",
195 ServerError::GatewayTimeout => "504 Gateway Timeout",
196 }
197 }
198}