2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module http
|
|
|
|
|
2019-08-06 15:43:09 +03:00
|
|
|
import net.urllib
|
|
|
|
|
2019-08-17 15:50:47 +03:00
|
|
|
const (
|
2020-05-16 17:12:23 +03:00
|
|
|
max_redirects = 4
|
2020-01-16 20:16:11 +03:00
|
|
|
content_type_default = 'text/plain'
|
2020-10-15 16:17:52 +03:00
|
|
|
bufsize = 1536
|
2019-08-17 15:50:47 +03:00
|
|
|
)
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// FetchConfig holds configuration data for the fetch function.
|
2020-01-16 20:16:11 +03:00
|
|
|
pub struct FetchConfig {
|
|
|
|
pub mut:
|
2021-08-17 09:16:59 +03:00
|
|
|
url string
|
2020-07-27 22:20:05 +03:00
|
|
|
method Method
|
2021-04-09 19:17:33 +03:00
|
|
|
header Header
|
2020-04-09 15:03:47 +03:00
|
|
|
data string
|
|
|
|
params map[string]string
|
|
|
|
cookies map[string]string
|
2020-05-20 09:58:57 +03:00
|
|
|
user_agent string = 'v.http'
|
2020-09-09 16:34:41 +03:00
|
|
|
verbose bool
|
2021-09-01 01:43:35 +03:00
|
|
|
//
|
2021-09-09 19:55:49 +03:00
|
|
|
validate bool // set this to true, if you want to stop requests, when their certificates are found to be invalid
|
|
|
|
verify string // the path to a rootca.pem file, containing trusted CA certificate(s)
|
|
|
|
cert string // the path to a cert.pem file, containing client certificate(s) for the request
|
|
|
|
cert_key string // the path to a key.pem file, containing private keys for the client certificate(s)
|
|
|
|
in_memory_verification bool // if true, verify, cert, and cert_key are read from memory, not from a file
|
2022-01-26 18:13:10 +03:00
|
|
|
allow_redirect bool = true // whether to allow redirect
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// new_request creates a new Request given the request `method`, `url_`, and
|
|
|
|
// `data`.
|
2023-03-18 00:17:22 +03:00
|
|
|
pub fn new_request(method Method, url_ string, data string) Request {
|
2021-09-08 05:11:02 +03:00
|
|
|
url := if method == .get && !url_.contains('?') { url_ + '?' + data } else { url_ }
|
2020-10-15 16:17:52 +03:00
|
|
|
// println('new req() method=$method url="$url" dta="$data"')
|
2020-05-10 03:07:15 +03:00
|
|
|
return Request{
|
2020-07-27 22:20:05 +03:00
|
|
|
method: method
|
2020-05-10 03:07:15 +03:00
|
|
|
url: url
|
2020-12-22 10:32:32 +03:00
|
|
|
data: data
|
|
|
|
/*
|
|
|
|
headers: {
|
2020-05-18 06:10:56 +03:00
|
|
|
'Accept-Encoding': 'compress'
|
|
|
|
}
|
2020-12-22 10:32:32 +03:00
|
|
|
*/
|
2020-05-10 03:07:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 11:15:16 +03:00
|
|
|
// get sends a GET HTTP request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn get(url string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(method: .get, url: url)
|
2020-01-15 01:19:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// post sends the string `data` as an HTTP POST request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn post(url string, data string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(
|
|
|
|
method: .post
|
|
|
|
url: url
|
2021-01-26 17:43:10 +03:00
|
|
|
data: data
|
2021-06-06 01:51:49 +03:00
|
|
|
header: new_header(key: .content_type, value: http.content_type_default)
|
2021-01-26 17:43:10 +03:00
|
|
|
)
|
2020-01-15 01:19:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// post_json sends the JSON `data` as an HTTP POST request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn post_json(url string, data string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(
|
|
|
|
method: .post
|
|
|
|
url: url
|
2021-01-26 17:43:10 +03:00
|
|
|
data: data
|
2021-06-06 01:51:49 +03:00
|
|
|
header: new_header(key: .content_type, value: 'application/json')
|
2021-01-26 17:43:10 +03:00
|
|
|
)
|
2020-06-30 15:36:11 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// post_form sends the map `data` as X-WWW-FORM-URLENCODED data to an HTTP POST request
|
|
|
|
// to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn post_form(url string, data map[string]string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(
|
|
|
|
method: .post
|
|
|
|
url: url
|
2021-06-06 01:51:49 +03:00
|
|
|
header: new_header(key: .content_type, value: 'application/x-www-form-urlencoded')
|
2021-01-26 17:43:10 +03:00
|
|
|
data: url_encode_form_data(data)
|
|
|
|
)
|
2020-01-15 01:19:50 +03:00
|
|
|
}
|
|
|
|
|
2021-09-25 10:09:49 +03:00
|
|
|
[params]
|
2021-09-16 07:34:07 +03:00
|
|
|
pub struct PostMultipartFormConfig {
|
2021-09-25 10:09:49 +03:00
|
|
|
pub mut:
|
|
|
|
form map[string]string
|
|
|
|
files map[string][]FileData
|
|
|
|
header Header
|
2021-09-16 07:34:07 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// post_multipart_form sends multipart form data `conf` as an HTTP POST
|
|
|
|
// request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn post_multipart_form(url string, conf PostMultipartFormConfig) !Response {
|
2021-09-16 07:34:07 +03:00
|
|
|
body, boundary := multipart_form_body(conf.form, conf.files)
|
2021-09-25 10:09:49 +03:00
|
|
|
mut header := conf.header
|
2022-11-15 16:53:13 +03:00
|
|
|
header.set(.content_type, 'multipart/form-data; boundary="${boundary}"')
|
2021-09-16 07:34:07 +03:00
|
|
|
return fetch(
|
|
|
|
method: .post
|
|
|
|
url: url
|
2021-09-25 10:09:49 +03:00
|
|
|
header: header
|
2021-09-16 07:34:07 +03:00
|
|
|
data: body
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// put sends string `data` as an HTTP PUT request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn put(url string, data string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(
|
|
|
|
method: .put
|
|
|
|
url: url
|
2021-01-26 17:43:10 +03:00
|
|
|
data: data
|
2021-06-06 01:51:49 +03:00
|
|
|
header: new_header(key: .content_type, value: http.content_type_default)
|
2021-01-26 17:43:10 +03:00
|
|
|
)
|
2020-01-15 01:19:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// patch sends string `data` as an HTTP PATCH request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn patch(url string, data string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(
|
|
|
|
method: .patch
|
|
|
|
url: url
|
2021-01-26 17:43:10 +03:00
|
|
|
data: data
|
2021-06-06 01:51:49 +03:00
|
|
|
header: new_header(key: .content_type, value: http.content_type_default)
|
2021-01-26 17:43:10 +03:00
|
|
|
)
|
2019-07-29 20:18:26 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// head sends an HTTP HEAD request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn head(url string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(method: .head, url: url)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// delete sends an HTTP DELETE request to the given `url`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn delete(url string) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(method: .delete, url: url)
|
2020-01-04 00:07:58 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// fetch sends an HTTP request to the `url` with the given method and configuration.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn fetch(config FetchConfig) !Response {
|
2021-08-17 09:16:59 +03:00
|
|
|
if config.url == '' {
|
2020-01-16 20:16:11 +03:00
|
|
|
return error('http.fetch: empty url')
|
2019-07-29 20:18:26 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
url := build_url_from_fetch(config) or { return error('http.fetch: invalid url ${config.url}') }
|
2020-01-16 20:16:11 +03:00
|
|
|
req := Request{
|
2020-07-27 22:20:05 +03:00
|
|
|
method: config.method
|
2019-07-13 15:10:15 +03:00
|
|
|
url: url
|
2021-08-17 09:16:59 +03:00
|
|
|
data: config.data
|
2021-04-09 19:17:33 +03:00
|
|
|
header: config.header
|
2020-01-16 20:16:11 +03:00
|
|
|
cookies: config.cookies
|
2020-01-29 06:05:37 +03:00
|
|
|
user_agent: config.user_agent
|
2019-06-22 21:20:28 +03:00
|
|
|
user_ptr: 0
|
2020-01-16 20:16:11 +03:00
|
|
|
verbose: config.verbose
|
2021-09-01 01:43:35 +03:00
|
|
|
validate: config.validate
|
|
|
|
verify: config.verify
|
|
|
|
cert: config.cert
|
|
|
|
cert_key: config.cert_key
|
2021-09-09 19:55:49 +03:00
|
|
|
in_memory_verification: config.in_memory_verification
|
2022-01-26 18:13:10 +03:00
|
|
|
allow_redirect: config.allow_redirect
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
res := req.do()!
|
2020-01-16 20:16:11 +03:00
|
|
|
return res
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// get_text sends an HTTP GET request to the given `url` and returns the text content of the response.
|
2019-07-31 23:10:28 +03:00
|
|
|
pub fn get_text(url string) string {
|
2021-08-17 09:16:59 +03:00
|
|
|
resp := fetch(url: url, method: .get) or { return '' }
|
2022-05-29 20:27:18 +03:00
|
|
|
return resp.body
|
2019-10-24 19:44:49 +03:00
|
|
|
}
|
2019-07-31 23:10:28 +03:00
|
|
|
|
2023-01-27 14:58:55 +03:00
|
|
|
// url_encode_form_data converts mapped data to a URL encoded string.
|
2020-01-16 20:16:11 +03:00
|
|
|
pub fn url_encode_form_data(data map[string]string) string {
|
2020-04-26 14:49:31 +03:00
|
|
|
mut pieces := []string{}
|
2020-07-05 16:27:37 +03:00
|
|
|
for key_, value_ in data {
|
|
|
|
key := urllib.query_escape(key_)
|
|
|
|
value := urllib.query_escape(value_)
|
2022-11-15 16:53:13 +03:00
|
|
|
pieces << '${key}=${value}'
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
return pieces.join('&')
|
|
|
|
}
|
|
|
|
|
2021-08-17 09:16:59 +03:00
|
|
|
[deprecated: 'use fetch()']
|
2022-10-16 09:28:57 +03:00
|
|
|
fn fetch_with_method(method Method, _config FetchConfig) !Response {
|
2020-01-16 20:16:11 +03:00
|
|
|
mut config := _config
|
|
|
|
config.method = method
|
2021-08-17 09:16:59 +03:00
|
|
|
return fetch(config)
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn build_url_from_fetch(config FetchConfig) !string {
|
|
|
|
mut url := urllib.parse(config.url)!
|
2021-02-17 07:19:25 +03:00
|
|
|
if config.params.len == 0 {
|
2020-01-16 20:16:11 +03:00
|
|
|
return url.str()
|
|
|
|
}
|
2021-02-17 07:19:25 +03:00
|
|
|
mut pieces := []string{cap: config.params.len}
|
|
|
|
for key, val in config.params {
|
2022-11-15 16:53:13 +03:00
|
|
|
pieces << '${key}=${val}'
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
mut query := pieces.join('&')
|
|
|
|
if url.raw_query.len > 1 {
|
|
|
|
query = url.raw_query + '&' + query
|
|
|
|
}
|
|
|
|
url.raw_query = query
|
|
|
|
return url.str()
|
|
|
|
}
|
|
|
|
|
2021-10-02 15:19:32 +03:00
|
|
|
[deprecated: 'unescape_url is deprecated, use urllib.query_unescape() instead']
|
2019-08-06 06:54:47 +03:00
|
|
|
pub fn unescape_url(s string) string {
|
2019-10-24 19:44:49 +03:00
|
|
|
panic('http.unescape_url() was replaced with urllib.query_unescape()')
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-02 15:19:32 +03:00
|
|
|
[deprecated: 'escape_url is deprecated, use urllib.query_escape() instead']
|
2019-08-06 06:54:47 +03:00
|
|
|
pub fn escape_url(s string) string {
|
2019-10-24 19:44:49 +03:00
|
|
|
panic('http.escape_url() was replaced with urllib.query_escape()')
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-02 15:19:32 +03:00
|
|
|
[deprecated: 'unescape is deprecated, use urllib.query_escape() instead']
|
2019-08-06 06:54:47 +03:00
|
|
|
pub fn unescape(s string) string {
|
2019-10-24 19:44:49 +03:00
|
|
|
panic('http.unescape() was replaced with http.unescape_url()')
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|
|
|
|
|
2021-10-02 15:19:32 +03:00
|
|
|
[deprecated: 'escape is deprecated, use urllib.query_unescape() instead']
|
2019-08-06 06:54:47 +03:00
|
|
|
pub fn escape(s string) string {
|
2019-10-24 19:44:49 +03:00
|
|
|
panic('http.escape() was replaced with http.escape_url()')
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|