Mercurial > emacs
annotate lisp/url/url-cache.el @ 110682:ba6a1c230532
gnus-html.el (gnus-html-prefetch-images): Adjust regexp to avoid regexp backtrace overflows.
author | Katsumi Yamaoka <yamaoka@jpl.org> |
---|---|
date | Fri, 01 Oct 2010 13:33:03 +0000 |
parents | 5874484c8de5 |
children | dab4cfd7ea71 |
rev | line source |
---|---|
54695 | 1 ;;; url-cache.el --- Uniform Resource Locator retrieval tool |
57612 | 2 |
110522
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, 2005, 2006, 2007, 2008, |
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
4 ;; 2009, 2010 Free Software Foundation, Inc. |
57612 | 5 |
54695 | 6 ;; Keywords: comm, data, processes, hypermedia |
7 | |
57612 | 8 ;; This file is part of GNU Emacs. |
9 | |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
57612 | 11 ;; it under the terms of the GNU General Public License as published by |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
13 ;; (at your option) any later version. |
57612 | 14 |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
57612 | 22 |
23 ;;; Code: | |
24 | |
54695 | 25 (require 'url-parse) |
54793
4051557a5374
(url-util): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
26 (require 'url-util) |
67974 | 27 (require 'url) ;E.g. for url-configuration-directory. |
54695 | 28 |
29 (defcustom url-cache-directory | |
30 (expand-file-name "cache" url-configuration-directory) | |
110307 | 31 "The directory where cache files should be stored." |
54695 | 32 :type 'directory |
33 :group 'url-file) | |
34 | |
110490 | 35 (defcustom url-cache-expire-time 3600 |
110493
4dea687a331f
* lisp/url/url-cache.el (url-cache-expire-time): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110492
diff
changeset
|
36 "Default maximum time in seconds before cache files expire. |
4dea687a331f
* lisp/url/url-cache.el (url-cache-expire-time): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110492
diff
changeset
|
37 Used by the function `url-cache-expired'." |
110490 | 38 :version "24.1" |
39 :type 'integer | |
40 :group 'url-cache) | |
41 | |
54695 | 42 ;; Cache manager |
43 (defun url-cache-file-writable-p (file) | |
44 "Follows the documentation of `file-writable-p', unlike `file-writable-p'." | |
45 (and (file-writable-p file) | |
46 (if (file-exists-p file) | |
47 (not (file-directory-p file)) | |
48 (file-directory-p (file-name-directory file))))) | |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
49 |
54695 | 50 (defun url-cache-prepare (file) |
51 "Makes it possible to cache data in FILE. | |
52 Creates any necessary parent directories, deleting any non-directory files | |
53 that would stop this. Returns nil if parent directories can not be | |
54 created. If FILE already exists as a non-directory, it changes | |
55 permissions of FILE or deletes FILE to make it possible to write a new | |
96486
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
56 version of FILE. Returns nil if this can not be done, or if FILE already |
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
57 exists as a directory. Otherwise, returns t, indicating that |
54695 | 58 FILE can be created or overwritten." |
59 (cond | |
60 ((url-cache-file-writable-p file) | |
61 t) | |
62 ((file-directory-p file) | |
63 nil) | |
64 (t | |
65 (condition-case () | |
66 (or (make-directory (file-name-directory file) t) t) | |
67 (error nil))))) | |
68 | |
69 ;;;###autoload | |
70 (defun url-store-in-cache (&optional buff) | |
71 "Store buffer BUFF in the cache." | |
110343
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
72 (with-current-buffer (get-buffer (or buff (current-buffer))) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
73 (let ((fname (url-cache-create-filename (url-view-url t)))) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
74 (if (url-cache-prepare fname) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
75 (let ((coding-system-for-write 'binary)) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
76 (write-region (point-min) (point-max) fname nil 5)))))) |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
77 |
110430
838a8c24850f
* lisp/url/url-cache.el (url-fetch-from-cache): New function.
Glenn Morris <rgm@gnu.org>
parents:
110357
diff
changeset
|
78 (defun url-fetch-from-cache (url) |
838a8c24850f
* lisp/url/url-cache.el (url-fetch-from-cache): New function.
Glenn Morris <rgm@gnu.org>
parents:
110357
diff
changeset
|
79 "Fetch URL from cache and return a buffer with the content." |
838a8c24850f
* lisp/url/url-cache.el (url-fetch-from-cache): New function.
Glenn Morris <rgm@gnu.org>
parents:
110357
diff
changeset
|
80 (with-current-buffer (generate-new-buffer " *temp*") |
838a8c24850f
* lisp/url/url-cache.el (url-fetch-from-cache): New function.
Glenn Morris <rgm@gnu.org>
parents:
110357
diff
changeset
|
81 (url-cache-extract (url-cache-create-filename url)) |
838a8c24850f
* lisp/url/url-cache.el (url-fetch-from-cache): New function.
Glenn Morris <rgm@gnu.org>
parents:
110357
diff
changeset
|
82 (current-buffer))) |
838a8c24850f
* lisp/url/url-cache.el (url-fetch-from-cache): New function.
Glenn Morris <rgm@gnu.org>
parents:
110357
diff
changeset
|
83 |
54695 | 84 ;;;###autoload |
85 (defun url-is-cached (url) | |
110522
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
86 "Return non-nil if the URL is cached. |
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
87 The actual return value is the last modification time of the cache file." |
54695 | 88 (let* ((fname (url-cache-create-filename url)) |
89 (attribs (file-attributes fname))) | |
90 (and fname ; got a filename | |
91 (file-exists-p fname) ; file exists | |
92 (not (eq (nth 0 attribs) t)) ; Its not a directory | |
93 (nth 5 attribs)))) ; Can get last mod-time | |
94 | |
95 (defun url-cache-create-filename-human-readable (url) | |
96486
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
96 "Return a filename in the local cache for URL." |
54695 | 97 (if url |
110578 | 98 (let* ((urlobj (url-generic-parse-url url)) |
54695 | 99 (protocol (url-type urlobj)) |
100 (hostname (url-host urlobj)) | |
101 (host-components | |
102 (cons | |
103 (user-real-login-name) | |
104 (cons (or protocol "file") | |
105 (reverse (split-string (or hostname "localhost") | |
106 (eval-when-compile | |
107 (regexp-quote "."))))))) | |
108 (fname (url-filename urlobj))) | |
109 (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/)) | |
110 (setq fname (substring fname 1 nil))) | |
111 (if fname | |
112 (let ((slash nil)) | |
113 (setq fname | |
114 (mapconcat | |
115 (function | |
116 (lambda (x) | |
117 (cond | |
118 ((and (= ?/ x) slash) | |
119 (setq slash nil) | |
120 "%2F") | |
121 ((= ?/ x) | |
122 (setq slash t) | |
123 "/") | |
124 (t | |
125 (setq slash nil) | |
126 (char-to-string x))))) fname "")))) | |
127 | |
128 (setq fname (and fname | |
129 (mapconcat | |
130 (function (lambda (x) | |
131 (if (= x ?~) "" (char-to-string x)))) | |
132 fname "")) | |
133 fname (cond | |
134 ((null fname) nil) | |
135 ((or (string= "" fname) (string= "/" fname)) | |
136 url-directory-index-file) | |
137 ((= (string-to-char fname) ?/) | |
138 (if (string= (substring fname -1 nil) "/") | |
139 (concat fname url-directory-index-file) | |
140 (substring fname 1 nil))) | |
141 (t | |
142 (if (string= (substring fname -1 nil) "/") | |
143 (concat fname url-directory-index-file) | |
144 fname)))) | |
145 (and fname | |
146 (expand-file-name fname | |
147 (expand-file-name | |
148 (mapconcat 'identity host-components "/") | |
149 url-cache-directory)))))) | |
150 | |
151 (defun url-cache-create-filename-using-md5 (url) | |
152 "Create a cached filename using MD5. | |
54793
4051557a5374
(url-util): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
153 Very fast if you have an `md5' primitive function, suitably fast otherwise." |
54695 | 154 (require 'md5) |
155 (if url | |
110578 | 156 (let* ((checksum (md5 url)) |
54695 | 157 (urlobj (url-generic-parse-url url)) |
158 (protocol (url-type urlobj)) | |
159 (hostname (url-host urlobj)) | |
160 (host-components | |
161 (cons | |
162 (user-real-login-name) | |
163 (cons (or protocol "file") | |
164 (nreverse | |
165 (delq nil | |
166 (split-string (or hostname "localhost") | |
167 (eval-when-compile | |
168 (regexp-quote ".")))))))) | |
169 (fname (url-filename urlobj))) | |
170 (and fname | |
171 (expand-file-name checksum | |
172 (expand-file-name | |
173 (mapconcat 'identity host-components "/") | |
174 url-cache-directory)))))) | |
175 | |
176 (defcustom url-cache-creation-function 'url-cache-create-filename-using-md5 | |
110307 | 177 "What function to use to create a cached filename." |
54695 | 178 :type '(choice (const :tag "MD5 of filename (low collision rate)" |
179 :value url-cache-create-filename-using-md5) | |
180 (const :tag "Human readable filenames (higher collision rate)" | |
181 :value url-cache-create-filename-human-readable) | |
182 (function :tag "Other")) | |
183 :group 'url-cache) | |
184 | |
185 (defun url-cache-create-filename (url) | |
110578 | 186 (funcall url-cache-creation-function |
187 ;; We need to parse+recreate in order to remove the default port | |
188 ;; if it has been specified: e.g. http://www.example.com:80 will | |
189 ;; be transcoded as http://www.example.com | |
190 (url-recreate-url | |
191 (if (vectorp url) url | |
192 (url-generic-parse-url url))))) | |
54695 | 193 |
194 ;;;###autoload | |
195 (defun url-cache-extract (fnam) | |
96486
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
196 "Extract FNAM from the local disk cache." |
54695 | 197 (erase-buffer) |
198 (insert-file-contents-literally fnam)) | |
199 | |
110490 | 200 (defun url-cache-expired (url &optional expire-time) |
110491
3501c2b9e719
* url-cache.el (url-cache-expired): Don't autoload. Tweak previous change.
Glenn Morris <rgm@gnu.org>
parents:
110490
diff
changeset
|
201 "Return non-nil if a cached URL is older than EXPIRE-TIME seconds. |
3501c2b9e719
* url-cache.el (url-cache-expired): Don't autoload. Tweak previous change.
Glenn Morris <rgm@gnu.org>
parents:
110490
diff
changeset
|
202 The default value of EXPIRE-TIME is `url-cache-expire-time'. |
3501c2b9e719
* url-cache.el (url-cache-expired): Don't autoload. Tweak previous change.
Glenn Morris <rgm@gnu.org>
parents:
110490
diff
changeset
|
203 If `url-standalone-mode' is non-nil, cached items never expire." |
3501c2b9e719
* url-cache.el (url-cache-expired): Don't autoload. Tweak previous change.
Glenn Morris <rgm@gnu.org>
parents:
110490
diff
changeset
|
204 (if url-standalone-mode |
3501c2b9e719
* url-cache.el (url-cache-expired): Don't autoload. Tweak previous change.
Glenn Morris <rgm@gnu.org>
parents:
110490
diff
changeset
|
205 (not (file-exists-p (url-cache-create-filename url))) |
3501c2b9e719
* url-cache.el (url-cache-expired): Don't autoload. Tweak previous change.
Glenn Morris <rgm@gnu.org>
parents:
110490
diff
changeset
|
206 (let ((cache-time (url-is-cached url))) |
110492 | 207 (or (not cache-time) |
208 (time-less-p | |
209 (time-add | |
210 cache-time | |
211 (seconds-to-time (or expire-time url-cache-expire-time))) | |
212 (current-time)))))) | |
54695 | 213 |
214 (provide 'url-cache) | |
54699 | 215 |
67851
eebc9bf86f06
(url-store-in-cache): Use save-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
64748
diff
changeset
|
216 ;; arch-tag: 95b050a6-8e81-4f23-8e63-191b9d1d657c |
57612 | 217 ;;; url-cache.el ends here |