def _create_web_factory(self, config): options = config.get('options', {}) # create Twisted Web root resource if '/' in config['paths']: root_config = config['paths']['/'] root = self._create_resource(root_config, nested=False) else: root = Resource404(self._templates, b'') # create Twisted Web resources on all non-root paths configured self._add_paths(root, config.get('paths', {})) # create the actual transport factory transport_factory = Site(root) transport_factory.noisy = False # Web access logging if not options.get('access_log', False): transport_factory.log = lambda _: None # Traceback rendering transport_factory.displayTracebacks = options.get('display_tracebacks', False) # HSTS if options.get('hsts', False): if 'tls' in config['endpoint']: hsts_max_age = int(options.get('hsts_max_age', 31536000)) transport_factory.requestFactory = createHSTSRequestFactory(transport_factory.requestFactory, hsts_max_age) else: self.log.warn("Warning: HSTS requested, but running on non-TLS - skipping HSTS") return transport_factory
def installApplication(self, application): """Install the WSGI application into the Twisted site. It's installed as a child with path "MAAS". This matches the default front-end configuration (i.e. Apache) so that there's no need to force script names. """ # Setup resources to process paths that twisted handles. metadata = Resource() metadata.putChild(b'status', StatusHandlerResource(self.status_worker)) maas = Resource() maas.putChild(b'metadata', metadata) maas.putChild(b'static', File(settings.STATIC_ROOT)) maas.putChild( b'ws', WebSocketsResource(lookupProtocolForFactory(self.websocket))) root = Resource() root.putChild(b'', Redirect(b"MAAS/")) root.putChild(b'MAAS', maas) # Setup the resources to process paths that django handles. underlay_maas = ResourceOverlay( WSGIResource(reactor, self.threadpool, application)) underlay_root = Resource() underlay_root.putChild(b'MAAS', underlay_maas) underlay_site = Site(underlay_root, logFormatter=reducedWebLogFormatter) underlay_site.requestFactory = CleanPathRequest # Setup the main resource as the twisted handler and the underlay # resource as the django handler. self.site.resource = root self.site.underlay = underlay_site
def installApplication(self, application): """Install the WSGI application into the Twisted site. It's installed as a child with path "MAAS". This matches the default front-end configuration (i.e. Apache) so that there's no need to force script names. """ # Setup resources to process paths that twisted handles. metadata = Resource() metadata.putChild(b"status", StatusHandlerResource(self.status_worker)) maas = Resource() maas.putChild(b"metadata", metadata) maas.putChild( b"ws", WebSocketsResource(lookupProtocolForFactory(self.websocket)) ) # /MAAS/r/{path} and /MAAS/l/{path} are all resolved by the new MAAS UI # react app, and legacy angularjs app respectively. # If any paths do not match then its routed to index.html in the new # UI code as it uses HTML 5 routing. maas.putChild(b"r", DefaultFallbackFile(settings.STATIC_ROOT)) maas.putChild(b"l", DefaultFallbackFile(settings.STATIC_ROOT)) # Redirect /MAAS to react app maas.putChild(b"", Redirect(b"/MAAS/r/")) # Setup static resources maas.putChild( b"assets", NoListingFile(os.path.join(settings.STATIC_ROOT, "assets")), ) # Setup static docs maas.putChild( b"docs", DocsFallbackFile(os.path.join(settings.STATIC_ROOT, "docs")), ) root = Resource() root.putChild(b"", Redirect(b"MAAS/")) root.putChild(b"MAAS", maas) # Setup the resources to process paths that django handles. underlay_maas = ResourceOverlay( WSGIResource(reactor, self.threadpool, application) ) underlay_root = Resource() underlay_root.putChild(b"MAAS", underlay_maas) underlay_site = Site( underlay_root, logFormatter=reducedWebLogFormatter ) underlay_site.requestFactory = CleanPathRequest # Setup the main resource as the twisted handler and the underlay # resource as the django handler. self.site.resource = root self.site.underlay = underlay_site
def get_site(resource, logging=False): """ :param resource: A :class:`twisted.web.resource.Resource` object. :return: a :class:`Site` that can be run """ site = Site(resource) site.displayTracebacks = False site.requestFactory = MimicLoggingRequest if logging else MimicRequest return site
def secureSite(_environ=environ): """Builds the secure (HTTPS, port 443) site. """ root = File(_environ["STATIC_PATH"]) root.putChild("subscribe", SubscribeResource("*****@*****.**")) site = Site(root) site.displayTracebacks = False site.requestFactory = _withHSTS(site.requestFactory) return site
def _create_web_factory(self, config, is_secure): options = config.get('options', {}) # create Twisted Web root resource if '/' in config['paths']: root_config = config['paths']['/'] root = self._create_resource(root_config, nested=False) else: root = Resource404(self._templates, b'') # create Twisted Web resources on all non-root paths configured self._add_paths(root, config.get('paths', {})) # create the actual transport factory transport_factory = Site( root, timeout=options.get('client_timeout', None), ) transport_factory.noisy = False # we override this factory so that we can inject # _LessNoisyHTTPChannel to avoid info-level logging on timing # out web clients (which happens all the time). def channel_protocol_factory(): return _GenericHTTPChannelProtocol(_LessNoisyHTTPChannel()) transport_factory.protocol = channel_protocol_factory # Web access logging if not options.get('access_log', False): transport_factory.log = lambda _: None # Traceback rendering transport_factory.displayTracebacks = options.get('display_tracebacks', False) # HSTS if options.get('hsts', False): if is_secure: hsts_max_age = int(options.get('hsts_max_age', 31536000)) transport_factory.requestFactory = createHSTSRequestFactory(transport_factory.requestFactory, hsts_max_age) else: self.log.warn("Warning: HSTS requested, but running on non-TLS - skipping HSTS") return transport_factory
## Web access logging ## if not options.get("access_log", False): transport_factory.log = lambda _: None ## Traceback rendering ## transport_factory.displayTracebacks = options.get("display_tracebacks", False) ## HSTS ## if options.get("hsts", False): if "tls" in config["endpoint"]: hsts_max_age = int(options.get("hsts_max_age", 31536000)) transport_factory.requestFactory = createHSTSRequestFactory( transport_factory.requestFactory, hsts_max_age ) else: log.msg("Warning: HSTS requested, but running on non-TLS - skipping HSTS") ## enable Hixie-76 on Twisted Web ## if options.get("hixie76_aware", False): transport_factory.protocol = HTTPChannelHixie76Aware # needed if Hixie76 is to be supported else: raise Exception("logic error") ## create transport endpoint / listening port from transport factory ## if True:
return self.session_uid_to_user[session_uid] class User(): """ any relevant user information """ def __init__(self, session_uid): self.session_uid = session_uid self.uid = User.uid User.uid += 1 def getid(self): return self.uid User.uid = 0 users = Users() index = Index() index.putChild('e', EventScheduler()) site = Site(index) site.requestFactory = RequestBootstrap # hook in our request reactor.listenTCP(8080, site) # HTTP listener osc_writer = OscWriter(reactor, 16666) # UDP(OSC) sender/receiver reactor.run()
import cgi class FormPage(Resource): def render_GET(self, request): return """ <html> <body> <form method="POST" enctype="multipart/form-data"> <input name="the-file" type="file" /> <input type="submit" /> </form> </body> </html> """ def render_POST(self, request): return '<html><body>You submitted file: %s<pre>%s</pre></body></html>' % ( # filename always placed in fieldname_filename request.args["the-file_filename"][0], # remember to treat the args as files/tempfiles now! cgi.escape(request.args["the-file"][0].read()) ) root = Resource() root.putChild("form", FormPage()) factory = Site(root) ## change to LargeRequest factory.requestFactory = LargeRequest reactor.listenTCP(8880, factory) reactor.run()