v/vlib/net/http/backend_nix.c.v

68 lines
1.9 KiB
V
Raw Normal View History

2020-02-03 05:00:36 +01:00
// Copyright (c) 2019-2020 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-11-24 04:27:02 +01:00
import strings
import net.openssl
2020-05-20 08:58:57 +02:00
const (
is_used = openssl.is_used
)
fn (req &Request) ssl_do(port int, method Method, host_name, path string) ?Response {
2019-12-21 23:41:42 +01:00
// ssl_method := C.SSLv23_method()
2019-11-24 04:27:02 +01:00
ssl_method := C.TLSv1_2_method()
ctx := C.SSL_CTX_new(ssl_method)
C.SSL_CTX_set_verify_depth(ctx, 4)
flags := C.SSL_OP_NO_SSLv2 | C.SSL_OP_NO_SSLv3 | C.SSL_OP_NO_COMPRESSION
C.SSL_CTX_set_options(ctx, flags)
mut res := C.SSL_CTX_load_verify_locations(ctx, 'random-org-chain.pem', 0)
web := C.BIO_new_ssl_connect(ctx)
2019-08-21 19:04:06 +02:00
addr := host_name + ':' + port.str()
2019-11-24 04:27:02 +01:00
res = C.BIO_set_conn_hostname(web, addr.str)
ssl := &openssl.SSL(0)
2019-11-24 04:27:02 +01:00
C.BIO_get_ssl(web, &ssl)
preferred_ciphers := 'HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4'
res = C.SSL_set_cipher_list(ssl, preferred_ciphers.str)
if res != 1 {
println('http: openssl: cipher failed')
2019-11-24 04:27:02 +01:00
}
res = C.SSL_set_tlsext_host_name(ssl, host_name.str)
res = C.BIO_do_connect(web)
2019-12-09 11:31:24 +01:00
if res != 1 {
return error('cannot connect the endpoint')
}
2019-11-24 04:27:02 +01:00
res = C.BIO_do_handshake(web)
C.SSL_get_peer_certificate(ssl)
2019-11-24 04:27:02 +01:00
res = C.SSL_get_verify_result(ssl)
2019-12-21 23:41:42 +01:00
// /////
req_headers := req.build_request_headers(method, host_name, path)
2020-07-26 15:54:18 +02:00
//println(req_headers)
C.BIO_puts(web, req_headers.str)
mut content := strings.new_builder(100)
mut buff := [bufsize]byte
mut readcounter := 0
for {
readcounter++
len := C.BIO_read(web, buff, bufsize)
if len <= 0 {
2019-11-24 04:27:02 +01:00
break
}
$if debug_http ? {
eprintln('ssl_do, read ${readcounter:4d} | len: $len')
eprintln('-'.repeat(20))
eprintln(tos(buff, len))
eprintln('-'.repeat(20))
}
content.write_bytes(buff, len)
2019-11-24 04:27:02 +01:00
}
if web != 0 {
C.BIO_free_all(web)
2019-11-24 04:27:02 +01:00
}
if ctx != 0 {
2019-11-24 04:27:02 +01:00
C.SSL_CTX_free(ctx)
}
return parse_response(content.str())
}