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:
parent
0197f20d47
commit
d3c89273e8
@ -399,6 +399,16 @@ pub fn (s string) index(p string) int {
|
|||||||
return -1
|
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 {
|
pub fn (s string) last_index(p string) int {
|
||||||
if p.len > s.len {
|
if p.len > s.len {
|
||||||
return -1
|
return -1
|
||||||
|
1110
vlib/net/urllib/urllib.v
Normal file
1110
vlib/net/urllib/urllib.v
Normal file
File diff suppressed because it is too large
Load Diff
23
vlib/net/urllib/urllib_test.v
Normal file
23
vlib/net/urllib/urllib_test.v
Normal 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¶m2=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
69
vlib/net/urllib/values.v
Normal 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
|
||||||
|
}
|
@ -7,6 +7,7 @@ module strings
|
|||||||
struct Builder {
|
struct Builder {
|
||||||
mut:
|
mut:
|
||||||
buf []byte
|
buf []byte
|
||||||
|
pub:
|
||||||
len int
|
len int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user