Exemplo n.º 1
0
    def uri_for_service(self, region, service_id, base_uri):
        """
        Generate a URI prefix for a given region and service ID.

        Each plugin loaded into mimic generates a list of catalog entries; each
        catalog entry has a list of endpoints.  Each endpoint has a URI
        associated with it, which we call a "URI prefix", because the endpoint
        will have numerous other URIs beneath it in the hierarchy, generally
        starting with a version number and tenant ID.  The URI prefixes
        generated for this function point to the top of the endpoint's
        hierarchy, not including any tenant information.

        :param unicode region: the name of the region that the service resource
            exists within.
        :param unicode service_id: the UUID for the service for the specified
            region
        :param str base_uri: the base uri to use instead of the default - most
            likely comes from a request URI

        :return: The full URI locating the service for that region
        :rtype: ``str``
        """
        if region:
            return str(URLPath.fromString(base_uri)
                    .child("mimicking").child(service_id).child(region).child(""))
        else:
            return str(URLPath.fromString(base_uri)
                    .child("mimicking").child(service_id).child(""))
Exemplo n.º 2
0
 def url(self, suffix):
     """
     Generate a URL to an object within the Heat URL hierarchy, given the
     part of the URL that comes after.
     """
     return str(URLPath.fromString(self.uri_prefix)
                .child(suffix.encode("utf-8")))
Exemplo n.º 3
0
    def request(self, method, uri, headers=None, bodyProducer=None):
        """
        Implement IAgent.request.
        """
        # We want to use Agent to parse the HTTP response, so let's ask it to
        # make a request against our in-memory reactor.
        response = self._realAgent.request(method, uri, headers, bodyProducer)

        # If the request has already finished, just propagate the result.  In
        # reality this would only happen in failure, but if the agent ever adds
        # a local cache this might be a success.
        already_called = []

        def check_already_called(r):
            already_called.append(r)
            return r
        response.addBoth(check_already_called)
        if already_called:
            return response

        # That will try to establish an HTTP connection with the reactor's
        # connectTCP method, and MemoryReactor will place Agent's factory into
        # the tcpClients list.  Alternately, it will try to establish an HTTPS
        # connection with the reactor's connectSSL method, and MemoryReactor
        # will place it into the sslClients list.  We'll extract that.
        if PY3:
            scheme = URLPath.fromBytes(uri).scheme
        else:
            scheme = URLPath.fromString(uri).scheme

        host, port, factory, timeout, bindAddress = (
            self._memoryReactor.tcpClients[-1])

        serverAddress = IPv4Address('TCP', '127.0.0.1', port)
        clientAddress = IPv4Address('TCP', '127.0.0.1', 31337)

        # Create the protocol and fake transport for the client and server,
        # using the factory that was passed to the MemoryReactor for the
        # client, and a Site around our rootResource for the server.
        serverProtocol = Site(self._rootResource).buildProtocol(None)
        serverTransport = iosim.FakeTransport(
            serverProtocol, isServer=True,
            hostAddress=serverAddress, peerAddress=clientAddress)
        clientProtocol = factory.buildProtocol(None)
        clientTransport = iosim.FakeTransport(
            clientProtocol, isServer=False,
            hostAddress=clientAddress, peerAddress=serverAddress)

        if scheme == b"https":
            # Provide ISSLTransport on both transports, so everyone knows that
            # this is HTTPS.
            directlyProvides(serverTransport, ISSLTransport)
            directlyProvides(clientTransport, ISSLTransport)

        # Make a pump for wiring the client and server together.
        pump = iosim.connect(
            serverProtocol, serverTransport, clientProtocol, clientTransport)
        self._pumps.add(pump)

        return response
Exemplo n.º 4
0
    def uri_for_service(self, region, service_id, base_uri):
        """
        Generate a URI prefix for a given region and service ID.

        Each plugin loaded into mimic generates a list of catalog entries; each
        catalog entry has a list of endpoints.  Each endpoint has a URI
        associated with it, which we call a "URI prefix", because the endpoint
        will have numerous other URIs beneath it in the hierarchy, generally
        starting with a version number and tenant ID.  The URI prefixes
        generated for this function point to the top of the endpoint's
        hierarchy, not including any tenant information.

        :param unicode region: the name of the region that the service resource
            exists within.
        :param unicode service_id: the UUID for the service for the specified
            region
        :param str base_uri: the base uri to use instead of the default - most
            likely comes from a request URI

        :return: The full URI locating the service for that region
        :rtype: ``str``
        """
        return str(
            URLPath.fromString(base_uri).child("mimicking").child(
                service_id).child(region).child(""))
