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 newtype Domain = Domain String deriving Show
31 -- | The derived instance of 'Eq' for domain names is incorrect. All
32 -- comparisons are currently made case-insensitively. The following is
33 -- an excerpt from RFC1035:
35 -- 2.3.3. Character Case
37 -- For all parts of the DNS that are part of the official
38 -- protocol, all comparisons between character strings (e.g.,
39 -- labels, domain names, etc.) are done in a case-insensitive
42 -- So to compare two DNS names, we compare their lower-case
45 instance Eq Domain where
46 (Domain d1) == (Domain d2) =
47 (map toLower d1) == (map toLower d2)
51 newtype Digit = Digit Char deriving (Eq, Show)
54 digit = fmap Digit Parsec.digit
58 newtype Letter = Letter Char deriving (Eq, Show)
60 letter :: Parser Letter
61 letter = fmap Letter Parsec.letter
65 data LetDig = LetDigLetter Letter | LetDigDigit Digit deriving (Eq, Show)
67 let_dig :: Parser LetDig
68 let_dig = (fmap LetDigLetter letter) <|> (fmap LetDigDigit digit)
72 newtype Hyphen = Hyphen Char deriving (Eq, Show)
74 hyphen :: Parser Hyphen
75 hyphen = fmap Hyphen (char '-')
78 -- * Letter, Digit, or Hyphen.
80 data LetDigHyp = LetDigHypLetDig LetDig
81 | LetDigHypHyphen Hyphen
85 -- | The following is the simplest type in the domain grammar that
86 -- isn't already implemented for us.
88 -- <let-dig> ::= <letter> | <digit>
92 -- >>> import Text.Parsec ( parseTest )
94 -- Letters, digits, and hyphens are all parsed:
96 -- >>> parseTest let_dig_hyp "a"
97 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
99 -- >>> parseTest let_dig_hyp "7"
100 -- LetDigHypLetDig (LetDigDigit (Digit '7'))
102 -- >>> parseTest let_dig_hyp "-"
103 -- LetDigHypHyphen (Hyphen '-')
105 -- However, an underscore (for example) is not:
107 -- >>> parseTest let_dig_hyp "_"
108 -- parse error at (line 1, column 1):
110 -- expecting letter, digit or "-"
112 let_dig_hyp :: Parser LetDigHyp
114 parse_letdig <|> parse_hyphen
116 parse_letdig :: Parser LetDigHyp
117 parse_letdig = fmap LetDigHypLetDig let_dig
119 parse_hyphen :: Parser LetDigHyp
120 parse_hyphen = fmap LetDigHypHyphen hyphen
123 -- * Letter/Digit/Hyphen strings
125 -- | A string of letters, digits, and hyphens from the RFC1035 grammar:
127 -- <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
129 -- These are represented as either a single instance of a
130 -- 'LetDigHyp', or a string of them (recursive).
134 -- >>> import Text.Parsec ( parseTest )
136 -- Single letters, digits, and hyphens are parsed:
138 -- >>> parseTest ldh_str "a"
139 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a')))
141 -- >>> parseTest ldh_str "0"
142 -- LdhStrSingleLdh (LetDigHypLetDig (LetDigDigit (Digit '0')))
144 -- >>> parseTest ldh_str "-"
145 -- LdhStrSingleLdh (LetDigHypHyphen (Hyphen '-'))
147 -- As well as strings of them:
149 -- LdhStr (LetDigHypLetter (Letter 'a')) (LdhStr (LetDigHypDigit (Digit '0')) (LdhStr (LetDigHypHyphen (Hyphen '-')) (LdhStrSingleLdh (LetDigHypLetter (Letter 'b')))))
152 LdhStrSingleLdh LetDigHyp | LdhStrMultipleLdh LetDigHyp LdhStr
155 ldh_str :: Parser LdhStr
156 ldh_str = try both <|> just_one
158 both :: Parser LdhStr
162 return $ LdhStrMultipleLdh ldh1 ldh_tail
164 just_one :: Parser LdhStr
165 just_one = fmap LdhStrSingleLdh let_dig_hyp
169 -- | A version of 'last' that works on a 'LdhStr' rather than a
170 -- list. That is, it returns the last 'LetDigHyp' in the
171 -- string. Since 'LdhStr' contains at least one character, there's
172 -- no \"nil\" case here.
176 -- >>> let (Right r) = parse ldh_str "" "a"
177 -- >>> last_ldh_str r
178 -- LetDigHypLetDig (LetDigLetter (Letter 'a'))
180 -- >>> let (Right r) = parse ldh_str "" "abc-def"
181 -- >>> last_ldh_str r
182 -- LetDigHypLetDig (LetDigLetter (Letter 'f'))
184 last_ldh_str :: LdhStr -> LetDigHyp
185 last_ldh_str (LdhStrSingleLdh x) = x
186 last_ldh_str (LdhStrMultipleLdh _ x) = last_ldh_str x
189 -- | A version of 'init' that works on a 'LdhStr' rather than a
190 -- list. That is, it returns everything /except/ the last character in
193 -- Since an 'LdhStr' must contain at least one character, this might
194 -- not be opssible (when the input is of length one). So, we return
199 -- >>> let (Right r) = parse ldh_str "" "a"
200 -- >>> init_ldh_str r
203 -- >>> let (Right r) = parse ldh_str "" "ab"
204 -- >>> init_ldh_str r
205 -- Just (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))))
207 -- >>> let (Right r) = parse ldh_str "" "abc-def"
208 -- >>> init_ldh_str r
209 -- 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')))))))))
211 init_ldh_str :: LdhStr -> Maybe LdhStr
212 init_ldh_str (LdhStrSingleLdh _) = Nothing
213 init_ldh_str (LdhStrMultipleLdh h t) =
214 Just $ case (init_ldh_str t) of
215 -- We just got the second-to-last character, we're done.
216 Nothing -> LdhStrSingleLdh h
218 -- There's still more stuff. Recurse.
219 Just rest -> LdhStrMultipleLdh h rest
222 -- | Compute the length of an 'LdhStr'. It will be at least one, since
223 -- 'LdhStr's are non-empty. And if there's something other than the
224 -- first character present, we simply recurse.
228 -- >>> let (Right r) = parse ldh_str "" "a"
229 -- >>> length_ldh_str r
232 -- >>> let (Right r) = parse ldh_str "" "abc-def"
233 -- >>> length_ldh_str r
236 length_ldh_str :: LdhStr -> Int
237 length_ldh_str (LdhStrSingleLdh _) = 1
238 length_ldh_str (LdhStrMultipleLdh _ t) = 1 + (length_ldh_str t)
240 -- * Letter/Digit/Hyphen string followed by a trailing Letter/Digit
242 -- | This type isn't explicitly part of the grammar, but it's what
243 -- shows up in the square brackets of,
245 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
247 -- The ldh-str is optional, but if one is present, we must also have
248 -- a trailing let-dig to prevent the name from ending with a
249 -- hyphen. This can be represented with a @Maybe LdhStrLetDig@,
250 -- which is why we're about to define it.
252 data LdhStrLetDig = LdhStrLetDig (Maybe LdhStr) LetDig
255 -- | Parse an 'LdhStrLetDig'. This isn't in the grammar, but we might
256 -- as well define the parser for it independently since we gave it
257 -- its own data type.
261 -- >>> import Text.Parsec ( parseTest )
263 -- Make sure we can parse a single character:
265 -- >>> parseTest ldh_str_let_dig "a"
266 -- LdhStrLetDig Nothing (LetDigLetter (Letter 'a'))
268 -- And longer strings:
270 -- >>> parseTest ldh_str_let_dig "ab"
271 -- LdhStrLetDig (Just (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))))) (LetDigLetter (Letter 'b'))
273 -- >>> parseTest ldh_str_let_dig "-b"
274 -- LdhStrLetDig (Just (LdhStrSingleLdh (LetDigHypHyphen (Hyphen '-')))) (LetDigLetter (Letter 'b'))
276 -- >>> parseTest ldh_str_let_dig "b-"
277 -- parse error at (line 1, column 3):
278 -- label cannot end with a hyphen
280 ldh_str_let_dig :: Parser LdhStrLetDig
282 -- This will happily eat up the trailing let-dig...
285 -- So we have to go back and see what happened.
286 case (last_ldh_str full_ldh) of
287 (LetDigHypHyphen _) -> fail "label cannot end with a hyphen"
288 (LetDigHypLetDig ld) ->
289 -- Ok, the label didn't end with a hyphen; now we need to split
290 -- off the last letter/digit so we can pack it into our return
292 return $ case (init_ldh_str full_ldh) of
293 -- We only parsed one letter/digit. This can happen
294 -- if the label contains two characters. For example,
295 -- if we try to parse the label "ab", then the "a"
296 -- will be eaten by the label parser, and this
297 -- function will be left with only "b".
298 Nothing -> LdhStrLetDig Nothing ld
300 -- Usual case: there's was some leading let-dig-hyp junk,
302 leading_ldhs -> LdhStrLetDig leading_ldhs ld
306 -- | Compute the length of a 'LdhStrLetDig'. It's at least one, since
307 -- the let-dig at the end is always there. And when there's an
308 -- ldh-str too, we add its length to one.
312 -- >>> let (Right r) = parse ldh_str_let_dig "" "a"
313 -- >>> length_ldh_str_let_dig r
316 -- >>> let (Right r) = parse ldh_str_let_dig "" "abc-def"
317 -- >>> length_ldh_str_let_dig r
320 length_ldh_str_let_dig :: LdhStrLetDig -> Int
321 length_ldh_str_let_dig (LdhStrLetDig Nothing _) = 1
322 length_ldh_str_let_dig (LdhStrLetDig (Just ldhstring) _) =
323 1 + (length_ldh_str ldhstring)
328 -- | The label type from the RFC1035 grammar:
330 -- <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
332 data Label = Label Letter (Maybe LdhStrLetDig)
336 -- | Parse a 'Label'.
338 -- In addition to the grammar, there's another restriction on
339 -- labels: their length must be 63 characters or less. Quoting
340 -- Section 2.3.1, \"Preferred name syntax\", of RFC1035:
342 -- The labels must follow the rules for ARPANET host names. They
343 -- must start with a letter, end with a letter or digit, and have
344 -- as interior characters only letters, digits, and hyphen. There
345 -- are also some restrictions on the length. Labels must be 63
346 -- characters or less.
348 -- We check this only after we have successfully parsed a label.
352 -- >>> import Text.Parsec ( parseTest )
354 -- Make sure we can parse a single character:
356 -- >>> parseTest label "a"
357 -- Label (Letter 'a') Nothing
359 -- And longer strings:
361 -- >>> parseTest label "abc-def"
362 -- Label (Letter 'a') (Just (LdhStrLetDig (Just (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'b'))) (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'c'))) (LdhStrMultipleLdh (LetDigHypHyphen (Hyphen '-')) (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'd'))) (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'e'))))))))) (LetDigLetter (Letter 'f'))))
364 -- But not anything ending in a hyphen:
366 -- >>> parseTest label "abc-"
367 -- parse error at (line 1, column 5):
368 -- label cannot end with a hyphen
370 -- Or anything over 63 characters:
372 -- >>> parseTest label (['a'..'z'] ++ ['a'..'z'] ++ ['a'..'z'])
373 -- parse error at (line 1, column 79):
374 -- labels must be 63 or fewer characters
376 -- However, /exactly/ 63 characters is acceptable:
380 label :: Parser Label
382 l <- letter -- Guaranteed to be there
383 maybe_s <- optionMaybe ldh_str_let_dig -- Might not be there
385 -- It can only be one character long, from the letter...
386 Nothing -> return $ Label l maybe_s
388 -- The letter gives us one character, so we check that the rest is
389 -- less than 62 characters long. But in the error message we need
391 Just s -> if (length_ldh_str_let_dig s) <= 62
392 then return $ Label l maybe_s
393 else fail "labels must be 63 or fewer characters"
396 domain :: Parser Domain
399 --subdomain :: Parser Subdomain
400 --subdomain = undefined