Skip to content

Commit

Permalink
Merge pull request #56 from CDLUC3/mrt284
Browse files Browse the repository at this point in the history
Mrt284 - Gracefully handle encoding issue in presigned-file request
  • Loading branch information
terrywbrady authored Apr 7, 2020
2 parents 2de6fe5 + ad2d00e commit a35325f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 15 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,22 @@ def url_for_with_proto(opts)
url_for(opts)
end

def log_error(message, exception = nil)
msg = message
msg << ": #{exception}" if exception

(log = Rails.logger) && log.error(msg)
warn(msg)
end

def url_string_with_proto(url, force_https = false)
return url unless force_https || APP_CONFIG['proto_force'] == 'https'
uri = URI.parse(url)
uri.scheme = 'https'
uri.to_s
begin
uri = URI.parse(url)
uri.scheme = 'https'
uri.to_s
rescue StandardError => e
log_error("Url format error caught: #{url}", e)
end
end
end
8 changes: 8 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@
# For testing purposes, simulate APP_CONFIG['proto_force'] == 'https'
expect(controller.send(:url_string_with_proto, http_req, true)).to eq(https_req), 'Location should start with https'
end

it 'catch encoding error in url' do
host = 'foo.bar'
https_req = "https://#{host}/api/presign-file/ark:/13030/m5sf7w9f/1/producer/Peuß%20Dryad%20raw%20data.xlsx?no_redirect=true"
expect(Rails.logger).to receive(:error).with(/Url format error.*/)
expect(controller.send(:url_string_with_proto, https_req, true)).not_to eq(https_req), 'Expect encoding error for URL'
end
end

describe ':stream_response' do
Expand Down Expand Up @@ -226,3 +233,4 @@
end

end
# NOTE:

0 comments on commit a35325f

Please sign in to comment.