nim-chronos/chronos/apps/http/httptable.nim

204 lines
6.3 KiB
Nim
Raw Permalink Normal View History

2021-01-27 06:14:17 +00:00
#
# Chronos HTTP/S case-insensitive non-unique
# key-value memory storage
# (c) Copyright 2021-Present
2021-01-27 06:14:17 +00:00
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
import std/[tables, strutils]
import stew/base10
2021-01-27 06:14:17 +00:00
2023-06-05 20:21:50 +00:00
{.push raises: [].}
2021-02-10 13:13:36 +00:00
2021-01-27 06:14:17 +00:00
type
HttpTable* = object
table: Table[string, seq[string]]
HttpTableRef* = ref HttpTable
HttpTables* = HttpTable | HttpTableRef
proc add*(ht: var HttpTables, key: string, value: string) =
## Add string ``value`` to header with key ``key``.
2021-02-10 13:13:36 +00:00
var default: seq[string]
ht.table.mgetOrPut(key.toLowerAscii(), default).add(value)
2021-01-27 06:14:17 +00:00
proc add*(ht: var HttpTables, key: string, value: SomeInteger) =
## Add integer ``value`` to header with key ``key``.
2021-01-27 06:14:17 +00:00
ht.add(key, $value)
proc set*(ht: var HttpTables, key: string, value: string) =
## Set/replace value of header with key ``key`` to value ``value``.
let lowkey = key.toLowerAscii()
ht.table[lowkey] = @[value]
proc hasKeyOrPut*(ht: var HttpTables, key: string, value: string): bool =
## Returns true if ``key`` is in the table ``ht``,
## otherwise inserts ``value``.
ht.table.hasKeyOrPut(key, @[value])
proc contains*(ht: HttpTables, key: string): bool =
## Returns ``true`` if header with name ``key`` is present in HttpTable/Ref.
2021-01-27 06:14:17 +00:00
ht.table.contains(key.toLowerAscii())
proc getList*(ht: HttpTables, key: string,
2021-12-10 09:55:25 +00:00
default: openArray[string] = []): seq[string] =
## Returns sequence of headers with key ``key``.
var defseq = @default
ht.table.getOrDefault(key.toLowerAscii(), defseq)
2021-01-27 06:14:17 +00:00
proc getString*(ht: HttpTables, key: string,
default: string = ""): string =
## Returns concatenated value of headers with key ``key``.
##
## If there multiple headers with the same name ``key`` the result value will
## be concatenation using `,`.
var defseq: seq[string]
let res = ht.table.getOrDefault(key.toLowerAscii(), defseq)
if len(res) == 0:
return default
else:
res.join(",")
2021-01-27 06:14:17 +00:00
proc count*(ht: HttpTables, key: string): int =
## Returns number of headers with key ``key``.
2021-01-27 06:14:17 +00:00
var default: seq[string]
len(ht.table.getOrDefault(key.toLowerAscii(), default))
2021-01-27 06:14:17 +00:00
proc getInt*(ht: HttpTables, key: string): uint64 =
## Parse header with key ``key`` as unsigned integer.
##
## Integers are parsed in safe way, there no exceptions or errors will be
## raised.
##
## Procedure returns `0` value in next cases:
## 1. The value is empty.
## 2. Non-decimal character encountered during the parsing of the value.
## 3. Result exceeds `uint64` maximum allowed value.
let res = Base10.decode(uint64, ht.getString(key))
if res.isOk():
res.get()
else:
0'u64
2021-01-27 06:14:17 +00:00
proc getLastString*(ht: HttpTables, key: string): string =
## Returns "last" value of header ``key``.
##
## If there multiple headers with the same name ``key`` the value of last
## encountered header will be returned.
2021-01-27 06:14:17 +00:00
var default: seq[string]
let item = ht.table.getOrDefault(key.toLowerAscii(), default)
if len(item) == 0:
""
else:
item[^1]
proc getLastInt*(ht: HttpTables, key: string): uint64 =
## Returns "last" value of header ``key`` as unsigned integer.
##
## If there multiple headers with the same name ``key`` the value of last
## encountered header will be returned.
##
## Unsigned integer will be parsed using rules of getInt() procedure.
let res = Base10.decode(uint64, ht.getLastString(key))
if res.isOk():
res.get()
else:
0'u64
2021-01-27 06:14:17 +00:00
proc init*(htt: typedesc[HttpTable]): HttpTable =
## Create empty HttpTable.
2021-01-27 06:14:17 +00:00
HttpTable(table: initTable[string, seq[string]]())
proc new*(htt: typedesc[HttpTableRef]): HttpTableRef =
## Create empty HttpTableRef.
2021-01-27 06:14:17 +00:00
HttpTableRef(table: initTable[string, seq[string]]())
proc init*(htt: typedesc[HttpTable],
data: openArray[tuple[key: string, value: string]]): HttpTable =
## Create HttpTable using array of tuples with header names and values.
var res = HttpTable.init()
for item in data:
res.add(item.key, item.value)
res
proc new*(htt: typedesc[HttpTableRef],
data: openArray[tuple[key: string, value: string]]): HttpTableRef =
## Create HttpTableRef using array of tuples with header names and values.
var res = HttpTableRef.new()
for item in data:
res.add(item.key, item.value)
res
2021-02-02 10:48:23 +00:00
proc isEmpty*(ht: HttpTables): bool =
## Returns ``true`` if HttpTable ``ht`` is empty (do not have any values).
len(ht.table) == 0
2021-02-01 16:04:38 +00:00
2021-01-27 06:14:17 +00:00
proc normalizeHeaderName*(value: string): string =
2021-02-02 10:48:23 +00:00
## Set any header name to have first capital letters in their name
##
## For example:
## "content-length" become "<C>ontent-<L>ength"
## "expect" become "<E>xpect"
2021-01-27 06:14:17 +00:00
var res = value.toLowerAscii()
var k = 0
while k < len(res):
if k == 0:
res[k] = toUpperAscii(res[k])
inc(k, 1)
else:
if res[k] == '-':
if k + 1 < len(res):
res[k + 1] = toUpperAscii(res[k + 1])
inc(k, 2)
else:
break
else:
inc(k, 1)
res
2021-02-02 10:48:23 +00:00
iterator stringItems*(ht: HttpTables,
2021-02-10 13:13:36 +00:00
normKey = false): tuple[key: string, value: string] =
## Iterate over HttpTable/Ref values.
2021-02-02 10:48:23 +00:00
##
2021-02-10 13:13:36 +00:00
## If ``normKey`` is true, key name value will be normalized using
2021-02-02 10:48:23 +00:00
## normalizeHeaderName() procedure.
for k, v in ht.table.pairs():
2021-02-10 13:13:36 +00:00
let key = if normKey: normalizeHeaderName(k) else: k
2021-02-02 10:48:23 +00:00
for item in v:
yield (key, item)
iterator items*(ht: HttpTables,
2021-02-10 13:13:36 +00:00
normKey = false): tuple[key: string, value: seq[string]] =
## Iterate over HttpTable/Ref values.
2021-02-02 10:48:23 +00:00
##
2021-02-10 13:13:36 +00:00
## If ``normKey`` is true, key name value will be normalized using
2021-02-02 10:48:23 +00:00
## normalizeHeaderName() procedure.
for k, v in ht.table.pairs():
2021-02-10 13:13:36 +00:00
let key = if normKey: normalizeHeaderName(k) else: k
2021-02-02 10:48:23 +00:00
yield (key, v)
2021-01-27 06:14:17 +00:00
proc `$`*(ht: HttpTables): string =
## Returns string representation of HttpTable/Ref.
2021-01-27 06:14:17 +00:00
var res = ""
for key, value in ht.table.pairs():
for item in value:
res.add(key.normalizeHeaderName())
res.add(": ")
res.add(item)
res.add("\p")
res
proc toList*(ht: HttpTables, normKey = false): auto =
## Returns sequence of (key, value) pairs.
var res: seq[tuple[key: string, value: string]]
for key, value in ht.stringItems(normKey):
res.add((key, value))
res
proc clear*(ht: var HttpTables) =
## Resets the HtppTable so that it is empty.
ht.table.clear()