Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use async to fix asynchttpserver #236

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions jester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,24 @@ proc unsafeSend(request: Request, content: string) =

proc send(
request: Request, code: HttpCode, headers: Option[RawHeaders], body: string
) =
): Future[void] =
when useHttpBeast:
let h =
if headers.isNone: ""
else: headers.get().createHeaders
request.getNativeReq.send(code, body, h)
var fut = newFuture[void]()
complete(fut)
return fut
else:
# TODO: This may cause issues if we send too fast.
asyncCheck request.getNativeReq.respond(
return request.getNativeReq.respond(
code, body, newHttpHeaders(headers.get(@({:})))
)

proc statusContent(request: Request, status: HttpCode, content: string,
headers: Option[RawHeaders]) =
headers: Option[RawHeaders]): Future[void] =
try:
send(request, status, headers, content)
result = send(request, status, headers, content)
when not defined(release):
logging.debug(" $1 $2" % [$status, toStr(headers)])
except:
Expand Down Expand Up @@ -188,18 +190,18 @@ proc sendStaticIfExists(
# If the user has a cached version of this file and it matches our
# version, let them use it
if req.headers.hasKey("If-None-Match") and req.headers["If-None-Match"] == hashed:
req.statusContent(Http304, "", none[RawHeaders]())
await req.statusContent(Http304, "", none[RawHeaders]())
else:
req.statusContent(Http200, file, some(@({
"Content-Type": mimetype,
"ETag": hashed
})))
await req.statusContent(Http200, file, some(@({
"Content-Type": mimetype,
"ETag": hashed
})))
else:
let headers = @({
"Content-Type": mimetype,
"Content-Length": $fileSize
})
req.statusContent(Http200, "", some(headers))
await req.statusContent(Http200, "", some(headers))

var fileStream = newFutureStream[string]("sendStaticIfExists")
var file = openAsync(p, fmRead)
Expand Down Expand Up @@ -227,7 +229,7 @@ proc close*(request: Request) =
## Routes using this procedure must enable raw mode.
let nativeReq = request.getNativeReq()
when useHttpBeast:
nativeReq.forget()
nativeReq.forget()
nativeReq.client.close()

proc defaultErrorFilter(error: RouteError): ResponseData =
Expand Down Expand Up @@ -363,7 +365,7 @@ proc handleRequestSlow(
not dispatchedError and respData.content.len == 0:
respData = await dispatchError(jes, req, initRouteError(respData))

statusContent(
await statusContent(
req,
respData.code,
respData.content,
Expand All @@ -384,7 +386,7 @@ proc handleRequest(jes: Jester, httpReq: NativeRequest): Future[void] =
if likely(jes.matchers.len == 1 and not jes.matchers[0].async):
let respData = jes.matchers[0].syncProc(req)
if likely(respData.matched):
statusContent(
return statusContent(
req,
respData.code,
respData.content,
Expand All @@ -398,9 +400,6 @@ proc handleRequest(jes: Jester, httpReq: NativeRequest): Future[void] =
let exc = getCurrentException()
let respDataFut = dispatchError(jes, req, initRouteError(exc))
return handleRequestSlow(jes, req, respDataFut, true)
let future = newFuture[void]()
complete(future)
return future
Comment on lines -401 to -403
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, so this was simply moved to send.


proc newSettings*(
port = Port(5000), staticDir = getCurrentDir() / "public",
Expand Down
1 change: 1 addition & 0 deletions jester/request.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ proc reqMethod*(req: Request): HttpMethod =
req.req.httpMethod.get()
else:
req.req.reqMethod

proc reqMeth*(req: Request): HttpMethod {.deprecated.} =
req.reqMethod

Expand Down