Skip to content

Commit

Permalink
Remove readuntil type piracy (#1083)
Browse files Browse the repository at this point in the history
* Remove readuntil type piracy

* fix test
  • Loading branch information
mortenpi committed Aug 4, 2023
1 parent f82ab85 commit bbe1746
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ end
Read until `find_delimiter(bytes)` returns non-zero.
Return view of bytes up to the delimiter.
"""
function Base.readuntil(c::Connection, f::F #=Vector{UInt8} -> Int=#,
function IOExtras.readuntil(c::Connection, f::F #=Vector{UInt8} -> Int=#,
sizehint=4096)::ByteView where {F <: Function}
buf = c.buffer
if bytesavailable(buf) == 0
read_to_buffer(c, sizehint)
end
while (bytes = readuntil(buf, f)) == nobytes
while (bytes = IOExtras.readuntil(buf, f)) == nobytes
read_to_buffer(c, sizehint)
end
return bytes
Expand Down
6 changes: 4 additions & 2 deletions src/IOExtras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using MbedTLS: SSLContext, MbedException
using OpenSSL: SSLStream

export bytes, isbytes, nbytes, ByteView, nobytes,
startwrite, closewrite, startread, closeread,
startwrite, closewrite, startread, closeread, readuntil,
tcpsocket, localport, safe_getpeername

"""
Expand Down Expand Up @@ -107,11 +107,13 @@ end
const ByteView = typeof(view(UInt8[], 1:0))
const nobytes = view(UInt8[], 1:0)

readuntil(args...) = Base.readuntil(args...)

"""
Read from an `IO` stream until `find_delimiter(bytes)` returns non-zero.
Return view of bytes up to the delimiter.
"""
function Base.readuntil(buf::IOBuffer,
function readuntil(buf::IOBuffer,
find_delimiter::F #= Vector{UInt8} -> Int =#
)::ByteView where {F <: Function}
l = find_delimiter(view(buf.data, buf.ptr:buf.size))
Expand Down
6 changes: 3 additions & 3 deletions src/Messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ Read headers (and startline) from an `IO` stream into a `Message` struct.
Throw `EOFError` if input is incomplete.
"""
function readheaders(io::IO, message::Message)
bytes = String(readuntil(io, find_end_of_header))
bytes = String(IOExtras.readuntil(io, find_end_of_header))
bytes = parse_start_line!(bytes, message)
parse_header_fields!(bytes, message)
return
Expand All @@ -555,9 +555,9 @@ Read chunk-size from an `IO` stream.
After the final zero size chunk, read trailers into a `Message` struct.
"""
function readchunksize(io::IO, message::Message)::Int
n = parse_chunk_size(readuntil(io, find_end_of_chunk_size))
n = parse_chunk_size(IOExtras.readuntil(io, find_end_of_chunk_size))
if n == 0
bytes = readuntil(io, find_end_of_trailer)
bytes = IOExtras.readuntil(io, find_end_of_trailer)
if bytes[2] != UInt8('\n')
parse_header_fields!(SubString(String(bytes)), message)
end
Expand Down
4 changes: 2 additions & 2 deletions src/Streams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ function readall!(http::Stream, buf::Base.GenericIOBuffer=PipeBuffer())
return n
end

function Base.readuntil(http::Stream, f::Function)::ByteView
function IOExtras.readuntil(http::Stream, f::Function)::ByteView
UInt(ntoread(http)) == 0 && return Connections.nobytes
try
bytes = readuntil(http.stream, f)
bytes = IOExtras.readuntil(http.stream, f)
update_ntoread(http, length(bytes))
return bytes
catch e
Expand Down
5 changes: 3 additions & 2 deletions test/client.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TestClient

using HTTP, HTTP.Exceptions, MbedTLS, OpenSSL
using HTTP: IOExtras
include(joinpath(dirname(pathof(HTTP)), "../test/resources/TestRequest.jl"))
import ..isok, ..httpbin
using .TestRequest
Expand Down Expand Up @@ -658,10 +659,10 @@ end

findnewline(bytes) = something(findfirst(==(UInt8('\n')), bytes), 0)

@testset "readuntil on Stream" begin
@testset "IOExtras.readuntil on Stream" begin
HTTP.open(:GET, "https://$httpbin/stream/5") do io
while !eof(io)
bytes = readuntil(io, findnewline)
bytes = IOExtras.readuntil(io, findnewline)
isempty(bytes) && break
x = JSON.parse(IOBuffer(bytes))
end
Expand Down

0 comments on commit bbe1746

Please sign in to comment.