1 /**
2 	List of all standard HTTP status codes.
3 
4 	Copyright: © 2012 Sönke Ludwig
5 	License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	Authors: Jan Krüger
7 */
8 module vibe.http.status;
9 
10 /**
11 	Definitions of all standard HTTP status codes.
12 */
13 enum HTTPStatus {
14 	continue_                    = 100,
15 	switchingProtocols           = 101,
16 	ok                           = 200,
17 	created                      = 201,
18 	accepted                     = 202,
19 	nonAuthoritativeInformation  = 203,
20 	noContent                    = 204,
21 	resetContent                 = 205,
22 	partialContent               = 206,
23 	multipleChoices              = 300,
24 	movedPermanently             = 301,
25 	found                        = 302,
26 	seeOther                     = 303,
27 	notModified                  = 304,
28 	useProxy                     = 305,
29 	temporaryRedirect            = 307,
30 	badRequest                   = 400,
31 	unauthorized                 = 401,
32 	paymentRequired              = 402,
33 	forbidden                    = 403,
34 	notFound                     = 404,
35 	methodNotAllowed             = 405,
36 	notAcceptable                = 406,
37 	proxyAuthenticationRequired  = 407,
38 	requestTimeout               = 408,
39 	conflict                     = 409,
40 	gone                         = 410,
41 	lengthRequired               = 411,
42 	preconditionFailed           = 412,
43 	requestEntityTooLarge        = 413,
44 	requestURITooLarge           = 414,
45 	unsupportedMediaType         = 415,
46 	rangeNotSatisfiable          = 416,
47 	expectationFailed            = 417,
48 	tooManyRequests              = 429,
49 	unavailableForLegalReasons   = 451,
50 	internalServerError          = 500,
51 	notImplemented               = 501,
52 	badGateway                   = 502,
53 	serviceUnavailable           = 503,
54 	gatewayTimeout               = 504,
55 	httpVersionNotSupported      = 505,
56 	// WebDAV status codes
57 	processing                   = 102, /// See: https://tools.ietf.org/html/rfc2518#section-10.1
58 	multiStatus                  = 207,
59 	unprocessableEntity          = 422,
60 	locked                       = 423,
61 	failedDependency             = 424,
62 	insufficientStorage          = 507,
63 
64 	deprecated("Use `rangeNotSatisfiable` instead") requestedrangenotsatisfiable = rangeNotSatisfiable,
65 	deprecated("Use `continue_` instead") Continue = continue_,
66 	deprecated("Use `switchingProtocols` instead") SwitchingProtocols = switchingProtocols,
67 	deprecated("Use `ok` instead") OK = ok,
68 	deprecated("Use `created` instead") Created = created,
69 	deprecated("Use `accepted` instead") Accepted = accepted,
70 	deprecated("Use `nonAuthoritativeInformation` instead") NonAuthoritativeInformation = nonAuthoritativeInformation,
71 	deprecated("Use `noContent` instead") NoContent = noContent,
72 	deprecated("Use `resetContent` instead") ResetContent = resetContent,
73 	deprecated("Use `partialContent` instead") PartialContent = partialContent,
74 	deprecated("Use `multipleChoices` instead") MultipleChoices = multipleChoices,
75 	deprecated("Use `movedPermanently` instead") MovedPermanently = movedPermanently,
76 	deprecated("Use `found` instead") Found = found,
77 	deprecated("Use `seeOther` instead") SeeOther = seeOther,
78 	deprecated("Use `notModified` instead") NotModified = notModified,
79 	deprecated("Use `useProxy` instead") UseProxy = useProxy,
80 	deprecated("Use `temporaryRedirect` instead") TemporaryRedirect = temporaryRedirect,
81 	deprecated("Use `badRequest` instead") BadRequest = badRequest,
82 	deprecated("Use `unauthorized` instead") Unauthorized = unauthorized,
83 	deprecated("Use `paymentRequired` instead") PaymentRequired = paymentRequired,
84 	deprecated("Use `forbidden` instead") Forbidden = forbidden,
85 	deprecated("Use `notFound` instead") NotFound = notFound,
86 	deprecated("Use `methodNotAllowed` instead") MethodNotAllowed = methodNotAllowed,
87 	deprecated("Use `notAcceptable` instead") NotAcceptable = notAcceptable,
88 	deprecated("Use `proxyAuthenticationRequired` instead") ProxyAuthenticationRequired = proxyAuthenticationRequired,
89 	deprecated("Use `requestTimeout` instead") RequestTimeout = requestTimeout,
90 	deprecated("Use `conflict` instead") Conflict = conflict,
91 	deprecated("Use `gone` instead") Gone = gone,
92 	deprecated("Use `lengthRequired` instead") LengthRequired = lengthRequired,
93 	deprecated("Use `preconditionFailed` instead") PreconditionFailed = preconditionFailed,
94 	deprecated("Use `requestEntityTooLarge` instead") RequestEntityTooLarge = requestEntityTooLarge,
95 	deprecated("Use `requestURITooLarge` instead") RequestURITooLarge = requestURITooLarge,
96 	deprecated("Use `unsupportedMediaType` instead") UnsupportedMediaType = unsupportedMediaType,
97 	deprecated("Use `requestedrangenotsatisfiable` instead") Requestedrangenotsatisfiable = rangeNotSatisfiable,
98 	deprecated("Use `expectationFailed` instead") ExpectationFailed = expectationFailed,
99 	deprecated("Use `internalServerError` instead") InternalServerError = internalServerError,
100 	deprecated("Use `notImplemented` instead") NotImplemented = notImplemented,
101 	deprecated("Use `badGateway` instead") BadGateway = badGateway,
102 	deprecated("Use `serviceUnavailable` instead") ServiceUnavailable = serviceUnavailable,
103 	deprecated("Use `gatewayTimeout` instead") GatewayTimeout = gatewayTimeout,
104 	deprecated("Use `httpVersionNotSupported` instead") HTTPVersionNotSupported = httpVersionNotSupported,
105 }
106 
107 
108 @safe nothrow @nogc pure:
109 
110 /**
111 	Returns a standard text description of the specified HTTP status code.
112 */
113 string httpStatusText(int code)
114 {
115 	switch(code)
116 	{
117 		default: break;
118 		case HTTPStatus.continue_                    : return "Continue";
119 		case HTTPStatus.switchingProtocols           : return "Switching Protocols";
120 		case HTTPStatus.ok                           : return "OK";
121 		case HTTPStatus.created                      : return "Created";
122 		case HTTPStatus.accepted                     : return "Accepted";
123 		case HTTPStatus.nonAuthoritativeInformation  : return "Non-Authoritative Information";
124 		case HTTPStatus.noContent                    : return "No Content";
125 		case HTTPStatus.resetContent                 : return "Reset Content";
126 		case HTTPStatus.partialContent               : return "Partial Content";
127 		case HTTPStatus.multipleChoices              : return "Multiple Choices";
128 		case HTTPStatus.movedPermanently             : return "Moved Permanently";
129 		case HTTPStatus.found                        : return "Found";
130 		case HTTPStatus.seeOther                     : return "See Other";
131 		case HTTPStatus.notModified                  : return "Not Modified";
132 		case HTTPStatus.useProxy                     : return "Use Proxy";
133 		case HTTPStatus.temporaryRedirect            : return "Temporary Redirect";
134 		case HTTPStatus.badRequest                   : return "Bad Request";
135 		case HTTPStatus.unauthorized                 : return "Unauthorized";
136 		case HTTPStatus.paymentRequired              : return "Payment Required";
137 		case HTTPStatus.forbidden                    : return "Forbidden";
138 		case HTTPStatus.notFound                     : return "Not Found";
139 		case HTTPStatus.methodNotAllowed             : return "Method Not Allowed";
140 		case HTTPStatus.notAcceptable                : return "Not Acceptable";
141 		case HTTPStatus.proxyAuthenticationRequired  : return "Proxy Authentication Required";
142 		case HTTPStatus.requestTimeout               : return "Request Time-out";
143 		case HTTPStatus.conflict                     : return "Conflict";
144 		case HTTPStatus.gone                         : return "Gone";
145 		case HTTPStatus.lengthRequired               : return "Length Required";
146 		case HTTPStatus.preconditionFailed           : return "Precondition Failed";
147 		case HTTPStatus.requestEntityTooLarge        : return "Request Entity Too Large";
148 		case HTTPStatus.requestURITooLarge           : return "Request-URI Too Large";
149 		case HTTPStatus.unsupportedMediaType         : return "Unsupported Media Type";
150 		case HTTPStatus.rangeNotSatisfiable          : return "Requested range not satisfiable";
151 		case HTTPStatus.expectationFailed            : return "Expectation Failed";
152 		case HTTPStatus.unavailableForLegalReasons   : return "Unavailable For Legal Reasons";
153 		case HTTPStatus.internalServerError          : return "Internal Server Error";
154 		case HTTPStatus.notImplemented               : return "Not Implemented";
155 		case HTTPStatus.badGateway                   : return "Bad Gateway";
156 		case HTTPStatus.serviceUnavailable           : return "Service Unavailable";
157 		case HTTPStatus.gatewayTimeout               : return "Gateway Time-out";
158 		case HTTPStatus.httpVersionNotSupported      : return "HTTP Version not supported";
159 		// WebDAV
160 		case HTTPStatus.multiStatus                  : return "Multi-Status";
161 		case HTTPStatus.unprocessableEntity          : return "Unprocessable Entity";
162 		case HTTPStatus.locked                       : return "Locked";
163 		case HTTPStatus.failedDependency             : return "Failed Dependency";
164 		case HTTPStatus.insufficientStorage          : return "Insufficient Storage";
165 		case HTTPStatus.processing                   : return "Processing";
166 	}
167 	if( code >= 600 ) return "Unknown";
168 	if( code >= 500 ) return "Unknown server error";
169 	if( code >= 400 ) return "Unknown error";
170 	if( code >= 300 ) return "Unknown redirection";
171 	if( code >= 200 ) return "Unknown success";
172 	if( code >= 100 ) return "Unknown information";
173 	return "Unknown";
174 }
175 
176 /**
177 	Determines if the given status code justifies closing the connection (e.g. evil big request bodies)
178 */
179 bool justifiesConnectionClose(int status)
180 {
181 	switch(status) {
182 		default: return false;
183 		case HTTPStatus.requestEntityTooLarge:
184 		case HTTPStatus.requestURITooLarge:
185 		case HTTPStatus.requestTimeout:
186 			return true;
187 	}
188 }
189 
190 /**
191 	Determines if status code is generally successful (>= 200 && < 300)
192 */
193 bool isSuccessCode(HTTPStatus status)
194 {
195 	return status >= 200 && status < 300;
196 }
197