1 /*
2  * Archttp - A highly performant web framework written in D.
3  *
4  * Copyright (C) 2021-2022 Kerisy.com
5  *
6  * Website: https://www.kerisy.com
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module archttp.HttpStatusCode;
13 
14 enum HttpStatusCode : ushort {
15     // 1XX - Informational
16     CONTINUE            = 100,
17     SWITCHING_PROTOCOLS = 101,
18     PROCESSING          = 102,
19     
20     // 2XX - Success
21     OK                   = 200,
22     CREATED              = 201,
23     ACCEPTED             = 202,
24     NON_AUTHORITIVE_INFO = 203,
25     NO_CONTENT           = 204,
26     RESET_CONTENT        = 205,
27     PARTIAL_CONTENT      = 206,
28     MULTI_STATUS         = 207,
29     ALREADY_REPORTED     = 208,
30     IM_USED              = 226,
31     
32     // 3XX - Redirectional
33     MULTI_CHOICES     = 300,
34     MOVED_PERMANENTLY = 301,
35     FOUND             = 302,
36     SEE_OTHER         = 303,
37     NOT_MODIFIED      = 304,
38     USE_PROXY         = 305,
39     SWITCH_PROXY      = 306,
40     TEMP_REDIRECT     = 307,
41     PERM_REDIRECT     = 308,
42     
43     // 4XX - Client error
44     BAD_REQUEST                 = 400,
45     UNAUTHORIZED                = 401,
46     PAYMENT_REQUIRED            = 402,
47     FORBIDDEN                   = 403,
48     NOT_FOUND                   = 404,
49     METHOD_NOT_ALLOWED          = 405,
50     NOT_ACCEPTABLE              = 406,
51     PROXY_AUTH_REQUIRED         = 407,
52     REQUEST_TIMEOUT             = 408,
53     CONFLICT                    = 409,
54     GONE                        = 410,
55     LENGTH_REQUIRED             = 411,
56     PRECONDITION_FAILED         = 412,
57     REQ_ENTITY_TOO_LARGE        = 413,
58     REQ_URI_TOO_LONG            = 414,
59     UNSUPPORTED_MEDIA_TYPE      = 415,
60     REQ_RANGE_NOT_SATISFYABLE   = 416,
61     EXPECTATION_FAILED          = 417,
62     IM_A_TEAPOT                 = 418,
63     AUTH_TIMEOUT                = 419, // not in RFC 2616
64     UNPROCESSABLE_ENTITY        = 422,
65     LOCKED                      = 423,
66     FAILED_DEPENDENCY           = 424,
67     UPGRADE_REQUIRED            = 426,
68     PRECONDITION_REQUIRED       = 428,
69     TOO_MANY_REQUESTS           = 429,
70     REQ_HEADER_FIELDS_TOO_LARGE = 431,
71     
72     // 5XX - Server error
73     INTERNAL_SERVER_ERROR       = 500,
74     NOT_IMPLEMENTED             = 501,
75     BAD_GATEWAY                 = 502,
76     SERVICE_UNAVAILABLE         = 503,
77     GATEWAY_TIMEOUT             = 504,
78     HTTP_VERSION_NOT_SUPPORTED  = 505,
79     VARIANT_ALSO_NEGOTIATES     = 506,
80     INSUFFICIENT_STORAGE        = 507,
81     LOOP_DETECTED               = 508,
82     NOT_EXTENDED                = 510,
83     NETWORK_AUTH_REQUIRED       = 511,
84     NETWORK_READ_TIMEOUT_ERR    = 598,
85     NETWORK_CONNECT_TIMEOUT_ERR = 599,
86 }
87 
88 /*
89  * Converts an HTTP status code into a known reason string.
90  *
91  * The reason string is a small line of text that gives a hint as to the underlying meaning of the
92  * status code for debugging purposes.
93  */
94 string getHttpStatusMessage(ushort status_code)
95 {
96     switch ( status_code )
97     {
98     // 1XX - Informational
99     case HttpStatusCode.CONTINUE:
100         return "CONTINUE";
101     case HttpStatusCode.SWITCHING_PROTOCOLS:
102         return "SWITCHING PROTOCOLS";
103     case HttpStatusCode.PROCESSING:
104         return "PROCESSING";
105     // 2XX - Success
106     case HttpStatusCode.OK:
107         return "OK";
108     case HttpStatusCode.CREATED:
109         return "CREATED";
110     case HttpStatusCode.ACCEPTED:
111         return "ACCEPTED";
112     case HttpStatusCode.NON_AUTHORITIVE_INFO:
113         return "NON AUTHORITIVE INFO";
114     case HttpStatusCode.NO_CONTENT:
115         return "NO CONTENT";
116     case HttpStatusCode.RESET_CONTENT:
117         return "RESET CONTENT";
118     case HttpStatusCode.PARTIAL_CONTENT:
119         return "PARTIAL CONTENT";
120     case HttpStatusCode.MULTI_STATUS:
121         return "MULTI STATUS";
122     case HttpStatusCode.ALREADY_REPORTED:
123         return "ALREADY REPORTED";
124     case HttpStatusCode.IM_USED:
125         return "IM USED";
126     // 3XX - Redirectional
127     case HttpStatusCode.MULTI_CHOICES:
128         return "MULTI CHOICES";
129     case HttpStatusCode.MOVED_PERMANENTLY:
130         return "MOVED PERMANENTLY";
131     case HttpStatusCode.FOUND:
132         return "FOUND";
133     case HttpStatusCode.SEE_OTHER:
134         return "SEE OTHER";
135     case HttpStatusCode.NOT_MODIFIED:
136         return "NOT MODIFIED";
137     case HttpStatusCode.USE_PROXY:
138         return "USE PROXY";
139     case HttpStatusCode.SWITCH_PROXY:
140         return "SWITCH PROXY";
141     case HttpStatusCode.TEMP_REDIRECT:
142         return "TEMP REDIRECT";
143     case HttpStatusCode.PERM_REDIRECT:
144         return "PERM REDIRECT";
145     // 4XX - Client error
146     case HttpStatusCode.BAD_REQUEST:
147         return "BAD REQUEST";
148     case HttpStatusCode.UNAUTHORIZED:
149         return "UNAUTHORIZED";
150     case HttpStatusCode.PAYMENT_REQUIRED:
151         return "PAYMENT REQUIRED";
152     case HttpStatusCode.FORBIDDEN:
153         return "FORBIDDEN";
154     case HttpStatusCode.NOT_FOUND:
155         return "NOT FOUND";
156     case HttpStatusCode.METHOD_NOT_ALLOWED:
157         return "METHOD NOT ALLOWED";
158     case HttpStatusCode.NOT_ACCEPTABLE:
159         return "NOT ACCEPTABLE";
160     case HttpStatusCode.PROXY_AUTH_REQUIRED:
161         return "PROXY AUTH REQUIRED";
162     case HttpStatusCode.REQUEST_TIMEOUT:
163         return "REQUEST TIMEOUT";
164     case HttpStatusCode.CONFLICT:
165         return "CONFLICT";
166     case HttpStatusCode.GONE:
167         return "GONE";
168     case HttpStatusCode.LENGTH_REQUIRED:
169         return "LENGTH REQUIRED";
170     case HttpStatusCode.PRECONDITION_FAILED:
171         return "PRECONDITION FAILED";
172     case HttpStatusCode.REQ_ENTITY_TOO_LARGE:
173         return "REQ ENTITY TOO LARGE";
174     case HttpStatusCode.REQ_URI_TOO_LONG:
175         return "REQ URI TOO LONG";
176     case HttpStatusCode.UNSUPPORTED_MEDIA_TYPE:
177         return "UNSUPPORTED MEDIA TYPE";
178     case HttpStatusCode.REQ_RANGE_NOT_SATISFYABLE:
179         return "REQ RANGE NOT SATISFYABLE";
180     case HttpStatusCode.EXPECTATION_FAILED:
181         return "EXPECTATION FAILED";
182     case HttpStatusCode.IM_A_TEAPOT:
183         return "IM A TEAPOT";
184     case HttpStatusCode.AUTH_TIMEOUT: // not in RFC 2616
185         return "AUTH TIMEOUT";
186     case HttpStatusCode.UNPROCESSABLE_ENTITY:
187         return "UNPROCESSABLE ENTITY";
188     case HttpStatusCode.LOCKED:
189         return "LOCKED";
190     case HttpStatusCode.FAILED_DEPENDENCY:
191         return "FAILED DEPENDENCY";
192     case HttpStatusCode.UPGRADE_REQUIRED:
193         return "UPGRADE REQUIRED";
194     case HttpStatusCode.PRECONDITION_REQUIRED:
195         return "PRECONDITION REQUIRED";
196     case HttpStatusCode.TOO_MANY_REQUESTS:
197         return "TOO MANY REQUESTS";
198     case HttpStatusCode.REQ_HEADER_FIELDS_TOO_LARGE:
199         return "REQ HEADER FIELDS TOO LARGE";
200     // 5XX - Server error
201     case HttpStatusCode.INTERNAL_SERVER_ERROR:
202         return "INTERNAL SERVER ERROR";
203     case HttpStatusCode.NOT_IMPLEMENTED:
204         return "NOT IMPLEMENTED";
205     case HttpStatusCode.BAD_GATEWAY:
206         return "BAD GATEWAY";
207     case HttpStatusCode.SERVICE_UNAVAILABLE:
208         return "SERVICE UNAVAILABLE";
209     case HttpStatusCode.GATEWAY_TIMEOUT:
210         return "GATEWAY TIMEOUT";
211     case HttpStatusCode.HTTP_VERSION_NOT_SUPPORTED:
212         return "HTTP VERSION NOT SUPPORTED";
213     case HttpStatusCode.VARIANT_ALSO_NEGOTIATES:
214         return "VARIANT ALSO NEGOTIATES";
215     case HttpStatusCode.INSUFFICIENT_STORAGE:
216         return "INSUFFICIENT STORAGE";
217     case HttpStatusCode.LOOP_DETECTED:
218         return "LOOP DETECTED";
219     case HttpStatusCode.NOT_EXTENDED:
220         return "NOT EXTENDED";
221     case HttpStatusCode.NETWORK_AUTH_REQUIRED:
222         return "NETWORK AUTH REQUIRED";
223     case HttpStatusCode.NETWORK_READ_TIMEOUT_ERR:
224         return "NETWORK READ TIMEOUT ERR";
225     case HttpStatusCode.NETWORK_CONNECT_TIMEOUT_ERR:
226         return "NETWORK CONNECT TIMEOUT ERR";
227     default:
228         return "- UNKNOW STATUS CODE";
229     }
230 }