2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
2019-08-06 06:54:47 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module http
|
|
|
|
|
2022-09-22 16:50:34 +03:00
|
|
|
import net.ssl
|
2019-11-24 06:27:02 +03:00
|
|
|
import strings
|
2019-08-06 06:54:47 +03:00
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (req &Request) ssl_do(port int, method Method, host_name string, path string) !Response {
|
2022-09-22 16:50:34 +03:00
|
|
|
mut ssl_conn := ssl.new_ssl_conn(
|
|
|
|
verify: req.verify
|
|
|
|
cert: req.cert
|
|
|
|
cert_key: req.cert_key
|
|
|
|
validate: req.validate
|
|
|
|
in_memory_verification: req.in_memory_verification
|
2022-10-16 09:28:57 +03:00
|
|
|
)!
|
2022-09-22 16:50:34 +03:00
|
|
|
ssl_conn.dial(host_name, port) or { return err }
|
|
|
|
|
2020-05-18 06:10:56 +03:00
|
|
|
req_headers := req.build_request_headers(method, host_name, path)
|
2021-03-30 18:11:00 +03:00
|
|
|
$if trace_http_request ? {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('> ${req_headers}')
|
2021-03-30 18:11:00 +03:00
|
|
|
}
|
2021-02-22 18:11:02 +03:00
|
|
|
// println(req_headers)
|
2022-09-22 16:50:34 +03:00
|
|
|
ssl_conn.write_string(req_headers) or { return err }
|
|
|
|
|
2020-05-20 14:32:59 +03:00
|
|
|
mut content := strings.new_builder(100)
|
2022-04-15 15:57:45 +03:00
|
|
|
mut buff := [bufsize]u8{}
|
2021-04-25 21:40:38 +03:00
|
|
|
bp := unsafe { &buff[0] }
|
2020-05-20 12:04:28 +03:00
|
|
|
mut readcounter := 0
|
2019-08-06 06:54:47 +03:00
|
|
|
for {
|
2020-05-20 12:04:28 +03:00
|
|
|
readcounter++
|
2022-09-22 16:50:34 +03:00
|
|
|
len := ssl_conn.socket_read_into_ptr(bp, bufsize) or { break }
|
2020-05-20 12:04:28 +03:00
|
|
|
$if debug_http ? {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('ssl_do, read ${readcounter:4d} | len: ${len}')
|
2020-05-20 21:40:29 +03:00
|
|
|
eprintln('-'.repeat(20))
|
2021-02-26 02:28:47 +03:00
|
|
|
eprintln(unsafe { tos(bp, len) })
|
2020-05-20 21:40:29 +03:00
|
|
|
eprintln('-'.repeat(20))
|
2020-05-18 06:10:56 +03:00
|
|
|
}
|
2021-03-20 10:02:28 +03:00
|
|
|
unsafe { content.write_ptr(bp, len) }
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
ssl_conn.shutdown()!
|
2021-03-30 18:11:00 +03:00
|
|
|
response_text := content.str()
|
|
|
|
$if trace_http_response ? {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('< ${response_text}')
|
2021-03-30 18:11:00 +03:00
|
|
|
}
|
|
|
|
return parse_response(response_text)
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|