2021-01-27 06:14:17 +00:00
|
|
|
#
|
|
|
|
# Chronos HTTP/S case-insensitive non-unique
|
|
|
|
# key-value memory storage
|
2021-02-02 22:33:14 +00:00
|
|
|
# (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]
|
2021-03-05 22:22:32 +00:00
|
|
|
import stew/base10
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-02-10 13:13:36 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
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) =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Add string ``value`` to header with key ``key``.
|
2021-02-10 13:13:36 +00:00
|
|
|
var default: seq[string]
|
2021-02-18 12:08:21 +00:00
|
|
|
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) =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Add integer ``value`` to header with key ``key``.
|
2021-01-27 06:14:17 +00:00
|
|
|
ht.add(key, $value)
|
|
|
|
|
2021-01-31 05:57:58 +00:00
|
|
|
proc set*(ht: var HttpTables, key: string, value: string) =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Set/replace value of header with key ``key`` to value ``value``.
|
2021-01-31 05:57:58 +00:00
|
|
|
let lowkey = key.toLowerAscii()
|
|
|
|
ht.table[lowkey] = @[value]
|
|
|
|
|
2021-03-11 11:41:13 +00:00
|
|
|
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])
|
|
|
|
|
2021-02-26 09:31:46 +00:00
|
|
|
proc contains*(ht: HttpTables, key: string): bool =
|
2021-02-18 12:08:21 +00:00
|
|
|
## 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())
|
|
|
|
|
2021-01-31 05:57:58 +00:00
|
|
|
proc getList*(ht: HttpTables, key: string,
|
|
|
|
default: openarray[string] = []): seq[string] =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Returns sequence of headers with key ``key``.
|
2021-01-31 05:57:58 +00:00
|
|
|
var defseq = @default
|
|
|
|
ht.table.getOrDefault(key.toLowerAscii(), defseq)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-01-31 05:57:58 +00:00
|
|
|
proc getString*(ht: HttpTables, key: string,
|
|
|
|
default: string = ""): string =
|
2021-02-18 12:08:21 +00:00
|
|
|
## 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]
|
2021-01-31 05:57:58 +00:00
|
|
|
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 =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Returns number of headers with key ``key``.
|
2021-01-27 06:14:17 +00:00
|
|
|
var default: seq[string]
|
2021-02-18 12:08:21 +00:00
|
|
|
len(ht.table.getOrDefault(key.toLowerAscii(), default))
|
2021-01-27 06:14:17 +00:00
|
|
|
|
|
|
|
proc getInt*(ht: HttpTables, key: string): uint64 =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Parse header with key ``key`` as unsigned integer.
|
|
|
|
##
|
|
|
|
## Integers are parsed in safe way, there no exceptions or errors will be
|
|
|
|
## raised.
|
|
|
|
##
|
2021-03-05 22:22:32 +00:00
|
|
|
## 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 =
|
2021-02-18 12:08:21 +00:00
|
|
|
## 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 =
|
2021-02-18 12:08:21 +00:00
|
|
|
## 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.
|
2021-03-05 22:22:32 +00:00
|
|
|
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 =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Create empty HttpTable.
|
2021-01-27 06:14:17 +00:00
|
|
|
HttpTable(table: initTable[string, seq[string]]())
|
|
|
|
|
|
|
|
proc new*(htt: typedesc[HttpTableRef]): HttpTableRef =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Create empty HttpTableRef.
|
2021-01-27 06:14:17 +00:00
|
|
|
HttpTableRef(table: initTable[string, seq[string]]())
|
|
|
|
|
2021-02-02 22:33:14 +00:00
|
|
|
proc init*(htt: typedesc[HttpTable],
|
|
|
|
data: openArray[tuple[key: string, value: string]]): HttpTable =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Create HttpTable using array of tuples with header names and values.
|
2021-02-02 22:33:14 +00:00
|
|
|
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 =
|
2021-02-18 12:08:21 +00:00
|
|
|
## Create HttpTableRef using array of tuples with header names and values.
|
2021-02-02 22:33:14 +00:00
|
|
|
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] =
|
2021-02-18 12:08:21 +00:00
|
|
|
## 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]] =
|
2021-02-18 12:08:21 +00:00
|
|
|
## 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 =
|
2021-02-18 12:08:21 +00:00
|
|
|
## 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
|