aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod/language.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-10-30 16:04:48 +1300
committerAldo Cortesi <aldo@nullcube.com>2012-10-30 16:08:36 +1300
commitf8df0a1e75422e5ffed1c7131d0c017afa227faa (patch)
treebcddeff4f1182864408db569f3e80ecfbc181d3a /libpathod/language.py
parenta09584b9e65d42b4a0f63be7d4e55c113d5fbc89 (diff)
downloadmitmproxy-f8df0a1e75422e5ffed1c7131d0c017afa227faa.tar.gz
mitmproxy-f8df0a1e75422e5ffed1c7131d0c017afa227faa.tar.bz2
mitmproxy-f8df0a1e75422e5ffed1c7131d0c017afa227faa.zip
Add a .values method to messages, simplify a lot of stuff as a consequence.
Diffstat (limited to 'libpathod/language.py')
-rw-r--r--libpathod/language.py48
1 files changed, 19 insertions, 29 deletions
diff --git a/libpathod/language.py b/libpathod/language.py
index 03cf66c7..706dd6b7 100644
--- a/libpathod/language.py
+++ b/libpathod/language.py
@@ -88,14 +88,7 @@ def serve(msg, fp, settings, request_host=None):
msg = msg.resolve(settings, request_host)
started = time.time()
- hdrs = msg.headervals(settings, request_host)
-
- vals = msg.preamble(settings)
- vals.append("\r\n")
- vals.extend(hdrs)
- vals.append("\r\n")
- if msg.body:
- vals.append(msg.body.value.get_generator(settings))
+ vals = msg.values(settings)
vals.reverse()
actions = msg.actions[:]
@@ -222,7 +215,7 @@ class _Token(object):
"""
return None
- def resolve(self, msg, settings, request_host): # pragma: no cover
+ def resolve(self, msg, settings): # pragma: no cover
"""
Resolves this token to ready it for transmission. This means that
the calculated offsets of actions are fixed.
@@ -553,13 +546,13 @@ class _Action(_Token):
def __init__(self, offset):
self.offset = offset
- def resolve(self, msg, settings, request_host):
+ def resolve(self, msg, settings):
"""
Resolves offset specifications to a numeric offset. Returns a copy
of the action object.
"""
c = copy.copy(self)
- l = msg.length(settings, request_host)
+ l = msg.length(settings)
if c.offset == "r":
c.offset = random.randrange(l)
elif c.offset == "a":
@@ -677,18 +670,11 @@ class _Message(object):
def headers(self):
return self._get_tokens(_Header)
- def length(self, settings, request_host):
+ def length(self, settings):
"""
Calculate the length of the base message without any applied actions.
"""
- l = sum(len(x) for x in self.preamble(settings))
- l += 2
- for h in self.headervals(settings, request_host):
- l += len(h)
- l += 2
- if self.body:
- l += len(self.body.value.get_generator(settings))
- return l
+ return sum(len(x) for x in self.values(settings))
def preview_safe(self):
"""
@@ -697,11 +683,11 @@ class _Message(object):
tokens = [i for i in self.tokens if not isinstance(i, PauseAt)]
return self.__class__(tokens)
- def maximum_length(self, settings, request_host):
+ def maximum_length(self, settings):
"""
Calculate the maximum length of the base message with all applied actions.
"""
- l = self.length(settings, request_host)
+ l = self.length(settings)
for i in self.actions:
if isinstance(i, InjectAt):
l += len(i.value.get_generator(settings))
@@ -734,13 +720,7 @@ class _Message(object):
)
)
intermediate = self.__class__(tokens)
- return self.__class__([i.resolve(intermediate, settings, request_host) for i in tokens])
-
- def headervals(self, settings, request_host):
- values = []
- for h in self.headers:
- values.extend(h.values(settings))
- return values
+ return self.__class__([i.resolve(intermediate, settings) for i in tokens])
@abc.abstractmethod
def preamble(self, settings): # pragma: no cover
@@ -768,6 +748,16 @@ class _Message(object):
ret["spec"] = self.spec()
return ret
+ def values(self, settings):
+ vals = self.preamble(settings)
+ vals.append("\r\n")
+ for h in self.headers:
+ vals.extend(h.values(settings))
+ vals.append("\r\n")
+ if self.body:
+ vals.append(self.body.value.get_generator(settings))
+ return vals
+
Sep = pp.Optional(pp.Literal(":")).suppress()