Exemplo n.º 5
0
    def initialize(self):
        self.clean()

        def resolveCb(result):
            self.mangle.createChain(chain="cattivo")

            entry = self._createAuthenticatorEntry(result)
            self.mangle.appendEntry(entry, chain="cattivo")

            entry = self._createLocalTrafficEntry()
            self.mangle.appendEntry(entry, chain="cattivo")

            entry = self._createDefaultTproxyEntry()
            self.mangle.appendEntry(entry, chain="cattivo")
            self.mangle.commit()

            entry = self._createJumpInCattivoEntry()
            self.mangle.appendEntry(entry, chain="PREROUTING")
            self.mangle.commit()

        authenticator = cattivo.config.get("authenticator", "host")
        url = URLPath.fromString(authenticator)
        address = url.netloc
        dfr = reactor.resolve(address)
        dfr.addCallback(resolveCb)

        return dfr
Exemplo n.º 6
0
 def url(self, suffix):
     """
     Generate a URL to an object within the Heat URL hierarchy, given the
     part of the URL that comes after.
     """
     return str(URLPath.fromString(self.uri_prefix)
                .child(suffix.encode("utf-8")))
Exemplo n.º 7
0
def makeURLPath(s):
    """
    Create a URLPath with the given text or bytes.
    """
    if hasattr(URLPath, 'fromBytes') and isinstance(s, bytes):
        return URLPath.fromBytes(s)
    else:
        return URLPath.fromString(s)
Exemplo n.º 8
0
 def __init__(self, mapsPath, fetchURL, deleteIfNotPresent, tfLevelSounds,
              listURL, keyPrefix):
     MapUpdater.__init__(self, mapsPath, fetchURL, deleteIfNotPresent,
                         tfLevelSounds)
     assert isinstance(listURL, str) and len(listURL)
     assert isinstance(keyPrefix, str) and len(keyPrefix)
     self.listURL = URLPath.fromString(listURL)
     self.keyPrefix = keyPrefix
Exemplo n.º 9
0
def makeURLPath(s):
    """
    Create a URLPath with the given text or bytes.
    """
    if hasattr(URLPath, 'fromBytes') and isinstance(s, bytes):
        return URLPath.fromBytes(s)
    else:
        return URLPath.fromString(s)
Exemplo n.º 10
0
 def __init__(self, mapsPath, fetchURL, deleteIfNotPresent, tfLevelSounds,
         listURL, keyPrefix):
     MapUpdater.__init__(self, mapsPath, fetchURL, deleteIfNotPresent,
                         tfLevelSounds)
     assert isinstance(listURL, str) and len(listURL)
     assert isinstance(keyPrefix, str) and len(keyPrefix)
     self.listURL = URLPath.fromString(listURL)
     self.keyPrefix = keyPrefix
Exemplo n.º 11
0
def request(testCase, rootResource, method, uri, body=b"", baseURI="http://localhost:8900/"):
    """
    Issue a request and return a synchronous response.
    """
    # allow for relative or absolute URIs, since we're going to the same
    # resource no matter what
    return RequestTraversalAgent(testCase, rootResource).request(
        method, str(URLPath.fromString(baseURI).click(uri)), bodyProducer=SynchronousProducer(body)
    )
Exemplo n.º 12
0
Arquivo: cli.py Projeto: olix0r/pub
 def _toUrl(spec):
     if not spec:
         raise cli.UsageError("No Pub server")
     from twisted.python.urlpath import URLPath
     try:
         url = URLPath.fromString(spec)
     except:
         raise cli.UsageError("Invalid URL: {0}".format(spec))
     if not spec.startswith(url.scheme):
         raise cli.UsageError("Invalid URL: {0}".format(spec))
     return url
