47 lines
1.4 KiB
Julia
47 lines
1.4 KiB
Julia
import Base.get
|
|
function get(request::Request; certs::Union{CAChain,Nothing} = nothing,
|
|
sni = true)
|
|
req = string(request)
|
|
let len = sizeof(req)
|
|
if len > 1024
|
|
throw(OverflowError(
|
|
"Maximum <URL> size is 1024 bytes (yours is $len)!"))
|
|
end
|
|
end
|
|
data = string(req, "\r\n")
|
|
ctx = OpenSSL.SSLContext(mode = OpenSSL.ClientMode)
|
|
# Use certificates, if provided
|
|
isnothing(certs) || Gemenon.OpenSSL.ca_chain!(ctx, certs.cert, certs.key)
|
|
|
|
io = Sockets.connect(request.host, request.port)
|
|
|
|
client = SSLClient(ctx, io)
|
|
sni && @show OpenSSL.set_host_name(client, request.host)
|
|
|
|
OpenSSL.do_ssl_handshake(client)
|
|
|
|
while true
|
|
if isreadable(io) && length(client.write_buf) == 0
|
|
# verbose && println("do_read")
|
|
if OpenSSL.do_sock_read(client) == -1
|
|
break
|
|
end
|
|
end
|
|
if iswritable(io) && length(client.write_buf) > 0
|
|
# verbose && println("do_write")
|
|
if OpenSSL.do_sock_write(client) == -1
|
|
break
|
|
end
|
|
end
|
|
# verbose && println("end loop")
|
|
if OpenSSL.ssl_init_finished(client)
|
|
write(client, data)
|
|
while isopen(io)
|
|
# println("HERE")
|
|
OpenSSL.do_sock_read(client)
|
|
end
|
|
return String(client.context.data)
|
|
end
|
|
end
|
|
end
|