Skip to content

Commit

Permalink
Read file URIs in binary mode
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Apr 4, 2011
1 parent ad35c01 commit 01039f5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* Blank cookie values are now skipped. GH #80
* Mechanize now adds a '.' to cookie domains if no '.' was sent. This is
not allowed by RFC 2109 but does appear in RFC 2965. GH #86
* file URIs are now read in binary mode. GH #83

=== 1.0.0

Expand Down
6 changes: 4 additions & 2 deletions lib/mechanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ def response_parse response, body, uri

def response_read response, request
body = StringIO.new
body.set_encoding Encoding::BINARY, Encoding::BINARY if
body.respond_to? :set_encoding
total = 0

response.read_body { |part|
Expand All @@ -674,6 +676,8 @@ def response_read response, request
end

case response['Content-Encoding']
when nil, 'none', '7bit', 'x-gzip' then
body.string
when 'gzip' then
Mechanize.log.debug('gunzip body') if Mechanize.log

Expand All @@ -694,8 +698,6 @@ def response_read response, request
zio.close if zio and not zio.closed?
end
end
when nil, 'none', '7bit', 'x-gzip' then
body.read
else
raise Mechanize::Error,
"Unsupported Content-Encoding: #{response['Content-Encoding']}"
Expand Down
20 changes: 14 additions & 6 deletions lib/mechanize/file_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def read_body
if directory?
yield dir_body
else
yield File.read(@file_path)
open @file_path, 'rb' do |io|
yield io.read
end
end
else
yield ''
Expand Down Expand Up @@ -46,15 +48,21 @@ def get_fields(key)
end

private

def dir_body
'<html><body>' +
Dir[::File.join(@file_path, '*')].map { |f|
"<a href=\"file://#{f}\">#{::File.basename(f)}</a>"
}.join("\n") + '</body></html>'
body = %w[<html><body>]
body.concat Dir[File.join(@file_path, '*')].map { |f|
"<a href=\"file://#{f}\">#{File.basename(f)}</a>"
}
body << %w[</body></html>]

body = body.join "\n"
body.force_encoding Encoding::BINARY if body.respond_to? :force_encoding
body
end

def directory?
::File.directory?(@file_path)
File.directory?(@file_path)
end
end

2 changes: 2 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'mechanize'
require 'webrick/httputils'
require 'servlets'
require 'tmpdir'
require 'tempfile'

BASE_DIR = File.dirname(__FILE__)

Expand Down
22 changes: 22 additions & 0 deletions test/test_mechanize.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding: utf-8

require 'helper'

class TestMechanize < Test::Unit::TestCase
Expand Down Expand Up @@ -401,6 +403,26 @@ def @res.content_length() 4 end
assert_equal 'Unsupported Content-Encoding: unknown', e.message
end

def test_response_read_file
Tempfile.open 'pi.txt' do |tempfile|
tempfile.write \n"
tempfile.flush
tempfile.rewind

uri = URI.parse "file://#{tempfile.path}"
req = Mechanize::FileRequest.new uri
res = Mechanize::FileResponse.new tempfile.path

body = @agent.response_read res, req

expected = \n"
expected.force_encoding Encoding::BINARY if expected.respond_to? :encoding

assert_equal expected, body
assert_equal Encoding::BINARY, body.encoding if body.respond_to? :encoding
end
end

def test_response_read_no_body
req = Net::HTTP::Options.new '/'

Expand Down

0 comments on commit 01039f5

Please sign in to comment.