Patch #1065257: Support passing open files as body in
HTTPConnection.request().
diff --git a/Lib/httplib.py b/Lib/httplib.py
index 5ae5efc..1e0037f 100644
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -704,7 +704,15 @@
         if self.debuglevel > 0:
             print "send:", repr(str)
         try:
-            self.sock.sendall(str)
+            blocksize=8192
+            if hasattr(str,'read') :
+                if self.debuglevel > 0: print "sendIng a read()able"
+                data=str.read(blocksize)
+                while data:
+                    self.sock.sendall(data)
+                    data=str.read(blocksize)
+            else:
+                self.sock.sendall(str)
         except socket.error, v:
             if v[0] == 32:      # Broken pipe
                 self.close()
@@ -879,7 +887,21 @@
         self.putrequest(method, url, **skips)
 
         if body and ('content-length' not in header_names):
-            self.putheader('Content-Length', str(len(body)))
+            thelen=None
+            try:
+                thelen=str(len(body))
+            except TypeError, te:
+                # If this is a file-like object, try to
+                # fstat its file descriptor
+                import os
+                try:
+                    thelen = str(os.fstat(body.fileno()).st_size)
+                except (AttributeError, OSError):
+                    # Don't send a length if this failed
+                    if self.debuglevel > 0: print "Cannot stat!!"
+                    
+            if thelen is not None:
+                self.putheader('Content-Length',thelen)
         for hdr, value in headers.iteritems():
             self.putheader(hdr, value)
         self.endheaders()
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 8d9a706..90a4e55 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -10,9 +10,10 @@
     def __init__(self, text, fileclass=StringIO.StringIO):
         self.text = text
         self.fileclass = fileclass
+        self.data = ''
 
     def sendall(self, data):
-        self.data = data
+        self.data += data
 
     def makefile(self, mode, bufsize=None):
         if mode != 'r' and mode != 'rb':
@@ -133,6 +134,16 @@
             self.fail("Did not expect response from HEAD request")
         resp.close()
 
+    def test_send_file(self):
+        expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
+                   'Accept-Encoding: identity\r\nContent-Length:'
+
+        body = open(__file__, 'rb')
+        conn = httplib.HTTPConnection('example.com')
+        sock = FakeSocket(body)
+        conn.sock = sock
+        conn.request('GET', '/foo', body)
+        self.assertTrue(sock.data.startswith(expected))
 
 class OfflineTest(TestCase):
     def test_responses(self):