2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
2019-08-01 16:01:03 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module urllib
|
|
|
|
|
2022-02-09 18:36:12 +03:00
|
|
|
struct QueryValue {
|
2019-12-13 20:09:11 +03:00
|
|
|
pub mut:
|
2022-02-09 18:36:12 +03:00
|
|
|
key string
|
|
|
|
value string
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:21:33 +03:00
|
|
|
pub struct Values {
|
2019-12-13 20:09:11 +03:00
|
|
|
pub mut:
|
2022-02-09 18:36:12 +03:00
|
|
|
data []QueryValue
|
2020-10-15 16:17:52 +03:00
|
|
|
len int
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 00:28:55 +03:00
|
|
|
// new_values returns a new Values struct for creating
|
2019-11-14 09:53:05 +03:00
|
|
|
// urlencoded query string parameters. it can also be to
|
2019-08-03 00:28:55 +03:00
|
|
|
// post form data with application/x-www-form-urlencoded.
|
|
|
|
// values.encode() will return the encoded data
|
|
|
|
pub fn new_values() Values {
|
2019-08-01 16:01:03 +03:00
|
|
|
return Values{
|
2022-02-09 18:36:12 +03:00
|
|
|
data: []QueryValue{}
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 00:28:55 +03:00
|
|
|
// get gets the first value associated with the given key.
|
|
|
|
// If there are no values associated with the key, get returns
|
2023-03-14 09:44:40 +03:00
|
|
|
// none.
|
|
|
|
pub fn (v &Values) get(key string) ?string {
|
2020-06-21 17:51:02 +03:00
|
|
|
if v.data.len == 0 {
|
2023-03-14 09:44:40 +03:00
|
|
|
return none
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
2022-02-09 18:36:12 +03:00
|
|
|
for qvalue in v.data {
|
|
|
|
if qvalue.key == key {
|
|
|
|
return qvalue.value
|
|
|
|
}
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
2023-03-14 09:44:40 +03:00
|
|
|
return none
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 00:28:55 +03:00
|
|
|
// get_all gets the all the values associated with the given key.
|
|
|
|
// If there are no values associated with the key, get returns
|
|
|
|
// a empty []string.
|
2019-10-10 20:04:11 +03:00
|
|
|
pub fn (v &Values) get_all(key string) []string {
|
2020-06-21 17:51:02 +03:00
|
|
|
if v.data.len == 0 {
|
2019-11-14 09:53:05 +03:00
|
|
|
return []
|
2019-08-03 00:28:55 +03:00
|
|
|
}
|
2022-02-09 18:36:12 +03:00
|
|
|
mut values := []string{}
|
|
|
|
for qvalue in v.data {
|
|
|
|
if qvalue.key == key {
|
|
|
|
values << qvalue.value
|
|
|
|
}
|
2019-08-03 00:28:55 +03:00
|
|
|
}
|
2022-02-09 18:36:12 +03:00
|
|
|
return values
|
2019-08-03 00:28:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// set sets the key to value. It replaces any existing
|
2022-02-13 16:06:00 +03:00
|
|
|
// values, or create a new bucket with the new key if it is missed.
|
2020-10-15 16:17:52 +03:00
|
|
|
pub fn (mut v Values) set(key string, value string) {
|
2022-02-09 18:36:12 +03:00
|
|
|
// A query string can contains several
|
|
|
|
// duplicate, so we need to make sure that we
|
|
|
|
// cover all the edge case.
|
2022-02-13 16:06:00 +03:00
|
|
|
mut found := false
|
2022-02-09 18:36:12 +03:00
|
|
|
for mut qvalue in v.data {
|
2022-02-13 16:06:00 +03:00
|
|
|
if qvalue.key == key {
|
|
|
|
found = true
|
|
|
|
qvalue.value = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
v.add(key, value)
|
2022-02-09 18:36:12 +03:00
|
|
|
}
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 00:28:55 +03:00
|
|
|
// add adds the value to key. It appends to any existing
|
2019-08-01 16:01:03 +03:00
|
|
|
// values associated with key.
|
2020-10-15 16:17:52 +03:00
|
|
|
pub fn (mut v Values) add(key string, value string) {
|
2022-02-09 18:36:12 +03:00
|
|
|
v.data << QueryValue{
|
|
|
|
key: key
|
|
|
|
value: value
|
2019-08-03 00:28:55 +03:00
|
|
|
}
|
2020-06-21 17:51:02 +03:00
|
|
|
v.len = v.data.len
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 00:28:55 +03:00
|
|
|
// del deletes the values associated with key.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut v Values) del(key string) {
|
2022-02-09 18:36:12 +03:00
|
|
|
for idx, qvalue in v.data {
|
|
|
|
if qvalue.key == key {
|
|
|
|
v.data.delete(idx)
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 17:51:02 +03:00
|
|
|
v.len = v.data.len
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
2022-02-09 18:36:12 +03:00
|
|
|
|
|
|
|
// return the list of values in the query string
|
|
|
|
pub fn (v Values) values() []string {
|
|
|
|
mut values := []string{}
|
|
|
|
for qvalue in v.data {
|
|
|
|
if qvalue.value != '' {
|
|
|
|
values << qvalue.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a map <key []value> of the query string
|
|
|
|
pub fn (v Values) to_map() map[string][]string {
|
|
|
|
mut result := map[string][]string{}
|
|
|
|
|
|
|
|
for qvalue in v.data {
|
|
|
|
if qvalue.key in result {
|
|
|
|
result[qvalue.key] << qvalue.value
|
|
|
|
} else {
|
|
|
|
result[qvalue.key] = [qvalue.value]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|