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
|
buf.clear
|
||||||
socket.write("0#{CRLF}#{CRLF}")
|
socket.write("0#{CRLF}#{CRLF}")
|
||||||
else
|
else
|
||||||
|
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 = @header['content-length']
|
||||||
size = size.to_i if size
|
size = size.to_i if size
|
||||||
@sent_size = IO.copy_stream(@body, socket, size)
|
end
|
||||||
|
@sent_size = IO.copy_stream(@body, socket, size, offset)
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
@body.close
|
@body.close
|
||||||
|
|
|
@ -116,17 +116,10 @@ module WEBrick
|
||||||
elsif range = ranges[0]
|
elsif range = ranges[0]
|
||||||
first, last = prepare_range(range, filesize)
|
first, last = prepare_range(range, filesize)
|
||||||
raise HTTPStatus::RequestRangeNotSatisfiable if first < 0
|
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-type'] = mtype
|
||||||
res['content-range'] = "bytes #{first}-#{last}/#{filesize}"
|
res['content-range'] = "bytes #{first}-#{last}/#{filesize}"
|
||||||
res['content-length'] = last - first + 1
|
res['content-length'] = last - first + 1
|
||||||
res.body = content
|
res.body = io.dup
|
||||||
else
|
else
|
||||||
raise HTTPStatus::BadRequest
|
raise HTTPStatus::BadRequest
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,16 +20,10 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_res_body(res)
|
def get_res_body(res)
|
||||||
body = res.body
|
sio = StringIO.new
|
||||||
if defined? body.read
|
sio.binmode
|
||||||
begin
|
res.send_body(sio)
|
||||||
body.read
|
sio.string
|
||||||
ensure
|
|
||||||
body.close
|
|
||||||
end
|
|
||||||
else
|
|
||||||
body
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_range_request(range_spec)
|
def make_range_request(range_spec)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue