Skip to content

Commit f732227

Browse files
committed
Add Base.readuntil(::HTTP.Stream, ::UInt8)
Fix IOExtras.unread!(::HTTP.Stream) Now Base.readline(::HTTP.Stream) works
1 parent cdeb97e commit f732227

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/ConnectionPool.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,15 @@ Push bytes back into a connection's `excess` buffer
184184
function IOExtras.unread!(t::Transaction, bytes::ByteView)
185185
@require isreadable(t)
186186
@require !isempty(bytes)
187-
@require t.c.excess === nobytes
188-
t.c.excess = bytes
187+
@require t.c.excess === nobytes || bytes.parent == t.c.excess.parent &&
188+
bytes.indexes[1].stop + 1 ==
189+
t.c.excess.indexes[1].start
190+
if t.c.excess === nobytes
191+
t.c.excess = bytes
192+
else
193+
t.c.excess = view(bytes.parent,
194+
bytes.indexes[1].start:t.c.excess.indexes[1].stop)
195+
end
189196
return
190197
end
191198

src/Streams.jl

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,44 @@ function Base.readavailable(http::Stream)::ByteView
223223
return bytes
224224
end
225225

226-
IOExtras.unread!(http::Stream, excess) = unread!(http.stream, excess)
226+
function IOExtras.unread!(http::Stream, excess)
227+
228+
if http.readchunked && http.ntoread == unknown_length
229+
# If the whole chunk was read, unread! needs to push
230+
# back the CRLF that came after the chunk data
231+
# (See readavailable above).
232+
excess = view(excess.parent, excess.indexes[1].start:
233+
excess.indexes[1].stop + 2)
234+
http.ntoread = length(excess)
235+
236+
elseif http.ntoread != unknown_length
237+
http.ntoread += length(excess)
238+
end
239+
240+
unread!(http.stream, excess)
241+
end
242+
243+
@static if VERSION < v"0.7.0-DEV.2005"
244+
245+
find_delim(bytes, d::UInt8) = findfirst(x->x==d, bytes)
246+
247+
Base.readuntil(http::Stream, delim::UInt8) =
248+
Vector{UInt8}(readuntil(http, bytes->find_delim(bytes, delim)))
249+
# See readuntil(::IO, ::Function) in IOExtras.jl
250+
251+
else
252+
253+
find_delim(bytes, d::UInt8) =
254+
(i = findfirst(equalto(d), bytes)) == nothing ? 0 : i
255+
256+
function Base.readuntil(http::Stream, delim::UInt8; keep::Bool=false)
257+
bytes = readuntil(http, bytes->find_delim(bytes, delim))
258+
if keep == false
259+
bytes = view(bytes, 1:length(bytes)-1)
260+
end
261+
return Vector{UInt8}(bytes)
262+
end
263+
end
227264

228265
function Base.read(http::Stream)
229266
buf = IOBuffer()

0 commit comments

Comments
 (0)