about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--doc/config.md44
-rw-r--r--res/config.toml4
-rw-r--r--src/config/config.nim9
-rw-r--r--src/display/pager.nim23
-rw-r--r--src/utils/twtstr.nim4
5 files changed, 63 insertions, 21 deletions
diff --git a/doc/config.md b/doc/config.md
index e7f10f82..47ac9f99 100644
--- a/doc/config.md
+++ b/doc/config.md
@@ -22,6 +22,7 @@ configuration.
 
 * [start](Start)
 * [external](External)
+* [network](Network)
 * [display](Display)
 * [omnirule](Omnirule)
 * [siteconf](Siteconf)
@@ -32,7 +33,7 @@ configuration.
 
 ## Start
 
-Start-up options are to be placed in the section `[start]`.
+Start-up options are to be placed in the `[start]` section.
 
 Following is a list of start-up options:
 
@@ -69,7 +70,7 @@ called with -r.</td>
 
 ## External
 
-External options are to be placed in the section `[external]`.
+External options are to be placed in the `[external]` section.
 
 Following is a list of external options:
 
@@ -96,9 +97,38 @@ the line number.</td>
 
 </table>
 
+## Network
+
+Network options are to be placed in the `[network]` section.
+
+<table>
+
+<tr>
+<th>**Name**</th>
+<th>**Value**</th>
+<th>**Function**</th>
+</tr>
+
+<tr>
+<td>max-redirect</td>
+<td>number</td>
+<td>Set the color mode. "auto" for automatic detection, "monochrome"
+for black on white, "ansi" for ansi colors, "24bit" for true colors. "8bit"
+is currently unimplemented (and falls back to ansi).</td>
+</tr>
+
+<tr>
+<td>prepend-https</td>
+<td>boolean</td>
+<td>Whether or not cha should attempt loading "raw" URLs without a scheme as
+https (e.g. wikipedia.org as https://wikipedia.org.)</td>
+</tr>
+
+</table>
+
 ## Display
 
-Display options are to be placed in the section `[display]`.
+Display options are to be placed in the `[display]` section.
 
 Following is a list of display options:
 
@@ -164,9 +194,9 @@ cells.</td>
 <tr>
 <td>minimum-contrast</td>
 <td>number</td>
-<td>Specify the minimum number of euclidian distance of the background and
-foreground colors on the RGB plane. -1 disables this function (i.e. allows
-black letters on black background, etc).</td>
+<td>Specify the minimum difference between the luminance (Y) of the background
+and the foreground. -1 disables this function (i.e. allows black letters on
+black background, etc).</td>
 </tr>
 
 <tr>
@@ -288,7 +318,7 @@ added in the future.</td>
 
 ## Stylesheets
 
-User stylesheets must be placed in a section called `[css]`.
+User stylesheets are to be placed in the `[css]` section.
 
 There are two ways to import user stylesheets:
 
diff --git a/res/config.toml b/res/config.toml
index af502275..7918bea3 100644
--- a/res/config.toml
+++ b/res/config.toml
@@ -7,6 +7,10 @@ headless = false
 tmpdir = "/tmp/cha"
 editor = "vi %s +%d"
 
+[network]
+max-redirect = 10
+prepend-https = true
+
 [display]
 color-mode = "auto"
 format-mode = "auto"
diff --git a/src/config/config.nim b/src/config/config.nim
index 16e1c652..e00724df 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -44,6 +44,8 @@ type
 
   Config* = ref ConfigObj
   ConfigObj* = object
+    maxredirect*: int
+    prependhttps*: bool
     termreload*: bool
     nmap*: ActionMap
     lemap*: ActionMap
@@ -237,6 +239,13 @@ proc parseConfig(config: Config, dir: string, t: TomlValue) =
           config.startup = v.s
         of "headless":
           config.headless = v.b
+    of "network":
+      for k, v in v:
+        case k
+        of "max-redirects":
+          config.maxredirect = int(v.i)
+        of "prepend-https":
+          config.prependhttps = v.b
     of "page":
       for k, v in v:
         config.nmap[getRealKey(k)] = v.s
diff --git a/src/display/pager.nim b/src/display/pager.nim
index 66280edc..d1eca1d7 100644
--- a/src/display/pager.nim
+++ b/src/display/pager.nim
@@ -302,6 +302,7 @@ proc redraw(pager: Pager) {.jsfunc.} =
   pager.term.clearCanvas()
 
 proc draw*(pager: Pager) =
+  if pager.container == nil: return
   pager.term.hideCursor()
   if pager.redraw:
     pager.refreshDisplay()
@@ -522,7 +523,6 @@ proc omniRewrite(pager: Pager, s: string): string =
 # * file://$PWD/<file>
 # * https://<url>
 # So we attempt to load both, and see what works.
-# (TODO: make this optional)
 proc loadURL*(pager: Pager, url: string, ctype = none(string)) =
   let url0 = pager.omniRewrite(url)
   let url = if url[0] == '~': expandPath(url0) else: url0
@@ -535,19 +535,15 @@ proc loadURL*(pager: Pager, url: string, ctype = none(string)) =
     pager.gotoURL(newRequest(firstparse.get), prev, ctype)
     return
   var urls: seq[URL]
-  if url[0] != '/':
+  if pager.config.prependhttps and url[0] != '/':
     let pageurl = parseURL("https://" & url)
     if pageurl.isSome: # attempt to load remote page
       urls.add(pageurl.get)
-  let cdir = parseURL("file://" & getCurrentDir() & DirSep)
-  let purl = percentEncode(url, LocalPathPercentEncodeSet)
-  if purl != url:
-    let newurl = parseURL(purl, cdir)
-    if newurl.isSome:
-      urls.add(newurl.get)
-  let localurl = parseURL(url, cdir)
-  if localurl.isSome: # attempt to load local file
-    urls.add(localurl.get)
+  let cdir = parseURL("file://" & percentEncode(getCurrentDir(), LocalPathPercentEncodeSet) & DirSep)
+  let localurl = percentEncode(url, LocalPathPercentEncodeSet)
+  let newurl = parseURL(localurl, cdir)
+  if newurl.isSome:
+    urls.add(newurl.get) # attempt to load local file
   if urls.len == 0:
     pager.alert("Invalid URL " & url)
   else:
@@ -713,12 +709,15 @@ proc handleEvent0(pager: Pager, container: Container, event: ContainerEvent): bo
     if pager.container == container:
       pager.authorize()
   of REDIRECT:
-    if container.redirectdepth < 10:
+    if container.redirectdepth < pager.config.maxredirect:
       let redirect = event.location
       pager.alert("Redirecting to " & $redirect)
       pager.gotoURL(newRequest(redirect), some(container.source.location), replace = container, redirectdepth = container.redirectdepth + 1)
     else:
       pager.alert("Error: maximum redirection depth reached")
+      pager.deleteContainer(container)
+      if pager.container == nil:
+        return false
   of ANCHOR:
     var url2 = newURL(container.source.location)
     url2.hash(event.anchor)
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 1a38e6c5..ff1d7c6a 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -449,9 +449,9 @@ const ComponentPercentEncodeSet* = (UserInfoPercentEncodeSet + {'$'..'&', '+', '
 const ApplicationXWWWFormUrlEncodedSet* = (ComponentPercentEncodeSet + {'!', '\''..')', '~'})
 # used by client
 when defined(windows) or defined(OS2) or defined(DOS):
-  const LocalPathPercentEncodeSet* = (QueryPercentEncodeSet + {'?', ':'})
+  const LocalPathPercentEncodeSet* = (Ascii - AsciiAlpha - AsciiDigit - {'\\', '/'})
 else:
-  const LocalPathPercentEncodeSet* = (QueryPercentEncodeSet + {'?', ':', '\\'})
+  const LocalPathPercentEncodeSet* = (Ascii - AsciiAlpha - AsciiDigit -  {'/'})
 
 proc percentEncode*(append: var string, c: char, set: set[char], spaceAsPlus = false) {.inline.} =
   if spaceAsPlus and c == ' ':