Fixes deprecation warnings in asynchttpserver.
This commit is contained in:
parent
e4a5b1af6c
commit
9471f585c2
1 changed files with 21 additions and 21 deletions
|
|
@ -18,7 +18,7 @@
|
||||||
##
|
##
|
||||||
## .. code-block::nim
|
## .. code-block::nim
|
||||||
## var server = newAsyncHttpServer()
|
## var server = newAsyncHttpServer()
|
||||||
## proc cb(req: TRequest) {.async.} =
|
## proc cb(req: Request) {.async.} =
|
||||||
## await req.respond(Http200, "Hello World")
|
## await req.respond(Http200, "Hello World")
|
||||||
##
|
##
|
||||||
## asyncCheck server.serve(Port(8080), cb)
|
## asyncCheck server.serve(Port(8080), cb)
|
||||||
|
|
@ -27,16 +27,16 @@
|
||||||
import strtabs, asyncnet, asyncdispatch, parseutils, uri, strutils
|
import strtabs, asyncnet, asyncdispatch, parseutils, uri, strutils
|
||||||
type
|
type
|
||||||
Request* = object
|
Request* = object
|
||||||
client*: PAsyncSocket # TODO: Separate this into a Response object?
|
client*: AsyncSocket # TODO: Separate this into a Response object?
|
||||||
reqMethod*: string
|
reqMethod*: string
|
||||||
headers*: PStringTable
|
headers*: StringTableRef
|
||||||
protocol*: tuple[orig: string, major, minor: int]
|
protocol*: tuple[orig: string, major, minor: int]
|
||||||
url*: TUri
|
url*: Uri
|
||||||
hostname*: string ## The hostname of the client that made the request.
|
hostname*: string ## The hostname of the client that made the request.
|
||||||
body*: string
|
body*: string
|
||||||
|
|
||||||
AsyncHttpServer* = ref object
|
AsyncHttpServer* = ref object
|
||||||
socket: PAsyncSocket
|
socket: AsyncSocket
|
||||||
reuseAddr: bool
|
reuseAddr: bool
|
||||||
|
|
||||||
HttpCode* = enum
|
HttpCode* = enum
|
||||||
|
|
@ -82,7 +82,7 @@ type
|
||||||
THttpCode: HttpCode, THttpVersion: HttpVersion].}
|
THttpCode: HttpCode, THttpVersion: HttpVersion].}
|
||||||
|
|
||||||
proc `==`*(protocol: tuple[orig: string, major, minor: int],
|
proc `==`*(protocol: tuple[orig: string, major, minor: int],
|
||||||
ver: THttpVersion): bool =
|
ver: HttpVersion): bool =
|
||||||
let major =
|
let major =
|
||||||
case ver
|
case ver
|
||||||
of HttpVer11, HttpVer10: 1
|
of HttpVer11, HttpVer10: 1
|
||||||
|
|
@ -92,23 +92,23 @@ proc `==`*(protocol: tuple[orig: string, major, minor: int],
|
||||||
of HttpVer10: 0
|
of HttpVer10: 0
|
||||||
result = protocol.major == major and protocol.minor == minor
|
result = protocol.major == major and protocol.minor == minor
|
||||||
|
|
||||||
proc newAsyncHttpServer*(reuseAddr = true): PAsyncHttpServer =
|
proc newAsyncHttpServer*(reuseAddr = true): AsyncHttpServer =
|
||||||
## Creates a new ``AsyncHttpServer`` instance.
|
## Creates a new ``AsyncHttpServer`` instance.
|
||||||
new result
|
new result
|
||||||
result.reuseAddr = reuseAddr
|
result.reuseAddr = reuseAddr
|
||||||
|
|
||||||
proc addHeaders(msg: var string, headers: PStringTable) =
|
proc addHeaders(msg: var string, headers: StringTableRef) =
|
||||||
for k, v in headers:
|
for k, v in headers:
|
||||||
msg.add(k & ": " & v & "\c\L")
|
msg.add(k & ": " & v & "\c\L")
|
||||||
|
|
||||||
proc sendHeaders*(req: TRequest, headers: PStringTable): Future[void] =
|
proc sendHeaders*(req: Request, headers: StringTableRef): Future[void] =
|
||||||
## Sends the specified headers to the requesting client.
|
## Sends the specified headers to the requesting client.
|
||||||
var msg = ""
|
var msg = ""
|
||||||
addHeaders(msg, headers)
|
addHeaders(msg, headers)
|
||||||
return req.client.send(msg)
|
return req.client.send(msg)
|
||||||
|
|
||||||
proc respond*(req: TRequest, code: THttpCode,
|
proc respond*(req: Request, code: HttpCode,
|
||||||
content: string, headers: PStringTable = newStringTable()) {.async.} =
|
content: string, headers = newStringTable()) {.async.} =
|
||||||
## Responds to the request with the specified ``HttpCode``, headers and
|
## Responds to the request with the specified ``HttpCode``, headers and
|
||||||
## content.
|
## content.
|
||||||
##
|
##
|
||||||
|
|
@ -119,7 +119,7 @@ proc respond*(req: TRequest, code: THttpCode,
|
||||||
msg.addHeaders(customHeaders)
|
msg.addHeaders(customHeaders)
|
||||||
await req.client.send(msg & "\c\L" & content)
|
await req.client.send(msg & "\c\L" & content)
|
||||||
|
|
||||||
proc newRequest(): TRequest =
|
proc newRequest(): Request =
|
||||||
result.headers = newStringTable(modeCaseInsensitive)
|
result.headers = newStringTable(modeCaseInsensitive)
|
||||||
result.hostname = ""
|
result.hostname = ""
|
||||||
result.body = ""
|
result.body = ""
|
||||||
|
|
@ -134,18 +134,18 @@ proc parseHeader(line: string): tuple[key, value: string] =
|
||||||
proc parseProtocol(protocol: string): tuple[orig: string, major, minor: int] =
|
proc parseProtocol(protocol: string): tuple[orig: string, major, minor: int] =
|
||||||
var i = protocol.skipIgnoreCase("HTTP/")
|
var i = protocol.skipIgnoreCase("HTTP/")
|
||||||
if i != 5:
|
if i != 5:
|
||||||
raise newException(EInvalidValue, "Invalid request protocol. Got: " &
|
raise newException(ValueError, "Invalid request protocol. Got: " &
|
||||||
protocol)
|
protocol)
|
||||||
result.orig = protocol
|
result.orig = protocol
|
||||||
i.inc protocol.parseInt(result.major, i)
|
i.inc protocol.parseInt(result.major, i)
|
||||||
i.inc # Skip .
|
i.inc # Skip .
|
||||||
i.inc protocol.parseInt(result.minor, i)
|
i.inc protocol.parseInt(result.minor, i)
|
||||||
|
|
||||||
proc sendStatus(client: PAsyncSocket, status: string): Future[void] =
|
proc sendStatus(client: AsyncSocket, status: string): Future[void] =
|
||||||
client.send("HTTP/1.1 " & status & "\c\L")
|
client.send("HTTP/1.1 " & status & "\c\L")
|
||||||
|
|
||||||
proc processClient(client: PAsyncSocket, address: string,
|
proc processClient(client: AsyncSocket, address: string,
|
||||||
callback: proc (request: TRequest):
|
callback: proc (request: Request):
|
||||||
Future[void] {.closure, gcsafe.}) {.async.} =
|
Future[void] {.closure, gcsafe.}) {.async.} =
|
||||||
while not client.closed:
|
while not client.closed:
|
||||||
# GET /path HTTP/1.1
|
# GET /path HTTP/1.1
|
||||||
|
|
@ -187,7 +187,7 @@ proc processClient(client: PAsyncSocket, address: string,
|
||||||
request.url = parseUri(path)
|
request.url = parseUri(path)
|
||||||
try:
|
try:
|
||||||
request.protocol = protocol.parseProtocol()
|
request.protocol = protocol.parseProtocol()
|
||||||
except EInvalidValue:
|
except ValueError:
|
||||||
asyncCheck request.respond(Http400, "Invalid request protocol. Got: " &
|
asyncCheck request.respond(Http400, "Invalid request protocol. Got: " &
|
||||||
protocol)
|
protocol)
|
||||||
continue
|
continue
|
||||||
|
|
@ -233,8 +233,8 @@ proc processClient(client: PAsyncSocket, address: string,
|
||||||
request.client.close()
|
request.client.close()
|
||||||
break
|
break
|
||||||
|
|
||||||
proc serve*(server: PAsyncHttpServer, port: Port,
|
proc serve*(server: AsyncHttpServer, port: Port,
|
||||||
callback: proc (request: TRequest): Future[void] {.closure,gcsafe.},
|
callback: proc (request: Request): Future[void] {.closure,gcsafe.},
|
||||||
address = "") {.async.} =
|
address = "") {.async.} =
|
||||||
## Starts the process of listening for incoming HTTP connections on the
|
## Starts the process of listening for incoming HTTP connections on the
|
||||||
## specified address and port.
|
## specified address and port.
|
||||||
|
|
@ -254,14 +254,14 @@ proc serve*(server: PAsyncHttpServer, port: Port,
|
||||||
#echo(f.isNil)
|
#echo(f.isNil)
|
||||||
#echo(f.repr)
|
#echo(f.repr)
|
||||||
|
|
||||||
proc close*(server: PAsyncHttpServer) =
|
proc close*(server: AsyncHttpServer) =
|
||||||
## Terminates the async http server instance.
|
## Terminates the async http server instance.
|
||||||
server.socket.close()
|
server.socket.close()
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
proc main =
|
proc main =
|
||||||
var server = newAsyncHttpServer()
|
var server = newAsyncHttpServer()
|
||||||
proc cb(req: TRequest) {.async.} =
|
proc cb(req: Request) {.async.} =
|
||||||
#echo(req.reqMethod, " ", req.url)
|
#echo(req.reqMethod, " ", req.url)
|
||||||
#echo(req.headers)
|
#echo(req.headers)
|
||||||
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
|
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue