Skip to content

Commit

Permalink
use ASCII-8BIT encoding for different charset MIME header
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtm committed Sep 23, 2012
1 parent 4090a30 commit 75ce314
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/mailparser/rfc2047.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ def decode(str, opt=nil)
end
words.push s
end
return words.join
begin
ret = words.join
rescue
ret = words.map{|s| s.force_encoding('binary')}.join
end
ret
end

def decode_word(str)
Expand Down
9 changes: 9 additions & 0 deletions test/test_rfc2047.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,25 @@ def test_decode_sjis()
s = MailParser::RFC2047.decode("=?sjis?b?h0A=?=", "UTF-8")
if String.method_defined? :force_encoding
assert_equal("\xe2\x91\xa0".force_encoding('utf-8'), s)
else
assert_equal("\xe2\x91\xa0", s)
end
end

def test_decode_iso2022jp()
s = MailParser::RFC2047.decode("=?iso-2022-jp?b?GyRCLSEbKEI=?=", "UTF-8")
if String.method_defined? :force_encoding
assert_equal("\xe2\x91\xa0".force_encoding('utf-8'), s)
else
assert_equal("\xe2\x91\xa0", s)
end
end

def test_decode_different_charset
s = MailParser::RFC2047.decode("=?iso-2022-jp?b?GyRCJCIbKEI=?= =?utf-8?b?44GE?=")
assert_equal("\e$B$\"\e(B\xE3\x81\x84", s)
end

def test_decode_charset_converter()
proc = Proc.new{|f,t,s| s.gsub(/o/, "X")}
s = MailParser::RFC2047.decode("=?us-ascii?q?hoge?=", :output_charset=>"utf-8", :charset_converter=>proc)
Expand Down

0 comments on commit 75ce314

Please sign in to comment.