diff options
author | bptato <nincsnevem662@gmail.com> | 2024-08-11 13:59:24 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-08-11 13:59:24 +0200 |
commit | d0910f85adfec4a2cddfe2297be821841f4dd671 (patch) | |
tree | 7fac3e6c83bcb96ab8156eb22a833649ac56659c | |
parent | 4393412d971269055401f2e8a6be766f4402e193 (diff) | |
download | chawan-d0910f85adfec4a2cddfe2297be821841f4dd671.tar.gz |
dom: fix crash on wrong image content type
+ slightly optimize getContentType
-rw-r--r-- | src/html/dom.nim | 7 | ||||
-rw-r--r-- | src/loader/response.nim | 5 | ||||
-rw-r--r-- | 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: |