mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
webrick: use IO.copy_stream for single range response
This is also compatible with range responses generated by Rack::File (tested with rack 2.0.3). * lib/webrick/httpresponse.rb (send_body_io): use Content-Range * lib/webrick/httpservlet/filehandler.rb (make_partial_content): use File object for the single range case * test/webrick/test_filehandler.rb (get_res_body): use send_body to test result git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62955 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2bdcd0bdde
commit
6360243fd2
3 changed files with 14 additions and 21 deletions
|
@ -410,9 +410,15 @@ module WEBrick
|
|||
buf.clear
|
||||
socket.write("0#{CRLF}#{CRLF}")
|
||||
else
|
||||
size = @header['content-length']
|
||||
size = size.to_i if size
|
||||
@sent_size = IO.copy_stream(@body, socket, size)
|
||||
if %r{\Abytes (\d+)-(\d+)/\d+\z} =~ @header['content-range']
|
||||
offset = $1.to_i
|
||||
size = $2.to_i - offset + 1
|
||||
else
|
||||
offset = nil
|
||||
size = @header['content-length']
|
||||
size = size.to_i if size
|
||||
end
|
||||
@sent_size = IO.copy_stream(@body, socket, size, offset)
|
||||
end
|
||||
ensure
|
||||
@body.close
|
||||
|
|
|
@ -116,17 +116,10 @@ module WEBrick
|
|||
elsif range = ranges[0]
|
||||
first, last = prepare_range(range, filesize)
|
||||
raise HTTPStatus::RequestRangeNotSatisfiable if first < 0
|
||||
if last == filesize - 1
|
||||
content = io.dup
|
||||
content.pos = first
|
||||
else
|
||||
io.pos = first
|
||||
content = io.read(last-first+1)
|
||||
end
|
||||
res['content-type'] = mtype
|
||||
res['content-range'] = "bytes #{first}-#{last}/#{filesize}"
|
||||
res['content-length'] = last - first + 1
|
||||
res.body = content
|
||||
res.body = io.dup
|
||||
else
|
||||
raise HTTPStatus::BadRequest
|
||||
end
|
||||
|
|
|
@ -20,16 +20,10 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def get_res_body(res)
|
||||
body = res.body
|
||||
if defined? body.read
|
||||
begin
|
||||
body.read
|
||||
ensure
|
||||
body.close
|
||||
end
|
||||
else
|
||||
body
|
||||
end
|
||||
sio = StringIO.new
|
||||
sio.binmode
|
||||
res.send_body(sio)
|
||||
sio.string
|
||||
end
|
||||
|
||||
def make_range_request(range_spec)
|
||||
|
|
Loading…
Add table
Reference in a new issue