Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def handlePLAINTEXTServer(self, header):
# the client sends us a GET message
lines = header.split("\r\n")
if not lines[0].startswith("GET "):
raise BananaError("not right")
command, url, version = lines[0].split()
if not url.startswith("/id/"):
# probably a web browser
raise BananaError("not right")
targetTubID = url[4:]
self.log("handlePLAINTEXTServer: targetTubID='%s'" % targetTubID,
level=NOISY)
if targetTubID == "":
targetTubID = None
if isSubstring("Upgrade: TLS/1.0\r\n", header):
wantEncrypted = True
else:
wantEncrypted = False
self.log("handlePLAINTEXTServer: wantEncrypted=%s" % wantEncrypted,
level=NOISY)
# we ignore the rest of the lines
if wantEncrypted and not crypto_available:
# this is a confused client, or a bad URL: if we don't have
# crypto, we couldn't have created a pb:// URL.
def receiveChild(self, token, ready_deferred=None):
assert not isinstance(token, Deferred)
assert ready_deferred is None
if self.key is None:
if self.d.has_key(token):
raise BananaError("duplicate key '%s'" % token)
self.key = token
else:
self.d[self.key] = token
self.key = None
# TODO: this is silly, of course (should pre-compute maxlen)
maxlen = reduce(max,
[len(cname) \
for cname in list(copyable.CopyableRegistry.keys())]
)
if size > maxlen:
why = "copyable-classname token is too long, %d>%d" % \
(size, maxlen)
raise Violation(why)
elif typebyte == tokens.VOCAB:
return
else:
# TODO: hack for testing
raise Violation("index token 0x%02x not STRING or VOCAB" % \
six.byte2int(typebyte))
raise BananaError("index token 0x%02x not STRING or VOCAB" % \
six.byte2int(typebyte))
def doOpen(self, opentype):
# check the opentype
if self.state == 1:
if opentype[0] not in ("instance", "none"):
raise BananaError("MethodUnslicer instance must be " +
"instance or None")
elif self.state == 2:
if opentype[0] != "class":
raise BananaError("MethodUnslicer class must be a class")
unslicer = self.open(opentype)
# TODO: apply constraint
return unslicer
def checkToken(self, typebyte, size):
if typebyte != tokens.INT:
raise BananaError("BooleanUnslicer only accepts an INT token")
if self.value != None:
raise BananaError("BooleanUnslicer only accepts one token")
if type(obj) != str:
raise BananaError("InstanceUnslicer classname must be string")
self.classname = obj
self.attrname = None
elif self.attrname is None:
if type(obj) != str:
raise BananaError("InstanceUnslicer keys must be STRINGs")
self.attrname = obj
else:
if isinstance(obj, Deferred):
# TODO: this is an artificial restriction, and it might
# be possible to remove it, but I need to think through
# it carefully first
raise BananaError("unreferenceable object in attribute")
if self.d.has_key(self.attrname):
raise BananaError("duplicate attribute name '%s'" %
self.attrname)
self.setAttribute(self.attrname, obj)
self.attrname = None
if theirTubID:
theirTubRef = referenceable.TubRef(theirTubID)
else:
theirTubRef = None # unauthenticated
self.theirTubRef = theirTubRef # for use by non-master side, later
if self.isClient and self.target.encrypted:
# verify that we connected to the Tub we expected to. If we
# weren't trying to connect to an encrypted tub, then don't
# bother checking.. we just accept whoever we managed to connect
# to.
if theirTubRef != self.target:
# TODO: how (if at all) should this error message be
# communicated to the other side?
raise BananaError("connected to the wrong Tub")
if myTubID is None and theirTubID is None:
iAmTheMaster = not self.isClient
elif myTubID is None:
iAmTheMaster = False
elif theirTubID is None:
iAmTheMaster = True
else:
# this is the most common case
iAmTheMaster = myTubID > theirTubID
self.log(format="iAmTheMaster: %(master)s", master=iAmTheMaster)
decision, params = None, None
if iAmTheMaster:
# Referenceable (callable) objects. All details of actually invoking methods
# live in call.py
from __future__ import print_function
import weakref
from functools import total_ordering
import six
from zope.interface import interface
from zope.interface import implementer
from twisted.python.components import registerAdapter
Interface = interface.Interface
from twisted.internet import defer
from twisted.python import failure, log
from foolscap import ipb, slicer, tokens, call
BananaError = tokens.BananaError
Violation = tokens.Violation
from foolscap.constraint import IConstraint, ByteStringConstraint
from foolscap.remoteinterface import getRemoteInterface, \
getRemoteInterfaceByName, RemoteInterfaceConstraint
from foolscap.schema import constraintMap
from foolscap.copyable import Copyable, RemoteCopy
from foolscap.eventual import eventually, fireEventually
from foolscap.furl import decode_furl
@implementer(ipb.IReferenceable)
class OnlyReferenceable(object):
def processUniqueID(self):
return id(self)
@implementer(ipb.IReferenceable, ipb.IRemotelyCallable)
class Referenceable(OnlyReferenceable):
def receiveChild(self, obj, ready_deferred=None):
assert not isinstance(obj, Deferred)
assert ready_deferred is None
if self.state == 0:
self.im_func = obj
self.state = 1
elif self.state == 1:
assert type(obj) in (types.InstanceType, types.NoneType)
self.im_self = obj
self.state = 2
elif self.state == 2:
assert type(obj) == types.ClassType # TODO: new-style classes?
self.im_class = obj
self.state = 3
else:
raise BananaError("MethodUnslicer only accepts three objects")
def checkToken(self, typebyte, size):
if self.classname is None:
if typebyte not in (tokens.STRING, tokens.VOCAB):
raise BananaError("InstanceUnslicer classname must be string")
elif self.attrname is None:
if typebyte not in (tokens.STRING, tokens.VOCAB):
raise BananaError("InstanceUnslicer keys must be STRINGs")