2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 20:20:28 +02:00
|
|
|
module http
|
|
|
|
|
2019-08-06 14:43:09 +02:00
|
|
|
import net.urllib
|
2019-12-30 05:42:23 +01:00
|
|
|
import net.http.chunked
|
2020-05-20 19:21:57 +02:00
|
|
|
import net
|
2020-11-15 21:54:47 +01:00
|
|
|
import io
|
2019-08-06 14:43:09 +02:00
|
|
|
|
2019-08-17 14:50:47 +02:00
|
|
|
const (
|
2020-05-16 16:12:23 +02:00
|
|
|
max_redirects = 4
|
2020-01-16 18:16:11 +01:00
|
|
|
content_type_default = 'text/plain'
|
2020-10-15 15:17:52 +02:00
|
|
|
bufsize = 1536
|
2019-08-17 14:50:47 +02:00
|
|
|
)
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// Request holds information about an HTTP request
|
2019-10-24 18:44:49 +02:00
|
|
|
pub struct Request {
|
2020-04-28 13:57:48 +02:00
|
|
|
pub mut:
|
2021-02-27 23:18:25 +01:00
|
|
|
version Version = .v1_1
|
2020-07-27 21:20:05 +02:00
|
|
|
method Method
|
2021-04-09 18:17:33 +02:00
|
|
|
header Header
|
2020-01-16 18:16:11 +01:00
|
|
|
cookies map[string]string
|
2019-12-21 23:41:42 +01:00
|
|
|
data string
|
|
|
|
url string
|
2020-05-20 08:58:57 +02:00
|
|
|
user_agent string = 'v.http'
|
2020-01-16 18:16:11 +01:00
|
|
|
verbose bool
|
2019-12-21 23:41:42 +01:00
|
|
|
user_ptr voidptr
|
|
|
|
ws_func voidptr
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// FetchConfig holds configurations of fetch
|
2020-01-16 18:16:11 +01:00
|
|
|
pub struct FetchConfig {
|
|
|
|
pub mut:
|
2020-07-27 21:20:05 +02:00
|
|
|
method Method
|
2021-04-09 18:17:33 +02:00
|
|
|
header Header
|
2020-04-09 14:03:47 +02:00
|
|
|
data string
|
|
|
|
params map[string]string
|
|
|
|
cookies map[string]string
|
2020-05-20 08:58:57 +02:00
|
|
|
user_agent string = 'v.http'
|
2020-09-09 15:34:41 +02:00
|
|
|
verbose bool
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// Response represents the result of the request
|
2019-10-24 18:44:49 +02:00
|
|
|
pub struct Response {
|
2019-06-22 20:20:28 +02:00
|
|
|
pub:
|
2019-07-31 22:10:28 +02:00
|
|
|
text string
|
2021-04-09 18:17:33 +02:00
|
|
|
header Header
|
2020-01-16 18:16:11 +01:00
|
|
|
cookies map[string]string
|
2019-06-22 20:20:28 +02:00
|
|
|
status_code int
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
pub fn new_request(method Method, url_ string, data string) ?Request {
|
2020-07-27 21:20:05 +02:00
|
|
|
url := if method == .get { url_ + '?' + data } else { url_ }
|
2020-10-15 15:17:52 +02:00
|
|
|
// println('new req() method=$method url="$url" dta="$data"')
|
2020-05-10 02:07:15 +02:00
|
|
|
return Request{
|
2020-07-27 21:20:05 +02:00
|
|
|
method: method
|
2020-05-10 02:07:15 +02:00
|
|
|
url: url
|
2020-12-22 08:32:32 +01:00
|
|
|
data: data
|
|
|
|
/*
|
|
|
|
headers: {
|
2020-05-18 05:10:56 +02:00
|
|
|
'Accept-Encoding': 'compress'
|
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
*/
|
2020-05-10 02:07:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// get sends a GET HTTP request to the URL
|
2020-01-16 18:16:11 +01:00
|
|
|
pub fn get(url string) ?Response {
|
2020-07-27 21:20:05 +02:00
|
|
|
return fetch_with_method(.get, url, FetchConfig{})
|
2020-01-14 23:19:50 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// post sends a POST HTTP request to the URL with a string data
|
2020-10-15 15:17:52 +02:00
|
|
|
pub fn post(url string, data string) ?Response {
|
2021-03-30 17:11:00 +02:00
|
|
|
return fetch_with_method(.post, url,
|
2021-01-26 15:43:10 +01:00
|
|
|
data: data
|
2021-04-09 18:17:33 +02:00
|
|
|
header: new_header({key: .content_type, value: http.content_type_default})
|
2021-01-26 15:43:10 +01:00
|
|
|
)
|
2020-01-14 23:19:50 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// post_json sends a POST HTTP request to the URL with a JSON data
|
2020-10-15 15:17:52 +02:00
|
|
|
pub fn post_json(url string, data string) ?Response {
|
2021-03-30 17:11:00 +02:00
|
|
|
return fetch_with_method(.post, url,
|
2021-01-26 15:43:10 +01:00
|
|
|
data: data
|
2021-04-09 18:17:33 +02:00
|
|
|
header: new_header({key: .content_type, value: 'application/json'})
|
2021-01-26 15:43:10 +01:00
|
|
|
)
|
2020-06-30 14:36:11 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// post_form sends a POST HTTP request to the URL with X-WWW-FORM-URLENCODED data
|
2020-01-16 18:16:11 +01:00
|
|
|
pub fn post_form(url string, data map[string]string) ?Response {
|
2021-04-09 18:17:33 +02:00
|
|
|
return fetch_with_method(.post, url,
|
|
|
|
header: new_header({key: .content_type, value: 'application/x-www-form-urlencoded'})
|
2021-01-26 15:43:10 +01:00
|
|
|
data: url_encode_form_data(data)
|
|
|
|
)
|
2020-01-14 23:19:50 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// put sends a PUT HTTP request to the URL with a string data
|
2020-10-15 15:17:52 +02:00
|
|
|
pub fn put(url string, data string) ?Response {
|
2021-03-30 17:11:00 +02:00
|
|
|
return fetch_with_method(.put, url,
|
2021-01-26 15:43:10 +01:00
|
|
|
data: data
|
2021-04-09 18:17:33 +02:00
|
|
|
header: new_header({key: .content_type, value: http.content_type_default})
|
2021-01-26 15:43:10 +01:00
|
|
|
)
|
2020-01-14 23:19:50 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// patch sends a PATCH HTTP request to the URL with a string data
|
2020-10-15 15:17:52 +02:00
|
|
|
pub fn patch(url string, data string) ?Response {
|
2021-03-30 17:11:00 +02:00
|
|
|
return fetch_with_method(.patch, url,
|
2021-01-26 15:43:10 +01:00
|
|
|
data: data
|
2021-04-09 18:17:33 +02:00
|
|
|
header: new_header({key: .content_type, value: http.content_type_default})
|
2021-01-26 15:43:10 +01:00
|
|
|
)
|
2019-07-29 19:18:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// head sends a HEAD HTTP request to the URL
|
2020-01-16 18:16:11 +01:00
|
|
|
pub fn head(url string) ?Response {
|
2020-07-27 21:20:05 +02:00
|
|
|
return fetch_with_method(.head, url, FetchConfig{})
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// delete sends a DELETE HTTP request to the URL
|
2020-01-16 18:16:11 +01:00
|
|
|
pub fn delete(url string) ?Response {
|
2020-07-27 21:20:05 +02:00
|
|
|
return fetch_with_method(.delete, url, FetchConfig{})
|
2020-01-03 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// fetch sends an HTTP request to the URL with the given method and configurations
|
2020-01-16 18:16:11 +01:00
|
|
|
pub fn fetch(_url string, config FetchConfig) ?Response {
|
2019-07-29 19:18:26 +02:00
|
|
|
if _url == '' {
|
2020-01-16 18:16:11 +01:00
|
|
|
return error('http.fetch: empty url')
|
2019-07-29 19:18:26 +02:00
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
url := build_url_from_fetch(_url, config) or { return error('http.fetch: invalid url $_url') }
|
2020-01-16 18:16:11 +01:00
|
|
|
data := config.data
|
|
|
|
req := Request{
|
2020-07-27 21:20:05 +02:00
|
|
|
method: config.method
|
2019-07-13 14:10:15 +02:00
|
|
|
url: url
|
|
|
|
data: data
|
2021-04-09 18:17:33 +02:00
|
|
|
header: config.header
|
2020-01-16 18:16:11 +01:00
|
|
|
cookies: config.cookies
|
2020-01-29 04:05:37 +01:00
|
|
|
user_agent: config.user_agent
|
2019-06-22 20:20:28 +02:00
|
|
|
ws_func: 0
|
|
|
|
user_ptr: 0
|
2020-01-16 18:16:11 +01:00
|
|
|
verbose: config.verbose
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-10-15 15:17:52 +02:00
|
|
|
res := req.do() ?
|
2020-01-16 18:16:11 +01:00
|
|
|
return res
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// get_text sends a GET HTTP request to the URL and returns the text content of the response
|
2019-07-31 22:10:28 +02:00
|
|
|
pub fn get_text(url string) string {
|
2020-12-22 08:32:32 +01:00
|
|
|
resp := fetch(url, method: .get) or { return '' }
|
2019-10-24 18:44:49 +02:00
|
|
|
return resp.text
|
|
|
|
}
|
2019-07-31 22:10:28 +02:00
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// url_encode_form_data converts mapped data to an URL encoded string
|
2020-01-16 18:16:11 +01:00
|
|
|
pub fn url_encode_form_data(data map[string]string) string {
|
2020-04-26 13:49:31 +02:00
|
|
|
mut pieces := []string{}
|
2020-07-05 15:27:37 +02:00
|
|
|
for key_, value_ in data {
|
|
|
|
key := urllib.query_escape(key_)
|
|
|
|
value := urllib.query_escape(value_)
|
2020-01-16 18:16:11 +01:00
|
|
|
pieces << '$key=$value'
|
|
|
|
}
|
|
|
|
return pieces.join('&')
|
|
|
|
}
|
|
|
|
|
2020-07-27 21:20:05 +02:00
|
|
|
fn fetch_with_method(method Method, url string, _config FetchConfig) ?Response {
|
2020-01-16 18:16:11 +01:00
|
|
|
mut config := _config
|
|
|
|
config.method = method
|
|
|
|
return fetch(url, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_url_from_fetch(_url string, config FetchConfig) ?string {
|
2020-10-15 15:17:52 +02:00
|
|
|
mut url := urllib.parse(_url) ?
|
2021-02-17 05:19:25 +01:00
|
|
|
if config.params.len == 0 {
|
2020-01-16 18:16:11 +01:00
|
|
|
return url.str()
|
|
|
|
}
|
2021-02-17 05:19:25 +01:00
|
|
|
mut pieces := []string{cap: config.params.len}
|
|
|
|
for key, val in config.params {
|
|
|
|
pieces << '$key=$val'
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
|
|
|
mut query := pieces.join('&')
|
|
|
|
if url.raw_query.len > 1 {
|
|
|
|
query = url.raw_query + '&' + query
|
|
|
|
}
|
|
|
|
url.raw_query = query
|
|
|
|
return url.str()
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:12:23 +02:00
|
|
|
fn (mut req Request) free() {
|
2021-04-09 18:17:33 +02:00
|
|
|
unsafe { req.header.free() }
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:12:23 +02:00
|
|
|
fn (mut resp Response) free() {
|
2021-04-09 18:17:33 +02:00
|
|
|
unsafe { resp.header.data.free() }
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-11-17 22:42:25 +01:00
|
|
|
// add_header adds the key and value of an HTTP request header
|
2021-04-09 18:17:33 +02:00
|
|
|
// To add a custom header, use add_custom_header
|
|
|
|
pub fn (mut req Request) add_header(key CommonHeader, val string) {
|
|
|
|
req.header.add(key, val)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 18:17:33 +02:00
|
|
|
// add_custom_header adds the key and value of an HTTP request header
|
|
|
|
// This method may fail if the key contains characters that are not permitted
|
|
|
|
pub fn (mut req Request) add_custom_header(key string, val string) ? {
|
|
|
|
return req.header.add_custom(key, val)
|
2019-09-05 14:46:24 +02:00
|
|
|
}
|
|
|
|
|
2019-11-17 22:42:25 +01:00
|
|
|
// do will send the HTTP request and returns `http.Response` as soon as the response is recevied
|
2019-08-17 16:02:01 +02:00
|
|
|
pub fn (req &Request) do() ?Response {
|
2020-12-22 08:32:32 +01:00
|
|
|
mut url := urllib.parse(req.url) or { return error('http.Request.do: invalid url $req.url') }
|
2019-08-21 19:04:06 +02:00
|
|
|
mut rurl := url
|
2019-12-23 11:37:52 +01:00
|
|
|
mut resp := Response{}
|
2019-08-17 14:50:47 +02:00
|
|
|
mut no_redirects := 0
|
2019-08-21 19:04:06 +02:00
|
|
|
for {
|
2021-01-26 15:43:10 +01:00
|
|
|
if no_redirects == http.max_redirects {
|
|
|
|
return error('http.request.do: maximum number of redirects reached ($http.max_redirects)')
|
2019-12-21 23:41:42 +01:00
|
|
|
}
|
2020-10-15 15:17:52 +02:00
|
|
|
qresp := req.method_and_url_to_response(req.method, rurl) ?
|
2019-08-21 19:04:06 +02:00
|
|
|
resp = qresp
|
2020-04-26 06:39:23 +02:00
|
|
|
if resp.status_code !in [301, 302, 303, 307, 308] {
|
2019-12-21 23:41:42 +01:00
|
|
|
break
|
|
|
|
}
|
2019-08-21 19:04:06 +02:00
|
|
|
// follow any redirects
|
2021-04-09 18:17:33 +02:00
|
|
|
mut redirect_url := resp.header.get(.location) or { '' }
|
2020-02-12 14:52:39 +01:00
|
|
|
if redirect_url.len > 0 && redirect_url[0] == `/` {
|
|
|
|
url.set_path(redirect_url) or {
|
|
|
|
return error('http.request.do: invalid path in redirect: "$redirect_url"')
|
|
|
|
}
|
|
|
|
redirect_url = url.str()
|
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
qrurl := urllib.parse(redirect_url) or {
|
|
|
|
return error('http.request.do: invalid URL in redirect "$redirect_url"')
|
|
|
|
}
|
2019-08-21 19:04:06 +02:00
|
|
|
rurl = qrurl
|
2019-08-17 14:50:47 +02:00
|
|
|
no_redirects++
|
|
|
|
}
|
|
|
|
return resp
|
2019-08-10 10:05:59 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 21:20:05 +02:00
|
|
|
fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) ?Response {
|
2019-08-25 00:48:06 +02:00
|
|
|
host_name := url.hostname()
|
|
|
|
scheme := url.scheme
|
2019-12-06 13:24:53 +01:00
|
|
|
p := url.path.trim_left('/')
|
2020-10-15 15:17:52 +02:00
|
|
|
path := if url.query().len > 0 { '/$p?$url.query().encode()' } else { '/$p' }
|
2019-08-21 19:04:06 +02:00
|
|
|
mut nport := url.port().int()
|
|
|
|
if nport == 0 {
|
2019-12-21 23:41:42 +01:00
|
|
|
if scheme == 'http' {
|
|
|
|
nport = 80
|
|
|
|
}
|
|
|
|
if scheme == 'https' {
|
|
|
|
nport = 443
|
|
|
|
}
|
2019-08-21 19:04:06 +02:00
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
// println('fetch $method, $scheme, $host_name, $nport, $path ')
|
2019-08-21 19:04:06 +02:00
|
|
|
if scheme == 'https' {
|
2019-12-21 23:41:42 +01:00
|
|
|
// println('ssl_do( $nport, $method, $host_name, $path )')
|
2020-10-15 15:17:52 +02:00
|
|
|
res := req.ssl_do(nport, method, host_name, path) ?
|
2019-10-10 19:24:36 +02:00
|
|
|
return res
|
2020-05-16 16:12:23 +02:00
|
|
|
} else if scheme == 'http' {
|
2019-12-21 23:41:42 +01:00
|
|
|
// println('http_do( $nport, $method, $host_name, $path )')
|
2020-12-22 08:32:32 +01:00
|
|
|
res := req.http_do('$host_name:$nport', method, path) ?
|
2019-10-10 19:24:36 +02:00
|
|
|
return res
|
2019-08-21 19:04:06 +02:00
|
|
|
}
|
2019-11-12 17:23:53 +01:00
|
|
|
return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"')
|
2019-08-21 19:04:06 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 22:42:25 +02:00
|
|
|
pub fn parse_response(resp string) Response {
|
2021-04-09 18:17:33 +02:00
|
|
|
mut header := new_header()
|
2020-01-16 18:16:11 +01:00
|
|
|
// TODO: Cookie data type
|
2020-05-16 16:12:23 +02:00
|
|
|
mut cookies := map[string]string{}
|
2019-10-24 18:44:49 +02:00
|
|
|
first_header := resp.all_before('\n')
|
|
|
|
mut status_code := 0
|
2019-08-06 05:54:47 +02:00
|
|
|
if first_header.contains('HTTP/') {
|
|
|
|
val := first_header.find_between(' ', ' ')
|
|
|
|
status_code = val.int()
|
|
|
|
}
|
2019-10-24 18:44:49 +02:00
|
|
|
mut text := ''
|
2021-04-09 18:17:33 +02:00
|
|
|
// Build resp header map and separate the body
|
2019-10-24 18:44:49 +02:00
|
|
|
mut nl_pos := 3
|
|
|
|
mut i := 1
|
|
|
|
for {
|
|
|
|
old_pos := nl_pos
|
2019-12-21 23:41:42 +01:00
|
|
|
nl_pos = resp.index_after('\n', nl_pos + 1)
|
2019-08-06 05:54:47 +02:00
|
|
|
if nl_pos == -1 {
|
2019-10-24 18:44:49 +02:00
|
|
|
break
|
|
|
|
}
|
2019-10-27 08:03:15 +01:00
|
|
|
h := resp[old_pos + 1..nl_pos]
|
2019-10-24 18:44:49 +02:00
|
|
|
// End of headers
|
2019-08-06 05:54:47 +02:00
|
|
|
if h.len <= 1 {
|
2019-10-27 08:03:15 +01:00
|
|
|
text = resp[nl_pos + 1..]
|
2019-10-24 18:44:49 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
2020-12-22 08:32:32 +01:00
|
|
|
pos := h.index(':') or { continue }
|
2020-06-07 23:01:20 +02:00
|
|
|
mut key := h[..pos]
|
2021-04-09 18:17:33 +02:00
|
|
|
val := h[pos + 2..].trim_space()
|
|
|
|
header.add_custom(key, val) or { eprintln('error parsing header: $err') }
|
|
|
|
}
|
|
|
|
// set cookies
|
|
|
|
for cookie in header.values(.set_cookie) {
|
|
|
|
parts := cookie.split_nth('=', 2)
|
|
|
|
cookies[parts[0]] = parts[1]
|
2019-08-07 03:57:47 +02:00
|
|
|
}
|
2021-04-09 18:17:33 +02:00
|
|
|
if header.get(.transfer_encoding) or { '' } == 'chunked' || header.get(.content_length) or { '' } == '' {
|
2019-12-21 23:41:42 +01:00
|
|
|
text = chunked.decode(text)
|
2019-08-07 03:57:47 +02:00
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
return Response{
|
2019-10-24 18:44:49 +02:00
|
|
|
status_code: status_code
|
2021-04-09 18:17:33 +02:00
|
|
|
header: header
|
2020-01-16 18:16:11 +01:00
|
|
|
cookies: cookies
|
2019-10-24 18:44:49 +02:00
|
|
|
text: text
|
2019-08-10 10:05:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn (req &Request) build_request_headers(method Method, host_name string, path string) string {
|
2019-08-25 00:48:06 +02:00
|
|
|
ua := req.user_agent
|
2020-04-26 13:49:31 +02:00
|
|
|
mut uheaders := []string{}
|
2021-04-09 18:17:33 +02:00
|
|
|
if !req.header.contains(.host) {
|
2020-01-16 18:16:11 +01:00
|
|
|
uheaders << 'Host: $host_name\r\n'
|
|
|
|
}
|
2021-04-09 18:17:33 +02:00
|
|
|
if !req.header.contains(.user_agent) {
|
2020-01-16 18:16:11 +01:00
|
|
|
uheaders << 'User-Agent: $ua\r\n'
|
|
|
|
}
|
2021-04-09 18:17:33 +02:00
|
|
|
if req.data.len > 0 && !req.header.contains(.content_length) {
|
2020-10-15 15:17:52 +02:00
|
|
|
uheaders << 'Content-Length: $req.data.len\r\n'
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
2021-04-09 18:17:33 +02:00
|
|
|
for key in req.header.keys() {
|
|
|
|
if key == CommonHeader.cookie.str() {
|
2020-01-16 18:16:11 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-04-09 18:17:33 +02:00
|
|
|
val := req.header.custom_values(key).join('; ')
|
2020-10-15 15:17:52 +02:00
|
|
|
uheaders << '$key: $val\r\n'
|
2019-08-25 00:48:06 +02:00
|
|
|
}
|
2020-01-16 18:16:11 +01:00
|
|
|
uheaders << req.build_request_cookies_header()
|
2021-02-27 23:18:25 +01:00
|
|
|
version := if req.version == .unknown { Version.v1_1 } else { req.version }
|
|
|
|
return '$method $path $version\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' + req.data
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (req &Request) build_request_cookies_header() string {
|
|
|
|
if req.cookies.keys().len < 1 {
|
|
|
|
return ''
|
|
|
|
}
|
2020-04-26 13:49:31 +02:00
|
|
|
mut cookie := []string{}
|
2020-01-16 18:16:11 +01:00
|
|
|
for key, val in req.cookies {
|
2020-07-26 15:54:18 +02:00
|
|
|
cookie << '$key=$val'
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
2021-04-09 18:17:33 +02:00
|
|
|
cookie << req.header.values(.cookie)
|
2020-01-16 18:16:11 +01:00
|
|
|
return 'Cookie: ' + cookie.join('; ') + '\r\n'
|
2019-08-06 05:54:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// unescape_url is deprecated, use urllib.query_unescape() instead
|
2019-08-06 05:54:47 +02:00
|
|
|
pub fn unescape_url(s string) string {
|
2019-10-24 18:44:49 +02:00
|
|
|
panic('http.unescape_url() was replaced with urllib.query_unescape()')
|
2019-08-06 05:54:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// escape_url is deprecated, use urllib.query_escape() instead
|
2019-08-06 05:54:47 +02:00
|
|
|
pub fn escape_url(s string) string {
|
2019-10-24 18:44:49 +02:00
|
|
|
panic('http.escape_url() was replaced with urllib.query_escape()')
|
2019-08-06 05:54:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// unescape is deprecated, use urllib.query_escape() instead
|
2019-08-06 05:54:47 +02:00
|
|
|
pub fn unescape(s string) string {
|
2019-10-24 18:44:49 +02:00
|
|
|
panic('http.unescape() was replaced with http.unescape_url()')
|
2019-08-06 05:54:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// escape is deprecated, use urllib.query_unescape() instead
|
2019-08-06 05:54:47 +02:00
|
|
|
pub fn escape(s string) string {
|
2019-10-24 18:44:49 +02:00
|
|
|
panic('http.escape() was replaced with http.escape_url()')
|
2019-08-06 05:54:47 +02:00
|
|
|
}
|
2020-05-20 19:21:57 +02:00
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
fn (req &Request) http_do(host string, method Method, path string) ?Response {
|
2020-12-22 08:32:32 +01:00
|
|
|
host_name, _ := net.split_address(host) ?
|
2020-05-20 19:21:57 +02:00
|
|
|
s := req.build_request_headers(method, host_name, path)
|
2020-12-22 08:32:32 +01:00
|
|
|
mut client := net.dial_tcp(host) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
// TODO this really needs to be exposed somehow
|
2020-12-22 08:32:32 +01:00
|
|
|
client.write(s.bytes()) ?
|
2021-03-30 17:11:00 +02:00
|
|
|
$if trace_http_request ? {
|
|
|
|
eprintln('> $s')
|
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
mut bytes := io.read_all(reader: client) ?
|
2021-01-26 15:43:10 +01:00
|
|
|
client.close() ?
|
2021-03-30 17:11:00 +02:00
|
|
|
response_text := bytes.bytestr()
|
|
|
|
$if trace_http_response ? {
|
|
|
|
eprintln('< $response_text')
|
|
|
|
}
|
|
|
|
return parse_response(response_text)
|
2020-05-20 19:21:57 +02:00
|
|
|
}
|
2020-07-07 14:37:43 +02:00
|
|
|
|
2021-03-01 10:48:38 +01:00
|
|
|
// referer returns 'Referer' header value of the given request
|
2020-07-07 14:37:43 +02:00
|
|
|
pub fn (req &Request) referer() string {
|
2021-04-09 18:17:33 +02:00
|
|
|
return req.header.get(.referer) or { '' }
|
2020-07-07 14:37:43 +02:00
|
|
|
}
|