From f41eb2771e796cb62e8b60cad9fa050059abeb1d Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Sun, 16 Dec 2007 01:09:41 -0800 Subject: [PATCH] Add some of the fetching logic. --- apt_dht/HTTPDownloader.py | 4 ++- apt_dht/PeerManager.py | 8 +++--- apt_dht/apt_dht.py | 56 ++++++++++++++++++++++++++++++++++----- apt_dht/apt_dht_conf.py | 2 +- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/apt_dht/HTTPDownloader.py b/apt_dht/HTTPDownloader.py index 7e5a06c..45ab4f7 100644 --- a/apt_dht/HTTPDownloader.py +++ b/apt_dht/HTTPDownloader.py @@ -116,8 +116,10 @@ class HTTPClientManager(ClientFactory): (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"): diff --git a/apt_dht/PeerManager.py b/apt_dht/PeerManager.py index e372f69..6a3b77d 100644 --- a/apt_dht/PeerManager.py +++ b/apt_dht/PeerManager.py @@ -11,22 +11,22 @@ class PeerManager: 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: diff --git a/apt_dht/apt_dht.py b/apt_dht/apt_dht.py index ecbfab6..642c3f7 100644 --- a/apt_dht/apt_dht.py +++ b/apt_dht/apt_dht.py @@ -1,24 +1,66 @@ +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')}, - """ -

P2P Download

-

TODO: eventually this will cause a P2P lookup.""") + 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 diff --git a/apt_dht/apt_dht_conf.py b/apt_dht/apt_dht_conf.py index 32ffa06..626f0ff 100644 --- a/apt_dht/apt_dht_conf.py +++ b/apt_dht/apt_dht_conf.py @@ -29,7 +29,7 @@ DEFAULTS = { 'username': '', # Which DHT implementation to use - 'DHT': 'Khashmir', + 'DHT': 'apt_dht_Khashmir', } DHT_DEFAULTS = { -- 2.39.5