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