r357 - in /debtorrent/trunk/DebTorrent: HTTPHandler.py SocketHandler.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Sat Jan 26 22:20:14 UTC 2008


Author: camrdale-guest
Date: Sat Jan 26 22:20:14 2008
New Revision: 357

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=357
Log:
Allow for files to be sent efficiently as HTTP responses

Modified:
    debtorrent/trunk/DebTorrent/HTTPHandler.py
    debtorrent/trunk/DebTorrent/SocketHandler.py

Modified: debtorrent/trunk/DebTorrent/HTTPHandler.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/HTTPHandler.py?rev=357&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/HTTPHandler.py (original)
+++ debtorrent/trunk/DebTorrent/HTTPHandler.py Sat Jan 26 22:20:14 2008
@@ -62,7 +62,7 @@
     @ivar encoding: the encoding to use when sending the response
     @type headers: C{dictionary}
     @ivar headers: the headers received with the request
-    @type answer: (C{int}, C{string}, C{dictionary}, C{string})
+    @type answer: (C{int}, C{string}, C{dictionary}, C{string} or C{file})
     @ivar answer: the HTTP status code, status message, headers, and package
         data, or None if the answer is not yet available
     
@@ -94,7 +94,7 @@
     def save_answer(self, r):
         """Save an answer, replacing the old one if it's better.
         
-        @type r: (C{int}, C{string}, C{dictionary}, C{string})
+        @type r: (C{int}, C{string}, C{dictionary}, C{string} or C{file})
         @param r: the HTTP status code, status message, headers, and package data
         
         """
@@ -368,7 +368,7 @@
     def answer(self, r, httpreq):
         """Add a response to the queued responses and check if any are ready to send.
         
-        @type r: (C{int}, C{string}, C{dictionary}, C{string})
+        @type r: (C{int}, C{string}, C{dictionary}, C{string} or C{file})
         @param r: the HTTP status code, status message, headers, and package data
         @type httpreq: L{HTTPRequest}
         @param httpreq: the request the answer is for
@@ -427,7 +427,7 @@
         @param responsestring: the response string to send
         @type headers: C{dictionary}
         @param headers: the headers to send with the response
-        @type data: C{string}
+        @type data: C{string} or C{file}
         @param data: the data to send with the response
         @type header: C{string}
         @param header: the first header line received from the request
@@ -486,12 +486,12 @@
                 r.write(key + ': ' + str(value) + '\r\n')
             r.write('\r\n')
             
+        self.connection.write(r.getvalue())
+    
         # Don't write the body if only the headers are requested
-        if command != 'HEAD' or not data:
-            r.write(data)
+        if command != 'HEAD' and data:
+            self.connection.write(data)
             
-        self.connection.write(r.getvalue())
-    
     def close(self):
         """Close the connection and drop all pending requests/answers."""
         logger.debug('HTTP connection closed')

Modified: debtorrent/trunk/DebTorrent/SocketHandler.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/SocketHandler.py?rev=357&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/SocketHandler.py (original)
+++ debtorrent/trunk/DebTorrent/SocketHandler.py Sat Jan 26 22:20:14 2008
@@ -167,8 +167,8 @@
         Adds the data to the buffer of data waiting to be written, then tries 
         to write the waiting data out.
         
-        @type s: C{string}
-        @param s: the data to write
+        @type s: C{string} or C{file}
+        @param s: the data to write, or an already opened file to write out
         
         """
         
@@ -190,6 +190,19 @@
             dead = False
             try:
                 while self.buffer:
+                    # Read data from the file and put in on the buffer
+                    while type(self.buffer[0]) == file:
+                        data = self.buffer[0].read(4096)
+                        if len(data) > 0:
+                            self.buffer.insert(0, data)
+                        else:
+                            # End of file has been reached
+                            del self.buffer[0]
+
+                    # Make sure there's still data to send
+                    if not self.buffer:
+                        break
+                            
                     buf = self.buffer[0]
                     amount = self.socket.send(buf)
                     if amount == 0:




More information about the Debtorrent-commits mailing list