about summary refs log tree commit diff stats
path: root/src/buffer
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-13 00:04:50 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-13 00:04:50 +0100
commitd9e430c8147c8c2d81b4ca5405786269b2cfc94d (patch)
tree91ddb6edbedc6e8310fedc13106b8b3bdcec2cdc /src/buffer
parent7a3fb32bff44a581d6926bfaf5f070f8ef062338 (diff)
downloadchawan-d9e430c8147c8c2d81b4ca5405786269b2cfc94d.tar.gz
Add all sorts of config options and cookies
Diffstat (limited to 'src/buffer')
-rw-r--r--src/buffer/buffer.nim8
-rw-r--r--src/buffer/container.nim25
2 files changed, 24 insertions, 9 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index 9f699ca3..b692e253 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -36,6 +36,7 @@ import render/renderdocument
 import render/rendertext
 import types/buffersource
 import types/color
+import types/cookie
 import types/url
 import utils/twtstr
 
@@ -528,7 +529,7 @@ 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] 
+type ConnectResult* = tuple[code: int, needsAuth: bool, redirect: Option[URL], contentType: string, cookies: seq[Cookie]] 
 
 proc setupSource(buffer: Buffer): ConnectResult =
   if buffer.loaded:
@@ -564,6 +565,11 @@ proc setupSource(buffer: Buffer): ConnectResult =
     SocketStream(buffer.istream).source.getFd().setBlocking(false)
     result.needsAuth = response.status == 401 # Unauthorized
     result.redirect = response.redirect
+    if "Set-Cookie" in response.headers.table:
+      for s in response.headers.table["Set-Cookie"]:
+        let cookie = newCookie(s)
+        if cookie != nil:
+          result.cookies.add(cookie)
   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 83bb6c8c..a148e0e1 100644
--- a/src/buffer/container.nim
+++ b/src/buffer/container.nim
@@ -1,3 +1,4 @@
+import deques
 import options
 import streams
 import strformat
@@ -17,6 +18,7 @@ import ips/socketstream
 import js/javascript
 import js/regex
 import types/buffersource
+import types/cookie
 import types/dispatcher
 import types/url
 import utils/twtstr
@@ -32,7 +34,8 @@ type
 
   ContainerEventType* = enum
     NO_EVENT, FAIL, SUCCESS, NEEDS_AUTH, REDIRECT, ANCHOR, NO_ANCHOR, UPDATE,
-    READ_LINE, READ_AREA, OPEN, INVALID_COMMAND, STATUS, ALERT, LOADED
+    READ_LINE, READ_AREA, OPEN, INVALID_COMMAND, STATUS, ALERT, LOADED,
+    SET_COOKIE
 
   ContainerEvent* = object
     case t*: ContainerEventType
@@ -52,6 +55,8 @@ type
       msg*: string
     of UPDATE:
       force*: bool
+    of SET_COOKIE:
+      cookies*: seq[Cookie]
     else: discard
 
   Highlight* = ref object
@@ -61,8 +66,9 @@ type
     clear*: bool
 
   Container* = ref object
+    config: BufferConfig
     iface*: BufferInterface
-    attrs*: WindowAttributes
+    attrs: WindowAttributes
     width*: int
     height*: int
     contenttype*: Option[string]
@@ -88,24 +94,25 @@ type
     redraw*: bool
     needslines*: bool
     canceled: bool
-    events*: seq[ContainerEvent]
+    events*: Deque[ContainerEvent]
     startpos: Option[CursorPosition]
     hasstart: bool
 
-proc newBuffer*(dispatcher: Dispatcher, config: Config, source: BufferSource, title = ""): Container =
+proc newBuffer*(dispatcher: Dispatcher, config: Config, source: BufferSource, cookiejar: CookieJar, title = ""): 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)
   ostream.swrite(source)
-  ostream.swrite(config.getBufferConfig(source.location))
+  ostream.swrite(config)
   ostream.swrite(attrs)
   ostream.swrite(dispatcher.mainproc)
   ostream.flush()
   result = Container(
     source: source, attrs: attrs, width: attrs.width,
     height: attrs.height - 1, contenttype: source.contenttype,
-    title: title
+    title: title, config: config
   )
   istream.sread(result.process)
   result.pos.setx = -1
@@ -250,7 +257,7 @@ func findHighlights*(container: Container, y: int): seq[Highlight] =
       result.add(hl)
 
 proc triggerEvent(container: Container, event: ContainerEvent) =
-  container.events.add(event)
+  container.events.addLast(event)
 
 proc triggerEvent(container: Container, t: ContainerEventType) =
   container.triggerEvent(ContainerEvent(t: t))
@@ -638,6 +645,8 @@ proc load*(container: Container) =
       container.code = res.code
       if res.code == 0:
         container.triggerEvent(SUCCESS)
+        if res.cookies.len > 0 and container.config.cookiejar != nil: # accept cookies
+          container.triggerEvent(ContainerEvent(t: SET_COOKIE, cookies: res.cookies))
         container.setLoadInfo("Connected to " & $container.source.location & ". Downloading...")
         if res.needsAuth:
           container.triggerEvent(NEEDS_AUTH)
@@ -690,7 +699,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.title)
+  container.pipeto = dispatcher.newBuffer(config, source, container.config.cookiejar, container.title)
   container.iface.getSource().then(proc() =
     if container.pipeto != nil:
       container.pipeto.load()