diff options
author | bptato <nincsnevem662@gmail.com> | 2024-09-30 21:29:17 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-09-30 21:29:17 +0200 |
commit | 7870b9aa5d32b5b3c938be205f9ba49fd4e4734f (patch) | |
tree | 396bc3465617dbc584668b6722dd5d39af99dc7d /src/local | |
parent | f9433f13765ca6ec6aa5d2c34dd4d3c76d93c08a (diff) | |
download | chawan-7870b9aa5d32b5b3c938be205f9ba49fd4e4734f.tar.gz |
pager: improve rewrite-url semantics
Previously, it just changed the URL before loading the site; now it's an actual redirect. Technically, the previous behavior was more flexible, because it let you apply siteconf rules exclusively for sites where you redirected from. Practically, this was not very useful, and probably unexpected for anybody trying to use the feature. This also fixes a bug where the loader filter would be set for the original page, so you couldn't switch from https to http, etc.
Diffstat (limited to 'src/local')
-rw-r--r-- | src/local/pager.nim | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/local/pager.nim b/src/local/pager.nim index df18c394..09951082 100644 --- a/src/local/pager.nim +++ b/src/local/pager.nim @@ -1206,8 +1206,8 @@ proc windowChange*(pager: Pager) = # Apply siteconf settings to a request. # Note that this may modify the URL passed. -proc applySiteconf(pager: Pager; url: var URL; charsetOverride: Charset; - loaderConfig: var LoaderClientConfig): BufferConfig = +proc applySiteconf(pager: Pager; url: URL; charsetOverride: Charset; + loaderConfig: var LoaderClientConfig; ourl: var URL): BufferConfig = let host = url.host let ctx = pager.jsctx var res = BufferConfig( @@ -1234,14 +1234,16 @@ proc applySiteconf(pager: Pager; url: var URL; charsetOverride: Charset; ), insecureSSLNoVerify: false ) + let surl = $url for sc in pager.config.siteconf: - if sc.url.isSome and not sc.url.get.match($url): + if sc.url.isSome and not sc.url.get.match(surl): continue elif sc.host.isSome and not sc.host.get.match(host): continue if sc.rewrite_url.isSome: let fun = sc.rewrite_url.get - var arg0 = ctx.toJS(url) + var tmpUrl = newURL(url) + var arg0 = ctx.toJS(tmpUrl) let ret = JS_Call(ctx, fun, JS_UNDEFINED, 1, arg0.toJSValueArray()) if not JS_IsException(ret): # Warning: we must only print exceptions if the *call* returned one. @@ -1249,12 +1251,15 @@ proc applySiteconf(pager: Pager; url: var URL; charsetOverride: Charset; # new URL, and that's fine. var nu: URL if ctx.fromJS(ret, nu).isSome and nu != nil: - url = nu + tmpUrl = nu else: #TODO should writeException the message to console pager.alert("Error rewriting URL: " & ctx.getExceptionMsg()) JS_FreeValue(ctx, arg0) JS_FreeValue(ctx, ret) + if $tmpUrl != surl: + ourl = tmpUrl + return if sc.cookie.isSome: if sc.cookie.get: # host/url might have changed by now @@ -1302,7 +1307,13 @@ proc gotoURL(pager: Pager; request: Request; prevurl = none(URL); if referrer != nil and referrer.config.refererFrom: request.referrer = referrer.url var loaderConfig: LoaderClientConfig - var bufferConfig = pager.applySiteconf(request.url, cs, loaderConfig) + var bufferConfig: BufferConfig + for i in 0 ..< pager.config.network.max_redirect: + var ourl: URL = nil + bufferConfig = pager.applySiteconf(request.url, cs, loaderConfig, ourl) + if ourl == nil: + break + request.url = ourl if prevurl.isNone or not prevurl.get.equals(request.url, true) or request.url.hash == "" or request.httpMethod != hmGet or save: # Basically, we want to reload the page *only* when @@ -1404,7 +1415,8 @@ proc readPipe0*(pager: Pager; contentType: string; cs: Charset; pager.loader.passFd(url.pathname, fd) safeClose(fd) var loaderConfig: LoaderClientConfig - let bufferConfig = pager.applySiteconf(url, cs, loaderConfig) + var ourl: URL + let bufferConfig = pager.applySiteconf(url, cs, loaderConfig, ourl) return pager.newContainer( bufferConfig, loaderConfig, |