annotate lisp/url/url-cache.el @ 111843:94c9743593b9

nnir.el (nnir-run-gmane): Restore sub-optimal test for gmane server. (nnir-request-article): Improve article retrieval.
author Katsumi Yamaoka <yamaoka@jpl.org>
date Tue, 07 Dec 2010 14:10:11 +0000
parents dab4cfd7ea71
children 417b1e4d63cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1 ;;; url-cache.el --- Uniform Resource Locator retrieval tool
57612
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
5
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
6 ;; Keywords: comm, data, processes, hypermedia
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
7
57612
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
8 ;; This file is part of GNU Emacs.
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
14
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
18 ;; GNU General Public License for more details.
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
19
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
22
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
23 ;;; Code:
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
24
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
25 (require 'url-parse)
54793
4051557a5374 (url-util): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54770
diff changeset
26 (require 'url-util)
67974
c307ab54eb58 Require `url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 67851
diff changeset
27 (require 'url) ;E.g. for url-configuration-directory.
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
28
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
29 (defcustom url-cache-directory
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
30 (expand-file-name "cache" url-configuration-directory)
110307
5495085c9b6c lisp/url tiny fixes.
Glenn Morris <rgm@gnu.org>
parents: 106815
diff changeset
31 "The directory where cache files should be stored."
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
32 :type 'directory
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
33 :group 'url-file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
34
110490
7c3194dcf52d Small url-cache update.
Glenn Morris <rgm@gnu.org>
parents: 110430
diff changeset
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
7c3194dcf52d Small url-cache update.
Glenn Morris <rgm@gnu.org>
parents: 110430
diff changeset
38 :version "24.1"
7c3194dcf52d Small url-cache update.
Glenn Morris <rgm@gnu.org>
parents: 110430
diff changeset
39 :type 'integer
7c3194dcf52d Small url-cache update.
Glenn Morris <rgm@gnu.org>
parents: 110430
diff changeset
40 :group 'url-cache)
7c3194dcf52d Small url-cache update.
Glenn Morris <rgm@gnu.org>
parents: 110430
diff changeset
41
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
42 ;; Cache manager
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
43 (defun url-cache-file-writable-p (file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
44 "Follows the documentation of `file-writable-p', unlike `file-writable-p'."
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
45 (and (file-writable-p file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
46 (if (file-exists-p file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
47 (not (file-directory-p file))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
50 (defun url-cache-prepare (file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
51 "Makes it possible to cache data in FILE.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
52 Creates any necessary parent directories, deleting any non-directory files
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
53 that would stop this. Returns nil if parent directories can not be
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
54 created. If FILE already exists as a non-directory, it changes
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
58 FILE can be created or overwritten."
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
59 (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
60 ((url-cache-file-writable-p file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
61 t)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
62 ((file-directory-p file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
63 nil)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
64 (t
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
65 (condition-case ()
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
66 (or (make-directory (file-name-directory file) t) t)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
67 (error nil)))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
68
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
69 ;;;###autoload
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
70 (defun url-store-in-cache (&optional buff)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
84 ;;;###autoload
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
88 (let* ((fname (url-cache-create-filename url))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
89 (attribs (file-attributes fname)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
90 (and fname ; got a filename
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
91 (file-exists-p fname) ; file exists
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
92 (not (eq (nth 0 attribs) t)) ; Its not a directory
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
93 (nth 5 attribs)))) ; Can get last mod-time
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
94
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
97 (if url
110578
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
98 (let* ((urlobj (url-generic-parse-url url))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
99 (protocol (url-type urlobj))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
100 (hostname (url-host urlobj))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
101 (host-components
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
102 (cons
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
103 (user-real-login-name)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
104 (cons (or protocol "file")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
105 (reverse (split-string (or hostname "localhost")
110744
dab4cfd7ea71 Cosmetic changes for some lisp/url files.
Glenn Morris <rgm@gnu.org>
parents: 110578
diff changeset
106 "\\.")))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
107 (fname (url-filename urlobj)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
108 (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
109 (setq fname (substring fname 1 nil)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
110 (if fname
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
111 (let ((slash nil))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
112 (setq fname
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
113 (mapconcat
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
114 (function
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
115 (lambda (x)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
116 (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
117 ((and (= ?/ x) slash)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
118 (setq slash nil)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
119 "%2F")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
120 ((= ?/ x)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
121 (setq slash t)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
122 "/")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
123 (t
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
124 (setq slash nil)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
125 (char-to-string x))))) fname ""))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
126
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
127 (setq fname (and fname
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
128 (mapconcat
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
129 (function (lambda (x)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
130 (if (= x ?~) "" (char-to-string x))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
131 fname ""))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
132 fname (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
133 ((null fname) nil)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
134 ((or (string= "" fname) (string= "/" fname))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
135 url-directory-index-file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
136 ((= (string-to-char fname) ?/)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
137 (if (string= (substring fname -1 nil) "/")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
138 (concat fname url-directory-index-file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
139 (substring fname 1 nil)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
140 (t
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
141 (if (string= (substring fname -1 nil) "/")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
142 (concat fname url-directory-index-file)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
143 fname))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
144 (and fname
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
145 (expand-file-name fname
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
146 (expand-file-name
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
147 (mapconcat 'identity host-components "/")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
148 url-cache-directory))))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
149
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
150 (defun url-cache-create-filename-using-md5 (url)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
151 "Create a cached filename using MD5.
54793
4051557a5374 (url-util): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54770
diff changeset
152 Very fast if you have an `md5' primitive function, suitably fast otherwise."
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
153 (require 'md5)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
154 (if url
110578
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
155 (let* ((checksum (md5 url))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
156 (urlobj (url-generic-parse-url url))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
157 (protocol (url-type urlobj))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
158 (hostname (url-host urlobj))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
159 (host-components
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
160 (cons
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
161 (user-real-login-name)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
162 (cons (or protocol "file")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
163 (nreverse
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
164 (delq nil
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
165 (split-string (or hostname "localhost")
110744
dab4cfd7ea71 Cosmetic changes for some lisp/url files.
Glenn Morris <rgm@gnu.org>
parents: 110578
diff changeset
166 "\\."))))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
167 (fname (url-filename urlobj)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
168 (and fname
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
169 (expand-file-name checksum
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
170 (expand-file-name
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
171 (mapconcat 'identity host-components "/")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
172 url-cache-directory))))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
173
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
174 (defcustom url-cache-creation-function 'url-cache-create-filename-using-md5
110307
5495085c9b6c lisp/url tiny fixes.
Glenn Morris <rgm@gnu.org>
parents: 106815
diff changeset
175 "What function to use to create a cached filename."
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
176 :type '(choice (const :tag "MD5 of filename (low collision rate)"
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
177 :value url-cache-create-filename-using-md5)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
178 (const :tag "Human readable filenames (higher collision rate)"
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
179 :value url-cache-create-filename-human-readable)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
180 (function :tag "Other"))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
181 :group 'url-cache)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
182
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
183 (defun url-cache-create-filename (url)
110578
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
184 (funcall url-cache-creation-function
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
185 ;; We need to parse+recreate in order to remove the default port
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
186 ;; if it has been specified: e.g. http://www.example.com:80 will
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
187 ;; be transcoded as http://www.example.com
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
188 (url-recreate-url
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
189 (if (vectorp url) url
5874484c8de5 Small url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110533
diff changeset
190 (url-generic-parse-url url)))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
191
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
192 ;;;###autoload
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
193 (defun url-cache-extract (fnam)
96486
7369ded3b436 Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents: 94668
diff changeset
194 "Extract FNAM from the local disk cache."
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
195 (erase-buffer)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
196 (insert-file-contents-literally fnam))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
197
110490
7c3194dcf52d Small url-cache update.
Glenn Morris <rgm@gnu.org>
parents: 110430
diff changeset
198 (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
199 "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
200 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
201 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
202 (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
203 (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
204 (let ((cache-time (url-is-cached url)))
110492
874fdd55f8e9 Fix previous url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110491
diff changeset
205 (or (not cache-time)
874fdd55f8e9 Fix previous url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110491
diff changeset
206 (time-less-p
874fdd55f8e9 Fix previous url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110491
diff changeset
207 (time-add
874fdd55f8e9 Fix previous url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110491
diff changeset
208 cache-time
874fdd55f8e9 Fix previous url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110491
diff changeset
209 (seconds-to-time (or expire-time url-cache-expire-time)))
874fdd55f8e9 Fix previous url-cache fix.
Glenn Morris <rgm@gnu.org>
parents: 110491
diff changeset
210 (current-time))))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
211
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
212 (provide 'url-cache)
54699
7784ae10206d Resolve CVS conflicts
Miles Bader <miles@gnu.org>
parents: 54695
diff changeset
213
67851
eebc9bf86f06 (url-store-in-cache): Use save-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 64748
diff changeset
214 ;; arch-tag: 95b050a6-8e81-4f23-8e63-191b9d1d657c
57612
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57427
diff changeset
215 ;;; url-cache.el ends here