mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
ci: fix warnings/errors due to the vfmt change
This commit is contained in:
@ -10,7 +10,7 @@ const (
|
||||
is_used = openssl.is_used
|
||||
)
|
||||
|
||||
fn (req &Request) ssl_do(port int, method Method, host_name, path string) ?Response {
|
||||
fn (req &Request) ssl_do(port int, method Method, host_name string, path string) ?Response {
|
||||
// ssl_method := C.SSLv23_method()
|
||||
ssl_method := C.TLSv1_2_method()
|
||||
ctx := C.SSL_CTX_new(ssl_method)
|
||||
|
@ -5,7 +5,7 @@ module http
|
||||
|
||||
import os
|
||||
|
||||
pub fn download_file(url, out string) bool {
|
||||
pub fn download_file(url string, out string) bool {
|
||||
$if debug_http? {
|
||||
println('download file url=$url out=$out')
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ mut:
|
||||
cb DownloadFn
|
||||
}
|
||||
*/
|
||||
fn download_cb(ptr voidptr, size, nmemb size_t, userp voidptr) {
|
||||
fn download_cb(ptr voidptr, size size_t, nmemb size_t, userp voidptr) {
|
||||
/*
|
||||
mut data := &DownloadStruct(userp)
|
||||
written := C.fwrite(ptr, size, nmemb, data.stream)
|
||||
@ -24,7 +24,7 @@ fn download_cb(ptr voidptr, size, nmemb size_t, userp voidptr) {
|
||||
*/
|
||||
}
|
||||
|
||||
pub fn download_file_with_progress(url, out string, cb DownloadFn, cb_finished fn()) {
|
||||
pub fn download_file_with_progress(url string, out string, cb DownloadFn, cb_finished fn()) {
|
||||
/*
|
||||
curl := C.curl_easy_init()
|
||||
if isnil(curl) {
|
||||
|
@ -11,7 +11,7 @@ import net
|
||||
const (
|
||||
max_redirects = 4
|
||||
content_type_default = 'text/plain'
|
||||
bufsize = 1536
|
||||
bufsize = 1536
|
||||
)
|
||||
|
||||
pub struct Request {
|
||||
@ -47,18 +47,17 @@ pub:
|
||||
status_code int
|
||||
}
|
||||
|
||||
pub fn new_request(method Method, url_, data string) ?Request {
|
||||
pub fn new_request(method Method, url_ string, data string) ?Request {
|
||||
url := if method == .get { url_ + '?' + data } else { url_ }
|
||||
//println('new req() method=$method url="$url" dta="$data"')
|
||||
// println('new req() method=$method url="$url" dta="$data"')
|
||||
return Request{
|
||||
method: method
|
||||
url: url
|
||||
data: data
|
||||
/*
|
||||
headers: {
|
||||
data: data /*
|
||||
headers: {
|
||||
'Accept-Encoding': 'compress'
|
||||
}
|
||||
*/
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +74,7 @@ pub fn get(url string) ?Response {
|
||||
return fetch_with_method(.get, url, FetchConfig{})
|
||||
}
|
||||
|
||||
pub fn post(url, data string) ?Response {
|
||||
pub fn post(url string, data string) ?Response {
|
||||
return fetch_with_method(.post, url, {
|
||||
data: data
|
||||
headers: {
|
||||
@ -84,7 +83,7 @@ pub fn post(url, data string) ?Response {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn post_json(url, data string) ?Response {
|
||||
pub fn post_json(url string, data string) ?Response {
|
||||
return fetch_with_method(.post, url, {
|
||||
data: data
|
||||
headers: {
|
||||
@ -102,7 +101,7 @@ pub fn post_form(url string, data map[string]string) ?Response {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn put(url, data string) ?Response {
|
||||
pub fn put(url string, data string) ?Response {
|
||||
return fetch_with_method(.put, url, {
|
||||
data: data
|
||||
headers: {
|
||||
@ -111,7 +110,7 @@ pub fn put(url, data string) ?Response {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn patch(url, data string) ?Response {
|
||||
pub fn patch(url string, data string) ?Response {
|
||||
return fetch_with_method(.patch, url, {
|
||||
data: data
|
||||
headers: {
|
||||
@ -133,7 +132,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response {
|
||||
return error('http.fetch: empty url')
|
||||
}
|
||||
url := build_url_from_fetch(_url, config) or {
|
||||
return error('http.fetch: invalid url ${_url}')
|
||||
return error('http.fetch: invalid url $_url')
|
||||
}
|
||||
data := config.data
|
||||
req := Request{
|
||||
@ -147,7 +146,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response {
|
||||
user_ptr: 0
|
||||
verbose: config.verbose
|
||||
}
|
||||
res := req.do()?
|
||||
res := req.do() ?
|
||||
return res
|
||||
}
|
||||
|
||||
@ -177,14 +176,14 @@ fn fetch_with_method(method Method, url string, _config FetchConfig) ?Response {
|
||||
}
|
||||
|
||||
fn build_url_from_fetch(_url string, config FetchConfig) ?string {
|
||||
mut url := urllib.parse(_url)?
|
||||
mut url := urllib.parse(_url) ?
|
||||
params := config.params
|
||||
if params.keys().len == 0 {
|
||||
return url.str()
|
||||
}
|
||||
mut pieces := []string{}
|
||||
for key in params.keys() {
|
||||
pieces << '${key}=${params[key]}'
|
||||
pieces << '$key=${params[key]}'
|
||||
}
|
||||
mut query := pieces.join('&')
|
||||
if url.raw_query.len > 1 {
|
||||
@ -195,19 +194,15 @@ fn build_url_from_fetch(_url string, config FetchConfig) ?string {
|
||||
}
|
||||
|
||||
fn (mut req Request) free() {
|
||||
unsafe {
|
||||
req.headers.free()
|
||||
}
|
||||
unsafe {req.headers.free()}
|
||||
}
|
||||
|
||||
fn (mut resp Response) free() {
|
||||
unsafe {
|
||||
resp.headers.free()
|
||||
}
|
||||
unsafe {resp.headers.free()}
|
||||
}
|
||||
|
||||
// add_header adds the key and value of an HTTP request header
|
||||
pub fn (mut req Request) add_header(key, val string) {
|
||||
pub fn (mut req Request) add_header(key string, val string) {
|
||||
req.headers[key] = val
|
||||
}
|
||||
|
||||
@ -229,7 +224,7 @@ pub fn parse_headers(lines []string) map[string]string {
|
||||
// do will send the HTTP request and returns `http.Response` as soon as the response is recevied
|
||||
pub fn (req &Request) do() ?Response {
|
||||
mut url := urllib.parse(req.url) or {
|
||||
return error('http.Request.do: invalid url ${req.url}')
|
||||
return error('http.Request.do: invalid url $req.url')
|
||||
}
|
||||
mut rurl := url
|
||||
mut resp := Response{}
|
||||
@ -238,7 +233,7 @@ pub fn (req &Request) do() ?Response {
|
||||
if no_redirects == max_redirects {
|
||||
return error('http.request.do: maximum number of redirects reached ($max_redirects)')
|
||||
}
|
||||
qresp := req.method_and_url_to_response(req.method, rurl)?
|
||||
qresp := req.method_and_url_to_response(req.method, rurl) ?
|
||||
resp = qresp
|
||||
if resp.status_code !in [301, 302, 303, 307, 308] {
|
||||
break
|
||||
@ -264,7 +259,7 @@ fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) ?Res
|
||||
host_name := url.hostname()
|
||||
scheme := url.scheme
|
||||
p := url.path.trim_left('/')
|
||||
path := if url.query().len > 0 { '/$p?${url.query().encode()}' } else { '/$p' }
|
||||
path := if url.query().len > 0 { '/$p?$url.query().encode()' } else { '/$p' }
|
||||
mut nport := url.port().int()
|
||||
if nport == 0 {
|
||||
if scheme == 'http' {
|
||||
@ -277,11 +272,11 @@ fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) ?Res
|
||||
// println('fetch $method, $scheme, $host_name, $nport, $path ')
|
||||
if scheme == 'https' {
|
||||
// println('ssl_do( $nport, $method, $host_name, $path )')
|
||||
res := req.ssl_do(nport, method, host_name, path)?
|
||||
res := req.ssl_do(nport, method, host_name, path) ?
|
||||
return res
|
||||
} else if scheme == 'http' {
|
||||
// println('http_do( $nport, $method, $host_name, $path )')
|
||||
res := req.http_do(nport, method, host_name, path)?
|
||||
res := req.http_do(nport, method, host_name, path) ?
|
||||
return res
|
||||
}
|
||||
return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"')
|
||||
@ -322,7 +317,6 @@ fn parse_response(resp string) Response {
|
||||
// if h.contains('Content-Type') {
|
||||
// continue
|
||||
// }
|
||||
|
||||
mut key := h[..pos]
|
||||
lkey := key.to_lower()
|
||||
val := h[pos + 2..]
|
||||
@ -346,7 +340,7 @@ fn parse_response(resp string) Response {
|
||||
}
|
||||
}
|
||||
|
||||
fn (req &Request) build_request_headers(method Method, host_name, path string) string {
|
||||
fn (req &Request) build_request_headers(method Method, host_name string, path string) string {
|
||||
ua := req.user_agent
|
||||
mut uheaders := []string{}
|
||||
if 'Host' !in req.headers {
|
||||
@ -356,17 +350,16 @@ fn (req &Request) build_request_headers(method Method, host_name, path string) s
|
||||
uheaders << 'User-Agent: $ua\r\n'
|
||||
}
|
||||
if req.data.len > 0 && 'Content-Length' !in req.headers {
|
||||
uheaders << 'Content-Length: ${req.data.len}\r\n'
|
||||
uheaders << 'Content-Length: $req.data.len\r\n'
|
||||
}
|
||||
for key, val in req.headers {
|
||||
if key == 'Cookie' {
|
||||
continue
|
||||
}
|
||||
uheaders << '${key}: ${val}\r\n'
|
||||
uheaders << '$key: $val\r\n'
|
||||
}
|
||||
uheaders << req.build_request_cookies_header()
|
||||
return '$method $path HTTP/1.1\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' +
|
||||
req.data
|
||||
return '$method $path HTTP/1.1\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' + req.data
|
||||
}
|
||||
|
||||
fn (req &Request) build_request_cookies_header() string {
|
||||
@ -399,13 +392,12 @@ pub fn escape(s string) string {
|
||||
panic('http.escape() was replaced with http.escape_url()')
|
||||
}
|
||||
|
||||
fn (req &Request) http_do(port int, method Method, host_name, path string) ?Response {
|
||||
fn (req &Request) http_do(port int, method Method, host_name string, path string) ?Response {
|
||||
rbuffer := [bufsize]byte{}
|
||||
mut sb := strings.new_builder(100)
|
||||
s := req.build_request_headers(method, host_name, path)
|
||||
client := net.dial(host_name, port)?
|
||||
client.send(s.str, s.len) or {
|
||||
}
|
||||
client := net.dial(host_name, port) ?
|
||||
client.send(s.str, s.len) or { }
|
||||
for {
|
||||
readbytes := client.crecv(rbuffer, bufsize)
|
||||
if readbytes < 0 {
|
||||
@ -416,8 +408,7 @@ fn (req &Request) http_do(port int, method Method, host_name, path string) ?Resp
|
||||
}
|
||||
sb.write(tos(rbuffer, readbytes))
|
||||
}
|
||||
client.close() or {
|
||||
}
|
||||
client.close() or { }
|
||||
return parse_response(sb.str())
|
||||
}
|
||||
|
||||
|
@ -1,48 +1,62 @@
|
||||
import net.http
|
||||
|
||||
fn test_http_get() {
|
||||
$if !network ? { return }
|
||||
$if !network ? {
|
||||
return
|
||||
}
|
||||
assert http.get_text('https://vlang.io/version') == '0.1.5'
|
||||
println('http ok')
|
||||
}
|
||||
|
||||
fn test_http_get_from_vlang_utc_now() {
|
||||
$if !network ? { return }
|
||||
$if !network ? {
|
||||
return
|
||||
}
|
||||
urls := ['http://vlang.io/utc_now', 'https://vlang.io/utc_now']
|
||||
for url in urls {
|
||||
println('Test getting current time from $url by http.get')
|
||||
res := http.get(url) or { panic(err) }
|
||||
res := http.get(url) or {
|
||||
panic(err)
|
||||
}
|
||||
assert 200 == res.status_code
|
||||
assert res.text.len > 0
|
||||
assert res.text.int() > 1566403696
|
||||
println('Current time is: ${res.text.int()}')
|
||||
println('Current time is: $res.text.int()')
|
||||
}
|
||||
}
|
||||
|
||||
fn test_public_servers() {
|
||||
$if !network ? { return }
|
||||
$if !network ? {
|
||||
return
|
||||
}
|
||||
urls := [
|
||||
'http://github.com/robots.txt',
|
||||
'http://google.com/robots.txt',
|
||||
'https://github.com/robots.txt',
|
||||
'https://google.com/robots.txt',
|
||||
// 'http://yahoo.com/robots.txt',
|
||||
// 'https://yahoo.com/robots.txt',
|
||||
// 'http://yahoo.com/robots.txt',
|
||||
// 'https://yahoo.com/robots.txt',
|
||||
]
|
||||
for url in urls {
|
||||
println('Testing http.get on public url: $url ')
|
||||
res := http.get( url ) or { panic(err) }
|
||||
res := http.get(url) or {
|
||||
panic(err)
|
||||
}
|
||||
assert 200 == res.status_code
|
||||
assert res.text.len > 0
|
||||
}
|
||||
}
|
||||
|
||||
fn test_relative_redirects() {
|
||||
$if !network ? { return }
|
||||
$else { return } // tempfix periodic: httpbin relative redirects are broken
|
||||
res := http.get('https://httpbin.org/relative-redirect/3?abc=xyz') or { panic(err) }
|
||||
$if !network ? {
|
||||
return
|
||||
} $else {
|
||||
return
|
||||
} // tempfix periodic: httpbin relative redirects are broken
|
||||
res := http.get('https://httpbin.org/relative-redirect/3?abc=xyz') or {
|
||||
panic(err)
|
||||
}
|
||||
assert 200 == res.status_code
|
||||
assert res.text.len > 0
|
||||
assert res.text.contains('"abc": "xyz"')
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user