Skip to content

Commit

Permalink
Use a singleton null object for empty request bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Dec 1, 2011
1 parent c87d543 commit bcdd535
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
20 changes: 20 additions & 0 deletions lib/puma/null_io.rb
@@ -0,0 +1,20 @@
module Puma
class NullIO
def gets
nil
end

def each
end

def read(count)
nil
end

def rewind
end

def close
end

This comment has been minimized.

Copy link
@rkh

rkh May 13, 2012

Contributor

This method is not needed. Calling close on the input string is explicitly forbidden by the Rack spec.

end
end
6 changes: 3 additions & 3 deletions lib/puma/server.rb
Expand Up @@ -5,6 +5,7 @@
require 'puma/thread_pool'
require 'puma/const'
require 'puma/events'
require 'puma/null_io'

require 'puma_http11'

Expand Down Expand Up @@ -249,8 +250,7 @@ def normalize_env(env, client)
env[REMOTE_ADDR] = client.peeraddr.last
end

EmptyBinary = ""
EmptyBinary.force_encoding("BINARY") if EmptyBinary.respond_to? :force_encoding
EmptyBody = NullIO.new

def handle_request(env, client, body, cl)
normalize_env env, client
Expand All @@ -259,7 +259,7 @@ def handle_request(env, client, body, cl)
body = read_body env, client, body, cl
return false unless body
else
body = StringIO.new(EmptyBinary)
body = EmptyBody
end

env[RACK_INPUT] = body
Expand Down
4 changes: 2 additions & 2 deletions test/test_persistent.rb
Expand Up @@ -197,8 +197,8 @@ def test_second_request_not_in_first_req_body
assert_equal "HTTP/1.1 200 OK\r\nX-Header: Works\r\nContent-Length: #{sz}\r\n\r\n", lines(4)
assert_equal "Hello", @client.read(5)

assert_equal "", @inputs[0].string
assert_equal "", @inputs[1].string
assert_kind_of Puma::NullIO, @inputs[0]
assert_kind_of Puma::NullIO, @inputs[1]

end

Expand Down

0 comments on commit bcdd535

Please sign in to comment.