-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebapi.patch
58 lines (53 loc) · 2 KB
/
webapi.patch
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--- web/webapi.py
+++ web/webapi_575.py
@@ -7,6 +7,7 @@
import cgi
import pprint
import sys
+import tempfile
from io import BytesIO
from .py3helpers import PY2, text_type, urljoin
@@ -391,6 +392,23 @@ def InternalError(message=None):
internalerror = InternalError
+class cgiFieldStorage(cgi.FieldStorage):
+ """
+ Subclass cgi.FieldStorage, as read_binary expects fp to return
+ bytes. If the headers do not contain a content-disposition with a
+ filename, cgi.FieldStorage's make_file will create a TemporaryFile
+ with `w+` flags. The write to that temporary file will fail, due
+ to incorrect encoding in Python 3.
+ """
+
+ def make_file(self, binary=None):
+ """
+ For backwards compatibility with Python 2, make_file accepted
+ a binary flag. This was unused, and removed in Python 3.
+ """
+ return tempfile.TemporaryFile("wb+")
+
+
def header(hdr, value, unique=False):
"""
Adds the header `hdr: value` with the response.
@@ -451,19 +451,19 @@
a = ctx.get("_fieldstorage")
if not a:
fp = e["wsgi.input"]
- a = cgi.FieldStorage(fp=fp, environ=e, keep_blank_values=1)
+ a = cgiFieldStorage(fp=fp, environ=e, keep_blank_values=1)
ctx._fieldstorage = a
else:
d = data()
- if PY2 and isinstance(d, text_type):
+ if isinstance(d, str):
d = d.encode("utf-8")
fp = BytesIO(d)
- a = cgi.FieldStorage(fp=fp, environ=e, keep_blank_values=1)
+ a = cgiFieldStorage(fp=fp, environ=e, keep_blank_values=1)
a = dictify(a)
if method.lower() in ["both", "get"]:
e["REQUEST_METHOD"] = "GET"
- b = dictify(cgi.FieldStorage(environ=e, keep_blank_values=1))
+ b = dictify(cgiFieldStorage(environ=e, keep_blank_values=1))
def process_fieldstorage(fs):
if isinstance(fs, list):