mitmproxy/libpathod/language/__init__.py

83 lines
2.1 KiB
Python
Raw Normal View History

import time
import pyparsing as pp
from . import base, http, websockets, writer, exceptions
from exceptions import *
from base import Settings
def parse_response(s):
"""
May raise ParseException
"""
try:
s = s.decode("ascii")
except UnicodeError:
raise exceptions.ParseException("Spec must be valid ASCII.", 0, 0)
try:
return http.Response.expr().parseString(s, parseAll=True)[0]
2015-05-30 00:03:13 +00:00
except pp.ParseException as v:
raise exceptions.ParseException(v.msg, v.line, v.col)
def parse_requests(s):
"""
May raise ParseException
"""
try:
s = s.decode("ascii")
except UnicodeError:
raise exceptions.ParseException("Spec must be valid ASCII.", 0, 0)
try:
reqs = pp.OneOrMore(
pp.Or(
[
websockets.WebsocketFrame.expr(),
http.Request.expr(),
]
)
).parseString(s, parseAll=True)
2015-05-30 00:03:13 +00:00
except pp.ParseException as v:
raise exceptions.ParseException(v.msg, v.line, v.col)
expanded = []
for i in reqs:
if i.times:
for j in range(int(i.times.value)):
2015-05-17 04:42:59 +00:00
expanded.append(i.strike_token("times"))
else:
expanded.append(i)
return expanded
def serve(msg, fp, settings):
"""
fp: The file pointer to write to.
request_host: If this a request, this is the connecting host. If
None, we assume it's a response. Used to decide what standard
modifications to make if raw is not set.
Calling this function may modify the object.
"""
msg = msg.resolve(settings)
started = time.time()
vals = msg.values(settings)
vals.reverse()
2015-05-30 00:03:13 +00:00
actions = sorted(msg.actions[:])
actions.reverse()
actions = [i.intermediate(settings) for i in actions]
disconnect = writer.write_values(fp, vals, actions[:])
duration = time.time() - started
ret = dict(
disconnect = disconnect,
started = started,
duration = duration,
)
ret.update(msg.log(settings))
return ret