From 23add65058876af3d9c7fb88dffec3f81dd4c87e Mon Sep 17 00:00:00 2001 From: Joakim Reinert Date: Sat, 6 Oct 2018 13:47:48 +0200 Subject: [PATCH] add support for 100-continue in HTTP::Server::Response --- spec/std/http/server/server_spec.cr | 9 +++++++++ src/http/server/response.cr | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/spec/std/http/server/server_spec.cr b/spec/std/http/server/server_spec.cr index e57b774a47e2..d851c0fc5d2e 100644 --- a/spec/std/http/server/server_spec.cr +++ b/spec/std/http/server/server_spec.cr @@ -216,6 +216,15 @@ module HTTP response.respond_with_error("Bad Request", 400) io.to_s.should eq("HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n10\r\n400 Bad Request\n\r\n") end + + it "supports 100-continue" do + io = MemoryIO.new + response = Response.new(io) + response.continue + response.content_type = "text/html" + response << "test" + io.to_s.should eq("HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200\r\n\r\ntest\r\n") + end end end diff --git a/src/http/server/response.cr b/src/http/server/response.cr index 615bec1dcaf5..f0b8b97890fc 100644 --- a/src/http/server/response.cr +++ b/src/http/server/response.cr @@ -79,6 +79,14 @@ class HTTP::Server raise "Can't read from HTTP::Server::Response" end + # Send a 100 Continue response to clients waiting for this before sending + # the request body + def continue + @status_code = 100 + ensure_headers_written + reset + end + # Upgrades this response, writing headers and yieling the connection `IO` (a socket) to the given block. # The block must invoke `close` afterwards, the server won't do it in this case. # This is useful to implement protocol upgrades, such as websockets.