diff options
author | bptato <nincsnevem662@gmail.com> | 2023-09-30 18:02:48 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-09-30 19:02:05 +0200 |
commit | 31fbd611aa107cc023dcf1e1358f19ff58f14075 (patch) | |
tree | c3360c2f4575a249bb30f18b746ab3a0ce0c91b2 /src/types | |
parent | bb2c7cb87a99f790b6c741f85de666dd7aeae10c (diff) | |
download | chawan-31fbd611aa107cc023dcf1e1358f19ff58f14075.tar.gz |
Add urimethodmap support
yay
Diffstat (limited to 'src/types')
-rw-r--r-- | src/types/urimethodmap.nim | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/types/urimethodmap.nim b/src/types/urimethodmap.nim new file mode 100644 index 00000000..6d57230b --- /dev/null +++ b/src/types/urimethodmap.nim @@ -0,0 +1,68 @@ +# w3m's URI method map format. + +import strutils +import tables + +import types/opt +import types/url +import utils/twtstr + +type URIMethodMap* = object + map: Table[string, string] + +func rewriteURL(pattern, surl: string): string = + result = "" + var was_perc = false + for c in pattern: + if was_perc: + if c == '%': + result &= '%' + elif c == 's': + result.percentEncode(surl, ComponentPercentEncodeSet) + else: + result &= '%' + result &= c + was_perc = false + elif c != '%': + result &= c + else: + was_perc = true + if was_perc: + result &= '%' + +proc `[]=`*(this: var URIMethodMap, k, v: string) = + this.map[k] = v + +type URIMethodMapResult* = enum + URI_RESULT_NOT_FOUND, URI_RESULT_SUCCESS, URI_RESULT_WRONG_URL + +proc findAndRewrite*(this: URIMethodMap, url: var URL): URIMethodMapResult = + let protocol = url.protocol + if protocol in this.map: + let surl = this.map[protocol].rewriteURL($url) + let x = newURL(surl) + if x.isNone: + return URI_RESULT_WRONG_URL + url = x.get + return URI_RESULT_SUCCESS + return URI_RESULT_NOT_FOUND + +proc parseURIMethodMap*(this: var URIMethodMap, s: string) = + for line in s.split('\n'): + if line.len == 0 or line[0] == '#': + continue # comments + var k = "" + var i = 0 + while i < line.len and line[i] != ':': + k &= line[i].toLowerAscii() + inc i + if i >= line.len: + continue # invalid + while i < line.len and line[i] in AsciiWhitespace: + inc i + var v = line.until(AsciiWhitespace, i) + if v.startsWith("file:/cgi-bin/"): + v = "cgi-bin:" & v.substr("file:/cgi-bin/".len) + elif v.startsWith("/cgi-bin/"): + v = "cgi-bin:" & v.substr("/cgi-bin/".len) + this[k] = v |