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

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Wed Aug 15 01:23:35 UTC 2007


Author: camrdale-guest
Date: Wed Aug 15 01:23:35 2007
New Revision: 247

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=247
Log:
Introduce protocol tracking in anticipation of new protocols.

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=247&op=diff
==============================================================================
--- debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py (original)
+++ debtorrent/branches/http1.1/DebTorrent/HTTPHandler.py Wed Aug 15 01:23:35 2007
@@ -65,15 +65,13 @@
     @ivar encoding: the encoding to use when sending the response
     @type headers: C{dictionary}
     @ivar headers: the headers received with the request
-    @type pre1: C{boolean}
-    @ivar pre1: whether the request is from a pre version 1.0 client
     @type answer: (C{int}, C{string}, C{dictionary}, C{string})
     @ivar answer: the HTTP status code, status message, headers, and package
         data, or None if the answer is not yet available
     
     """
     
-    def __init__(self, header, command, path, encoding, headers, pre1):
+    def __init__(self, header, command, path, encoding, headers):
         """Initialize the instance.
         
         @type header: C{string}
@@ -86,8 +84,6 @@
         @param encoding: the encoding to use when sending the response
         @type headers: C{dictionary}
         @param headers: the headers received with the request
-        @type pre1: C{boolean}
-        @param pre1: whether the request is from a pre version 1.0 client
         
         """
         
@@ -96,7 +92,6 @@
         self.path = path
         self.encoding = encoding
         self.headers = headers
-        self.pre1 = pre1
         self.answer = None
         
     def save_answer(self, r):
@@ -150,8 +145,10 @@
     @ivar buf: the buffered data received on the connection
     @type requests: C{list} of L{HTTPRequest}
     @ivar requests: the outstanding requests for paths
-    @type version: C{string}
-    @ivar version: the HTTP protocol version of the request
+    @type protocol: C{string}
+    @ivar protocol: the protocol used to make the request
+    @type version: (C{int}, C{int})
+    @ivar version: the protocol version of the request
     @type close_connection: C{boolean}
     @ivar close_connection: whether the connection will be closed after this
         request
@@ -169,8 +166,6 @@
     @ivar command: the requested command ('GET' or 'HEAD')
     @type path: C{string}
     @ivar path: the requested path to get
-    @type pre1: C{boolean}
-    @ivar pre1: whether the request is from a pre version 1.0 client
     @type headers: C{dictionary}
     @ivar headers: the headers received with the request
     @type encoding: C{string}
@@ -192,6 +187,7 @@
         self.connection = connection
         self.buf = ''
         self.requests = []
+        self.protocol = ''
         self.version = None
         self.close_connection = True
         self.closed = False
@@ -254,32 +250,33 @@
         words = data.split()
         if len(words) == 3:
             # Must be HTTP 1.0 or greater
-            self.command, self.path, self.version = words
-            self.pre1 = False
+            self.command, self.path, version = words
             
             if self.handler.protocol >= "HTTP/1.1":
                 # Extract the version number from the request
-                if self.version[:5] != 'HTTP/':
-                    logger.error("Bad request version (%r)", self.version)
+                if version[:5] != 'HTTP/':
+                    logger.error("Bad request protocol (%r)", self.version)
                     return None
+                self.protocol = "HTTP"
                 try:
-                    base_version_number = self.version.split('/', 1)[1]
+                    base_version_number = version.split('/', 1)[1]
                     version_number = base_version_number.split(".")
                     if len(version_number) != 2:
-                        logger.error("Bad request version (%r)", self.version)
+                        logger.error("Bad request version (%r)", version)
                         return None
-                    version_number = int(version_number[0]), int(version_number[1])
+                    self.version = int(version_number[0]), int(version_number[1])
                 except (ValueError, IndexError):
-                    logger.error("Bad request version (%r)", self.version)
+                    logger.error("Bad request version (%r)", version)
                     return None
                 
                 # Use persistent connections for HTTP 1.1
-                if version_number >= (1, 1):
+                if self.version >= (1, 1):
                     self.close_connection = False
         elif len(words) == 2:
             # Old HTTP 0.9 connections don't include the version and only support GET
             self.command, self.path = words
-            self.pre1 = True
+            self.protocol = 'HTTP'
+            self.version = (0, 9)
             if self.command != 'GET':
                 logger.warning('connection closed, improper command: '+self.command)
                 return None
@@ -326,7 +323,7 @@
             newrequest = None
             if not self.close_connection or self.requests:
                 newrequest = HTTPRequest(self.header, self.command, self.path,
-                                         self.encoding, self.headers, self.pre1)
+                                         self.encoding, self.headers)
                 self.requests.append(newrequest)
 
             # Call the function to process the request
@@ -340,7 +337,7 @@
                 else:
                     # It's the only request, so just send it
                     self.send_answer(r, self.header, self.command, self.path,
-                                     self.encoding, self.headers, self.pre1)
+                                     self.encoding, self.headers)
                 
             # Request complete, close or wait for more
             if self.close_connection:
@@ -389,10 +386,10 @@
             httpreq = self.requests.pop(0)
             r = httpreq.get_answer()
             self.send_answer(r, httpreq.header, httpreq.command, httpreq.path,
-                             httpreq.encoding, httpreq.headers, httpreq.pre1)
+                             httpreq.encoding, httpreq.headers)
 
     def send_answer(self, (responsecode, responsestring, headers, data),
-                    header, command, path, encoding, req_headers, pre1):
+                    header, command, path, encoding, req_headers):
         """Send out the complete request.
         
         @type responsecode: C{int}
@@ -413,8 +410,6 @@
         @param encoding: the encoding to use when sending the response
         @type req_headers: C{dictionary}
         @param req_headers: the headers received with the request
-        @type pre1: C{boolean}
-        @param pre1: whether the request is from a pre version 1.0 client
         
         """
 
@@ -447,7 +442,7 @@
         r = StringIO()
         r.write(self.handler.protocol + ' ' + str(responsecode) + ' ' + 
             responsestring + '\r\n')
-        if not self.pre1:
+        if self.version >= (1, 0):
             headers['Content-Length'] = len(data)
             for key, value in headers.items():
                 r.write(key + ': ' + str(value) + '\r\n')
@@ -470,6 +465,9 @@
         
 class HTTPHandler:
     """The handler for all new and existing HTTP connections.
+    
+    Supports HTTP/1.1 persistent connections with pipelining if the protocol
+    is set to 'HTTP/1.1'.
     
     @type connections: C{dictionary}
     @ivar connections: all the existing connections, keys are the connection 




More information about the Debtorrent-commits mailing list