More cleanups.

This commit is contained in:
Itamar Turner-Trauring 2022-06-23 12:43:46 -04:00
parent 026d63cd6a
commit d70f583172

View File

@ -30,7 +30,8 @@ class FoolscapOrHttp(Protocol, metaclass=PretendToBeNegotiation):
Pretends to be a ``foolscap.negotiate.Negotiation`` instance. Pretends to be a ``foolscap.negotiate.Negotiation`` instance.
""" """
# These three will be set by a subclass # These three will be set by a subclass in update_foolscap_or_http_class()
# below.
swissnum: bytes swissnum: bytes
certificate: PrivateCertificate certificate: PrivateCertificate
storage_server: StorageServer storage_server: StorageServer
@ -53,19 +54,18 @@ class FoolscapOrHttp(Protocol, metaclass=PretendToBeNegotiation):
def __getattr__(self, name): def __getattr__(self, name):
return getattr(self._foolscap, name) return getattr(self._foolscap, name)
def _convert_to_negotiation(self) -> Tuple[bytes, Optional[ITransport]]: def _convert_to_negotiation(self):
"""Convert self to a ``Negotiation`` instance, return any buffered bytes""" """
transport = self.transport Convert self to a ``Negotiation`` instance, return any buffered
buf = self._buffer bytes and the transport if any.
"""
self.__class__ = Negotiation # type: ignore self.__class__ = Negotiation # type: ignore
self.__dict__ = self._foolscap.__dict__ self.__dict__ = self._foolscap.__dict__
return buf, transport
def initClient(self, *args, **kwargs): def initClient(self, *args, **kwargs):
# After creation, a Negotiation instance either has initClient() or # After creation, a Negotiation instance either has initClient() or
# initServer() called. SInce this is a client, we're never going to do # initServer() called. Since this is a client, we're never going to do
# HTTP. Relying on __getattr__/__setattr__ doesn't work, for some # HTTP, so we can immediately become a Negotiation instance.
# reason, so just mutate ourselves appropriately.
assert not self._buffer assert not self._buffer
self._convert_to_negotiation() self._convert_to_negotiation()
return self.initClient(*args, **kwargs) return self.initClient(*args, **kwargs)
@ -83,7 +83,9 @@ class FoolscapOrHttp(Protocol, metaclass=PretendToBeNegotiation):
# Check if it looks like a Foolscap request. If so, it can handle this # Check if it looks like a Foolscap request. If so, it can handle this
# and later data: # and later data:
if self._buffer.startswith(b"GET /id/"): if self._buffer.startswith(b"GET /id/"):
buf, transport = self._convert_to_negotiation() transport = self.transport
buf = self._buffer
self._convert_to_negotiation()
self.makeConnection(transport) self.makeConnection(transport)
self.dataReceived(buf) self.dataReceived(buf)
return return