net.http: add ability to parametrize the read and write timeouts of a http request (#10482)

pull/10484/head
Andreas Heissenberger 2021-06-16 19:21:49 +02:00 committed by GitHub
parent 7b52dbfdf8
commit b2e2a53f98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import io
import net import net
import net.urllib import net.urllib
import strings import strings
import time
// Request holds information about an HTTP request (either received by // Request holds information about an HTTP request (either received by
// a server or to be sent by a client) // a server or to be sent by a client)
@ -21,6 +22,10 @@ pub mut:
user_agent string = 'v.http' user_agent string = 'v.http'
verbose bool verbose bool
user_ptr voidptr user_ptr voidptr
// NOT implemented for ssl connections
// time = -1 for no timeout
read_timeout i64 = 30 * time.second
write_timeout i64 = 30 * time.second
} }
fn (mut req Request) free() { fn (mut req Request) free() {
@ -138,6 +143,8 @@ fn (req &Request) http_do(host string, method Method, path string) ?Response {
host_name, _ := net.split_address(host) ? host_name, _ := net.split_address(host) ?
s := req.build_request_headers(method, host_name, path) s := req.build_request_headers(method, host_name, path)
mut client := net.dial_tcp(host) ? mut client := net.dial_tcp(host) ?
client.set_read_timeout(req.read_timeout)
client.set_write_timeout(req.write_timeout)
// TODO this really needs to be exposed somehow // TODO this really needs to be exposed somehow
client.write(s.bytes()) ? client.write(s.bytes()) ?
$if trace_http_request ? { $if trace_http_request ? {