Moved encodeUrl and decodeUrl from cgi to url
This commit is contained in:
parent
073c2ddd4b
commit
27ea1750e5
2 changed files with 50 additions and 42 deletions
|
|
@ -29,21 +29,8 @@
|
||||||
## writeLine(stdout, "your password: " & myData["password"])
|
## writeLine(stdout, "your password: " & myData["password"])
|
||||||
## writeLine(stdout, "</body></html>")
|
## writeLine(stdout, "</body></html>")
|
||||||
|
|
||||||
import strutils, os, strtabs, cookies
|
import strutils, os, strtabs, cookies, uri
|
||||||
|
export uri.encodeUrl, uri.decodeUrl
|
||||||
proc encodeUrl*(s: string): string =
|
|
||||||
## Encodes a value to be HTTP safe: This means that characters in the set
|
|
||||||
## ``{'A'..'Z', 'a'..'z', '0'..'9', '_'}`` are carried over to the result,
|
|
||||||
## a space is converted to ``'+'`` and every other character is encoded as
|
|
||||||
## ``'%xx'`` where ``xx`` denotes its hexadecimal value.
|
|
||||||
result = newStringOfCap(s.len + s.len shr 2) # assume 12% non-alnum-chars
|
|
||||||
for i in 0..s.len-1:
|
|
||||||
case s[i]
|
|
||||||
of 'a'..'z', 'A'..'Z', '0'..'9', '_': add(result, s[i])
|
|
||||||
of ' ': add(result, '+')
|
|
||||||
else:
|
|
||||||
add(result, '%')
|
|
||||||
add(result, toHex(ord(s[i]), 2))
|
|
||||||
|
|
||||||
proc handleHexChar(c: char, x: var int) {.inline.} =
|
proc handleHexChar(c: char, x: var int) {.inline.} =
|
||||||
case c
|
case c
|
||||||
|
|
@ -52,28 +39,6 @@ proc handleHexChar(c: char, x: var int) {.inline.} =
|
||||||
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)
|
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)
|
|
||||||
|
|
||||||
proc addXmlChar(dest: var string, c: char) {.inline.} =
|
proc addXmlChar(dest: var string, c: char) {.inline.} =
|
||||||
case c
|
case c
|
||||||
of '&': add(dest, "&")
|
of '&': add(dest, "&")
|
||||||
|
|
@ -390,8 +355,3 @@ proc existsCookie*(name: string): bool =
|
||||||
## Checks if a cookie of `name` exists.
|
## Checks if a cookie of `name` exists.
|
||||||
if gcookies == nil: gcookies = parseCookies(getHttpCookie())
|
if gcookies == nil: gcookies = parseCookies(getHttpCookie())
|
||||||
result = hasKey(gcookies, name)
|
result = hasKey(gcookies, name)
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
const test1 = "abc\L+def xyz"
|
|
||||||
assert encodeUrl(test1) == "abc%0A%2Bdef+xyz"
|
|
||||||
assert decodeUrl(encodeUrl(test1)) == test1
|
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,49 @@ proc add*(url: var Url, a: Url) {.deprecated.} =
|
||||||
url = url / a
|
url = url / a
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
|
proc encodeUrl*(s: string): string =
|
||||||
|
## Encodes a value to be HTTP safe: This means that characters in the set
|
||||||
|
## ``{'A'..'Z', 'a'..'z', '0'..'9', '_'}`` are carried over to the result,
|
||||||
|
## a space is converted to ``'+'`` and every other character is encoded as
|
||||||
|
## ``'%xx'`` where ``xx`` denotes its hexadecimal value.
|
||||||
|
result = newStringOfCap(s.len + s.len shr 2) # assume 12% non-alnum-chars
|
||||||
|
for i in 0..s.len-1:
|
||||||
|
case s[i]
|
||||||
|
of 'a'..'z', 'A'..'Z', '0'..'9', '_': add(result, s[i])
|
||||||
|
of ' ': add(result, '+')
|
||||||
|
else:
|
||||||
|
add(result, '%')
|
||||||
|
add(result, toHex(ord(s[i]), 2))
|
||||||
|
|
||||||
|
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.
|
||||||
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
proc parseAuthority(authority: string, result: var Uri) =
|
proc parseAuthority(authority: string, result: var Uri) =
|
||||||
var i = 0
|
var i = 0
|
||||||
var inPort = false
|
var inPort = false
|
||||||
|
|
@ -327,6 +370,11 @@ proc `$`*(u: Uri): string =
|
||||||
result.add(u.anchor)
|
result.add(u.anchor)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
block:
|
||||||
|
const test1 = "abc\L+def xyz"
|
||||||
|
doAssert encodeUrl(test1) == "abc%0A%2Bdef+xyz"
|
||||||
|
doAssert decodeUrl(encodeUrl(test1)) == test1
|
||||||
|
|
||||||
block:
|
block:
|
||||||
let str = "http://localhost"
|
let str = "http://localhost"
|
||||||
let test = parseUri(str)
|
let test = parseUri(str)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue