diff --git a/src/Connections.jl b/src/Connections.jl index 60d6eb9b..dd716340 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -234,12 +234,12 @@ Read until `find_delimiter(bytes)` returns non-zero. Return view of bytes up to the delimiter. """ function IOExtras.readuntil(c::Connection, f::F #=Vector{UInt8} -> Int=#, - sizehint=4096)::ByteView where {F <: Function} + sizehint=4096) where {F <: Function} buf = c.buffer if bytesavailable(buf) == 0 read_to_buffer(c, sizehint) end - while (bytes = IOExtras.readuntil(buf, f)) == nobytes + while isempty(begin bytes = IOExtras.readuntil(buf, f) end) read_to_buffer(c, sizehint) end return bytes diff --git a/src/IOExtras.jl b/src/IOExtras.jl index f45127d2..545ef70f 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -11,7 +11,7 @@ using Sockets using MbedTLS: SSLContext, MbedException using OpenSSL: SSLStream -export bytes, isbytes, nbytes, ByteView, nobytes, +export bytes, isbytes, nbytes, nobytes, startwrite, closewrite, startread, closeread, readuntil, tcpsocket, localport, safe_getpeername @@ -104,7 +104,6 @@ function safe_getpeername(io) end -const ByteView = typeof(view(UInt8[], 1:0)) const nobytes = view(UInt8[], 1:0) readuntil(args...) = Base.readuntil(args...) @@ -114,8 +113,8 @@ Read from an `IO` stream until `find_delimiter(bytes)` returns non-zero. Return view of bytes up to the delimiter. """ function readuntil(buf::IOBuffer, - find_delimiter::F #= Vector{UInt8} -> Int =# - )::ByteView where {F <: Function} + find_delimiter::F #= Vector{UInt8} -> Int =# + ) where {F <: Function} l = find_delimiter(view(buf.data, buf.ptr:buf.size)) if l == 0 return nobytes diff --git a/src/Streams.jl b/src/Streams.jl index 220d11c4..d58e14c8 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -320,14 +320,15 @@ function readall!(http::Stream, buf::Base.GenericIOBuffer=PipeBuffer()) return n end -function IOExtras.readuntil(http::Stream, f::Function)::ByteView +function IOExtras.readuntil(http::Stream, f::Function) UInt(ntoread(http)) == 0 && return Connections.nobytes try bytes = IOExtras.readuntil(http.stream, f) update_ntoread(http, length(bytes)) return bytes - catch + catch ex # if we error, it means we didn't find what we were looking for + # TODO: this seems very sketchy return UInt8[] end end