about summary refs log tree commit diff stats
path: root/src/buffer
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/buffer
parent4b482418c22ea31729ca94583ffd2a6aa167d811 (diff)
downloadchawan-e80ae4b136fe82f916868e1e9b728d69c403a70a.tar.gz
Add referer support, re-render on windowChange
Diffstat (limited to 'src/buffer')
-rw-r--r--src/buffer/buffer.nim22
-rw-r--r--src/buffer/container.nim26
2 files changed, 37 insertions, 11 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index 11492834..5d69325f 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -37,6 +37,7 @@ import render/rendertext
 import types/buffersource
 import types/color
 import types/cookie
+import types/referer
 import types/url
 import utils/twtstr
 
@@ -137,6 +138,17 @@ proc then*[T](promise: Promise[T], cb: (proc(x: T))): EmptyPromise {.discardable
       promise.stream.sread(promise.res)
     cb(promise.res))
 
+proc then*[T](promise: EmptyPromise, cb: (proc(): Promise[T])): Promise[T] {.discardable.} =
+  if promise == nil: return
+  let next = Promise[T]()
+  promise.then(proc() =
+    let p2 = cb()
+    if p2 != nil:
+      p2.then(proc(x: T) =
+        next.res = x
+        next.cb()))
+  return next
+
 proc then*[T, U](promise: Promise[T], cb: (proc(x: T): Promise[U])): Promise[U] {.discardable.} =
   if promise == nil: return
   let next = Promise[U]()
@@ -530,7 +542,13 @@ proc loadResources(buffer: Buffer, document: Document) =
     for child in elem.children_rev:
       stack.add(child)
 
-type ConnectResult* = tuple[code: int, needsAuth: bool, redirect: Option[URL], contentType: string, cookies: seq[Cookie]] 
+type ConnectResult* = object
+  code*: int
+  needsAuth*: bool
+  redirect*: Option[URL]
+  contentType*: string
+  cookies*: seq[Cookie]
+  referrerpolicy*: Option[ReferrerPolicy]
 
 proc setupSource(buffer: Buffer): ConnectResult =
   if buffer.loaded:
@@ -571,6 +589,8 @@ proc setupSource(buffer: Buffer): ConnectResult =
         let cookie = newCookie(s)
         if cookie != nil:
           result.cookies.add(cookie)
+    if "Referrer-Policy" in response.headers.table:
+      result.referrerpolicy = getReferrerPolicy(response.headers.table["Referrer-Policy"][0])
   buffer.istream = newTeeStream(buffer.istream, buffer.sstream, closedest = false)
   if setct:
     result.contentType = buffer.contenttype
diff --git a/src/buffer/container.nim b/src/buffer/container.nim
index 5c579c27..8fc2261a 100644
--- a/src/buffer/container.nim
+++ b/src/buffer/container.nim
@@ -63,7 +63,7 @@ type
     clear*: bool
 
   Container* = ref object
-    config: BufferConfig
+    config*: BufferConfig
     iface*: BufferInterface
     attrs: WindowAttributes
     width*: int
@@ -96,11 +96,9 @@ type
     hasstart: bool
     redirectdepth*: int
 
-proc newBuffer*(dispatcher: Dispatcher, config: Config, source: BufferSource,
-                cookiejar: CookieJar, title = "",
-                redirectdepth = 0): Container =
+proc newBuffer*(dispatcher: Dispatcher, config: BufferConfig,
+                source: BufferSource, title = "", redirectdepth = 0): Container =
   let attrs = getWindowAttributes(stdout)
-  let config = config.getBufferConfig(source.location, cookiejar)
   let ostream = dispatcher.forkserver.ostream
   let istream = dispatcher.forkserver.istream
   ostream.swrite(FORK_BUFFER)
@@ -652,6 +650,8 @@ proc load*(container: Container) =
         container.triggerEvent(SUCCESS)
         if res.cookies.len > 0 and container.config.cookiejar != nil: # accept cookies
           container.config.cookiejar.cookies.add(res.cookies)
+        if res.referrerpolicy.isSome and container.config.refererfrom:
+          container.config.referrerpolicy = res.referrerpolicy.get
         container.setLoadInfo("Connected to " & $container.source.location & ". Downloading...")
         if res.needsAuth:
           container.triggerEvent(NEEDS_AUTH)
@@ -704,7 +704,7 @@ proc dupeBuffer*(dispatcher: Dispatcher, container: Container, config: Config, l
     contenttype: if contenttype.isSome: contenttype else: container.contenttype,
     clonepid: container.process,
   )
-  container.pipeto = dispatcher.newBuffer(config, source, container.config.cookiejar, container.title)
+  container.pipeto = dispatcher.newBuffer(container.config, source, container.title)
   container.iface.getSource().then(proc() =
     if container.pipeto != nil:
       container.pipeto.load()
@@ -736,10 +736,16 @@ proc click*(container: Container) {.jsfunc.} =
 
 proc windowChange*(container: Container, attrs: WindowAttributes) =
   container.attrs = attrs
-  container.width = attrs.width
-  container.height = attrs.height - 1
-  container.iface.windowChange(attrs).then(proc() =
-    container.needslines = true)
+  if attrs.width != container.width or attrs.height - 1 != container.height:
+    container.width = attrs.width
+    container.height = attrs.height - 1
+    container.iface.windowChange(attrs).then(proc(): auto =
+      container.needslines = true
+      return container.iface.render()
+    ).then(proc(lines: int) =
+      if lines != container.numLines:
+        container.setNumLines(lines, true)
+      container.needslines = true)
 
 proc peek*(container: Container) {.jsfunc.} =
   container.alert($container.source.location)