From a2a45597bce66ebe788cedaaab848a37bd04b25a Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sun, 10 Feb 2013 11:14:16 -0800 Subject: [PATCH] Split on an exact \r\n\r\n sequence if it exists This has been broken since 17783f8536fc09b926c7425dbacfc35e0e851ef5 --- lib/mail/message.rb | 2 +- spec/mail/message_spec.rb | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/mail/message.rb b/lib/mail/message.rb index 6d1298297..ade1b7b4e 100644 --- a/lib/mail/message.rb +++ b/lib/mail/message.rb @@ -1889,7 +1889,7 @@ def text? # Additionally, I allow for the case where someone might have put whitespace # on the "gap line" def parse_message - header_part, body_part = raw_source.lstrip.split(/#{CRLF}#{WSP}*#{CRLF}(?!#{WSP})/m, 2) + header_part, body_part = raw_source.lstrip.split(/#{CRLF}#{CRLF}|#{CRLF}#{WSP}*#{CRLF}(?!#{WSP})/m, 2) self.header = header_part self.body = body_part end diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb index f66cf5a08..d99da70b1 100644 --- a/spec/mail/message_spec.rb +++ b/spec/mail/message_spec.rb @@ -189,6 +189,44 @@ def basic_email deserialized.parts.map(&:body).should == ['body', 'body'] end end + + describe "splitting" do + it "should split the body from the header" do + message = Mail::Message.new("To: Example \r\n\r\nHello there\r\n") + message.decoded.should == "Hello there\n" + end + + it "should split when the body starts with a space" do + message = Mail::Message.new("To: Example \r\n\r\n Hello there\r\n") + message.decoded.should == " Hello there\n" + end + + it "should split if the body starts with an empty line" do + message = Mail::Message.new("To: Example \r\n\r\n\r\nHello there\r\n") + message.decoded.should == "\nHello there\n" + end + + it "should split if the body starts with a blank line" do + message = Mail::Message.new("To: Example \r\n\r\n\t\r\nHello there\r\n") + message.decoded.should == "\t\nHello there\n" + end + + it 'should split after headers that contain "\r\n "' do + message = Mail::Message.new("To: Example\r\n \r\n\r\n Hello there\r\n") + message.decoded.should == " Hello there\n" + end + + it 'should split only once if there are "\r\n\r\n"s in the body' do + message = Mail::Message.new("To: Example \r\n\r\nHello\r\n\r\nthere\r\n") + message.decoded.should == "Hello\n\nthere\n" + end + + # N.B. this is not in any RFCs + it "should split on a line with whitespace on it" do + message = Mail::Message.new("To: Example \r\n \r\nHello there\r\n") + message.decoded.should == "Hello there\n" + end + end end describe "envelope line handling" do