2015-08-31 11:43:30 +00:00
|
|
|
from __future__ import (absolute_import, print_function, division)
|
2014-03-11 01:16:22 +00:00
|
|
|
import copy
|
2014-09-18 00:22:10 +00:00
|
|
|
import uuid
|
2015-08-30 01:42:11 +00:00
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
from .. import stateobject, utils, version
|
2015-08-30 13:27:29 +00:00
|
|
|
from .connections import ClientConnection, ServerConnection
|
2014-03-10 20:57:50 +00:00
|
|
|
|
|
|
|
|
2014-09-16 23:35:14 +00:00
|
|
|
class Error(stateobject.StateObject):
|
2014-02-04 04:02:17 +00:00
|
|
|
"""
|
|
|
|
An Error.
|
|
|
|
|
2014-09-16 21:40:25 +00:00
|
|
|
This is distinct from an protocol error response (say, a HTTP code 500),
|
|
|
|
which is represented by a normal HTTPResponse object. This class is
|
|
|
|
responsible for indicating errors that fall outside of normal protocol
|
|
|
|
communications, like interrupted connections, timeouts, protocol errors.
|
2014-02-04 04:02:17 +00:00
|
|
|
|
|
|
|
Exposes the following attributes:
|
|
|
|
|
|
|
|
flow: Flow object
|
|
|
|
msg: Message describing the error
|
|
|
|
timestamp: Seconds since the epoch
|
|
|
|
"""
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
def __init__(self, msg, timestamp=None):
|
|
|
|
"""
|
|
|
|
@type msg: str
|
|
|
|
@type timestamp: float
|
|
|
|
"""
|
2014-02-05 13:33:17 +00:00
|
|
|
self.flow = None # will usually be set by the flow backref mixin
|
2014-02-04 04:02:17 +00:00
|
|
|
self.msg = msg
|
|
|
|
self.timestamp = timestamp or utils.timestamp()
|
|
|
|
|
|
|
|
_stateobject_attributes = dict(
|
|
|
|
msg=str,
|
|
|
|
timestamp=float
|
|
|
|
)
|
|
|
|
|
2014-02-07 17:14:15 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.msg
|
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
@classmethod
|
2014-09-16 23:35:14 +00:00
|
|
|
def from_state(cls, state):
|
2014-09-16 21:40:25 +00:00
|
|
|
# the default implementation assumes an empty constructor. Override
|
|
|
|
# accordingly.
|
|
|
|
f = cls(None)
|
2014-09-16 23:35:14 +00:00
|
|
|
f.load_state(state)
|
2014-02-04 04:02:17 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
c = copy.copy(self)
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
2014-09-16 23:35:14 +00:00
|
|
|
class Flow(stateobject.StateObject):
|
2014-09-05 13:16:20 +00:00
|
|
|
"""
|
|
|
|
A Flow is a collection of objects representing a single transaction.
|
|
|
|
This class is usually subclassed for each protocol, e.g. HTTPFlow.
|
|
|
|
"""
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2014-10-18 16:29:35 +00:00
|
|
|
def __init__(self, type, client_conn, server_conn, live=None):
|
|
|
|
self.type = type
|
2014-09-18 00:22:10 +00:00
|
|
|
self.id = str(uuid.uuid4())
|
2014-02-04 04:02:17 +00:00
|
|
|
self.client_conn = client_conn
|
2014-02-05 19:26:47 +00:00
|
|
|
"""@type: ClientConnection"""
|
2014-02-04 04:02:17 +00:00
|
|
|
self.server_conn = server_conn
|
2014-02-05 19:26:47 +00:00
|
|
|
"""@type: ServerConnection"""
|
2014-09-03 21:44:54 +00:00
|
|
|
self.live = live
|
2014-08-24 12:22:11 +00:00
|
|
|
"""@type: LiveConnection"""
|
2014-02-05 19:26:47 +00:00
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
self.error = None
|
2014-02-05 19:26:47 +00:00
|
|
|
"""@type: Error"""
|
2014-12-23 19:33:42 +00:00
|
|
|
self.intercepted = False
|
|
|
|
"""@type: bool"""
|
2014-02-05 19:26:47 +00:00
|
|
|
self._backup = None
|
2014-12-23 19:33:42 +00:00
|
|
|
self.reply = None
|
2014-02-04 04:02:17 +00:00
|
|
|
|
|
|
|
_stateobject_attributes = dict(
|
2014-09-18 00:22:10 +00:00
|
|
|
id=str,
|
2014-02-04 04:02:17 +00:00
|
|
|
error=Error,
|
|
|
|
client_conn=ClientConnection,
|
|
|
|
server_conn=ServerConnection,
|
2014-12-23 19:33:42 +00:00
|
|
|
type=str,
|
|
|
|
intercepted=bool
|
2014-02-04 04:02:17 +00:00
|
|
|
)
|
|
|
|
|
2014-09-17 01:58:56 +00:00
|
|
|
def get_state(self, short=False):
|
|
|
|
d = super(Flow, self).get_state(short)
|
2014-02-04 04:02:17 +00:00
|
|
|
d.update(version=version.IVERSION)
|
2014-12-25 15:10:47 +00:00
|
|
|
if self._backup and self._backup != d:
|
|
|
|
if short:
|
|
|
|
d.update(modified=True)
|
|
|
|
else:
|
|
|
|
d.update(backup=self._backup)
|
2014-02-04 04:02:17 +00:00
|
|
|
return d
|
|
|
|
|
2014-02-05 13:33:17 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self is other
|
|
|
|
|
2014-02-04 04:02:17 +00:00
|
|
|
def copy(self):
|
|
|
|
f = copy.copy(self)
|
|
|
|
|
2014-12-24 00:07:57 +00:00
|
|
|
f.id = str(uuid.uuid4())
|
|
|
|
f.live = False
|
2014-02-04 04:02:17 +00:00
|
|
|
f.client_conn = self.client_conn.copy()
|
|
|
|
f.server_conn = self.server_conn.copy()
|
|
|
|
|
|
|
|
if self.error:
|
|
|
|
f.error = self.error.copy()
|
|
|
|
return f
|
|
|
|
|
|
|
|
def modified(self):
|
|
|
|
"""
|
|
|
|
Has this Flow been modified?
|
|
|
|
"""
|
|
|
|
if self._backup:
|
2014-09-16 23:35:14 +00:00
|
|
|
return self._backup != self.get_state()
|
2014-02-04 04:02:17 +00:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def backup(self, force=False):
|
|
|
|
"""
|
|
|
|
Save a backup of this Flow, which can be reverted to using a
|
|
|
|
call to .revert().
|
|
|
|
"""
|
|
|
|
if not self._backup:
|
2014-09-16 23:35:14 +00:00
|
|
|
self._backup = self.get_state()
|
2014-02-04 04:02:17 +00:00
|
|
|
|
|
|
|
def revert(self):
|
|
|
|
"""
|
|
|
|
Revert to the last backed up state.
|
|
|
|
"""
|
|
|
|
if self._backup:
|
2014-09-16 23:35:14 +00:00
|
|
|
self.load_state(self._backup)
|
2014-03-10 20:57:50 +00:00
|
|
|
self._backup = None
|
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
def kill(self, master):
|
|
|
|
"""
|
|
|
|
Kill this request.
|
|
|
|
"""
|
2015-08-30 13:27:29 +00:00
|
|
|
from ..protocol import Kill
|
|
|
|
|
2014-12-23 19:33:42 +00:00
|
|
|
self.error = Error("Connection killed")
|
|
|
|
self.intercepted = False
|
2015-08-30 13:27:29 +00:00
|
|
|
self.reply(Kill)
|
2014-12-23 19:33:42 +00:00
|
|
|
master.handle_error(self)
|
|
|
|
|
|
|
|
def intercept(self, master):
|
|
|
|
"""
|
|
|
|
Intercept this Flow. Processing will stop until accept_intercept is
|
|
|
|
called.
|
|
|
|
"""
|
2014-12-24 00:07:57 +00:00
|
|
|
if self.intercepted:
|
|
|
|
return
|
2014-12-23 19:33:42 +00:00
|
|
|
self.intercepted = True
|
|
|
|
master.handle_intercept(self)
|
|
|
|
|
|
|
|
def accept_intercept(self, master):
|
|
|
|
"""
|
|
|
|
Continue with the flow - called after an intercept().
|
|
|
|
"""
|
2014-12-24 00:07:57 +00:00
|
|
|
if not self.intercepted:
|
|
|
|
return
|
2014-12-23 19:33:42 +00:00
|
|
|
self.intercepted = False
|
|
|
|
self.reply()
|
|
|
|
master.handle_accept_intercept(self)
|