Skip to content

Commit

Permalink
Change parse_http to follow http redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioperrella committed Feb 10, 2021
1 parent 5930ed7 commit 584ed63
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions format_parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'exifr', '~> 1', '>= 1.3.8'
spec.add_dependency 'id3tag', '~> 0.14'
spec.add_dependency 'faraday', '~> 0.13'
spec.add_dependency 'faraday_middleware', '~> 0.14'
spec.add_dependency 'measurometer', '~> 1'

spec.add_development_dependency 'rspec', '~> 3.0'
Expand Down
7 changes: 6 additions & 1 deletion lib/remote_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class InvalidRequest < UpstreamError
# @param uri[URI, String] the remote URL to obtain
def initialize(uri)
require 'faraday'
require 'faraday_middleware/response/follow_redirects'
@uri = uri
@pos = 0
@remote_size = false
Expand Down Expand Up @@ -78,7 +79,11 @@ def request_range(range)
# We use a GET and not a HEAD request followed by a GET because
# S3 does not allow HEAD requests if you only presigned your URL for GETs, so we
# combine the first GET of a segment and retrieving the size of the resource
response = Faraday.get(@uri, nil, range: 'bytes=%d-%d' % [range.begin, range.end])
conn = Faraday.new do |faraday|
faraday.use FaradayMiddleware::FollowRedirects
faraday.adapter Faraday.default_adapter
end
response = conn.get(@uri, nil, range: 'bytes=%d-%d' % [range.begin, range.end])

case response.status
when 200, 206
Expand Down
11 changes: 11 additions & 0 deletions spec/remote_fetching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
}
@server = WEBrick::HTTPServer.new(options)
@server.mount '/', WEBrick::HTTPServlet::FileHandler, fixtures_dir
@server.mount_proc '/redirect' do |req,res|
res.status = 302
res.header['Location'] = req.path.sub("/redirect","")
end
trap('INT') { @server.stop }
@server_thread = Thread.new { @server.start }
end
Expand Down Expand Up @@ -91,6 +95,13 @@
end
end

context 'when the server respond with a redirect' do
it 'follows the redirect' do
file_information = FormatParser.parse_http('http://localhost:9399/redirect/TIFF/test.tif')
expect(file_information.format).to eq(:tif)
end
end

after(:all) do
@server.stop
@server_thread.join(0.5)
Expand Down

0 comments on commit 584ed63

Please sign in to comment.