A nicer API.

This commit is contained in:
Itamar Turner-Trauring
2022-06-30 14:26:36 -04:00
parent 03c515191e
commit d1bdce9682
3 changed files with 22 additions and 19 deletions

View File

@ -64,7 +64,6 @@ from allmydata.interfaces import (
from allmydata.nodemaker import NodeMaker
from allmydata.blacklist import Blacklist
from allmydata import node
from .protocol_switch import update_foolscap_or_http_class
KiB=1024
@ -820,7 +819,9 @@ class _Client(node.Node, pollmixin.PollMixin):
furl_file = self.config.get_private_path("storage.furl").encode(get_filesystem_encoding())
furl = self.tub.registerReference(FoolscapStorageServer(ss), furlFile=furl_file)
(_, _, swissnum) = furl.rpartition("/")
update_foolscap_or_http_class(self.tub.negotiationClass, self.tub.myCertificate, ss, swissnum.encode("ascii"))
self.tub.negotiationClass.add_storage_server(
self.tub.myCertificate, ss, swissnum.encode("ascii")
)
announcement["anonymous-storage-FURL"] = furl

View File

@ -55,7 +55,7 @@ from allmydata.util.yamlutil import (
from . import (
__full_version__,
)
from .protocol_switch import create_foolscap_or_http_class
from .protocol_switch import support_foolscap_and_https
def _common_valid_config():
@ -709,7 +709,7 @@ def create_tub(tub_options, default_connection_handlers, foolscap_connection_han
the new Tub via `Tub.setOption`
"""
tub = Tub(**kwargs)
tub.negotiationClass = create_foolscap_or_http_class()
support_foolscap_and_https(tub)
for (name, value) in list(tub_options.items()):
tub.setOption(name, value)
handlers = default_connection_handlers.copy()

View File

@ -6,9 +6,10 @@ simple as possible, with no extra configuration needed. Listening on the same
port means a user upgrading Tahoe-LAFS will automatically get HTTPS working
with no additional changes.
Use ``create_foolscap_or_http_class()`` to create a new subclass per ``Tub``,
and then ``update_foolscap_or_http_class()`` to add the relevant information to
the subclass once it becomes available later in the configuration process.
Use ``support_foolscap_and_https()`` to create a new subclass for a ``Tub``
instance, and then ``add_storage_server()`` on the resulting class to add the
relevant information for a storage server once it becomes available later in
the configuration process.
"""
from twisted.internet.protocol import Protocol
@ -19,6 +20,7 @@ from twisted.protocols.tls import TLSMemoryBIOFactory
from twisted.internet import reactor
from foolscap.negotiate import Negotiation
from foolscap.api import Tub
from .storage.http_server import HTTPServer
from .storage.server import StorageServer
@ -54,6 +56,16 @@ class _FoolscapOrHttps(Protocol, metaclass=_PretendToBeNegotiation):
_timeout: IDelayedCall
@classmethod
def add_storage_server(cls, certificate, storage_server, swissnum):
"""
Add the various parameters needed by a ``Tub``-specific
``_FoolscapOrHttps`` subclass.
"""
cls.certificate = certificate
cls.storage_server = storage_server
cls.swissnum = swissnum
def __init__(self, *args, **kwargs):
self._foolscap: Negotiation = Negotiation(*args, **kwargs)
self._buffer: bytes = b""
@ -124,7 +136,7 @@ class _FoolscapOrHttps(Protocol, metaclass=_PretendToBeNegotiation):
self.__dict__ = protocol.__dict__
def create_foolscap_or_http_class():
def support_foolscap_and_https(tub: Tub):
"""
Create a new Foolscap-or-HTTPS protocol class for a specific ``Tub``
instance.
@ -133,14 +145,4 @@ def create_foolscap_or_http_class():
class FoolscapOrHttpWithCert(_FoolscapOrHttps):
pass
return FoolscapOrHttpWithCert
def update_foolscap_or_http_class(cls, certificate, storage_server, swissnum):
"""
Add the various parameters needed by a ``Tub``-specific
``_FoolscapOrHttps`` subclass.
"""
cls.certificate = certificate
cls.storage_server = storage_server
cls.swissnum = swissnum
tub.negotiationClass = FoolscapOrHttpWithCert # type: ignore