Exemplo n.º 13
0
 def __init__(self, mapsPath, fetchURL, deleteIfNotPresent, tfLevelSounds):
     assert isinstance(mapsPath, str) and len(mapsPath)
     assert isinstance(fetchURL, str) and len(fetchURL)
     self.mapsPath = FilePath(mapsPath)
     self.downloadTempPath = self.mapsPath.child('mapupdater')
     self.fetchURL = URLPath.fromString(fetchURL)
     self.deleteIfNotPresent = deleteIfNotPresent
     self.tfLevelSounds = tfLevelSounds
     self.semaphore = DeferredSemaphore(1)
     self.downloadSemaphore = DeferredSemaphore(4)
     for fp in self.downloadTempPath.globChildren('*.bsp.bz2'):
         fp.remove()
Exemplo n.º 14
0
 def __init__(self, mapsPath, fetchURL, deleteIfNotPresent, tfLevelSounds):
     assert isinstance(mapsPath, str) and len(mapsPath)
     assert isinstance(fetchURL, str) and len(fetchURL)
     self.mapsPath = FilePath(mapsPath)
     self.downloadTempPath = self.mapsPath.child('mapupdater')
     self.fetchURL = URLPath.fromString(fetchURL)
     self.deleteIfNotPresent = deleteIfNotPresent
     self.tfLevelSounds = tfLevelSounds
     self.semaphore = DeferredSemaphore(1)
     self.downloadSemaphore = DeferredSemaphore(4)
     for fp in self.downloadTempPath.globChildren('*.bsp.bz2'):
         fp.remove()
Exemplo n.º 15
0
    def __init__(self, uri, agent=None):
        """
        @type  uri: L{unicode}
        @param uri: Entropy endpoint URI, for example:
            C{http://example.com/entropy/}.

        @type  agent: L{twisted.web.iweb.IAgent}
        @param agent: Twisted Web agent.
        """
        self.uri = URLPath.fromString(uri)
        if agent is None:
            agent = Agent(reactor)
        self._agent = agent
Exemplo n.º 16
0
    def __init__(self, uri, agent=None):
        """
        @type  uri: L{unicode}
        @param uri: Entropy endpoint URI, for example:
            C{http://example.com/entropy/}.

        @type  agent: L{twisted.web.iweb.IAgent}
        @param agent: Twisted Web agent.
        """
        self.uri = URLPath.fromString(uri)
        if agent is None:
            agent = Agent(reactor)
        self._agent = agent
Exemplo n.º 17
0
 def __init__(self, uri, verify, timeout=600, reactor=reactor, clientCert=None):
     Resource.__init__(self)
     self._uri = URLPath.fromString(uri)
     self._verify = verify
     self._timeout = timeout
     self._reactor = reactor
     pool = HTTPConnectionPool(reactor)
     if clientCert is not None:
         clientCert = PrivateCertificate.loadPEM(
             FilePath(clientCert).getContent())
     self._agent = Agent(
         reactor,
         StupidPolicyForHTTPS(InsecureTLSOptions(clientCert)),
         pool=pool)
Exemplo n.º 18
0
def request(testCase,
            rootResource,
            method,
            uri,
            body=b"",
            baseURI='http://localhost:8900/'):
    """
    Issue a request and return a synchronous response.
    """
    # allow for relative or absolute URIs, since we're going to the same
    # resource no matter what
    return (RequestTraversalAgent(testCase, rootResource).request(
        method,
        str(URLPath.fromString(baseURI).click(uri)),
        bodyProducer=SynchronousProducer(body)))
Exemplo n.º 19
0
Arquivo: core.py Projeto: jirwin/mimic
    def uri_for_service(self, region, service_id, base_uri):
        """
        Generate a URI prefix for a given region and service ID.

        :param unicode region_name: the name of the region that the service
            resource exists within.
        :param unicode service_id: the UUID for the service for the
            specified region
        :param str base_uri: the base uri to use instead of the default -
            most likely comes from a request URI

        :return: The full URI locating the service for that region
        """
        return str(URLPath.fromString(base_uri)
                   .child("service").child(region).child(service_id).child(""))
