2019-08-21 20:04:06 +03:00
|
|
|
module http
|
|
|
|
|
|
|
|
import net
|
|
|
|
import strings
|
|
|
|
|
2019-08-25 01:48:06 +03:00
|
|
|
fn (req &Request) http_do(port int, method, host_name, path string) ?Response {
|
2019-08-21 20:04:06 +03:00
|
|
|
bufsize := 512
|
|
|
|
rbuffer := [512]byte
|
|
|
|
mut sb := strings.new_builder(100)
|
2019-08-25 01:48:06 +03:00
|
|
|
s := req.build_request_headers(method, host_name, path)
|
2019-08-21 20:04:06 +03:00
|
|
|
|
|
|
|
client := net.dial( host_name, port) or { return error(err) }
|
2019-11-04 02:38:49 +03:00
|
|
|
client.send( s.str, s.len ) or {}
|
2019-08-21 20:04:06 +03:00
|
|
|
for {
|
|
|
|
readbytes := client.crecv( rbuffer, bufsize )
|
2019-11-12 19:23:53 +03:00
|
|
|
if readbytes < 0 { return error('http.request.http_do: error reading response. readbytes=$readbytes') }
|
2019-08-21 20:04:06 +03:00
|
|
|
if readbytes == 0 { break }
|
|
|
|
sb.write( tos(rbuffer, readbytes) )
|
|
|
|
}
|
2019-11-04 02:38:49 +03:00
|
|
|
client.close() or {}
|
2019-08-21 20:04:06 +03:00
|
|
|
return parse_response(sb.str())
|
|
|
|
}
|