url-cache.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ;;; url-cache.el --- Uniform Resource Locator retrieval tool
  2. ;; Copyright (C) 1996-1999, 2004-2012 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes, hypermedia
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'url-parse)
  17. (require 'url-util)
  18. (require 'url) ;E.g. for url-configuration-directory.
  19. (defcustom url-cache-directory
  20. (expand-file-name "cache" url-configuration-directory)
  21. "The directory where cache files should be stored."
  22. :type 'directory
  23. :group 'url-file)
  24. (defcustom url-cache-expire-time 3600
  25. "Default maximum time in seconds before cache files expire.
  26. Used by the function `url-cache-expired'."
  27. :version "24.1"
  28. :type 'integer
  29. :group 'url-cache)
  30. ;; Cache manager
  31. (defun url-cache-file-writable-p (file)
  32. "Follows the documentation of `file-writable-p', unlike `file-writable-p'."
  33. (and (file-writable-p file)
  34. (if (file-exists-p file)
  35. (not (file-directory-p file))
  36. (file-directory-p (file-name-directory file)))))
  37. (defun url-cache-prepare (file)
  38. "Makes it possible to cache data in FILE.
  39. Creates any necessary parent directories, deleting any non-directory files
  40. that would stop this. Returns nil if parent directories can not be
  41. created. If FILE already exists as a non-directory, it changes
  42. permissions of FILE or deletes FILE to make it possible to write a new
  43. version of FILE. Returns nil if this can not be done, or if FILE already
  44. exists as a directory. Otherwise, returns t, indicating that
  45. FILE can be created or overwritten."
  46. (cond
  47. ((url-cache-file-writable-p file)
  48. t)
  49. ((file-directory-p file)
  50. nil)
  51. (t
  52. (condition-case ()
  53. (or (make-directory (file-name-directory file) t) t)
  54. (error nil)))))
  55. ;;;###autoload
  56. (defun url-store-in-cache (&optional buff)
  57. "Store buffer BUFF in the cache."
  58. (with-current-buffer (get-buffer (or buff (current-buffer)))
  59. (let ((fname (url-cache-create-filename (url-view-url t))))
  60. (if (url-cache-prepare fname)
  61. (let ((coding-system-for-write 'binary))
  62. (write-region (point-min) (point-max) fname nil 5))))))
  63. (defun url-fetch-from-cache (url)
  64. "Fetch URL from cache and return a buffer with the content."
  65. (with-current-buffer (generate-new-buffer " *temp*")
  66. (url-cache-extract (url-cache-create-filename url))
  67. (current-buffer)))
  68. ;;;###autoload
  69. (defun url-is-cached (url)
  70. "Return non-nil if the URL is cached.
  71. The actual return value is the last modification time of the cache file."
  72. (let* ((fname (url-cache-create-filename url))
  73. (attribs (file-attributes fname)))
  74. (and fname ; got a filename
  75. (file-exists-p fname) ; file exists
  76. (not (eq (nth 0 attribs) t)) ; Its not a directory
  77. (nth 5 attribs)))) ; Can get last mod-time
  78. (defun url-cache-create-filename-human-readable (url)
  79. "Return a filename in the local cache for URL."
  80. (if url
  81. (let* ((urlobj (url-generic-parse-url url))
  82. (protocol (url-type urlobj))
  83. (hostname (url-host urlobj))
  84. (host-components
  85. (cons
  86. (user-real-login-name)
  87. (cons (or protocol "file")
  88. (reverse (split-string (or hostname "localhost")
  89. "\\.")))))
  90. (fname (url-filename urlobj)))
  91. (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/))
  92. (setq fname (substring fname 1 nil)))
  93. (if fname
  94. (let ((slash nil))
  95. (setq fname
  96. (mapconcat
  97. (function
  98. (lambda (x)
  99. (cond
  100. ((and (= ?/ x) slash)
  101. (setq slash nil)
  102. "%2F")
  103. ((= ?/ x)
  104. (setq slash t)
  105. "/")
  106. (t
  107. (setq slash nil)
  108. (char-to-string x))))) fname ""))))
  109. (setq fname (and fname
  110. (mapconcat
  111. (function (lambda (x)
  112. (if (= x ?~) "" (char-to-string x))))
  113. fname ""))
  114. fname (cond
  115. ((null fname) nil)
  116. ((or (string= "" fname) (string= "/" fname))
  117. url-directory-index-file)
  118. ((= (string-to-char fname) ?/)
  119. (if (string= (substring fname -1 nil) "/")
  120. (concat fname url-directory-index-file)
  121. (substring fname 1 nil)))
  122. (t
  123. (if (string= (substring fname -1 nil) "/")
  124. (concat fname url-directory-index-file)
  125. fname))))
  126. (and fname
  127. (expand-file-name fname
  128. (expand-file-name
  129. (mapconcat 'identity host-components "/")
  130. url-cache-directory))))))
  131. (defun url-cache-create-filename-using-md5 (url)
  132. "Create a cached filename using MD5.
  133. Very fast if you have an `md5' primitive function, suitably fast otherwise."
  134. (require 'md5)
  135. (if url
  136. (let* ((checksum (md5 url))
  137. (urlobj (url-generic-parse-url url))
  138. (protocol (url-type urlobj))
  139. (hostname (url-host urlobj))
  140. (host-components
  141. (cons
  142. (user-real-login-name)
  143. (cons (or protocol "file")
  144. (nreverse
  145. (delq nil
  146. (split-string (or hostname "localhost")
  147. "\\."))))))
  148. (fname (url-filename urlobj)))
  149. (and fname
  150. (expand-file-name checksum
  151. (expand-file-name
  152. (mapconcat 'identity host-components "/")
  153. url-cache-directory))))))
  154. (defcustom url-cache-creation-function 'url-cache-create-filename-using-md5
  155. "What function to use to create a cached filename."
  156. :type '(choice (const :tag "MD5 of filename (low collision rate)"
  157. :value url-cache-create-filename-using-md5)
  158. (const :tag "Human readable filenames (higher collision rate)"
  159. :value url-cache-create-filename-human-readable)
  160. (function :tag "Other"))
  161. :group 'url-cache)
  162. (defun url-cache-create-filename (url)
  163. (funcall url-cache-creation-function
  164. ;; We need to parse+recreate in order to remove the default port
  165. ;; if it has been specified: e.g. http://www.example.com:80 will
  166. ;; be transcoded as http://www.example.com
  167. (url-recreate-url
  168. (if (vectorp url) url
  169. (url-generic-parse-url url)))))
  170. ;;;###autoload
  171. (defun url-cache-extract (fnam)
  172. "Extract FNAM from the local disk cache."
  173. (erase-buffer)
  174. (set-buffer-multibyte nil)
  175. (insert-file-contents-literally fnam))
  176. (defun url-cache-expired (url &optional expire-time)
  177. "Return non-nil if a cached URL is older than EXPIRE-TIME seconds.
  178. The default value of EXPIRE-TIME is `url-cache-expire-time'.
  179. If `url-standalone-mode' is non-nil, cached items never expire."
  180. (if url-standalone-mode
  181. (not (file-exists-p (url-cache-create-filename url)))
  182. (let ((cache-time (url-is-cached url)))
  183. (or (not cache-time)
  184. (time-less-p
  185. (time-add
  186. cache-time
  187. (seconds-to-time (or expire-time url-cache-expire-time)))
  188. (current-time))))))
  189. (defun url-cache-prune-cache (&optional directory)
  190. "Remove all expired files from the cache.
  191. `url-cache-expire-time' says how old a file has to be to be
  192. considered \"expired\"."
  193. (let ((current-time (current-time))
  194. (total-files 0)
  195. (deleted-files 0))
  196. (setq directory (or directory url-cache-directory))
  197. (when (file-exists-p directory)
  198. (dolist (file (directory-files directory t))
  199. (unless (member (file-name-nondirectory file) '("." ".."))
  200. (setq total-files (1+ total-files))
  201. (cond
  202. ((file-directory-p file)
  203. (when (url-cache-prune-cache file)
  204. (setq deleted-files (1+ deleted-files))))
  205. ((time-less-p
  206. (time-add
  207. (nth 5 (file-attributes file))
  208. (seconds-to-time url-cache-expire-time))
  209. current-time)
  210. (delete-file file)
  211. (setq deleted-files (1+ deleted-files))))))
  212. (if (< deleted-files total-files)
  213. nil
  214. (delete-directory directory)
  215. t))))
  216. (provide 'url-cache)
  217. ;;; url-cache.el ends here