about summary refs log tree commit diff stats
path: root/src/local
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-15 00:41:17 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-15 00:41:54 +0100
commitb175d8200561f6afc894b40aaf70502ee529aed8 (patch)
tree8c2998ab4edce764eced2f5dfa3f5b03b557516c /src/local
parent880685a1c3d7b0e4ac386783489512b61aeb806c (diff)
downloadchawan-b175d8200561f6afc894b40aaf70502ee529aed8.tar.gz
Clean up BufferConfig
It was defined in the wrong module, and unnecessarily included
LoaderClientConfig.

Also, referrerPolicy was not being propagated to loader clients because
it was (incorrectly) in BufferConfig instead of LoaderClientConfig.
Diffstat (limited to 'src/local')
-rw-r--r--src/local/client.nim3
-rw-r--r--src/local/container.nim25
-rw-r--r--src/local/pager.nim69
3 files changed, 61 insertions, 36 deletions
diff --git a/src/local/client.nim b/src/local/client.nim
index 6b52536e..2c5963aa 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -487,8 +487,7 @@ proc acceptBuffers(client: Client) =
     if stream == nil:
       pager.alert("Error: failed to set up buffer")
       continue
-    let key = pager.addLoaderClient(container.process,
-      container.config.loaderConfig)
+    let key = pager.addLoaderClient(container.process, container.loaderConfig)
     stream.swrite(key)
     let loader = pager.loader
     if item.fdin != -1:
diff --git a/src/local/container.nim b/src/local/container.nim
index 6661380f..21981049 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -6,6 +6,7 @@ when defined(posix):
   import std/posix
 
 import config/config
+import config/mimetypes
 import io/promise
 import io/serialize
 import io/socketstream
@@ -104,6 +105,7 @@ type
     parent* {.jsget.}: Container
     children* {.jsget.}: seq[Container]
     config*: BufferConfig
+    loaderConfig*: LoaderClientConfig
     iface*: BufferInterface
     width* {.jsget.}: int
     height* {.jsget.}: int
@@ -133,7 +135,7 @@ type
     events*: Deque[ContainerEvent]
     startpos: Option[CursorPosition]
     hasstart: bool
-    redirectdepth*: int
+    redirectDepth*: int
     select*: Select
     canreinterpret*: bool
     cloned: bool
@@ -150,9 +152,9 @@ type
 jsDestructor(Highlight)
 jsDestructor(Container)
 
