2019-06-23 05:21:30 +03:00
|
|
|
// 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.
|
2019-06-22 21:20:28 +03:00
|
|
|
module http
|
|
|
|
|
2019-08-06 15:43:09 +03:00
|
|
|
import net.urllib
|
2019-12-30 07:42:23 +03:00
|
|
|
import net.http.chunked
|
2019-08-06 15:43:09 +03:00
|
|
|
|
2019-08-17 15:50:47 +03:00
|
|
|
const (
|
|
|
|
max_redirects = 4
|
|
|
|
)
|
|
|
|
|
2019-10-24 19:44:49 +03:00
|
|
|
pub struct Request {
|
2019-06-22 21:20:28 +03:00
|
|
|
pub:
|
2019-12-22 01:41:42 +03:00
|
|
|
headers map[string]string
|
|
|
|
method string
|
2019-06-22 21:20:28 +03:00
|
|
|
// cookies map[string]string
|
2019-12-22 01:41:42 +03:00
|
|
|
h string
|
|
|
|
cmd string
|
|
|
|
typ string // GET POST
|
|
|
|
data string
|
|
|
|
url string
|
|
|
|
verbose bool
|
2019-08-25 01:48:06 +03:00
|
|
|
user_agent string
|
2019-12-03 13:08:57 +03:00
|
|
|
mut:
|
2019-12-22 01:41:42 +03:00
|
|
|
user_ptr voidptr
|
|
|
|
ws_func voidptr
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 19:44:49 +03:00
|
|
|
pub struct Response {
|
2019-06-22 21:20:28 +03:00
|
|
|
pub:
|
2019-07-31 23:10:28 +03:00
|
|
|
text string
|
2019-10-24 19:44:49 +03:00
|
|
|
headers map[string]string
|
2019-06-22 21:20:28 +03:00
|
|
|
status_code int
|
|
|
|
}
|
|
|
|
|
2020-01-15 01:19:50 +03:00
|
|
|
pub fn get(url string) ?Response {
|
|
|
|
return method('GET', url, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn head(url string) ?Response {
|
|
|
|
return method('HEAD', url, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(url string) ?Response {
|
|
|
|
return method('DELETE', url, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn patch(url string) ?Response {
|
|
|
|
return method('PATCH', url, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn put(url string) ?Response {
|
|
|
|
return method('PUT', url, '')
|
2019-07-29 20:18:26 +03:00
|
|
|
}
|
|
|
|
|
2019-07-31 23:10:28 +03:00
|
|
|
pub fn post(url, data string) ?Response {
|
|
|
|
req := new_request('POST', url, data) or {
|
2019-07-29 20:18:26 +03:00
|
|
|
return error(err)
|
|
|
|
}
|
2019-10-10 20:24:36 +03:00
|
|
|
res := req.do() or {
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return res
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 01:19:50 +03:00
|
|
|
pub fn method(mname string, url string, data string) ?Response {
|
|
|
|
req := new_request(mname, url, data) or { return error(err) }
|
|
|
|
res := req.do() or { return error(err)}
|
2020-01-04 00:07:58 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-11-18 00:42:25 +03:00
|
|
|
// new_request creates a new HTTP request
|
2019-07-29 20:18:26 +03:00
|
|
|
pub fn new_request(typ, _url, _data string) ?Request {
|
|
|
|
if _url == '' {
|
2019-11-12 19:23:53 +03:00
|
|
|
return error('http.new_request: empty url')
|
2019-07-29 20:18:26 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
mut url := _url
|
|
|
|
mut data := _data
|
|
|
|
// req.headers['User-Agent'] = 'V $VERSION'
|
|
|
|
if typ == 'GET' && !url.contains('?') && data != '' {
|
|
|
|
url = '$url?$data'
|
|
|
|
data = ''
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
return Request{
|
2019-06-22 21:20:28 +03:00
|
|
|
typ: typ
|
2019-07-13 15:10:15 +03:00
|
|
|
url: url
|
|
|
|
data: data
|
2019-06-22 21:20:28 +03:00
|
|
|
ws_func: 0
|
|
|
|
user_ptr: 0
|
2019-08-17 02:55:11 +03:00
|
|
|
headers: map[string]string
|
2019-08-25 01:48:06 +03:00
|
|
|
user_agent: 'v'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:10:28 +03:00
|
|
|
pub fn get_text(url string) string {
|
2019-12-22 01:41:42 +03:00
|
|
|
resp := get(url) or {
|
|
|
|
return ''
|
|
|
|
}
|
2019-10-24 19:44:49 +03:00
|
|
|
return resp.text
|
|
|
|
}
|
2019-07-31 23:10:28 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (req mut Request) free() {
|
|
|
|
req.headers.free()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (resp mut Response) free() {
|
|
|
|
resp.headers.free()
|
|
|
|
}
|
|
|
|
|
2019-11-18 00:42:25 +03:00
|
|
|
// add_header adds the key and value of an HTTP request header
|
2019-06-26 18:45:48 +03:00
|
|
|
pub fn (req mut Request) add_header(key, val string) {
|
2019-06-22 21:20:28 +03:00
|
|
|
req.headers[key] = val
|
|
|
|
}
|
|
|
|
|
2019-09-05 15:46:24 +03:00
|
|
|
pub fn parse_headers(lines []string) map[string]string {
|
|
|
|
mut headers := map[string]string
|
|
|
|
for i, line in lines {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
words := line.split(': ')
|
|
|
|
if words.len != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
headers[words[0]] = words[1]
|
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2019-11-18 00:42:25 +03:00
|
|
|
// do will send the HTTP request and returns `http.Response` as soon as the response is recevied
|
2019-08-17 17:02:01 +03:00
|
|
|
pub fn (req &Request) do() ?Response {
|
2019-08-06 06:54:47 +03:00
|
|
|
if req.typ == 'POST' {
|
|
|
|
// req.headers << 'Content-Type: application/x-www-form-urlencoded'
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
url := urllib.parse(req.url) or {
|
|
|
|
return error('http.request.do: invalid URL "$req.url"')
|
|
|
|
}
|
2019-08-21 20:04:06 +03:00
|
|
|
mut rurl := url
|
2019-12-23 13:37:52 +03:00
|
|
|
mut resp := Response{}
|
2019-08-17 15:50:47 +03:00
|
|
|
mut no_redirects := 0
|
2019-08-21 20:04:06 +03:00
|
|
|
for {
|
2019-12-22 01:41:42 +03:00
|
|
|
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.typ, rurl) or {
|
|
|
|
return error(err)
|
|
|
|
}
|
2019-08-21 20:04:06 +03:00
|
|
|
resp = qresp
|
2019-12-22 01:41:42 +03:00
|
|
|
if !(resp.status_code in [301, 302, 303, 307, 308]) {
|
|
|
|
break
|
|
|
|
}
|
2019-08-21 20:04:06 +03:00
|
|
|
// follow any redirects
|
|
|
|
redirect_url := resp.headers['Location']
|
2019-12-22 01:41:42 +03:00
|
|
|
qrurl := urllib.parse(redirect_url) or {
|
|
|
|
return error('http.request.do: invalid URL in redirect "$redirect_url"')
|
|
|
|
}
|
2019-08-21 20:04:06 +03:00
|
|
|
rurl = qrurl
|
2019-08-17 15:50:47 +03:00
|
|
|
no_redirects++
|
|
|
|
}
|
|
|
|
return resp
|
2019-08-10 11:05:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-25 01:48:06 +03:00
|
|
|
fn (req &Request) method_and_url_to_response(method string, url net_dot_urllib.URL) ?Response {
|
|
|
|
host_name := url.hostname()
|
|
|
|
scheme := url.scheme
|
2019-12-06 15:24:53 +03:00
|
|
|
p := url.path.trim_left('/')
|
|
|
|
path := if url.query().size > 0 { '/$p?${url.query().encode()}' } else { '/$p' }
|
2019-08-21 20:04:06 +03:00
|
|
|
mut nport := url.port().int()
|
|
|
|
if nport == 0 {
|
2019-12-22 01:41:42 +03:00
|
|
|
if scheme == 'http' {
|
|
|
|
nport = 80
|
|
|
|
}
|
|
|
|
if scheme == 'https' {
|
|
|
|
nport = 443
|
|
|
|
}
|
2019-08-21 20:04:06 +03:00
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
// println('fetch $method, $scheme, $host_name, $nport, $path ')
|
2019-08-21 20:04:06 +03:00
|
|
|
if scheme == 'https' {
|
2019-12-22 01:41:42 +03:00
|
|
|
// println('ssl_do( $nport, $method, $host_name, $path )')
|
|
|
|
res := req.ssl_do(nport, method, host_name, path) or {
|
2019-10-10 20:24:36 +03:00
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return res
|
2019-12-22 01:41:42 +03:00
|
|
|
}
|
|
|
|
else if scheme == 'http' {
|
|
|
|
// println('http_do( $nport, $method, $host_name, $path )')
|
|
|
|
res := req.http_do(nport, method, host_name, path) or {
|
2019-10-10 20:24:36 +03:00
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return res
|
2019-08-21 20:04:06 +03:00
|
|
|
}
|
2019-11-12 19:23:53 +03:00
|
|
|
return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"')
|
2019-08-21 20:04:06 +03:00
|
|
|
}
|
|
|
|
|
2019-08-10 11:05:59 +03:00
|
|
|
fn parse_response(resp string) Response {
|
2019-08-17 02:55:11 +03:00
|
|
|
mut headers := map[string]string
|
2019-10-24 19:44:49 +03:00
|
|
|
first_header := resp.all_before('\n')
|
|
|
|
mut status_code := 0
|
2019-08-06 06:54:47 +03:00
|
|
|
if first_header.contains('HTTP/') {
|
|
|
|
val := first_header.find_between(' ', ' ')
|
|
|
|
status_code = val.int()
|
|
|
|
}
|
2019-10-24 19:44:49 +03:00
|
|
|
mut text := ''
|
|
|
|
// Build resp headers map and separate the body
|
|
|
|
mut nl_pos := 3
|
|
|
|
mut i := 1
|
|
|
|
for {
|
|
|
|
old_pos := nl_pos
|
2019-12-22 01:41:42 +03:00
|
|
|
nl_pos = resp.index_after('\n', nl_pos + 1)
|
2019-08-06 06:54:47 +03:00
|
|
|
if nl_pos == -1 {
|
2019-10-24 19:44:49 +03:00
|
|
|
break
|
|
|
|
}
|
2019-10-27 10:03:15 +03:00
|
|
|
h := resp[old_pos + 1..nl_pos]
|
2019-10-24 19:44:49 +03:00
|
|
|
// End of headers
|
2019-08-06 06:54:47 +03:00
|
|
|
if h.len <= 1 {
|
2019-10-27 10:03:15 +03:00
|
|
|
text = resp[nl_pos + 1..]
|
2019-10-24 19:44:49 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
2019-11-30 15:27:16 +03:00
|
|
|
pos := h.index(':') or {
|
2019-08-06 06:54:47 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
// if h.contains('Content-Type') {
|
|
|
|
// continue
|
|
|
|
// }
|
2019-10-27 10:03:15 +03:00
|
|
|
key := h[..pos]
|
2019-12-22 01:41:42 +03:00
|
|
|
val := h[pos + 2..]
|
2019-08-06 06:54:47 +03:00
|
|
|
headers[key] = val.trim_space()
|
2019-08-07 04:57:47 +03:00
|
|
|
}
|
|
|
|
if headers['Transfer-Encoding'] == 'chunked' {
|
2019-12-22 01:41:42 +03:00
|
|
|
text = chunked.decode(text)
|
2019-08-07 04:57:47 +03:00
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
return Response{
|
2019-10-24 19:44:49 +03:00
|
|
|
status_code: status_code
|
2019-08-06 06:54:47 +03:00
|
|
|
headers: headers
|
2019-10-24 19:44:49 +03:00
|
|
|
text: text
|
2019-08-10 11:05:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 01:48:06 +03:00
|
|
|
fn (req &Request) build_request_headers(method, host_name, path string) string {
|
|
|
|
ua := req.user_agent
|
|
|
|
mut uheaders := []string
|
2019-11-30 15:27:16 +03:00
|
|
|
for key, val in req.headers {
|
2019-10-24 19:44:49 +03:00
|
|
|
uheaders << '${key}: ${val}\r\n'
|
2019-08-25 01:48:06 +03:00
|
|
|
}
|
|
|
|
if req.data.len > 0 {
|
|
|
|
uheaders << 'Content-Length: ${req.data.len}\r\n'
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
return '$method $path HTTP/1.1\r\n' + 'Host: $host_name\r\n' + 'User-Agent: $ua\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' + req.data
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-22 01:41:42 +03:00
|
|
|
type wsfn fn(s string, ptr voidptr)
|