Skip to content

Commit

Permalink
add Expect: 100-continue header handling in HTTP::Server::Response
Browse files Browse the repository at this point in the history
  • Loading branch information
Joakim Reinert committed Oct 9, 2018
1 parent 4fb09af commit e453c1a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
33 changes: 33 additions & 0 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,39 @@ module HTTP
HTTP::Client.get("http://#{address1}/").body.should eq "Test Server (#{address1})"
end

it "handles Expect: 100-continue correctly" do
server = Server.new do |context|
context.response << context.request.body.not_nil!.gets_to_end
end

address = server.bind_unused_port
spawn { server.listen }

Fiber.yield

TCPSocket.open(address.address, address.port) do |socket|
socket << requestize(<<-REQUEST
POST / HTTP/1.1
Expect: 100-continue
Content-Length: 5
REQUEST
)
socket << "\r\n"
socket.flush

response = Client::Response.from_io(socket)
response.status_code.should eq(100)

socket << "hello"
socket.flush

response = Client::Response.from_io(socket)
response.status_code.should eq(200)
response.body.should eq("hello")
end
end

it "lists addresses" do
server = Server.new { }

Expand Down
8 changes: 8 additions & 0 deletions src/http/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module HTTP
body = UnknownLengthContent.new(io)
end

if body.is_a?(Content) && expect_continue?(headers)
body.expect_continue
end

if decompress && body
{% if flag?(:without_zlib) %}
raise "Can't decompress because `-D without_zlib` was passed at compile time"
Expand Down Expand Up @@ -194,6 +198,10 @@ module HTTP
end
end

def self.expect_continue?(headers)
headers["Expect"]?.try(&.downcase) == "100-continue"
end

record ComputedContentTypeHeader,
content_type : String?,
charset : String?
Expand Down
43 changes: 43 additions & 0 deletions src/http/content.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,52 @@ require "http/common"
module HTTP
# :nodoc:
module Content
CONTINUE = "HTTP/1.1 100 Continue\r\n\r\n"

@continue_sent = false
@expects_continue = false

def close
skip_to_end
super
end

def expect_continue
@expects_continue = true
end

protected def ensure_send_continue
return unless @expects_continue && !@continue_sent
@io << CONTINUE
@io.flush
@continue_sent = true
end
end

# :nodoc:
class FixedLengthContent < IO::Sized
include Content

def read(slice : Bytes)
ensure_send_continue
super
end

def read_byte
ensure_send_continue
super
end

def peek
ensure_send_continue
super
end

def skip(bytes_count)
ensure_send_continue
super
end

def write(slice : Bytes)
raise IO::Error.new "Can't write to FixedLengthContent"
end
Expand All @@ -26,14 +62,17 @@ module HTTP
end

def read(slice : Bytes)
ensure_send_continue
@io.read(slice)
end

def read_byte
ensure_send_continue
@io.read_byte
end

def peek
ensure_send_continue
@io.peek
end

Expand Down Expand Up @@ -66,6 +105,7 @@ module HTTP
end

def read(slice : Bytes)
ensure_send_continue
count = slice.size
return 0 if count == 0

Expand All @@ -87,6 +127,7 @@ module HTTP
end

def read_byte
ensure_send_continue
next_chunk
return super if @received_final_chunk

Expand All @@ -100,6 +141,7 @@ module HTTP
end

def peek
ensure_send_continue
next_chunk
return Bytes.empty if @received_final_chunk

Expand All @@ -115,6 +157,7 @@ module HTTP
end

def skip(bytes_count)
ensure_send_continue
if bytes_count <= @chunk_remaining
@io.skip(bytes_count)
@chunk_remaining -= bytes_count
Expand Down

0 comments on commit e453c1a

Please sign in to comment.