From f88def5f5b419214b422fe8d2061c1ee638c4e06 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Tue, 2 Feb 2021 17:34:00 +0100 Subject: [PATCH] PERF: Avoid lookbehinds when replacing links in imported emails (#11931) Follow-up to 3c678df942b9322bfda5eb47d428b1c692ac0bf4 --- lib/email_cook.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/email_cook.rb b/lib/email_cook.rb index 9bc96fef762..89b59891f12 100644 --- a/lib/email_cook.rb +++ b/lib/email_cook.rb @@ -3,10 +3,6 @@ # A very simple formatter for imported emails class EmailCook - def self.url_regexp - @url_regexp ||= /((?:https?:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»“”‘’\s]))/ - end - def self.raw_regexp @raw_regexp ||= /^\[plaintext\]$\n(.*)\n^\[\/plaintext\]$(?:\s^\[attachments\]$\n(.*)\n^\[\/attachments\]$)?(?:\s^\[elided\]$\n(.*)\n^\[\/elided\]$)?/m end @@ -25,17 +21,21 @@ class EmailCook def link_string!(line, unescaped_line) unescaped_line = unescaped_line.strip - unescaped_line.scan(EmailCook.url_regexp).each do |m| - url = m[0] - - if unescaped_line == url - # this could be oneboxed - val = %|#{url}| - else - val = %|#{url}| + line.gsub!(/\S+/) do |str| + if str.match?(/^(https?:\/\/)[\S]+$/i) + begin + url = URI.parse(str).to_s + if unescaped_line == url + # this could be oneboxed + str = %|#{url}| + else + str = %|#{url}| + end + rescue URI::Error + # don't fail if uri does not parse + end end - - line.gsub!(url, val) + str end end