1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vlib.http: fix http schannel & follow redirects & cleanup

This commit is contained in:
joe-conigliaro
2019-08-10 18:05:59 +10:00
committed by Alexander Medvednikov
parent 2ebfc8ab73
commit a0b59783a2
5 changed files with 164 additions and 117 deletions

View File

@@ -93,7 +93,6 @@ pub fn (req mut Request) add_header(key, val string) {
}
pub fn (req &Request) do() Response {
mut headers := map[string]string{}
if req.typ == 'POST' {
// req.headers << 'Content-Type: application/x-www-form-urlencoded'
}
@@ -108,9 +107,13 @@ pub fn (req &Request) do() Response {
if !is_ssl {
panic('non https requests are not supported right now')
}
s := ssl_do(req.typ, url.host, url.path)
// s := ssl_do(req.typ, url.host, url.path)
first_header := s.all_before('\n')
return ssl_do(req.typ, url.hostname(), url.path)
}
fn parse_response(resp string) Response {
mut headers := map[string]string{}
first_header := resp.all_before('\n')
mut status_code := 0
if first_header.contains('HTTP/') {
val := first_header.find_between(' ', ' ')
@@ -122,14 +125,14 @@ pub fn (req &Request) do() Response {
mut i := 1
for {
old_pos := nl_pos
nl_pos = s.index_after('\n', nl_pos+1)
nl_pos = resp.index_after('\n', nl_pos+1)
if nl_pos == -1 {
break
}
h := s.substr(old_pos + 1, nl_pos)
h := resp.substr(old_pos + 1, nl_pos)
// End of headers
if h.len <= 1 {
text = s.right(nl_pos + 1)
text = resp.right(nl_pos + 1)
break
}
i++
@@ -144,14 +147,24 @@ pub fn (req &Request) do() Response {
val := h.right(pos + 2)
headers[key] = val.trim_space()
}
if headers['Transfer-Encoding'] == 'chunked' {
text = chunked.decode( text )
}
return Response {
status_code: status_code
headers: headers
text: text
}
}
}
fn build_request_headers(user_agent, method, host_name, path string) string {
ua := if user_agent == '' { 'v' } else { user_agent }
return '$method $path HTTP/1.1\r\n' +
'Host: $host_name\r\n' +
'User-Agent: $ua\r\n' +
'Connection: close\r\n\r\n'
}
pub fn unescape_url(s string) string {