http: use bufsize const

pull/4967/head
Alexander Medvednikov 2020-05-20 19:21:57 +02:00
parent e49ca2a799
commit 5d08c9d5a8
2 changed files with 30 additions and 30 deletions

View File

@ -5,6 +5,8 @@ module http
import net.urllib
import net.http.chunked
import strings
import net
const (
max_redirects = 4
@ -377,3 +379,31 @@ pub fn unescape(s string) string {
pub fn escape(s string) string {
panic('http.escape() was replaced with http.escape_url()')
}
const (
bufsize = 512
)
fn (req &Request) http_do(port int, method, host_name, path string) ?Response {
rbuffer := [bufsize]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())
}

View File

@ -1,30 +0,0 @@
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())
}