summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--NEWS4
-rw-r--r--lib/webrick/httpresponse.rb10
-rw-r--r--test/webrick/test_httpresponse.rb89
4 files changed, 107 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 2cac5a8001..b4c9c8ae39 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Thu Aug 8 03:37:38 2013 Eric Hodel <drbrain@segment7.net>
+
+ * lib/webrick/httpresponse.rb: Allow #body to be an IO-like object
+ that responds to #readpartial and #read.
+ [ruby-trunk - Feature #8155]
+ * NEWS: NEWS for above
+ * test/webrick/test_httpresponse.rb: Tests for above.
+
Wed Aug 7 23:06:26 2013 Akinori MUSHA <knu@iDaemons.org>
* ruby.c (Process.argv0): New method to return the original value
diff --git a/NEWS b/NEWS
index 822b2f8096..61b646d194 100644
--- a/NEWS
+++ b/NEWS
@@ -145,6 +145,10 @@ with all sufficient information, see the ChangeLog file.
* CGI::Util
* All class methods modulized.
+* WEBrick
+ * The body of a response may now be a StringIO or other IO-like that responds
+ to #readpartial and #read.
+
* XMLRPC::Client
* New methods:
* XMLRPC::Client#http. It returns Net::HTTP for the client. Normally,
diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb
index 8e3eb39a31..044b8dfcaf 100644
--- a/lib/webrick/httpresponse.rb
+++ b/lib/webrick/httpresponse.rb
@@ -47,7 +47,8 @@ module WEBrick
attr_accessor :reason_phrase
##
- # Body may be a String or IO subclass.
+ # Body may be a String or IO-like object that responds to #read and
+ # #readpartial.
attr_accessor :body
@@ -299,9 +300,10 @@ module WEBrick
# Sends the body on +socket+
def send_body(socket) # :nodoc:
- case @body
- when IO then send_body_io(socket)
- else send_body_string(socket)
+ if @body.respond_to? :readpartial then
+ send_body_io(socket)
+ else
+ send_body_string(socket)
end
end
diff --git a/test/webrick/test_httpresponse.rb b/test/webrick/test_httpresponse.rb
index d5d5552796..aae4973b4d 100644
--- a/test/webrick/test_httpresponse.rb
+++ b/test/webrick/test_httpresponse.rb
@@ -1,5 +1,6 @@
require "webrick"
require "minitest/autorun"
+require "stringio"
module WEBrick
class TestHTTPResponse < MiniTest::Unit::TestCase
@@ -45,5 +46,93 @@ module WEBrick
assert_equal 0, logger.messages.length
end
+
+ def test_send_body_io
+ body_r, body_w = IO.pipe
+
+ body_w.write 'hello'
+ body_w.close
+
+ @res.body = body_r
+
+ r, w = IO.pipe
+
+ @res.send_body w
+
+ w.close
+
+ assert_equal 'hello', r.read
+ end
+
+ def test_send_body_string
+ @res.body = 'hello'
+
+ r, w = IO.pipe
+
+ @res.send_body w
+
+ w.close
+
+ assert_equal 'hello', r.read
+ end
+
+ def test_send_body_string_io
+ @res.body = StringIO.new 'hello'
+
+ r, w = IO.pipe
+
+ @res.send_body w
+
+ w.close
+
+ assert_equal 'hello', r.read
+ end
+
+ def test_send_body_io_chunked
+ @res.chunked = true
+
+ body_r, body_w = IO.pipe
+
+ body_w.write 'hello'
+ body_w.close
+
+ @res.body = body_r
+
+ r, w = IO.pipe
+
+ @res.send_body w
+
+ w.close
+
+ assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
+ end
+
+ def test_send_body_string_chunked
+ @res.chunked = true
+
+ @res.body = 'hello'
+
+ r, w = IO.pipe
+
+ @res.send_body w
+
+ w.close
+
+ assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
+ end
+
+ def test_send_body_string_io_chunked
+ @res.chunked = true
+
+ @res.body = StringIO.new 'hello'
+
+ r, w = IO.pipe
+
+ @res.send_body w
+
+ w.close
+
+ assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
+ end
end
end