diff options
Diffstat (limited to 'mitmproxy/addons/setheaders.py')
| -rw-r--r-- | mitmproxy/addons/setheaders.py | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/mitmproxy/addons/setheaders.py b/mitmproxy/addons/setheaders.py index 601e7521..95cf9a09 100644 --- a/mitmproxy/addons/setheaders.py +++ b/mitmproxy/addons/setheaders.py @@ -2,6 +2,43 @@ from mitmproxy import exceptions from mitmproxy import flowfilter +def parse_setheader(s): + """ + Returns a (pattern, regex, replacement) tuple. + + The general form for a replacement hook is as follows: + + /patt/regex/replacement + + The first character specifies the separator. Example: + + :~q:foo:bar + + If only two clauses are specified, the pattern is set to match + universally (i.e. ".*"). Example: + + /foo/bar/ + + Clauses are parsed from left to right. Extra separators are taken to be + part of the final clause. For instance, the replacement clause below is + "foo/bar/": + + /one/two/foo/bar/ + """ + sep, rem = s[0], s[1:] + parts = rem.split(sep, 2) + if len(parts) == 2: + patt = ".*" + a, b = parts + elif len(parts) == 3: + patt, a, b = parts + else: + raise exceptions.OptionsError( + "Invalid replacement specifier: %s" % s + ) + return patt, a, b + + class SetHeaders: def __init__(self): self.lst = [] @@ -16,7 +53,12 @@ class SetHeaders: """ if "setheaders" in updated: self.lst = [] - for fpatt, header, value in options.setheaders: + for shead in options.setheaders: + if isinstance(shead, str): + fpatt, header, value = parse_setheader(shead) + else: + fpatt, header, value = shead + flt = flowfilter.parse(fpatt) if not flt: raise exceptions.OptionsError( |
