Skip to content

Commit

Permalink
Add more helpers to handle Accept header. (#224)
Browse files Browse the repository at this point in the history
* Add API working with strings instead of `HttpRequestRef` object.

* Fix comment.
  • Loading branch information
cheatfate committed Sep 27, 2021
1 parent d57aae2 commit 59b91bf
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions chronos/apps/http/httpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,9 @@ proc getAcceptInfo*(request: HttpRequestRef): Result[AcceptInfo, cstring] =
let acceptHeader = request.headers.getString(AcceptHeaderName)
getAcceptInfo(acceptHeader)

proc preferredContentMediaType*(request: HttpRequestRef): MediaType =
## Returns preferred content-type using ``Accept`` header specified by
## client in request ``request``.
let acceptHeader = request.headers.getString(AcceptHeaderName)
proc preferredContentMediaType*(acceptHeader: string): MediaType =
## Returns preferred content-type using ``Accept`` header value specified by
## string ``acceptHeader``.
let res = getAcceptInfo(acceptHeader)
if res.isErr():
# If `Accept` header is incorrect, client accepts any type of content.
Expand All @@ -485,10 +484,10 @@ proc preferredContentMediaType*(request: HttpRequestRef): MediaType =
else:
MediaType.init("*", "*")

proc preferredContentType*(request: HttpRequestRef,
proc preferredContentType*(acceptHeader: string,
types: varargs[string]): Result[string, cstring] =
## Match or obtain preferred content-type using ``Accept`` header specified by
## client in request ``request``.
## string ``acceptHeader``.
##
## If ``Accept`` header is missing in client's request - ``types[0]`` or
## ``*/*`` value will be returned as result.
Expand All @@ -500,7 +499,6 @@ proc preferredContentType*(request: HttpRequestRef,
## by client, the best value will be selected from ``types`` using
## quality value (weight) reported in ``Accept`` header. If client do not
## support any methods in ``types`` error will be returned.
let acceptHeader = request.headers.getString(AcceptHeaderName)
if len(types) == 0:
if len(acceptHeader) == 0:
# If `Accept` header is missing, return `*/*`.
Expand Down Expand Up @@ -538,6 +536,17 @@ proc preferredContentType*(request: HttpRequestRef,
return ok($expect)
err("Preferred content type not found")

proc preferredContentMediaType*(request: HttpRequestRef): MediaType =
## Returns preferred content-type using ``Accept`` header specified by
## client in request ``request``.
preferredContentMediaType(request.headers.getString(AcceptHeaderName))

proc preferredContentType*(request: HttpRequestRef,
types: varargs[string]): Result[string, cstring] =
## Match or obtain preferred content-type using ``Accept`` header specified by
## client in request ``request``.
preferredContentType(request.headers.getString(AcceptHeaderName), types)

proc sendErrorResponse(conn: HttpConnectionRef, version: HttpVersion,
code: HttpCode, keepAlive = true,
datatype = "text/text",
Expand Down

0 comments on commit 59b91bf

Please sign in to comment.