emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] scratch/with-url f402e35 3/8: Heed the Cache-Control heade


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] scratch/with-url f402e35 3/8: Heed the Cache-Control header
Date: Sun, 22 Jan 2017 22:25:22 +0000 (UTC)

branch: scratch/with-url
commit f402e351cc2810709a838b479949de94f810af0e
Author: Lars Ingebrigtsen <address@hidden>
Commit: Lars Ingebrigtsen <address@hidden>

    Heed the Cache-Control header
---
 lisp/url/with-url.el |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/lisp/url/with-url.el b/lisp/url/with-url.el
index a38f0f3..b696870 100644
--- a/lisp/url/with-url.el
+++ b/lisp/url/with-url.el
@@ -701,7 +701,10 @@ If the headers don't allow caching, nothing will be done."
   ;; future.
   (let ((expires nil))
     (current-buffer)
-    (when (and (url-header 'last-modified)
+    (when (and (or (url-header 'last-modified)
+                   (and (url-header 'cache-control)
+                        (setq expires (with-url--parse-cache-control
+                                       (url-header 'cache-control)))))
                (or (not (url-header 'expires))
                    (progn
                      (setq expires
@@ -717,7 +720,8 @@ If the headers don't allow caching, nothing will be done."
           (insert "Content-Type: " (or (url-header 'content-type buffer)
                                        "text/plain")
                   "\n")
-          (insert "Last-Modified: " (url-header 'last-modified buffer) "\n")
+          (when (url-header 'last-modified buffer)
+            (insert "Last-Modified: " (url-header 'last-modified buffer) "\n"))
           ;; If there's no Expires header, we cache for one day.
           (insert "Expires: "
                   (let ((system-time-locale "C"))
@@ -733,6 +737,12 @@ If the headers don't allow caching, nothing will be done."
               (make-directory (file-name-directory file) t))
             (write-region (point-min) (point-max) file nil 'silent)))))))
 
+(defun with-url--parse-cache-control (control)
+  ;; Cache-Control: public, max-age=604800
+  (when (string-match "max-age *= *\\([0-9]+\\)" control)
+    (time-add (current-time) (seconds-to-time
+                              (string-to-number (match-string 1 control))))))
+
 (defun with-url-cache-time (url)
   "Return the Last-Modified timestamp for the cached version of URL, if any."
   (let ((file (with-url--cache-file-name url)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]