Skip to content

Commit

Permalink
Few cleanups for IO interface
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Aug 23, 2024
1 parent fdfe12b commit d755521
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 55 deletions.
51 changes: 0 additions & 51 deletions src/AwsIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,6 @@ sockerr(msg::String) = CapturedException(SocketError(msg), Base.backtrace())
sockerr(code::Integer) = sockerr(unsafe_string(aws_error_str(code)))
sockerr(e::Exception) = CapturedException(e, Base.backtrace())

# NOTE: caller is responsible for cleaning up tls_options
function tlsoptions(host::String;
allocator=default_aws_allocator(),
# tls options
ssl_cert=nothing,
ssl_key=nothing,
ssl_capath=nothing,
ssl_cacert=nothing,
ssl_insecure=false,
ssl_alpn_list=nothing,
on_negotiation_result=C_NULL,
on_negotiation_result_user_data=C_NULL
)
tls_options = aws_tls_connection_options(C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL, false, UInt32(0))
tls_ctx_options = Ptr{aws_tls_ctx_options}(aws_mem_acquire(allocator, sizeof(aws_tls_ctx_options)))
tls_ctx = C_NULL
try
if ssl_cert !== nothing && ssl_key !== nothing
aws_tls_ctx_options_init_client_mtls_from_path(tls_ctx_options, allocator, ssl_cert, ssl_key) != 0 && sockerr("aws_tls_ctx_options_init_client_mtls_from_path failed")
elseif Sys.iswindows() && ssl_cert !== nothing && ssl_key === nothing
aws_tls_ctx_options_init_client_mtls_from_system_path(tls_ctx_options, allocator, ssl_cert) != 0 && sockerr("aws_tls_ctx_options_init_client_mtls_from_system_path failed")
else
aws_tls_ctx_options_init_default_client(tls_ctx_options, allocator)
end
if ssl_capath !== nothing && ssl_cacert !== nothing
aws_tls_ctx_options_override_default_trust_store_from_path(tls_ctx_options, ssl_capath, ssl_cacert) != 0 && sockerr("aws_tls_ctx_options_override_default_trust_store_from_path failed")
end
if ssl_insecure
aws_tls_ctx_options_set_verify_peer(tls_ctx_options, false)
end
if ssl_alpn_list !== nothing
aws_tls_ctx_options_set_alpn_list(tls_ctx_options, ssl_alpn_list) != 0 && sockerr("aws_tls_ctx_options_set_alpn_list failed")
end
tls_ctx = aws_tls_client_ctx_new(allocator, tls_ctx_options)
tls_ctx == C_NULL && sockerr("")
ref = Ref(tls_options)
host_ref = Ref(aws_byte_cursor(sizeof(host), pointer(host)))
aws_tls_connection_options_init_from_ctx(ref, tls_ctx)
aws_tls_connection_options_set_server_name(ref, allocator, host_ref) != 0 && sockerr("aws_tls_connection_options_set_server_name failed")
if on_negotiation_result !== C_NULL
aws_tls_connection_options_set_callbacks(ref, on_negotiation_result, C_NULL, C_NULL, pointer_from_objref(on_negotiation_result_user_data))
end
tls_options = ref[]
finally
aws_tls_ctx_options_clean_up(tls_ctx_options)
aws_tls_ctx_release(tls_ctx)
aws_mem_release(allocator, tls_ctx_options)
end
return tls_options
end

include("sockets/client.jl")

function __init__()
Expand Down
9 changes: 5 additions & 4 deletions src/sockets/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ mutable struct Client
)
end
if tls && tls_options === nothing
tls_options = tlsoptions(host;
tls_options = LibAwsIO.tlsoptions(host;
ssl_cert,
ssl_key,
ssl_capath,
Expand Down Expand Up @@ -294,8 +294,9 @@ Base.read(sock::Client, n::Integer) = read(sock.readbuf, n)
Base.unsafe_read(sock::Client, ptr::Ptr{UInt8}, n::Integer) = unsafe_read(sock.readbuf, ptr, n)

Base.skip(sock::Client, n) = skip(sock.readbuf, n)

Base.isopen(sock::Client) = isopen(sock.ch)
Base.bytesavailable(sock::Client) = bytesavailable(sock.readbuf)
Base.isopen(sock::Client) = sock.slot == C_NULL ? false : aws_socket_is_open(aws_socket_handler_get_socket(FieldRef(sock, :handler)))
Base.readbytes!(sock::Client, buf::Vector{UInt8}, nb=length(buf)) = readbytes!(sock.readbuf, buf, nb)

function Base.close(sock::Client)
close(sock.ch)
Expand Down Expand Up @@ -374,7 +375,7 @@ function tlsupgrade!(sock::Client;
ssl_insecure::Bool=false,
ssl_alpn_list::Union{String, Nothing}=nothing
)
tls_options = tlsoptions(
tls_options = LibAwsIO.tlsoptions(
sock.host;
ssl_cert=ssl_cert,
ssl_key=ssl_key,
Expand Down

0 comments on commit d755521

Please sign in to comment.