diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-02-10 14:27:39 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-02-10 14:27:39 +1300 |
commit | 9c985f2d20e4086881bde5ecc63c21a208393894 (patch) | |
tree | 93b78b58b9a6111743ebb99e22a307a376866429 /libmproxy/flow.py | |
parent | d9fda2b207c7589d19c45ac132311f3879b93483 (diff) | |
download | mitmproxy-9c985f2d20e4086881bde5ecc63c21a208393894.tar.gz mitmproxy-9c985f2d20e4086881bde5ecc63c21a208393894.tar.bz2 mitmproxy-9c985f2d20e4086881bde5ecc63c21a208393894.zip |
Methods for getting and setting form urlencoded data on Request.
Diffstat (limited to 'libmproxy/flow.py')
-rw-r--r-- | libmproxy/flow.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/libmproxy/flow.py b/libmproxy/flow.py index e929440e..85c48ce1 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -8,6 +8,8 @@ import tnetstring, filt, script, utils, encoding, proxy from email.utils import parsedate_tz, formatdate, mktime_tz import controller, version +HDR_FORM_URLENCODED = "x-www-form-urlencoded" + class RunException(Exception): def __init__(self, msg, returncode, errout): Exception.__init__(self, msg) @@ -56,6 +58,8 @@ class Headers: return new def __setitem__(self, k, hdrs): + if isinstance(hdrs, basestring): + raise ValueError("Header values should be lists.") k = self._kconv(k) new = self._filter_lst(k, self.lst) for i in hdrs: @@ -312,6 +316,25 @@ class Request(HTTPMsg): host = "%s:%s"%(self.host, self.port) return host + def get_form_urlencoded(self): + """ + Retrieves the URL-encoded form data, returning a list of (key, + value) tuples. Returns an empty list if there is no data or the + content-type indicates non-form data. + """ + hv = [i.lower() for i in self.headers["content-type"]] + if HDR_FORM_URLENCODED in hv: + return utils.urldecode(self.content) + return [] + + def set_form_urlencoded(self, data): + """ + Sets the body to the URL-encoded form data, and adds the + appropriate content-type header. + """ + self.headers["content-type"] = [HDR_FORM_URLENCODED] + self.content = utils.urlencode(data) + def get_query(self): """ Gets the request query string. Returns a list of (key, value) |