net.http: add a Header.starting_with method, to get the first header, starting with a key (#10119)
parent
eef445508f
commit
86778d06b1
|
@ -469,6 +469,16 @@ pub fn (h Header) get_custom(key string, flags ...HeaderQueryConfig) ?string {
|
||||||
return h.data[data_key][0]
|
return h.data[data_key][0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gets the first value of the header starting with key, or none if the key does not exist.
|
||||||
|
pub fn (h Header) starting_with(key string) ?string {
|
||||||
|
for k, _ in h.data {
|
||||||
|
if k.starts_with(key) {
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return none
|
||||||
|
}
|
||||||
|
|
||||||
// Gets all values for the CommonHeader.
|
// Gets all values for the CommonHeader.
|
||||||
pub fn (h Header) values(key CommonHeader) []string {
|
pub fn (h Header) values(key CommonHeader) []string {
|
||||||
return h.custom_values(key.str())
|
return h.custom_values(key.str())
|
||||||
|
|
|
@ -115,6 +115,14 @@ fn test_get_custom() ? {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_starting_with() ? {
|
||||||
|
mut h := http.new_header()
|
||||||
|
h.add_custom('Hello-1', 'world') ?
|
||||||
|
h.add_custom('Hello-21', 'world') ?
|
||||||
|
assert h.starting_with('Hello-') ? == 'Hello-1'
|
||||||
|
assert h.starting_with('Hello-2') ? == 'Hello-21'
|
||||||
|
}
|
||||||
|
|
||||||
fn test_custom_values() ? {
|
fn test_custom_values() ? {
|
||||||
mut h := http.new_header()
|
mut h := http.new_header()
|
||||||
h.add_custom('Hello', 'world') ?
|
h.add_custom('Hello', 'world') ?
|
||||||
|
|
Loading…
Reference in New Issue