Skip to content

Commit

Permalink
encoding an invalid utf-8 to utf-8 does not trigger ":invalid => :rep…
Browse files Browse the repository at this point in the history
…lace". when it happens, we need to hop to utf-16le then back to utf-8
  • Loading branch information
choonkeat committed Apr 3, 2011
1 parent 6a0bd67 commit dee3397
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/mail/version_specific/ruby_1_9.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def Ruby19.b_value_decode(str)
str = Ruby19.decode_base64(match[2])
str.force_encoding(fix_encoding(encoding))
end
str.encode("utf-8", :invalid => :replace, :replace => "")
decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
end

def Ruby19.q_value_encode(str, encoding = nil)
Expand All @@ -69,7 +70,8 @@ def Ruby19.q_value_decode(str)
str = Encodings::QuotedPrintable.decode(match[2])
str.force_encoding(fix_encoding(encoding))
end
str.encode("utf-8", :invalid => :replace, :replace => "")
decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
end

def Ruby19.param_decode(str, encoding)
Expand Down
5 changes: 5 additions & 0 deletions spec/mail/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,9 @@
mail.parts[0].content_type.should == "text/html; charset=ISO-8859-1"
end

it "should skip invalid characters" do
m = Mail.new
m['Subject'] = Mail::SubjectField.new("=?utf-8?Q?Hello_=96_World?=")
lambda { m.subject.should be_valid_encoding }.should_not raise_error
end
end

0 comments on commit dee3397

Please sign in to comment.