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

Remove readuntil type piracy #1083

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
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
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
Loading