about summary refs log tree commit diff stats
path: root/adapter/tools/urlenc.nim
blob: 79d3e4520da0fd52fd08213287ca071c4fbc1ca3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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()