Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #113 - empty read after eof() false #114

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions src/ssl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,11 @@ function Base.readbytes!(ctx::SSLContext, buf::Vector{UInt8}, nbytes::UInt)
return Int(nr::UInt)
end

function Base.readavailable(ctx::SSLContext)
# For unknown reasons, nb_available on SSLContext erroneously returns 0
# until `read` is called on the context. As a temporary hack, we read one
# byte from the SSL context to cause nb_available to be accurate.
# TODO: figure out and fix the root cause of this
b = IOBuffer()
write(b, read(ctx, 1))
write(b, read(ctx, nb_available(ctx)))
return take!(b)
end
Base.readavailable(ctx::SSLContext) = read(ctx, nb_available(ctx))

function Base.eof(ctx::SSLContext)
# Not quite semantically correct, since nb_available might still be zero
# when this returns false (ie, the underlying socket has bytes available but not
# a complete record)
nb_available(ctx)>0 && return false
return eof(ctx.bio)
return eof(ctx.bio) && nb_available(ctx) == 0
end

function Base.close(ctx::SSLContext)
Expand Down Expand Up @@ -261,6 +249,10 @@ function get_ciphersuite(ctx::SSLContext)
end

function Base.nb_available(ctx::SSLContext)
# First try to read from the socket and decrypt incoming data if possible.
# https://esp32.com/viewtopic.php?t=1101#p4884
ccall((:mbedtls_ssl_read, MBED_TLS), Cint, (Ptr{Void}, Ptr{Void}, Csize_t),
ctx.data, C_NULL, 0)
n = ccall((:mbedtls_ssl_get_bytes_avail, MBED_TLS), Csize_t, (Ptr{Void},), ctx.data)
return Int(n)
end
Expand Down