def makeService(config): if config['logfile']: logObserver = log.FileAccessLoggingObserver(config['logfile']) else: logObserver = log.DefaultCommonAccessLoggingObserver() if config['root']: if config['indexes']: config['root'].indexNames = config['indexes'] root = log.LogWrapperResource(config['root']) s = Web2Service(logObserver) site = server.Site(root) chan = channel.HTTPFactory(site) if config['https']: from twisted.internet.ssl import DefaultOpenSSLContextFactory i = internet.SSLServer( int(config['https']), chan, DefaultOpenSSLContextFactory(config['privkey'], config['certificate'])) i.setServiceParent(s) strports.service(config['port'], chan).setServiceParent(s) return s
def test_unknownResource(self): """ Test urlForResource() on unknown resource. """ root = resource.Resource() child = resource.Resource() request = SimpleRequest(server.Site(root), "GET", "/") self.assertRaises(server.NoURLForResourceError, request.urlForResource, child)
def test_scriptsExecute(self): """ Verify that CGI scripts within a CGIDirectory can actually be executed """ cgiBinDir = os.path.abspath(self.mktemp()) os.mkdir(cgiBinDir) root = twcgi.CGIDirectory(cgiBinDir) self.createScript(os.path.join(cgiBinDir, 'dummy')) cgiSubDir = os.path.join(cgiBinDir, 'sub') os.mkdir(cgiSubDir) self.createScript(os.path.join(cgiSubDir, 'dummy')) site = server.Site(root) request = SimpleRequest(site, "GET", "/dummy") d = request.locateResource('/dummy') def _firstResponse(res): self.failUnlessEqual(res, "cgi output%s" % os.linesep) def _firstRequest(resource): d1 = self.getPage(request, resource) d1.addCallback(_firstResponse) return d1 d.addCallback(_firstRequest) def _secondResponse(res): self.failUnlessEqual(res, "cgi output%s" % os.linesep) def _secondRequest(ign): request = SimpleRequest(site, "GET", '/sub/dummy') d2 = request.locateResource('/sub/dummy') d2.addCallback(lambda resource: self.getPage(request, resource)) d2.addCallback(_secondResponse) return d2 d.addCallback(_secondRequest) return d
def test_locateResource(self): """ Test urlForResource() on resource looked up via a locateResource() call. """ root = resource.Resource() child = resource.Resource() root.putChild("foo", child) request = SimpleRequest(server.Site(root), "GET", "/") def gotResource(resource): self.assertEquals("/foo", request.urlForResource(resource)) d = defer.maybeDeferred(request.locateResource, "/foo") d.addCallback(gotResource) return d
def test_deferredLocateChild(self): """ Test deferred value from locateChild() """ class DeferredLocateChild(resource.Resource): def locateChild(self, req, segments): return defer.maybeDeferred( super(DeferredLocateChild, self).locateChild, req, segments) root = DeferredLocateChild() child = resource.Resource() root.putChild("foo", child) request = SimpleRequest(server.Site(root), "GET", "/foo") def gotResource(resource): self.assertEquals("/foo", request.urlForResource(resource)) d = request.locateResource("/foo") d.addCallback(gotResource) return d
def test_locateChildResource(self): """ Test urlForResource() on deeply nested resource looked up via locateChildResource(). """ root = EmptyResource(self) root.expectedURI = "/" foo = EmptyResource(self) foo.expectedURI = "/foo" root.putChild("foo", foo) bar = EmptyResource(self) bar.expectedURI = "/foo/bar" foo.putChild("bar", bar) baz = EmptyResource(self) baz.expectedURI = "/foo/bar/b%20a%20z" bar.putChild("b a z", baz) request = SimpleRequest(server.Site(root), "GET", "/") def gotResource(resource): # Make sure locateChildResource() gave us the right answer self.assertEquals(resource, bar) return request.locateChildResource( resource, "b a z").addCallback(gotChildResource) def gotChildResource(resource): # Make sure locateChildResource() gave us the right answer self.assertEquals(resource, baz) self.assertEquals(resource.expectedURI, request.urlForResource(resource)) d = request.locateResource(bar.expectedURI) d.addCallback(gotResource) return d
def chanrequest(self, root, uri, length, headers, method, version, prepath, content): site = server.Site(root) return TestChanRequest(site, method, prepath, uri, length, headers, version, content)
if os.path.exists(location): contentTypes.update(mimetypes.read_mime_types(location)) return contentTypes def getTypeAndEncoding(filename, types, encodings, defaultType): p, ext = os.path.splitext(filename) ext = ext.lower() if encodings.has_key(ext): enc = encodings[ext] ext = os.path.splitext(p)[1].lower() else: enc = None type = types.get(ext, defaultType) return type, enc ## # Test code ## if __name__ == '__builtin__': # Running from twistd -y from twisted.application import service, strports from OPSI.web2 import server res = File('/') application = service.Application("demo") s = strports.service('8080', server.Site(res)) s.setServiceParent(application)