Exemplo n.º 20
0
    def _dummy_request_get(self,
                           url='http://example/status',
                           path=None,
                           revision=None):
        if path is None:
            path = []

        # The request to make.
        request = DummyRequest(path)
        request.URLPath = lambda: URLPath.fromString(url)

        if revision is not None:
            request.addArg('revision', str(revision))

        return request
    def start(self):
        if self.interrupted:
            self.failed(Failure(Exception('Interrupted')))
            return

        self.current_attempt += 1

        if self.current_attempt > self.attempts:
            if len(self.servers) < 1:
                self.failed(
                    Failure(Exception('No more signing servers to try.')))
            else:
                self.current_attempt = 1

        if self.current_attempt == 1:
            uri, self.username, self.password, _ = self.servers.pop()
            self.uri = 'https://%s/token' % uri

        slaveName = self.getSlaveName()
        slaveIP = self.buildslave.slave.broker.transport.getPeer().host

        if not self.stdio_log:
            self.stdio_log = self.addLog('output')
            self.stdio_log.addHeader("Slave: %s\n" % slaveName)
            self.stdio_log.addHeader("IP: %s\n" % slaveIP)
            self.stdio_log.addHeader("Duration: %s\n" % self.duration)
        self.stdio_log.addStdout("URI: %s\n" % self.uri)

        method = 'POST'
        postdata = {
            'slave_ip': slaveIP,
            'duration': self.duration,
        }
        headers = self.generateHeaders(method=method,
                                       credentials=(self.username,
                                                    self.password))
        # send the HTTPSVerifyingContextFactory the hostname of the signing
        # server + the cert
        contextFactory = HTTPSVerifyingContextFactory(
            URLPath.fromString(self.uri).netloc.split(':')[0],
            self.server_cert)
        d = getPage(self.uri,
                    method=method,
                    headers=headers,
                    postdata=urlencode(postdata),
                    contextFactory=contextFactory)
        d.addCallbacks(self.downloadSignature, self.requestFailed)
Exemplo n.º 22
0
def getSteamIDFromURL(url, steamAPI):
    path = URLPath.fromString(url)
    steamID = None
    if path.netloc.lower() == 'steamcommunity.com':
        if path.path.startswith('/id/'):
            vanityURL = path.path[4:].rstrip('/')
            response = yield deferToThread(
                steamAPI['ISteamUser'].ResolveVanityURL,
                vanityurl=vanityURL)
            try:
                steamID = int(response['response']['steamid'])
            except (KeyError, ValueError):
                pass
        elif path.path.startswith('/profiles/'):
            try:
                steamID = int(path.path[10:].rstrip('/'))
            except ValueError:
                pass
    returnValue(steamID)
Exemplo n.º 23
0
    def start(self):
        if self.interrupted:
            self.failed(Failure(Exception('Interrupted')))
            return

        self.current_attempt += 1

        if self.current_attempt > self.attempts:
            if len(self.servers) < 1:
                self.failed(Failure(Exception(
                    'No more signing servers to try.')))
            else:
                self.current_attempt = 1

        if self.current_attempt == 1:
            uri, self.username, self.password, _ = self.servers.pop()
            self.uri = 'https://%s/token' % uri

        slaveName = self.getSlaveName()
        slaveIP = self.buildslave.slave.broker.transport.getPeer().host

        if not self.stdio_log:
            self.stdio_log = self.addLog('output')
            self.stdio_log.addHeader("Slave: %s\n" % slaveName)
            self.stdio_log.addHeader("IP: %s\n" % slaveIP)
            self.stdio_log.addHeader("Duration: %s\n" % self.duration)
        self.stdio_log.addStdout("URI: %s\n" % self.uri)

        method = 'POST'
        postdata = {
            'slave_ip': slaveIP,
            'duration': self.duration,
        }
        headers = self.generateHeaders(
            method=method,
            credentials=(self.username, self.password))
        # send the HTTPSVerifyingContextFactory the hostname of the signing
        # server + the cert
        contextFactory = HTTPSVerifyingContextFactory(
            URLPath.fromString(self.uri).netloc.split(':')[0], self.server_cert)
        d = getPage(self.uri, method=method, headers=headers,
                    postdata=urlencode(postdata), contextFactory=contextFactory)
        d.addCallbacks(self.downloadSignature, self.requestFailed)
Exemplo n.º 24
0
def request(testCase, rootResource, method, uri, body=b"",
            baseURI='http://localhost:8900/',
            headers=None):
    """
    Issue a request and return a synchronous response.
    """
    # allow for relative or absolute URIs, since we're going to the same
    # resource no matter what
    if headers is not None:
        headers_object = Headers()
        for key, value in headers.items():
            headers_object.setRawHeaders(key, value)
    else:
        headers_object = None
    return (
        RequestTraversalAgent(testCase, rootResource)
        .request(method, str(URLPath.fromString(baseURI).click(uri)),
                 bodyProducer=SynchronousProducer(body),
                 headers=headers_object)
    )
