--- url-cache.el 2006-05-06 18:32:00.000000000 -0400 +++ /home/happy/src/emacs/multi-tty/lisp/url/url-cache.el 2006-05-06 21:29:57.000000000 -0400 @@ -34,6 +34,12 @@ :type 'directory :group 'url-file) +;; If the cache file is large, we don't want to read the whole file just to +;; get the headers +(defvar url-cache-max-header-size 1024 + "Number of bytes to read when looking for header in cache file. +Used by `url-cache-get-header'.") + ;; Cache manager (defun url-cache-file-writable-p (file) "Follows the documentation of `file-writable-p', unlike `file-writable-p'." @@ -75,13 +81,29 @@ ;;;###autoload (defun url-is-cached (url) - "Return non-nil if the URL is cached." + "Return non-nil if the URL is cached. +If URL is cached return a list: '(lastmod etag)." (let* ((fname (url-cache-create-filename url)) - (attribs (file-attributes fname))) - (and fname ; got a filename - (file-exists-p fname) ; file exists - (not (eq (nth 0 attribs) t)) ; Its not a directory - (nth 5 attribs)))) ; Can get last mod-time + (attribs (file-attributes fname)) + (lastmod (and fname ; got a filename + (file-exists-p fname) ; file exists + (not (eq (nth 0 attribs) t)) ; Its not a directory + (nth 5 attribs))) ; Can get last mod-time + (etag (and lastmod + (url-cache-get-header "ETag" fname)))) + (if (or lastmod + etag) + `(,lastmod ,etag)))) + +(defun url-cache-get-header (header file) + "Return value of HEADER from cache FILE. +FILE must be a value returned by `url-cache-create-filename'. +Reads up to `url-cache-max-header-size' bytes of the cache file.." + (if (file-exists-p file) + (with-temp-buffer + (insert-file-contents-literally file nil 0 url-cache-max-header-size) + (mail-narrow-to-head) + (mail-fetch-field "ETag")))) (defun url-cache-create-filename-human-readable (url) "Return a filename in the local cache for URL" @@ -182,7 +204,8 @@ (defun url-cache-extract (fnam) "Extract FNAM from the local disk cache" (erase-buffer) - (insert-file-contents-literally fnam)) + (if (file-exists-p fnam) + (insert-file-contents-literally fnam))) ;;;###autoload (defun url-cache-expired (url mod)