26
|
1 ;;; url-cache.el --- Uniform Resource Locator retrieval tool
|
|
2 ;; Author: wmperry
|
118
|
3 ;; Created: 1997/04/03 21:04:08
|
|
4 ;; Version: 1.11
|
26
|
5 ;; Keywords: comm, data, processes, hypermedia
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
8 ;;; Copyright (c) 1993-1996 by William M. Perry (wmperry@cs.indiana.edu)
|
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
|
10 ;;;
|
|
11 ;;; This file is not part of GNU Emacs, but the same permissions apply.
|
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28 (require 'md5)
|
|
29
|
30
|
30 (defvar url-cache-directory "~/.w3/cache/"
|
|
31 "*The directory where cache files should be stored.")
|
|
32
|
26
|
33 ;; Cache manager
|
|
34 (defun url-cache-file-writable-p (file)
|
|
35 "Follows the documentation of file-writable-p, unlike file-writable-p."
|
|
36 (and (file-writable-p file)
|
|
37 (if (file-exists-p file)
|
|
38 (not (file-directory-p file))
|
|
39 (file-directory-p (file-name-directory file)))))
|
|
40
|
30
|
41 (defun url-cache-prepare (file)
|
26
|
42 "Makes it possible to cache data in FILE.
|
|
43 Creates any necessary parent directories, deleting any non-directory files
|
|
44 that would stop this. Returns nil if parent directories can not be
|
|
45 created. If FILE already exists as a non-directory, it changes
|
|
46 permissions of FILE or deletes FILE to make it possible to write a new
|
|
47 version of FILE. Returns nil if this can not be done. Returns nil if
|
|
48 FILE already exists as a directory. Otherwise, returns t, indicating that
|
|
49 FILE can be created or overwritten."
|
|
50 (cond
|
|
51 ((url-cache-file-writable-p file)
|
|
52 t)
|
|
53 ((file-directory-p file)
|
|
54 nil)
|
|
55 (t
|
32
|
56 (condition-case ()
|
|
57 (or (make-directory (file-name-directory file) t) t)
|
|
58 (error nil)))))
|
26
|
59
|
|
60 (defvar url-cache-ignored-protocols
|
|
61 '("www" "about" "https" "mailto")
|
|
62 "*A list of protocols that we should never cache.")
|
|
63
|
|
64 (defun url-cache-cachable-p (obj)
|
|
65 ;; return t iff the current buffer is cachable
|
|
66 (cond
|
32
|
67 ((not url-automatic-caching) ; User doesn't want to cache
|
|
68 nil)
|
26
|
69 ((null obj) ; Something horribly confused
|
|
70 nil)
|
|
71 ((member (url-type obj) url-cache-ignored-protocols)
|
|
72 ;; We have been told to ignore this type of object
|
|
73 nil)
|
|
74 ((and (member (url-type obj) '("file" "ftp")) (not (url-host obj)))
|
|
75 ;; We never want to cache local files... what's the point?
|
|
76 nil)
|
|
77 ((member (url-type obj) '("http" "https"))
|
|
78 (let* ((status (cdr-safe (assoc "status" url-current-mime-headers)))
|
|
79 (class (if status (/ status 100) 0)))
|
30
|
80 (cond
|
|
81 ((string-match (eval-when-compile (regexp-quote "?"))
|
|
82 (url-filename obj))
|
|
83 nil)
|
|
84 ((= class 2)
|
|
85 (memq status '(200)))
|
|
86 (t nil))))
|
26
|
87 (t
|
|
88 nil)))
|
|
89
|
|
90 ;;;###autoload
|
|
91 (defun url-store-in-cache (&optional buff)
|
|
92 "Store buffer BUFF in the cache"
|
30
|
93 (if (not (and buff (get-buffer buff)))
|
26
|
94 nil
|
|
95 (save-excursion
|
|
96 (and buff (set-buffer buff))
|
|
97 (if (not (url-cache-cachable-p url-current-object))
|
|
98 nil
|
30
|
99 (let* ((fname (url-cache-create-filename (url-view-url t)))
|
26
|
100 (fname-hdr (concat fname ".hdr"))
|
|
101 (info (mapcar (function (lambda (var)
|
|
102 (cons (symbol-name var)
|
|
103 (symbol-value var))))
|
|
104 '( url-current-content-length
|
|
105 url-current-object
|
|
106 url-current-isindex
|
|
107 url-current-mime-encoding
|
|
108 url-current-mime-headers
|
|
109 url-current-mime-type
|
|
110 ))))
|
30
|
111 (cond ((and (url-cache-prepare fname)
|
|
112 (url-cache-prepare fname-hdr))
|
26
|
113 (write-region (point-min) (point-max) fname nil 5)
|
|
114 (set-buffer (get-buffer-create " *cache-tmp*"))
|
|
115 (erase-buffer)
|
|
116 (insert "(setq ")
|
|
117 (mapcar
|
|
118 (function
|
|
119 (lambda (x)
|
|
120 (insert (car x) " "
|
|
121 (cond ((null (setq x (cdr x))) "nil")
|
|
122 ((stringp x) (prin1-to-string x))
|
|
123 ((listp x) (concat "'" (prin1-to-string x)))
|
|
124 ((vectorp x) (prin1-to-string x))
|
|
125 ((numberp x) (int-to-string x))
|
|
126 (t "'???")) "\n")))
|
|
127 info)
|
|
128 (insert ")\n")
|
|
129 (write-region (point-min) (point-max) fname-hdr nil 5))))))))
|
|
130
|
|
131
|
|
132 ;;;###autoload
|
|
133 (defun url-is-cached (url)
|
|
134 "Return non-nil if the URL is cached."
|
30
|
135 (let* ((fname (url-cache-create-filename url))
|
26
|
136 (attribs (file-attributes fname)))
|
|
137 (and fname ; got a filename
|
|
138 (file-exists-p fname) ; file exists
|
|
139 (not (eq (nth 0 attribs) t)) ; Its not a directory
|
|
140 (nth 5 attribs)))) ; Can get last mod-time
|
|
141
|
30
|
142 (defun url-cache-create-filename-human-readable (url)
|
26
|
143 "Return a filename in the local cache for URL"
|
|
144 (if url
|
32
|
145 (let* ((url (if (vectorp url) (url-recreate-url url) url))
|
30
|
146 (urlobj (url-generic-parse-url url))
|
26
|
147 (protocol (url-type urlobj))
|
|
148 (hostname (url-host urlobj))
|
|
149 (host-components
|
|
150 (cons
|
|
151 (user-real-login-name)
|
|
152 (cons (or protocol "file")
|
118
|
153 (reverse (split-string (or hostname "localhost")
|
|
154 (eval-when-compile
|
|
155 (regexp-quote ".")))))))
|
26
|
156 (fname (url-filename urlobj)))
|
|
157 (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/))
|
|
158 (setq fname (substring fname 1 nil)))
|
|
159 (if fname
|
|
160 (let ((slash nil))
|
|
161 (setq fname
|
|
162 (mapconcat
|
|
163 (function
|
|
164 (lambda (x)
|
|
165 (cond
|
|
166 ((and (= ?/ x) slash)
|
|
167 (setq slash nil)
|
|
168 "%2F")
|
|
169 ((= ?/ x)
|
|
170 (setq slash t)
|
|
171 "/")
|
|
172 (t
|
|
173 (setq slash nil)
|
|
174 (char-to-string x))))) fname ""))))
|
|
175
|
|
176 (setq fname (and fname
|
|
177 (mapconcat
|
|
178 (function (lambda (x)
|
|
179 (if (= x ?~) "" (char-to-string x))))
|
|
180 fname ""))
|
|
181 fname (cond
|
|
182 ((null fname) nil)
|
|
183 ((or (string= "" fname) (string= "/" fname))
|
|
184 url-directory-index-file)
|
|
185 ((= (string-to-char fname) ?/)
|
|
186 (if (string= (substring fname -1 nil) "/")
|
|
187 (concat fname url-directory-index-file)
|
|
188 (substring fname 1 nil)))
|
|
189 (t
|
|
190 (if (string= (substring fname -1 nil) "/")
|
|
191 (concat fname url-directory-index-file)
|
|
192 fname))))
|
|
193 (and fname
|
|
194 (expand-file-name fname
|
|
195 (expand-file-name
|
|
196 (mapconcat 'identity host-components "/")
|
30
|
197 url-cache-directory))))))
|
|
198
|
|
199 (defun url-cache-create-filename-using-md5 (url)
|
|
200 "Create a cached filename using MD5.
|
|
201 Very fast if you are in XEmacs, suitably fast otherwise."
|
|
202 (if url
|
|
203 (let* ((checksum (md5 url))
|
32
|
204 (url (if (vectorp url) (url-recreate-url url) url))
|
30
|
205 (urlobj (url-generic-parse-url url))
|
|
206 (protocol (url-type urlobj))
|
|
207 (hostname (url-host urlobj))
|
|
208 (host-components
|
|
209 (cons
|
|
210 (user-real-login-name)
|
|
211 (cons (or protocol "file")
|
|
212 (nreverse
|
|
213 (delq nil
|
|
214 (split-string (or hostname "localhost")
|
|
215 (eval-when-compile
|
|
216 (regexp-quote "."))))))))
|
|
217 (fname (url-filename urlobj)))
|
|
218 (and fname
|
|
219 (expand-file-name checksum
|
|
220 (expand-file-name
|
|
221 (mapconcat 'identity host-components "/")
|
|
222 url-cache-directory))))))
|
|
223
|
|
224 (defvar url-cache-creation-function 'url-cache-create-filename-using-md5
|
|
225 "*What function to use to create a cached filename.")
|
|
226
|
|
227 (defun url-cache-create-filename (url)
|
|
228 (funcall url-cache-creation-function url))
|
26
|
229
|
|
230 ;;;###autoload
|
30
|
231 (defun url-cache-extract (fnam)
|
26
|
232 "Extract FNAM from the local disk cache"
|
|
233 (set-buffer (get-buffer-create url-working-buffer))
|
|
234 (erase-buffer)
|
|
235 (setq url-current-mime-viewer nil)
|
|
236 (insert-file-contents-literally fnam)
|
|
237 (load (concat (if (memq system-type '(ms-windows ms-dos os2))
|
|
238 (url-file-extension fnam t)
|
|
239 fnam) ".hdr") t t))
|
|
240
|
|
241 ;;;###autoload
|
|
242 (defun url-cache-expired (url mod)
|
|
243 "Return t iff a cached file has expired."
|
32
|
244 (let* ((urlobj (if (vectorp url) url (url-generic-parse-url url)))
|
|
245 (type (url-type urlobj)))
|
|
246 (cond
|
|
247 (url-standalone-mode
|
|
248 (not (file-exists-p (url-cache-create-filename url))))
|
|
249 ((string= type "http")
|
|
250 t)
|
|
251 ((member type '("file" "ftp"))
|
|
252 (if (or (equal mod '(0 0)) (not mod))
|
|
253 (return t)
|
|
254 (or (> (nth 0 mod) (nth 0 (current-time)))
|
|
255 (> (nth 1 mod) (nth 1 (current-time))))))
|
|
256 (t nil))))
|
26
|
257
|
|
258 (provide 'url-cache)
|