]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
Move all url parsing into the PeerManager.
authorCameron Dale <camrdale@gmail.com>
Fri, 4 Jan 2008 00:01:38 +0000 (16:01 -0800)
committerCameron Dale <camrdale@gmail.com>
Fri, 4 Jan 2008 00:01:38 +0000 (16:01 -0800)
Also added a lookupHash_done function to the main code.

apt_dht/HTTPServer.py
apt_dht/PeerManager.py
apt_dht/apt_dht.py

index 3d0b9d3312ca4fc4c30c5dbecde5dbfe0ed641ba..f5e969ccfc81c8565c77d56c56b3d60ea8662803 100644 (file)
@@ -13,10 +13,11 @@ class FileDownloader(static.File):
         resp = super(FileDownloader, self).render(req)
         
         if self.manager:
+            path = 'http:/' + req.uri
             if resp != responsecode.NOT_FOUND:
-                return self.manager.check_freshness(req.uri, resp.headers.getHeader('Last-Modified'), resp)
+                return self.manager.check_freshness(path, resp.headers.getHeader('Last-Modified'), resp)
             
-            return self.manager.get_resp(req.uri)
+            return self.manager.get_resp(path)
         
         return resp
 
index 115edad8cc694931691b33b618a3f6baf1148bd7..33c0688e0875ae3cd2cb492c39b7f6c9bf79f589 100644 (file)
@@ -1,5 +1,6 @@
 
 from random import choice
+from urlparse import urlparse, urlunparse
 
 from twisted.internet import reactor, defer
 from twisted.trial import unittest
@@ -11,13 +12,24 @@ class PeerManager:
     def __init__(self):
         self.clients = {}
         
-    def get(self, location_list, method="GET", modtime=None):
+    def get(self, locations, 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
+        @type locations: C{list} of C{string}
+        @var locations: a list of the locations where the file can be found
         """
-        host, port, path = choice(location_list)
+        url = choice(locations)
+        parsed = urlparse(url)
+        assert(parsed[0] == "http", "Only HTTP is supported, not '%s'" % parsed[0])
+        host = parsed[1]
+        path = urlunparse(('', '') + parsed[2:])
+        
+        # Make sure a port is included for consistency
+        if host.find(':') >= 0:
+            host, port = host.split(':', 1)
+            port = int(port)
+        else:
+            port = 80
         return self.getPeer(host, port, path, method, modtime)
         
     def getPeer(self, host, port, path, method="GET", modtime=None):
@@ -54,7 +66,7 @@ class TestPeerManager(unittest.TestCase):
         self.timeout = 10
         
         host = 'www.camrdale.org'
-        d = self.manager.get([(host, 80, '/robots.txt')])
+        d = self.manager.get(['http://' + host + '/robots.txt'])
         d.addCallback(self.gotResp, 1, 309)
         return d
         
@@ -63,7 +75,7 @@ class TestPeerManager(unittest.TestCase):
         self.timeout = 10
         
         host = 'www.camrdale.org'
-        d = self.manager.get([(host, 80, '/robots.txt')], "HEAD")
+        d = self.manager.get(['http://' + host + '/robots.txt'], "HEAD")
         d.addCallback(self.gotResp, 1, 0)
         return d
         
@@ -73,7 +85,7 @@ class TestPeerManager(unittest.TestCase):
         lastDefer = defer.Deferred()
         
         def newRequest(host, path, num, expect, last=False):
-            d = self.manager.get([(host, 80, path)])
+            d = self.manager.get(['http://' + host + ':' + str(80) + path])
             d.addCallback(self.gotResp, num, expect)
             if last:
                 d.addBoth(lastDefer.callback)
index 8d81c3de5f8c17d61cb43b243a8a53bd3d77f634..50d79c72f2669f6c99beb213419913eab699f341 100644 (file)
@@ -19,19 +19,7 @@ class AptDHT:
         return self.http_site
     
     def check_freshness(self, path, modtime, 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 = self.peers.get([path], "HEAD", modtime)
         d.addCallback(self.check_freshness_done, path, resp)
         return d
     
@@ -51,17 +39,19 @@ class AptDHT:
         
     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
+            getDefer = self.peers.get([path])
+            getDefer.addCallback(d.callback)
+        else:
+            # Lookup hash from DHT
+            lookupDefer = self.dht.getValue(hash)
+            lookupDefer.addCallback(self.lookupHash_done, hash, size, path, d)
             
-            # 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)])
+    def lookupHash_done(self, locations, hash, size, path, d):
+        if not locations:
+            getDefer = self.peers.get([path])
+            getDefer.addCallback(d.callback)
+        else:
+            # Download from the found peers
+            getDefer = self.peers.get(locations)
             getDefer.addCallback(d.callback)
             
\ No newline at end of file