Skip to content

Commit

Permalink
Replace parse content type.
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Oct 12, 2023
1 parent 0dd7e83 commit e6bf82b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/twisted/web/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import tempfile
import time
import warnings
from email.message import EmailMessage
from io import BytesIO
from typing import AnyStr, Callable, List, Optional, Tuple
from urllib.parse import (
Expand Down Expand Up @@ -224,9 +225,12 @@
monthname_lower = [name and name.lower() for name in monthname]


def _parseHeader(line):
# cgi.parse_header requires a str
key, pdict = cgi.parse_header(line.decode("charmap"))
def _parseContentType(line: bytes):
msg = EmailMessage()
msg["content-type"] = line.decode("charmap")

key = msg.get_content_type()
pdict = msg["content-type"].params

# We want the key as bytes, and cgi.parse_multipart (which consumes
# pdict) expects a dict of str keys but bytes values
Expand Down Expand Up @@ -973,7 +977,7 @@ def requestReceived(self, command, path, version):

if self.method == b"POST" and ctype and clength:
mfd = b"multipart/form-data"
key, pdict = _parseHeader(ctype)
key, pdict = _parseContentType(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
Expand Down

0 comments on commit e6bf82b

Please sign in to comment.