Skip to content

Commit

Permalink
More CGI revert.
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Oct 11, 2023
1 parent ddabffa commit 4c9e788
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/twisted/web/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import base64
import binascii
import calendar
import cgi
import math
import os
import re
Expand Down Expand Up @@ -129,7 +130,6 @@
from twisted.python.components import proxyForInterface
from twisted.python.deprecate import deprecated
from twisted.python.failure import Failure
from twisted.web import _cgi

# twisted imports
from twisted.web._responses import (
Expand Down Expand Up @@ -225,10 +225,10 @@


def _parseHeader(line):
# _cgi.parse_header requires a str
key, pdict = _cgi.parse_header(line.decode("charmap"))
# cgi.parse_header requires a str
key, pdict = cgi.parse_header(line.decode("charmap"))

# We want the key as bytes, and _cgi.parse_multipart (which consumes
# We want the key as bytes, and cgi.parse_multipart (which consumes
# pdict) expects a dict of str keys but bytes values
key = key.encode("charmap")
pdict = {x: y.encode("charmap") for x, y in pdict.items()}
Expand Down Expand Up @@ -974,17 +974,26 @@ def requestReceived(self, command, path, version):
if self.method == b"POST" and ctype and clength:
mfd = b"multipart/form-data"
key, pdict = _parseHeader(ctype)
# This weird CONTENT-LENGTH param is required by
# cgi.parse_multipart() in some versions of Python 3.7+, see
# bpo-29979. It looks like this will be relaxed and backported, see
# https://github.com/python/cpython/pull/8530.
pdict["CONTENT-LENGTH"] = clength
if key == b"application/x-www-form-urlencoded":
args.update(parse_qs(self.content.read(), 1))
elif key == mfd:
try:
cgiArgs = _cgi.parse_multipart(
cgiArgs = cgi.parse_multipart(
self.content,
pdict,
encoding="utf8",
errors="surrogateescape",
)

# The parse_multipart function on Python 3.7+
# decodes the header bytes as iso-8859-1 and
# decodes the body bytes as utf8 with
# surrogateescape -- we want bytes
self.args.update(
{
x.encode("iso-8859-1"): [
Expand Down
4 changes: 2 additions & 2 deletions src/twisted/web/test/test_cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
"""

URL_PARAMETER_CGI = """\
from twisted.web import _cgi
fs = _cgi.FieldStorage()
import cgi
fs = cgi.FieldStorage()
param = fs.getvalue("param")
print("Header: OK")
print("")
Expand Down

0 comments on commit 4c9e788

Please sign in to comment.