about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.nim2
-rw-r--r--src/types/url.nim7
-rw-r--r--src/utils/twtstr.nim18
3 files changed, 21 insertions, 6 deletions
diff --git a/src/client.nim b/src/client.nim
index 7ec5b3de..2de2a639 100644
--- a/src/client.nim
+++ b/src/client.nim
@@ -45,7 +45,7 @@ proc interruptError() =
 
 proc getPage(client: Client, url: Url): tuple[s: Stream, contenttype: string] =
   if url.scheme == "file":
-    let path = url.path.serialize()
+    let path = url.path.serialize_unicode()
     result.contenttype = guessContentType(path)
     result.s = newFileStream(path, fmRead)
   elif url.scheme == "http" or url.scheme == "https":
diff --git a/src/types/url.nim b/src/types/url.nim
index 81ae4ea4..4e1a52f7 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -791,6 +791,13 @@ func serialize*(path: UrlPath): string {.inline.} =
     result &= '/'
     result &= s
 
+func serialize_unicode*(path: UrlPath): string {.inline.} =
+  if path.opaque:
+    return percentDecode(path.s)
+  for s in path.ss:
+    result &= '/'
+    result &= percentDecode(s)
+
 func serialize*(url: Url, excludefragment = false): string =
   result = url.scheme & ':'
   if url.host.issome:
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 4b414719..43e287fa 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -173,8 +173,8 @@ func decValue*(r: Rune): int =
 const HexChars = "0123456789ABCDEF"
 func toHex*(c: char): string =
   result = newString(2)
-  result[0] = HexChars[(uint8(c) and 0xF)]
-  result[1] = HexChars[(uint8(c) shr 4)]
+  result[0] = HexChars[(uint8(c) shr 4)]
+  result[1] = HexChars[(uint8(c) and 0xF)]
 
 func equalsIgnoreCase*(s1: seq[Rune], s2: string): bool =
   var i = 0
@@ -432,12 +432,20 @@ func percentEncode*(c: char, set: set[char]): string {.inline.} =
   result.percentEncode(c, set)
 
 func percentDecode*(input: string): string =
-  for i in low(input)..high(input):
+  var i = 0
+  while i < input.len:
     let c = input[i]
-    if c != '%' or i + 2 >= input.len or input[i + 1].hexValue == -1 or input[i + 2].hexValue == -1:
+    if c != '%' or i + 2 >= input.len:
       result &= c
     else:
-      result &= char((input[i + 1].hexValue shr 4) or (input[i + 2].hexValue and 0xF))
+      let h1 = input[i + 1].hexValue
+      let h2 = input[i + 2].hexValue
+      if h1 == -1 or h2 == -1:
+        result &= c
+      else:
+        result &= char((h1 shl 4) or h2)
+        i += 2
+    inc i
 
 #basically std join but with char
 func join*(ss: openarray[string], sep: char): string =