Aurelien Larcher
2021-01-08 b5a8497ec9558d0061024ac4b49f445bbe7d1094
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Fixes Python CGI being confused about binary files (bugs 31546357 & 31936635).
 
Upstream issue:
https://bugs.python.org/issue27777
 
WIP patch taken from upstream is still buggy, so we use
this in-house developed one.
 
--- Python-3.9.0/Lib/cgi.py
+++ Python-3.9.0/Lib/cgi.py
@@ -689,7 +689,10 @@
                 if not data:
                     self.done = -1
                     break
-                self.file.write(data)
+                if self._binary_file:
+                    self.file.write(data)
+                else:
+                    self.file.write(data.decode())
                 todo = todo - len(data)
 
     def read_lines(self):
--- Python-3.9.0/Lib/test/test_cgi.py
+++ Python-3.9.0/Lib/test/test_cgi.py
@@ -366,6 +366,18 @@ Larry
         self.assertEqual(fs.list[0].name, 'submit-name')
         self.assertEqual(fs.list[0].value, 'Larry')
 
+    def test_content_length_no_content_disposition(self):
+        body = b'{"test":123}'
+        env = {
+            'CONTENT_LENGTH': len(body),
+            'REQUEST_METHOD': 'POST',
+            'CONTENT_TYPE': 'application/json',
+            'wsgi.input': BytesIO(body),
+        }
+
+        form = cgi.FieldStorage(fp=env['wsgi.input'], environ=env)
+        self.assertEqual(form.file.read(), body.decode(form.encoding))
+
     def test_field_storage_multipart_no_content_length(self):
         fp = BytesIO(b"""--MyBoundary
 Content-Disposition: form-data; name="my-arg"; filename="foo"