1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Use a singleton null object for empty request bodies

This commit is contained in:
Evan Phoenix 2011-12-01 14:33:34 -08:00
parent c87d543b68
commit bcdd5357fb
3 changed files with 25 additions and 5 deletions

20
lib/puma/null_io.rb Normal file
View file

@ -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
end
end

View file

@ -5,6 +5,7 @@ require 'stringio'
require 'puma/thread_pool'
require 'puma/const'
require 'puma/events'
require 'puma/null_io'
require 'puma_http11'
@ -249,8 +250,7 @@ module Puma
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
@ -259,7 +259,7 @@ module Puma
body = read_body env, client, body, cl
return false unless body
else
body = StringIO.new(EmptyBinary)
body = EmptyBody
end
env[RACK_INPUT] = body

View file

@ -197,8 +197,8 @@ class TestPersistent < Test::Unit::TestCase
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