diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index fd0ef3856476f..f6c867adfbd3b 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -838,6 +838,7 @@ proc parseResponse(client: HttpClient | AsyncHttpClient, var parsedStatus = false var linei = 0 var fullyRead = false + var prevHeader = "" var line = "" result.headers = newHttpHeaders() while true: @@ -873,13 +874,24 @@ proc parseResponse(client: HttpClient | AsyncHttpClient, else: # Parse headers var name = "" + var leadingSpaces = skipWhitespace(line, linei) var le = parseUntil(line, name, ':', linei) - if le <= 0: httpError("invalid headers") - inc(linei, le) - if line[linei] != ':': httpError("invalid headers") - inc(linei) # Skip : + # We only check lines that have `:` in them and don't have + # leading whitespace, so non-header lines are just ignored + if le < line.len: + if leadingSpaces == 0 and le < line.len: + if le <= 0: httpError("invalid headers") + prevHeader = name + inc(linei, le) + if line[linei] != ':': httpError("invalid headers") + inc(linei) # Skip : + result.headers.add(name, line[linei .. ^1].strip()) + # If there are spaces before the header name, it's actually a value + # that should be appended to the previous header, see bug #19261 + # Also, if there was no header before this, we just ignore the line + elif prevHeader != "": + result.headers.table[result.headers.toCaseInsensitive(prevHeader)][^1].add line.strip() - result.headers.add(name, line[linei .. ^1].strip()) if result.headers.len > headerLimit: httpError("too many headers") diff --git a/lib/pure/httpcore.nim b/lib/pure/httpcore.nim index 3b5cbc3cfc9e5..93ed1590b4c1b 100644 --- a/lib/pure/httpcore.nim +++ b/lib/pure/httpcore.nim @@ -126,7 +126,7 @@ func toTitleCase(s: string): string = result[i] = if upper: toUpperAscii(s[i]) else: toLowerAscii(s[i]) upper = s[i] == '-' -func toCaseInsensitive(headers: HttpHeaders, s: string): string {.inline.} = +func toCaseInsensitive*(headers: HttpHeaders, s: string): string {.inline.} = return if headers.isTitleCase: toTitleCase(s) else: toLowerAscii(s) func newHttpHeaders*(titleCase=false): HttpHeaders =