| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Util.Http; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides common HTTP status code constants and grouped collections for convenience. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// The groups are immutable singletons rather than freshly built arrays, so reading one in a hot path |
| | | 10 | | /// costs nothing and no caller can alter what another caller sees. They list the codes this type declares, |
| | | 11 | | /// not every code the relevant RFCs define. |
| | | 12 | | /// </remarks> |
| | | 13 | | public static class HttpStatusCodes |
| | | 14 | | { |
| | | 15 | | /// <summary>HTTP 200 - OK.</summary> |
| | | 16 | | public const int Ok = 200; |
| | | 17 | | /// <summary>HTTP 201 - Created.</summary> |
| | | 18 | | public const int Created = 201; |
| | | 19 | | /// <summary>HTTP 202 - Accepted.</summary> |
| | | 20 | | public const int Accepted = 202; |
| | | 21 | | /// <summary>HTTP 204 - No Content.</summary> |
| | | 22 | | public const int NoContent = 204; |
| | | 23 | | |
| | | 24 | | /// <summary>HTTP 301 - Moved Permanently.</summary> |
| | | 25 | | public const int MovedPermanently = 301; |
| | | 26 | | /// <summary>HTTP 302 - Found.</summary> |
| | | 27 | | public const int Found = 302; |
| | | 28 | | /// <summary>HTTP 304 - Not Modified.</summary> |
| | | 29 | | public const int NotModified = 304; |
| | | 30 | | /// <summary>HTTP 307 - Temporary Redirect.</summary> |
| | | 31 | | public const int TemporaryRedirect = 307; |
| | | 32 | | /// <summary>HTTP 308 - Permanent Redirect.</summary> |
| | | 33 | | public const int PermanentRedirect = 308; |
| | | 34 | | |
| | | 35 | | /// <summary>HTTP 400 - Bad Request.</summary> |
| | | 36 | | public const int BadRequest = 400; |
| | | 37 | | /// <summary>HTTP 401 - Unauthorized.</summary> |
| | | 38 | | public const int Unauthorized = 401; |
| | | 39 | | /// <summary>HTTP 403 - Forbidden.</summary> |
| | | 40 | | public const int Forbidden = 403; |
| | | 41 | | /// <summary>HTTP 404 - Not Found.</summary> |
| | | 42 | | public const int NotFound = 404; |
| | | 43 | | /// <summary>HTTP 405 - Method Not Allowed.</summary> |
| | | 44 | | public const int MethodNotAllowed = 405; |
| | | 45 | | /// <summary>HTTP 409 - Conflict.</summary> |
| | | 46 | | public const int Conflict = 409; |
| | | 47 | | /// <summary>HTTP 422 - Unprocessable Entity.</summary> |
| | | 48 | | public const int UnprocessableEntity = 422; |
| | | 49 | | /// <summary>HTTP 429 - Too Many Requests.</summary> |
| | | 50 | | public const int TooManyRequests = 429; |
| | | 51 | | |
| | | 52 | | /// <summary>HTTP 500 - Internal Server Error.</summary> |
| | | 53 | | public const int InternalServerError = 500; |
| | | 54 | | /// <summary>HTTP 501 - Not Implemented.</summary> |
| | | 55 | | public const int NotImplemented = 501; |
| | | 56 | | /// <summary>HTTP 502 - Bad Gateway.</summary> |
| | | 57 | | public const int BadGateway = 502; |
| | | 58 | | /// <summary>HTTP 503 - Service Unavailable.</summary> |
| | | 59 | | public const int ServiceUnavailable = 503; |
| | | 60 | | /// <summary>HTTP 504 - Gateway Timeout.</summary> |
| | | 61 | | public const int GatewayTimeout = 504; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Group containing the success status codes (2xx) declared by this type. |
| | | 65 | | /// </summary> |
| | 12 | 66 | | public static ImmutableArray<int> Success { get; } = [Ok, Created, Accepted, NoContent]; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Group containing the redirection status codes (3xx) declared by this type. |
| | | 70 | | /// </summary> |
| | 10 | 71 | | public static ImmutableArray<int> Redirection { get; } = |
| | 1 | 72 | | [MovedPermanently, Found, NotModified, TemporaryRedirect, PermanentRedirect]; |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Group containing the client error status codes (4xx) declared by this type. |
| | | 76 | | /// </summary> |
| | 13 | 77 | | public static ImmutableArray<int> ClientError { get; } = |
| | 1 | 78 | | [BadRequest, Unauthorized, Forbidden, NotFound, MethodNotAllowed, Conflict, UnprocessableEntity, TooManyRequests |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Group containing the server error status codes (5xx) declared by this type. |
| | | 82 | | /// </summary> |
| | 10 | 83 | | public static ImmutableArray<int> ServerError { get; } = |
| | 1 | 84 | | [InternalServerError, NotImplemented, BadGateway, ServiceUnavailable, GatewayTimeout]; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Convenience property containing every status code declared by this type. |
| | | 88 | | /// </summary> |
| | 29 | 89 | | public static ImmutableArray<int> All { get; } = [..Success, ..Redirection, ..ClientError, ..ServerError]; |
| | | 90 | | } |