Exemplo n.º 25
0
def request(testCase,
            rootResource,
            method,
            uri,
            body=b"",
            baseURI='http://localhost:8900/',
            headers=None):
    """
    Issue a request and return a synchronous response.
    """
    # allow for relative or absolute URIs, since we're going to the same
    # resource no matter what
    if headers is not None:
        headers_object = Headers()
        for key, value in headers.items():
            headers_object.setRawHeaders(key, value)
    else:
        headers_object = None
    return (RequestTraversalAgent(testCase, rootResource).request(
        method,
        str(URLPath.fromString(baseURI).click(uri)),
        bodyProducer=SynchronousProducer(body),
        headers=headers_object))
Exemplo n.º 26
0
    def _rewriteUriResponse(self, originalHostname, host, url):
        """
        Rewrite the location header in the URL response.

        Makes two changes:

        - If the URL is absolute, we will change the hostname to the proxy
          hostname instead of the target hostname.
        - If there are URL rewrite rules, it will *undo* them.
        """
        targetHostname = host["host"]
        parsedURL = URLPath.fromString(url)
        if parsedURL.netloc == targetHostname:
            parsedURL.netloc = originalHostname

        rewriteRules = host.get("rewrite", [])
        path = parsedURL.path

        for replace, start in rewriteRules:
            if path.startswith(start):
                parsedURL.path = path.replace(start, replace, 1)
                break

        return str(parsedURL)
Exemplo n.º 27
0
    def request(self, method, uri, headers=None, bodyProducer=None):
        """
        Implement IAgent.request.
        """
        # We want to use Agent to parse the HTTP response, so let's ask it to
        # make a request against our in-memory reactor.
        response = self._realAgent.request(method, uri, headers, bodyProducer)

        # If the request has already finished, just propagate the result.  In
        # reality this would only happen in failure, but if the agent ever adds
        # a local cache this might be a success.
        already_called = []

        def check_already_called(r):
            already_called.append(r)
            return r

        response.addBoth(check_already_called)
        if already_called:
            return response

        # That will try to establish an HTTP connection with the reactor's
        # connectTCP method, and MemoryReactor will place Agent's factory into
        # the tcpClients list.  Alternately, it will try to establish an HTTPS
        # connection with the reactor's connectSSL method, and MemoryReactor
        # will place it into the sslClients list.  We'll extract that.
        scheme = URLPath.fromString(uri).scheme
        if scheme == "https":
            host, port, factory, context_factory, timeout, bindAddress = (
                self._memoryReactor.sslClients[-1])
        else:
            host, port, factory, timeout, bindAddress = (
                self._memoryReactor.tcpClients[-1])

        # Then we need to convince that factory it's connected to something and
        # it will give us a protocol for that connection.
        protocol = factory.buildProtocol(None)

        # We want to capture the output of that connection so we'll make an
        # in-memory transport.
        clientTransport = AbortableStringTransport()
        if scheme == "https":
            directlyProvides(clientTransport, ISSLTransport)

        # When the protocol is connected to a transport, it ought to send the
        # whole request because callers of this should not use an asynchronous
        # bodyProducer.
        protocol.makeConnection(clientTransport)

        # Get the data from the request.
        requestData = clientTransport.io.getvalue()

        # Now time for the server to do its job.  Ask it to build an HTTP
        # channel.
        channel = Site(self._rootResource).buildProtocol(None)

        # Connect the channel to another in-memory transport so we can collect
        # the response.
        serverTransport = AbortableStringTransport()
        if scheme == "https":
            directlyProvides(serverTransport, ISSLTransport)
        serverTransport.hostAddr = IPv4Address('TCP', '127.0.0.1', port)
        channel.makeConnection(serverTransport)

        # Feed it the data that the Agent synthesized.
        channel.dataReceived(requestData)

        # Now we have the response data, let's give it back to the Agent.
        protocol.dataReceived(serverTransport.io.getvalue())

        def finish(r):
            # By now the Agent should have all it needs to parse a response.
            protocol.connectionLost(Failure(ConnectionDone()))
            # Tell it that the connection is now complete so it can clean up.
            channel.connectionLost(Failure(ConnectionDone()))
            # Propogate the response.
            return r

        # Return the response in the accepted format (Deferred firing
        # IResponse).  This should be synchronously fired, and if not, it's the
        # system under test's problem.
        return response.addBoth(finish)
