diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-06-29 12:32:17 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-06-29 12:50:16 +0200 |
| commit | 2e50aa23237da76802d2a61cb7426bf51c122d14 (patch) | |
| tree | 9ed3f42f9ef3f6a6f97c4118bc5fbc90248f00ce /src/html | |
| parent | e7786e39e38ddf5ec75b95cc19e1bee108cd37d2 (diff) | |
| download | chawan-2e50aa23237da76802d2a61cb7426bf51c122d14.tar.gz | |
dom, pager: cache images from network
With many limitations:
* slightly randomized expiry, so it's harder to fingerprint
* only images. so e.g. CSS is still left uncached
* it's per-buffer and non-persistent, so images are still redownloaded
for every new page load
so it's more of an image sharing between placements than true caching.
Diffstat (limited to 'src/html')
| -rw-r--r-- | src/html/dom.nim | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index bb5128dc..0fbdda7d 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -5,6 +5,7 @@ import std/options import std/sets import std/strutils import std/tables +import std/times import chagashi/charset import chagashi/decoder @@ -70,6 +71,12 @@ type Location = ref object window: Window + CachedURLImage = ref object + expiry: int64 + loading: bool + shared: seq[HTMLImageElement] + bmp: NetworkBitmap + Window* = ref object of EventTarget attrs*: WindowAttributes internalConsole*: Console @@ -86,6 +93,7 @@ type importMapsAllowed*: bool factory*: CAtomFactory loadingResourcePromises*: seq[EmptyPromise] + imageURLCache: Table[string, CachedURLImage] images*: bool styling*: bool # ID of the next image @@ -2912,6 +2920,16 @@ proc loadResource(window: Window; image: HTMLImageElement) = # mixed content :/ #TODO maybe do this in loader? url.scheme = "https" + let surl = $url + window.imageURLCache.withValue(surl, p): + if p[].expiry > getTime().utc().toTime().toUnix(): + image.bitmap = p[].bmp + return + elif p[].loading: + p[].shared.add(image) + return + let cachedURL = CachedURLImage(expiry: -1, loading: true) + window.imageURLCache[surl] = cachedURL let p = window.loader.fetch(newRequest(url)).then( proc(res: JSResult[Response]): EmptyPromise = if res.isNone: @@ -2932,9 +2950,21 @@ proc loadResource(window: Window; image: HTMLImageElement) = response.resume() response.unregisterFun() response.body.sclose() + var expiry = -1i64 + if "Cache-Control" in response.headers: + for hdr in response.headers.table["Cache-Control"]: + var i = hdr.find("max-age=") + if i != -1: + i = hdr.skipBlanks(i + "max-age=".len) + let s = hdr.until(AllChars - AsciiDigit, i) + let pi = parseInt64(s) + if pi.isSome: + expiry = getTime().utc().toTime().toUnix() + pi.get + break + cachedURL.loading = false + cachedURL.expiry = expiry return r.then(proc(res: JSResult[Response]): EmptyPromise = if res.isNone: - window.console.error("Failed to decode", $response.url) return let response = res.get # close immediately; all data we're interested in is in the headers. @@ -2951,13 +2981,17 @@ proc loadResource(window: Window; image: HTMLImageElement) = if width.isNone or height.isNone: window.console.error("wrong Cha-Image-Dimensions in", $response.url) return - image.bitmap = NetworkBitmap( + let bmp = NetworkBitmap( width: width.get, height: height.get, cacheId: cacheId, imageId: window.getImageId(), contentType: contentType ) + image.bitmap = bmp + cachedURL.bmp = bmp + for share in cachedURL.shared: + share.bitmap = bmp ) ) window.loadingResourcePromises.add(p) |