about summary refs log tree commit diff stats
path: root/src/loader/loader.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-10-01 17:44:48 +0200
committerbptato <nincsnevem662@gmail.com>2023-10-01 18:06:26 +0200
commit136d8d9e5c9a2a0414e4934cc928bca23bfe944b (patch)
treea35f3e30bc9839b612d7382a017c460fb335fd5b /src/loader/loader.nim
parent478db794b90ebd205a6a191369581adbd440fd4e (diff)
downloadchawan-136d8d9e5c9a2a0414e4934cc928bca23bfe944b.tar.gz
Add w3m-cgi-compat option
Diffstat (limited to 'src/loader/loader.nim')
-rw-r--r--src/loader/loader.nim24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index a148b77b..1f313a64 100644
--- a/src/loader/loader.nim
+++ b/src/loader/loader.nim
@@ -103,6 +103,7 @@ type
     acceptProxy*: bool
     cgiDir*: seq[string]
     uriMethodMap*: URIMethodMap
+    w3mCGICompat*: bool
 
   FetchPromise* = Promise[JSResult[Response]]
 
@@ -114,6 +115,16 @@ proc addFd(ctx: LoaderContext, fd: int, flags: int) =
 
 const MaxRewrites = 2 # should be enough? TODO find out what w3m thinks
 
+func canRewriteForCGICompat(ctx: LoaderContext, path: string): bool =
+  if not ctx.config.w3mCGICompat:
+    return false
+  if path.startsWith("/cgi-bin/") or path.startsWith("/$LIB/"):
+    return true
+  for dir in ctx.config.cgiDir:
+    if path.startsWith(dir):
+      return true
+  return false
+
 proc loadResource(ctx: LoaderContext, request: Request, handle: LoaderHandle) =
   var redo = true
   var tries = 0
@@ -121,7 +132,15 @@ proc loadResource(ctx: LoaderContext, request: Request, handle: LoaderHandle) =
     redo = false
     case request.url.scheme
     of "file":
-      handle.loadFilePath(request.url)
+      let path = request.url.path.serialize_unicode()
+      if ctx.canRewriteForCGICompat(path):
+        let newURL = newURL("cgi-bin:" & path & request.url.search)
+        if newURL.isSome:
+          request.url = newURL.get
+          inc tries
+          redo = true
+          continue
+      handle.loadFilePath(request.url, path)
       handle.close()
     of "http", "https":
       let handleData = handle.loadHttp(ctx.curlm, request)
@@ -280,6 +299,9 @@ proc initLoaderContext(fd: cint, config: LoaderConfig): LoaderContext =
     discard sig
     gctx.exitLoader()
   ctx.addFd(int(ctx.ssock.sock.getFd()), CURL_WAIT_POLLIN)
+  for dir in ctx.config.cgiDir.mitems:
+    if dir.len > 0 and dir[^1] != '/':
+      dir &= '/'
   return ctx
 
 proc runFileLoader*(fd: cint, config: LoaderConfig) =