about summary refs log tree commit diff stats
path: root/src/local
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-22 22:17:53 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-22 22:24:27 +0100
commit6f68626e202f93991f678086634692e6366958c6 (patch)
tree411bf8bf78aaae5dc391344370cbb2350de4b884 /src/local
parentb2da391458bca7b7f38fc9f03925d704841165c8 (diff)
downloadchawan-6f68626e202f93991f678086634692e6366958c6.tar.gz
buffer: clean up contentType
This fixes a bug where setContentType would call setHTML twice, which
messed up charsets and probably a couple more things. As a bonus, it
allows us to pass around the content type less.

In fact, buffer does not have to know its exact content type, just
whether it is in HTML mode or not. So that's all we tell it now;
only container still keeps track of the content type (as it should).
Diffstat (limited to 'src/local')
-rw-r--r--src/local/container.nim34
-rw-r--r--src/local/pager.nim28
2 files changed, 36 insertions, 26 deletions
diff --git a/src/local/container.nim b/src/local/container.nim
index e61d4fbf..4114566b 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -101,6 +101,9 @@ type
     hovertext: array[HoverType, string]
     lastpeek: HoverType
     source*: BufferSource
+    # if set, this *overrides* any content type received from the network. (this
+    # is because it stores the content type from the -T flag.)
+    contentType* {.jsget.}: Option[string]
     pos: CursorPosition
     bpos: seq[CursorPosition]
     highlights: seq[Highlight]
@@ -138,7 +141,8 @@ jsDestructor(Container)
 
 proc newBuffer*(forkserver: ForkServer, config: BufferConfig,
     source: BufferSource, attrs: WindowAttributes, title = "",
-    redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1)): Container =
+    redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1),
+    contentType: Option[string]): Container =
   let (process, loaderPid) = forkserver.forkBuffer(source, config, attrs)
   if fd != -1:
     loaderPid.passFd(source.request.url.host, fd)
