Skip to content

Commit bbe1746

Browse files
authored
Remove readuntil type piracy (#1083)
* Remove readuntil type piracy * fix test
1 parent f82ab85 commit bbe1746

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

src/Connections.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ end
233233
Read until `find_delimiter(bytes)` returns non-zero.
234234
Return view of bytes up to the delimiter.
235235
"""
236-
function Base.readuntil(c::Connection, f::F #=Vector{UInt8} -> Int=#,
236+
function IOExtras.readuntil(c::Connection, f::F #=Vector{UInt8} -> Int=#,
237237
sizehint=4096)::ByteView where {F <: Function}
238238
buf = c.buffer
239239
if bytesavailable(buf) == 0
240240
read_to_buffer(c, sizehint)
241241
end
242-
while (bytes = readuntil(buf, f)) == nobytes
242+
while (bytes = IOExtras.readuntil(buf, f)) == nobytes
243243
read_to_buffer(c, sizehint)
244244
end
245245
return bytes

src/IOExtras.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using MbedTLS: SSLContext, MbedException
1212
using OpenSSL: SSLStream
1313

1414
export bytes, isbytes, nbytes, ByteView, nobytes,
15-
startwrite, closewrite, startread, closeread,
15+
startwrite, closewrite, startread, closeread, readuntil,
1616
tcpsocket, localport, safe_getpeername
1717

1818
"""
@@ -107,11 +107,13 @@ end
107107
const ByteView = typeof(view(UInt8[], 1:0))
108108
const nobytes = view(UInt8[], 1:0)
109109

110+
readuntil(args...) = Base.readuntil(args...)
111+
110112
"""
111113
Read from an `IO` stream until `find_delimiter(bytes)` returns non-zero.
112114
Return view of bytes up to the delimiter.
113115
"""
114-
function Base.readuntil(buf::IOBuffer,
116+
function readuntil(buf::IOBuffer,
115117
find_delimiter::F #= Vector{UInt8} -> Int =#
116118
)::ByteView where {F <: Function}
117119
l = find_delimiter(view(buf.data, buf.ptr:buf.size))

src/Messages.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ Read headers (and startline) from an `IO` stream into a `Message` struct.
530530
Throw `EOFError` if input is incomplete.
531531
"""
532532
function readheaders(io::IO, message::Message)
533-
bytes = String(readuntil(io, find_end_of_header))
533+
bytes = String(IOExtras.readuntil(io, find_end_of_header))
534534
bytes = parse_start_line!(bytes, message)
535535
parse_header_fields!(bytes, message)
536536
return
@@ -555,9 +555,9 @@ Read chunk-size from an `IO` stream.
555555
After the final zero size chunk, read trailers into a `Message` struct.
556556
"""
557557
function readchunksize(io::IO, message::Message)::Int
558-
n = parse_chunk_size(readuntil(io, find_end_of_chunk_size))
558+
n = parse_chunk_size(IOExtras.readuntil(io, find_end_of_chunk_size))
559559
if n == 0
560-
bytes = readuntil(io, find_end_of_trailer)
560+
bytes = IOExtras.readuntil(io, find_end_of_trailer)
561561
if bytes[2] != UInt8('\n')
562562
parse_header_fields!(SubString(String(bytes)), message)
563563
end

src/Streams.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ function readall!(http::Stream, buf::Base.GenericIOBuffer=PipeBuffer())
320320
return n
321321
end
322322

323-
function Base.readuntil(http::Stream, f::Function)::ByteView
323+
function IOExtras.readuntil(http::Stream, f::Function)::ByteView
324324
UInt(ntoread(http)) == 0 && return Connections.nobytes
325325
try
326-
bytes = readuntil(http.stream, f)
326+
bytes = IOExtras.readuntil(http.stream, f)
327327
update_ntoread(http, length(bytes))
328328
return bytes
329329
catch e

test/client.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module TestClient
22

33
using HTTP, HTTP.Exceptions, MbedTLS, OpenSSL
4+
using HTTP: IOExtras
45
include(joinpath(dirname(pathof(HTTP)), "../test/resources/TestRequest.jl"))
56
import ..isok, ..httpbin
67
using .TestRequest
@@ -658,10 +659,10 @@ end
658659

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

661-
@testset "readuntil on Stream" begin
662+
@testset "IOExtras.readuntil on Stream" begin
662663
HTTP.open(:GET, "https://$httpbin/stream/5") do io
663664
while !eof(io)
664-
bytes = readuntil(io, findnewline)
665+
bytes = IOExtras.readuntil(io, findnewline)
665666
isempty(bytes) && break
666667
x = JSON.parse(IOBuffer(bytes))
667668
end

0 commit comments

Comments
 (0)