asynchttpserver's Response object's req method uses HttpMethod now.

Fixes #4423.
This commit is contained in:
Dominik Picheta
2016-09-25 00:39:57 +02:00
parent 6a83bc1ff5
commit 26c7a76c8a
4 changed files with 25 additions and 15 deletions

View File

@@ -38,7 +38,7 @@ export httpcore except parseHeader
type
Request* = object
client*: AsyncSocket # TODO: Separate this into a Response object?
reqMethod*: string
reqMethod*: HttpMethod
headers*: HttpHeaders
protocol*: tuple[orig: string, major, minor: int]
url*: Uri
@@ -127,7 +127,14 @@ proc processClient(client: AsyncSocket, address: string,
var i = 0
for linePart in lineFut.mget.split(' '):
case i
of 0: request.reqMethod.shallowCopy(linePart.normalize)
of 0:
try:
# TODO: this is likely slow.
request.reqMethod = parseEnum[HttpMethod]("http" & linePart)
except ValueError:
asyncCheck request.respond(Http400, "Invalid request method. Got: " &
linePart)
continue
of 1: parseUri(linePart, request.url)
of 2:
try:
@@ -159,7 +166,7 @@ proc processClient(client: AsyncSocket, address: string,
request.client.close()
return
if request.reqMethod == "post":
if request.reqMethod == HttpPost:
# Check for Expect header
if request.headers.hasKey("Expect"):
if "100-continue" in request.headers["Expect"]:
@@ -178,17 +185,12 @@ proc processClient(client: AsyncSocket, address: string,
else:
request.body = await client.recv(contentLength)
assert request.body.len == contentLength
elif request.reqMethod == "post":
elif request.reqMethod == HttpPost:
await request.respond(Http400, "Bad Request. No Content-Length.")
continue
case request.reqMethod
of "get", "post", "head", "put", "delete", "trace", "options",
"connect", "patch":
await callback(request)
else:
await request.respond(Http400, "Invalid request method. Got: " &
request.reqMethod)
# Call the user's callback.
await callback(request)
if "upgrade" in request.headers.getOrDefault("connection"):
return

View File

@@ -413,7 +413,7 @@ proc request*(url: string, httpMethod: string, extraHeaders = "",
## **Deprecated since version 0.15.0**: use ``HttpClient.request`` instead.
var r = if proxy == nil: parseUri(url) else: proxy.url
var hostUrl = if proxy == nil: r else: parseUri(url)
var headers = substr(httpMethod, len("http")).toUpper()
var headers = httpMethod.toUpper()
# TODO: Use generateHeaders further down once it supports proxies.
var s = newSocket()
@@ -497,7 +497,7 @@ proc request*(url: string, httpMethod: string, extraHeaders = "",
if body != "":
s.send(body)
result = parseResponse(s, httpMethod != "httpHEAD", timeout)
result = parseResponse(s, httpMethod != "HEAD", timeout)
proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
body = "", sslContext = defaultSSLContext, timeout = -1,
@@ -1034,7 +1034,7 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
##
## When a request is made to a different hostname, the current connection will
## be closed.
result = await request(client, url, substr($httpMethod, len("http")), body)
result = await request(client, url, $httpMethod, body)
proc get*(client: HttpClient | AsyncHttpClient,
url: string): Future[Response] {.multisync.} =

View File

@@ -226,7 +226,7 @@ proc `$`*(code: HttpCode): string =
## For example:
##
## .. code-block:: nim
## doAssert(Http404.status == "404 Not Found")
## doAssert($Http404 == "404 Not Found")
case code.int
of 100: "100 Continue"
of 101: "101 Switching Protocols"
@@ -298,6 +298,9 @@ proc is5xx*(code: HttpCode): bool =
## Determines whether ``code`` is a 5xx HTTP status code.
return code.int in {500 .. 599}
proc `$`*(httpMethod: HttpMethod): string =
return (system.`$`(httpMethod))[4 .. ^1].toUpper()
when isMainModule:
var test = newHttpHeaders()
test["Connection"] = @["Upgrade", "Close"]

View File

@@ -69,6 +69,11 @@ that have tuple name:
value no longer requires this value to be prefixed with ``"http"``
(or similar).
- Converting a ``HttpMethod`` value to string using the ``$`` operator will
give string values without the ``"Http"`` prefix now.
- The ``Request`` object defined in the ``asynchttpserver`` module now uses
the ``HttpMethod`` type for the request method.
Library Additions
-----------------