Move escape_uri method to a more suitable place
This commit is contained in:
parent
5fbfe34356
commit
e30851e45a
|
@ -1,4 +1,5 @@
|
||||||
require_dependency 'nokogiri'
|
require_dependency 'nokogiri'
|
||||||
|
require_dependency 'url_helper'
|
||||||
|
|
||||||
class TopicEmbed < ActiveRecord::Base
|
class TopicEmbed < ActiveRecord::Base
|
||||||
include Trashable
|
include Trashable
|
||||||
|
@ -26,16 +27,6 @@ class TopicEmbed < ActiveRecord::Base
|
||||||
"\n<hr>\n<small>#{I18n.t('embed.imported_from', link: "<a href='#{url}'>#{url}</a>")}</small>\n"
|
"\n<hr>\n<small>#{I18n.t('embed.imported_from', link: "<a href='#{url}'>#{url}</a>")}</small>\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
DOUBLE_ESCAPED_EXPR = /%25([0-9a-f]{2})/i
|
|
||||||
|
|
||||||
# Prevents double URL encode
|
|
||||||
# https://stackoverflow.com/a/37599235
|
|
||||||
def self.escape_uri(uri, pattern = URI::UNSAFE)
|
|
||||||
encoded = URI.encode(uri, pattern)
|
|
||||||
encoded.gsub!(DOUBLE_ESCAPED_EXPR, '%\1')
|
|
||||||
encoded
|
|
||||||
end
|
|
||||||
|
|
||||||
# Import an article from a source (RSS/Atom/Other)
|
# Import an article from a source (RSS/Atom/Other)
|
||||||
def self.import(user, url, title, contents)
|
def self.import(user, url, title, contents)
|
||||||
return unless url =~ /^https?\:\/\//
|
return unless url =~ /^https?\:\/\//
|
||||||
|
@ -87,7 +78,7 @@ class TopicEmbed < ActiveRecord::Base
|
||||||
def self.find_remote(url)
|
def self.find_remote(url)
|
||||||
require 'ruby-readability'
|
require 'ruby-readability'
|
||||||
|
|
||||||
url = escape_uri(url)
|
url = UrlHelper.escape_uri(url)
|
||||||
original_uri = URI.parse(url)
|
original_uri = URI.parse(url)
|
||||||
opts = {
|
opts = {
|
||||||
tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote],
|
tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote],
|
||||||
|
@ -130,7 +121,7 @@ class TopicEmbed < ActiveRecord::Base
|
||||||
src = node[url_param]
|
src = node[url_param]
|
||||||
unless (src.nil? || src.empty?)
|
unless (src.nil? || src.empty?)
|
||||||
begin
|
begin
|
||||||
uri = URI.parse(escape_uri(src))
|
uri = URI.parse(UrlHelper.escape_uri(src))
|
||||||
unless uri.host
|
unless uri.host
|
||||||
uri.scheme = original_uri.scheme
|
uri.scheme = original_uri.scheme
|
||||||
uri.host = original_uri.host
|
uri.host = original_uri.host
|
||||||
|
@ -170,7 +161,7 @@ class TopicEmbed < ActiveRecord::Base
|
||||||
# Convert any relative URLs to absolute. RSS is annoying for this.
|
# Convert any relative URLs to absolute. RSS is annoying for this.
|
||||||
def self.absolutize_urls(url, contents)
|
def self.absolutize_urls(url, contents)
|
||||||
url = normalize_url(url)
|
url = normalize_url(url)
|
||||||
uri = URI(escape_uri(url))
|
uri = URI(UrlHelper.escape_uri(url))
|
||||||
prefix = "#{uri.scheme}://#{uri.host}"
|
prefix = "#{uri.scheme}://#{uri.host}"
|
||||||
prefix << ":#{uri.port}" if uri.port != 80 && uri.port != 443
|
prefix << ":#{uri.port}" if uri.port != 80 && uri.port != 443
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ require 'socket'
|
||||||
require 'ipaddr'
|
require 'ipaddr'
|
||||||
require 'excon'
|
require 'excon'
|
||||||
require 'rate_limiter'
|
require 'rate_limiter'
|
||||||
|
require 'url_helper'
|
||||||
|
|
||||||
# Determine the final endpoint for a Web URI, following redirects
|
# Determine the final endpoint for a Web URI, following redirects
|
||||||
class FinalDestination
|
class FinalDestination
|
||||||
|
@ -237,7 +238,7 @@ class FinalDestination
|
||||||
end
|
end
|
||||||
|
|
||||||
def escape_url
|
def escape_url
|
||||||
TopicEmbed.escape_uri(
|
UrlHelper.escape_uri(
|
||||||
CGI.unescapeHTML(@url),
|
CGI.unescapeHTML(@url),
|
||||||
Regexp.new("[^#{URI::PATTERN::UNRESERVED}#{URI::PATTERN::RESERVED}#]")
|
Regexp.new("[^#{URI::PATTERN::UNRESERVED}#{URI::PATTERN::RESERVED}#]")
|
||||||
)
|
)
|
||||||
|
|
|
@ -21,4 +21,14 @@ class UrlHelper
|
||||||
url.sub(/^http:/i, "")
|
url.sub(/^http:/i, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
DOUBLE_ESCAPED_REGEXP ||= /%25([0-9a-f]{2})/i
|
||||||
|
|
||||||
|
# Prevents double URL encode
|
||||||
|
# https://stackoverflow.com/a/37599235
|
||||||
|
def self.escape_uri(uri, pattern = URI::UNSAFE)
|
||||||
|
encoded = URI.encode(uri, pattern)
|
||||||
|
encoded.gsub!(DOUBLE_ESCAPED_REGEXP, '%\1')
|
||||||
|
encoded
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -79,4 +79,26 @@ describe UrlHelper do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#escape_uri" do
|
||||||
|
it "doesn't escape simple URL" do
|
||||||
|
url = UrlHelper.escape_uri('http://example.com/foo/bar')
|
||||||
|
expect(url).to eq('http://example.com/foo/bar')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "escapes unsafe chars" do
|
||||||
|
url = UrlHelper.escape_uri("http://example.com/?a=\11\15")
|
||||||
|
expect(url).to eq('http://example.com/?a=%09%0D')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "escapes non-ascii chars" do
|
||||||
|
url = UrlHelper.escape_uri('http://example.com/ماهی')
|
||||||
|
expect(url).to eq('http://example.com/%D9%85%D8%A7%D9%87%DB%8C')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't escape already escaped chars" do
|
||||||
|
url = UrlHelper.escape_uri('http://example.com/foo%20bar/foo bar/')
|
||||||
|
expect(url).to eq('http://example.com/foo%20bar/foo%20bar/')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -233,25 +233,4 @@ describe TopicEmbed do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context ".escape_uri" do
|
|
||||||
it "doesn't escape simple URL" do
|
|
||||||
url = TopicEmbed.escape_uri('http://example.com/foo/bar')
|
|
||||||
expect(url).to eq('http://example.com/foo/bar')
|
|
||||||
end
|
|
||||||
|
|
||||||
it "escapes unsafe chars" do
|
|
||||||
url = TopicEmbed.escape_uri("http://example.com/?a=\11\15")
|
|
||||||
expect(url).to eq('http://example.com/?a=%09%0D')
|
|
||||||
end
|
|
||||||
|
|
||||||
it "escapes non-ascii chars" do
|
|
||||||
url = TopicEmbed.escape_uri('http://example.com/ماهی')
|
|
||||||
expect(url).to eq('http://example.com/%D9%85%D8%A7%D9%87%DB%8C')
|
|
||||||
end
|
|
||||||
|
|
||||||
it "doesn't escape already escaped chars" do
|
|
||||||
url = TopicEmbed.escape_uri('http://example.com/foo%20bar/foo bar/')
|
|
||||||
expect(url).to eq('http://example.com/foo%20bar/foo%20bar/')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue