aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/http/message.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-05-18 18:46:42 -0700
committerMaximilian Hils <git@maximilianhils.com>2016-05-18 18:46:42 -0700
commit44ac64aa7235362acbb96e0f12aa27534580e575 (patch)
treec03b8c3519c273a4f42b60cb2bce8cc0dd524925 /netlib/http/message.py
parent4c3fb8f5097fad2c5de96104dae3f8026b0b4666 (diff)
downloadmitmproxy-44ac64aa7235362acbb96e0f12aa27534580e575.tar.gz
mitmproxy-44ac64aa7235362acbb96e0f12aa27534580e575.tar.bz2
mitmproxy-44ac64aa7235362acbb96e0f12aa27534580e575.zip
add MultiDict
This commit introduces MultiDict, a multi-dictionary similar to ODict, but with improved semantics (as in the Headers class). MultiDict fixes a few issues that were present in the Request/Response API. In particular, `request.cookies["foo"] = "bar"` has previously been a no-op, as the cookies property returned a mutable _copy_ of the cookies.
Diffstat (limited to 'netlib/http/message.py')
-rw-r--r--netlib/http/message.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/netlib/http/message.py b/netlib/http/message.py
index da9681a0..262ef3e1 100644
--- a/netlib/http/message.py
+++ b/netlib/http/message.py
@@ -4,6 +4,7 @@ import warnings
import six
+from ..multidict import MultiDict
from .headers import Headers
from .. import encoding, utils
@@ -235,3 +236,37 @@ class decoded(object):
def __exit__(self, type, value, tb):
if self.ce:
self.message.encode(self.ce)
+
+
+class MessageMultiDict(MultiDict):
+ """
+ A MultiDict that provides a proxy view to the underlying message.
+ """
+
+ def __init__(self, attr, message):
+ if False:
+ # We do not want to call the parent constructor here as that
+ # would cause an unnecessary parse/unparse pass.
+ # This is here to silence linters. Message
+ super(MessageMultiDict, self).__init__(None)
+ self._attr = attr
+ self._message = message # type: Message
+
+ @staticmethod
+ def _kconv(key):
+ # All request-attributes are case-sensitive.
+ return key
+
+ @staticmethod
+ def _reduce_values(values):
+ # We just return the first element if
+ # multiple elements exist with the same key.
+ return values[0]
+
+ @property
+ def fields(self):
+ return getattr(self._message, "_" + self._attr)
+
+ @fields.setter
+ def fields(self, value):
+ setattr(self._message, self._attr, value)