Skip to content

Commit

Permalink
Add missing trailing equal sign snoyberg#17
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Aug 11, 2013
1 parent 5505302 commit 143086c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
10 changes: 5 additions & 5 deletions mime-mail/Network/Mail/Mime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ buildQPs =
rest
| S.null y = qps
| otherwise = QPPlain y : qps
in helper (S.length x) (copyByteString x) rest
in helper (S.length x) (copyByteString x) (S.null y) rest
QPEscape bs ->
let toTake = (75 - currLine) `div` 3
(x, y) = S.splitAt toTake bs
Expand All @@ -392,7 +392,7 @@ buildQPs =
| otherwise = QPEscape y : qps
in if toTake == 0
then copyByteString "\r\n" `mappend` go 0 (qp:qps)
else helper (S.length x * 3) (escape x) rest
else helper (S.length x * 3) (escape x) (S.null y) rest
where
escape =
S.foldl' add mempty
Expand All @@ -403,11 +403,11 @@ buildQPs =
escaped = fromWord8 61 `mappend` hex (w `shiftR` 4)
`mappend` hex (w .&. 15)

helper added builder rest =
helper added builder noMore rest =
builder' `mappend` go newLine rest
where
(newLine, builder')
| added + currLine >= 75 =
| not noMore || (added + currLine) >= 75 =
(0, builder `mappend` copyByteString "=\r\n")
| otherwise = (added + currLine, builder)

Expand All @@ -416,7 +416,7 @@ buildQPs =
if currLine <= 73
then enc
else copyByteString "\r\n=" `mappend` enc
| otherwise = helper 1 raw qps
| otherwise = helper 1 raw (currLine < 76) qps

-- | The first parameter denotes whether the input should be treated as text.
-- If treated as text, then CRs will be stripped and LFs output as CRLFs. If
Expand Down
1 change: 1 addition & 0 deletions mime-mail/mime-mail.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test-suite tests
, mime-mail
, blaze-builder
, bytestring
, text
ghc-options: -Wall

source-repository head
Expand Down
18 changes: 18 additions & 0 deletions mime-mail/test/Network/Mail/MimeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Network.Mail.Mime
import qualified Data.ByteString.Lazy.Char8 as L8
import Blaze.ByteString.Builder (toLazyByteString)
import Control.Monad (forM_)
import Data.Text.Lazy.Encoding (encodeUtf8)

spec :: Spec
spec = describe "Network.Mail.Mime" $ do
Expand Down Expand Up @@ -38,6 +39,23 @@ spec = describe "Network.Mail.Mime" $ do
dec = "If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy."
in toLazyByteString (quotedPrintable True dec) `shouldBe` enc

it "issue #17- as text" $
let enc = "</a>=E3=81=AB=E3=81=A4=E3=81=84=E3=81=A6=E3=81=AE=E3=83=86=E3=82=B9=E3=83=\r\n=88"
dec = encodeUtf8 "</a>についてのテスト"
in toLazyByteString (quotedPrintable True dec) `shouldBe` enc

it "issue #17- as binary" $
let enc = "</a>=E3=81=AB=E3=81=A4=E3=81=84=E3=81=A6=E3=81=AE=E3=83=86=E3=82=B9=E3=83=\r\n=88"
dec = encodeUtf8 "</a>についてのテスト"
in toLazyByteString (quotedPrintable False dec) `shouldBe` enc

it "concrete example: over 76 characters" $
let orig = "\240\238\191aa\149aa\226a\235\255a=aa\SI\159a\187a\147aa\ACKa\184aaaaaa\191a\237aaaa\EM a"
gen = toLazyByteString $ quotedPrintable True orig
in if all (\l -> L8.length l <= 76) $ lines' gen
then True
else error $ show $ lines' gen

lines' :: L8.ByteString -> [L8.ByteString]
lines' =
map stripCR . L8.lines
Expand Down

0 comments on commit 143086c

Please sign in to comment.