aboutsummaryrefslogtreecommitdiffstats
path: root/src/types/url.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-11-15 17:37:12 +0100
committerbptato <nincsnevem662@gmail.com>2024-11-15 17:37:12 +0100
commitf00a6728ef23061ab69fed90414ee9aa5b21f132 (patch)
treea4448369fd52d17cdf557f97d7dd9c521f231e9c /src/types/url.nim
parente94dae5d8aba469876a40d6400332c674189d126 (diff)
downloadchawan-f00a6728ef23061ab69fed90414ee9aa5b21f132.tar.gz
url: fix serializeFormURLEncoded, refactor
kill qmark hack, plus refactor the form data parser/serializer
Diffstat (limited to 'src/types/url.nim')
-rw-r--r--src/types/url.nim43
1 files changed, 17 insertions, 26 deletions
diff --git a/src/types/url.nim b/src/types/url.nim
index f3b52611..83ef020a 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -1034,37 +1034,28 @@ proc setHref(ctx: JSContext; url: URL; s: string) {.jsfset: "href".} =
func isIP*(url: URL): bool =
return url.hostType in {htIpv4, htIpv6}
-#https://url.spec.whatwg.org/#concept-urlencoded-serializer
+# https://url.spec.whatwg.org/#urlencoded-parsing
proc parseFromURLEncoded(input: string): seq[(string, string)] =
+ result = @[]
for s in input.split('&'):
if s == "":
continue
- var name = ""
- var value = ""
- for i in 0..<s.len:
- if s[i] == '=':
- name = s.substr(0, i - 1)
- value = s.substr(i + 1)
- break
- if name == "":
- name = s
- for i in 0..<name.len:
- if name[i] == '+':
- name[i] = ' '
- for i in 0..<value.len:
- if value[i] == '+':
- value[i] = ' '
- result.add((percentDecode(name), percentDecode(value)))
-
-#https://url.spec.whatwg.org/#concept-urlencoded-serializer
-proc serializeFormURLEncoded*(kvs: seq[(string, string)]; spaceAsPlus = true;
- qmark = false): string =
+ var name = s.until('=')
+ var value = s.after('=')
+ for c in name.mitems:
+ if c == '+':
+ c = ' '
+ for c in value.mitems:
+ if c == '+':
+ c = ' '
+ result.add((name.percentDecode(), value.percentDecode()))
+
+# https://url.spec.whatwg.org/#urlencoded-serializing
+proc serializeFormURLEncoded*(kvs: seq[(string, string)]; spaceAsPlus = true):
+ string =
result = ""
- if qmark:
- result &= '?'
- for it in kvs:
- let (name, value) = it
- if result != "":
+ for (name, value) in kvs:
+ if result.len > 0:
result &= '&'
result.percentEncode(name, ApplicationXWWWFormUrlEncodedSet, spaceAsPlus)
result &= '='