status.d 6.5 KB

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