def links(self, request, tag): ds = self.root.edits(self.ob) therange = range(len(ds)) rev = therange[self.rev] ul = tags.ul() for i in therange: li = tags.li() if i: u = URLPath.fromRequest(request) u = u.sibling('diff') u.query = urllib.urlencode({ 'ob': self.ob.fullName(), 'revA': i-1, 'revB': i, }) li(tags.a(href=str(u))("(diff)")) else: li("(diff)") li(" - ") if i == len(ds) - 1: label = "Latest" else: label = str(i) if i == rev: li(label) else: u = URLPath.fromRequest(request) u.query = urllib.urlencode({ 'rev': str(i), 'ob': self.ob.fullName(), }) li(tags.a(href=str(u))(label)) li(' - ' + ds[i].user + '/' + ds[i].time) ul(li) return tag(ul)
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(""))
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 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
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")))
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(""))
def hist(self, data, request): u = URLPath.fromRequest(request) u = u.sibling('diff') u.query = urllib.urlencode({ 'ob': data.obj.fullName(), 'rev': data.rev, }) return tags.a(href=str(u))("(hist)")
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
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) )
def base_uri_from_request(request): """ Given a request, return the base URI of the request :param request: a twisted HTTP request :type request: :class:`twisted.web.http.Request` :return: the base uri the request was trying to access :rtype: ``str`` """ return str(URLPath.fromRequest(request).click('/'))
def base_uri_from_request(request): """ Given a request, return the base URI of the request :param request: a twisted HTTP request :type request: :class:`twisted.web.http.Request` :return: the base uri the request was trying to access :rtype: ``str`` """ return str(URLPath.fromRequest(request).click(b'/'))
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
def redirect_to(self, req): """ :param allmydata.webish.MyRequest req: """ ophandle = get_arg(req, "ophandle").decode("utf-8") assert ophandle here = DecodedURL.from_text(str(URLPath.fromRequest(req))) target = here.click(u"/").child(u"operations", ophandle) output = get_arg(req, "output") if output: target = target.add(u"output", output.decode("utf-8")) return target
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()
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
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)
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)))
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 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(""))
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 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)
def absoluteURL(request, ob): if ob.documentation_location == model.DocLocation.PARENT_PAGE: p = ob.parent if isinstance(p, model.Module) and p.name == '__init__': p = p.parent child = p.fullName() + '.html' frag = ob.name elif ob.documentation_location == model.DocLocation.OWN_PAGE: child = ob.fullName() + '.html' frag = None else: raise AssertionError("XXX") u = URLPath.fromRequest(request) u = u.sibling(child) u.query = '' u.fragment = frag return str(u)
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)
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 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)
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) )
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))
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)
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
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)
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
def locateChild(zelf, request, segments): return URLPath.fromString(b'http://quux.com/bar'), segments
def URLPath(self): return URLPath.fromRequest(self)
def url(suffix): return str(URLPath.fromString(compute_uri_prefix).child(suffix))
def secureGet(url): return getPage(url, HTTPSVerifyingContextFactory(URLPath.fromString(url).netloc))
def getFrackResource(reactor): return ReverseProxyResource( UNIXClientEndpoint(reactor, path='/var/run/frack/json.sock'), URLPath("http://twistedmatrix.com/"))