From fb11d9f8ab6e5495b9db7f9f794aa869f27ed0f9 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 2 Feb 2021 12:48:23 +0200 Subject: [PATCH] Add httptable changes. --- chronos/apps/http/httptable.nim | 37 ++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/chronos/apps/http/httptable.nim b/chronos/apps/http/httptable.nim index b5ba1ef..73d9807 100644 --- a/chronos/apps/http/httptable.nim +++ b/chronos/apps/http/httptable.nim @@ -105,16 +105,16 @@ proc init*(htt: typedesc[HttpTable]): HttpTable = proc new*(htt: typedesc[HttpTableRef]): HttpTableRef = HttpTableRef(table: initTable[string, seq[string]]()) -iterator stringItems*(ht: HttpTables): tuple[key: string, value: string] = - for k, v in ht.table.pairs(): - for item in v: - yield (k, item) - -iterator items*(ht: HttpTables): tuple[key: string, value: seq[string]] = - for k, v in ht.table.pairs(): - yield (k, v) +proc isEmpty*(ht: HttpTables): bool = + ## Returns ``true`` if HttpTable ``ht`` is empty (do not have any values). + len(ht.table) == 0 proc normalizeHeaderName*(value: string): string = + ## Set any header name to have first capital letters in their name + ## + ## For example: + ## "content-length" become "ontent-ength" + ## "expect" become "xpect" var res = value.toLowerAscii() var k = 0 while k < len(res): @@ -132,6 +132,27 @@ proc normalizeHeaderName*(value: string): string = inc(k, 1) res +iterator stringItems*(ht: HttpTables, + normalizeKey = false): tuple[key: string, value: string] = + ## Iterate over HttpTable values. + ## + ## If ``normalizeKey`` is true, key name value will be normalized using + ## normalizeHeaderName() procedure. + for k, v in ht.table.pairs(): + let key = if normalizeKey: normalizeHeaderName(k) else: k + for item in v: + yield (key, item) + +iterator items*(ht: HttpTables, + normalizeKey = false): tuple[key: string, value: seq[string]] = + ## Iterate over HttpTable values. + ## + ## If ``normalizeKey`` is true, key name value will be normalized using + ## normalizeHeaderName() procedure. + for k, v in ht.table.pairs(): + let key = if normalizeKey: normalizeHeaderName(k) else: k + yield (key, v) + proc `$`*(ht: HttpTables): string = var res = "" for key, value in ht.table.pairs():