server: Use the Last-Modified header to set last-modified time (mtime)
authorW. Trevor King <wking@tremily.us>
Thu, 20 Feb 2014 21:42:19 +0000 (13:42 -0800)
committerW. Trevor King <wking@tremily.us>
Thu, 20 Feb 2014 21:42:19 +0000 (13:42 -0800)
This also sets the access time to the same value, but we're only
calling _get_file if we're about to serve the file to a client, which
will clobber any value of atime set here.

package_cache/server.py

index 1da14d645b55335e61e141b010dba0f88b8f98e8..21213ae34ebe852b020a5ed9c0eab9a8a1d59f13 100644 (file)
@@ -1,5 +1,6 @@
 # Copyright
 
+import calendar as _calendar
 import email.utils as _email_utils
 import mimetypes as _mimetypes
 import os as _os
@@ -73,6 +74,7 @@ class Server (object):
 
     def _get_file(self, url, path):
         with self.opener.open(url) as response:
+            last_modified = response.getheader('Last-Modified', None)
             content_length = int(response.getheader('Content-Length'))
             with open(path, 'wb') as f:
                 block_size = 8192
@@ -81,6 +83,9 @@ class Server (object):
                     f.write(data)
                     if len(data) < block_size:
                         break
+        if last_modified:
+            mtime = _calendar.timegm(_email_utils.parsedate(last_modified))
+            _os.utime(path=path, times=(mtime, mtime))
 
     def _serve_file(self, path, environ, start_response):
         headers = {