about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-12 00:22:18 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-12 00:22:18 +0100
commit4874b92f0d177c0a7e84c1dd106f9b16eec7cb05 (patch)
tree6b0b9f31ee2f9b0f6f3d88317e1d1759b4a783e6 /src/io
parentbce29ef14df2fc8cc4cd227d6ef5af6f16b377fd (diff)
downloadchawan-4874b92f0d177c0a7e84c1dd106f9b16eec7cb05.tar.gz
Add urlfilter
Diffstat (limited to 'src/io')
-rw-r--r--src/io/loader.nim15
-rw-r--r--src/io/urlfilter.nim20
2 files changed, 30 insertions, 5 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 150831a0..dbca256b 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -22,6 +22,7 @@ import bindings/curl
 import io/about
 import io/http
 import io/request
+import io/urlfilter
 import ips/serialize
 import ips/serversocket
 import ips/socketstream
@@ -83,7 +84,7 @@ proc loadResource(request: Request, ostream: Stream) =
     ostream.flush()
 
 var ssock: ServerSocket
-proc runFileLoader*(fd: cint, defaultHeaders: HeaderList) =
+proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter) =
   if curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK:
     raise newException(Defect, "Failed to initialize libcurl.")
   ssock = initServerSocket()
@@ -108,10 +109,14 @@ proc runFileLoader*(fd: cint, defaultHeaders: HeaderList) =
       of LOAD:
         var request: Request
         stream.sread(request)
-        for k, v in defaultHeaders.table:
-          if k notin request.headers.table:
-            request.headers.table[k] = v
-        loadResource(request, stream)
+        if not filter.match(request.url):
+          stream.swrite(-1) # error
+          stream.flush()
+        else:
+          for k, v in defaultHeaders.table:
+            if k notin request.headers.table:
+              request.headers.table[k] = v
+          loadResource(request, stream)
         stream.close()
       of QUIT:
         stream.close()
diff --git a/src/io/urlfilter.nim b/src/io/urlfilter.nim
new file mode 100644
index 00000000..6c0b71c9
--- /dev/null
+++ b/src/io/urlfilter.nim
@@ -0,0 +1,20 @@
+import options
+
+import types/url
+
+type URLFilter* = object
+  scheme: Option[string]
+  host: Option[string]
+
+proc newURLFilter*(scheme = none(string), host = none(string)): URLFilter =
+  return URLFilter(
+    scheme: scheme,
+    host: host
+  )
+
+func 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