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
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)
def handleProcess(self, request): path = URLPath.fromBytes(getUrlForRequest(request)) args = {k: v[0] for k, v in request.args.iteritems()} oidconsumer = self.getConsumer(request) def _cb(info): if info.status == consumer.FAILURE: request.setResponseCode(http.UNAUTHORIZED) return Data('Login failed', 'text/plain') ident = info.getDisplayIdentifier() return self.loginCallback(request, ident) d = self.semaphore.run(oidconsumer.complete, args, str(path)) d.addCallback(_cb) return DeferredResource(d)
def handleLogin(self, request): path = URLPath.fromBytes(getUrlForRequest(request)) def _tx(): oidconsumer = self.getConsumer(request) oidrequest = oidconsumer.begin(self.providerURL) return oidrequest.redirectURL(str(path.parent()), str(path.sibling('process')), immediate=False) def _eb(failure): failure.trap(DiscoveryFailure) request.setResponseCode(http.SEVICE_UNAVAILABLE) return Data('Steam login service unavailable.', 'text/plain') d = self.semaphore.run(deferToThread, _tx) d.addCallback(Redirect) d.addErrback(_eb) return DeferredResource(d)
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