From 7b6722fd73282888f46d68ca9e1d3a99814c35c3 Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Wed, 8 Sep 2021 12:12:08 +1000 Subject: [PATCH] Do not call `close()` on the `response_stream` This allows a plain `IOBuffer` to be used with `response_stream`. It seems unnecessary to be calling `close(response_stream)` inside HTTP.request() - given that HTTP.request is a blocking operation, the caller can easily close the stream themselves after `HTTP.request()` returns. This could be considered a breaking change for users who use the internal IO type Base.BufferStream with the response_stream keyword. Fixes #543 --- src/StreamRequest.jl | 1 - test/async.jl | 1 + test/client.jl | 3 ++- test/loopback.jl | 2 +- test/messages.jl | 9 +++++---- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 5842b260c..a97de4390 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -146,7 +146,6 @@ function readbody(http::Stream, res::Response, response_stream, reached_redirect if reached_redirect_limit || !isredirect(res) res.body = body_was_streamed write(response_stream, http) - close(response_stream) end end end diff --git a/test/async.jl b/test/async.jl index 122e7e14f..126799f1f 100644 --- a/test/async.jl +++ b/test/async.jl @@ -114,6 +114,7 @@ using Test, HTTP, JSON try stream = Base.BufferStream() response = HTTP.request("GET", url; response_stream=stream, config...) + close(stream) if response.status != 200 @error "non-200 response" response=response diff --git a/test/client.jl b/test/client.jl index 0d9d25951..6c92e277d 100644 --- a/test/client.jl +++ b/test/client.jl @@ -69,8 +69,9 @@ end a = [JSON.parse(l) for l in split(chomp(String(bytes)), "\n")] totallen = length(bytes) # number of bytes to expect - io = Base.BufferStream() + io = IOBuffer() r = HTTP.get("$sch://httpbin.org/stream/100"; response_stream=io) + seekstart(io) @test status(r) == 200 b = [JSON.parse(l) for l in eachline(io)] diff --git a/test/loopback.jl b/test/loopback.jl index 07323acc7..87961ad20 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -448,4 +448,4 @@ end HTTP.ConnectionPool.closeall() end # @static -end \ No newline at end of file +end diff --git a/test/messages.jl b/test/messages.jl index 07ec8e33c..e6d0cfa0e 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -117,10 +117,11 @@ using JSON @test r.status == 200 r1 = JSON.parse(String(r.body)) - io = Base.BufferStream() + io = IOBuffer() r = request(method, uri, response_stream=io, verbose=1) + seekstart(io) @test r.status == 200 - r2 = JSON.parse(IOBuffer(read(io))) + r2 = JSON.parse(io) for (k, v) in r1 if k == "headers" for (k2, v2) in r1[k] @@ -136,7 +137,7 @@ using JSON @testset "Body - JSON Parse" for protocol in protocols, method in http_writes uri = "$protocol://httpbin.org/$(lowercase(method))" - io = Base.BufferStream() + io = IOBuffer() r = request(method, uri, response_stream=io, verbose=1) @test r.status == 200 @@ -186,4 +187,4 @@ using JSON # don't display raw binary (non-Unicode) data: @test repr(Response(200, []; body=String([0xde,0xad,0xc1,0x71,0x1c]))) == "Response:\n\"\"\"\nHTTP/1.1 200 OK\r\n\r\n\n⋮\n5-byte body\n\"\"\"" end -end \ No newline at end of file +end