Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace cgi.FieldStorage with multipart #466

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exclude = '''
\.git
| .tox
)/
| src/webob/multipart.py
| tests/test_multipart/
'''

# This next section only exists for people that have their editors
Expand All @@ -17,7 +19,7 @@ exclude = '''
profile = "black"
multi_line_output = 3
src_paths = ["src", "tests"]
skip_glob = ["docs/*"]
skip_glob = ["docs/*", "tests/test_multipart/*", "src/webob/multipart.py"]
include_trailing_comma = true
force_grid_wrap = false
combine_as_imports = true
Expand Down
117 changes: 0 additions & 117 deletions src/webob/compat.py

This file was deleted.

80 changes: 79 additions & 1 deletion src/webob/multidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"""
import binascii
from collections.abc import MutableMapping
from urllib.parse import urlencode as url_encode
from urllib.parse import parse_qsl, urlencode as url_encode
import warnings

from .multipart import parse_options_header

__all__ = ["MultiDict", "NestedMultiDict", "NoVars", "GetDict"]


Expand Down Expand Up @@ -57,6 +59,9 @@ def view_list(cls, lst):
def from_fieldstorage(cls, fs):
"""
Create a multidict from a cgi.FieldStorage instance

Legacy.

"""
obj = cls()
# fs.list can be None when there's nothing to parse
Expand Down Expand Up @@ -96,6 +101,25 @@ def decode(b):

return obj

@classmethod
def from_multipart(cls, mp):
obj = cls()

for part in mp:
if part.filename or not part.is_buffered():
container = MultiDictFile.from_multipart_part(part)
obj.add(part.name, container)
else:
obj.add(part.name, part.value)
return obj

@classmethod
def from_qs(cls, data, charset="utf-8"):
data = parse_qsl(data, keep_blank_values=True)
return cls(
(key.decode(charset), value.decode(charset)) for (key, value) in data
)

def __getitem__(self, key):
for k, v in reversed(self._items):
if k == key:
Expand Down Expand Up @@ -286,6 +310,60 @@ def values(self):
_dummy = object()


class MultiDictFile:
"""
An object representing a file upload in a ``multipart/form-data`` request.

This object has the same shape as Python's deprecated ``cgi.FieldStorage``
object, which was previously used by webob to represent file uploads.

"""

def __init__(
self,
name,
filename,
file,
type,
type_options,
disposition,
disposition_options,
headers,
):
self.name = name
self.filename = filename
self.file = file
self.type = type
self.type_options = type_options
self.disposition = disposition
self.disposition_options = disposition_options
self.headers = headers

@classmethod
def from_multipart_part(cls, part):
content_type = part.headers.get("Content-Type", "")
content_type, options = parse_options_header(part.content_type)
disposition, disp_options = parse_options_header(part.disposition)
return cls(
name=part.name,
filename=part.filename,
file=part.file,
type=content_type,
type_options=options,
disposition=disposition,
disposition_options=disp_options,
headers=part.headers,
)

@property
def value(self):
pos = self.file.tell()
self.file.seek(0)
val = self.file.read()
self.file.seek(pos)
return val


class GetDict(MultiDict):
# def __init__(self, data, tracker, encoding, errors):
# d = lambda b: b.decode(encoding, errors)
Expand Down
Loading
Loading