about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-01-08 00:37:54 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-08 00:37:54 +0100
commitb0547ba9f48bf402665b89f84b88b80ee58d8824 (patch)
tree39bf645df6ea365f912040255eb136c5deafe2b1 /src
parent10a02b2ae2613b383453ba99318330560e9371ac (diff)
downloadchawan-b0547ba9f48bf402665b89f84b88b80ee58d8824.tar.gz
Add urlenc, urldec; fix a URL encoding bug; improve trans.cgi
* Fix incorrect internal definition of the fragment percent-encode set
* urlenc, urldec: these are simple utility programs mainly for use
  with shell local CGI scripts. (Sadly the printf + xargs solution is
  not portable.)
* Pass libexec directory as an env var to local CGI scripts
* Update trans.cgi to use urldec and add an example for combining
  it with selections
Diffstat (limited to 'src')
-rw-r--r--src/config/chapath.nim2
-rw-r--r--src/loader/cgi.nim2
-rw-r--r--src/utils/twtstr.nim30
3 files changed, 21 insertions, 13 deletions
diff --git a/src/config/chapath.nim b/src/config/chapath.nim
index adf852a0..a2800c6c 100644
--- a/src/config/chapath.nim
+++ b/src/config/chapath.nim
@@ -9,7 +9,7 @@ import js/tojs
 import types/opt
 import utils/twtstr
 
-const libexecPath {.strdefine.} = "${%CHA_BIN_DIR}/../libexec/chawan"
+const libexecPath* {.strdefine.} = "${%CHA_BIN_DIR}/../libexec/chawan"
 
 type ChaPath* = distinct string
 
diff --git a/src/loader/cgi.nim b/src/loader/cgi.nim
index 1da8fc4d..76076b1c 100644
--- a/src/loader/cgi.nim
+++ b/src/loader/cgi.nim
@@ -4,6 +4,7 @@ import std/posix
 import std/streams
 import std/strutils
 
+import config/chapath
 import extern/stdio
 import io/posixstream
 import loader/connecterror
@@ -38,6 +39,7 @@ proc setupEnv(cmd, scriptName, pathInfo, requestURI: string, request: Request,
   putEnv("SCRIPT_FILENAME", cmd)
   putEnv("REQUEST_URI", requestURI)
   putEnv("REQUEST_METHOD", $request.httpMethod)
+  putEnv("CHA_LIBEXEC_DIR", libexecPath)
   var headers = ""
   for k, v in request.headers:
     headers &= k & ": " & v & "\r\n"
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index d7e5a5ba..f1af1998 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -491,19 +491,25 @@ func isNonCharacter*(r: Rune): bool =
         0xCFFFE, 0xCFFFF, 0xDFFFE, 0xDFFFF, 0xEFFFE, 0xEFFFF, 0xFFFFE, 0xFFFFF,
         0x10FFFE, 0x10FFFF]
 
-const ControlPercentEncodeSet* = (Controls + NonAscii)
-const FragmentPercentEncodeSet* = (Controls + NonAscii)
-const QueryPercentEncodeSet* = (ControlPercentEncodeSet + {' ', '"', '#', '<', '>'})
-const SpecialQueryPercentEncodeSet* = (QueryPercentEncodeSet + {'\''})
-const PathPercentEncodeSet* = (QueryPercentEncodeSet + {'?', '`', '{', '}'})
-const UserInfoPercentEncodeSet* = (PathPercentEncodeSet + {'/', ':', ';', '=', '@', '['..'^', '|'})
-const ComponentPercentEncodeSet* = (UserInfoPercentEncodeSet + {'$'..'&', '+', ','})
-const ApplicationXWWWFormUrlEncodedSet* = (ComponentPercentEncodeSet + {'!', '\''..')', '~'})
-# used by client
-when defined(windows) or defined(OS2) or defined(DOS):
-  const LocalPathPercentEncodeSet* = (Ascii - AsciiAlpha - AsciiDigit - {'.', '\\', '/'})
+const ControlPercentEncodeSet* = Controls + NonAscii
+const FragmentPercentEncodeSet* = ControlPercentEncodeSet +
+  {' ', '"', '<', '>', '`'}
+const QueryPercentEncodeSet* = FragmentPercentEncodeSet - {'`'} + {'#'}
+const SpecialQueryPercentEncodeSet* = QueryPercentEncodeSet + {'\''}
+const PathPercentEncodeSet* = QueryPercentEncodeSet + {'?', '`', '{', '}'}
+const UserInfoPercentEncodeSet* = PathPercentEncodeSet +
+  {'/', ':', ';', '=', '@', '['..'^', '|'}
+const ComponentPercentEncodeSet* = UserInfoPercentEncodeSet +
+  {'$'..'&', '+', ','}
+const ApplicationXWWWFormUrlEncodedSet* = ComponentPercentEncodeSet +
+  {'!', '\''..')', '~'}
+# used by pager
+when DirSep == '\\':
+  const LocalPathPercentEncodeSet* = Ascii - AsciiAlpha - AsciiDigit -
+    {'.', '\\', '/'}
 else:
-  const LocalPathPercentEncodeSet* = (Ascii - AsciiAlpha - AsciiDigit -  {'.', '/'})
+  const LocalPathPercentEncodeSet* = Ascii - AsciiAlpha - AsciiDigit -
+    {'.', '/'}
 
 proc percentEncode*(append: var string, c: char, set: set[char], spaceAsPlus = false) {.inline.} =
   if spaceAsPlus and c == ' ':