r249 - /debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Wed Aug 15 03:14:09 UTC 2007


Author: camrdale-guest
Date: Wed Aug 15 03:14:09 2007
New Revision: 249

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=249
Log:
Add support for the DEBTORRENT protocol.

Modified:
    debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py

Modified: debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py?rev=249&op=diff
==============================================================================
--- debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py (original)
+++ debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py Wed Aug 15 03:14:09 2007
@@ -33,6 +33,8 @@
 
 months = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
     'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
+
+DEBTORRENT_PROTOCOL = "0.1"
 
 def isotime(secs = None):
     """Create an ISO formatted string of the time.
@@ -251,15 +253,18 @@
         if len(words) == 3:
             # Must be HTTP 1.0 or greater
             self.command, self.path, version = words
+
+            try:
+                # Extract the protocol from the request
+                self.protocol, base_version_number = version.split('/', 1)
+            except:
+                logger.error("Bad request protocol (%r)", version)
+                return None
             
             if self.handler.protocol >= "HTTP/1.1":
-                # Extract the version number from the request
-                if version[:5] != 'HTTP/':
-                    logger.error("Bad request protocol (%r)", self.version)
-                    return None
-                self.protocol = "HTTP"
                 try:
-                    base_version_number = version.split('/', 1)[1]
+                    # Extract the version number from the request
+                    self.protocol, base_version_number = version.split('/', 1)
                     version_number = base_version_number.split(".")
                     if len(version_number) != 2:
                         logger.error("Bad request version (%r)", version)
@@ -269,9 +274,15 @@
                     logger.error("Bad request version (%r)", version)
                     return None
                 
-                # Use persistent connections for HTTP 1.1
-                if self.version >= (1, 1):
+                # Use persistent connections for DEBTORRENT/HTTP1.1
+                if (self.protocol == "DEBTORRENT" or 
+                    (self.protocol == "HTTP" and self.version >= (1, 1))):
                     self.close_connection = False
+                    
+            elif self.protocol != "HTTP":
+                logger.error("Unsupported protocol (%r)", version)
+                return None
+            
         elif len(words) == 2:
             # Old HTTP 0.9 connections don't include the version and only support GET
             self.command, self.path = words
@@ -288,7 +299,7 @@
             logger.warning('connection closed, improper command: '+self.command)
             return None
         
-        logger.info(str(self.req_count)+': '+self.header)
+        logger.info(str(self.req_count)+': '+self.protocol+' '+self.header)
         self.headers = {}
         return self.read_header
 
@@ -379,7 +390,15 @@
             if httpreq not in self.requests:
                 logger.error('Got an answer for an unknown request')
             else:
-                httpreq.save_answer(r)
+                if self.protocol == "DEBTORRENT":
+                    # DEBTORRENT requests get sent immediately
+                    #self.requests.remove(httpreq)
+                    #self.send_answer(r, httpreq.header, httpreq.command,
+                    #                 httpreq.path, httpreq.encoding,
+                    #                 httpreq.headers)
+                    httpreq.save_answer(r)
+                else:
+                    httpreq.save_answer(r)
 
         # Answer all possible requests
         while self.requests and self.requests[0].has_answer():
@@ -437,18 +456,30 @@
                                 req_headers.get('referer', '-'),
                                 req_headers.get('user-agent', '-') )
 
-        logger.info('sending response: '+str(responsecode)+' '+responsestring+
+        logger.info('sending response: '+self.protocol+' '+str(responsecode)+' '+responsestring+
                     ' ('+str(len(data))+' bytes)')
+        
         r = StringIO()
-        r.write(self.handler.protocol + ' ' + str(responsecode) + ' ' + 
-            responsestring + '\r\n')
-        if self.version >= (1, 0):
+        
+        # Write the header line
+        if self.protocol == "HTTP":
+            r.write(self.handler.protocol + ' ' + str(responsecode) + ' ' + 
+                    responsestring + '\r\n')
+        elif self.protocol == "DEBTORRENT":
+            r.write('DEBTORRENT/'+DEBTORRENT_PROTOCOL+' '+
+                    str(responsecode)+' '+responsestring+'\r\n')
+            
+        # Write the individual headers
+        if self.version >= (1, 0) or self.protocol != 'HTTP':
             headers['Content-Length'] = len(data)
             for key, value in headers.items():
                 r.write(key + ': ' + str(value) + '\r\n')
             r.write('\r\n')
+            
+        # Don't write the body if only the headers are requested
         if command != 'HEAD':
             r.write(data)
+            
         self.connection.write(r.getvalue())
     
     def close(self):




More information about the Debtorrent-commits mailing list