Exemplo n.º 28
0
def secureGet(url):
    return getPage(url, HTTPSVerifyingContextFactory(URLPath.fromString(url).netloc))
Exemplo n.º 29
0
Arquivo: nova.py Projeto: jirwin/mimic
 def url(suffix):
     return str(URLPath.fromString(compute_uri_prefix).child(suffix))
Exemplo n.º 30
0
Arquivo: nova.py Projeto: jirwin/mimic
 def url(suffix):
     return str(URLPath.fromString(compute_uri_prefix).child(suffix))
Exemplo n.º 31
0
    def request(self, method, uri, headers=None, bodyProducer=None):
        """
        Implement IAgent.request.
        """
        # We want to use Agent to parse the HTTP response, so let's ask it to
        # make a request against our in-memory reactor.
        response = self._realAgent.request(method, uri, headers, bodyProducer)

        # If the request has already finished, just propagate the result.  In
        # reality this would only happen in failure, but if the agent ever adds
        # a local cache this might be a success.
        already_called = []

        def check_already_called(r):
            already_called.append(r)
            return r
        response.addBoth(check_already_called)
        if already_called:
            return response

        # That will try to establish an HTTP connection with the reactor's
        # connectTCP method, and MemoryReactor will place Agent's factory into
        # the tcpClients list.  Alternately, it will try to establish an HTTPS
        # connection with the reactor's connectSSL method, and MemoryReactor
        # will place it into the sslClients list.  We'll extract that.
        scheme = URLPath.fromString(uri).scheme
        if scheme == "https":
            host, port, factory, context_factory, timeout, bindAddress = (
                self._memoryReactor.sslClients[-1])
        else:
            host, port, factory, timeout, bindAddress = (
                self._memoryReactor.tcpClients[-1])

        # Then we need to convince that factory it's connected to something and
        # it will give us a protocol for that connection.
        protocol = factory.buildProtocol(None)

        # We want to capture the output of that connection so we'll make an
        # in-memory transport.
        clientTransport = AbortableStringTransport()
        if scheme == "https":
            directlyProvides(clientTransport, ISSLTransport)

        # When the protocol is connected to a transport, it ought to send the
        # whole request because callers of this should not use an asynchronous
        # bodyProducer.
        protocol.makeConnection(clientTransport)

        # Get the data from the request.
        requestData = clientTransport.io.getvalue()

        # Now time for the server to do its job.  Ask it to build an HTTP
        # channel.
        channel = Site(self._rootResource).buildProtocol(None)

        # Connect the channel to another in-memory transport so we can collect
        # the response.
        serverTransport = AbortableStringTransport()
        if scheme == "https":
            directlyProvides(serverTransport, ISSLTransport)
        serverTransport.hostAddr = IPv4Address('TCP', '127.0.0.1', port)
        channel.makeConnection(serverTransport)

        # Feed it the data that the Agent synthesized.
        channel.dataReceived(requestData)

        # Now we have the response data, let's give it back to the Agent.
        protocol.dataReceived(serverTransport.io.getvalue())

        def finish(r):
            # By now the Agent should have all it needs to parse a response.
            protocol.connectionLost(Failure(ConnectionDone()))
            # Tell it that the connection is now complete so it can clean up.
            channel.connectionLost(Failure(ConnectionDone()))
            # Propogate the response.
            return r

        # Return the response in the accepted format (Deferred firing
        # IResponse).  This should be synchronously fired, and if not, it's the
        # system under test's problem.
        return response.addBoth(finish)
