92 lines
3.8 KiB
Text
92 lines
3.8 KiB
Text
-- | `DomainParser` is a parser for modern domain names as seen in practice.
|
|
-- | See `DomainParserRFC1035` for a domain parser restricted to RFC1035 recommandations.
|
|
module GenericParser.DomainParser where
|
|
|
|
import Prelude (bind, not, pure, ($), (&&), (*>), (<<<), (<>), (>), (-))
|
|
|
|
import Control.Alt ((<|>))
|
|
import Control.Lazy (defer)
|
|
import Data.Array as A
|
|
import Data.Either (Either(..))
|
|
import Data.Maybe (Maybe(..), maybe)
|
|
import Data.String as S
|
|
import Data.String.CodeUnits as CU
|
|
|
|
-- Import all common functions between RFC1035 and modern domain parsing.
|
|
import GenericParser.DomainParser.Common (DomainError(..), eof, ldh_str, let_dig, let_dig_hyp, max_domain_length, max_label_length, Size)
|
|
|
|
import GenericParser.Parser (Parser(..)
|
|
, success, failureError
|
|
, current_position
|
|
, alphanum, char, letter, many1, parse, string
|
|
, try, tryMaybe)
|
|
|
|
-- | From RFC 1035: <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
|
|
-- | In practice, the first character can be an underscore (for example, see `_dmarc.example.com`).
|
|
label :: Parser DomainError String
|
|
label = do
|
|
pos <- current_position
|
|
maybeu <- tryMaybe $ char '_'
|
|
maybel <- tryMaybe letter
|
|
case maybel of
|
|
Nothing -> Parser \_ -> failureError pos (Just InvalidCharacter)
|
|
Just l -> do
|
|
s <- tryMaybe ldh_str
|
|
lastpos <- current_position
|
|
let labelstr = maybe "" (\_ -> "_") maybeu <> CU.singleton l <> maybe "" CU.fromCharArray s
|
|
if (S.length labelstr > max_label_length)
|
|
then Parser \_ -> failureError pos (Just <<< LabelTooLarge $ S.length labelstr)
|
|
else if (S.length labelstr > 1 && not (parse_last_char labelstr let_dig))
|
|
then Parser \_ -> failureError (lastpos - 1) (Just InvalidCharacter)
|
|
else pure labelstr
|
|
where
|
|
-- Get the last character of a String.
|
|
last_char :: String -> Maybe Char
|
|
last_char = A.last <<< CU.toCharArray
|
|
|
|
-- Parse the last character of a String.
|
|
parse_last_char :: forall e. String -> Parser e Char -> Boolean
|
|
parse_last_char s p = case last_char s of
|
|
Nothing -> false
|
|
Just c -> case parse p { string: CU.singleton c, position: 0 } of
|
|
Left _ -> false
|
|
_ -> true
|
|
|
|
-- | From RFC 1035: <subdomain> ::= <label> | <subdomain> "." <label>
|
|
subdomain :: Parser DomainError String
|
|
subdomain = do
|
|
-- First: read a label. This is bare minimum for a subdomain.
|
|
lab <- label
|
|
upperlabels <- try do
|
|
_ <- char '.'
|
|
sub <- defer \_ -> subdomain
|
|
pure sub
|
|
case upperlabels of
|
|
Nothing -> pure lab
|
|
Just l -> pure $ lab <> "." <> l
|
|
|
|
-- | Test for the domain to be a list of subdomains then an end-of-file.
|
|
-- | Said otherwise, the input must only contain a domain (with or without a final dot '.').
|
|
sub_eof :: Parser DomainError String
|
|
sub_eof = do
|
|
sub <- subdomain
|
|
maybe_final_point <- tryMaybe $ char '.'
|
|
_ <- eof -- In case there is still some input, it fails.
|
|
pos <- current_position
|
|
let parsed_domain = did_we_parse_the_final_point maybe_final_point sub
|
|
if S.length parsed_domain > max_domain_length
|
|
then Parser \_ -> failureError pos (Just <<< DomainTooLarge $ S.length parsed_domain)
|
|
else pure parsed_domain
|
|
where
|
|
did_we_parse_the_final_point Nothing sub = sub
|
|
did_we_parse_the_final_point _ sub = sub <> "."
|
|
|
|
-- | From RFC 1035: <domain> ::= <subdomain> | " "
|
|
-- |
|
|
-- | Accepting an optional '.' at the end of the subdomain doesn't conform
|
|
-- | to the (prefered) syntax of a domain as described in RFC 1035.
|
|
-- | However, this last '.' character should be acceptable in most applications.
|
|
-- | In some cases, a fully qualified domain name (FQDN) such as `example.com.`
|
|
-- | has to be differenciated from a "relative" name (www).
|
|
domain :: Parser DomainError String
|
|
domain = (string " " *> eof) <|> sub_eof
|