about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-13 13:08:33 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-13 13:08:33 +0100
commit672ab553c4a2b10a703ea40e049eda52db149a93 (patch)
tree10c581ab40702e113bffe6605c58a640ac5095ee /src/io
parentf3e2cb7bfa4517155a2c6675e6310f17d8ca4d89 (diff)
downloadchawan-672ab553c4a2b10a703ea40e049eda52db149a93.tar.gz
Add more cookie options
Diffstat (limited to 'src/io')
-rw-r--r--src/io/loader.nim7
-rw-r--r--src/io/urlfilter.nim30
2 files changed, 27 insertions, 10 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 671b9ca3..0b0f5531 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -118,9 +118,10 @@ proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter, coo
             if k notin request.headers.table:
               request.headers.table[k] = v
           if cookiejar != nil and cookiejar.cookies.len > 0:
-            if request.url.host == cookiejar.location.host:
-              if "Cookie" notin request.headers.table:
-                request.headers["Cookie"] = $cookiejar
+            if "Cookie" notin request.headers.table:
+              let cookie = cookiejar.serialize(request.url)
+              if cookie != "":
+                request.headers["Cookie"] = cookie
           loadResource(request, stream)
         stream.close()
       of QUIT:
diff --git a/src/io/urlfilter.nim b/src/io/urlfilter.nim
index 6c0b71c9..29dad8c6 100644
--- a/src/io/urlfilter.nim
+++ b/src/io/urlfilter.nim
@@ -1,20 +1,36 @@
 import options
 
+import js/regex
 import types/url
 
+#TODO add denyhost/s for blocklists
 type URLFilter* = object
   scheme: Option[string]
-  host: Option[string]
+  allowhost: Option[string]
+  allowhosts: Option[seq[Regex]]
+  default: bool
 
-proc newURLFilter*(scheme = none(string), host = none(string)): URLFilter =
+proc newURLFilter*(scheme = none(string), allowhost = none(string),
+                   allowhosts = none(seq[Regex]), default = false): URLFilter =
   return URLFilter(
     scheme: scheme,
-    host: host
+    allowhost: allowhost,
+    allowhosts: allowhosts,
+    default: default
   )
 
-func match*(filter: URLFilter, url: URL): bool =
+# Filters as follows:
+# If scheme is given, only URLs with the same scheme are matched.
+# Then, allowhost and allowhosts are checked; if none of these match the host,
+# the function returns the value of `default'.
+proc match*(filter: URLFilter, url: URL): bool =
   if filter.scheme.isSome and filter.scheme.get != url.scheme:
     return false
-  if filter.host.isSome and filter.host.get != url.host:
-    return false
-  return true
+  let host = url.host
+  if filter.allowhost.isSome and filter.allowhost.get == host:
+    return true
+  if filter.allowhosts.isSome:
+    for regex in filter.allowhosts.get:
+      if regex.match(host):
+        return true
+  return filter.default