Skip to content

Commit

Permalink
use func in httpcore (#15457)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Oct 2, 2020
1 parent d1eb761 commit 6e32a9e
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions lib/pure/httpcore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,24 @@ const
const httpNewLine* = "\c\L"
const headerLimit* = 10_000

proc toTitleCase(s: string): string =
func toTitleCase(s: string): string =
result = newString(len(s))
var upper = true
for i in 0..len(s) - 1:
result[i] = if upper: toUpperAscii(s[i]) else: toLowerAscii(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)

proc newHttpHeaders*(titleCase=false): HttpHeaders =
func newHttpHeaders*(titleCase=false): HttpHeaders =
## Returns a new ``HttpHeaders`` object. if ``titleCase`` is set to true,
## headers are passed to the server in title case (e.g. "Content-Length")
new result
result.table = newTable[string, seq[string]]()
result.isTitleCase = titleCase

proc newHttpHeaders*(keyValuePairs:
func newHttpHeaders*(keyValuePairs:
openArray[tuple[key: string, val: string]], titleCase=false): HttpHeaders =
## 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")
Expand All @@ -134,13 +134,13 @@ proc newHttpHeaders*(keyValuePairs:
result.table[key] = @[pair.val]


proc `$`*(headers: HttpHeaders): string =
func `$`*(headers: HttpHeaders): string =
return $headers.table

proc clear*(headers: HttpHeaders) =
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
## values are passed to a procedure expecting a ``string``, the first
## value is automatically picked. If there are
Expand All @@ -153,7 +153,7 @@ proc `[]`*(headers: HttpHeaders, key: string): HttpHeaderValues =
converter toString*(values: HttpHeaderValues): string =
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
## no values associated with the key or the ``i``'th value doesn't exist,
## an exception is raised.
Expand Down Expand Up @@ -188,16 +188,16 @@ iterator pairs*(headers: HttpHeaders): tuple[key, value: string] =
for value in v:
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
## is performed without case sensitivity.
for val in seq[string](values):
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))

proc getOrDefault*(headers: HttpHeaders, key: string,
func getOrDefault*(headers: HttpHeaders, key: string,
default = @[""].HttpHeaderValues): HttpHeaderValues =
## Returns the values associated with the given ``key``. If there are no
## values associated with the key, then ``default`` is returned.
Expand All @@ -206,9 +206,9 @@ proc getOrDefault*(headers: HttpHeaders, key: string,
else:
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 current = ""
while start+i < line.len and line[start + i] notin {'\c', '\l'}:
Expand All @@ -219,7 +219,7 @@ proc parseList(line: string, list: var seq[string], start: int): int =
i.inc # Skip ,
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.
##
## Used by ``asynchttpserver`` and ``httpclient`` internally and should not
Expand All @@ -239,7 +239,7 @@ proc parseHeader*(line: string): tuple[key: string, value: seq[string]] =
else:
result.value = @[]

proc `==`*(protocol: tuple[orig: string, major, minor: int],
func `==`*(protocol: tuple[orig: string, major, minor: int],
ver: HttpVersion): bool =
let major =
case ver
Expand All @@ -250,10 +250,10 @@ proc `==`*(protocol: tuple[orig: string, major, minor: int],
of HttpVer10: 0
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

proc `$`*(code: HttpCode): string =
func `$`*(code: HttpCode): string =
## Converts the specified ``HttpCode`` into a HTTP status.
##
## For example:
Expand Down Expand Up @@ -311,7 +311,7 @@ proc `$`*(code: HttpCode): string =
of 505: "505 HTTP Version Not Supported"
else: $(int(code))

proc `==`*(a, b: HttpCode): bool {.borrow.}
func `==`*(a, b: HttpCode): bool {.borrow.}

proc `==`*(rawCode: string, code: HttpCode): bool
{.deprecated: "Deprecated since v1.2; use rawCode == $code instead".} =
Expand All @@ -323,23 +323,23 @@ proc `==`*(rawCode: string, code: HttpCode): bool
## string form of itself.
return cmpIgnoreCase(rawCode, $code) == 0

proc is2xx*(code: HttpCode): bool =
func is2xx*(code: HttpCode): bool =
## Determines whether ``code`` is a 2xx HTTP status code.
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.
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.
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.
return code.int in {500 .. 599}

proc `$`*(httpMethod: HttpMethod): string =
func `$`*(httpMethod: HttpMethod): string =
return (system.`$`(httpMethod))[4 .. ^1].toUpperAscii()

when isMainModule:
Expand Down

0 comments on commit 6e32a9e

Please sign in to comment.