Add missing HTTP codes (#16454)

This commit is contained in:
Juan Carlos
2020-12-23 17:26:37 -03:00
committed by GitHub
parent b1c3dab208
commit a29bbeee41
2 changed files with 56 additions and 3 deletions

View File

@@ -74,6 +74,8 @@
- Added `sequtils` import to `prelude`.
- Added `euclDiv` and `euclMod` to `math`.
- Added `httpcore.is1xx` and missing HTTP codes.
## Language changes

View File

@@ -11,7 +11,7 @@
## ``asynchttpserver`` modules.
##
## Unstable API.
import std/private/since
import tables, strutils, parseutils
type
@@ -52,6 +52,8 @@ type
const
Http100* = HttpCode(100)
Http101* = HttpCode(101)
Http102* = HttpCode(102) ## https://tools.ietf.org/html/rfc2518.html WebDAV
Http103* = HttpCode(103) ## https://tools.ietf.org/html/rfc8297.html Early hints
Http200* = HttpCode(200)
Http201* = HttpCode(201)
Http202* = HttpCode(202)
@@ -59,6 +61,9 @@ const
Http204* = HttpCode(204)
Http205* = HttpCode(205)
Http206* = HttpCode(206)
Http207* = HttpCode(207) ## https://tools.ietf.org/html/rfc4918.html WebDAV
Http208* = HttpCode(208) ## https://tools.ietf.org/html/rfc5842.html WebDAV, Section 7.1
Http226* = HttpCode(226) ## https://tools.ietf.org/html/rfc3229.html Delta encoding, Section 10.4.1
Http300* = HttpCode(300)
Http301* = HttpCode(301)
Http302* = HttpCode(302)
@@ -69,6 +74,7 @@ const
Http308* = HttpCode(308)
Http400* = HttpCode(400)
Http401* = HttpCode(401)
Http402* = HttpCode(402) ## https://tools.ietf.org/html/rfc7231.html Payment required, Section 6.5.2
Http403* = HttpCode(403)
Http404* = HttpCode(404)
Http405* = HttpCode(405)
@@ -87,6 +93,9 @@ const
Http418* = HttpCode(418)
Http421* = HttpCode(421)
Http422* = HttpCode(422)
Http423* = HttpCode(423) ## https://tools.ietf.org/html/rfc4918.html WebDAV, Section 11.3
Http424* = HttpCode(424) ## https://tools.ietf.org/html/rfc4918.html WebDAV, Section 11.3
Http425* = HttpCode(425) ## https://tools.ietf.org/html/rfc8470.html Early data
Http426* = HttpCode(426)
Http428* = HttpCode(428)
Http429* = HttpCode(429)
@@ -98,6 +107,12 @@ const
Http503* = HttpCode(503)
Http504* = HttpCode(504)
Http505* = HttpCode(505)
Http506* = HttpCode(506) ## https://tools.ietf.org/html/rfc2295.html Content negotiation, Section 8.1
Http507* = HttpCode(507) ## https://tools.ietf.org/html/rfc4918.html WebDAV, Section 11.5
Http508* = HttpCode(508) ## https://tools.ietf.org/html/rfc5842.html WebDAV, Section 7.2
Http510* = HttpCode(510) ## https://tools.ietf.org/html/rfc2774.html Extension framework, Section 7
Http511* = HttpCode(511) ## https://tools.ietf.org/html/rfc6585.html Additional status code, Section 6
const httpNewLine* = "\c\L"
const headerLimit* = 10_000
@@ -267,6 +282,8 @@ func `$`*(code: HttpCode): string =
case code.int
of 100: "100 Continue"
of 101: "101 Switching Protocols"
of 102: "102 Processing"
of 103: "103 Early Hints"
of 200: "200 OK"
of 201: "201 Created"
of 202: "202 Accepted"
@@ -274,6 +291,9 @@ func `$`*(code: HttpCode): string =
of 204: "204 No Content"
of 205: "205 Reset Content"
of 206: "206 Partial Content"
of 207: "207 Multi-Status"
of 208: "208 Already Reported"
of 226: "226 IM Used"
of 300: "300 Multiple Choices"
of 301: "301 Moved Permanently"
of 302: "302 Found"
@@ -284,6 +304,7 @@ func `$`*(code: HttpCode): string =
of 308: "308 Permanent Redirect"
of 400: "400 Bad Request"
of 401: "401 Unauthorized"
of 402: "402 Payment Required"
of 403: "403 Forbidden"
of 404: "404 Not Found"
of 405: "405 Method Not Allowed"
@@ -302,6 +323,9 @@ func `$`*(code: HttpCode): string =
of 418: "418 I'm a teapot"
of 421: "421 Misdirected Request"
of 422: "422 Unprocessable Entity"
of 423: "423 Locked"
of 424: "424 Failed Dependency"
of 425: "425 Too Early"
of 426: "426 Upgrade Required"
of 428: "428 Precondition Required"
of 429: "429 Too Many Requests"
@@ -313,6 +337,11 @@ func `$`*(code: HttpCode): string =
of 503: "503 Service Unavailable"
of 504: "504 Gateway Timeout"
of 505: "505 HTTP Version Not Supported"
of 506: "506 Variant Also Negotiates"
of 507: "507 Insufficient Storage"
of 508: "508 Loop Detected"
of 510: "510 Not Extended"
of 511: "511 Network Authentication Required"
else: $(int(code))
func `==`*(a, b: HttpCode): bool {.borrow.}
@@ -327,6 +356,13 @@ proc `==`*(rawCode: string, code: HttpCode): bool
## string form of itself.
return cmpIgnoreCase(rawCode, $code) == 0
func is1xx*(code: HttpCode): bool {.inline, since: (1, 5).} =
## Determines whether ``code`` is a 1xx HTTP status code.
runnableExamples:
doAssert is1xx(HttpCode(103))
code.int in {100 .. 199}
func is2xx*(code: HttpCode): bool {.inline.} =
## Determines whether ``code`` is a 2xx HTTP status code.
code.int in {200 .. 299}
@@ -343,5 +379,20 @@ func is5xx*(code: HttpCode): bool {.inline.} =
## Determines whether ``code`` is a 5xx HTTP status code.
code.int in {500 .. 599}
func `$`*(httpMethod: HttpMethod): string =
return (system.`$`(httpMethod))[4 .. ^1].toUpperAscii()
func `$`*(httpMethod: HttpMethod): string {.inline.} =
runnableExamples:
doAssert $HttpHead == "HEAD"
doAssert $HttpPatch == "PATCH"
doAssert $HttpGet == "GET"
doAssert $HttpPost == "POST"
result = case httpMethod
of HttpHead: "HEAD"
of HttpGet: "GET"
of HttpPost: "POST"
of HttpPut: "PUT"
of HttpDelete: "DELETE"
of HttpTrace: "TRACE"
of HttpOptions: "OPTIONS"
of HttpConnect: "CONNECT"
of HttpPatch: "PATCH"