mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 02:24:18 +00:00
Language: minor refactoring and whitespace
This commit is contained in:
parent
5d5f2bdd1f
commit
a46e17459d
@ -64,6 +64,9 @@ class Token(object):
|
||||
"""
|
||||
Resolves this token to ready it for transmission. This means that
|
||||
the calculated offsets of actions are fixed.
|
||||
|
||||
settings: a language.Settings instance
|
||||
msg: The containing message
|
||||
"""
|
||||
return self
|
||||
|
||||
@ -76,7 +79,7 @@ class _ValueLiteral(Token):
|
||||
self.val = val.decode("string_escape")
|
||||
|
||||
def get_generator(self, settings):
|
||||
return generators.LiteralGenerator(self.val)
|
||||
return self.val
|
||||
|
||||
def freeze(self, settings):
|
||||
return self
|
||||
@ -215,13 +218,14 @@ Offset = pp.MatchFirst(
|
||||
class _Component(Token):
|
||||
"""
|
||||
A value component of the primary specification of an message.
|
||||
Components produce byte values desribe the bytes of the message.
|
||||
"""
|
||||
@abc.abstractmethod
|
||||
def values(self, settings): # pragma: no cover
|
||||
"""
|
||||
A sequence of value objects.
|
||||
A sequence of values, which can either be strings or generators.
|
||||
"""
|
||||
return None
|
||||
pass
|
||||
|
||||
def string(self, settings=None):
|
||||
"""
|
||||
@ -361,7 +365,7 @@ class Integer(_Component):
|
||||
return e.setParseAction(lambda x: klass(*x))
|
||||
|
||||
def values(self, settings):
|
||||
return [generators.LiteralGenerator(self.value)]
|
||||
return self.value
|
||||
|
||||
def spec(self):
|
||||
return "%s"%(self.value)
|
||||
|
@ -43,23 +43,6 @@ class TransformGenerator:
|
||||
return "'transform(%s)'"%self.gen
|
||||
|
||||
|
||||
class LiteralGenerator:
|
||||
def __init__(self, s):
|
||||
self.s = s
|
||||
|
||||
def __len__(self):
|
||||
return len(self.s)
|
||||
|
||||
def __getitem__(self, x):
|
||||
return self.s.__getitem__(x)
|
||||
|
||||
def __getslice__(self, a, b):
|
||||
return self.s.__getslice__(a, b)
|
||||
|
||||
def __repr__(self):
|
||||
return "'%s'"%self.s
|
||||
|
||||
|
||||
class RandomGenerator:
|
||||
def __init__(self, dtype, length):
|
||||
self.dtype = dtype
|
||||
|
@ -81,7 +81,7 @@ class ShortcutUserAgent(_HeaderMixin, base.OptionsOrValue):
|
||||
self.value.val.lower()
|
||||
)[2]
|
||||
else:
|
||||
value = self.value
|
||||
value = self.value.val
|
||||
return self.format_header(
|
||||
self.key.get_generator(settings),
|
||||
value
|
||||
@ -129,7 +129,7 @@ class _HTTPMessage(message.Message):
|
||||
vals.extend(h.values(settings))
|
||||
vals.append("\r\n")
|
||||
if self.body:
|
||||
vals.append(self.body.value.get_generator(settings))
|
||||
vals.extend(self.body.values(settings))
|
||||
return vals
|
||||
|
||||
|
||||
@ -169,11 +169,9 @@ class Response(_HTTPMessage):
|
||||
l.extend(self.reason.values(settings))
|
||||
else:
|
||||
l.append(
|
||||
generators.LiteralGenerator(
|
||||
http_status.RESPONSES.get(
|
||||
code,
|
||||
"Unknown code"
|
||||
)
|
||||
http_status.RESPONSES.get(
|
||||
code,
|
||||
"Unknown code"
|
||||
)
|
||||
)
|
||||
return l
|
||||
@ -205,7 +203,9 @@ class Response(_HTTPMessage):
|
||||
if not self.body:
|
||||
length = 0
|
||||
else:
|
||||
length = len(self.body.value.get_generator(settings))
|
||||
length = sum(
|
||||
len(i) for i in self.body.values(settings)
|
||||
)
|
||||
tokens.append(
|
||||
Header(
|
||||
base.ValueLiteral("Content-Length"),
|
||||
@ -301,7 +301,9 @@ class Request(_HTTPMessage):
|
||||
if not self.raw:
|
||||
if not get_header("Content-Length", self.headers):
|
||||
if self.body:
|
||||
length = len(self.body.value.get_generator(settings))
|
||||
length = sum(
|
||||
len(i) for i in self.body.values(settings)
|
||||
)
|
||||
tokens.append(
|
||||
Header(
|
||||
base.ValueLiteral("Content-Length"),
|
||||
|
@ -208,7 +208,7 @@ class TestMisc:
|
||||
assert e.freeze({})
|
||||
assert e.values({})
|
||||
|
||||
def test_value(self):
|
||||
def test_prevalue(self):
|
||||
class TT(base.PreValue):
|
||||
preamble = "m"
|
||||
e = TT.expr()
|
||||
@ -226,6 +226,7 @@ class TestMisc:
|
||||
|
||||
class TKeyValue(base.KeyValue):
|
||||
preamble = "h"
|
||||
|
||||
def values(self, settings):
|
||||
return [
|
||||
self.key.get_generator(settings),
|
||||
|
@ -14,13 +14,6 @@ def test_randomgenerator():
|
||||
assert g[0]
|
||||
|
||||
|
||||
def test_literalgenerator():
|
||||
g = generators.LiteralGenerator("one")
|
||||
assert repr(g)
|
||||
assert g[:] == "one"
|
||||
assert g[1] == "n"
|
||||
|
||||
|
||||
def test_filegenerator():
|
||||
with tutils.tmpdir() as t:
|
||||
path = os.path.join(t, "foo")
|
||||
@ -41,7 +34,7 @@ def test_filegenerator():
|
||||
def test_transform_generator():
|
||||
def trans(offset, data):
|
||||
return "a" * len(data)
|
||||
g = generators.LiteralGenerator("one")
|
||||
g = "one"
|
||||
t = generators.TransformGenerator(g, trans)
|
||||
assert len(t) == len(g)
|
||||
assert t[0] == "a"
|
||||
|
@ -309,11 +309,11 @@ def test_shortcuts():
|
||||
def test_user_agent():
|
||||
e = http.ShortcutUserAgent.expr()
|
||||
v = e.parseString("ua")[0]
|
||||
assert "Android" in str(v.values({})[2])
|
||||
assert "Android" in v.string()
|
||||
|
||||
e = http.ShortcutUserAgent.expr()
|
||||
v = e.parseString("u'a'")[0]
|
||||
assert "Android" not in str(v.values({})[2])
|
||||
assert "Android" not in v.string()
|
||||
|
||||
v = e.parseString("u@100'")[0]
|
||||
assert len(str(v.freeze({}).value)) > 100
|
||||
|
Loading…
Reference in New Issue
Block a user