2019-08-09 12:52:14 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module http
|
|
|
|
|
2019-08-10 10:05:59 +02:00
|
|
|
import strings
|
|
|
|
import net.urllib
|
2019-08-09 12:52:14 +02:00
|
|
|
|
2019-08-10 08:21:20 +02:00
|
|
|
|
2019-08-09 12:52:14 +02:00
|
|
|
#flag windows -I @VROOT/thirdparty/vschannel
|
2019-08-10 10:05:59 +02:00
|
|
|
#flag -l ws2_32
|
|
|
|
#flag -l crypt32
|
2019-08-09 12:52:14 +02:00
|
|
|
|
|
|
|
#include "vschannel.c"
|
|
|
|
|
2019-08-10 10:05:59 +02:00
|
|
|
const (
|
|
|
|
max_redirects = 4
|
|
|
|
)
|
2019-08-09 12:52:14 +02:00
|
|
|
|
|
|
|
fn init_module() {}
|
|
|
|
|
2019-08-10 10:05:59 +02:00
|
|
|
fn ssl_do(method, host_name, path string) Response {
|
|
|
|
C.vschannel_init()
|
|
|
|
// TODO: joe-c
|
|
|
|
// dynamically increase in vschannel.c if needed
|
|
|
|
mut buff := malloc(44000)
|
2019-08-09 12:52:14 +02:00
|
|
|
|
2019-08-10 10:05:59 +02:00
|
|
|
mut p := if path == '' { '/' } else { path }
|
|
|
|
mut req := build_request_headers('', method, host_name, p)
|
|
|
|
mut length := int(C.request(host_name.str, req.str, buff))
|
|
|
|
mut resp := parse_response(string(buff, length))
|
2019-08-09 12:52:14 +02:00
|
|
|
|
2019-08-10 10:05:59 +02:00
|
|
|
mut no_redirects := 0
|
|
|
|
for resp.status_code == 301 && no_redirects <= max_redirects {
|
|
|
|
u := urllib.parse(resp.headers['Location']) or { break }
|
|
|
|
p = if u.path == '' { '/' } else { u.path }
|
|
|
|
req = build_request_headers('', method, u.hostname(), p)
|
|
|
|
length = int(C.request(u.hostname().str, req.str, buff))
|
|
|
|
resp = parse_response(string(buff, length))
|
|
|
|
no_redirects++
|
2019-08-09 12:52:14 +02:00
|
|
|
}
|
|
|
|
|
2019-08-10 10:05:59 +02:00
|
|
|
free(buff)
|
|
|
|
C.vschannel_cleanup()
|
2019-08-09 12:52:14 +02:00
|
|
|
return resp
|
|
|
|
}
|