about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-13 22:55:43 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-13 22:55:43 +0100
commite80ae4b136fe82f916868e1e9b728d69c403a70a (patch)
treed112f10934818394c746b9179de53552b9c778a1 /src/io
parent4b482418c22ea31729ca94583ffd2a6aa167d811 (diff)
downloadchawan-e80ae4b136fe82f916868e1e9b728d69c403a70a.tar.gz
Add referer support, re-render on windowChange
Diffstat (limited to 'src/io')
-rw-r--r--src/io/loader.nim30
-rw-r--r--src/io/request.nim1
-rw-r--r--src/io/urlfilter.nim11
3 files changed, 22 insertions, 20 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 3e8baaf0..a1d449c7 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -28,18 +28,10 @@ import ips/serversocket
 import ips/socketstream
 import types/cookie
 import types/mime
+import types/referer
 import types/url
 import utils/twtstr
 
-const DefaultHeaders0 = {
-  "User-Agent": "chawan",
-  "Accept": "text/html,text/*;q=0.5",
-  "Accept-Language": "en;q=1.0",
-  "Pragma": "no-cache",
-  "Cache-Control": "no-cache",
-}.toTable()
-let DefaultHeaders* = DefaultHeaders0.newHeaderList()
-
 type
   FileLoader* = object
     process*: Pid
@@ -47,6 +39,12 @@ type
   LoaderCommand = enum
     LOAD, QUIT
 
+  LoaderConfig* = object
+    defaultheaders*: HeaderList
+    filter*: URLFilter
+    cookiejar*: CookieJar
+    referrerpolicy*: ReferrerPolicy
+
 proc loadFile(url: Url, ostream: Stream) =
   when defined(windows) or defined(OS2) or defined(DOS):
     let path = url.path.serialize_unicode_dos()
@@ -85,7 +83,7 @@ proc loadResource(request: Request, ostream: Stream) =
     ostream.flush()
 
 var ssock: ServerSocket
-proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter, cookiejar: CookieJar) =
+proc runFileLoader*(fd: cint, config: LoaderConfig) =
   if curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK:
     raise newException(Defect, "Failed to initialize libcurl.")
   ssock = initServerSocket()
@@ -110,18 +108,22 @@ proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter, coo
       of LOAD:
         var request: Request
         stream.sread(request)
-        if not filter.match(request.url):
+        if not config.filter.match(request.url):
           stream.swrite(-1) # error
           stream.flush()
         else:
-          for k, v in defaultHeaders.table:
+          for k, v in config.defaultHeaders.table:
             if k notin request.headers.table:
               request.headers.table[k] = v
-          if cookiejar != nil and cookiejar.cookies.len > 0:
+          if config.cookiejar != nil and config.cookiejar.cookies.len > 0:
             if "Cookie" notin request.headers.table:
-              let cookie = cookiejar.serialize(request.url)
+              let cookie = config.cookiejar.serialize(request.url)
               if cookie != "":
                 request.headers["Cookie"] = cookie
+          if request.referer != nil and "Referer" notin request.headers.table:
+            let r = getReferer(request.referer, request.url, config.referrerpolicy)
+            if r != "":
+              request.headers["Referer"] = r
           loadResource(request, stream)
         stream.close()
       of QUIT:
diff --git a/src/io/request.nim b/src/io/request.nim
index 7587b2e0..4a0888f3 100644
--- a/src/io/request.nim
+++ b/src/io/request.nim
@@ -25,6 +25,7 @@ type
     headers*: HeaderList
     body*: Option[string]
     multipart*: Option[MimeData]
+    referer*: URL
 
   Response* = ref object
     body*: Stream
diff --git a/src/io/urlfilter.nim b/src/io/urlfilter.nim
index 28bdef28..9541a699 100644
--- a/src/io/urlfilter.nim
+++ b/src/io/urlfilter.nim
@@ -7,11 +7,11 @@ import types/url
 type URLFilter* = object
   scheme: Option[string]
   allowhost*: Option[string]
-  allowhosts: Option[seq[Regex]]
+  allowhosts: seq[Regex]
   default: bool
 
 proc newURLFilter*(scheme = none(string), allowhost = none(string),
-                   allowhosts = none(seq[Regex]), default = false): URLFilter =
+                   allowhosts: seq[Regex] = @[], default = false): URLFilter =
   return URLFilter(
     scheme: scheme,
     allowhost: allowhost,
@@ -29,8 +29,7 @@ proc match*(filter: URLFilter, url: URL): bool =
   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
+  for regex in filter.allowhosts:
+    if regex.match(host):
+      return true
   return filter.default