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

net.urllib module

This commit is contained in:
joe-conigliaro 2019-08-01 23:01:03 +10:00 committed by Alexander Medvednikov
parent 0197f20d47
commit d3c89273e8
5 changed files with 1213 additions and 0 deletions

View File

@ -399,6 +399,16 @@ pub fn (s string) index(p string) int {
return -1
}
pub fn (s string) index_any(chars string) int {
for c in chars {
index := s.index(c.str())
if index != -1 {
return index
}
}
return -1
}
pub fn (s string) last_index(p string) int {
if p.len > s.len {
return -1

1110
vlib/net/urllib/urllib.v Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,23 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import net.urllib
fn test_new_urllib() {
test_query := 'Hellö Wörld@vlang'
assert urllib.query_escape(test_query) == 'Hell%C3%B6+W%C3%B6rld%40vlang'
test_url := 'https://joe:pass@www.mydomain.com:8080/som/url?param1=test1&param2=test2&foo=bar#testfragment'
u := urllib.parse(test_url) or {
assert false
return
}
assert u.scheme == 'https' &&
u.hostname() == 'www.mydomain.com' &&
u.port() == '8080' &&
u.path == '/som/url' &&
u.fragment == 'testfragment' &&
u.user.username == 'joe' &&
u.user.password == 'pass'
}

69
vlib/net/urllib/values.v Normal file
View File

@ -0,0 +1,69 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module urllib
struct ValueStruct {
pub:
mut:
data []string
}
type Value ValueStruct
struct Values {
pub:
mut:
data map[string]Value
size int
}
fn new_values() Values {
return Values{
data: map[string]Value{}
}
}
pub fn (v Value) all() []string {
return v.data
}
// Get gets the first value associated with the given key.
// If there are no values associated with the key, Get returns
// the empty string. To access multiple values, use the map
// directly.
fn (v Values) get(key string) string {
if v.data.size == 0 {
return ''
}
vs := v.data[key]
if vs.data.len == 0 {
return ''
}
return vs.data[0]
}
// Set sets the key to value. It replaces any existing
// values.
fn (v Values) set(key, value string) {
v.data[key].data = [value]
v.size = v.data.size
}
// Add adds the value to key. It appends to any existing
// values associated with key.
fn (v mut Values) add(key, value string) {
mut a := v.data[key]
a.data << value
}
// Del deletes the values associated with key.
fn (v mut Values) del(key string) {
v.data.delete(key)
v.size = v.data.size
}
pub fn (v Values) iter() map[string]Value {
return v.data
}

View File

@ -7,6 +7,7 @@ module strings
struct Builder {
mut:
buf []byte
pub:
len int
}