annotate lisp/w3/url-cache.el @ 118:7d55a9ba150c r20-1b11

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