summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@gmail.com>2016-09-19 20:58:27 +0200
committerDominik Picheta <dominikpicheta@gmail.com>2016-09-19 20:58:27 +0200
commitcfd187d16ea5ff8924c0f907714134d2b77baa4e (patch)
tree991d384b94e21407edc87fa1fa278b2c89fbc6ea
parent1fb5dd247745e06c85a2ed1ad8f8f09f0a38b9b9 (diff)
downloadNim-cfd187d16ea5ff8924c0f907714134d2b77baa4e.tar.gz
Use `distinct range` for HttpCode as suggested by @nigredo-tori.
-rw-r--r--lib/pure/httpclient.nim5
-rw-r--r--lib/pure/httpcore.nim168
-rw-r--r--web/news/version_0_15_released.rst3
3 files changed, 121 insertions, 55 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim
index 117709f95..27b3b46be 100644
--- a/lib/pure/httpclient.nim
+++ b/lib/pure/httpclient.nim
@@ -93,12 +93,13 @@ type
     headers*: HttpHeaders
     body*: string
 
-proc code*(response: Response): HttpCode {.raises: [ValueError].} =
+proc code*(response: Response): HttpCode
+           {.raises: [ValueError, OverflowError].} =
   ## Retrieves the specified response's ``HttpCode``.
   ##
   ## Raises a ``ValueError`` if the response's ``status`` does not have a
   ## corresponding ``HttpCode``.
-  return parseEnum[HttpCode](response.status)
+  return response.status[0 .. 2].parseInt.HttpCode
 
 type
   Proxy* = ref object
diff --git a/lib/pure/httpcore.nim b/lib/pure/httpcore.nim
index 2900d0ce4..33a5357e2 100644
--- a/lib/pure/httpcore.nim
+++ b/lib/pure/httpcore.nim
@@ -18,54 +18,7 @@ type
 
   HttpHeaderValues* = distinct seq[string]
 
-  HttpCode* = enum
-    Http100 = "100 Continue",
-    Http101 = "101 Switching Protocols",
-    Http200 = "200 OK",
-    Http201 = "201 Created",
-    Http202 = "202 Accepted",
-    Http203 = "203 Non-Authoritative Information",
-    Http204 = "204 No Content",
-    Http205 = "205 Reset Content",
-    Http206 = "206 Partial Content",
-    Http300 = "300 Multiple Choices",
-    Http301 = "301 Moved Permanently",
-    Http302 = "302 Found",
-    Http303 = "303 See Other",
-    Http304 = "304 Not Modified",
-    Http305 = "305 Use Proxy",
-    Http307 = "307 Temporary Redirect",
-    Http400 = "400 Bad Request",
-    Http401 = "401 Unauthorized",
-    Http403 = "403 Forbidden",
-    Http404 = "404 Not Found",
-    Http405 = "405 Method Not Allowed",
-    Http406 = "406 Not Acceptable",
-    Http407 = "407 Proxy Authentication Required",
-    Http408 = "408 Request Timeout",
-    Http409 = "409 Conflict",
-    Http410 = "410 Gone",
-    Http411 = "411 Length Required",
-    Http412 = "412 Precondition Failed",
-    Http413 = "413 Request Entity Too Large",
-    Http414 = "414 Request-URI Too Long",
-    Http415 = "415 Unsupported Media Type",
-    Http416 = "416 Requested Range Not Satisfiable",
-    Http417 = "417 Expectation Failed",
-    Http418 = "418 I'm a teapot",
-    Http421 = "421 Misdirected Request",
-    Http422 = "422 Unprocessable Entity",
-    Http426 = "426 Upgrade Required",
-    Http428 = "428 Precondition Required",
-    Http429 = "429 Too Many Requests",
-    Http431 = "431 Request Header Fields Too Large",
-    Http451 = "451 Unavailable For Legal Reasons",
-    Http500 = "500 Internal Server Error",
-    Http501 = "501 Not Implemented",
-    Http502 = "502 Bad Gateway",
-    Http503 = "503 Service Unavailable",
-    Http504 = "504 Gateway Timeout",
-    Http505 = "505 HTTP Version Not Supported"
+  HttpCode* = distinct range[100 .. 599]
 
   HttpVersion* = enum
     HttpVer11,
@@ -93,6 +46,56 @@ type
                httpPut: HttpPut, httpDelete: HttpDelete, httpTrace: HttpTrace,
                httpOptions: HttpOptions, httpConnect: HttpConnect].}
 
