mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
add Serializeable.copy
This commit is contained in:
parent
f1c5721c8c
commit
4ee8808b44
@ -26,14 +26,6 @@ class MessageMixin(object):
|
|||||||
return self.content
|
return self.content
|
||||||
return encoding.decode(ce, self.content)
|
return encoding.decode(ce, self.content)
|
||||||
|
|
||||||
def copy(self):
|
|
||||||
c = copy.copy(self)
|
|
||||||
if hasattr(self, "data"): # FIXME remove condition
|
|
||||||
c.data = copy.copy(self.data)
|
|
||||||
|
|
||||||
c.headers = self.headers.copy()
|
|
||||||
return c
|
|
||||||
|
|
||||||
def replace(self, pattern, repl, *args, **kwargs):
|
def replace(self, pattern, repl, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Replaces a regular expression pattern with repl in both the headers
|
Replaces a regular expression pattern with repl in both the headers
|
||||||
|
@ -5,7 +5,7 @@ Unicode Handling
|
|||||||
See also: http://lucumr.pocoo.org/2013/7/2/the-updated-guide-to-unicode/
|
See also: http://lucumr.pocoo.org/2013/7/2/the-updated-guide-to-unicode/
|
||||||
"""
|
"""
|
||||||
from __future__ import absolute_import, print_function, division
|
from __future__ import absolute_import, print_function, division
|
||||||
import copy
|
|
||||||
try:
|
try:
|
||||||
from collections.abc import MutableMapping
|
from collections.abc import MutableMapping
|
||||||
except ImportError: # pragma: no cover
|
except ImportError: # pragma: no cover
|
||||||
@ -190,9 +190,6 @@ class Headers(MutableMapping, Serializable):
|
|||||||
[name, value] for value in values
|
[name, value] for value in values
|
||||||
)
|
)
|
||||||
|
|
||||||
def copy(self):
|
|
||||||
return Headers(copy.copy(self.fields))
|
|
||||||
|
|
||||||
def get_state(self):
|
def get_state(self):
|
||||||
return tuple(tuple(field) for field in self.fields)
|
return tuple(tuple(field) for field in self.fields)
|
||||||
|
|
||||||
|
@ -43,9 +43,6 @@ class MessageData(utils.Serializable):
|
|||||||
|
|
||||||
|
|
||||||
class Message(utils.Serializable):
|
class Message(utils.Serializable):
|
||||||
def __init__(self, data):
|
|
||||||
self.data = data
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Message):
|
if isinstance(other, Message):
|
||||||
return self.data == other.data
|
return self.data == other.data
|
||||||
@ -62,6 +59,7 @@ class Message(utils.Serializable):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_state(cls, state):
|
def from_state(cls, state):
|
||||||
|
state["headers"] = Headers.from_state(state["headers"])
|
||||||
return cls(**state)
|
return cls(**state)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -42,8 +42,7 @@ class Request(Message):
|
|||||||
An HTTP request.
|
An HTTP request.
|
||||||
"""
|
"""
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
data = RequestData(*args, **kwargs)
|
self.data = RequestData(*args, **kwargs)
|
||||||
super(Request, self).__init__(data)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if self.host and self.port:
|
if self.host and self.port:
|
||||||
|
@ -29,8 +29,7 @@ class Response(Message):
|
|||||||
An HTTP response.
|
An HTTP response.
|
||||||
"""
|
"""
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
data = ResponseData(*args, **kwargs)
|
self.data = ResponseData(*args, **kwargs)
|
||||||
super(Response, self).__init__(data)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if self.content:
|
if self.content:
|
||||||
|
@ -41,6 +41,9 @@ class Serializable(object):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
return self.from_state(self.get_state())
|
||||||
|
|
||||||
|
|
||||||
def always_bytes(unicode_or_bytes, *encode_args):
|
def always_bytes(unicode_or_bytes, *encode_args):
|
||||||
if isinstance(unicode_or_bytes, six.text_type):
|
if isinstance(unicode_or_bytes, six.text_type):
|
||||||
|
@ -139,3 +139,30 @@ def test_parse_content_type():
|
|||||||
|
|
||||||
v = p("text/html; charset=UTF-8")
|
v = p("text/html; charset=UTF-8")
|
||||||
assert v == ('text', 'html', {'charset': 'UTF-8'})
|
assert v == ('text', 'html', {'charset': 'UTF-8'})
|
||||||
|
|
||||||
|
|
||||||
|
class SerializableDummy(utils.Serializable):
|
||||||
|
def __init__(self, i):
|
||||||
|
self.i = i
|
||||||
|
|
||||||
|
def get_state(self):
|
||||||
|
return self.i
|
||||||
|
|
||||||
|
def set_state(self, i):
|
||||||
|
self.i = i
|
||||||
|
|
||||||
|
def from_state(self, state):
|
||||||
|
return type(self)(state)
|
||||||
|
|
||||||
|
|
||||||
|
class TestSerializable:
|
||||||
|
|
||||||
|
def test_copy(self):
|
||||||
|
a = SerializableDummy(42)
|
||||||
|
assert a.i == 42
|
||||||
|
b = a.copy()
|
||||||
|
assert b.i == 42
|
||||||
|
|
||||||
|
a.set_state(1)
|
||||||
|
assert a.i == 1
|
||||||
|
assert b.i == 42
|
||||||
|
Loading…
Reference in New Issue
Block a user