Permalink
Browse files

Use a singleton null object for empty request bodies

  • Loading branch information...
1 parent c87d543 commit bcdd5357fb8796aad1f0ac9d7a80b03f175e0411 @evanphx evanphx committed Dec 1, 2011
Showing with 25 additions and 5 deletions.
  1. +20 −0 lib/puma/null_io.rb
  2. +3 −3 lib/puma/server.rb
  3. +2 −2 test/test_persistent.rb
View
@@ -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
@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
View
@@ -5,6 +5,7 @@
require 'puma/thread_pool'
require 'puma/const'
require 'puma/events'
+require 'puma/null_io'
require 'puma_http11'
@@ -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
@@ -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
@@ -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

0 comments on commit bcdd535

Please sign in to comment.