Skip to content

Commit

Permalink
fix incomplete check in closeread(http::Stream{Response})
Browse files Browse the repository at this point in the history
Co-Authored-By: Fredrik Ekre <[email protected]>
  • Loading branch information
IanButterworth and fredrikekre committed Nov 17, 2021
1 parent bd588af commit f43616f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/StreamRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ function request(::Type{StreamLayer{Next}}, io::IO, req::Request, body;
closewrite(http)
@debug 2 "client closeread"
closeread(http)
catch;
catch e
e isa EOFError && rethrow()
end

verbose == 1 && printlncompact(response)
Expand Down
7 changes: 3 additions & 4 deletions src/Streams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ function Base.eof(http::Stream)
if http.ntoread == 0
return true
end
if eof(http.stream)
return true
end
return false
return eof(http.stream)
end

@inline function ntoread(http::Stream)
Expand Down Expand Up @@ -355,6 +352,8 @@ function IOExtras.closeread(http::Stream{Response})
# Close conncetion if server sent "Connection: close"...
@debug 1 "\"Connection: close\": $(http.stream)"
close(http.stream)
# Error if Message is not complete...
incomplete(http) && throw(EOFError())
else

# Discard body bytes that were not read...
Expand Down
24 changes: 24 additions & 0 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ end
@test status(HTTP.post("$sch://httpbin.org/post"; body=f, #=chunksize=2=#)) == 200
end

@testset "Incomplete response with known content length" begin
server = Sockets.listen(ip"0.0.0.0", 8080)
task = @async HTTP.listen("0.0.0.0", 8080; server=server) do http
HTTP.setstatus(http, 200)
HTTP.setheader(http, "Content-Length" => "64") # Promise 64 bytes...
HTTP.startwrite(http)
HTTP.write(http, rand(UInt8, 63)) # ...but only send 63 bytes.
# Close the stream so that eof(stream) is true and the client isn't
# waiting forever for the last byte.
HTTP.close(http.stream)
end

err = try
HTTP.get("http://localhost:8080"; retry=false)
catch err
err
end
@test err isa HTTP.IOError
@test err.e isa EOFError

# Shutdown
try; close(server); wait(task); catch; end
end

@testset "ASync Client Request Body" begin
f = Base.BufferStream()
write(f, "hey")
Expand Down

0 comments on commit f43616f

Please sign in to comment.