diff options
author | bptato <nincsnevem662@gmail.com> | 2022-12-13 03:00:16 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-12-13 03:01:10 +0100 |
commit | 1b9c56ac70d8a0af3cd39ba0d49f7651f6ac8fb4 (patch) | |
tree | 9a6c879b129c6e95d5b090dcc7f7b0078f666a2a /src | |
parent | 89750ef4ad621fe7fdce533d1265f3d970098a19 (diff) | |
download | chawan-1b9c56ac70d8a0af3cd39ba0d49f7651f6ac8fb4.tar.gz |
Add network.max-redirect, prepend-https options
Diffstat (limited to 'src')
-rw-r--r-- | src/config/config.nim | 9 | ||||
-rw-r--r-- | src/display/pager.nim | 23 | ||||
-rw-r--r-- | src/utils/twtstr.nim | 4 |
3 files changed, 22 insertions, 14 deletions
diff --git a/src/config/config.nim b/src/config/config.nim index 16e1c652..e00724df 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -44,6 +44,8 @@ type Config* = ref ConfigObj ConfigObj* = object + maxredirect*: int + prependhttps*: bool termreload*: bool nmap*: ActionMap lemap*: ActionMap @@ -237,6 +239,13 @@ proc parseConfig(config: Config, dir: string, t: TomlValue) = config.startup = v.s of "headless": config.headless = v.b + of "network": + for k, v in v: + case k + of "max-redirects": + config.maxredirect = int(v.i) + of "prepend-https": + config.prependhttps = v.b of "page": for k, v in v: config.nmap[getRealKey(k)] = v.s diff --git a/src/display/pager.nim b/src/display/pager.nim index 66280edc..d1eca1d7 100644 --- a/src/display/pager.nim +++ b/src/display/pager.nim @@ -302,6 +302,7 @@ proc redraw(pager: Pager) {.jsfunc.} = pager.term.clearCanvas() proc draw*(pager: Pager) = + if pager.container == nil: return pager.term.hideCursor() if pager.redraw: pager.refreshDisplay() @@ -522,7 +523,6 @@ proc omniRewrite(pager: Pager, s: string): string = # * file://$PWD/<file> # * https://<url> # So we attempt to load both, and see what works. -# (TODO: make this optional) proc loadURL*(pager: Pager, url: string, ctype = none(string)) = let url0 = pager.omniRewrite(url) let url = if url[0] == '~': expandPath(url0) else: url0 @@ -535,19 +535,15 @@ proc loadURL*(pager: Pager, url: string, ctype = none(string)) = pager.gotoURL(newRequest(firstparse.get), prev, ctype) return var urls: seq[URL] - if url[0] != '/': + if pager.config.prependhttps and url[0] != '/': let pageurl = parseURL("https://" & url) if pageurl.isSome: # attempt to load remote page urls.add(pageurl.get) - let cdir = parseURL("file://" & getCurrentDir() & DirSep) - let purl = percentEncode(url, LocalPathPercentEncodeSet) - if purl != url: - let newurl = parseURL(purl, cdir) - if newurl.isSome: - urls.add(newurl.get) - let localurl = parseURL(url, cdir) - if localurl.isSome: # attempt to load local file - urls.add(localurl.get) + let cdir = parseURL("file://" & percentEncode(getCurrentDir(), LocalPathPercentEncodeSet) & DirSep) + let localurl = percentEncode(url, LocalPathPercentEncodeSet) + let newurl = parseURL(localurl, cdir) + if newurl.isSome: + urls.add(newurl.get) # attempt to load local file if urls.len == 0: pager.alert("Invalid URL " & url) else: @@ -713,12 +709,15 @@ proc handleEvent0(pager: Pager, container: Container, event: ContainerEvent): bo if pager.container == container: pager.authorize() of REDIRECT: - if container.redirectdepth < 10: + if container.redirectdepth < pager.config.maxredirect: let redirect = event.location pager.alert("Redirecting to " & $redirect) pager.gotoURL(newRequest(redirect), some(container.source.location), replace = container, redirectdepth = container.redirectdepth + 1) else: pager.alert("Error: maximum redirection depth reached") + pager.deleteContainer(container) + if pager.container == nil: + return false of ANCHOR: var url2 = newURL(container.source.location) url2.hash(event.anchor) diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 1a38e6c5..ff1d7c6a 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -449,9 +449,9 @@ const ComponentPercentEncodeSet* = (UserInfoPercentEncodeSet + {'$'..'&', '+', ' const ApplicationXWWWFormUrlEncodedSet* = (ComponentPercentEncodeSet + {'!', '\''..')', '~'}) # used by client when defined(windows) or defined(OS2) or defined(DOS): - const LocalPathPercentEncodeSet* = (QueryPercentEncodeSet + {'?', ':'}) + const LocalPathPercentEncodeSet* = (Ascii - AsciiAlpha - AsciiDigit - {'\\', '/'}) else: - const LocalPathPercentEncodeSet* = (QueryPercentEncodeSet + {'?', ':', '\\'}) + const LocalPathPercentEncodeSet* = (Ascii - AsciiAlpha - AsciiDigit - {'/'}) proc percentEncode*(append: var string, c: char, set: set[char], spaceAsPlus = false) {.inline.} = if spaceAsPlus and c == ' ': |