If a binhost file sets a TTL header, honor it.

BUG=chromium:381970
TEST=Run it locally and verify TTL headers are respected.

Change-Id: I54b2afa6fefdb3de1b9a03aaa3af37fd587e1a11
Reviewed-on: https://chromium-review.googlesource.com/225279
Reviewed-by: Zac Medico <zmedico@gmail.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: David James <davidjames@chromium.org>
Tested-by: David James <davidjames@chromium.org>
diff --git a/pym/portage/dbapi/bintree.py b/pym/portage/dbapi/bintree.py
index 229ce3b..e6eae33 100644
--- a/pym/portage/dbapi/bintree.py
+++ b/pym/portage/dbapi/bintree.py
@@ -43,6 +43,7 @@
 import sys
 import tempfile
 import textwrap
+import time
 import traceback
 import warnings
 from gzip import GzipFile
@@ -879,11 +880,16 @@
 				if e.errno != errno.ENOENT:
 					raise
 			local_timestamp = pkgindex.header.get("TIMESTAMP", None)
+			download_timestamp = float(pkgindex.header.get("DOWNLOAD_TIMESTAMP", 0))
 			remote_timestamp = None
 			rmt_idx = self._new_pkgindex()
 			proc = None
 			tmp_filename = None
 			try:
+				ttl = float(pkgindex.header.get("TTL", 0))
+				if download_timestamp and ttl and download_timestamp + ttl > time.time():
+					raise UseCachedCopyOfRemoteIndex()
+
 				# urlparse.urljoin() only works correctly with recognized
 				# protocols and requires the base url to have a trailing
 				# slash, so join manually...
@@ -1022,6 +1028,7 @@
 					pass
 			if pkgindex is rmt_idx:
 				pkgindex.modified = False # don't update the header
+				pkgindex.header["DOWNLOAD_TIMESTAMP"] = str(long(time.time()))
 				try:
 					ensure_dirs(os.path.dirname(pkgindex_file))
 					f = atomic_ofstream(pkgindex_file)