[Debtorrent-commits] r76 - in /debtorrent/branches/http-listen/DebTorrent/BT1: HTTPDownloader.py Storage.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Fri Jun 1 05:56:58 UTC 2007


Author: camrdale-guest
Date: Fri Jun  1 05:56:58 2007
New Revision: 76

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=76
Log:
First attempt at modifying HTTPDownloader to work with mirrors.

Modified:
    debtorrent/branches/http-listen/DebTorrent/BT1/HTTPDownloader.py
    debtorrent/branches/http-listen/DebTorrent/BT1/Storage.py

Modified: debtorrent/branches/http-listen/DebTorrent/BT1/HTTPDownloader.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/http-listen/DebTorrent/BT1/HTTPDownloader.py?rev=76&op=diff
==============================================================================
--- debtorrent/branches/http-listen/DebTorrent/BT1/HTTPDownloader.py (original)
+++ debtorrent/branches/http-listen/DebTorrent/BT1/HTTPDownloader.py Fri Jun  1 05:56:58 2007
@@ -6,9 +6,6 @@
 
 """Manage downloading pieces over HTTP.
 
- at type EXPIRE_TIME: C{int}
- at var EXPIRE_TIME: number of seconds after which disconnected seeds are expired
-    (not used)
 @type VERSION: C{string}
 @var VERSION: the UserAgent identifier sent to all sites
 @type haveall: L{haveComplete}
@@ -29,8 +26,6 @@
     True = 1
     False = 0
 
-EXPIRE_TIME = 60 * 60
-
 VERSION = product_name+'/'+version_short
 
 class haveComplete:
@@ -62,6 +57,12 @@
     @ivar connection: the connection to the HTTP server
     @type seedurl: C{string}
     @ivar seedurl: the path component from the L{baseurl}
+    @type params: C{string}
+    @ivar params: the parameters component from the L{baseurl}
+    @type query: C{string}
+    @ivar query: the query component from the L{baseurl}
+    @type headers: C{dictionary}
+    @ivar headres: the HTTP headers to send in the request
     @type measure: L{DebTorrent.CurrentRateMeasure.Measure}
     @ivar measure: tracks the download rate from the site
     @type index: C{int}
@@ -104,14 +105,14 @@
         @type downloader: L{HTTPDownloader}
         @param downloader: the collection of all HTTP downloads
         @type url: C{string}
-        @param url: the base URL to append download info to
+        @param url: the base URL to add download info to
         
         """
         
         self.downloader = downloader
         self.baseurl = url
         try:
-            (scheme, self.netloc, path, pars, query, fragment) = urlparse(url)
+            (scheme, self.netloc, path, params, query, fragment) = urlparse(url)
         except:
             self.downloader.errorfunc('cannot parse http seed address: '+url)
             return
@@ -124,13 +125,18 @@
             self.downloader.errorfunc('cannot connect to http seed: '+url)
             return
         self.seedurl = path
-        if pars:
-            self.seedurl += ';'+pars
-        self.seedurl += '?'
+        if path[-1:] != '/':
+            self.seedurl += '/'
+        if params:
+            self.params = ';'+params
+        else:
+            self.params = ''
         if query:
-            self.seedurl += query+'&'
-        self.seedurl += 'info_hash='+quote(self.downloader.infohash)
-
+            self.query = '?'+query+'&'
+        else:
+            self.query = ''
+        
+        self.headers = {'User-Agent': VERSION}
         self.measure = Measure(downloader.max_rate_period)
         self.index = None
         self.url = ''
@@ -195,10 +201,13 @@
             self.endflag = True
             self.resched()
         else:
-            self.url = ( self.seedurl+'&piece='+str(self.index) )
+            (start, end, length, file) = self.downloader.storage.storage.get_file_range(self.index)
+            self.url = ( self.seedurl + file + self.params + self.query )
             self._get_requests()
-            if self.request_size < self.downloader.storage._piecelen(self.index):
-                self.url += '&ranges='+self._request_ranges()
+            if self.headers.has_key('Range'):
+                del self.headers['Range']
+            if self.request_size < length:
+                self.headers['Range'] = 'bytes=' + self._request_ranges()
             rq = Thread(target = self._request)
             rq.setDaemon(False)
             rq.start()
@@ -218,8 +227,7 @@
         self.error = None
         self.received_data = None
         try:
-            self.connection.request('GET',self.url, None,
-                                {'User-Agent': VERSION})
+            self.connection.request('GET',self.url, None, self.headers)
             r = self.connection.getresponse()
             self.connection_status = r.status
             self.received_data = r.read()

Modified: debtorrent/branches/http-listen/DebTorrent/BT1/Storage.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/http-listen/DebTorrent/BT1/Storage.py?rev=76&op=diff
==============================================================================
--- debtorrent/branches/http-listen/DebTorrent/BT1/Storage.py (original)
+++ debtorrent/branches/http-listen/DebTorrent/BT1/Storage.py Fri Jun  1 05:56:58 2007
@@ -77,6 +77,9 @@
         end offset, offset within the file, and file name
     @type file_pieces: C{list} of (C{int}, C{int})
     @ivar file_pieces: for each file, the starting and ending piece of the file
+    @type piece_files: C{dictionary}
+    @ivar piece_files: for each piece, the starting and ending offset of the 
+        piece in the file, the length of the file, and the file name
     @type disabled_ranges: C{list} of C{tuple}
     @ivar disabled_ranges: for each file, a tuple containing the working range, 
         shared pieces, and disabled range (see L{_get_disabled_ranges} for their
@@ -160,6 +163,7 @@
         self.disabled = [True] * len(files)
         self.file_ranges = []
         self.file_pieces = []
+        self.piece_files = {}
         self.disabled_ranges = []
         self.working_ranges = []
         numfiles = 0
@@ -201,10 +205,15 @@
                 numfiles += 1
                 total += length
                 start_piece = cur_piece
+                cur_piece_offset = 0l
                 for cur_piece in xrange(start_piece,len(self.piece_lengths)+1):
                     if piece_total >= total:
                         break
+                    self.piece_files[cur_piece] = (cur_piece_offset, 
+                           cur_piece_offset + self.piece_lengths[cur_piece], 
+                           length, file)
                     piece_total += self.piece_lengths[cur_piece]
+                    cur_piece_offset += self.piece_lengths[cur_piece]
                 end_piece = cur_piece-1
                 if piece_total > total:
                     cur_piece -= 1
@@ -232,6 +241,8 @@
                 self.sizes[file] = length
                 so_far += l
 
+        if DEBUG:
+            print 'piece_files:', self.piece_files
         self.total_length = total
         self._reset_ranges()
 
@@ -530,6 +541,19 @@
             print 'file ranges:', str(self.ranges)
             print 'file begins:', str(self.begins)
 
+    def get_file_range(self, index):
+        """Get the file name and range that corresponds to this piece.
+        
+        @type index: C{int}
+        @param index: the piece index to get a file range for
+        @rtype: (C{long}, C{long}, C{long}, C{string})
+        @return: the start and end offsets of the piece in the file, the length
+            of the file, and the name of the file
+        
+        """
+        
+        return self.piece_files[index]
+
     def _intervals(self, pos, amount):
         """Get the files that are within the range.
         




More information about the Debtorrent-commits mailing list