+
+const
+  Http100* = HttpCode(100)
+  Http101* = HttpCode(101)
+  Http200* = HttpCode(200)
+  Http201* = HttpCode(201)
+  Http202* = HttpCode(202)
+  Http203* = HttpCode(203)
+  Http204* = HttpCode(204)
+  Http205* = HttpCode(205)
+  Http206* = HttpCode(206)
+  Http300* = HttpCode(300)
+  Http301* = HttpCode(301)
+  Http302* = HttpCode(302)
+  Http303* = HttpCode(303)
+  Http304* = HttpCode(304)
+  Http305* = HttpCode(305)
+  Http307* = HttpCode(307)
+  Http400* = HttpCode(400)
+  Http401* = HttpCode(401)
+  Http403* = HttpCode(403)
+  Http404* = HttpCode(404)
+  Http405* = HttpCode(405)
+  Http406* = HttpCode(406)
+  Http407* = HttpCode(407)
+  Http408* = HttpCode(408)
+  Http409* = HttpCode(409)
+  Http410* = HttpCode(410)
+  Http411* = HttpCode(411)
+  Http412* = HttpCode(412)
+  Http413* = HttpCode(413)
+  Http414* = HttpCode(414)
+  Http415* = HttpCode(415)
+  Http416* = HttpCode(416)
+  Http417* = HttpCode(417)
+  Http418* = HttpCode(418)
+  Http421* = HttpCode(421)
+  Http422* = HttpCode(422)
+  Http426* = HttpCode(426)
+  Http428* = HttpCode(428)
+  Http429* = HttpCode(429)
+  Http431* = HttpCode(431)
+  Http451* = HttpCode(451)
+  Http500* = HttpCode(500)
+  Http501* = HttpCode(501)
+  Http502* = HttpCode(502)
+  Http503* = HttpCode(503)
+  Http504* = HttpCode(504)
+  Http505* = HttpCode(505)
+
 const headerLimit* = 10_000
 
 proc newHttpHeaders*(): HttpHeaders =
@@ -213,24 +216,83 @@ proc `==`*(protocol: tuple[orig: string, major, minor: int],
 proc contains*(methods: set[HttpMethod], x: string): bool =
   return parseEnum[HttpMethod](x) in methods
 
+proc status*(code: HttpCode): string =
+  ## Converts the specified ``HttpCode`` into a HTTP status.
+  ##
+  ## For example:
+  ##
+  ##   .. code-block:: nim
+  ##       doAssert(Http404.status == "404 Not Found")
+  case code.int
+  of 100: "100 Continue"
+  of 101: "101 Switching Protocols"
+  of 200: "200 OK"
+  of 201: "201 Created"
+  of 202: "202 Accepted"
+  of 203: "203 Non-Authoritative Information"
+  of 204: "204 No Content"
+  of 205: "205 Reset Content"
+  of 206: "206 Partial Content"
+  of 300: "300 Multiple Choices"
+  of 301: "301 Moved Permanently"
+  of 302: "302 Found"
+  of 303: "303 See Other"
+  of 304: "304 Not Modified"
+  of 305: "305 Use Proxy"
+  of 307: "307 Temporary Redirect"
+  of 400: "400 Bad Request"
+  of 401: "401 Unauthorized"
+  of 403: "403 Forbidden"
+  of 404: "404 Not Found"
+  of 405: "405 Method Not Allowed"
+  of 406: "406 Not Acceptable"
+  of 407: "407 Proxy Authentication Required"
+  of 408: "408 Request Timeout"
+  of 409: "409 Conflict"
+  of 410: "410 Gone"
+  of 411: "411 Length Required"
+  of 412: "412 Precondition Failed"
+  of 413: "413 Request Entity Too Large"
+  of 414: "414 Request-URI Too Long"
+  of 415: "415 Unsupported Media Type"
+  of 416: "416 Requested Range Not Satisfiable"
+  of 417: "417 Expectation Failed"
+  of 418: "418 I'm a teapot"
+  of 421: "421 Misdirected Request"
+  of 422: "422 Unprocessable Entity"
+  of 426: "426 Upgrade Required"
+  of 428: "428 Precondition Required"
+  of 429: "429 Too Many Requests"
+  of 431: "431 Request Header Fields Too Large"
+  of 451: "451 Unavailable For Legal Reasons"
+  of 500: "500 Internal Server Error"
+  of 501: "501 Not Implemented"
+  of 502: "502 Bad Gateway"
+  of 503: "503 Service Unavailable"
+  of 504: "504 Gateway Timeout"
+  of 505: "505 HTTP Version Not Supported"
+  else: $(int(code))
+
+proc `==`*(a, b: HttpCode): bool {.borrow.}
+
 proc `==`*(rawCode: string, code: HttpCode): bool =
-  return rawCode.toLower() == ($code).toLower()
+  return rawCode.toLower() == code.status.toLower()
 
 proc is2xx*(code: HttpCode): bool =
   ## Determines whether ``code`` is a 2xx HTTP status code.
-  return ($code).startsWith("2")
+  return code.int in {200 .. 299}
 
 proc is3xx*(code: HttpCode): bool =
   ## Determines whether ``code`` is a 3xx HTTP status code.
-  return ($code).startsWith("3")
+  return code.int in {300 .. 399}
 
 proc is4xx*(code: HttpCode): bool =
   ## Determines whether ``code`` is a 4xx HTTP status code.
-  return ($code).startsWith("4")
+  return code.int in {400 .. 499}
 
 proc is5xx*(code: HttpCode): bool =
   ## Determines whether ``code`` is a 5xx HTTP status code.
-  return ($code).startsWith("5")
+  return code.int in {500 .. 599}
 
 when isMainModule:
   var test = newHttpHeaders()
diff --git a/web/news/version_0_15_released.rst b/web/news/version_0_15_released.rst
index dea893ed1..f5217aa02 100644
--- a/web/news/version_0_15_released.rst
+++ b/web/news/version_0_15_released.rst
@@ -65,6 +65,9 @@ that have tuple name:
 
 - ``AsyncHttpClient.headers`` type is now ``HttpHeaders``.
 
+- The `$` operator for ``HttpCode`` no longer exists, use the ``status``
+procedure to get the code's status message.
+
 Library Additions
 -----------------