{-# LANGUAGE OverloadedStrings #-}
module Data.Text.IDN
(
toASCII
, toUnicode
, validateLabel
, module Data.Text.IDN.Types
) where
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.IDN.Types
import Data.Text.IDN.Internal.Unicode
import Data.Text.IDN.Internal.Validation
import Data.Text.IDN.Internal.Punycode (encodePunycode, decodePunycode)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Data.Char (ord, isAscii)
toASCII :: Text -> Either IDNError Text
toASCII :: Text -> Either IDNError Text
toASCII Text
input = do
DomainName
domain <- Text -> Either IDNError DomainName
mkDomainName Text
input
NonEmpty Label
processedLabels <- (Label -> Either IDNError Label)
-> NonEmpty Label -> Either IDNError (NonEmpty Label)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
traverse Label -> Either IDNError Label
processLabelToASCII (DomainName -> NonEmpty Label
labels DomainName
domain)
Text -> Either IDNError Text
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Either IDNError Text) -> Text -> Either IDNError Text
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
T.intercalate Text
"." ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Label -> Text) -> [Label] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Label -> Text
labelText (NonEmpty Label -> [Label]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty Label
processedLabels)
where
labelText :: Label -> Text
labelText (ULabel Text
t) = Text
t
labelText (ALabel Text
t) = Text
t
toUnicode :: Text -> Either IDNError Text
toUnicode :: Text -> Either IDNError Text
toUnicode Text
input = do
DomainName
domain <- Text -> Either IDNError DomainName
mkDomainName Text
input
NonEmpty Label
processedLabels <- (Label -> Either IDNError Label)
-> NonEmpty Label -> Either IDNError (NonEmpty Label)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NonEmpty a -> f (NonEmpty b)
traverse Label -> Either IDNError Label
processLabelToUnicode (DomainName -> NonEmpty Label
labels DomainName
domain)
Text -> Either IDNError Text
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Either IDNError Text) -> Text -> Either IDNError Text
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
T.intercalate Text
"." ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Label -> Text) -> [Label] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Label -> Text
labelText (NonEmpty Label -> [Label]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty Label
processedLabels)
where
labelText :: Label -> Text
labelText (ULabel Text
t) = Text
t
labelText (ALabel Text
t) = Text
t
processLabelToASCII :: Label -> Either IDNError Label
processLabelToASCII :: Label -> Either IDNError Label
processLabelToASCII (ALabel Text
t)
| Text
"xn--" Text -> Text -> Bool
`T.isPrefixOf` Text
t = do
Label
ulabel <- Label -> Either IDNError Label
processLabelToUnicode (Text -> Label
ALabel Text
t)
Label -> Either IDNError Label
processLabelToASCII Label
ulabel
| Bool
otherwise =
Text -> Either IDNError ()
validateLabel Text
t Either IDNError ()
-> Either IDNError Label -> Either IDNError Label
forall a b.
Either IDNError a -> Either IDNError b -> Either IDNError b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Label -> Either IDNError Label
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Label
ALabel Text
t)
processLabelToASCII (ULabel Text
t)
| Text -> Bool
isAsciiLabel Text
t = do
Text -> Either IDNError ()
validateLabel Text
t
Label -> Either IDNError Label
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Label
ULabel Text
t)
| Bool
otherwise = do
Text -> Either IDNError ()
validateLabel Text
t
Text
encoded <- case Text -> Either PunycodeError Text
encodePunycode Text
t of
Left PunycodeError
err -> IDNError -> Either IDNError Text
forall a b. a -> Either a b
Left (Text -> Text -> IDNError
PunycodeDecodeError Text
t (String -> Text
T.pack (PunycodeError -> String
forall a. Show a => a -> String
show PunycodeError
err)))
Right Text
e -> Text -> Either IDNError Text
forall a b. b -> Either a b
Right Text
e
let aLabel :: Text
aLabel = Text
"xn--" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
encoded
if Text -> Int
T.length Text
aLabel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
63
then IDNError -> Either IDNError Label
forall a b. a -> Either a b
Left (Int -> Int -> IDNError
LabelTooLong (Text -> Int
T.length Text
aLabel) Int
63)
else Label -> Either IDNError Label
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Label
ALabel Text
aLabel)
processLabelToUnicode :: Label -> Either IDNError Label
processLabelToUnicode :: Label -> Either IDNError Label
processLabelToUnicode (ULabel Text
t) =
Text -> Either IDNError ()
validateLabel Text
t Either IDNError ()
-> Either IDNError Label -> Either IDNError Label
forall a b.
Either IDNError a -> Either IDNError b -> Either IDNError b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Label -> Either IDNError Label
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Label
ULabel Text
t)
processLabelToUnicode (ALabel Text
t)
| Text
"xn--" Text -> Text -> Bool
`T.isPrefixOf` Text -> Text
T.toLower Text
t = do
let encoded :: Text
encoded = Int -> Text -> Text
T.drop Int
4 Text
t
encodedLower :: Text
encodedLower = Text -> Text
T.toLower Text
encoded
Text
decoded <- case Text -> Either PunycodeError Text
decodePunycode Text
encodedLower of
Left PunycodeError
err -> IDNError -> Either IDNError Text
forall a b. a -> Either a b
Left (Text -> Text -> IDNError
PunycodeDecodeError Text
encoded (String -> Text
T.pack (PunycodeError -> String
forall a. Show a => a -> String
show PunycodeError
err)))
Right Text
d -> Text -> Either IDNError Text
forall a b. b -> Either a b
Right Text
d
if Text -> Bool
isAsciiLabel Text
decoded
then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Text -> Text -> IDNError
PunycodeDecodeError Text
encoded Text
"Punycode used for pure ASCII label")
else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Text
reencoded <- case Text -> Either PunycodeError Text
encodePunycode Text
decoded of
Left PunycodeError
err -> IDNError -> Either IDNError Text
forall a b. a -> Either a b
Left (Text -> Text -> IDNError
PunycodeDecodeError Text
decoded (String -> Text
T.pack (PunycodeError -> String
forall a. Show a => a -> String
show PunycodeError
err)))
Right Text
e -> Text -> Either IDNError Text
forall a b. b -> Either a b
Right Text
e
if Text
reencoded Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
encodedLower
then IDNError -> Either IDNError Label
forall a b. a -> Either a b
Left (Text -> Text -> IDNError
PunycodeDecodeError Text
encoded Text
"Round-trip re-encoding failed")
else do
Text -> Either IDNError ()
validateLabel Text
decoded
Label -> Either IDNError Label
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Label
ULabel Text
decoded)
| Bool
otherwise =
Text -> Either IDNError ()
validateLabel Text
t Either IDNError ()
-> Either IDNError Label -> Either IDNError Label
forall a b.
Either IDNError a -> Either IDNError b -> Either IDNError b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Label -> Either IDNError Label
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Label
ALabel Text
t)
validateLabel :: Text -> Either IDNError ()
validateLabel :: Text -> Either IDNError ()
validateLabel Text
label = do
if Text -> Bool
T.null Text
label
then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left IDNError
EmptyLabel
else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
if Text -> Int
T.length Text
label Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
63
then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Int -> Int -> IDNError
LabelTooLong (Text -> Int
T.length Text
label) Int
63)
else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Text -> Either IDNError ()
checkHyphens Text
label
case Text -> Maybe (Char, Text)
T.uncons Text
label of
Just (Char
c, Text
_) | Char -> Bool
isCombiningMark Char
c ->
IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> Int -> IDNError
StartsWithCombiningMark Char
c (Char -> Int
ord Char
c))
Maybe (Char, Text)
_ -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
(Char -> Either IDNError ()) -> String -> Either IDNError ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Char -> Either IDNError ()
validateCodePoint (Text -> String
T.unpack Text
label)
Text -> Either IDNError ()
validateContextualRules Text
label
Text -> Either IDNError ()
validateBidiRules Text
label
() -> Either IDNError ()
forall a. a -> Either IDNError a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkHyphens :: Text -> Either IDNError ()
checkHyphens :: Text -> Either IDNError ()
checkHyphens Text
label = do
case Text -> Maybe (Char, Text)
T.uncons Text
label of
Just (Char
'-', Text
_) -> IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (HyphenError -> IDNError
InvalidHyphenPosition HyphenError
StartsWithHyphen)
Maybe (Char, Text)
_ -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
case Text -> Maybe (Text, Char)
T.unsnoc Text
label of
Just (Text
_, Char
'-') -> IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (HyphenError -> IDNError
InvalidHyphenPosition HyphenError
EndsWithHyphen)
Maybe (Text, Char)
_ -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
let labelLower :: Text
labelLower = Text -> Text
T.toLower Text
label
if Text -> Int
T.length Text
label Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
4 Bool -> Bool -> Bool
&& Bool -> Bool
not (Text
"xn--" Text -> Text -> Bool
`T.isPrefixOf` Text
labelLower)
then if HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label Int
2 Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-' Bool -> Bool -> Bool
&& HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label Int
3 Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-'
then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (HyphenError -> IDNError
InvalidHyphenPosition HyphenError
HyphensAt3And4NotPunycode)
else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
validateCodePoint :: Char -> Either IDNError ()
validateCodePoint :: Char -> Either IDNError ()
validateCodePoint Char
c =
case Char -> CodePointStatus
codePointStatus Char
c of
CodePointStatus
PVALID -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
CodePointStatus
DISALLOWED -> IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> Int -> IDNError
DisallowedCodePoint Char
c (Char -> Int
ord Char
c))
CodePointStatus
CONTEXTJ -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
CodePointStatus
CONTEXTO -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
CodePointStatus
UNASSIGNED -> IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> Int -> IDNError
DisallowedCodePoint Char
c (Char -> Int
ord Char
c))
isAsciiLabel :: Text -> Bool
isAsciiLabel :: Text -> Bool
isAsciiLabel = (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isAscii