Skip to content

Commit

Permalink
Merge pull request #114 from samoconnor/patch-1
Browse files Browse the repository at this point in the history
Fix #113 - empty read after eof() false
  • Loading branch information
quinnj authored Dec 14, 2017
2 parents 43e1a45 + d98a691 commit 6b014f1
Showing 1 changed file with 6 additions and 14 deletions.
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

0 comments on commit 6b014f1

Please sign in to comment.