1 -- | The 'Domain' data type and its parser. A 'Domain' represents a
2 -- name in the domain name system (DNS) as described by
3 -- RFC1035. In particular, we enforce the restrictions from Section
4 -- 2.3.1 \"Preferred name syntax\". See for example,
6 -- <https://tools.ietf.org/html/rfc1035#section-2.3.1>
13 import Data.Char ( toLower )
27 import qualified Text.Parsec as Parsec ( digit, letter)
28 import Text.Parsec.String ( Parser )
30 import Pretty ( Pretty(..) )
34 -- | A wrapper around a digit character.
36 newtype Digit = Digit Char deriving (Eq, Show)
37 instance Pretty Digit where pretty_show (Digit d) = [d]
39 -- | Parse a single digit, but wrap it in our 'Digit' type.
42 digit = fmap Digit Parsec.digit
47 -- | A wrapper around a letter character.
49 newtype Letter = Letter Char deriving (Show)
50 instance Pretty Letter where pretty_show (Letter l) = [l]
53 -- | Parse a single letter, but wrap it in our 'Letter' type.
55 letter :: Parser Letter
56 letter = fmap Letter Parsec.letter
58 -- | The derived instance of 'Eq' for letters is incorrect. All
59 -- comparisons should be made case-insensitively. The following
60 -- is an excerpt from RFC1035:
62 -- 2.3.3. Character Case
64 -- For all parts of the DNS that are part of the official
65 -- protocol, all comparisons between character strings (e.g.,
66 -- labels, domain names, etc.) are done in a case-insensitive
69 -- Since each part of DNS name is composed of our custom types, it
70 -- suffices to munge the equality for 'Letter'.
72 instance Eq Letter where
73 (Letter l1) == (Letter l2) = (toLower l1) == (toLower l2)
77 -- | A sum type representing either a letter or a digit.
84 instance Pretty LetDig where
85 pretty_show (LetDigLetter l) = pretty_show l
86 pretty_show (LetDigDigit d) = pretty_show d
88 -- | Parse a letter or a digit and wrap it in our 'LetDig' type.
90 let_dig :: Parser LetDig
91 let_dig = (fmap LetDigLetter letter) <|> (fmap LetDigDigit digit)
96 -- | A wrapper around a single hyphen character.
98 newtype Hyphen = Hyphen Char deriving (Eq, Show)
99 instance Pretty Hyphen where pretty_show (Hyphen h) = [h]
101 -- | Parse a single hyphen and wrap it in our 'Hyphen' type.
103 hyphen :: Parser Hyphen
104 hyphen = fmap Hyphen (char '-')
107 -- * Letter, Digit, or Hyphen.
109 -- | A sum type representing a letter, digit, or hyphen.
112 LetDigHypLetDig LetDig |
113 LetDigHypHyphen Hyphen
116 instance Pretty LetDigHyp where
117 pretty_show (LetDigHypLetDig ld) = pretty_show ld
118 pretty_show (LetDigHypHyphen h) = pretty_show h
121 -- | The following is the simplest type in the domain grammar that
122 -- isn't already implemented for us.
124 -- <let-dig> ::= <letter> | <digit>
128 -- >>> import Text.Parsec ( parseTest )
130 -- Letters, digits, and hyphens are all parsed:
132 -- >>> parseTest let_dig_hyp "a"
133 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
135 -- >>> parseTest let_dig_hyp "7"
136 -- LetDigHypLetDig (LetDigDigit (Digit '7'))
138 -- >>> parseTest let_dig_hyp "-"
139 -- LetDigHypHyphen (Hyphen '-')
141 -- However, an underscore (for example) is not:
143 -- >>> parseTest let_dig_hyp "_"
144 -- parse error at (line 1, column 1):
146 -- expecting letter, digit or "-"
148 let_dig_hyp :: Parser LetDigHyp
150 parse_letdig <|> parse_hyphen
152 parse_letdig :: Parser LetDigHyp
153 parse_letdig = fmap LetDigHypLetDig let_dig
155 parse_hyphen :: Parser LetDigHyp
156 parse_hyphen = fmap LetDigHypHyphen hyphen
159 -- * Letter/Digit/Hyphen strings
161 -- | A string of letters, digits, and hyphens from the RFC1035 grammar:
163 -- <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
165 -- These are represented as either a single instance of a
166 -- 'LetDigHyp', or a string of them (recursive).
169 LdhStrSingleLdh LetDigHyp |
170 LdhStrMultipleLdh LetDigHyp LdhStr
173 instance Pretty LdhStr where
174 pretty_show (LdhStrSingleLdh ldh) = pretty_show ldh
175 pretty_show (LdhStrMultipleLdh ldh s) = (pretty_show ldh) ++ (pretty_show s)
177 -- | Parse a string of letters, digits, and hyphens (an 'LdhStr').
181 -- >>> import Text.Parsec ( parseTest )
183 -- Single letters, digits, and hyphens are parsed:
185 -- >>> parseTest ldh_str "a"
186 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a')))
188 -- >>> parseTest ldh_str "0"
189 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigDigit (Digit '0')))
191 -- >>> parseTest ldh_str "-"
192 -- LdhStrSingleLdh (LetDigHypHyphen (Hyphen '-'))
194 -- As well as strings of them:
196 -- >>> pretty_print $ parse ldh_str "" "a0-b"
199 ldh_str :: Parser LdhStr
200 ldh_str = try both <|> just_one
202 both :: Parser LdhStr
206 return $ LdhStrMultipleLdh ldh1 ldh_tail
208 just_one :: Parser LdhStr
209 just_one = fmap LdhStrSingleLdh let_dig_hyp
213 -- | A version of 'last' that works on a 'LdhStr' rather than a
214 -- list. That is, it returns the last 'LetDigHyp' in the
215 -- string. Since 'LdhStr' contains at least one character, there's
216 -- no \"nil\" case here.
220 -- >>> let (Right r) = parse ldh_str "" "a"
221 -- >>> last_ldh_str r
222 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
224 -- >>> let (Right r) = parse ldh_str "" "abc-def"
225 -- >>> last_ldh_str r
226 -- LetDigHypLetDig (LetDigLetter (Letter 'f'))
228 last_ldh_str :: LdhStr -> LetDigHyp
229 last_ldh_str (LdhStrSingleLdh x) = x
230 last_ldh_str (LdhStrMultipleLdh _ x) = last_ldh_str x
233 -- | A version of 'init' that works on a 'LdhStr' rather than a
234 -- list. That is, it returns everything /except/ the last character in
237 -- Since an 'LdhStr' must contain at least one character, this might
238 -- not be opssible (when the input is of length one). So, we return
243 -- >>> let (Right r) = parse ldh_str "" "a"
244 -- >>> init_ldh_str r
247 -- >>> let (Right r) = parse ldh_str "" "ab"
248 -- >>> init_ldh_str r
249 -- Just (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))))
251 -- >>> let (Right r) = parse ldh_str "" "abc-def"
252 -- >>> init_ldh_str r
253 -- Just (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))) (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'b'))) (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'c'))) (LdhStrMultipleLdh (LetDigHypHyphen (Hyphen '-')) (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'd'))) (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'e')))))))))
255 init_ldh_str :: LdhStr -> Maybe LdhStr
256 init_ldh_str (LdhStrSingleLdh _) = Nothing
257 init_ldh_str (LdhStrMultipleLdh h t) =
258 Just $ case (init_ldh_str t) of
259 -- We just got the second-to-last character, we're done.
260 Nothing -> LdhStrSingleLdh h
262 -- There's still more stuff. Recurse.
263 Just rest -> LdhStrMultipleLdh h rest
266 -- | Compute the length of an 'LdhStr'. It will be at least one, since
267 -- 'LdhStr's are non-empty. And if there's something other than the
268 -- first character present, we simply recurse.
272 -- >>> let (Right r) = parse ldh_str "" "a"
273 -- >>> length_ldh_str r
276 -- >>> let (Right r) = parse ldh_str "" "abc-def"
277 -- >>> length_ldh_str r
280 length_ldh_str :: LdhStr -> Int
281 length_ldh_str (LdhStrSingleLdh _) = 1
282 length_ldh_str (LdhStrMultipleLdh _ t) = 1 + (length_ldh_str t)
284 -- * Letter/Digit/Hyphen string followed by a trailing Letter/Digit
286 -- | This type isn't explicitly part of the grammar, but it's what
287 -- shows up in the square brackets of,
289 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
291 -- The ldh-str is optional, but if one is present, we must also have
292 -- a trailing let-dig to prevent the name from ending with a
293 -- hyphen. This can be represented with a @Maybe LdhStrLetDig@,
294 -- which is why we're about to define it.
296 data LdhStrLetDig = LdhStrLetDig (Maybe LdhStr) LetDig
299 instance Pretty LdhStrLetDig where
300 pretty_show (LdhStrLetDig Nothing ld) = pretty_show ld
301 pretty_show (LdhStrLetDig (Just s) ld) = (pretty_show s) ++ (pretty_show ld)
303 -- | Parse an 'LdhStrLetDig'. This isn't in the grammar, but we might
304 -- as well define the parser for it independently since we gave it
305 -- its own data type.
309 -- >>> import Text.Parsec ( parseTest )
311 -- Make sure we can parse a single character:
313 -- >>> parseTest ldh_str_let_dig "a"
314 -- LdhStrLetDig Nothing (LetDigLetter (Letter 'a'))
316 -- And longer strings:
318 -- >>> pretty_print $ parse ldh_str_let_dig "" "ab"
321 -- >>> pretty_print $ parse ldh_str_let_dig "" "-b"
324 -- >>> parseTest ldh_str_let_dig "b-"
325 -- parse error at (line 1, column 3):
326 -- label cannot end with a hyphen
328 ldh_str_let_dig :: Parser LdhStrLetDig
330 -- This will happily eat up the trailing let-dig...
333 -- So we have to go back and see what happened.
334 case (last_ldh_str full_ldh) of
335 (LetDigHypHyphen _) -> fail "label cannot end with a hyphen"
336 (LetDigHypLetDig ld) ->
337 -- Ok, the label didn't end with a hyphen; now we need to split
338 -- off the last letter/digit so we can pack it into our return
340 return $ case (init_ldh_str full_ldh) of
341 -- We only parsed one letter/digit. This can happen
342 -- if the label contains two characters. For example,
343 -- if we try to parse the label "ab", then the "a"
344 -- will be eaten by the label parser, and this
345 -- function will be left with only "b".
346 Nothing -> LdhStrLetDig Nothing ld
348 -- Usual case: there's was some leading let-dig-hyp junk,
350 leading_ldhs -> LdhStrLetDig leading_ldhs ld
354 -- | Compute the length of a 'LdhStrLetDig'. It's at least one, since
355 -- the let-dig at the end is always there. And when there's an
356 -- ldh-str too, we add its length to one.
360 -- >>> let (Right r) = parse ldh_str_let_dig "" "a"
361 -- >>> length_ldh_str_let_dig r
364 -- >>> let (Right r) = parse ldh_str_let_dig "" "abc-def"
365 -- >>> length_ldh_str_let_dig r
368 length_ldh_str_let_dig :: LdhStrLetDig -> Int
369 length_ldh_str_let_dig (LdhStrLetDig Nothing _) = 1
370 length_ldh_str_let_dig (LdhStrLetDig (Just ldhstring) _) =
371 1 + (length_ldh_str ldhstring)
376 -- | The label type from the RFC1035 grammar:
378 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
380 data Label = Label Letter (Maybe LdhStrLetDig)
383 instance Pretty Label where
384 pretty_show (Label l Nothing) = pretty_show l
385 pretty_show (Label l (Just s)) = (pretty_show l) ++ (pretty_show s)
387 -- | Parse a 'Label'.
389 -- In addition to the grammar, there's another restriction on
390 -- labels: their length must be 63 characters or less. Quoting
391 -- Section 2.3.1, \"Preferred name syntax\", of RFC1035:
393 -- The labels must follow the rules for ARPANET host names. They
394 -- must start with a letter, end with a letter or digit, and have
395 -- as interior characters only letters, digits, and hyphen. There
396 -- are also some restrictions on the length. Labels must be 63
397 -- characters or less.
399 -- We check this only after we have successfully parsed a label.
403 -- >>> import Text.Parsec ( parseTest )
405 -- Make sure we can parse a single character:
407 -- >>> parseTest label "a"
408 -- Label (Letter 'a') Nothing
410 -- And longer strings:
412 -- >>> pretty_print $ parse label "" "abc-def"
415 -- But not anything ending in a hyphen:
417 -- >>> parseTest label "abc-"
418 -- parse error at (line 1, column 5):
419 -- label cannot end with a hyphen
421 -- Or anything over 63 characters:
423 -- >>> parseTest label (['a'..'z'] ++ ['a'..'z'] ++ ['a'..'z'])
424 -- parse error at (line 1, column 79):
425 -- labels must be 63 or fewer characters
427 -- However, /exactly/ 63 characters is acceptable:
429 -- >>> pretty_print $ parse label "" (replicate 63 'x')
430 -- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
432 label :: Parser Label
434 l <- letter -- Guaranteed to be there
435 maybe_s <- optionMaybe ldh_str_let_dig -- Might not be there
437 -- It can only be one character long, from the letter...
438 Nothing -> return $ Label l maybe_s
440 -- The letter gives us one character, so we check that the rest is
441 -- less than 62 characters long. But in the error message we need
443 Just s -> if (length_ldh_str_let_dig s) <= 62
444 then return $ Label l maybe_s
445 else fail "labels must be 63 or fewer characters"
452 -- | The data type representing a \"subdomain\" from RFC1035:
454 -- <subdomain> ::= <label> | <subdomain> "." <label>
456 -- We have reversed the order of the subdomain and label in the
457 -- second option, however. This is explained in 'subdomain'.
460 SubdomainSingleLabel Label |
461 SubdomainMultipleLabel Label Subdomain
464 instance Pretty Subdomain where
465 pretty_show (SubdomainSingleLabel l) = pretty_show l
466 pretty_show (SubdomainMultipleLabel l s) =
467 (pretty_show l) ++ "." ++ (pretty_show s)
469 -- | Parse an RFC1035 \"subdomain\". The given grammar is,
471 -- <subdomain> ::= <label> | <subdomain> "." <label>
473 -- However, we have reversed the order of the subdomain and label to
474 -- prevent infinite recursion. The second option (subdomain + label)
475 -- is obviously more specific, we we need to try it first. This
476 -- presents a problem: we're trying to parse a subdomain in terms of
477 -- a subdomain! The given grammar represents subdomains how we like
478 -- to think of them; from right to left. But it's better to parse
479 -- from left to right, so we pick off the leading label and then
480 -- recurse into the definition of subdomain.
484 -- >>> import Text.Parsec ( parseTest )
486 -- Make sure we can parse a single character:
488 -- >>> parseTest subdomain "a"
489 -- SubdomainSingleLabel (Label (Letter 'a') Nothing)
491 -- >>> pretty_print $ parse subdomain "" "example.com"
494 -- >>> pretty_print $ parse subdomain "" "www.example.com"
497 subdomain :: Parser Subdomain
498 subdomain = try both <|> just_one
500 both :: Parser Subdomain
505 return (SubdomainMultipleLabel l s)
507 just_one :: Parser Subdomain
508 just_one = fmap SubdomainSingleLabel label
514 -- | An RFC1035 domain. According to RFC1035 a domain can be either a
515 -- subdomain or \" \", which according to RFC2181
516 -- <https://tools.ietf.org/html/rfc2181#section-11> means the root:
518 -- The zero length full name is defined as representing the root
519 -- of the DNS tree, and is typically written and displayed as
522 -- We let the 'Domain' type remain true to those RFCs, even though
523 -- they don't support an absolute domain name of e.g. a single dot.
524 -- We have one more data type 'UserDomain' which handles the possibility
525 -- of an absolute path.
528 DomainName Subdomain |
532 instance Pretty Domain where
533 pretty_show DomainRoot = ""
534 pretty_show (DomainName s) = pretty_show s
536 -- | Parse an RFC1035 \"domain\"
540 -- >>> import Text.Parsec ( parseTest )
542 -- Make sure we can parse a single character:
544 -- >>> parseTest domain "a"
545 -- DomainName (SubdomainSingleLabel (Label (Letter 'a') Nothing))
547 -- And the empty domain:
549 -- >>> parseTest domain ""
552 -- We will in fact parse the \"empty\" domain off the front of
553 -- pretty much anything:
555 -- >>> parseTest domain "8===D"
558 -- Equality of domains is case-insensitive:
560 -- >>> let (Right r1) = parse domain "" "example.com"
561 -- >>> let (Right r2) = parse domain "" "ExaMPle.coM"
565 -- A single dot IS parsed as the root, but the dot isn't consumed:
567 -- >>> parseTest domain "."
570 domain :: Parser Domain
571 domain = try parse_subdomain <|> parse_empty
573 parse_subdomain = fmap DomainName subdomain
574 parse_empty = string "" >> return DomainRoot
580 -- | This type helps clarify some murkiness in the DNS \"domain\" name
581 -- specification. In RFC1034, it is acknowledged that a domain name
582 -- input with a trailing \".\" will represent an absolute domain
583 -- name (i.e. with respect to the DNS root). However, the grammar in
584 -- RFC1035 disallows a trailing dot.
586 -- This makes some sense: within the DNS, everything knows its
587 -- position in the tree. The relative/absolute distinction only
588 -- makes sense on the client side, where a user's resolver might
589 -- decide to append some suffix to a relative
590 -- request. Unfortunately, that's where we live. So we have to deal
591 -- with the possibility of having a trailing dot at the end of any
595 UserDomainRelative Domain |
596 UserDomainAbsolute Domain
599 instance Pretty UserDomain where
600 pretty_show (UserDomainRelative d) = pretty_show d
601 pretty_show (UserDomainAbsolute d) = (pretty_show d) ++ "."
604 -- | Parse a 'UserDomain'. This is what we'll be using to read user
605 -- input, since it supports both relative and absolute domain names
606 -- (unlike the implicitly-absolute 'Domain').
610 -- >>> import Text.Parsec ( parseTest )
612 -- We can really parse the root now!
614 -- >>> parseTest user_domain "."
615 -- UserDomainAbsolute DomainRoot
617 -- But multiple dots aren't (only the first):
619 -- >>> pretty_print $ parse user_domain "" ".."
622 -- We can also optionally have a trailing dot at the end of a
625 -- >>> pretty_print $ parse user_domain "" "www.example.com"
628 -- >>> pretty_print $ parse user_domain "" "www.example.com."
631 -- A \"relative root\" can also be parsed, letting the user's
632 -- resolver deal with it:
634 -- >>> parseTest user_domain ""
635 -- UserDomainRelative DomainRoot
637 user_domain :: Parser UserDomain
638 user_domain = try absolute <|> relative
640 absolute :: Parser UserDomain
644 return $ UserDomainAbsolute d
646 relative :: Parser UserDomain
647 relative = fmap UserDomainRelative domain