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 
65 
66 @safe nothrow @nogc pure:
67 
68 /**
69 	Returns a standard text description of the specified HTTP status code.
70 */
71 string httpStatusText(int code)
72 {
73 	switch(code)
74 	{
75 		default: break;
76 		case HTTPStatus.continue_                    : return "Continue";
77 		case HTTPStatus.switchingProtocols           : return "Switching Protocols";
78 		case HTTPStatus.ok                           : return "OK";
79 		case HTTPStatus.created                      : return "Created";
80 		case HTTPStatus.accepted                     : return "Accepted";
81 		case HTTPStatus.nonAuthoritativeInformation  : return "Non-Authoritative Information";
82 		case HTTPStatus.noContent                    : return "No Content";
83 		case HTTPStatus.resetContent                 : return "Reset Content";
84 		case HTTPStatus.partialContent               : return "Partial Content";
85 		case HTTPStatus.multipleChoices              : return "Multiple Choices";
86 		case HTTPStatus.movedPermanently             : return "Moved Permanently";
87 		case HTTPStatus.found                        : return "Found";
88 		case HTTPStatus.seeOther                     : return "See Other";
89 		case HTTPStatus.notModified                  : return "Not Modified";
90 		case HTTPStatus.useProxy                     : return "Use Proxy";
91 		case HTTPStatus.temporaryRedirect            : return "Temporary Redirect";
92 		case HTTPStatus.badRequest                   : return "Bad Request";
93 		case HTTPStatus.unauthorized                 : return "Unauthorized";
94 		case HTTPStatus.paymentRequired              : return "Payment Required";
95 		case HTTPStatus.forbidden                    : return "Forbidden";
96 		case HTTPStatus.notFound                     : return "Not Found";
97 		case HTTPStatus.methodNotAllowed             : return "Method Not Allowed";
98 		case HTTPStatus.notAcceptable                : return "Not Acceptable";
99 		case HTTPStatus.proxyAuthenticationRequired  : return "Proxy Authentication Required";
100 		case HTTPStatus.requestTimeout               : return "Request Time-out";
101 		case HTTPStatus.conflict                     : return "Conflict";
102 		case HTTPStatus.gone                         : return "Gone";
103 		case HTTPStatus.lengthRequired               : return "Length Required";
104 		case HTTPStatus.preconditionFailed           : return "Precondition Failed";
105 		case HTTPStatus.requestEntityTooLarge        : return "Request Entity Too Large";
106 		case HTTPStatus.requestURITooLarge           : return "Request-URI Too Large";
107 		case HTTPStatus.unsupportedMediaType         : return "Unsupported Media Type";
108 		case HTTPStatus.rangeNotSatisfiable          : return "Requested range not satisfiable";
109 		case HTTPStatus.expectationFailed            : return "Expectation Failed";
110 		case HTTPStatus.unavailableForLegalReasons   : return "Unavailable For Legal Reasons";
111 		case HTTPStatus.internalServerError          : return "Internal Server Error";
112 		case HTTPStatus.notImplemented               : return "Not Implemented";
113 		case HTTPStatus.badGateway                   : return "Bad Gateway";
114 		case HTTPStatus.serviceUnavailable           : return "Service Unavailable";
115 		case HTTPStatus.gatewayTimeout               : return "Gateway Time-out";
116 		case HTTPStatus.httpVersionNotSupported      : return "HTTP Version not supported";
117 		// WebDAV
118 		case HTTPStatus.multiStatus                  : return "Multi-Status";
119 		case HTTPStatus.unprocessableEntity          : return "Unprocessable Entity";
120 		case HTTPStatus.locked                       : return "Locked";
121 		case HTTPStatus.failedDependency             : return "Failed Dependency";
122 		case HTTPStatus.insufficientStorage          : return "Insufficient Storage";
123 		case HTTPStatus.processing                   : return "Processing";
124 	}
125 	if( code >= 600 ) return "Unknown";
126 	if( code >= 500 ) return "Unknown server error";
127 	if( code >= 400 ) return "Unknown error";
128 	if( code >= 300 ) return "Unknown redirection";
129 	if( code >= 200 ) return "Unknown success";
130 	if( code >= 100 ) return "Unknown information";
131 	return "Unknown";
132 }
133 
134 /**
135 	Determines if the given status code justifies closing the connection (e.g. evil big request bodies)
136 */
137 bool justifiesConnectionClose(int status)
138 {
139 	switch(status) {
140 		default: return false;
141 		case HTTPStatus.requestEntityTooLarge:
142 		case HTTPStatus.requestURITooLarge:
143 		case HTTPStatus.requestTimeout:
144 			return true;
145 	}
146 }
147 
148 /**
149 	Determines if status code is generally successful (>= 200 && < 300)
150 */
151 bool isSuccessCode(HTTPStatus status)
152 {
153 	return status >= 200 && status < 300;
154 }
155