FIX: proper hack to support underscores in URLs

This commit is contained in:
Régis Hanol 2014-11-12 18:15:50 +01:00
parent e26e5312d7
commit 961f676b91
3 changed files with 12 additions and 17 deletions

View file

@ -249,10 +249,6 @@ gem 'stringex', require: false
gem 'logster' gem 'logster'
# we need that to support underscore in URLs (mostly when using S3 for storing files)
# cf. http://stackoverflow.com/a/17108137/11983
gem 'addressable'
# perftools only works on 1.9 atm # perftools only works on 1.9 atm
group :profile do group :profile do
# travis refuses to install this, instead of fuffing, just avoid it for now # travis refuses to install this, instead of fuffing, just avoid it for now

View file

@ -407,7 +407,6 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
actionpack-action_caching actionpack-action_caching
active_model_serializers (~> 0.8.0) active_model_serializers (~> 0.8.0)
addressable
annotate annotate
barber barber
better_errors better_errors

View file

@ -1,15 +1,4 @@
require "open-uri" require "open-uri"
require "addressable/uri"
class URI::Parser
# HACK to support underscores in URLs
def split(url)
a = Addressable::URI::parse(url)
[a.scheme, a.userinfo, a.host, a.port, nil, a.path, nil, a.query, a.fragment]
end
end
class FileHelper class FileHelper
@ -20,7 +9,7 @@ class FileHelper
def self.download(url, max_file_size, tmp_file_name, follow_redirect=false) def self.download(url, max_file_size, tmp_file_name, follow_redirect=false)
raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\// raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\//
uri = URI.parse(url) uri = parse_url(url)
extension = File.extname(uri.path) extension = File.extname(uri.path)
tmp = Tempfile.new([tmp_file_name, extension]) tmp = Tempfile.new([tmp_file_name, extension])
@ -46,4 +35,15 @@ class FileHelper
@@images_regexp ||= /\.(#{images.to_a.join("|")})$/i @@images_regexp ||= /\.(#{images.to_a.join("|")})$/i
end end
# HACK to support underscores in URLs
# cf. http://stackoverflow.com/a/18938253/11983
def self.parse_url(url)
URI.parse(url)
rescue URI::InvalidURIError
host = url.match(".+\:\/\/([^\/]+)")[1]
uri = URI.parse(url.sub(host, 'valid-host'))
uri.instance_variable_set("@host", host)
uri
end
end end