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>
8 -- We basically work with strings and characters everywhere, even
9 -- though this isn't really correct. The length specifications in
10 -- the RFCs are all in terms of octets, so really a ByteString.Char8
11 -- would be more appropriate. With strings, for example, we could
12 -- have a unicode mumbo jumbo character that takes up two bytes
20 import Data.Char ( toLower )
34 import qualified Text.Parsec as Parsec ( digit, letter)
35 import Text.Parsec.String ( Parser )
37 import Pretty ( Pretty(..) )
41 -- | A wrapper around a digit character.
43 newtype Digit = Digit Char deriving (Eq, Show)
44 instance Pretty Digit where pretty_show (Digit d) = [d]
46 -- | Parse a single digit, but wrap it in our 'Digit' type.
49 digit = fmap Digit Parsec.digit
54 -- | A wrapper around a letter character.
56 newtype Letter = Letter Char deriving (Show)
57 instance Pretty Letter where pretty_show (Letter l) = [l]
60 -- | Parse a single letter, but wrap it in our 'Letter' type.
62 letter :: Parser Letter
63 letter = fmap Letter Parsec.letter
65 -- | The derived instance of 'Eq' for letters is incorrect. All
66 -- comparisons should be made case-insensitively. The following
67 -- is an excerpt from RFC1035:
69 -- 2.3.3. Character Case
71 -- For all parts of the DNS that are part of the official
72 -- protocol, all comparisons between character strings (e.g.,
73 -- labels, domain names, etc.) are done in a case-insensitive
76 -- Since each part of DNS name is composed of our custom types, it
77 -- suffices to munge the equality for 'Letter'. RFC4343
78 -- <https://tools.ietf.org/html/rfc4343> clarifies the
79 -- case-insensitivity rules, but the fact that we're treating DNS
80 -- names as strings makes most of those problems go away (in
81 -- exchange for new ones).
83 instance Eq Letter where
84 (Letter l1) == (Letter l2) = (toLower l1) == (toLower l2)
88 -- | A sum type representing either a letter or a digit.
95 instance Pretty LetDig where
96 pretty_show (LetDigLetter l) = pretty_show l
97 pretty_show (LetDigDigit d) = pretty_show d
99 -- | Parse a letter or a digit and wrap it in our 'LetDig' type.
101 let_dig :: Parser LetDig
102 let_dig = (fmap LetDigLetter letter) <|> (fmap LetDigDigit digit)
107 -- | A wrapper around a single hyphen character.
109 newtype Hyphen = Hyphen Char deriving (Eq, Show)
110 instance Pretty Hyphen where pretty_show (Hyphen h) = [h]
112 -- | Parse a single hyphen and wrap it in our 'Hyphen' type.
114 hyphen :: Parser Hyphen
115 hyphen = fmap Hyphen (char '-')
118 -- * Letter, Digit, or Hyphen.
120 -- | A sum type representing a letter, digit, or hyphen.
123 LetDigHypLetDig LetDig |
124 LetDigHypHyphen Hyphen
127 instance Pretty LetDigHyp where
128 pretty_show (LetDigHypLetDig ld) = pretty_show ld
129 pretty_show (LetDigHypHyphen h) = pretty_show h
132 -- | The following is the simplest type in the domain grammar that
133 -- isn't already implemented for us.
135 -- <let-dig> ::= <letter> | <digit>
139 -- >>> import Text.Parsec ( parseTest )
141 -- Letters, digits, and hyphens are all parsed:
143 -- >>> parseTest let_dig_hyp "a"
144 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
146 -- >>> parseTest let_dig_hyp "7"
147 -- LetDigHypLetDig (LetDigDigit (Digit '7'))
149 -- >>> parseTest let_dig_hyp "-"
150 -- LetDigHypHyphen (Hyphen '-')
152 -- However, an underscore (for example) is not:
154 -- >>> parseTest let_dig_hyp "_"
155 -- parse error at (line 1, column 1):
157 -- expecting letter, digit or "-"
159 let_dig_hyp :: Parser LetDigHyp
161 parse_letdig <|> parse_hyphen
163 parse_letdig :: Parser LetDigHyp
164 parse_letdig = fmap LetDigHypLetDig let_dig
166 parse_hyphen :: Parser LetDigHyp
167 parse_hyphen = fmap LetDigHypHyphen hyphen
170 -- * Letter/Digit/Hyphen strings
172 -- | A string of letters, digits, and hyphens from the RFC1035 grammar:
174 -- <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
176 -- These are represented as either a single instance of a
177 -- 'LetDigHyp', or a string of them (recursive).
180 LdhStrSingleLdh LetDigHyp |
181 LdhStrMultipleLdh LetDigHyp LdhStr
184 instance Pretty LdhStr where
185 pretty_show (LdhStrSingleLdh ldh) = pretty_show ldh
186 pretty_show (LdhStrMultipleLdh ldh s) = (pretty_show ldh) ++ (pretty_show s)
188 -- | Parse a string of letters, digits, and hyphens (an 'LdhStr').
192 -- >>> import Text.Parsec ( parseTest )
194 -- Single letters, digits, and hyphens are parsed:
196 -- >>> parseTest ldh_str "a"
197 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a')))
199 -- >>> parseTest ldh_str "0"
200 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigDigit (Digit '0')))
202 -- >>> parseTest ldh_str "-"
203 -- LdhStrSingleLdh (LetDigHypHyphen (Hyphen '-'))
205 -- As well as strings of them:
207 -- >>> pretty_print $ parse ldh_str "" "a0-b"
210 ldh_str :: Parser LdhStr
211 ldh_str = try both <|> just_one
213 both :: Parser LdhStr
217 return $ LdhStrMultipleLdh ldh1 ldh_tail
219 just_one :: Parser LdhStr
220 just_one = fmap LdhStrSingleLdh let_dig_hyp
224 -- | A version of 'last' that works on a 'LdhStr' rather than a
225 -- list. That is, it returns the last 'LetDigHyp' in the
226 -- string. Since 'LdhStr' contains at least one character, there's
227 -- no \"nil\" case here.
231 -- >>> let (Right r) = parse ldh_str "" "a"
232 -- >>> last_ldh_str r
233 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
235 -- >>> let (Right r) = parse ldh_str "" "abc-def"
236 -- >>> last_ldh_str r
237 -- LetDigHypLetDig (LetDigLetter (Letter 'f'))
239 last_ldh_str :: LdhStr -> LetDigHyp
240 last_ldh_str (LdhStrSingleLdh x) = x
241 last_ldh_str (LdhStrMultipleLdh _ x) = last_ldh_str x
244 -- | A version of 'init' that works on a 'LdhStr' rather than a
245 -- list. That is, it returns everything /except/ the last character in
248 -- Since an 'LdhStr' must contain at least one character, this might
249 -- not be opssible (when the input is of length one). So, we return
254 -- >>> let (Right r) = parse ldh_str "" "a"
255 -- >>> init_ldh_str r
258 -- >>> let (Right r) = parse ldh_str "" "ab"
259 -- >>> init_ldh_str r
260 -- Just (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))))
262 -- >>> let (Right r) = parse ldh_str "" "abc-def"
263 -- >>> init_ldh_str r
264 -- 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')))))))))
266 init_ldh_str :: LdhStr -> Maybe LdhStr
267 init_ldh_str (LdhStrSingleLdh _) = Nothing
268 init_ldh_str (LdhStrMultipleLdh h t) =
269 Just $ case (init_ldh_str t) of
270 -- We just got the second-to-last character, we're done.
271 Nothing -> LdhStrSingleLdh h
273 -- There's still more stuff. Recurse.
274 Just rest -> LdhStrMultipleLdh h rest
277 -- | Compute the length of an 'LdhStr'. It will be at least one, since
278 -- 'LdhStr's are non-empty. And if there's something other than the
279 -- first character present, we simply recurse.
283 -- >>> let (Right r) = parse ldh_str "" "a"
284 -- >>> length_ldh_str r
287 -- >>> let (Right r) = parse ldh_str "" "abc-def"
288 -- >>> length_ldh_str r
291 length_ldh_str :: LdhStr -> Int
292 length_ldh_str (LdhStrSingleLdh _) = 1
293 length_ldh_str (LdhStrMultipleLdh _ t) = 1 + (length_ldh_str t)
295 -- * Letter/Digit/Hyphen string followed by a trailing Letter/Digit
297 -- | This type isn't explicitly part of the grammar, but it's what
298 -- shows up in the square brackets of,
300 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
302 -- The ldh-str is optional, but if one is present, we must also have
303 -- a trailing let-dig to prevent the name from ending with a
304 -- hyphen. This can be represented with a @Maybe LdhStrLetDig@,
305 -- which is why we're about to define it.
307 data LdhStrLetDig = LdhStrLetDig (Maybe LdhStr) LetDig
310 instance Pretty LdhStrLetDig where
311 pretty_show (LdhStrLetDig Nothing ld) = pretty_show ld
312 pretty_show (LdhStrLetDig (Just s) ld) = (pretty_show s) ++ (pretty_show ld)
314 -- | Parse an 'LdhStrLetDig'. This isn't in the grammar, but we might
315 -- as well define the parser for it independently since we gave it
316 -- its own data type.
320 -- >>> import Text.Parsec ( parseTest )
322 -- Make sure we can parse a single character:
324 -- >>> parseTest ldh_str_let_dig "a"
325 -- LdhStrLetDig Nothing (LetDigLetter (Letter 'a'))
327 -- And longer strings:
329 -- >>> pretty_print $ parse ldh_str_let_dig "" "ab"
332 -- >>> pretty_print $ parse ldh_str_let_dig "" "-b"
335 -- >>> parseTest ldh_str_let_dig "b-"
336 -- parse error at (line 1, column 3):
337 -- label cannot end with a hyphen
339 ldh_str_let_dig :: Parser LdhStrLetDig
341 -- This will happily eat up the trailing let-dig...
344 -- So we have to go back and see what happened.
345 case (last_ldh_str full_ldh) of
346 (LetDigHypHyphen _) -> fail "label cannot end with a hyphen"
347 (LetDigHypLetDig ld) ->
348 -- Ok, the label didn't end with a hyphen; now we need to split
349 -- off the last letter/digit so we can pack it into our return
351 return $ case (init_ldh_str full_ldh) of
352 -- We only parsed one letter/digit. This can happen
353 -- if the label contains two characters. For example,
354 -- if we try to parse the label "ab", then the "a"
355 -- will be eaten by the label parser, and this
356 -- function will be left with only "b".
357 Nothing -> LdhStrLetDig Nothing ld
359 -- Usual case: there's was some leading let-dig-hyp junk,
361 leading_ldhs -> LdhStrLetDig leading_ldhs ld
365 -- | Compute the length of a 'LdhStrLetDig'. It's at least one, since
366 -- the let-dig at the end is always there. And when there's an
367 -- ldh-str too, we add its length to one.
371 -- >>> let (Right r) = parse ldh_str_let_dig "" "a"
372 -- >>> length_ldh_str_let_dig r
375 -- >>> let (Right r) = parse ldh_str_let_dig "" "abc-def"
376 -- >>> length_ldh_str_let_dig r
379 length_ldh_str_let_dig :: LdhStrLetDig -> Int
380 length_ldh_str_let_dig (LdhStrLetDig Nothing _) = 1
381 length_ldh_str_let_dig (LdhStrLetDig (Just ldhstring) _) =
382 1 + (length_ldh_str ldhstring)
387 -- | The label type from the RFC1035 grammar:
389 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
391 -- We allow the slightly more general syntax from RFC1123, Section 2.1:
393 -- The syntax of a legal Internet host name was specified in RFC-952
394 -- [DNS:4]. One aspect of host name syntax is hereby changed: the
395 -- restriction on the first character is relaxed to allow either a
396 -- letter or a digit. Host software MUST support this more liberal
399 data Label = Label Letter (Maybe LdhStrLetDig)
402 instance Pretty Label where
403 pretty_show (Label l Nothing) = pretty_show l
404 pretty_show (Label l (Just s)) = (pretty_show l) ++ (pretty_show s)
406 -- | Parse a 'Label'.
408 -- In addition to the grammar, there's another restriction on
409 -- labels: their length must be 63 characters or less. Quoting
410 -- Section 2.3.1, \"Preferred name syntax\", of RFC1035:
412 -- The labels must follow the rules for ARPANET host names. They
413 -- must start with a letter, end with a letter or digit, and have
414 -- as interior characters only letters, digits, and hyphen. There
415 -- are also some restrictions on the length. Labels must be 63
416 -- characters or less.
418 -- We check this only after we have successfully parsed a label.
422 -- >>> import Text.Parsec ( parseTest )
424 -- Make sure we can parse a single character:
426 -- >>> parseTest label "a"
427 -- Label (Letter 'a') Nothing
429 -- And longer strings:
431 -- >>> pretty_print $ parse label "" "abc-def"
434 -- But not anything ending in a hyphen:
436 -- >>> parseTest label "abc-"
437 -- parse error at (line 1, column 5):
438 -- label cannot end with a hyphen
440 -- Or anything over 63 characters:
442 -- >>> parseTest label (['a'..'z'] ++ ['a'..'z'] ++ ['a'..'z'])
443 -- parse error at (line 1, column 79):
444 -- labels must be 63 or fewer characters
446 -- However, /exactly/ 63 characters is acceptable:
448 -- >>> pretty_print $ parse label "" (replicate 63 'x')
449 -- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
451 label :: Parser Label
453 l <- letter -- Guaranteed to be there
454 maybe_s <- optionMaybe ldh_str_let_dig -- Might not be there
456 -- It can only be one character long, from the letter...
457 Nothing -> return $ Label l maybe_s
459 -- The letter gives us one character, so we check that the rest is
460 -- less than 62 characters long. But in the error message we need
462 Just s -> if (length_ldh_str_let_dig s) <= 62
463 then return $ Label l maybe_s
464 else fail "labels must be 63 or fewer characters"
471 -- | The data type representing a \"subdomain\" from RFC1035:
473 -- <subdomain> ::= <label> | <subdomain> "." <label>
475 -- We have reversed the order of the subdomain and label in the
476 -- second option, however. This is explained in 'subdomain'.
479 SubdomainSingleLabel Label |
480 SubdomainMultipleLabel Label Subdomain
485 instance Pretty Subdomain where
486 pretty_show (SubdomainSingleLabel l) = pretty_show l
487 pretty_show (SubdomainMultipleLabel l s) =
488 (pretty_show l) ++ "." ++ (pretty_show s)
490 -- | Parse an RFC1035 \"subdomain\". The given grammar is,
492 -- <subdomain> ::= <label> | <subdomain> "." <label>
494 -- However, we have reversed the order of the subdomain and label to
495 -- prevent infinite recursion. The second option (subdomain + label)
496 -- is obviously more specific, we we need to try it first. This
497 -- presents a problem: we're trying to parse a subdomain in terms of
498 -- a subdomain! The given grammar represents subdomains how we like
499 -- to think of them; from right to left. But it's better to parse
500 -- from left to right, so we pick off the leading label and then
501 -- recurse into the definition of subdomain.
503 -- According to RFC1034, Section 3.1, two neighboring labels in a
504 -- DNS name cannot be equal:
506 -- Each node has a label, which is zero to 63 octets in length. Brother
507 -- nodes may not have the same label, although the same label can be used
508 -- for nodes which are not brothers. One label is reserved, and that is
509 -- the null (i.e., zero length) label used for the root.
511 -- We enforce this restriction, but the result is usually that we
512 -- only parse the part of the subdomain leading up to the repeated
517 -- >>> import Text.Parsec ( parseTest )
519 -- Make sure we can parse a single character:
521 -- >>> parseTest subdomain "a"
522 -- SubdomainSingleLabel (Label (Letter 'a') Nothing)
524 -- >>> pretty_print $ parse subdomain "" "example.com"
527 -- >>> pretty_print $ parse subdomain "" "www.example.com"
530 -- We reject a subdomain with equal neighbors, but this leads to
531 -- only the single first label being parsed instead:
533 -- >>> pretty_print $ parse subdomain "" "www.www.example.com"
536 -- But not one with a repeated but non-neighboring label:
538 -- >>> pretty_print $ parse subdomain "" "www.example.www.com"
539 -- www.example.www.com
541 subdomain :: Parser Subdomain
542 subdomain = try both <|> just_one
544 both :: Parser Subdomain
549 let result = SubdomainMultipleLabel l s
550 if (subdomain_has_equal_neighbors result)
551 then fail "subdomain cannot have equal neighboring labels"
554 just_one :: Parser Subdomain
555 just_one = fmap SubdomainSingleLabel label
559 -- | Retrieve a list of labels contained in a 'Subdomain'.
563 -- >>> let (Right r) = parse subdomain "" "a"
564 -- >>> pretty_print $ subdomain_labels r
567 -- >>> let (Right r) = parse subdomain "" "example.com"
568 -- >>> pretty_print $ subdomain_labels r
571 -- >>> let (Right r) = parse subdomain "" "www.example.com"
572 -- >>> pretty_print $ subdomain_labels r
573 -- ["www","example","com"]
575 subdomain_labels :: Subdomain -> [Label]
576 subdomain_labels (SubdomainSingleLabel l) = [l]
577 subdomain_labels (SubdomainMultipleLabel l s) = l : (subdomain_labels s)
580 -- | Return a list of pairs of neighboring labels in a subdomain.
584 -- >>> let (Right r) = parse subdomain "" "www.example.com"
585 -- >>> pretty_print $ subdomain_label_neighbors r
586 -- ["(\"www\",\"example\")","(\"example\",\"com\")"]
588 subdomain_label_neighbors :: Subdomain -> [(Label,Label)]
589 subdomain_label_neighbors s =
592 ls = subdomain_labels s
595 -- | Return @True@ if the subdomain has any two equal neighboring
596 -- labels, and @False@ otherwise.
600 -- >>> let (Right r) = parse subdomain "" "www.example.com"
601 -- >>> subdomain_has_equal_neighbors r
604 -- >>> let (Right l) = parse label "" "www"
605 -- >>> let (Right s) = parse subdomain "" "www.example.com"
606 -- >>> let bad_subdomain = SubdomainMultipleLabel l s
607 -- >>> subdomain_has_equal_neighbors bad_subdomain
610 subdomain_has_equal_neighbors :: Subdomain -> Bool
611 subdomain_has_equal_neighbors s =
612 or [ x == y | (x,y) <- subdomain_label_neighbors s ]
618 -- | An RFC1035 domain. According to RFC1035 a domain can be either a
619 -- subdomain or \" \", which according to RFC2181
620 -- <https://tools.ietf.org/html/rfc2181#section-11> means the root:
622 -- The zero length full name is defined as representing the root
623 -- of the DNS tree, and is typically written and displayed as
626 -- We let the 'Domain' type remain true to those RFCs, even though
627 -- they don't support an absolute domain name of e.g. a single dot.
628 -- We have one more data type 'UserDomain' which handles the possibility
629 -- of an absolute path.
632 DomainName Subdomain |
636 instance Pretty Domain where
637 pretty_show DomainRoot = ""
638 pretty_show (DomainName s) = pretty_show s
640 -- | Parse an RFC1035 \"domain\"
644 -- >>> import Text.Parsec ( parseTest )
646 -- Make sure we can parse a single character:
648 -- >>> parseTest domain "a"
649 -- DomainName (SubdomainSingleLabel (Label (Letter 'a') Nothing))
651 -- And the empty domain:
653 -- >>> parseTest domain ""
656 -- We will in fact parse the \"empty\" domain off the front of
657 -- pretty much anything:
659 -- >>> parseTest domain "8===D"
662 -- Equality of domains is case-insensitive:
664 -- >>> let (Right r1) = parse domain "" "example.com"
665 -- >>> let (Right r2) = parse domain "" "ExaMPle.coM"
669 -- A single dot IS parsed as the root, but the dot isn't consumed:
671 -- >>> parseTest domain "."
674 -- Anything over 255 characters is an error, so the root will be
677 -- >>> let big_l1 = replicate 63 'x'
678 -- >>> let big_l2 = replicate 63 'y' -- Avoid equal neighboring labels!
679 -- >>> let big_labels = big_l1 ++ "." ++ big_l2 ++ "."
680 -- >>> let big_subdomain = concat $ replicate 3 big_labels
681 -- >>> parseTest domain big_subdomain
684 -- But exactly 255 is allowed:
686 -- >>> import Data.List ( intercalate )
687 -- >>> let l1 = replicate 63 'w'
688 -- >>> let l2 = replicate 63 'x'
689 -- >>> let l3 = replicate 63 'y'
690 -- >>> let l4 = replicate 63 'z'
691 -- >>> let big_subdomain = intercalate "." [l1,l2,l3,l4]
692 -- >>> let (Right r) = parse domain "" big_subdomain
693 -- >>> length (pretty_show r)
696 domain :: Parser Domain
697 domain = try parse_subdomain <|> parse_empty
699 parse_subdomain :: Parser Domain
702 if (length $ pretty_show s) <= 255
703 then return $ DomainName s
704 else fail "subdomains can be at most 255 characters"
706 parse_empty :: Parser Domain
707 parse_empty = string "" >> return DomainRoot
713 -- | This type helps clarify some murkiness in the DNS \"domain\" name
714 -- specification. In RFC1034, it is acknowledged that a domain name
715 -- input with a trailing \".\" will represent an absolute domain
716 -- name (i.e. with respect to the DNS root). However, the grammar in
717 -- RFC1035 disallows a trailing dot.
719 -- This makes some sense: within the DNS, everything knows its
720 -- position in the tree. The relative/absolute distinction only
721 -- makes sense on the client side, where a user's resolver might
722 -- decide to append some suffix to a relative
723 -- request. Unfortunately, that's where we live. So we have to deal
724 -- with the possibility of having a trailing dot at the end of any
728 UserDomainRelative Domain |
729 UserDomainAbsolute Domain
732 instance Pretty UserDomain where
733 pretty_show (UserDomainRelative d) = pretty_show d
734 pretty_show (UserDomainAbsolute d) = (pretty_show d) ++ "."
737 -- | Parse a 'UserDomain'. This is what we'll be using to read user
738 -- input, since it supports both relative and absolute domain names
739 -- (unlike the implicitly-absolute 'Domain').
743 -- >>> import Text.Parsec ( parseTest )
745 -- We can really parse the root now!
747 -- >>> parseTest user_domain "."
748 -- UserDomainAbsolute DomainRoot
750 -- But multiple dots aren't (only the first):
752 -- >>> pretty_print $ parse user_domain "" ".."
755 -- We can also optionally have a trailing dot at the end of a
758 -- >>> pretty_print $ parse user_domain "" "www.example.com"
761 -- >>> pretty_print $ parse user_domain "" "www.example.com."
764 -- A \"relative root\" can also be parsed, letting the user's
765 -- resolver deal with it:
767 -- >>> parseTest user_domain ""
768 -- UserDomainRelative DomainRoot
770 user_domain :: Parser UserDomain
771 user_domain = try absolute <|> relative
773 absolute :: Parser UserDomain
777 return $ UserDomainAbsolute d
779 relative :: Parser UserDomain
780 relative = fmap UserDomainRelative domain