(version.short(), twisted_version.short(), web2_version.short()))
return headers
- def get(self, path, method="GET"):
+ def get(self, path, method="GET", modtime=None):
headers = self.setCommonHeaders()
+ if modtime:
+ headers.setHeader('If-Modified-Since', modtime)
return self.submitRequest(ClientRequest(method, path, headers, None))
def getRange(self, path, rangeStart, rangeEnd, method="GET"):
def __init__(self):
self.clients = {}
- def get(self, location_list, method="GET"):
+ def get(self, location_list, method="GET", modtime=None):
"""Download from a list of peers.
@type location_list: C{list} of (C{string}, C{int}, C{string})
@var location_list: a list of the locations where the file can be found
"""
host, port, path = choice(location_list)
- return self.getPeer(host, port, path, method)
+ return self.getPeer(host, port, path, method, modtime)
- def getPeer(self, host, port, path, method="GET"):
+ def getPeer(self, host, port, path, method="GET", modtime=None):
if not port:
port = 80
site = host + ":" + str(port)
if site not in self.clients:
self.clients[site] = HTTPClientManager(host, port)
- return self.clients[site].get(path, method)
+ return self.clients[site].get(path, method, modtime)
def close(self):
for site in self.clients:
+from twisted.internet import defer
from twisted.web2 import server, http, http_headers
from apt_dht_conf import config
+from PeerManager import PeerManager
from HTTPServer import TopLevel
+from MirrorManager import MirrorManager
class AptDHT:
def __init__(self):
self.http_server = TopLevel(config.defaults()['cache_dir'], self)
self.http_site = server.Site(self.http_server)
+ self.peers = PeerManager()
+ self.mirrors = MirrorManager(config.defaults()['cache_dir'])
def getSite(self):
return self.http_site
def check_freshness(self, path, modtime, resp):
- return resp
+ host, path = path.split('/',1)
+ if not host:
+ host, path = path.split('/',1)
+ path = '/'+path
+
+ # Make sure a port is included for consistency
+ if host.find(':') >= 0:
+ host, port = host.split(':', 1)
+ port = int(port)
+ else:
+ port = 80
+
+ d = self.peers.get([(host, port, path)], "HEAD", modtime)
+ d.addCallback(self.check_freshness_done, path, resp)
+ return d
+
+ def check_freshness_done(self, resp, path, orig_resp):
+ if resp.code == "304":
+ return orig_resp
+ else:
+ return self.get_resp(path)
def get_resp(self, path):
- return http.Response(
- 200,
- {'content-type': http_headers.MimeType('text', 'html')},
- """<html><body>
- <h2>P2P Download</h2>
- <p>TODO: eventually this will cause a P2P lookup.</body></html>""")
+ d = defer.Deferred()
+
+ findDefer = self.mirrors.findHash(path)
+
+ findDefer.addcallback(self.findHash_done, path, d)
+ return d
+
+ def findHash_done(self, (hash, size), path, d):
+ if hash is None:
+ host, path = path.split('/',1)
+ if not host:
+ host, path = path.split('/',1)
+ path = '/'+path
+
+ # Make sure a port is included for consistency
+ if host.find(':') >= 0:
+ host, port = host.split(':', 1)
+ port = int(port)
+ else:
+ port = 80
+ getDefer = self.peers.get([(host, port, path)])
+ getDefer.addCallback(d.callback)
+
\ No newline at end of file