Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Merge 6952a82 into 9c6fe16
Browse files Browse the repository at this point in the history
  • Loading branch information
Masahiro Fujiwara committed Nov 7, 2013
2 parents 9c6fe16 + 6952a82 commit 35dcf8e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
12 changes: 9 additions & 3 deletions lib/reel/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def respond(response, headers_or_body = {}, body = nil)
# Enable streaming mode
if response.chunked? and response.body.nil?
@response_state = :chunked_body
end

if @keepalive
elsif @keepalive
reset_request
else
@current_request = nil
Expand Down Expand Up @@ -160,5 +158,13 @@ def reset_request
@parser.reset
end
private :reset_request

# Set response state for the connection.
def response_state=(state)
if state == :header
reset_request
end
@response_state = state
end
end
end
55 changes: 47 additions & 8 deletions spec/reel/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,22 @@
end
end

it "streams responses when transfer-encoding is chunked" do
with_socket_pair do |client, peer|
connection = Reel::Connection.new(peer)
client << ExampleRequest.new.to_s
request = connection.request

context "streams responses when transfer-encoding is chunked" do
def test_chunked_response(request, client)
# Sending transfer_encoding chunked without a body enables streaming mode
request.respond :ok, :transfer_encoding => :chunked

# This will send individual chunks
request << "Hello"
request << "World"
request.finish_response # Write trailer and reset connection to header mode
connection.close

response = ""

begin
while chunk = client.readpartial(4096)
response << chunk
break if response =~ /0\r\n\r\n$/
end
rescue EOFError
end
Expand All @@ -101,8 +97,51 @@
fixture = "5#{crlf}Hello#{crlf}5#{crlf}World#{crlf}0#{crlf*2}"
response[(response.length - fixture.length)..-1].should eq fixture
end
end

it "with keep-alive" do
with_socket_pair do |client, peer|
connection = Reel::Connection.new(peer)
client << ExampleRequest.new.to_s
request = connection.request

test_chunked_response(request, client)
connection.close
end
end

it "without keep-alive" do
with_socket_pair do |client, peer|
connection = Reel::Connection.new(peer)
client << ExampleRequest.new.tap{ |r|
r['Connection'] = 'close'
}.to_s
request = connection.request

test_chunked_response(request, client)
connection.close
end
end

it "with pipelined requests" do
with_socket_pair do |client, peer|
connection = Reel::Connection.new(peer)

2.times do
client << ExampleRequest.new.to_s
end
client << ExampleRequest.new.tap { |r|
r['Connection'] = 'close'
}.to_s

3.times do
request = connection.request
test_chunked_response(request, client)
end
connection.close
end
end
end

it "reset the request after a response is sent" do
with_socket_pair do |client, peer|
connection = Reel::Connection.new(peer)
Expand Down

0 comments on commit 35dcf8e

Please sign in to comment.