@@ -152,6 +156,7 @@ proc newBuffer*(forkserver: ForkServer, config: BufferConfig,
     process: process,
     loaderPid: loaderPid,
     source: source,
+    contentType: contentType,
     width: attrs.width,
     height: attrs.height - 1,
     title: title,
@@ -165,8 +170,8 @@ proc newBuffer*(forkserver: ForkServer, config: BufferConfig,
 
 proc newBufferFrom*(forkserver: ForkServer, attrs: WindowAttributes,
     container: Container, contentTypeOverride: string): Container =
+  container.contentType = some(contentTypeOverride)
   var source = container.source
-  source.contentType = some(contentTypeOverride)
   source.request = newRequest(source.request.url, fromcache = true)
   let config = container.config
   let loaderPid = container.loaderPid
@@ -236,9 +241,6 @@ proc clone*(container: Container, newurl: URL): Promise[Container] =
 func charset*(container: Container): Charset =
   return container.source.charset
 
-func contentType*(container: Container): Option[string] {.jsfget.} =
-  return container.source.contentType
-
 func lineLoaded(container: Container, y: int): bool =
   return y - container.lineshift in 0..container.lines.high
 
@@ -1396,15 +1398,17 @@ proc load(container: Container) =
         if res.redirect != nil:
           container.triggerEvent(ContainerEvent(t: REDIRECT, request: res.redirect))
         container.source.charset = res.charset
-        container.ishtml = res.contentType == "text/html"
-        if res.contentType == "application/octet-stream":
-          let contentType = guessContentType(container.location.pathname,
-            "application/octet-stream", container.config.mimeTypes)
-          if contentType != "application/octet-stream":
-            container.iface.setContentType(contentType)
-          container.source.contentType = some(contentType)
-        else:
-          container.source.contentType = some(res.contentType)
+        if container.contentType.isNone:
+          if res.contentType == "application/octet-stream":
+            let contentType = guessContentType(container.location.pathname,
+              "application/octet-stream", container.config.mimeTypes)
+            if contentType != "application/octet-stream":
+              container.contentType = some(contentType)
+            else:
+              container.contentType = some(res.contentType)
+          else:
+            container.contentType = some(res.contentType)
+        container.ishtml = container.contentType.get == "text/html"
         container.triggerEvent(CHECK_MAILCAP)
       else:
         if res.errorMessage != "":
@@ -1423,7 +1427,7 @@ proc startload*(container: Container) =
       container.onload(res))
 
 proc connect2*(container: Container): EmptyPromise =
-  return container.iface.connect2()
+  return container.iface.connect2(container.ishtml)
 
 proc redirectToFd*(container: Container, fdin: FileHandle, wait, cache: bool):
     EmptyPromise =
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 5d84aed5..37d6ada8 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -452,8 +452,8 @@ proc addContainer*(pager: Pager, container: Container) =
   pager.setContainer(container)
 
 proc newBuffer(pager: Pager, bufferConfig: BufferConfig, source: BufferSource,
-    title = "", redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1)):
-    Container =
+    title = "", redirectdepth = 0, canreinterpret = true, fd = FileHandle(-1),
+    contentType = none(string)): Container =
   return newBuffer(
     pager.forkserver,
     bufferConfig,
@@ -462,7 +462,8 @@ proc newBuffer(pager: Pager, bufferConfig: BufferConfig, source: BufferSource,
     title,
     redirectdepth,
     canreinterpret,
-    fd
+    fd,
+    contentType
   )
 
 proc dupeBuffer(pager: Pager, container: Container, location: URL) =
@@ -683,7 +684,7 @@ proc applySiteconf(pager: Pager, url: var URL): BufferConfig =
 
 # Load request in a new buffer.
 proc gotoURL(pager: Pager, request: Request, prevurl = none(URL),
-    ctype = none(string), cs = CHARSET_UNKNOWN, replace: Container = nil,
+    contentType = none(string), cs = CHARSET_UNKNOWN, replace: Container = nil,
     redirectdepth = 0, referrer: Container = nil) =
   if referrer != nil and referrer.config.referer_from:
     request.referer = referrer.location
@@ -698,7 +699,6 @@ proc gotoURL(pager: Pager, request: Request, prevurl = none(URL),
     # feedback on what is actually going to happen when typing a URL; TODO.
     let source = BufferSource(
       request: request,
-      contentType: ctype,
       charset: cs
     )
     if referrer != nil:
@@ -706,7 +706,8 @@ proc gotoURL(pager: Pager, request: Request, prevurl = none(URL),
     let container = pager.newBuffer(
       bufferconfig,
       source,
-      redirectdepth = redirectdepth
+      redirectdepth = redirectdepth,
+      contentType = contentType
     )
     if replace != nil:
       container.replace = replace
@@ -759,7 +760,7 @@ proc loadURL*(pager: Pager, url: string, ctype = none(string),
     pager.alert("Invalid URL " & url)
   else:
     let prevc = pager.container
-    pager.gotoURL(newRequest(urls.pop()), ctype = ctype, cs = cs)
+    pager.gotoURL(newRequest(urls.pop()), contentType = ctype, cs = cs)
     if pager.container != prevc:
       pager.container.retry = urls
 
@@ -770,11 +771,16 @@ proc readPipe0*(pager: Pager, ctype: Option[string], cs: Charset,
   let bufferconfig = pager.applySiteconf(location)
   let source = BufferSource(
     request: newRequest(location),
-    contentType: some(ctype.get("text/plain")),
     charset: cs
   )
-  return pager.newBuffer(bufferconfig, source, title = title,
-    canreinterpret = canreinterpret, fd = fd)
+  return pager.newBuffer(
+    bufferconfig,
+    source,
+    title = title,
+    canreinterpret = canreinterpret,
+    fd = fd,
+    contentType = some(ctype.get("text/plain"))
+  )
 
 proc readPipe*(pager: Pager, ctype: Option[string], cs: Charset, fd: FileHandle,
     title: string) =
@@ -1189,7 +1195,7 @@ proc handleEvent0(pager: Pager, container: Container, event: ContainerEvent): bo
     pager.deleteContainer(container)
     if container.retry.len > 0:
       pager.gotoURL(newRequest(container.retry.pop()),
-        ctype = container.contentType)
+        contentType = container.contentType)
     else:
       pager.alert("Can't load " & $container.location & " (" &
         container.errorMessage & ")")