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>
11 import Data.Char ( toLower )
25 import qualified Text.Parsec as Parsec ( digit, letter)
26 import Text.Parsec.String ( Parser )
28 import Pretty ( Pretty(..) )
32 -- | A wrapper around a digit character.
34 newtype Digit = Digit Char deriving (Eq, Show)
35 instance Pretty Digit where pretty_show (Digit d) = [d]
37 -- | Parse a single digit, but wrap it in our 'Digit' type.
40 digit = fmap Digit Parsec.digit
45 -- | A wrapper around a letter character.
47 newtype Letter = Letter Char deriving (Show)
48 instance Pretty Letter where pretty_show (Letter l) = [l]
51 -- | Parse a single letter, but wrap it in our 'Letter' type.
53 letter :: Parser Letter
54 letter = fmap Letter Parsec.letter
56 -- | The derived instance of 'Eq' for letters is incorrect. All
57 -- comparisons should be made case-insensitively. The following
58 -- is an excerpt from RFC1035:
60 -- 2.3.3. Character Case
62 -- For all parts of the DNS that are part of the official
63 -- protocol, all comparisons between character strings (e.g.,
64 -- labels, domain names, etc.) are done in a case-insensitive
67 -- Since each part of DNS name is composed of our custom types, it
68 -- suffices to munge the equality for 'Letter'.
70 instance Eq Letter where
71 (Letter l1) == (Letter l2) = (toLower l1) == (toLower l2)
75 -- | A sum type representing either a letter or a digit.
82 instance Pretty LetDig where
83 pretty_show (LetDigLetter l) = pretty_show l
84 pretty_show (LetDigDigit d) = pretty_show d
86 -- | Parse a letter or a digit and wrap it in our 'LetDig' type.
88 let_dig :: Parser LetDig
89 let_dig = (fmap LetDigLetter letter) <|> (fmap LetDigDigit digit)
94 -- | A wrapper around a single hyphen character.
96 newtype Hyphen = Hyphen Char deriving (Eq, Show)
97 instance Pretty Hyphen where pretty_show (Hyphen h) = [h]
99 -- | Parse a single hyphen and wrap it in our 'Hyphen' type.
101 hyphen :: Parser Hyphen
102 hyphen = fmap Hyphen (char '-')
105 -- * Letter, Digit, or Hyphen.
107 -- | A sum type representing a letter, digit, or hyphen.
110 LetDigHypLetDig LetDig |
111 LetDigHypHyphen Hyphen
114 instance Pretty LetDigHyp where
115 pretty_show (LetDigHypLetDig ld) = pretty_show ld
116 pretty_show (LetDigHypHyphen h) = pretty_show h
119 -- | The following is the simplest type in the domain grammar that
120 -- isn't already implemented for us.
122 -- <let-dig> ::= <letter> | <digit>
126 -- >>> import Text.Parsec ( parseTest )
128 -- Letters, digits, and hyphens are all parsed:
130 -- >>> parseTest let_dig_hyp "a"
131 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
133 -- >>> parseTest let_dig_hyp "7"
134 -- LetDigHypLetDig (LetDigDigit (Digit '7'))
136 -- >>> parseTest let_dig_hyp "-"
137 -- LetDigHypHyphen (Hyphen '-')
139 -- However, an underscore (for example) is not:
141 -- >>> parseTest let_dig_hyp "_"
142 -- parse error at (line 1, column 1):
144 -- expecting letter, digit or "-"
146 let_dig_hyp :: Parser LetDigHyp
148 parse_letdig <|> parse_hyphen
150 parse_letdig :: Parser LetDigHyp
151 parse_letdig = fmap LetDigHypLetDig let_dig
153 parse_hyphen :: Parser LetDigHyp
154 parse_hyphen = fmap LetDigHypHyphen hyphen
157 -- * Letter/Digit/Hyphen strings
159 -- | A string of letters, digits, and hyphens from the RFC1035 grammar:
161 -- <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
163 -- These are represented as either a single instance of a
164 -- 'LetDigHyp', or a string of them (recursive).
167 LdhStrSingleLdh LetDigHyp |
168 LdhStrMultipleLdh LetDigHyp LdhStr
171 instance Pretty LdhStr where
172 pretty_show (LdhStrSingleLdh ldh) = pretty_show ldh
173 pretty_show (LdhStrMultipleLdh ldh s) = (pretty_show ldh) ++ (pretty_show s)
175 -- | Parse a string of letters, digits, and hyphens (an 'LdhStr').
179 -- >>> import Text.Parsec ( parseTest )
181 -- Single letters, digits, and hyphens are parsed:
183 -- >>> parseTest ldh_str "a"
184 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a')))
186 -- >>> parseTest ldh_str "0"
187 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigDigit (Digit '0')))
189 -- >>> parseTest ldh_str "-"
190 -- LdhStrSingleLdh (LetDigHypHyphen (Hyphen '-'))
192 -- As well as strings of them:
194 -- >>> pretty_print $ parse ldh_str "" "a0-b"
197 ldh_str :: Parser LdhStr
198 ldh_str = try both <|> just_one
200 both :: Parser LdhStr
204 return $ LdhStrMultipleLdh ldh1 ldh_tail
206 just_one :: Parser LdhStr
207 just_one = fmap LdhStrSingleLdh let_dig_hyp
211 -- | A version of 'last' that works on a 'LdhStr' rather than a
212 -- list. That is, it returns the last 'LetDigHyp' in the
213 -- string. Since 'LdhStr' contains at least one character, there's
214 -- no \"nil\" case here.
218 -- >>> let (Right r) = parse ldh_str "" "a"
219 -- >>> last_ldh_str r
220 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
222 -- >>> let (Right r) = parse ldh_str "" "abc-def"
223 -- >>> last_ldh_str r
224 -- LetDigHypLetDig (LetDigLetter (Letter 'f'))
226 last_ldh_str :: LdhStr -> LetDigHyp
227 last_ldh_str (LdhStrSingleLdh x) = x
228 last_ldh_str (LdhStrMultipleLdh _ x) = last_ldh_str x
231 -- | A version of 'init' that works on a 'LdhStr' rather than a
232 -- list. That is, it returns everything /except/ the last character in
235 -- Since an 'LdhStr' must contain at least one character, this might
236 -- not be opssible (when the input is of length one). So, we return
241 -- >>> let (Right r) = parse ldh_str "" "a"
242 -- >>> init_ldh_str r
245 -- >>> let (Right r) = parse ldh_str "" "ab"
246 -- >>> init_ldh_str r
247 -- Just (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))))
249 -- >>> let (Right r) = parse ldh_str "" "abc-def"
250 -- >>> init_ldh_str r
251 -- 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')))))))))
253 init_ldh_str :: LdhStr -> Maybe LdhStr
254 init_ldh_str (LdhStrSingleLdh _) = Nothing
255 init_ldh_str (LdhStrMultipleLdh h t) =
256 Just $ case (init_ldh_str t) of
257 -- We just got the second-to-last character, we're done.
258 Nothing -> LdhStrSingleLdh h
260 -- There's still more stuff. Recurse.
261 Just rest -> LdhStrMultipleLdh h rest
264 -- | Compute the length of an 'LdhStr'. It will be at least one, since
265 -- 'LdhStr's are non-empty. And if there's something other than the
266 -- first character present, we simply recurse.
270 -- >>> let (Right r) = parse ldh_str "" "a"
271 -- >>> length_ldh_str r
274 -- >>> let (Right r) = parse ldh_str "" "abc-def"
275 -- >>> length_ldh_str r
278 length_ldh_str :: LdhStr -> Int
279 length_ldh_str (LdhStrSingleLdh _) = 1
280 length_ldh_str (LdhStrMultipleLdh _ t) = 1 + (length_ldh_str t)
282 -- * Letter/Digit/Hyphen string followed by a trailing Letter/Digit
284 -- | This type isn't explicitly part of the grammar, but it's what
285 -- shows up in the square brackets of,
287 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
289 -- The ldh-str is optional, but if one is present, we must also have
290 -- a trailing let-dig to prevent the name from ending with a
291 -- hyphen. This can be represented with a @Maybe LdhStrLetDig@,
292 -- which is why we're about to define it.
294 data LdhStrLetDig = LdhStrLetDig (Maybe LdhStr) LetDig
297 instance Pretty LdhStrLetDig where
298 pretty_show (LdhStrLetDig Nothing ld) = pretty_show ld
299 pretty_show (LdhStrLetDig (Just s) ld) = (pretty_show s) ++ (pretty_show ld)
301 -- | Parse an 'LdhStrLetDig'. This isn't in the grammar, but we might
302 -- as well define the parser for it independently since we gave it
303 -- its own data type.
307 -- >>> import Text.Parsec ( parseTest )
309 -- Make sure we can parse a single character:
311 -- >>> parseTest ldh_str_let_dig "a"
312 -- LdhStrLetDig Nothing (LetDigLetter (Letter 'a'))
314 -- And longer strings:
316 -- >>> pretty_print $ parse ldh_str_let_dig "" "ab"
319 -- >>> pretty_print $ parse ldh_str_let_dig "" "-b"
322 -- >>> parseTest ldh_str_let_dig "b-"
323 -- parse error at (line 1, column 3):
324 -- label cannot end with a hyphen
326 ldh_str_let_dig :: Parser LdhStrLetDig
328 -- This will happily eat up the trailing let-dig...
331 -- So we have to go back and see what happened.
332 case (last_ldh_str full_ldh) of
333 (LetDigHypHyphen _) -> fail "label cannot end with a hyphen"
334 (LetDigHypLetDig ld) ->
335 -- Ok, the label didn't end with a hyphen; now we need to split
336 -- off the last letter/digit so we can pack it into our return
338 return $ case (init_ldh_str full_ldh) of
339 -- We only parsed one letter/digit. This can happen
340 -- if the label contains two characters. For example,
341 -- if we try to parse the label "ab", then the "a"
342 -- will be eaten by the label parser, and this
343 -- function will be left with only "b".
344 Nothing -> LdhStrLetDig Nothing ld
346 -- Usual case: there's was some leading let-dig-hyp junk,
348 leading_ldhs -> LdhStrLetDig leading_ldhs ld
352 -- | Compute the length of a 'LdhStrLetDig'. It's at least one, since
353 -- the let-dig at the end is always there. And when there's an
354 -- ldh-str too, we add its length to one.
358 -- >>> let (Right r) = parse ldh_str_let_dig "" "a"
359 -- >>> length_ldh_str_let_dig r
362 -- >>> let (Right r) = parse ldh_str_let_dig "" "abc-def"
363 -- >>> length_ldh_str_let_dig r
366 length_ldh_str_let_dig :: LdhStrLetDig -> Int
367 length_ldh_str_let_dig (LdhStrLetDig Nothing _) = 1
368 length_ldh_str_let_dig (LdhStrLetDig (Just ldhstring) _) =
369 1 + (length_ldh_str ldhstring)
374 -- | The label type from the RFC1035 grammar:
376 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
378 data Label = Label Letter (Maybe LdhStrLetDig)
381 instance Pretty Label where
382 pretty_show (Label l Nothing) = pretty_show l
383 pretty_show (Label l (Just s)) = (pretty_show l) ++ (pretty_show s)
385 -- | Parse a 'Label'.
387 -- In addition to the grammar, there's another restriction on
388 -- labels: their length must be 63 characters or less. Quoting
389 -- Section 2.3.1, \"Preferred name syntax\", of RFC1035:
391 -- The labels must follow the rules for ARPANET host names. They
392 -- must start with a letter, end with a letter or digit, and have
393 -- as interior characters only letters, digits, and hyphen. There
394 -- are also some restrictions on the length. Labels must be 63
395 -- characters or less.
397 -- We check this only after we have successfully parsed a label.
401 -- >>> import Text.Parsec ( parseTest )
403 -- Make sure we can parse a single character:
405 -- >>> parseTest label "a"
406 -- Label (Letter 'a') Nothing
408 -- And longer strings:
410 -- >>> pretty_print $ parse label "" "abc-def"
413 -- But not anything ending in a hyphen:
415 -- >>> parseTest label "abc-"
416 -- parse error at (line 1, column 5):
417 -- label cannot end with a hyphen
419 -- Or anything over 63 characters:
421 -- >>> parseTest label (['a'..'z'] ++ ['a'..'z'] ++ ['a'..'z'])
422 -- parse error at (line 1, column 79):
423 -- labels must be 63 or fewer characters
425 -- However, /exactly/ 63 characters is acceptable:
427 -- >>> pretty_print $ parse label "" (replicate 63 'x')
428 -- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
430 label :: Parser Label
432 l <- letter -- Guaranteed to be there
433 maybe_s <- optionMaybe ldh_str_let_dig -- Might not be there
435 -- It can only be one character long, from the letter...
436 Nothing -> return $ Label l maybe_s
438 -- The letter gives us one character, so we check that the rest is
439 -- less than 62 characters long. But in the error message we need
441 Just s -> if (length_ldh_str_let_dig s) <= 62
442 then return $ Label l maybe_s
443 else fail "labels must be 63 or fewer characters"
450 -- | The data type representing a \"subdomain\" from RFC1035:
452 -- <subdomain> ::= <label> | <subdomain> "." <label>
454 -- We have reversed the order of the subdomain and label in the
455 -- second option, however. This is explained in 'subdomain'.
458 SubdomainSingleLabel Label |
459 SubdomainMultipleLabel Label Subdomain
462 instance Pretty Subdomain where
463 pretty_show (SubdomainSingleLabel l) = pretty_show l
464 pretty_show (SubdomainMultipleLabel l s) =
465 (pretty_show l) ++ "." ++ (pretty_show s)
467 -- | Parse an RFC1035 \"subdomain\". The given grammar is,
469 -- <subdomain> ::= <label> | <subdomain> "." <label>
471 -- However, we have reversed the order of the subdomain and label to
472 -- prevent infinite recursion. The second option (subdomain + label)
473 -- is obviously more specific, we we need to try it first. This
474 -- presents a problem: we're trying to parse a subdomain in terms of
475 -- a subdomain! The given grammar represents subdomains how we like
476 -- to think of them; from right to left. But it's better to parse
477 -- from left to right, so we pick off the leading label and then
478 -- recurse into the definition of subdomain.
482 -- >>> import Text.Parsec ( parseTest )
484 -- Make sure we can parse a single character:
486 -- >>> parseTest subdomain "a"
487 -- SubdomainSingleLabel (Label (Letter 'a') Nothing)
489 -- >>> pretty_print $ parse subdomain "" "example.com"
492 -- >>> pretty_print $ parse subdomain "" "www.example.com"
495 subdomain :: Parser Subdomain
496 subdomain = try both <|> just_one
498 both :: Parser Subdomain
503 return (SubdomainMultipleLabel l s)
505 just_one :: Parser Subdomain
506 just_one = fmap SubdomainSingleLabel label
512 -- | An RFC1035 domain. According to RFC1035 a domain can be either a
513 -- subdomain or \" \", which according to RFC2181
514 -- <https://tools.ietf.org/html/rfc2181#section-11> means the root:
516 -- The zero length full name is defined as representing the root
517 -- of the DNS tree, and is typically written and displayed as
520 -- We let the 'Domain' type remain true to those RFCs, even though
521 -- they don't support an absolute domain name of e.g. a single dot.
522 -- We have one more data type 'UserDomain' which handles the possibility
523 -- of an absolute path.
526 DomainName Subdomain |
530 instance Pretty Domain where
531 pretty_show DomainRoot = ""
532 pretty_show (DomainName s) = pretty_show s
534 -- | Parse an RFC1035 \"domain\"
538 -- >>> import Text.Parsec ( parseTest )
540 -- Make sure we can parse a single character:
542 -- >>> parseTest domain "a"
543 -- DomainName (SubdomainSingleLabel (Label (Letter 'a') Nothing))
545 -- And the empty domain:
547 -- >>> parseTest domain ""
550 -- We will in fact parse the \"empty\" domain off the front of
551 -- pretty much anything:
553 -- >>> parseTest domain "8===D"
556 -- Equality of domains is case-insensitive:
558 -- >>> let (Right r1) = parse domain "" "example.com"
559 -- >>> let (Right r2) = parse domain "" "ExaMPle.coM"
563 -- A single dot IS parsed as the root, but the dot isn't consumed:
565 -- >>> parseTest domain "."
568 domain :: Parser Domain
569 domain = try parse_subdomain <|> parse_empty
571 parse_subdomain = fmap DomainName subdomain
572 parse_empty = string "" >> return DomainRoot
578 -- | This type helps clarify some murkiness in the DNS \"domain\" name
579 -- specification. In RFC1034, it is acknowledged that a domain name
580 -- input with a trailing \".\" will represent an absolute domain
581 -- name (i.e. with respect to the DNS root). However, the grammar in
582 -- RFC1035 disallows a trailing dot.
584 -- This makes some sense: within the DNS, everything knows its
585 -- position in the tree. The relative/absolute distinction only
586 -- makes sense on the client side, where a user's resolver might
587 -- decide to append some suffix to a relative
588 -- request. Unfortunately, that's where we live. So we have to deal
589 -- with the possibility of having a trailing dot at the end of any
593 UserDomainRelative Domain |
594 UserDomainAbsolute Domain
597 instance Pretty UserDomain where
598 pretty_show (UserDomainRelative d) = pretty_show d
599 pretty_show (UserDomainAbsolute d) = (pretty_show d) ++ "."
602 -- | Parse a 'UserDomain'. This is what we'll be using to read user
603 -- input, since it supports both relative and absolute domain names
604 -- (unlike the implicitly-absolute 'Domain').
608 -- >>> import Text.Parsec ( parseTest )
610 -- We can really parse the root now!
612 -- >>> parseTest user_domain "."
613 -- UserDomainAbsolute DomainRoot
615 -- But multiple dots aren't (only the first):
617 -- >>> pretty_print $ parse user_domain "" ".."
620 -- We can also optionally have a trailing dot at the end of a
623 -- >>> pretty_print $ parse user_domain "" "www.example.com"
626 -- >>> pretty_print $ parse user_domain "" "www.example.com."
629 -- A \"relative root\" can also be parsed, letting the user's
630 -- resolver deal with it:
632 -- >>> parseTest user_domain ""
633 -- UserDomainRelative DomainRoot
635 user_domain :: Parser UserDomain
636 user_domain = try absolute <|> relative
638 absolute :: Parser UserDomain
642 return $ UserDomainAbsolute d
644 relative :: Parser UserDomain
645 relative = fmap UserDomainRelative domain