Skip to content

#11919 drop dep on deprecated cgi (removed in 3.13)#11918

Merged
adiroiban merged 22 commits into
twisted:trunkfrom
graingert:drop-deprecated-cgi
Nov 3, 2023
Merged

#11919 drop dep on deprecated cgi (removed in 3.13)#11918
adiroiban merged 22 commits into
twisted:trunkfrom
graingert:drop-deprecated-cgi

Conversation

@graingert

@graingert graingert commented Aug 18, 2023

Copy link
Copy Markdown
Member

Scope and purpose

Fixes #11919

@graingert graingert changed the title drop dep on deprecated cgi (removed in 3.13) #11919 drop dep on deprecated cgi (removed in 3.13) Aug 18, 2023
@graingert graingert force-pushed the drop-deprecated-cgi branch from d0282dd to 60b5ed5 Compare August 18, 2023 11:23
@graingert graingert marked this pull request as ready for review August 18, 2023 11:23
@chevah-robot chevah-robot requested a review from a team August 18, 2023 11:23
Comment thread docs/core/howto/tutorial/listings/finger/finger16.tac Outdated
Comment thread docs/core/howto/tutorial/listings/finger/finger17.tac Outdated
Comment thread docs/core/howto/tutorial/listings/finger/finger20.tac Outdated
Comment thread docs/core/howto/tutorial/listings/finger/finger21.tac Outdated
@adiroiban

Copy link
Copy Markdown
Member

Many thanks for the PR. I think that vendoring is the only way to keep backward compatibilty.

We can replace parse_multipart with email.message_from_bytes and use Message.walk() but we will not keep the FieldStorage API.

I think that it's ok to merge this.

We should find a better API for twisted form parsing...and once we have that new API we can deprecate this.

Comment thread src/twisted/web/_cgi.py
@graingert graingert requested a review from twm August 21, 2023 11:32

@twm twm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hesitant to vendor so much code without also vendoring its tests. It looks like using the older parse_multipart implementation would lower the burden in both ways.

Comment thread src/twisted/web/_cgi.py
Comment on lines +807 to +838
def parse_multipart(fp, pdict, encoding="utf-8", errors="replace", separator="&"):
"""Parse multipart input.

Arguments:
fp : input file
pdict: dictionary containing other parameters of content-type header
encoding, errors: request encoding and error handler, passed to
FieldStorage

Returns a dictionary just like parse_qs(): keys are the field names, each
value is a list of values for that field. For non-file fields, the value
is a list of strings.
"""
# RFC 2046, Section 5.1 : The "multipart" boundary delimiters are always
# represented as 7bit US-ASCII.
boundary = pdict["boundary"].decode("ascii")
ctype = "multipart/form-data; boundary={}".format(boundary)
headers = Message()
headers.set_type(ctype)
try:
headers["Content-Length"] = pdict["CONTENT-LENGTH"]
except KeyError:
pass
fs = FieldStorage(
fp,
headers=headers,
encoding=encoding,
errors=errors,
environ={"REQUEST_METHOD": "POST"},
separator=separator,
)
return {k: fs.getlist(k) for k in fs}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should use the version of cgi.parse_multipart from before this commit. That commit regressed on the type of headers (from bytes to str) and introduced the encoding stuff, which is (a) a questionable design choice at this layer and (b) actively counterproductive for Twisted, since we need bytes.

Comment on lines +90 to +91
from twisted.web import _cgi
fs = _cgi.FieldStorage()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you take my suggestion above I think that we can avoid vendoring FieldStorage entirely by replace this with something that echoes os.environ['QUERY_STRING'] (RFC). FieldStorage appears to have been used purely for convenience, and as the cgi module is going away I don't think there's any sense verifying compatibility with it.

HEADER_OUTPUT_CGI looks like it'd work for this.

@graingert graingert requested a review from adiroiban September 8, 2023 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

drop dep on deprecated cgi (removed in 3.13)

5 participants