1 {-# LANGUAGE DoAndIfThenElse #-}
3 -- | The 'Domain' data type and its parser. A 'Domain' represents a
4 -- name in the domain name system (DNS) as described by
5 -- RFC1035. In particular, we enforce the restrictions from Section
6 -- 2.3.1 \"Preferred name syntax\". See for example,
8 -- <https://tools.ietf.org/html/rfc1035#section-2.3.1>
10 -- We basically work with strings and characters everywhere, even
11 -- though this isn't really correct. The length specifications in
12 -- the RFCs are all in terms of octets, so really a ByteString.Char8
13 -- would be more appropriate. With strings, for example, we could
14 -- have a unicode mumbo jumbo character that takes up two bytes
17 module Network.DNS.RBL.Domain (
22 import Data.Char ( toLower )
29 import qualified Text.Parsec as Parsec ( digit, letter)
30 import Text.Parsec.String ( Parser )
32 import Network.DNS.RBL.Pretty ( Pretty(..) )
33 import Network.DNS.RBL.Reversible ( Reversible(..) )
37 -- | A wrapper around a digit character.
39 newtype Digit = Digit Char deriving (Eq, Show)
40 instance Pretty Digit where pretty_show (Digit d) = [d]
42 -- | Parse a single digit, but wrap it in our 'Digit' type.
45 digit = fmap Digit Parsec.digit
50 -- | A wrapper around a letter character.
52 newtype Letter = Letter Char deriving (Show)
53 instance Pretty Letter where pretty_show (Letter l) = [l]
56 -- | Parse a single letter, but wrap it in our 'Letter' type.
58 letter :: Parser Letter
59 letter = fmap Letter Parsec.letter
61 -- | The derived instance of 'Eq' for letters is incorrect. All
62 -- comparisons should be made case-insensitively. The following
63 -- is an excerpt from RFC1035:
65 -- 2.3.3. Character Case
67 -- For all parts of the DNS that are part of the official
68 -- protocol, all comparisons between character strings (e.g.,
69 -- labels, domain names, etc.) are done in a case-insensitive
72 -- Since each part of DNS name is composed of our custom types, it
73 -- suffices to munge the equality for 'Letter'. RFC4343
74 -- <https://tools.ietf.org/html/rfc4343> clarifies the
75 -- case-insensitivity rules, but the fact that we're treating DNS
76 -- names as strings makes most of those problems go away (in
77 -- exchange for new ones).
79 instance Eq Letter where
80 (Letter l1) == (Letter l2) = (toLower l1) == (toLower l2)
84 -- | A sum type representing either a letter or a digit.
91 instance Pretty LetDig where
92 pretty_show (LetDigLetter l) = pretty_show l
93 pretty_show (LetDigDigit d) = pretty_show d
95 -- | Parse a letter or a digit and wrap it in our 'LetDig' type.
97 let_dig :: Parser LetDig
98 let_dig = (fmap LetDigLetter letter) <|> (fmap LetDigDigit digit)
103 -- | A wrapper around a single hyphen character.
105 newtype Hyphen = Hyphen Char deriving (Eq, Show)
106 instance Pretty Hyphen where pretty_show (Hyphen h) = [h]
108 -- | Parse a single hyphen and wrap it in our 'Hyphen' type.
110 hyphen :: Parser Hyphen
111 hyphen = fmap Hyphen (char '-')
114 -- * Letter, Digit, or Hyphen.
116 -- | A sum type representing a letter, digit, or hyphen.
119 LetDigHypLetDig LetDig |
120 LetDigHypHyphen Hyphen
123 instance Pretty LetDigHyp where
124 pretty_show (LetDigHypLetDig ld) = pretty_show ld
125 pretty_show (LetDigHypHyphen h) = pretty_show h
128 -- | The following is the simplest type in the domain grammar that
129 -- isn't already implemented for us.
131 -- <let-dig> ::= <letter> | <digit>
135 -- >>> import Text.Parsec ( parseTest )
137 -- Letters, digits, and hyphens are all parsed:
139 -- >>> parseTest let_dig_hyp "a"
140 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
142 -- >>> parseTest let_dig_hyp "7"
143 -- LetDigHypLetDig (LetDigDigit (Digit '7'))
145 -- >>> parseTest let_dig_hyp "-"
146 -- LetDigHypHyphen (Hyphen '-')
148 -- However, an underscore (for example) is not:
150 -- >>> parseTest let_dig_hyp "_"
151 -- parse error at (line 1, column 1):
153 -- expecting letter, digit or "-"
155 let_dig_hyp :: Parser LetDigHyp
157 parse_letdig <|> parse_hyphen
159 parse_letdig :: Parser LetDigHyp
160 parse_letdig = fmap LetDigHypLetDig let_dig
162 parse_hyphen :: Parser LetDigHyp
163 parse_hyphen = fmap LetDigHypHyphen hyphen
166 -- * Letter/Digit/Hyphen strings
168 -- | A string of letters, digits, and hyphens from the RFC1035 grammar:
170 -- <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
172 -- These are represented as either a single instance of a
173 -- 'LetDigHyp', or a string of them (recursive).
176 LdhStrSingleLdh LetDigHyp |
177 LdhStrMultipleLdh LetDigHyp LdhStr
180 instance Pretty LdhStr where
181 pretty_show (LdhStrSingleLdh ldh) = pretty_show ldh
182 pretty_show (LdhStrMultipleLdh ldh s) = (pretty_show ldh) ++ (pretty_show s)
184 -- | Parse a string of letters, digits, and hyphens (an 'LdhStr').
188 -- >>> import Text.Parsec ( parseTest )
190 -- Single letters, digits, and hyphens are parsed:
192 -- >>> parseTest ldh_str "a"
193 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a')))
195 -- >>> parseTest ldh_str "0"
196 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigDigit (Digit '0')))
198 -- >>> parseTest ldh_str "-"
199 -- LdhStrSingleLdh (LetDigHypHyphen (Hyphen '-'))
201 -- As well as strings of them:
203 -- >>> import Text.Parsec ( parse )
204 -- >>> pretty_print $ parse ldh_str "" "a0-b"
207 ldh_str :: Parser LdhStr
208 ldh_str = try both <|> just_one
210 both :: Parser LdhStr
214 return $ LdhStrMultipleLdh ldh1 ldh_tail
216 just_one :: Parser LdhStr
217 just_one = fmap LdhStrSingleLdh let_dig_hyp
221 -- | A version of 'last' that works on a 'LdhStr' rather than a
222 -- list. That is, it returns the last 'LetDigHyp' in the
223 -- string. Since 'LdhStr' contains at least one character, there's
224 -- no \"nil\" case here.
228 -- >>> import Text.Parsec ( parse )
230 -- >>> let (Right r) = parse ldh_str "" "a"
231 -- >>> last_ldh_str r
232 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
234 -- >>> let (Right r) = parse ldh_str "" "abc-def"
235 -- >>> last_ldh_str r
236 -- LetDigHypLetDig (LetDigLetter (Letter 'f'))
238 last_ldh_str :: LdhStr -> LetDigHyp
239 last_ldh_str (LdhStrSingleLdh x) = x
240 last_ldh_str (LdhStrMultipleLdh _ x) = last_ldh_str x
243 -- | A version of 'init' that works on a 'LdhStr' rather than a
244 -- list. That is, it returns everything /except/ the last character in
247 -- Since an 'LdhStr' must contain at least one character, this might
248 -- not be opssible (when the input is of length one). So, we return
253 -- >>> import Text.Parsec ( parse )
255 -- >>> let (Right r) = parse ldh_str "" "a"
256 -- >>> init_ldh_str r
259 -- >>> let (Right r) = parse ldh_str "" "ab"
260 -- >>> init_ldh_str r
261 -- Just (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))))
263 -- >>> let (Right r) = parse ldh_str "" "abc-def"
264 -- >>> init_ldh_str r
265 -- 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')))))))))
267 init_ldh_str :: LdhStr -> Maybe LdhStr
268 init_ldh_str (LdhStrSingleLdh _) = Nothing
269 init_ldh_str (LdhStrMultipleLdh h t) =
270 Just $ case (init_ldh_str t) of
271 -- We just got the second-to-last character, we're done.
272 Nothing -> LdhStrSingleLdh h
274 -- There's still more stuff. Recurse.
275 Just rest -> LdhStrMultipleLdh h rest
278 -- | Compute the length of an 'LdhStr'. It will be at least one, since
279 -- 'LdhStr's are non-empty. And if there's something other than the
280 -- first character present, we simply recurse.
284 -- >>> import Text.Parsec ( parse )
286 -- >>> let (Right r) = parse ldh_str "" "a"
287 -- >>> length_ldh_str r
290 -- >>> let (Right r) = parse ldh_str "" "abc-def"
291 -- >>> length_ldh_str r
294 length_ldh_str :: LdhStr -> Int
295 length_ldh_str (LdhStrSingleLdh _) = 1
296 length_ldh_str (LdhStrMultipleLdh _ t) = 1 + (length_ldh_str t)
298 -- * Letter/Digit/Hyphen string followed by a trailing Letter/Digit
300 -- | This type isn't explicitly part of the grammar, but it's what
301 -- shows up in the square brackets of,
303 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
305 -- The ldh-str is optional, but if one is present, we must also have
306 -- a trailing let-dig to prevent the name from ending with a
307 -- hyphen. This can be represented with a @Maybe LdhStrLetDig@,
308 -- which is why we're about to define it.
310 data LdhStrLetDig = LdhStrLetDig (Maybe LdhStr) LetDig
313 instance Pretty LdhStrLetDig where
314 pretty_show (LdhStrLetDig Nothing ld) = pretty_show ld
315 pretty_show (LdhStrLetDig (Just s) ld) = (pretty_show s) ++ (pretty_show ld)
317 -- | Parse an 'LdhStrLetDig'. This isn't in the grammar, but we might
318 -- as well define the parser for it independently since we gave it
319 -- its own data type.
323 -- >>> import Text.Parsec ( parse, parseTest )
325 -- Make sure we can parse a single character:
327 -- >>> parseTest ldh_str_let_dig "a"
328 -- LdhStrLetDig Nothing (LetDigLetter (Letter 'a'))
330 -- And longer strings:
332 -- >>> pretty_print $ parse ldh_str_let_dig "" "ab"
335 -- >>> pretty_print $ parse ldh_str_let_dig "" "-b"
338 -- >>> parseTest ldh_str_let_dig "b-"
339 -- parse error at (line 1, column 3):
340 -- label cannot end with a hyphen
342 ldh_str_let_dig :: Parser LdhStrLetDig
344 -- This will happily eat up the trailing let-dig...
347 -- So we have to go back and see what happened.
348 case (last_ldh_str full_ldh) of
349 (LetDigHypHyphen _) -> fail "label cannot end with a hyphen"
350 (LetDigHypLetDig ld) ->
351 -- Ok, the label didn't end with a hyphen; now we need to split
352 -- off the last letter/digit so we can pack it into our return
354 return $ case (init_ldh_str full_ldh) of
355 -- We only parsed one letter/digit. This can happen
356 -- if the label contains two characters. For example,
357 -- if we try to parse the label "ab", then the "a"
358 -- will be eaten by the label parser, and this
359 -- function will be left with only "b".
360 Nothing -> LdhStrLetDig Nothing ld
362 -- Usual case: there's was some leading let-dig-hyp junk,
364 leading_ldhs -> LdhStrLetDig leading_ldhs ld
368 -- | Compute the length of a 'LdhStrLetDig'. It's at least one, since
369 -- the let-dig at the end is always there. And when there's an
370 -- ldh-str too, we add its length to one.
374 -- >>> import Text.Parsec ( parse )
376 -- >>> let (Right r) = parse ldh_str_let_dig "" "a"
377 -- >>> length_ldh_str_let_dig r
380 -- >>> let (Right r) = parse ldh_str_let_dig "" "abc-def"
381 -- >>> length_ldh_str_let_dig r
384 length_ldh_str_let_dig :: LdhStrLetDig -> Int
385 length_ldh_str_let_dig (LdhStrLetDig Nothing _) = 1
386 length_ldh_str_let_dig (LdhStrLetDig (Just ldhstring) _) =
387 1 + (length_ldh_str ldhstring)
392 -- | The label type from the RFC1035 grammar:
394 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
396 -- We allow the slightly more general syntax from RFC1123, Section 2.1:
398 -- The syntax of a legal Internet host name was specified in RFC-952
399 -- [DNS:4]. One aspect of host name syntax is hereby changed: the
400 -- restriction on the first character is relaxed to allow either a
401 -- letter or a digit. Host software MUST support this more liberal
404 data Label = Label LetDig (Maybe LdhStrLetDig)
407 instance Pretty Label where
408 pretty_show (Label l Nothing) = pretty_show l
409 pretty_show (Label l (Just s)) = (pretty_show l) ++ (pretty_show s)
411 -- | Parse a 'Label'.
413 -- In addition to the grammar, there's another restriction on
414 -- labels: their length must be 63 characters or less. Quoting
415 -- Section 2.3.1, \"Preferred name syntax\", of RFC1035:
417 -- The labels must follow the rules for ARPANET host names. They
418 -- must start with a letter, end with a letter or digit, and have
419 -- as interior characters only letters, digits, and hyphen. There
420 -- are also some restrictions on the length. Labels must be 63
421 -- characters or less.
423 -- We check this only after we have successfully parsed a label.
427 -- >>> import Text.Parsec ( parse, parseTest )
429 -- Make sure we can parse a single character:
431 -- >>> parseTest label "a"
432 -- Label (LetDigLetter (Letter 'a')) Nothing
434 -- And longer strings:
436 -- >>> pretty_print $ parse label "" "abc-def"
439 -- But not anything ending in a hyphen:
441 -- >>> parseTest label "abc-"
442 -- parse error at (line 1, column 5):
443 -- label cannot end with a hyphen
445 -- Or anything over 63 characters:
447 -- >>> parseTest label (['a'..'z'] ++ ['a'..'z'] ++ ['a'..'z'])
448 -- parse error at (line 1, column 79):
449 -- labels must be 63 or fewer characters
451 -- However, /exactly/ 63 characters is acceptable:
453 -- >>> pretty_print $ parse label "" (replicate 63 'x')
454 -- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
456 -- Ensure that a label can begin with a digit:
458 -- >>> pretty_print $ parse label "" "3com"
461 label :: Parser Label
463 l <- let_dig -- Guaranteed to be there
464 maybe_s <- optionMaybe ldh_str_let_dig -- Might not be there
466 -- It can only be one character long, from the letter...
467 Nothing -> return $ Label l maybe_s
469 -- The letter gives us one character, so we check that the rest is
470 -- less than 62 characters long. But in the error message we need
472 Just s -> if (length_ldh_str_let_dig s) <= 62
473 then return $ Label l maybe_s
474 else fail "labels must be 63 or fewer characters"
481 -- | The data type representing a \"subdomain\" from RFC1035:
483 -- <subdomain> ::= <label> | <subdomain> "." <label>
485 -- We have reversed the order of the subdomain and label in the
486 -- second option, however. This is explained in 'subdomain'.
489 SubdomainSingleLabel Label |
490 SubdomainMultipleLabel Label Subdomain
495 instance Pretty Subdomain where
496 pretty_show (SubdomainSingleLabel l) = pretty_show l
497 pretty_show (SubdomainMultipleLabel l s) =
498 (pretty_show l) ++ "." ++ (pretty_show s)
501 instance Reversible Subdomain where
502 -- | Reverse the labels of the given subdomain.
506 -- >>> import Text.Parsec ( parse )
510 -- >>> let (Right r) = parse subdomain "" "com"
511 -- >>> pretty_print $ backwards r
514 -- >>> let (Right r) = parse subdomain "" "example.com"
515 -- >>> pretty_print $ backwards r
518 -- >>> let (Right r) = parse subdomain "" "www.example.com"
519 -- >>> pretty_print $ backwards r
522 -- >>> let (Right r) = parse subdomain "" "new.www.example.com"
523 -- >>> pretty_print $ backwards r
524 -- com.example.www.new
527 -- It's easy to reverse a single label...
528 backwards s@(SubdomainSingleLabel _) = s
530 -- For multiple labels we have two cases. The first is where we have
531 -- exactly two labels, and we just need to swap them.
532 backwards (SubdomainMultipleLabel l (SubdomainSingleLabel m)) =
533 SubdomainMultipleLabel m (SubdomainSingleLabel l)
535 -- And now the hard case. If we reversed @s@, then the "head" of
536 -- the result (@last_s@) should be the last label in the entire
537 -- subdomain. Stick @last_s@ on the front of the result. That makes
540 -- But what to do about the rest? We need to get \"init s\"
541 -- somehow. Well, we have the reverse of it... why not waste a bunch
542 -- of time and reverse that, too? With @init s@ in hand, we can
543 -- prepend @l@ to that, and THEN reverse the entire thing. What we'll
544 -- wind up with looks like @[last_s, init_s_rev, l]@ which you can
545 -- pretend you recognize as the subdomain in reverse.
547 backwards (SubdomainMultipleLabel l s) =
548 case (backwards s) of
549 SubdomainMultipleLabel last_s init_s_rev ->
550 let init_s = backwards init_s_rev
552 SubdomainMultipleLabel
554 (backwards (SubdomainMultipleLabel l init_s))
556 -- Reversing a multiple label thing gives you back a multiple
557 -- label thing but there's no way to promise that.
558 impossible -> impossible
562 -- | Parse an RFC1035 \"subdomain\". The given grammar is,
564 -- <subdomain> ::= <label> | <subdomain> "." <label>
566 -- However, we have reversed the order of the subdomain and label to
567 -- prevent infinite recursion. The second option (subdomain + label)
568 -- is obviously more specific, we we need to try it first. This
569 -- presents a problem: we're trying to parse a subdomain in terms of
570 -- a subdomain! The given grammar represents subdomains how we like
571 -- to think of them; from right to left. But it's better to parse
572 -- from left to right, so we pick off the leading label and then
573 -- recurse into the definition of subdomain.
575 -- According to RFC1034, Section 3.1, two neighboring labels in a
576 -- DNS name cannot be equal:
578 -- Each node has a label, which is zero to 63 octets in length. Brother
579 -- nodes may not have the same label, although the same label can be used
580 -- for nodes which are not brothers. One label is reserved, and that is
581 -- the null (i.e., zero length) label used for the root.
583 -- We enforce this restriction, but the result is usually that we
584 -- only parse the part of the subdomain leading up to the repeated
589 -- >>> import Text.Parsec ( parse, parseTest )
591 -- Make sure we can parse a single character:
593 -- >>> parseTest subdomain "a"
594 -- SubdomainSingleLabel (Label (LetDigLetter (Letter 'a')) Nothing)
596 -- >>> pretty_print $ parse subdomain "" "example.com"
599 -- >>> pretty_print $ parse subdomain "" "www.example.com"
602 -- We reject a subdomain with equal neighbors, but this leads to
603 -- only the single first label being parsed instead:
605 -- >>> pretty_print $ parse subdomain "" "www.www.example.com"
608 -- But not one with a repeated but non-neighboring label:
610 -- >>> pretty_print $ parse subdomain "" "www.example.www.com"
611 -- www.example.www.com
613 subdomain :: Parser Subdomain
614 subdomain = try both <|> just_one
616 both :: Parser Subdomain
621 let result = SubdomainMultipleLabel l s
622 if (subdomain_has_equal_neighbors result)
623 then fail "subdomain cannot have equal neighboring labels"
626 just_one :: Parser Subdomain
627 just_one = fmap SubdomainSingleLabel label
631 -- | Retrieve a list of labels contained in a 'Subdomain'.
635 -- >>> import Text.Parsec ( parse )
637 -- >>> let (Right r) = parse subdomain "" "a"
638 -- >>> pretty_print $ subdomain_labels r
641 -- >>> let (Right r) = parse subdomain "" "example.com"
642 -- >>> pretty_print $ subdomain_labels r
645 -- >>> let (Right r) = parse subdomain "" "www.example.com"
646 -- >>> pretty_print $ subdomain_labels r
647 -- ["www","example","com"]
649 subdomain_labels :: Subdomain -> [Label]
650 subdomain_labels (SubdomainSingleLabel l) = [l]
651 subdomain_labels (SubdomainMultipleLabel l s) = l : (subdomain_labels s)
654 -- | Return a list of pairs of neighboring labels in a subdomain.
658 -- >>> import Text.Parsec ( parse )
659 -- >>> let (Right r) = parse subdomain "" "www.example.com"
660 -- >>> pretty_print $ subdomain_label_neighbors r
661 -- ["(\"www\",\"example\")","(\"example\",\"com\")"]
663 subdomain_label_neighbors :: Subdomain -> [(Label,Label)]
664 subdomain_label_neighbors s =
667 ls = subdomain_labels s
670 -- | Return @True@ if the subdomain has any two equal neighboring
671 -- labels, and @False@ otherwise.
675 -- >>> import Text.Parsec ( parse )
677 -- >>> let (Right r) = parse subdomain "" "www.example.com"
678 -- >>> subdomain_has_equal_neighbors r
681 -- >>> let (Right l) = parse label "" "www"
682 -- >>> let (Right s) = parse subdomain "" "www.example.com"
683 -- >>> let bad_subdomain = SubdomainMultipleLabel l s
684 -- >>> subdomain_has_equal_neighbors bad_subdomain
687 subdomain_has_equal_neighbors :: Subdomain -> Bool
688 subdomain_has_equal_neighbors s =
689 or [ x == y | (x,y) <- subdomain_label_neighbors s ]
696 -- | An RFC1035 domain. According to RFC1035 a domain can be either a
697 -- subdomain or \" \", which according to RFC2181
698 -- <https://tools.ietf.org/html/rfc2181#section-11> means the root:
700 -- The zero length full name is defined as representing the root
701 -- of the DNS tree, and is typically written and displayed as
704 -- We let the 'Domain' type remain true to those RFCs, even though
705 -- they don't support an absolute domain name of e.g. a single dot.
708 DomainName Subdomain |
712 instance Pretty Domain where
713 pretty_show DomainRoot = ""
714 pretty_show (DomainName s) = pretty_show s
716 -- | Parse an RFC1035 \"domain\"
720 -- >>> import Text.Parsec ( parse, parseTest )
722 -- Make sure we can parse a single character:
724 -- >>> pretty_print $ parse domain "" "a"
727 -- And the empty domain:
729 -- >>> parseTest domain ""
732 -- We will in fact parse the \"empty\" domain off the front of
733 -- pretty much anything:
735 -- >>> parseTest domain "!8===D"
738 -- Equality of domains is case-insensitive:
740 -- >>> let (Right r1) = parse domain "" "example.com"
741 -- >>> let (Right r2) = parse domain "" "ExaMPle.coM"
745 -- A single dot IS parsed as the root, but the dot isn't consumed:
747 -- >>> parseTest domain "."
750 -- Anything over 255 characters is an error, so the root will be
753 -- >>> let big_l1 = replicate 63 'x'
754 -- >>> let big_l2 = replicate 63 'y' -- Avoid equal neighboring labels!
755 -- >>> let big_labels = big_l1 ++ "." ++ big_l2 ++ "."
756 -- >>> let big_subdomain = concat $ replicate 3 big_labels
757 -- >>> parseTest domain big_subdomain
760 -- But exactly 255 is allowed:
762 -- >>> import Data.List ( intercalate )
763 -- >>> let l1 = replicate 63 'w'
764 -- >>> let l2 = replicate 63 'x'
765 -- >>> let l3 = replicate 63 'y'
766 -- >>> let l4 = replicate 63 'z'
767 -- >>> let big_subdomain = intercalate "." [l1,l2,l3,l4]
768 -- >>> let (Right r) = parse domain "" big_subdomain
769 -- >>> length (pretty_show r)
772 domain :: Parser Domain
773 domain = try parse_subdomain <|> parse_empty
775 parse_subdomain :: Parser Domain
778 if length (pretty_show s) <= 255
779 then return $ DomainName s
780 else fail "subdomains can be at most 255 characters"
782 parse_empty :: Parser Domain
783 parse_empty = string "" >> return DomainRoot
786 instance Reversible Domain where
787 -- | Reverse the labels of a 'Domain'.
789 -- -- ==== _Examples_
791 -- >>> import Text.Parsec ( parse )
793 -- The root reverses to itself:
795 -- >>> let (Right r) = parse domain "" ""
799 -- >>> let (Right r) = parse domain "" "new.www.example.com"
800 -- >>> pretty_print $ backwards r
801 -- com.example.www.new
803 backwards DomainRoot = DomainRoot
804 backwards (DomainName s) = DomainName $ backwards s