Exemplo n.º 32
0
    def request(self, method, uri, headers=None, bodyProducer=None):
        """
        Implement IAgent.request.
        """
        # We want to use Agent to parse the HTTP response, so let's ask it to
        # make a request against our in-memory reactor.
        response = self._realAgent.request(method, uri, headers, bodyProducer)

        # If the request has already finished, just propagate the result.  In
        # reality this would only happen in failure, but if the agent ever adds
        # a local cache this might be a success.
        already_called = []

        def check_already_called(r):
            already_called.append(r)
            return r
        response.addBoth(check_already_called)
        if already_called:
            return response

        # That will try to establish an HTTP connection with the reactor's
        # connectTCP method, and MemoryReactor will place Agent's factory into
        # the tcpClients list.  Alternately, it will try to establish an HTTPS
        # connection with the reactor's connectSSL method, and MemoryReactor
        # will place it into the sslClients list.  We'll extract that.
        if PY3:
            scheme = URLPath.fromBytes(uri).scheme
        else:
            scheme = URLPath.fromString(uri).scheme

        if scheme == b"https":
            host, port, factory, context_factory, timeout, bindAddress = (
                self._memoryReactor.sslClients[-1])
        else:
            host, port, factory, timeout, bindAddress = (
                self._memoryReactor.tcpClients[-1])

        serverAddress = IPv4Address('TCP', '127.0.0.1', port)
        clientAddress = IPv4Address('TCP', '127.0.0.1', 31337)

        # Create the protocol and fake transport for the client and server,
        # using the factory that was passed to the MemoryReactor for the
        # client, and a Site around our rootResource for the server.
        serverProtocol = Site(self._rootResource).buildProtocol(None)
        serverTransport = iosim.FakeTransport(
            serverProtocol, isServer=True,
            hostAddress=serverAddress, peerAddress=clientAddress)
        clientProtocol = factory.buildProtocol(None)
        clientTransport = iosim.FakeTransport(
            clientProtocol, isServer=False,
            hostAddress=clientAddress, peerAddress=serverAddress)

        # Twisted 13.2 compatibility.
        serverTransport.abortConnection = serverTransport.loseConnection
        clientTransport.abortConnection = clientTransport.loseConnection

        if scheme == b"https":
            # Provide ISSLTransport on both transports, so everyone knows that
            # this is HTTPS.
            directlyProvides(serverTransport, ISSLTransport)
            directlyProvides(clientTransport, ISSLTransport)

        # Make a pump for wiring the client and server together.
        pump = iosim.connect(
            serverProtocol, serverTransport, clientProtocol, clientTransport)
        self._pumps.add(pump)

        return response
Exemplo n.º 33
0
 def locateChild(zelf, request, segments):
     return URLPath.fromString(b'http://quux.com/bar'), segments
Exemplo n.º 34
0
    def render(self, request):
        """
        Forward the request.
        """
        def writeResponse(response):
            request.setResponseCode(response.code)
            request.setHeader(
                'content-type',
                response.headers.getRawHeaders(
                    'content-type', ['application/xml'])[0])
            return readBody(response).addErrback(eatPartial).addCallback(write)

        def eatPartial(f):
            f.trap(PartialDownloadError)
            return f.value.response

        def writeError(f):
            request.setResponseCode(500)
            request.setHeader('content-type', 'application/xml')
            faultcode = E.faultcode()
            fault = SOAP_ENV.Fault(
                faultcode,
                E.faultstring(f.getErrorMessage()),
                E.faultactor(request.uri),
                E.detail(
                    E.traceback(f.getTraceback())))
            faultcode.text = QName(SOAP_ENV_URI, 'Server')
            request.write(tostring(SOAP_ENV.Envelope(fault)))
            request.finish()

        def write(r):
            request.setHeader('content-length', '%d' % (len(r),))
            request.write(r)
            request.finish()

        def notify(reason):
            if reason is not None:
                d.cancel()

        uri = URLPath.fromString(request.uri)
        uri.scheme = self._uri.scheme
        uri.netloc = self._uri.netloc
        if request.method in {'GET', 'HEAD', 'DELETE'}:
            body = None
        else:
            body = FileBodyProducer(request.content)
        d = self._agent.request(
            request.method,
            str(uri),
            Headers({'user-agent': ['Fusion SOAP proxy'],
                     'content-type': request.requestHeaders.getRawHeaders(
                         'content-type', ['application/xml']),
                     'soapaction': request.requestHeaders.getRawHeaders(
                         'soapaction', ['']),
                     'authorization': request.requestHeaders.getRawHeaders(
                         'authorization', [])}),
            body)
        call = self._reactor.callLater(self._timeout, d.cancel)
        def cancelTimeout(result):
            if call.active():
                call.cancel()
            return result
        d.addCallback(writeResponse)
        d.addBoth(cancelTimeout)
        d.addErrback(writeError)
        request.notifyFinish().addCallback(notify)
        return NOT_DONE_YET