-proc newContainer*(config: BufferConfig; url: URL; request: Request;
-    attrs: WindowAttributes; title: string; redirectdepth: int;
-    canreinterpret: bool; contentType: Option[string];
+proc newContainer*(config: BufferConfig; loaderConfig: LoaderClientConfig;
+    url: URL; request: Request; attrs: WindowAttributes; title: string;
+    redirectDepth: int; canreinterpret: bool; contentType: Option[string];
     charsetStack: seq[Charset]; cacheId: int; cacheFile: string): Container =
   return Container(
     url: url,
@@ -162,7 +164,8 @@ proc newContainer*(config: BufferConfig; url: URL; request: Request;
     height: attrs.height - 1,
     title: title,
     config: config,
-    redirectdepth: redirectdepth,
+    loaderConfig: loaderConfig,
+    redirectDepth: redirectDepth,
     pos: CursorPosition(
       setx: -1
     ),
@@ -1389,25 +1392,25 @@ proc extractReferrerPolicy(response: Response): Option[ReferrerPolicy] =
 
 # Apply data received in response.
 # Note: pager must call this before checkMailcap.
-proc applyResponse*(container: Container; response: Response) =
+proc applyResponse*(container: Container; response: Response;
+    mimeTypes: MimeTypes) =
   container.code = response.res
   # accept cookies
-  let cookieJar = container.config.loaderConfig.cookieJar
+  let cookieJar = container.loaderConfig.cookieJar
   if cookieJar != nil:
     cookieJar.add(response.extractCookies())
   # set referrer policy, if any
   let referrerPolicy = response.extractReferrerPolicy()
   if container.config.referer_from:
     if referrerPolicy.isSome:
-      container.config.referrerPolicy = referrerPolicy.get
+      container.loaderConfig.referrerPolicy = referrerPolicy.get
   else:
-    container.config.referrerPolicy = NO_REFERRER
+    container.loaderConfig.referrerPolicy = NO_REFERRER
   # setup content type; note that isSome means an override so we skip it
   if container.contentType.isNone:
     var contentType = response.getContentType()
     if contentType == "application/octet-stream":
-      contentType = container.config.mimeTypes
-        .guessContentType(container.url.pathname)
+      contentType = mimeTypes.guessContentType(container.url.pathname)
     container.contentType = some(contentType)
   # setup charsets:
   # * override charset
diff --git a/src/local/pager.nim b/src/local/pager.nim
index bc17f8fa..d4c02bca 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -36,6 +36,7 @@ import local/container
 import local/lineedit
 import local/select
 import local/term
+import server/buffer
 import server/forkserver
 import types/cell
 import types/color
@@ -518,32 +519,34 @@ proc onSetLoadInfo(pager: Pager; container: Container) =
       pager.writeStatusMessage(container.loadinfo)
       pager.alertState = pasLoadInfo
 
-proc newContainer(pager: Pager; bufferConfig: BufferConfig; request: Request;
-    title = ""; redirectdepth = 0; canreinterpret = true;
-    contentType = none(string); charsetStack: seq[Charset] = @[];
-    url: URL = request.url; cacheId = -1; cacheFile = ""): Container =
+proc newContainer(pager: Pager; bufferConfig: BufferConfig;
+    loaderConfig: LoaderClientConfig; request: Request; title = "";
+    redirectDepth = 0; canreinterpret = true; contentType = none(string);
+    charsetStack: seq[Charset] = @[]; url = request.url; cacheId = -1;
+    cacheFile = ""): Container =
   request.suspended = true
-  if bufferConfig.loaderConfig.cookieJar != nil:
+  if loaderConfig.cookieJar != nil:
     # loader stores cookie jars per client, but we have no client yet.
     # therefore we must set cookie here
-    let cookie = bufferConfig.loaderConfig.cookieJar.serialize(request.url)
+    let cookie = loaderConfig.cookieJar.serialize(request.url)
     if cookie != "":
       request.headers["Cookie"] = cookie
   if request.referrer != nil:
     # same with referrer
     let r = request.referrer.getReferrer(request.url,
-      bufferConfig.referrerPolicy)
+      loaderConfig.referrerPolicy)
     if r != "":
       request.headers["Referer"] = r
   let stream = pager.loader.startRequest(request)
   pager.loader.registerFun(stream.fd)
   let container = newContainer(
     bufferConfig,
+    loaderConfig,
     url,
     request,
     pager.term.attrs,
     title,
-    redirectdepth,
+    redirectDepth,
     canreinterpret,
     contentType,
     charsetStack,
@@ -563,6 +566,7 @@ proc newContainerFrom(pager: Pager; container: Container; contentType: string):
   let url = newURL("cache:" & $container.cacheId).get
   return pager.newContainer(
     container.config,
+    container.loaderConfig,
     newRequest(url),
     contentType = some(contentType),
     charsetStack = container.charsetStack,
@@ -894,7 +898,8 @@ proc windowChange*(pager: Pager) =
 
 # Apply siteconf settings to a request.
 # Note that this may modify the URL passed.
-proc applySiteconf(pager: Pager; url: var URL; cs: Charset): BufferConfig =
+proc applySiteconf(pager: Pager; url: var URL; charsetOverride: Charset;
+    loaderConfig: var LoaderClientConfig): BufferConfig =
   let host = url.host
   var referer_from = false
   var cookieJar: CookieJar = nil
@@ -904,8 +909,6 @@ proc applySiteconf(pager: Pager; url: var URL; cs: Charset): BufferConfig =
   var charsets = pager.config.encoding.document_charset
   var userstyle = pager.config.css.stylesheet
   var proxy = pager.proxy
-  let mimeTypes = pager.mimeTypes
-  let urimethodmap = pager.urimethodmap
   for sc in pager.siteconf:
     if sc.url.isSome and not sc.url.get.match($url):
       continue
@@ -938,17 +941,34 @@ proc applySiteconf(pager: Pager; url: var URL; cs: Charset): BufferConfig =
       userstyle &= sc.stylesheet.get
     if sc.proxy.isSome:
       proxy = sc.proxy.get
-  return pager.config.getBufferConfig(url, cookieJar, headers, referer_from,
-    scripting, charsets, images, userstyle, proxy, mimeTypes, urimethodmap,
-    pager.cgiDir, pager.tmpdir, cs)
+  loaderConfig = LoaderClientConfig(
+    defaultHeaders: headers,
+    cookiejar: cookieJar,
+    proxy: proxy,
+    filter: newURLFilter(
+      scheme = some(url.scheme),
+      allowschemes = @["data", "cache"],
+      default = true
+    )
+  )
+  return BufferConfig(
+    userstyle: userstyle,
+    referer_from: referer_from,
+    scripting: scripting,
+    charsets: charsets,
+    images: images,
+    isdump: pager.config.start.headless,
+    charsetOverride: charsetOverride,
+  )
 
 # Load request in a new buffer.
 proc gotoURL(pager: Pager, request: Request, prevurl = none(URL),
     contentType = none(string), cs = CHARSET_UNKNOWN, replace: Container = nil,
-    redirectdepth = 0, referrer: Container = nil) =
+    redirectDepth = 0, referrer: Container = nil) =
   if referrer != nil and referrer.config.referer_from:
     request.referrer = referrer.url
-  var bufferConfig = pager.applySiteconf(request.url, cs)
+  var loaderConfig: LoaderClientConfig
+  var bufferConfig = pager.applySiteconf(request.url, cs, loaderConfig)
   if prevurl.isNone or not prevurl.get.equals(request.url, true) or
       request.url.hash == "" or request.httpMethod != HTTP_GET:
     # Basically, we want to reload the page *only* when
@@ -958,11 +978,12 @@ proc gotoURL(pager: Pager, request: Request, prevurl = none(URL),
     # what other browsers do. Still, it would be nice if we got some visual
     # feedback on what is actually going to happen when typing a URL; TODO.
     if referrer != nil:
-      bufferConfig.referrerPolicy = referrer.config.referrerPolicy
+      loaderConfig.referrerPolicy = referrer.loaderConfig.referrerPolicy
     let container = pager.newContainer(
       bufferConfig,
+      loaderConfig,
       request,
-      redirectdepth = redirectdepth,
+      redirectDepth = redirectDepth,
       contentType = contentType
     )
     if replace != nil:
@@ -1027,9 +1048,11 @@ proc readPipe0*(pager: Pager, contentType: string, cs: Charset,
   var url = url
   pager.loader.passFd(url.pathname, fd)
   safeClose(fd)
-  let bufferConfig = pager.applySiteconf(url, cs)
+  var loaderConfig: LoaderClientConfig
+  let bufferConfig = pager.applySiteconf(url, cs, loaderConfig)
   return pager.newContainer(
     bufferConfig,
+    loaderConfig,
     newRequest(url),
     title = title,
     canreinterpret = canreinterpret,
@@ -1483,7 +1506,7 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
 
 proc redirectTo(pager: Pager; container: Container; request: Request) =
   pager.gotoURL(request, some(container.url), replace = container,
-    redirectdepth = container.redirectdepth + 1, referrer = container)
+    redirectDepth = container.redirectDepth + 1, referrer = container)
   pager.container.loadinfo = "Redirecting to " & $request.url
   pager.onSetLoadInfo(pager.container)
   dec pager.numload
@@ -1500,8 +1523,8 @@ proc fail(pager: Pager; container: Container; errorMessage: string) =
 proc redirect(pager: Pager; container: Container; response: Response;
     request: Request) =
   # still need to apply response, or we lose cookie jars.
-  container.applyResponse(response)
-  if container.redirectdepth < pager.config.network.max_redirect:
+  container.applyResponse(response, pager.mimeTypes)
+  if container.redirectDepth < pager.config.network.max_redirect:
     if container.url.scheme == request.url.scheme or
         container.url.scheme == "cgi-bin" or
         container.url.scheme == "http" and request.url.scheme == "https" or
@@ -1519,7 +1542,7 @@ proc redirect(pager: Pager; container: Container; response: Response;
 
 proc connected(pager: Pager; container: Container; response: Response) =
   let istream = response.body
-  container.applyResponse(response)
+  container.applyResponse(response, pager.mimeTypes)
   if response.status == 401: # unauthorized
     pager.authorize()
     istream.close()