1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net.urllib: keep the query parameter order (#13405)

This commit is contained in:
Vincenzo Palazzo
2022-02-09 16:36:12 +01:00
committed by GitHub
parent 4be3c92640
commit 0d1d259bb4
5 changed files with 134 additions and 49 deletions

View File

@ -835,28 +835,32 @@ fn parse_query_values(mut m Values, query string) ?bool {
}
// encode encodes the values into ``URL encoded'' form
// ('bar=baz&foo=quux') sorted by key.
// ('bar=baz&foo=quux').
// The syntx of the query string is specified in the
// RFC173 https://datatracker.ietf.org/doc/html/rfc1738
//
// HTTP grammar
//
// httpurl = "http://" hostport [ "/" hpath [ "?" search ]]
// hpath = hsegment *[ "/" hsegment ]
// hsegment = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
// search = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
pub fn (v Values) encode() string {
if v.len == 0 {
return ''
}
mut buf := strings.new_builder(200)
mut keys := []string{}
for k, _ in v.data {
keys << k
}
keys.sort()
for k in keys {
vs := v.data[k]
key_kscaped := query_escape(k)
for _, val in vs.data {
if buf.len > 0 {
buf.write_string('&')
}
buf.write_string(key_kscaped)
buf.write_string('=')
buf.write_string(query_escape(val))
for qvalue in v.data {
key_kscaped := query_escape(qvalue.key)
if buf.len > 0 {
buf.write_string('&')
}
buf.write_string(key_kscaped)
if qvalue.value == '' {
continue
}
buf.write_string('=')
buf.write_string(query_escape(qvalue.value))
}
return buf.str()
}