mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
Merge pull request #1644 from cortesi/multidict
Clean up multidict a bit
This commit is contained in:
commit
477f8868ad
@ -59,7 +59,8 @@ class StickyCookie:
|
||||
if not self.jar[dom_port_path]:
|
||||
self.jar.pop(dom_port_path, None)
|
||||
else:
|
||||
b = attrs.with_insert(0, name, value)
|
||||
b = attrs.copy()
|
||||
b.insert(0, name, value)
|
||||
self.jar[dom_port_path][name] = b
|
||||
|
||||
def request(self, flow):
|
||||
|
@ -31,7 +31,7 @@ _cookie_params = set((
|
||||
ESCAPE = re.compile(r"([\"\\])")
|
||||
|
||||
|
||||
class CookieAttrs(multidict.ImmutableMultiDict):
|
||||
class CookieAttrs(multidict.MultiDict):
|
||||
@staticmethod
|
||||
def _kconv(key):
|
||||
return key.lower()
|
||||
@ -300,7 +300,7 @@ def refresh_set_cookie_header(c, delta):
|
||||
e = email.utils.parsedate_tz(attrs["expires"])
|
||||
if e:
|
||||
f = email.utils.mktime_tz(e) + delta
|
||||
attrs = attrs.with_set_all("expires", [email.utils.formatdate(f)])
|
||||
attrs.set_all("expires", [email.utils.formatdate(f)])
|
||||
else:
|
||||
# This can happen when the expires tag is invalid.
|
||||
# reddit.com sends a an expires tag like this: "Thu, 31 Dec
|
||||
@ -308,7 +308,7 @@ def refresh_set_cookie_header(c, delta):
|
||||
# strictly correct according to the cookie spec. Browsers
|
||||
# appear to parse this tolerantly - maybe we should too.
|
||||
# For now, we just ignore this.
|
||||
attrs = attrs.with_delitem("expires")
|
||||
del attrs["expires"]
|
||||
|
||||
rv = format_set_cookie_header([(name, value, attrs)])
|
||||
if not rv:
|
||||
|
@ -1,11 +1,6 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
try:
|
||||
from collections.abc import MutableMapping
|
||||
except ImportError: # pragma: no cover
|
||||
from collections import MutableMapping # Workaround for Python < 3.3
|
||||
|
||||
from collections.abc import MutableMapping
|
||||
from mitmproxy.types import serializable
|
||||
|
||||
|
||||
@ -179,27 +174,6 @@ class _MultiDict(MutableMapping, serializable.Serializable, metaclass=ABCMeta):
|
||||
coll.append([key, values])
|
||||
return coll
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Get the MultiDict as a plain Python dict.
|
||||
Keys with multiple values are returned as lists.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Simple dict with duplicate values.
|
||||
>>> d = MultiDict([("name", "value"), ("a", False), ("a", 42)])
|
||||
>>> d.to_dict()
|
||||
{
|
||||
"name": "value",
|
||||
"a": [False, 42]
|
||||
}
|
||||
"""
|
||||
return {
|
||||
k: v for k, v in self.collect()
|
||||
}
|
||||
|
||||
def get_state(self):
|
||||
return self.fields
|
||||
|
||||
@ -227,46 +201,6 @@ class MultiDict(_MultiDict):
|
||||
return key
|
||||
|
||||
|
||||
class ImmutableMultiDict(MultiDict, metaclass=ABCMeta):
|
||||
def _immutable(self, *_):
|
||||
raise TypeError('{} objects are immutable'.format(self.__class__.__name__))
|
||||
|
||||
__delitem__ = set_all = insert = _immutable
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.fields)
|
||||
|
||||
def with_delitem(self, key):
|
||||
"""
|
||||
Returns:
|
||||
An updated ImmutableMultiDict. The original object will not be modified.
|
||||
"""
|
||||
ret = self.copy()
|
||||
# FIXME: This is filthy...
|
||||
super(ImmutableMultiDict, ret).__delitem__(key)
|
||||
return ret
|
||||
|
||||
def with_set_all(self, key, values):
|
||||
"""
|
||||
Returns:
|
||||
An updated ImmutableMultiDict. The original object will not be modified.
|
||||
"""
|
||||
ret = self.copy()
|
||||
# FIXME: This is filthy...
|
||||
super(ImmutableMultiDict, ret).set_all(key, values)
|
||||
return ret
|
||||
|
||||
def with_insert(self, index, key, value):
|
||||
"""
|
||||
Returns:
|
||||
An updated ImmutableMultiDict. The original object will not be modified.
|
||||
"""
|
||||
ret = self.copy()
|
||||
# FIXME: This is filthy...
|
||||
super(ImmutableMultiDict, ret).insert(index, key, value)
|
||||
return ret
|
||||
|
||||
|
||||
class MultiDictView(_MultiDict):
|
||||
"""
|
||||
The MultiDictView provides the MultiDict interface over calculated data.
|
||||
|
@ -12,10 +12,6 @@ class TMultiDict(_TMulti, multidict.MultiDict):
|
||||
pass
|
||||
|
||||
|
||||
class TImmutableMultiDict(_TMulti, multidict.ImmutableMultiDict):
|
||||
pass
|
||||
|
||||
|
||||
class TestMultiDict:
|
||||
@staticmethod
|
||||
def _multi():
|
||||
@ -176,13 +172,6 @@ class TestMultiDict:
|
||||
assert list(md.items()) == [("foo", "bar"), ("bar", "baz")]
|
||||
assert list(md.items(multi=True)) == [("foo", "bar"), ("bar", "baz"), ("Bar", "bam")]
|
||||
|
||||
def test_to_dict(self):
|
||||
md = self._multi()
|
||||
assert md.to_dict() == {
|
||||
"foo": "bar",
|
||||
"bar": ["baz", "bam"]
|
||||
}
|
||||
|
||||
def test_state(self):
|
||||
md = self._multi()
|
||||
assert len(md.get_state()) == 3
|
||||
@ -194,36 +183,6 @@ class TestMultiDict:
|
||||
assert md == md2
|
||||
|
||||
|
||||
class TestImmutableMultiDict:
|
||||
def test_modify(self):
|
||||
md = TImmutableMultiDict()
|
||||
with tutils.raises(TypeError):
|
||||
md["foo"] = "bar"
|
||||
|
||||
with tutils.raises(TypeError):
|
||||
del md["foo"]
|
||||
|
||||
with tutils.raises(TypeError):
|
||||
md.add("foo", "bar")
|
||||
|
||||
def test_hash(self):
|
||||
assert hash(TImmutableMultiDict())
|
||||
|
||||
def test_with_delitem(self):
|
||||
md = TImmutableMultiDict([("foo", "bar")])
|
||||
assert md.with_delitem("foo").fields == ()
|
||||
assert md.fields == (("foo", "bar"),)
|
||||
|
||||
def test_with_set_all(self):
|
||||
md = TImmutableMultiDict()
|
||||
assert md.with_set_all("foo", ["bar"]).fields == (("foo", "bar"),)
|
||||
assert md.fields == ()
|
||||
|
||||
def test_with_insert(self):
|
||||
md = TImmutableMultiDict()
|
||||
assert md.with_insert(0, "foo", "bar").fields == (("foo", "bar"),)
|
||||
|
||||
|
||||
class TParent:
|
||||
def __init__(self):
|
||||
self.vals = tuple()
|
||||
|
Loading…
Reference in New Issue
Block a user