Commit 28225e1b authored by Kirill Smelkov's avatar Kirill Smelkov

BigFile: Factor out "get .data mtime" into helper

Since .data can be BTreeData or None (or as we'll see next and str),
._p_mtime() is not always defined on it and in several places current
code has branches from where to get it.

Move this logic out to a separate helper and the code which needs to
know mtime gets streamlined.
Suggested-by: Vincent Pelletier's avatarVincent Pelletier <vincent@nexedi.com>
parent 87575acf
...@@ -116,6 +116,17 @@ class BigFile(File): ...@@ -116,6 +116,17 @@ class BigFile(File):
self.serialize() self.serialize()
return btree, len(btree) return btree, len(btree)
def _data_mtime(self):
"""get .data mtime if present and fallback to self._p_mtime"""
# there is no data._p_mtime when data is None.
# so try and fallback to self._p_mtime
data = self._baseGetData()
if data is not None:
mtime = data._p_mtime
else:
mtime = self._p_mtime
return mtime
def _range_request_handler(self, REQUEST, RESPONSE): def _range_request_handler(self, REQUEST, RESPONSE):
# HTTP Range header handling: return True if we've served a range # HTTP Range header handling: return True if we've served a range
# chunk out of our data. # chunk out of our data.
...@@ -147,13 +158,10 @@ class BigFile(File): ...@@ -147,13 +158,10 @@ class BigFile(File):
try: mod_since=long(DateTime(date).timeTime()) try: mod_since=long(DateTime(date).timeTime())
except: mod_since=None except: mod_since=None
if mod_since is not None: if mod_since is not None:
if data is not None: last_mod = self._data_mtime()
last_mod = long(data._p_mtime) if last_mod is None:
else: last_mod = 0
if self._p_mtime: last_mod = long(last_mod)
last_mod = long(self._p_mtime)
else:
last_mod = long(0)
if last_mod > mod_since: if last_mod > mod_since:
# Modified, so send a normal response. We delete # Modified, so send a normal response. We delete
# the ranges, which causes us to skip to the 200 # the ranges, which causes us to skip to the 200
...@@ -172,10 +180,7 @@ class BigFile(File): ...@@ -172,10 +180,7 @@ class BigFile(File):
RESPONSE.setHeader('Content-Range', RESPONSE.setHeader('Content-Range',
'bytes */%d' % self.getSize()) 'bytes */%d' % self.getSize())
RESPONSE.setHeader('Accept-Ranges', 'bytes') RESPONSE.setHeader('Accept-Ranges', 'bytes')
if data is not None: RESPONSE.setHeader('Last-Modified', rfc1123_date(self._data_mtime()))
RESPONSE.setHeader('Last-Modified', rfc1123_date(data._p_mtime))
else:
RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
RESPONSE.setHeader('Content-Type', self.content_type) RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Content-Length', self.getSize()) RESPONSE.setHeader('Content-Length', self.getSize())
RESPONSE.setStatus(416) RESPONSE.setStatus(416)
...@@ -188,10 +193,7 @@ class BigFile(File): ...@@ -188,10 +193,7 @@ class BigFile(File):
start, end = ranges[0] start, end = ranges[0]
size = end - start size = end - start
if data is not None: RESPONSE.setHeader('Last-Modified', rfc1123_date(self._data_mtime()))
RESPONSE.setHeader('Last-Modified', rfc1123_date(data._p_mtime))
else:
RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
RESPONSE.setHeader('Content-Type', self.content_type) RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Content-Length', size) RESPONSE.setHeader('Content-Length', size)
RESPONSE.setHeader('Accept-Ranges', 'bytes') RESPONSE.setHeader('Accept-Ranges', 'bytes')
...@@ -227,10 +229,7 @@ class BigFile(File): ...@@ -227,10 +229,7 @@ class BigFile(File):
RESPONSE.setHeader('Content-Length', size) RESPONSE.setHeader('Content-Length', size)
RESPONSE.setHeader('Accept-Ranges', 'bytes') RESPONSE.setHeader('Accept-Ranges', 'bytes')
if data is not None: RESPONSE.setHeader('Last-Modified', rfc1123_date(self._data_mtime()))
RESPONSE.setHeader('Last-Modified', rfc1123_date(data._p_mtime))
else:
RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
RESPONSE.setHeader('Content-Type', RESPONSE.setHeader('Content-Type',
'multipart/%sbyteranges; boundary=%s' % ( 'multipart/%sbyteranges; boundary=%s' % (
draftprefix, boundary)) draftprefix, boundary))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment