about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-08-26 10:58:10 +0200
committerbptato <nincsnevem662@gmail.com>2023-08-26 10:58:10 +0200
commite5d170aa66a9fdc2ba2258f42d054d7d998e5915 (patch)
treefe398035de20112de76701b95c695303638ca6f1
parenta11e9468001f19ac3874bec540a5a9cbd94739bc (diff)
downloadchawan-e5d170aa66a9fdc2ba2258f42d054d7d998e5915.tar.gz
Allow overriding default headers
-rw-r--r--doc/config.md5
-rw-r--r--res/config.toml7
-rw-r--r--src/config/config.nim14
-rw-r--r--src/display/client.nim1
-rw-r--r--src/display/pager.nim3
-rw-r--r--src/ips/forkserver.nim5
6 files changed, 19 insertions, 16 deletions
diff --git a/doc/config.md b/doc/config.md
index 6d90f947..0f986de4 100644
--- a/doc/config.md
+++ b/doc/config.md
@@ -207,6 +207,11 @@ https (e.g. wikipedia.org as https://wikipedia.org.)</td>
 supported by cURL may be used. Can be overridden by siteconf.</td>
 </tr>
 
+<tr>
+<td>default-headers</td>
+<td>Table</td>
+<td>Specify a list of default headers for all HTTP(S) network requests.</td>
+
 </table>
 
 ## Display
diff --git a/res/config.toml b/res/config.toml
index eac589c3..c8b0091a 100644
--- a/res/config.toml
+++ b/res/config.toml
@@ -30,6 +30,13 @@ editor = "vi %s +%d"
 [network]
 max-redirect = 10
 prepend-https = true
+default-headers = {
+	User-Agent = "chawan",
+	Accept = "text/html,text/*;q=0.5",
+	Accept-Language = "en;q=1.0",
+	Pragma = "no-cache",
+	Cache-Control = "no-cache"
+}
 
 [display]
 color-mode = "auto"
diff --git a/src/config/config.nim b/src/config/config.nim
index e5dbad9c..afffdf23 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -90,6 +90,7 @@ type
     max_redirect*: int32
     prepend_https*: bool
     proxy*: Opt[string]
+    default_headers*: Table[string, string]
 
   DisplayConfig = object
     color_mode*: Opt[ColorMode]
@@ -141,14 +142,6 @@ type
 
 jsDestructor(Config)
 
-const DefaultHeaders* = {
-  "User-Agent": "chawan",
-  "Accept": "text/html,text/*;q=0.5",
-  "Accept-Language": "en;q=1.0",
-  "Pragma": "no-cache",
-  "Cache-Control": "no-cache",
-}.toTable().newHeaders()[]
-
 func getForkServerConfig*(config: Config): ForkServerConfig =
   return ForkServerConfig(
     tmpdir: config.external.tmpdir,
@@ -165,6 +158,9 @@ func getProxy*(config: Config): URL =
       raise newException(Defect, "Invalid proxy URL: " & s)
   return nil
 
+func getDefaultHeaders*(config: Config): Headers =
+  return newHeaders(config.network.default_headers)
+
 proc getBufferConfig*(config: Config, location: URL, cookiejar: CookieJar,
     headers: Headers, referer_from, scripting: bool, charsets: seq[Charset],
     images: bool, userstyle: string, proxy: URL, mimeTypes: MimeTypes):
@@ -181,8 +177,6 @@ proc getBufferConfig*(config: Config, location: URL, cookiejar: CookieJar,
     proxy: proxy,
     mimeTypes: mimeTypes
   )
-  new(result.headers)
-  result.headers[] = DefaultHeaders
 
 proc getSiteConfig*(config: Config, jsctx: JSContext): seq[SiteConfig] =
   for sc in config.siteconf:
diff --git a/src/display/client.nim b/src/display/client.nim
index 4d0d59f2..b78088e2 100644
--- a/src/display/client.nim
+++ b/src/display/client.nim
@@ -600,6 +600,7 @@ proc newClient*(config: Config, forkserver: ForkServer, mainproc: Pid): Client =
     mainproc: mainproc,
     attrs: attrs,
     loader: forkserver.newFileLoader(
+      defaultHeaders = config.getDefaultHeaders(),
       proxy = config.getProxy(),
       acceptProxy = true
     ),
diff --git a/src/display/pager.nim b/src/display/pager.nim
index ba9a8453..c39a0650 100644
--- a/src/display/pager.nim
+++ b/src/display/pager.nim
@@ -18,7 +18,6 @@ import config/mailcap
 import config/mimetypes
 import display/term
 import io/connecterror
-import io/headers
 import io/lineedit
 import io/loader
 import io/promise
@@ -582,7 +581,7 @@ proc applySiteconf(pager: Pager, url: var URL): BufferConfig =
   let host = url.host
   var referer_from: bool
   var cookiejar: CookieJar
-  var headers: Headers
+  var headers = pager.config.getDefaultHeaders()
   var scripting: bool
   var images: bool
   var charsets = pager.config.encoding.document_charset
diff --git a/src/ips/forkserver.nim b/src/ips/forkserver.nim
index ec0c60d1..681b3aa5 100644
--- a/src/ips/forkserver.nim
+++ b/src/ips/forkserver.nim
@@ -34,14 +34,11 @@ type
     ostream: Stream
     children: seq[(Pid, Pid)]
 
-proc newFileLoader*(forkserver: ForkServer, defaultHeaders: Headers = nil,
+proc newFileLoader*(forkserver: ForkServer, defaultHeaders: Headers,
     filter = newURLFilter(default = true), cookiejar: CookieJar = nil,
     proxy: URL = nil, acceptProxy = false): FileLoader =
   forkserver.ostream.swrite(FORK_LOADER)
   var defaultHeaders = defaultHeaders
-  if defaultHeaders == nil:
-    new(defaultHeaders)
-    defaultHeaders[] = DefaultHeaders
   let config = LoaderConfig(
     defaultHeaders: defaultHeaders,
     filter: filter,