use func in httpcore (#15457)
This commit is contained in:
parent
d1eb76184b
commit
6e32a9e88c
1 changed files with 22 additions and 22 deletions
|
|
@ -102,24 +102,24 @@ const
|
||||||
const httpNewLine* = "\c\L"
|
const httpNewLine* = "\c\L"
|
||||||
const headerLimit* = 10_000
|
const headerLimit* = 10_000
|
||||||
|
|
||||||
proc toTitleCase(s: string): string =
|
func toTitleCase(s: string): string =
|
||||||
result = newString(len(s))
|
result = newString(len(s))
|
||||||
var upper = true
|
var upper = true
|
||||||
for i in 0..len(s) - 1:
|
for i in 0..len(s) - 1:
|
||||||
result[i] = if upper: toUpperAscii(s[i]) else: toLowerAscii(s[i])
|
result[i] = if upper: toUpperAscii(s[i]) else: toLowerAscii(s[i])
|
||||||
upper = s[i] == '-'
|
upper = s[i] == '-'
|
||||||
|
|
||||||
proc toCaseInsensitive(headers: HttpHeaders, s: string): string {.inline.} =
|
func toCaseInsensitive(headers: HttpHeaders, s: string): string {.inline.} =
|
||||||
return if headers.isTitleCase: toTitleCase(s) else: toLowerAscii(s)
|
return if headers.isTitleCase: toTitleCase(s) else: toLowerAscii(s)
|
||||||
|
|
||||||
proc newHttpHeaders*(titleCase=false): HttpHeaders =
|
func newHttpHeaders*(titleCase=false): HttpHeaders =
|
||||||
## Returns a new ``HttpHeaders`` object. if ``titleCase`` is set to true,
|
## Returns a new ``HttpHeaders`` object. if ``titleCase`` is set to true,
|
||||||
## headers are passed to the server in title case (e.g. "Content-Length")
|
## headers are passed to the server in title case (e.g. "Content-Length")
|
||||||
new result
|
new result
|
||||||
result.table = newTable[string, seq[string]]()
|
result.table = newTable[string, seq[string]]()
|
||||||
result.isTitleCase = titleCase
|
result.isTitleCase = titleCase
|
||||||
|
|
||||||
proc newHttpHeaders*(keyValuePairs:
|
func newHttpHeaders*(keyValuePairs:
|
||||||
openArray[tuple[key: string, val: string]], titleCase=false): HttpHeaders =
|
openArray[tuple[key: string, val: string]], titleCase=false): HttpHeaders =
|
||||||
## Returns a new ``HttpHeaders`` object from an array. if ``titleCase`` is set to true,
|
## Returns a new ``HttpHeaders`` object from an array. if ``titleCase`` is set to true,
|
||||||
## headers are passed to the server in title case (e.g. "Content-Length")
|
## headers are passed to the server in title case (e.g. "Content-Length")
|
||||||
|
|
@ -134,13 +134,13 @@ proc newHttpHeaders*(keyValuePairs:
|
||||||
result.table[key] = @[pair.val]
|
result.table[key] = @[pair.val]
|
||||||
|
|
||||||
|
|
||||||
proc `$`*(headers: HttpHeaders): string =
|
func `$`*(headers: HttpHeaders): string =
|
||||||
return $headers.table
|
return $headers.table
|
||||||
|
|
||||||
proc clear*(headers: HttpHeaders) =
|
proc clear*(headers: HttpHeaders) =
|
||||||
headers.table.clear()
|
headers.table.clear()
|
||||||
|
|
||||||
proc `[]`*(headers: HttpHeaders, key: string): HttpHeaderValues =
|
func `[]`*(headers: HttpHeaders, key: string): HttpHeaderValues =
|
||||||
## Returns the values associated with the given ``key``. If the returned
|
## Returns the values associated with the given ``key``. If the returned
|
||||||
## values are passed to a procedure expecting a ``string``, the first
|
## values are passed to a procedure expecting a ``string``, the first
|
||||||
## value is automatically picked. If there are
|
## value is automatically picked. If there are
|
||||||
|
|
@ -153,7 +153,7 @@ proc `[]`*(headers: HttpHeaders, key: string): HttpHeaderValues =
|
||||||
converter toString*(values: HttpHeaderValues): string =
|
converter toString*(values: HttpHeaderValues): string =
|
||||||
return seq[string](values)[0]
|
return seq[string](values)[0]
|
||||||
|
|
||||||
proc `[]`*(headers: HttpHeaders, key: string, i: int): string =
|
func `[]`*(headers: HttpHeaders, key: string, i: int): string =
|
||||||
## Returns the ``i``'th value associated with the given key. If there are
|
## Returns the ``i``'th value associated with the given key. If there are
|
||||||
## no values associated with the key or the ``i``'th value doesn't exist,
|
## no values associated with the key or the ``i``'th value doesn't exist,
|
||||||
## an exception is raised.
|
## an exception is raised.
|
||||||
|
|
@ -188,16 +188,16 @@ iterator pairs*(headers: HttpHeaders): tuple[key, value: string] =
|
||||||
for value in v:
|
for value in v:
|
||||||
yield (k, value)
|
yield (k, value)
|
||||||
|
|
||||||
proc contains*(values: HttpHeaderValues, value: string): bool =
|
func contains*(values: HttpHeaderValues, value: string): bool =
|
||||||
## Determines if ``value`` is one of the values inside ``values``. Comparison
|
## Determines if ``value`` is one of the values inside ``values``. Comparison
|
||||||
## is performed without case sensitivity.
|
## is performed without case sensitivity.
|
||||||
for val in seq[string](values):
|
for val in seq[string](values):
|
||||||
if val.toLowerAscii == value.toLowerAscii: return true
|
if val.toLowerAscii == value.toLowerAscii: return true
|
||||||
|
|
||||||
proc hasKey*(headers: HttpHeaders, key: string): bool =
|
func hasKey*(headers: HttpHeaders, key: string): bool =
|
||||||
return headers.table.hasKey(headers.toCaseInsensitive(key))
|
return headers.table.hasKey(headers.toCaseInsensitive(key))
|
||||||
|
|
||||||
proc getOrDefault*(headers: HttpHeaders, key: string,
|
func getOrDefault*(headers: HttpHeaders, key: string,
|
||||||
default = @[""].HttpHeaderValues): HttpHeaderValues =
|
default = @[""].HttpHeaderValues): HttpHeaderValues =
|
||||||
## Returns the values associated with the given ``key``. If there are no
|
## Returns the values associated with the given ``key``. If there are no
|
||||||
## values associated with the key, then ``default`` is returned.
|
## values associated with the key, then ``default`` is returned.
|
||||||
|
|
@ -206,9 +206,9 @@ proc getOrDefault*(headers: HttpHeaders, key: string,
|
||||||
else:
|
else:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
proc len*(headers: HttpHeaders): int = return headers.table.len
|
func len*(headers: HttpHeaders): int = return headers.table.len
|
||||||
|
|
||||||
proc parseList(line: string, list: var seq[string], start: int): int =
|
func parseList(line: string, list: var seq[string], start: int): int =
|
||||||
var i = 0
|
var i = 0
|
||||||
var current = ""
|
var current = ""
|
||||||
while start+i < line.len and line[start + i] notin {'\c', '\l'}:
|
while start+i < line.len and line[start + i] notin {'\c', '\l'}:
|
||||||
|
|
@ -219,7 +219,7 @@ proc parseList(line: string, list: var seq[string], start: int): int =
|
||||||
i.inc # Skip ,
|
i.inc # Skip ,
|
||||||
current.setLen(0)
|
current.setLen(0)
|
||||||
|
|
||||||
proc parseHeader*(line: string): tuple[key: string, value: seq[string]] =
|
func parseHeader*(line: string): tuple[key: string, value: seq[string]] =
|
||||||
## Parses a single raw header HTTP line into key value pairs.
|
## Parses a single raw header HTTP line into key value pairs.
|
||||||
##
|
##
|
||||||
## Used by ``asynchttpserver`` and ``httpclient`` internally and should not
|
## Used by ``asynchttpserver`` and ``httpclient`` internally and should not
|
||||||
|
|
@ -239,7 +239,7 @@ proc parseHeader*(line: string): tuple[key: string, value: seq[string]] =
|
||||||
else:
|
else:
|
||||||
result.value = @[]
|
result.value = @[]
|
||||||
|
|
||||||
proc `==`*(protocol: tuple[orig: string, major, minor: int],
|
func `==`*(protocol: tuple[orig: string, major, minor: int],
|
||||||
ver: HttpVersion): bool =
|
ver: HttpVersion): bool =
|
||||||
let major =
|
let major =
|
||||||
case ver
|
case ver
|
||||||
|
|
@ -250,10 +250,10 @@ 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 contains*(methods: set[HttpMethod], x: string): bool =
|
func contains*(methods: set[HttpMethod], x: string): bool =
|
||||||
return parseEnum[HttpMethod](x) in methods
|
return parseEnum[HttpMethod](x) in methods
|
||||||
|
|
||||||
proc `$`*(code: HttpCode): string =
|
func `$`*(code: HttpCode): string =
|
||||||
## Converts the specified ``HttpCode`` into a HTTP status.
|
## Converts the specified ``HttpCode`` into a HTTP status.
|
||||||
##
|
##
|
||||||
## For example:
|
## For example:
|
||||||
|
|
@ -311,7 +311,7 @@ proc `$`*(code: HttpCode): string =
|
||||||
of 505: "505 HTTP Version Not Supported"
|
of 505: "505 HTTP Version Not Supported"
|
||||||
else: $(int(code))
|
else: $(int(code))
|
||||||
|
|
||||||
proc `==`*(a, b: HttpCode): bool {.borrow.}
|
func `==`*(a, b: HttpCode): bool {.borrow.}
|
||||||
|
|
||||||
proc `==`*(rawCode: string, code: HttpCode): bool
|
proc `==`*(rawCode: string, code: HttpCode): bool
|
||||||
{.deprecated: "Deprecated since v1.2; use rawCode == $code instead".} =
|
{.deprecated: "Deprecated since v1.2; use rawCode == $code instead".} =
|
||||||
|
|
@ -323,23 +323,23 @@ proc `==`*(rawCode: string, code: HttpCode): bool
|
||||||
## string form of itself.
|
## string form of itself.
|
||||||
return cmpIgnoreCase(rawCode, $code) == 0
|
return cmpIgnoreCase(rawCode, $code) == 0
|
||||||
|
|
||||||
proc is2xx*(code: HttpCode): bool =
|
func is2xx*(code: HttpCode): bool =
|
||||||
## Determines whether ``code`` is a 2xx HTTP status code.
|
## Determines whether ``code`` is a 2xx HTTP status code.
|
||||||
return code.int in {200 .. 299}
|
return code.int in {200 .. 299}
|
||||||
|
|
||||||
proc is3xx*(code: HttpCode): bool =
|
func is3xx*(code: HttpCode): bool =
|
||||||
## Determines whether ``code`` is a 3xx HTTP status code.
|
## Determines whether ``code`` is a 3xx HTTP status code.
|
||||||
return code.int in {300 .. 399}
|
return code.int in {300 .. 399}
|
||||||
|
|
||||||
proc is4xx*(code: HttpCode): bool =
|
func is4xx*(code: HttpCode): bool =
|
||||||
## Determines whether ``code`` is a 4xx HTTP status code.
|
## Determines whether ``code`` is a 4xx HTTP status code.
|
||||||
return code.int in {400 .. 499}
|
return code.int in {400 .. 499}
|
||||||
|
|
||||||
proc is5xx*(code: HttpCode): bool =
|
func is5xx*(code: HttpCode): bool =
|
||||||
## Determines whether ``code`` is a 5xx HTTP status code.
|
## Determines whether ``code`` is a 5xx HTTP status code.
|
||||||
return code.int in {500 .. 599}
|
return code.int in {500 .. 599}
|
||||||
|
|
||||||
proc `$`*(httpMethod: HttpMethod): string =
|
func `$`*(httpMethod: HttpMethod): string =
|
||||||
return (system.`$`(httpMethod))[4 .. ^1].toUpperAscii()
|
return (system.`$`(httpMethod))[4 .. ^1].toUpperAscii()
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue