diff options
| author | bptato <nincsnevem662@gmail.com> | 2022-12-13 22:55:43 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2022-12-13 22:55:43 +0100 |
| commit | e80ae4b136fe82f916868e1e9b728d69c403a70a (patch) | |
| tree | d112f10934818394c746b9179de53552b9c778a1 /src/types | |
| parent | 4b482418c22ea31729ca94583ffd2a6aa167d811 (diff) | |
| download | chawan-e80ae4b136fe82f916868e1e9b728d69c403a70a.tar.gz | |
Add referer support, re-render on windowChange
Diffstat (limited to 'src/types')
| -rw-r--r-- | src/types/cookie.nim | 5 | ||||
| -rw-r--r-- | src/types/referer.nim | 71 | ||||
| -rw-r--r-- | src/types/url.nim | 42 |
3 files changed, 104 insertions, 14 deletions
diff --git a/src/types/cookie.nim b/src/types/cookie.nim index e5e50d3e..41de0f4c 100644 --- a/src/types/cookie.nim +++ b/src/types/cookie.nim @@ -123,7 +123,7 @@ proc serialize*(cookiejar: CookieJar, location: URL): string = let t = now().toTime().toUnix() for i in countdown(cookiejar.cookies.high, 0): let cookie = cookiejar.cookies[i] - if cookie.expires <= t: + if cookie.expires != -1 and cookie.expires <= t: cookiejar.cookies.delete(i) elif cookie.domain == "" or location.host.endsWith(cookie.domain): result.percentEncode(cookie.name, UserInfoPercentEncodeSet) @@ -133,6 +133,7 @@ proc serialize*(cookiejar: CookieJar, location: URL): string = proc newCookie*(str: string): Cookie {.jsctor.} = let cookie = new(Cookie) + cookie.expires = -1 var first = true for part in str.split(';'): if first: @@ -169,7 +170,7 @@ proc newCookieJar*(location: URL, allowhosts: seq[Regex]): CookieJar = filter: newURLFilter( scheme = some(location.scheme), allowhost = some(location.host), - allowhosts = some(allowhosts) + allowhosts = allowhosts ) ) diff --git a/src/types/referer.nim b/src/types/referer.nim new file mode 100644 index 00000000..77cb4e4d --- /dev/null +++ b/src/types/referer.nim @@ -0,0 +1,71 @@ +import url +import options + +type ReferrerPolicy* = enum + STRICT_ORIGIN_WHEN_CROSS_ORIGIN + NO_REFERRER + NO_REFERRER_WHEN_DOWNGRADE + STRICT_ORIGIN + ORIGIN + SAME_ORIGIN + ORIGIN_WHEN_CROSS_ORIGIN + UNSAFE_URL + +const DefaultPolicy* = STRICT_ORIGIN_WHEN_CROSS_ORIGIN + +proc getReferrerPolicy*(s: string): Option[ReferrerPolicy] = + case s + of "no-referrer": + return some(NO_REFERRER) + of "no-referrer-when-downgrade": + return some(NO_REFERRER_WHEN_DOWNGRADE) + of "origin": + return some(ORIGIN) + of "origin-when-cross-origin": + return some(ORIGIN_WHEN_CROSS_ORIGIN) + of "same-origin": + return some(SAME_ORIGIN) + of "strict-origin": + return some(STRICT_ORIGIN) + of "strict-origin-when-cross-origin": + return some(STRICT_ORIGIN_WHEN_CROSS_ORIGIN) + of "unsafe-url": + return some(UNSAFE_URL) + +proc getReferer*(prev, target: URL, policy: ReferrerPolicy): string = + let origin = prev.origin0 + if origin.isNone: + return "" + if prev.scheme != "http" and prev.scheme != "https": + return "" + if target.scheme != "http" and target.scheme != "https": + return "" + case policy + of NO_REFERRER: + return "" + of NO_REFERRER_WHEN_DOWNGRADE: + if prev.scheme == "https" and target.scheme == "http": + return "" + return $origin & prev.pathname & prev.search + of SAME_ORIGIN: + if origin == target.origin0: + return $origin + return "" + of ORIGIN: + return $origin + of STRICT_ORIGIN: + if prev.scheme == "https" and target.scheme == "http": + return "" + return $origin + of ORIGIN_WHEN_CROSS_ORIGIN: + if origin != target.origin0: + return $origin + return $origin & prev.pathname & prev.search + of STRICT_ORIGIN_WHEN_CROSS_ORIGIN: + if prev.scheme == "https" and target.scheme == "http": + return $origin + if origin != target.origin0: + return $origin + return $origin & prev.pathname & prev.search + of UNSAFE_URL: + return $origin & prev.pathname & prev.search diff --git a/src/types/url.nim b/src/types/url.nim index da27d96b..0b4471e3 100644 --- a/src/types/url.nim +++ b/src/types/url.nim @@ -53,6 +53,13 @@ type blob: Option[BlobUrlEntry] searchParams* {.jsget.}: URLSearchParams + Origin* = Option[tuple[ + scheme: string, + host: Host, + port: Option[uint16], + domain: Option[string] + ]] + const EmptyPath = UrlPath(opaque: true, s: "") const EmptyHost = Host(domain: "").some @@ -973,7 +980,7 @@ proc newURL*(s: string, base: Option[string] = none(string)): URL {.jserr, jscto url.get.searchParams.initURLSearchParams(url.get.query.get("")) return url.get -proc origin*(url: URL): string {.jsfget.} = +proc origin0*(url: URL): Origin = case url.scheme of "blob": if url.blob.issome: @@ -981,22 +988,33 @@ proc origin*(url: URL): string {.jsfget.} = discard let pathURL = parseURL($url.path) if pathURL.isnone: - return "null" - return pathURL.get.origin + return # opaque + return pathURL.get.origin0 of "ftp", "http", "https", "ws", "wss": - # return the tuple origin. - #TODO split this out - result = url.scheme - result &= "://" - result &= url.host.get.serialize() - if url.port.issome: - result &= ":" - result &= $url.port.get + return some((url.scheme, url.host.get, url.port, none(string))) of "file": #??? - return "null" + return # opaque else: + return # opaque + +proc `==`*(a, b: Origin): bool = + if a.isNone or b.isNone: return false + return a.get == b.get + +proc `$`*(origin: Origin): string = + if origin.isNone: return "null" + let origin = origin.get + result = origin.scheme + result &= "://" + result &= origin.host.serialize() + if origin.port.isSome: + result &= ':' + result &= $origin.port.get + +proc origin*(url: URL): string {.jsfget.} = + return $url.origin0 proc protocol*(url: URL): string {.jsfget.} = return url.scheme & ':' |