annotate lisp/w3/url-cache.el @ 26:441bb1e64a06 r19-15b96

Import from CVS: tag r19-15b96
author cvs
date Mon, 13 Aug 2007 08:51:32 +0200
parents
children ec9a17fef872
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
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
3 ;; Created: 1997/02/20 15:33:47
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
4 ;; Version: 1.3
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
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
30 ;; Cache manager
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
31 (defun url-cache-file-writable-p (file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
32 "Follows the documentation of file-writable-p, unlike file-writable-p."
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
33 (and (file-writable-p file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
34 (if (file-exists-p file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
35 (not (file-directory-p file))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
36 (file-directory-p (file-name-directory file)))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
37
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
38 (defun url-prepare-cache-for-file (file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
39 "Makes it possible to cache data in FILE.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
40 Creates any necessary parent directories, deleting any non-directory files
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
41 that would stop this. Returns nil if parent directories can not be
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
42 created. If FILE already exists as a non-directory, it changes
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
43 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
44 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
45 FILE already exists as a directory. Otherwise, returns t, indicating that
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
46 FILE can be created or overwritten."
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
47
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
48 ;; COMMENT: We don't delete directories because that requires
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
49 ;; recursively deleting the directories's contents, which might
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
50 ;; eliminate a substantial portion of the cache.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
51
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
52 (cond
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
53 ((url-cache-file-writable-p file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
54 t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
55 ((file-directory-p file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
56 nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
57 (t
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
58 (catch 'upcff-tag
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
59 (let ((dir (file-name-directory file))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
60 dir-parent dir-last-component)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
61 (if (string-equal dir file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
62 ;; *** Should I have a warning here?
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
63 ;; FILE must match a pattern like /foo/bar/, indicating it is a
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
64 ;; name only suitable for a directory. So presume we won't be
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
65 ;; able to overwrite FILE and return nil.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
66 (throw 'upcff-tag nil))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
67
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
68 ;; Make sure the containing directory exists, or throw a failure
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
69 ;; if we can't create it.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
70 (if (file-directory-p dir)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
71 nil
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
72 (or (fboundp 'make-directory)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
73 (throw 'upcff-tag nil))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
74 (make-directory dir t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
75 ;; make-directory silently fails if there is an obstacle, so
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
76 ;; we must verify its results.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
77 (if (file-directory-p dir)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
78 nil
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
79 ;; Look at prefixes of the path to find the obstacle that is
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
80 ;; stopping us from making the directory. Unfortunately, there
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
81 ;; is no portable function in Emacs to find the parent directory
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
82 ;; of a *directory*. So this code may not work on VMS.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
83 (while (progn
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
84 (if (eq ?/ (aref dir (1- (length dir))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
85 (setq dir (substring dir 0 -1))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
86 ;; Maybe we're on VMS where the syntax is different.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
87 (throw 'upcff-tag nil))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
88 (setq dir-parent (file-name-directory dir))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
89 (not (file-directory-p dir-parent)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
90 (setq dir dir-parent))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
91 ;; We have found the longest path prefix that exists as a
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
92 ;; directory. Deal with any obstacles in this directory.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
93 (if (file-exists-p dir)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
94 (condition-case nil
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
95 (delete-file dir)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
96 (error (throw 'upcff-tag nil))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
97 (if (file-exists-p dir)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
98 (throw 'upcff-tag nil))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
99 ;; Try making the directory again.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
100 (setq dir (file-name-directory file))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
101 (make-directory dir t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
102 (or (file-directory-p dir)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
103 (throw 'upcff-tag nil))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
104
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
105 ;; The containing directory exists. Let's see if there is
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
106 ;; something in the way in this directory.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
107 (if (url-cache-file-writable-p file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
108 (throw 'upcff-tag t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
109 (condition-case nil
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
110 (delete-file file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
111 (error (throw 'upcff-tag nil))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
112
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
113 ;; The return value, if we get this far.
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
114 (url-cache-file-writable-p file))))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
115
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
116 (defvar url-cache-ignored-protocols
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
117 '("www" "about" "https" "mailto")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
118 "*A list of protocols that we should never cache.")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
119
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
120 (defun url-cache-cachable-p (obj)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
121 ;; return t iff the current buffer is cachable
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
122 (cond
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
123 ((null obj) ; Something horribly confused
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
124 nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
125 ((member (url-type obj) url-cache-ignored-protocols)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
126 ;; We have been told to ignore this type of object
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
127 nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
128 ((and (member (url-type obj) '("file" "ftp")) (not (url-host obj)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
129 ;; We never want to cache local files... what's the point?
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
130 nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
131 ((member (url-type obj) '("http" "https"))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
132 (let* ((status (cdr-safe (assoc "status" url-current-mime-headers)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
133 (class (if status (/ status 100) 0)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
134 (case class
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
135 (2 ; Various 'OK' statuses
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
136 (memq status '(200)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
137 (otherwise nil))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
138 (t
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
139 nil)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
140
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
141 ;;;###autoload
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
142 (defun url-store-in-cache (&optional buff)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
143 "Store buffer BUFF in the cache"
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
144 (if (and buff (get-buffer buff))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
145 nil
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
146 (save-excursion
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
147 (and buff (set-buffer buff))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
148 (if (not (url-cache-cachable-p url-current-object))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
149 nil
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
150 (let* ((fname (url-create-cached-filename (url-view-url t)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
151 (fname-hdr (concat fname ".hdr"))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
152 (info (mapcar (function (lambda (var)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
153 (cons (symbol-name var)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
154 (symbol-value var))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
155 '( url-current-content-length
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
156 url-current-object
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
157 url-current-isindex
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
158 url-current-mime-encoding
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
159 url-current-mime-headers
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
160 url-current-mime-type
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
161 ))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
162 (cond ((and (url-prepare-cache-for-file fname)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
163 (url-prepare-cache-for-file fname-hdr))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
164 (write-region (point-min) (point-max) fname nil 5)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
165 (set-buffer (get-buffer-create " *cache-tmp*"))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
166 (erase-buffer)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
167 (insert "(setq ")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
168 (mapcar
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
169 (function
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
170 (lambda (x)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
171 (insert (car x) " "
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
172 (cond ((null (setq x (cdr x))) "nil")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
173 ((stringp x) (prin1-to-string x))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
174 ((listp x) (concat "'" (prin1-to-string x)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
175 ((vectorp x) (prin1-to-string x))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
176 ((numberp x) (int-to-string x))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
177 (t "'???")) "\n")))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
178 info)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
179 (insert ")\n")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
180 (write-region (point-min) (point-max) fname-hdr nil 5))))))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
181
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
182
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
183 ;;;###autoload
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
184 (defun url-is-cached (url)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
185 "Return non-nil if the URL is cached."
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
186 (let* ((fname (url-create-cached-filename url))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
187 (attribs (file-attributes fname)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
188 (and fname ; got a filename
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
189 (file-exists-p fname) ; file exists
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
190 (not (eq (nth 0 attribs) t)) ; Its not a directory
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
191 (nth 5 attribs)))) ; Can get last mod-time
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
192
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
193 (defun url-create-cached-filename-using-md5 (url)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
194 (if url
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
195 (expand-file-name (md5 url)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
196 (concat url-temporary-directory "/"
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
197 (user-real-login-name)))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
198
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
199 ;;;###autoload
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
200 (defun url-create-cached-filename (url)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
201 "Return a filename in the local cache for URL"
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
202 (if url
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
203 (let* ((url url)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
204 (urlobj (if (vectorp url)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
205 url
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
206 (url-generic-parse-url url)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
207 (protocol (url-type urlobj))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
208 (hostname (url-host urlobj))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
209 (host-components
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
210 (cons
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
211 (user-real-login-name)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
212 (cons (or protocol "file")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
213 (nreverse
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
214 (delq nil
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
215 (mm-string-to-tokens
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
216 (or hostname "localhost") ?.))))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
217 (fname (url-filename urlobj)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
218 (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
219 (setq fname (substring fname 1 nil)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
220 (if fname
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
221 (let ((slash nil))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
222 (setq fname
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
223 (mapconcat
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
224 (function
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
225 (lambda (x)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
226 (cond
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
227 ((and (= ?/ x) slash)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
228 (setq slash nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
229 "%2F")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
230 ((= ?/ x)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
231 (setq slash t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
232 "/")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
233 (t
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
234 (setq slash nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
235 (char-to-string x))))) fname ""))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
236
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
237 (if (and fname (memq system-type '(ms-windows ms-dos windows-nt))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
238 (string-match "\\([A-Za-z]\\):[/\\]" fname))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
239 (setq fname (concat (url-match fname 1) "/"
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
240 (substring fname (match-end 0)))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
241
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
242 (setq fname (and fname
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
243 (mapconcat
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
244 (function (lambda (x)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
245 (if (= x ?~) "" (char-to-string x))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
246 fname ""))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
247 fname (cond
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
248 ((null fname) nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
249 ((or (string= "" fname) (string= "/" fname))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
250 url-directory-index-file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
251 ((= (string-to-char fname) ?/)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
252 (if (string= (substring fname -1 nil) "/")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
253 (concat fname url-directory-index-file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
254 (substring fname 1 nil)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
255 (t
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
256 (if (string= (substring fname -1 nil) "/")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
257 (concat fname url-directory-index-file)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
258 fname))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
259
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
260 ;; Honor hideous 8.3 filename limitations on dos and windows
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
261 ;; we don't have to worry about this in Windows NT/95 (or OS/2?)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
262 (if (and fname (memq system-type '(ms-windows ms-dos)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
263 (let ((base (url-file-extension fname t))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
264 (ext (url-file-extension fname nil)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
265 (setq fname (concat (substring base 0 (min 8 (length base)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
266 (substring ext 0 (min 4 (length ext)))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
267 (setq host-components
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
268 (mapcar
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
269 (function
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
270 (lambda (x)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
271 (if (> (length x) 8)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
272 (concat
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
273 (substring x 0 8) "."
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
274 (substring x 8 (min (length x) 11)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
275 x)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
276 host-components))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
277
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
278 (and fname
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
279 (expand-file-name fname
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
280 (expand-file-name
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
281 (mapconcat 'identity host-components "/")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
282 url-temporary-directory))))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
283
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
284 ;;;###autoload
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
285 (defun url-extract-from-cache (fnam)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
286 "Extract FNAM from the local disk cache"
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
287 (set-buffer (get-buffer-create url-working-buffer))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
288 (erase-buffer)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
289 (setq url-current-mime-viewer nil)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
290 (insert-file-contents-literally fnam)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
291 (load (concat (if (memq system-type '(ms-windows ms-dos os2))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
292 (url-file-extension fnam t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
293 fnam) ".hdr") t t))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
294
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
295 ;;;###autoload
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
296 (defun url-cache-expired (url mod)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
297 "Return t iff a cached file has expired."
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
298 (if (not (string-match url-nonrelative-link url))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
299 t
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
300 (let* ((urlobj (url-generic-parse-url url))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
301 (type (url-type urlobj)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
302 (cond
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
303 (url-standalone-mode
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
304 (not (file-exists-p (url-create-cached-filename urlobj))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
305 ((string= type "http")
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
306 (if (not url-standalone-mode) t
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
307 (not (file-exists-p (url-create-cached-filename urlobj)))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
308 ((not (fboundp 'current-time))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
309 t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
310 ((member type '("file" "ftp"))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
311 (if (or (equal mod '(0 0)) (not mod))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
312 (return t)
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
313 (or (> (nth 0 mod) (nth 0 (current-time)))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
314 (> (nth 1 mod) (nth 1 (current-time))))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
315 (t nil)))))
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
316
441bb1e64a06 Import from CVS: tag r19-15b96
cvs
parents:
diff changeset
317 (provide 'url-cache)