Add more helpers to handle Accept header. (#224)
* Add API working with strings instead of `HttpRequestRef` object. * Fix comment.
This commit is contained in:
parent
d57aae27a0
commit
59b91bf0ca
|
@ -470,10 +470,9 @@ proc getAcceptInfo*(request: HttpRequestRef): Result[AcceptInfo, cstring] =
|
||||||
let acceptHeader = request.headers.getString(AcceptHeaderName)
|
let acceptHeader = request.headers.getString(AcceptHeaderName)
|
||||||
getAcceptInfo(acceptHeader)
|
getAcceptInfo(acceptHeader)
|
||||||
|
|
||||||
proc preferredContentMediaType*(request: HttpRequestRef): MediaType =
|
proc preferredContentMediaType*(acceptHeader: string): MediaType =
|
||||||
## Returns preferred content-type using ``Accept`` header specified by
|
## Returns preferred content-type using ``Accept`` header value specified by
|
||||||
## client in request ``request``.
|
## string ``acceptHeader``.
|
||||||
let acceptHeader = request.headers.getString(AcceptHeaderName)
|
|
||||||
let res = getAcceptInfo(acceptHeader)
|
let res = getAcceptInfo(acceptHeader)
|
||||||
if res.isErr():
|
if res.isErr():
|
||||||
# If `Accept` header is incorrect, client accepts any type of content.
|
# If `Accept` header is incorrect, client accepts any type of content.
|
||||||
|
@ -485,10 +484,10 @@ proc preferredContentMediaType*(request: HttpRequestRef): MediaType =
|
||||||
else:
|
else:
|
||||||
MediaType.init("*", "*")
|
MediaType.init("*", "*")
|
||||||
|
|
||||||
proc preferredContentType*(request: HttpRequestRef,
|
proc preferredContentType*(acceptHeader: string,
|
||||||
types: varargs[string]): Result[string, cstring] =
|
types: varargs[string]): Result[string, cstring] =
|
||||||
## Match or obtain preferred content-type using ``Accept`` header specified by
|
## 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
|
## If ``Accept`` header is missing in client's request - ``types[0]`` or
|
||||||
## ``*/*`` value will be returned as result.
|
## ``*/*`` value will be returned as result.
|
||||||
|
@ -500,7 +499,6 @@ proc preferredContentType*(request: HttpRequestRef,
|
||||||
## by client, the best value will be selected from ``types`` using
|
## by client, the best value will be selected from ``types`` using
|
||||||
## quality value (weight) reported in ``Accept`` header. If client do not
|
## quality value (weight) reported in ``Accept`` header. If client do not
|
||||||
## support any methods in ``types`` error will be returned.
|
## support any methods in ``types`` error will be returned.
|
||||||
let acceptHeader = request.headers.getString(AcceptHeaderName)
|
|
||||||
if len(types) == 0:
|
if len(types) == 0:
|
||||||
if len(acceptHeader) == 0:
|
if len(acceptHeader) == 0:
|
||||||
# If `Accept` header is missing, return `*/*`.
|
# If `Accept` header is missing, return `*/*`.
|
||||||
|
@ -538,6 +536,17 @@ proc preferredContentType*(request: HttpRequestRef,
|
||||||
return ok($expect)
|
return ok($expect)
|
||||||
err("Preferred content type not found")
|
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,
|
proc sendErrorResponse(conn: HttpConnectionRef, version: HttpVersion,
|
||||||
code: HttpCode, keepAlive = true,
|
code: HttpCode, keepAlive = true,
|
||||||
datatype = "text/text",
|
datatype = "text/text",
|
||||||
|
|
Loading…
Reference in New Issue