Advertisement
ptrelford

Domain host name decoding

Jan 16th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.71 KB | None | 0 0
  1. #if INTERACTIVE
  2. #r "System.Net.Http.dll"
  3. #endif
  4.  
  5. let publicSuffixList =
  6.     let url = "https://publicsuffix.org/list/public_suffix_list.dat"
  7.     use httpClient = new System.Net.Http.HttpClient()
  8.     let text = httpClient.GetStringAsync(url).Result
  9.     let lines = text.Split('\n')
  10.     lines
  11.     |> Array.filter (fun line -> not(System.String.IsNullOrWhiteSpace line))
  12.     |> Array.filter (fun line -> not(line.StartsWith "//"))
  13.     |> Array.filter (fun line -> not(line.StartsWith "!"))
  14.     |> Array.map (fun line -> if line.StartsWith "*." then line.Substring 2 else line)
  15.     |> Array.map (fun suffix -> "." + suffix.Trim())
  16.     |> Array.sortByDescending (fun text -> text.Length)
  17.  
  18. let suffix (host:string) =
  19.     publicSuffixList |> Array.tryFind (fun suffix -> host.EndsWith suffix)
  20.  
  21. let registeredName (url:string) =
  22.     let uri = System.Uri(url,System.UriKind.Absolute)
  23.     let host = uri.Host
  24.     match suffix host with
  25.     | Some suffix ->
  26.         let name = host.Substring(0,host.Length-suffix.Length)
  27.         match name.LastIndexOf('.') with
  28.         | -1 -> Some name
  29.         | index -> Some(name.Substring(index+1))
  30.     | None -> None
  31.    
  32. suffix "austin.rr.com" // -> Some ".com"
  33. suffix "hotmail.co.jp" // -> Some ".co.jp"
  34. suffix "yahoo.com.br" // -> Some ".com.br"
  35. suffix "prodigy.net.mx" // -> Some ".net.mx"
  36. suffix "alum.mit.edu" // Some ".edu"
  37.  
  38. registeredName "http://www.idtechex.com" // -> Some "idtechex"
  39. registeredName "http://wilderblog.azurewebsites.net" // -> Some "wilderblog"
  40. registeredName "http://www.eng.cam.ac.uk/" // -> Some "cam"
  41. registeredName "http://web.mit.edu/facts/academic.html" // -> Some "mit"
  42. registeredName "http://www.research.ibm.com/quantum/" // -> Some "ibm"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement