Seal.hs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SMTP.Parser.Seal (
  3. parseURISeal
  4. ) where
  5. import Data.SMTP.Types.Seal
  6. -- import Data.SMTP.Types.Resource
  7. import qualified Data.Attoparsec.Text as A
  8. import qualified Data.ByteString.Base64 as B64
  9. import Data.ByteString (ByteString)
  10. import Text.StringConvert
  11. parseURISeal :: A.Parser Seal
  12. parseURISeal = do
  13. mycp <- base64
  14. A.string ":"
  15. mycode <- base64
  16. A.choice [
  17. do
  18. A.string ":"
  19. mynonce <- base64
  20. return . Seal mycp mycode $ Just mynonce,
  21. return $ Seal mycp mycode Nothing
  22. ]
  23. base64 :: A.Parser ByteString
  24. base64 = do
  25. e <- A.takeWhile isBase64Char
  26. case B64.decode . s . map replaceSlash . s $ e of
  27. Left r -> fail r
  28. Right r -> return r
  29. where
  30. replaceSlash '-' = '/'
  31. replaceSlash c = c
  32. -- | c == W8._hyphen = W8._slash
  33. -- | otherwise = c
  34. isBase64Char c
  35. | c >= '0' && c <= '9' = True
  36. | c >= 'a' && c <= 'z' = True
  37. | c >= 'A' && c <= 'Z' = True
  38. | c == '+' || c == '-' || c == '=' = True
  39. | otherwise = False
  40. -- headersToSeal :: [Header] -> Maybe Seal
  41. -- headersToSeal hh = let
  42. -- mycp = decode2maybe =<< getH hh cpHeaderName
  43. -- mynonce = decode2maybe =<< getH hh nonceHeaderName
  44. -- mycode = decode2maybe =<< getH hh sealHeaderName
  45. -- in do
  46. -- cp' <- mycp
  47. -- code' <- mycode
  48. -- Just $ Seal cp' code' mynonce
  49. -- where
  50. -- getH :: [Header] -> String -> Maybe ByteString
  51. -- getH hh' h = C8.pack <$> getHeaderValue hh' h
  52. -- either2maybef :: (a -> Either b c) -> a -> Maybe c
  53. -- either2maybef f v = either2maybe $ f v
  54. -- either2maybe :: Either a b -> Maybe b
  55. -- either2maybe (Left _) = Nothing
  56. -- either2maybe (Right v) = Just v
  57. -- decode2maybe = either2maybef B64.decode