2018-07-09 02:09:20 -04:00
|
|
|
import json
|
2019-01-22 15:28:48 -05:00
|
|
|
import logging
|
2018-06-27 05:37:24 -04:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
from six import text_type
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import attr
|
2018-09-13 10:15:51 -04:00
|
|
|
from zope.interface import implementer
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2018-09-13 10:15:51 -04:00
|
|
|
from twisted.internet import address, threads, udp
|
2019-01-29 04:38:29 -05:00
|
|
|
from twisted.internet._resolver import SimpleResolverComplexifier
|
|
|
|
from twisted.internet.defer import Deferred, fail, succeed
|
2018-09-13 10:15:51 -04:00
|
|
|
from twisted.internet.error import DNSLookupError
|
2019-08-28 07:18:53 -04:00
|
|
|
from twisted.internet.interfaces import (
|
|
|
|
IReactorPluggableNameResolver,
|
|
|
|
IReactorTCP,
|
|
|
|
IResolverSimple,
|
|
|
|
)
|
2018-07-09 02:09:20 -04:00
|
|
|
from twisted.python.failure import Failure
|
2019-08-28 07:18:53 -04:00
|
|
|
from twisted.test.proto_helpers import AccumulatingProtocol, MemoryReactorClock
|
2018-11-15 16:55:58 -05:00
|
|
|
from twisted.web.http import unquote
|
|
|
|
from twisted.web.http_headers import Headers
|
2020-01-03 09:19:48 -05:00
|
|
|
from twisted.web.server import Site
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
from synapse.http.site import SynapseRequest
|
2018-08-08 22:22:01 -04:00
|
|
|
from synapse.util import Clock
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
from tests.utils import setup_test_homeserver as _sth
|
|
|
|
|
2019-01-22 15:28:48 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2018-11-02 09:19:23 -04:00
|
|
|
class TimedOutException(Exception):
|
|
|
|
"""
|
|
|
|
A web query timed out.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
@attr.s
|
|
|
|
class FakeChannel(object):
|
|
|
|
"""
|
|
|
|
A fake Twisted Web Channel (the part that interfaces with the
|
|
|
|
wire).
|
|
|
|
"""
|
|
|
|
|
2020-01-03 09:19:48 -05:00
|
|
|
site = attr.ib(type=Site)
|
2018-11-06 11:00:00 -05:00
|
|
|
_reactor = attr.ib()
|
2018-07-09 19:11:39 -04:00
|
|
|
result = attr.ib(default=attr.Factory(dict))
|
2018-08-15 09:43:41 -04:00
|
|
|
_producer = None
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def json_body(self):
|
|
|
|
if not self.result:
|
|
|
|
raise Exception("No result yet.")
|
2019-06-20 05:32:02 -04:00
|
|
|
return json.loads(self.result["body"].decode("utf8"))
|
2018-08-08 22:22:01 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def code(self):
|
|
|
|
if not self.result:
|
|
|
|
raise Exception("No result yet.")
|
|
|
|
return int(self.result["code"])
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2018-11-15 16:55:58 -05:00
|
|
|
@property
|
|
|
|
def headers(self):
|
|
|
|
if not self.result:
|
|
|
|
raise Exception("No result yet.")
|
|
|
|
h = Headers()
|
|
|
|
for i in self.result["headers"]:
|
|
|
|
h.addRawHeader(*i)
|
|
|
|
return h
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
def writeHeaders(self, version, code, reason, headers):
|
|
|
|
self.result["version"] = version
|
|
|
|
self.result["code"] = code
|
|
|
|
self.result["reason"] = reason
|
|
|
|
self.result["headers"] = headers
|
|
|
|
|
|
|
|
def write(self, content):
|
2018-11-07 09:37:43 -05:00
|
|
|
assert isinstance(content, bytes), "Should be bytes! " + repr(content)
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
if "body" not in self.result:
|
|
|
|
self.result["body"] = b""
|
|
|
|
|
|
|
|
self.result["body"] += content
|
|
|
|
|
2018-08-15 09:43:41 -04:00
|
|
|
def registerProducer(self, producer, streaming):
|
|
|
|
self._producer = producer
|
2018-11-06 11:00:00 -05:00
|
|
|
self.producerStreaming = streaming
|
|
|
|
|
|
|
|
def _produce():
|
|
|
|
if self._producer:
|
|
|
|
self._producer.resumeProducing()
|
|
|
|
self._reactor.callLater(0.1, _produce)
|
|
|
|
|
|
|
|
if not streaming:
|
|
|
|
self._reactor.callLater(0.0, _produce)
|
2018-08-15 09:43:41 -04:00
|
|
|
|
|
|
|
def unregisterProducer(self):
|
|
|
|
if self._producer is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._producer = None
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
def requestDone(self, _self):
|
|
|
|
self.result["done"] = True
|
|
|
|
|
|
|
|
def getPeer(self):
|
2018-08-23 13:33:04 -04:00
|
|
|
# We give an address so that getClientIP returns a non null entry,
|
|
|
|
# causing us to record the MAU
|
2018-09-06 12:58:18 -04:00
|
|
|
return address.IPv4Address("TCP", "127.0.0.1", 3423)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
def getHost(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def transport(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
class FakeSite:
|
|
|
|
"""
|
|
|
|
A fake Twisted Web Site, with mocks of the extra things that
|
|
|
|
Synapse adds.
|
|
|
|
"""
|
|
|
|
|
|
|
|
server_version_string = b"1"
|
|
|
|
site_tag = "test"
|
2019-03-20 14:00:02 -04:00
|
|
|
access_logger = logging.getLogger("synapse.access.http.fake")
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
|
2018-11-05 13:53:44 -05:00
|
|
|
def make_request(
|
2018-11-06 11:00:00 -05:00
|
|
|
reactor,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
content=b"",
|
|
|
|
access_token=None,
|
|
|
|
request=SynapseRequest,
|
|
|
|
shorthand=True,
|
2019-03-04 05:05:39 -05:00
|
|
|
federation_auth_origin=None,
|
2018-11-05 13:53:44 -05:00
|
|
|
):
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
|
|
|
Make a web request using the given method and path, feed it the
|
|
|
|
content, and return the Request and the Channel underneath.
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
Args:
|
|
|
|
method (bytes/unicode): The HTTP request method ("verb").
|
|
|
|
path (bytes/unicode): The HTTP path, suitably URL encoded (e.g.
|
|
|
|
escaped UTF-8 & spaces and such).
|
|
|
|
content (bytes or dict): The body of the request. JSON-encoded, if
|
|
|
|
a dict.
|
|
|
|
shorthand: Whether to try and be helpful and prefix the given URL
|
|
|
|
with the usual REST API path, if it doesn't contain it.
|
2019-03-04 05:05:39 -05:00
|
|
|
federation_auth_origin (bytes|None): if set to not-None, we will add a fake
|
|
|
|
Authorization header pretenting to be the given server name.
|
2018-11-05 13:53:44 -05:00
|
|
|
|
|
|
|
Returns:
|
2019-03-04 05:05:39 -05:00
|
|
|
Tuple[synapse.http.site.SynapseRequest, channel]
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
2018-08-08 22:22:01 -04:00
|
|
|
if not isinstance(method, bytes):
|
2019-06-20 05:32:02 -04:00
|
|
|
method = method.encode("ascii")
|
2018-08-08 22:22:01 -04:00
|
|
|
|
|
|
|
if not isinstance(path, bytes):
|
2019-06-20 05:32:02 -04:00
|
|
|
path = path.encode("ascii")
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2018-11-05 13:53:44 -05:00
|
|
|
# Decorate it to be the full path, if we're using shorthand
|
2019-10-31 07:30:25 -04:00
|
|
|
if (
|
|
|
|
shorthand
|
|
|
|
and not path.startswith(b"/_matrix")
|
|
|
|
and not path.startswith(b"/_synapse")
|
|
|
|
):
|
2018-07-17 06:43:18 -04:00
|
|
|
path = b"/_matrix/client/r0/" + path
|
2018-08-08 22:22:01 -04:00
|
|
|
path = path.replace(b"//", b"/")
|
2018-07-17 06:43:18 -04:00
|
|
|
|
2018-11-15 16:55:58 -05:00
|
|
|
if not path.startswith(b"/"):
|
|
|
|
path = b"/" + path
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
if isinstance(content, text_type):
|
2019-06-20 05:32:02 -04:00
|
|
|
content = content.encode("utf8")
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
site = FakeSite()
|
2020-01-03 09:19:48 -05:00
|
|
|
channel = FakeChannel(site, reactor)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2020-01-03 09:19:48 -05:00
|
|
|
req = request(channel)
|
2018-06-27 05:37:24 -04:00
|
|
|
req.process = lambda: b""
|
|
|
|
req.content = BytesIO(content)
|
2019-06-20 05:32:02 -04:00
|
|
|
req.postpath = list(map(unquote, path[1:].split(b"/")))
|
2018-08-23 13:33:04 -04:00
|
|
|
|
|
|
|
if access_token:
|
2018-10-30 08:55:43 -04:00
|
|
|
req.requestHeaders.addRawHeader(
|
2019-06-20 05:32:02 -04:00
|
|
|
b"Authorization", b"Bearer " + access_token.encode("ascii")
|
2018-10-30 08:55:43 -04:00
|
|
|
)
|
2018-08-23 13:33:04 -04:00
|
|
|
|
2019-03-04 05:05:39 -05:00
|
|
|
if federation_auth_origin is not None:
|
|
|
|
req.requestHeaders.addRawHeader(
|
2019-05-10 01:12:11 -04:00
|
|
|
b"Authorization",
|
|
|
|
b"X-Matrix origin=%s,key=,sig=" % (federation_auth_origin,),
|
2019-03-04 05:05:39 -05:00
|
|
|
)
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
if content:
|
|
|
|
req.requestHeaders.addRawHeader(b"Content-Type", b"application/json")
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
req.requestReceived(method, path, b"1.1")
|
|
|
|
|
|
|
|
return req, channel
|
|
|
|
|
|
|
|
|
2018-08-15 09:43:41 -04:00
|
|
|
def wait_until_result(clock, request, timeout=100):
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
2018-08-15 09:43:41 -04:00
|
|
|
Wait until the request is finished.
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
|
|
|
clock.run()
|
|
|
|
x = 0
|
|
|
|
|
2018-08-15 09:43:41 -04:00
|
|
|
while not request.finished:
|
|
|
|
|
|
|
|
# If there's a producer, tell it to resume producing so we get content
|
|
|
|
if request._channel._producer:
|
|
|
|
request._channel._producer.resumeProducing()
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
x += 1
|
|
|
|
|
|
|
|
if x > timeout:
|
2018-11-02 09:19:23 -04:00
|
|
|
raise TimedOutException("Timed out waiting for request to finish.")
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
clock.advance(0.1)
|
|
|
|
|
|
|
|
|
2018-07-17 06:43:18 -04:00
|
|
|
def render(request, resource, clock):
|
|
|
|
request.render(resource)
|
2018-08-15 09:43:41 -04:00
|
|
|
wait_until_result(clock, request)
|
2018-07-17 06:43:18 -04:00
|
|
|
|
|
|
|
|
2018-09-13 10:15:51 -04:00
|
|
|
@implementer(IReactorPluggableNameResolver)
|
2018-06-27 05:37:24 -04:00
|
|
|
class ThreadedMemoryReactorClock(MemoryReactorClock):
|
|
|
|
"""
|
|
|
|
A MemoryReactorClock that supports callFromThread.
|
|
|
|
"""
|
2018-08-10 09:54:09 -04:00
|
|
|
|
2018-09-13 10:15:51 -04:00
|
|
|
def __init__(self):
|
2019-05-13 16:01:14 -04:00
|
|
|
self.threadpool = ThreadPool(self)
|
|
|
|
|
2018-09-13 10:15:51 -04:00
|
|
|
self._udp = []
|
2019-01-29 04:38:29 -05:00
|
|
|
lookups = self.lookups = {}
|
|
|
|
|
|
|
|
@implementer(IResolverSimple)
|
|
|
|
class FakeResolver(object):
|
|
|
|
def getHostByName(self, name, timeout=None):
|
|
|
|
if name not in lookups:
|
2019-05-10 01:12:11 -04:00
|
|
|
return fail(DNSLookupError("OH NO: unknown %s" % (name,)))
|
2019-01-29 04:38:29 -05:00
|
|
|
return succeed(lookups[name])
|
|
|
|
|
|
|
|
self.nameResolver = SimpleResolverComplexifier(FakeResolver())
|
2018-09-13 10:15:51 -04:00
|
|
|
super(ThreadedMemoryReactorClock, self).__init__()
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
def listenUDP(self, port, protocol, interface="", maxPacketSize=8196):
|
2018-09-13 10:15:51 -04:00
|
|
|
p = udp.Port(port, protocol, interface, maxPacketSize, self)
|
|
|
|
p.startListening()
|
|
|
|
self._udp.append(p)
|
|
|
|
return p
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
def callFromThread(self, callback, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Make the callback fire in the next reactor iteration.
|
|
|
|
"""
|
|
|
|
d = Deferred()
|
|
|
|
d.addCallback(lambda x: callback(*args, **kwargs))
|
|
|
|
self.callLater(0, d.callback, True)
|
|
|
|
return d
|
|
|
|
|
2019-05-13 16:01:14 -04:00
|
|
|
def getThreadPool(self):
|
|
|
|
return self.threadpool
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadPool:
|
|
|
|
"""
|
|
|
|
Threadless thread pool.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, reactor):
|
|
|
|
self._reactor = reactor
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def callInThreadWithCallback(self, onResult, function, *args, **kwargs):
|
|
|
|
def _(res):
|
|
|
|
if isinstance(res, Failure):
|
|
|
|
onResult(False, res)
|
|
|
|
else:
|
|
|
|
onResult(True, res)
|
|
|
|
|
|
|
|
d = Deferred()
|
|
|
|
d.addCallback(lambda x: function(*args, **kwargs))
|
|
|
|
d.addBoth(_)
|
|
|
|
self._reactor.callLater(0, d.callback, True)
|
|
|
|
return d
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2018-08-13 02:47:46 -04:00
|
|
|
def setup_test_homeserver(cleanup_func, *args, **kwargs):
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
|
|
|
Set up a synchronous test server, driven by the reactor used by
|
|
|
|
the homeserver.
|
|
|
|
"""
|
2019-12-18 05:45:12 -05:00
|
|
|
server = _sth(cleanup_func, *args, **kwargs)
|
2018-08-13 02:47:46 -04:00
|
|
|
|
2019-12-18 05:45:12 -05:00
|
|
|
database = server.config.database.get_single_database()
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
# Make the thread pool synchronous.
|
2019-12-18 05:45:12 -05:00
|
|
|
clock = server.get_clock()
|
|
|
|
|
|
|
|
for database in server.get_datastores().databases:
|
|
|
|
pool = database._db_pool
|
|
|
|
|
|
|
|
def runWithConnection(func, *args, **kwargs):
|
|
|
|
return threads.deferToThreadPool(
|
|
|
|
pool._reactor,
|
|
|
|
pool.threadpool,
|
|
|
|
pool._runWithConnection,
|
|
|
|
func,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
def runInteraction(interaction, *args, **kwargs):
|
|
|
|
return threads.deferToThreadPool(
|
|
|
|
pool._reactor,
|
|
|
|
pool.threadpool,
|
|
|
|
pool._runInteraction,
|
|
|
|
interaction,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2019-03-04 05:05:39 -05:00
|
|
|
pool.runWithConnection = runWithConnection
|
|
|
|
pool.runInteraction = runInteraction
|
2019-05-13 16:01:14 -04:00
|
|
|
pool.threadpool = ThreadPool(clock._reactor)
|
2019-03-04 05:05:39 -05:00
|
|
|
pool.running = True
|
2019-12-18 05:45:12 -05:00
|
|
|
|
|
|
|
return server
|
2018-08-08 22:22:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_clock():
|
|
|
|
clock = ThreadedMemoryReactorClock()
|
|
|
|
hs_clock = Clock(clock)
|
2019-08-30 11:28:26 -04:00
|
|
|
return clock, hs_clock
|
2018-09-18 13:17:15 -04:00
|
|
|
|
|
|
|
|
2019-01-23 06:25:36 -05:00
|
|
|
@attr.s(cmp=False)
|
2018-09-18 13:17:15 -04:00
|
|
|
class FakeTransport(object):
|
|
|
|
"""
|
|
|
|
A twisted.internet.interfaces.ITransport implementation which sends all its data
|
|
|
|
straight into an IProtocol object: it exists to connect two IProtocols together.
|
|
|
|
|
|
|
|
To use it, instantiate it with the receiving IProtocol, and then pass it to the
|
|
|
|
sending IProtocol's makeConnection method:
|
|
|
|
|
|
|
|
server = HTTPChannel()
|
|
|
|
client.makeConnection(FakeTransport(server, self.reactor))
|
|
|
|
|
|
|
|
If you want bidirectional communication, you'll need two instances.
|
|
|
|
"""
|
|
|
|
|
|
|
|
other = attr.ib()
|
|
|
|
"""The Protocol object which will receive any data written to this transport.
|
|
|
|
|
|
|
|
:type: twisted.internet.interfaces.IProtocol
|
|
|
|
"""
|
|
|
|
|
|
|
|
_reactor = attr.ib()
|
|
|
|
"""Test reactor
|
|
|
|
|
|
|
|
:type: twisted.internet.interfaces.IReactorTime
|
|
|
|
"""
|
|
|
|
|
2019-01-29 08:53:02 -05:00
|
|
|
_protocol = attr.ib(default=None)
|
|
|
|
"""The Protocol which is producing data for this transport. Optional, but if set
|
|
|
|
will get called back for connectionLost() notifications etc.
|
|
|
|
"""
|
|
|
|
|
2018-09-18 13:17:15 -04:00
|
|
|
disconnecting = False
|
2019-01-30 05:55:25 -05:00
|
|
|
disconnected = False
|
2019-11-25 11:45:50 -05:00
|
|
|
connected = True
|
2019-06-20 05:32:02 -04:00
|
|
|
buffer = attr.ib(default=b"")
|
2018-09-18 13:17:15 -04:00
|
|
|
producer = attr.ib(default=None)
|
2019-04-02 07:42:39 -04:00
|
|
|
autoflush = attr.ib(default=True)
|
2018-09-18 13:17:15 -04:00
|
|
|
|
|
|
|
def getPeer(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getHost(self):
|
|
|
|
return None
|
|
|
|
|
2019-01-29 08:53:02 -05:00
|
|
|
def loseConnection(self, reason=None):
|
|
|
|
if not self.disconnecting:
|
2019-01-30 05:55:25 -05:00
|
|
|
logger.info("FakeTransport: loseConnection(%s)", reason)
|
2019-01-29 08:53:02 -05:00
|
|
|
self.disconnecting = True
|
|
|
|
if self._protocol:
|
|
|
|
self._protocol.connectionLost(reason)
|
2019-11-01 10:07:44 -04:00
|
|
|
|
|
|
|
# if we still have data to write, delay until that is done
|
|
|
|
if self.buffer:
|
|
|
|
logger.info(
|
|
|
|
"FakeTransport: Delaying disconnect until buffer is flushed"
|
|
|
|
)
|
|
|
|
else:
|
2019-11-25 11:45:50 -05:00
|
|
|
self.connected = False
|
2019-11-01 10:07:44 -04:00
|
|
|
self.disconnected = True
|
2018-09-18 13:17:15 -04:00
|
|
|
|
|
|
|
def abortConnection(self):
|
2019-01-30 05:55:25 -05:00
|
|
|
logger.info("FakeTransport: abortConnection()")
|
2019-11-01 10:07:44 -04:00
|
|
|
|
|
|
|
if not self.disconnecting:
|
|
|
|
self.disconnecting = True
|
|
|
|
if self._protocol:
|
|
|
|
self._protocol.connectionLost(None)
|
|
|
|
|
|
|
|
self.disconnected = True
|
2018-09-18 13:17:15 -04:00
|
|
|
|
|
|
|
def pauseProducing(self):
|
2018-12-21 09:56:13 -05:00
|
|
|
if not self.producer:
|
|
|
|
return
|
|
|
|
|
2018-09-18 13:17:15 -04:00
|
|
|
self.producer.pauseProducing()
|
|
|
|
|
2018-12-21 09:56:13 -05:00
|
|
|
def resumeProducing(self):
|
|
|
|
if not self.producer:
|
|
|
|
return
|
|
|
|
self.producer.resumeProducing()
|
|
|
|
|
2018-09-18 13:17:15 -04:00
|
|
|
def unregisterProducer(self):
|
|
|
|
if not self.producer:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.producer = None
|
|
|
|
|
|
|
|
def registerProducer(self, producer, streaming):
|
|
|
|
self.producer = producer
|
|
|
|
self.producerStreaming = streaming
|
|
|
|
|
|
|
|
def _produce():
|
|
|
|
d = self.producer.resumeProducing()
|
|
|
|
d.addCallback(lambda x: self._reactor.callLater(0.1, _produce))
|
|
|
|
|
|
|
|
if not streaming:
|
|
|
|
self._reactor.callLater(0.0, _produce)
|
|
|
|
|
|
|
|
def write(self, byt):
|
2019-11-01 10:07:44 -04:00
|
|
|
if self.disconnecting:
|
|
|
|
raise Exception("Writing to disconnecting FakeTransport")
|
|
|
|
|
2018-09-18 13:17:15 -04:00
|
|
|
self.buffer = self.buffer + byt
|
|
|
|
|
2019-01-22 15:28:48 -05:00
|
|
|
# always actually do the write asynchronously. Some protocols (notably the
|
|
|
|
# TLSMemoryBIOProtocol) get very confused if a read comes back while they are
|
|
|
|
# still doing a write. Doing a callLater here breaks the cycle.
|
2019-04-02 07:42:39 -04:00
|
|
|
if self.autoflush:
|
|
|
|
self._reactor.callLater(0.0, self.flush)
|
2018-09-18 13:17:15 -04:00
|
|
|
|
|
|
|
def writeSequence(self, seq):
|
|
|
|
for x in seq:
|
|
|
|
self.write(x)
|
2019-04-02 07:42:39 -04:00
|
|
|
|
|
|
|
def flush(self, maxbytes=None):
|
|
|
|
if not self.buffer:
|
|
|
|
# nothing to do. Don't write empty buffers: it upsets the
|
|
|
|
# TLSMemoryBIOProtocol
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.disconnected:
|
|
|
|
return
|
|
|
|
|
|
|
|
if getattr(self.other, "transport") is None:
|
|
|
|
# the other has no transport yet; reschedule
|
|
|
|
if self.autoflush:
|
|
|
|
self._reactor.callLater(0.0, self.flush)
|
|
|
|
return
|
|
|
|
|
|
|
|
if maxbytes is not None:
|
|
|
|
to_write = self.buffer[:maxbytes]
|
|
|
|
else:
|
|
|
|
to_write = self.buffer
|
|
|
|
|
|
|
|
logger.info("%s->%s: %s", self._protocol, self.other, to_write)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.other.dataReceived(to_write)
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning("Exception writing to protocol: %s", e)
|
|
|
|
return
|
|
|
|
|
2019-05-10 01:12:11 -04:00
|
|
|
self.buffer = self.buffer[len(to_write) :]
|
2019-04-02 07:42:39 -04:00
|
|
|
if self.buffer and self.autoflush:
|
|
|
|
self._reactor.callLater(0.0, self.flush)
|
2019-08-28 07:18:53 -04:00
|
|
|
|
2019-11-01 10:07:44 -04:00
|
|
|
if not self.buffer and self.disconnecting:
|
|
|
|
logger.info("FakeTransport: Buffer now empty, completing disconnect")
|
|
|
|
self.disconnected = True
|
|
|
|
|
2019-08-28 07:18:53 -04:00
|
|
|
|
|
|
|
def connect_client(reactor: IReactorTCP, client_id: int) -> AccumulatingProtocol:
|
|
|
|
"""
|
|
|
|
Connect a client to a fake TCP transport.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
reactor
|
|
|
|
factory: The connecting factory to build.
|
|
|
|
"""
|
|
|
|
factory = reactor.tcpClients[client_id][2]
|
|
|
|
client = factory.buildProtocol(None)
|
|
|
|
server = AccumulatingProtocol()
|
|
|
|
server.makeConnection(FakeTransport(client, reactor))
|
|
|
|
client.makeConnection(FakeTransport(server, reactor))
|
|
|
|
|
|
|
|
reactor.tcpClients.pop(client_id)
|
|
|
|
|
|
|
|
return client, server
|