-
Notifications
You must be signed in to change notification settings - Fork 0
/
externals.nim
65 lines (61 loc) · 1.86 KB
/
externals.nim
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##
## Some things that I've copied from libraries, rather than bring in the
## whole library. Just to try and make the executable smaller
##
import json, strutils
#### cgi
#### FROM: https://github.com/nim-lang/Nim/blob/master/lib/pure/cgi.nim#L55
####
proc handleHexChar(c: char, x: var int) {.inline.} =
case c
of '0'..'9': x = (x shl 4) or (ord(c) - ord('0'))
of 'a'..'f': x = (x shl 4) or (ord(c) - ord('a') + 10)
of 'A'..'F': x = (x shl 4) or (ord(c) - ord('A') + 10)
else: assert(false)
proc decodeUrl*(s: string): string =
## Decodes a value from its HTTP representation: This means that a ``'+'``
## is converted to a space, ``'%xx'`` (where ``xx`` denotes a hexadecimal
## value) is converted to the character with ordinal number ``xx``, and
## and every other character is carried over.
result = newString(s.len)
var i = 0
var j = 0
while i < s.len:
case s[i]
of '%':
var x = 0
handleHexChar(s[i+1], x)
handleHexChar(s[i+2], x)
inc(i, 2)
result[j] = chr(x)
of '+': result[j] = ' '
else: result[j] = s[i]
inc(i)
inc(j)
setLen(result, j)
####
#### END FROM: https://github.com/nim-lang/Nim/blob/master/lib/pure/cgi.nim#L55
#### httpform
#### FROM: https://github.com/tulayang/httpform/blob/master/httpform.nim#L22
####
proc parseUrlencoded*(body: string): JsonNode {.inline.} =
var
a: seq[string]
i, h: int
result = newJObject()
for s in body.split('&'):
if s.len() == 0 or s == "=":
result[""] = newJString("")
else:
i = s.find('=')
h = s.high()
if i == -1:
result[s] = newJString("")
elif i == 0:
result[""] = newJString(s[i+1..h])
elif i == h:
result[s[0..h-1]] = newJString("")
else:
result[s[0..i-1]] = newJString(s[i+1..h])
####
#### END FROM: https://github.com/tulayang/httpform/blob/master/httpform.nim#L22