From d0910f85adfec4a2cddfe2297be821841f4dd671 Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 11 Aug 2024 13:59:24 +0200 Subject: dom: fix crash on wrong image content type + slightly optimize getContentType --- src/html/dom.nim | 7 +++++-- src/loader/response.nim | 5 ++--- src/utils/twtstr.nim | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/html/dom.nim b/src/html/dom.nim index e58be7fc..20f21264 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -3035,12 +3035,15 @@ proc loadResource(window: Window; image: HTMLImageElement) = return newResolvedPromise() let response = res.get let contentType = response.getContentType("image/x-unknown") - if contentType.until('/') != "image": + if not contentType.startsWith("image/"): return newResolvedPromise() let cacheId = window.loader.addCacheFile(response.outputId, window.loader.clientPid) + let url = newURL("img-codec+" & contentType.after('/') & ":decode") + if url.isNone: + return newResolvedPromise() let request = newRequest( - newURL("img-codec+" & contentType.after('/') & ":decode").get, + url.get, httpMethod = hmPost, headers = newHeaders({"Cha-Image-Info-Only": "1"}), body = RequestBody(t: rbtOutput, outputId: response.outputId), diff --git a/src/loader/response.nim b/src/loader/response.nim index e97e931b..a3b7e3e4 100644 --- a/src/loader/response.nim +++ b/src/loader/response.nim @@ -97,9 +97,8 @@ func getCharset*(this: Response; fallback: Charset): Charset = func getContentType*(this: Response; fallback = "application/octet-stream"): string = - if "Content-Type" in this.headers.table: - let header = this.headers.table["Content-Type"][0].toLowerAscii() - return header.until(';').strip() + this.headers.table.withValue("Content-Type", p): + return p[][0].untilLower(';').strip() # also use DefaultGuess for container, so that local mime.types cannot # override buffer mime.types return DefaultGuess.guessContentType(this.url.pathname, fallback) diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 81384eed..c41b545f 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -158,6 +158,9 @@ func untilLower*(s: string; c: set[char]; starti = 0): string = func until*(s: string; c: char; starti = 0): string = return s.until({c}, starti) +func untilLower*(s: string; c: char; starti = 0): string = + return s.untilLower({c}, starti) + func after*(s: string; c: set[char]): string = let i = s.find(c) if i != -1: -- cgit 1.4.1-2-gfad0