mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
move http module to net.http
This commit is contained in:
committed by
Alexander Medvednikov
parent
6cee50afda
commit
63b70ddb06
30
vlib/net/http/http_client.v
Normal file
30
vlib/net/http/http_client.v
Normal file
@@ -0,0 +1,30 @@
|
||||
module http
|
||||
|
||||
import net
|
||||
import strings
|
||||
|
||||
fn (req &Request) http_do(port int, method, host_name, path string) ?Response {
|
||||
bufsize := 512
|
||||
rbuffer := [512]byte
|
||||
mut sb := strings.new_builder(100)
|
||||
s := req.build_request_headers(method, host_name, path)
|
||||
client := net.dial(host_name, port) or {
|
||||
return error(err)
|
||||
}
|
||||
client.send(s.str, s.len) or {
|
||||
}
|
||||
for {
|
||||
readbytes := client.crecv(rbuffer, bufsize)
|
||||
if readbytes < 0 {
|
||||
return error('http.request.http_do: error reading response. readbytes=$readbytes')
|
||||
}
|
||||
if readbytes == 0 {
|
||||
break
|
||||
}
|
||||
sb.write(tos(rbuffer, readbytes))
|
||||
}
|
||||
client.close() or {
|
||||
}
|
||||
return parse_response(sb.str())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user