From 4099389b9c0e2752bcbe4f79a42dbec29c53a396 Mon Sep 17 00:00:00 2001 From: Panagiotis Georgakopoulos Date: Wed, 20 Sep 2023 13:22:58 +0300 Subject: [PATCH 1/2] fix: prevent HEAD requests from writing body --- src/Handlers.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Handlers.jl b/src/Handlers.jl index 88167960a..a121015c7 100644 --- a/src/Handlers.jl +++ b/src/Handlers.jl @@ -58,7 +58,9 @@ function streamhandler(handler) request.response::Response = handler(request) request.response.request = request startwrite(stream) - write(stream, request.response.body) + if request.method != "HEAD" + write(stream, request.response.body) + end return end end From 64d36e25a16abdb2c47ef2f22d9e42ade4dd1ffe Mon Sep 17 00:00:00 2001 From: Panagiotis Georgakopoulos Date: Wed, 6 Dec 2023 13:19:57 +0200 Subject: [PATCH 2/2] test: test that HEAD requests don't have return a body --- test/server.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/server.jl b/test/server.jl index af1a98741..0ebd8096b 100644 --- a/test/server.jl +++ b/test/server.jl @@ -313,4 +313,23 @@ end # @testset @test occursin(r"^\*/\* text/plain HEAD / HTTP/1\.1 HEAD / 127\.0\.0\.1 \d+ - HTTP/1\.1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.* \d+/.*/\d{4}:\d{2}:\d{2}:\d{2}.* 200 0$", logs[4].message) end +@testset "HEAD request without body" begin + sometext = "This is a big body that we don't want returned during a head" + handler = req -> begin + return HTTP.Response(200, [], sometext) + end + server = HTTP.serve!(handler; listenany=true) + port = HTTP.port(server) + + response = HTTP.head("http://localhost:$port") + @test response.status == 200 + @test String(response.body) == "" + + response = HTTP.get("http://localhost:$port") + @test response.status == 200 + @test String(response.body) == sometext + + close(server) +end + end # module