about summary refs log tree commit diff stats
path: root/src/types
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-29 02:25:40 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-29 02:25:40 +0100
commit53f9d91dd6377c4f689e2b237d4a25c33c1d9e16 (patch)
tree0bd48aeea9c8b0f4ddf0531a8f3a7df02f6124e5 /src/types
parentb7f19942e3d0a4cf25981c1686ffdb200ec4fd6f (diff)
downloadchawan-53f9d91dd6377c4f689e2b237d4a25c33c1d9e16.tar.gz
Add punycode support
Diffstat (limited to 'src/types')
-rw-r--r--src/types/url.nim34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/types/url.nim b/src/types/url.nim
index 6dea5064..81ae4ea4 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -248,6 +248,40 @@ func endsInNumber(input: string): bool =
     return true
   return false
 
+func domainToAscii*(domain: string, bestrict = false): Option[string] =
+  var needsprocessing = false
+  for s in domain.split('.'):
+    var i = 0
+    var xn = 0
+    while i < s.len:
+      if s[i] notin Ascii:
+        needsprocessing = true
+        break
+      case i
+      of 0:
+        if s[i] == 'x': inc xn
+      of 1:
+        if s[i] == 'n': inc xn
+      of 2:
+        if s[i] == '-': inc xn
+      of 3:
+        if s[i] == '-' and xn == 3:
+          needsprocessing = true
+          break
+      else: discard
+      inc i
+    if needsprocessing:
+      break
+  if bestrict or needsprocessing:
+    #Note: we don't implement STD3 separately, it's always true
+    result = domain.unicodeToAscii(false, true, true, false, bestrict)
+    if result.isnone or result.get == "":
+      #TODO validation error
+      return none(string)
+    return result
+  else:
+    return domain.toAsciiLower().some
+
 func parseHost(input: string, isnotspecial = false): Option[Host] =
   if input.len == 0: return
   if input[0] == '[':