durusmail: quixote-users: patch: Binary file uploads on windows
patch: Binary file uploads on windows
Re: patch: Binary file uploads on windows
2004-07-31
patch: Binary file uploads on windows
Brendan T O'Connor
2004-07-31
Hi,

I was having problems with the file upload demo on Windows with
Apache/CGI.  Turns out that to safely upload binary files, you have to
get sys.stdin to be in binary mode, else it quits reading once it hits
the first null byte.  Here's my patch to upload.py which seems to
solve it on windows at least -- are there any problems with this?

Thanks, Brendan

--- /cygdrive/c/Quixote-1.0/upload.py   2004-06-15 00:28:20.000000000 +0200
+++ upload.py   2004-07-31 10:59:00.000000000 +0200
@@ -362,8 +362,18 @@
         # parameter.  Barf if not there or unexpected type.
         (ctype, boundary) = self.parse_content_type()

+        # binary mode on stdin, needed to upload a file with nulls on windows.
+        # (at least on apache cgi?)
+        # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/273844
+        try:
+            import msvcrt
+            msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
+            msvcrt.setmode (1, os.O_BINARY) # stdout = 1
+        except ImportError: pass
+
         # The meat of the body starts after the first occurrence of
         # the boundary, so read up to that point.
+
         file = CountingFile(self.stdin)
         read_mime_part(file, boundary)

reply