url-domsuf.el 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;;; url-domsuf.el --- Say what domain names can have cookies set.
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: comm, data, processes, hypermedia
  5. ;; This file is part of GNU Emacs.
  6. ;;
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; The rules for what domains can have cookies set is defined here:
  19. ;; http://publicsuffix.org/list/
  20. ;;; Code:
  21. (defvar url-domsuf-domains nil)
  22. (defun url-domsuf-parse-file ()
  23. (with-temp-buffer
  24. (insert-file-contents
  25. (expand-file-name "publicsuffix.txt" data-directory))
  26. (let ((domains nil)
  27. domain exception)
  28. (while (not (eobp))
  29. (when (not (looking-at "[/\n\t ]"))
  30. ;; !pref.aichi.jp means that it's allowed.
  31. (if (not (eq (following-char) ?!))
  32. (setq exception nil)
  33. (setq exception t)
  34. (forward-char 1))
  35. (setq domain (buffer-substring (point) (line-end-position)))
  36. (cond
  37. ((string-match "\\`\\*\\." domain)
  38. (setq domain (substring domain 2))
  39. (push (cons domain (1+ (length (split-string domain "[.]"))))
  40. domains))
  41. (exception
  42. (push (cons domain t) domains))
  43. (t
  44. (push (cons domain nil) domains))))
  45. (forward-line 1))
  46. (setq url-domsuf-domains (nreverse domains)))))
  47. (defun url-domsuf-cookie-allowed-p (domain)
  48. (unless url-domsuf-domains
  49. (url-domsuf-parse-file))
  50. (let* ((allowedp t)
  51. (domain-bits (split-string domain "[.]"))
  52. (length (length domain-bits))
  53. (upper-domain (mapconcat 'identity (cdr domain-bits) "."))
  54. entry modifier)
  55. (dolist (elem url-domsuf-domains)
  56. (setq entry (car elem)
  57. modifier (cdr elem))
  58. (cond
  59. ;; "com"
  60. ((and (null modifier)
  61. (string= domain entry))
  62. (setq allowedp nil))
  63. ;; "!pref.hokkaido.jp"
  64. ((and (eq modifier t)
  65. (string= domain entry))
  66. (setq allowedp t))
  67. ;; "*.ar"
  68. ((and (numberp modifier)
  69. (= length modifier)
  70. (string= entry upper-domain))
  71. (setq allowedp nil))))
  72. allowedp))
  73. ;; Tests:
  74. ;; (url-domsuf-cookie-allowed-p "com") => nil
  75. ;; (url-domsuf-cookie-allowed-p "foo.bar.ar") => t
  76. ;; (url-domsuf-cookie-allowed-p "bar.ar") => nil
  77. ;; (url-domsuf-cookie-allowed-p "co.uk") => nil
  78. ;; (url-domsuf-cookie-allowed-p "foo.bar.hokkaido.jo") => t
  79. ;; (url-domsuf-cookie-allowed-p "bar.hokkaido.jp") => nil
  80. ;; (url-domsuf-cookie-allowed-p "pref.hokkaido.jp") => t
  81. (provide 'url-domsuf)
  82. ;;; url-domsuf.el ends here