about summary refs log tree commit diff stats
path: root/src/loader/cgi.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-12-12 10:38:42 +0100
committerbptato <nincsnevem662@gmail.com>2023-12-12 19:37:14 +0100
commit4eb57c2b88325d3963c6671a6f27bd08fc07cb59 (patch)
tree06bec584dcaf017d94e570e6cffb394b75aca338 /src/loader/cgi.nim
parentf779f672e6aa28efe03733226465c73c1a7490ad (diff)
downloadchawan-4eb57c2b88325d3963c6671a6f27bd08fc07cb59.tar.gz
local CGI: add mapped URI env vars; move about: to adapters
* Add MAPPED_URI_* as environment variables when a request is coming
  from urimethodmap

It costs us compatibility with w3m, but it seems to be a massive
improvement over smuggling in the URL as a query string and then
writing an ad-hoc parser for every single urimethodmap script.

The variables are set for every urimethodmap request, to avoid
accidental leaking of global environment variables.

* Move about: to adapters (an obvious improvement over the previous
  solution)
Diffstat (limited to 'src/loader/cgi.nim')
-rw-r--r--src/loader/cgi.nim19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/loader/cgi.nim b/src/loader/cgi.nim
index b0341a59..592111c9 100644
--- a/src/loader/cgi.nim
+++ b/src/loader/cgi.nim
@@ -14,8 +14,17 @@ import types/opt
 import types/url
 import utils/twtstr
 
+proc putMappedURL(url: URL) =
+  putEnv("MAPPED_URI_SCHEME", url.scheme)
+  putEnv("MAPPED_URI_USERNAME", url.username)
+  putEnv("MAPPED_URI_PASSWORD", url.password)
+  putEnv("MAPPED_URI_HOST", url.hostname)
+  putEnv("MAPPED_URI_PORT", url.port)
+  putEnv("MAPPED_URI_PATH", url.path.serialize())
+  putEnv("MAPPED_URI_QUERY", url.query.get(""))
+
 proc setupEnv(cmd, scriptName, pathInfo, requestURI: string, request: Request,
-    contentLen: int) =
+    contentLen: int, prevURL: URL) =
   let url = request.url
   putEnv("SERVER_SOFTWARE", "Chawan")
   putEnv("SERVER_PROTOCOL", "HTTP/1.0")
@@ -28,6 +37,8 @@ proc setupEnv(cmd, scriptName, pathInfo, requestURI: string, request: Request,
   putEnv("SCRIPT_FILENAME", cmd)
   putEnv("REQUEST_URI", requestURI)
   putEnv("REQUEST_METHOD", $request.httpmethod)
+  if prevURL != nil:
+    putMappedURL(prevURL)
   if pathInfo != "":
     putEnv("PATH_INFO", pathInfo)
   if url.query.isSome:
@@ -47,7 +58,8 @@ proc setupEnv(cmd, scriptName, pathInfo, requestURI: string, request: Request,
       putEnv("HTTPS_proxy", s)
     putEnv("ALL_PROXY", s)
 
-proc loadCGI*(handle: LoaderHandle, request: Request, cgiDir: seq[string]) =
+proc loadCGI*(handle: LoaderHandle, request: Request, cgiDir: seq[string],
+    prevURL: URL) =
   template t(body: untyped) =
     if not body:
       return
@@ -127,7 +139,8 @@ proc loadCGI*(handle: LoaderHandle, request: Request, cgiDir: seq[string]) =
     else:
       closeStdin()
     # we leave stderr open, so it can be seen in the browser console
-    setupEnv(cmd, scriptName, pathInfo, requestURI, request, contentLen)
+    setupEnv(cmd, scriptName, pathInfo, requestURI, request, contentLen,
+      prevURL)
     discard execl(cstring(cmd), cstring(basename), nil)
     stdout.write("Content-Type: text/plain\r\n\r\nFailed to execute script.")
     quit(1)