about summary refs log tree commit diff stats
path: root/adapter/tools
diff options
context:
space:
mode:
Diffstat (limited to 'adapter/tools')
-rw-r--r--adapter/tools/urldec.nim6
-rw-r--r--adapter/tools/urlenc.nim41
2 files changed, 47 insertions, 0 deletions
diff --git a/adapter/tools/urldec.nim b/adapter/tools/urldec.nim
new file mode 100644
index 00000000..b26fcb68
--- /dev/null
+++ b/adapter/tools/urldec.nim
@@ -0,0 +1,6 @@
+# Percent-decode input received on stdin.
+#TODO a streaming implementation of this could be useful
+
+import utils/twtstr
+
+stdout.write(percentDecode(stdin.readAll()))
diff --git a/adapter/tools/urlenc.nim b/adapter/tools/urlenc.nim
new file mode 100644
index 00000000..79d3e452
--- /dev/null
+++ b/adapter/tools/urlenc.nim
@@ -0,0 +1,41 @@
+# Percent-encode input received on stdin with a specified percent-encoding set.
+#TODO a streaming implementation of this could be useful
+
+import std/os
+
+import utils/twtstr
+
+proc usage() =
+  stderr.write("""
+Usage: urlenc [set]
+The input to be decoded is read from stdin.
+[set] decides which characters are encoded. It can be:
+    control
+    fragment
+    query
+    path
+    userinfo
+    component
+    application-x-www-form-urlencoded
+""")
+
+proc main() =
+  if paramCount() != 1:
+    usage()
+    quit(1)
+  let s = stdin.readAll()
+  let enc = case paramStr(1)
+  of "control": percentEncode(s, ControlPercentEncodeSet)
+  of "fragment": percentEncode(s, FragmentPercentEncodeSet)
+  of "query": percentEncode(s, QueryPercentEncodeSet)
+  of "path": percentEncode(s, PathPercentEncodeSet)
+  of "userinfo": percentEncode(s, UserInfoPercentEncodeSet)
+  of "component": percentEncode(s, ComponentPercentEncodeSet)
+  of "application-x-www-form-urlencoded":
+    percentEncode(s, ApplicationXWWWFormUrlEncodedSet)
+  else:
+    usage()
+    quit(1)
+  stdout.write(enc)
+
+main()