def __init__(self, original_children): Resource.__init__(self) try: from twisted.web.server import GzipEncoderFactory from twisted.web.resource import EncodingResourceWrapper for path, child in original_children.items(): self.putChild( path, EncodingResourceWrapper(child, [GzipEncoderFactory()])) except ImportError: pass
def __init__(self, port=80): self.port = port root = Resource() root.putChild("", Redirect("generate")) root.putChild("generate", GeneratorPage()) root.putChild("manage", ManagePage()) root.putChild("download", DownloadPage()) root.putChild("resources", LimitedFile("/srv/templates/static")) wrapped = EncodingResourceWrapper(root, [GzipEncoderFactory()]) site = server.Site(wrapped) self.service = internet.TCPServer(self.port, site) return None
def EncodingJSONRPCServer(server): """ Return wrapped JSON-RPC server that supports HTTP compression (currently gzip). @type server: t.w.r.Resource @param server: Instance of JSONRPCServer @rtype: t.w.r.EncodingResourceWrapper @return: Wrapper that implements HTTP compression """ from twisted.web.resource import EncodingResourceWrapper from twisted.web.server import GzipEncoderFactory return EncodingResourceWrapper(server, [GzipEncoderFactory()])
def __init__(self): Resource.__init__(self) self.putChild(b"status", Status()) self.putChild(b"follow", Follow()) self.putChild(b"delay", Delay()) self.putChild(b"partial", Partial()) self.putChild(b"drop", Drop()) self.putChild(b"raw", Raw()) self.putChild(b"echo", Echo()) self.putChild(b"payload", PayloadResource()) self.putChild( b"xpayload", EncodingResourceWrapper(PayloadResource(), [GzipEncoderFactory()])) self.putChild(b"files", File(os.path.join(tests_datadir, 'test_site/files/'))) self.putChild(b"redirect-to", RedirectTo())
def __init__(self): Resource.__init__(self) self.putChild("status", Status()) self.putChild("follow", Follow()) self.putChild("delay", Delay()) self.putChild("partial", Partial()) self.putChild("drop", Drop()) self.putChild("raw", Raw()) self.putChild("echo", Echo()) if six.PY2 and twisted_version > (12, 3, 0): from twisted.web.test.test_webclient import PayloadResource from twisted.web.server import GzipEncoderFactory from twisted.web.resource import EncodingResourceWrapper self.putChild('payload', PayloadResource()) self.putChild("xpayload", EncodingResourceWrapper(PayloadResource(), [GzipEncoderFactory()]))
def __init__(self, session, path=""): BaseController.__init__(self, path=path, session=session) piconpath = getPiconPath() self.putChild("web", WebController(session)) self.putChild("api", ApiController(session)) self.putChild("ajax", AjaxController(session)) encoder_factory = rest_fs_access.GzipEncodeByFileExtensionFactory( extensions=[ 'txt', 'json', 'html', 'xml', 'js', 'conf', 'cfg', 'eit', 'sc', 'ap' ]) #: gzip compression enabled file controller wrapped_fs_controller = EncodingResourceWrapper( rest_fs_access.FileController(root='/', resource_prefix="/file", session=session), [encoder_factory]) self.putChild("file", wrapped_fs_controller) self.putChild("grab", grabScreenshot(session)) if os.path.exists(getPublicPath('mobile')): self.putChild("mobile", MobileController(session)) self.putChild("m", static.File(getPublicPath() + "/mobile")) self.putChild("js", static.File(getPublicPath() + "/js")) self.putChild("css", static.File(getPublicPath() + "/css")) self.putChild("static", static.File(getPublicPath() + "/static")) self.putChild("images", static.File(getPublicPath() + "/images")) self.putChild("fonts", static.File(getPublicPath() + "/fonts")) if os.path.exists(getPublicPath('themes')): self.putChild("themes", static.File(getPublicPath() + "/themes")) if os.path.exists(getPublicPath('webtv')): self.putChild("webtv", static.File(getPublicPath() + "/webtv")) if os.path.exists(getPublicPath('vxg')): self.putChild("vxg", static.File(getPublicPath() + "/vxg")) if os.path.exists('/usr/bin/shellinaboxd'): self.putChild("terminal", proxy.ReverseProxyResource('::1', 4200, '/')) self.putChild("ipkg", IpkgController(session)) self.putChild("autotimer", ATController(session)) self.putChild("serienrecorder", SRController(session)) self.putChild("epgrefresh", ERController(session)) self.putChild("bouqueteditor", BQEController(session)) self.putChild("transcoding", TranscodingController()) self.putChild("wol", WOLClientController()) self.putChild("wolsetup", WOLSetupController(session)) if piconpath: self.putChild("picon", static.File(piconpath))
def autostart(reason, **kwargs): if "session" in kwargs: sss = StreamServerSeek(session=kwargs["session"]) print "session %s" % sss root = File( eEnv.resolve( "${libdir}/enigma2/python/Plugins/Extensions/StreamServerSeek/web-data" )) root.putChild( "web", ScreenPageCORS(kwargs["session"], util.sibpath(__file__, "web"), True)) root.putChild("stream", StreamResource(kwargs["session"])) root.putChild("proxy", ProxyResource(kwargs["session"])) root.putChild( "vod", EncodingResourceWrapper(VodResource(kwargs["session"]), [M3u8GzipEncoderFactory()])) addExternalChild(("streamserverseek", root))
def __init__(self, port=80): self.port = port root = Resource() root.putChild("", Redirect("generate")) root.putChild("generate", GeneratorPage()) root.putChild("manage", ManagePage()) root.putChild("download", DownloadPage()) root.putChild("settings", SettingsPage()) root.putChild("history", HistoryPage()) root.putChild("resources", LimitedFile("/srv/templates/static")) wrapped = EncodingResourceWrapper(root, [GzipEncoderFactory()]) site = server.Site(wrapped) if settings.DEBUG: site.displayTracebacks = settings.DEBUG else: site.displayTracebacks = False self.service = internet.TCPServer(self.port, site) return None
def getChild(self, path, request): request.postpath = [] if self.master.config["web_function"] != "ddl": self.brief = "DDL Disabled" self.detail = "The owner of this txoffer instance has chosen to disable DDL. Use XDCC or pester them to turn it back on." return self try: number = int(path) except: self.brief = "Invalid Pack Number" self.detail = "Given pack number \"{}\" is not a valid integer".format(path) return self if number not in self.master.packs: self.brief = "No Such Pack" self.detail = "Could not find a pack with number \"{:d}\".".format(number) return self if not os.path.isfile(self.master.packs[number]): self.brief = "Pack #{:d} Deleted".format(number) self.detail = "{} no longer exists.".format(os.path.basename(self.master.packs[number])) return self if self.master.config["proxy_header"] is not None: host = request.getHeader(self.master.config["proxy_header"]) else: host = request.getClientIP() if host in self.factory.authorized and number in self.factory.authorized[host]: if self.master.config["log_requests"]: log.msg("Serving: {} - {}".format(host, os.path.basename(self.master.packs[number])), system="WEB") self.factory.authorized[host].remove(number) request.notifyFinish().addBoth(self.factory.done, host, number) r = EncodingResourceWrapper(File(self.master.packs[number]), [GzipEncoderFactory()]) return ThrottledResource(r, self.master.downloads[host][number], limit=self.master.config["throttle"]) elif host in self.master.downloads and number in self.master.downloads[host]: request.redirect("/") self.type = self.types.REDIRECT return self else: try: position = list(self.master.queue).index((self.factory, host, number)) + 1 element = QueueElement(self.master, os.path.basename(self.master.packs[number]), position) request.write("<!doctype html>\n") d = flatten(request, element, request.write) d.addCallback(lambda _: request.finish()) d.addErrback(request.processingFailed) self.type = self.types.QUEUE return self except ValueError: self.master.queue.append((self.factory, host, number)) self.master.checkQueue() request.redirect("/pack/{:d}/{}".format(number, os.path.basename(self.master.packs[number]))) self.type = self.types.REDIRECT return self
def getChild(self, path, request): return EncodingResourceWrapper(self, [GzipEncoderFactory()])
def gz_wrap(r): return EncodingResourceWrapper(r, [GzipEncoderFactory()])
def buildSite(ack, staticPath): state = WebSocketState(ack) websocket = WebSocketResource(state.factory) static = EncodingResourceWrapper(File(staticPath), [GzipEncoderFactory()]) static.putChild(b'spam', websocket) return Site(static)
def getChild(self, path, request): child = static.File.getChild(self, path, request) return EncodingResourceWrapper(child, [GzipEncoderFactory()])
def gzip_wrapper(page): return EncodingResourceWrapper(page, [GzipEncoderFactory()])
def getChild(self, path, request): # NOQA: N802 request.render_file = path return EncodingResourceWrapper(self, [server.GzipEncoderFactory()])
return self._json_response(request, response_data) if __name__ == '__main__': from twisted.web.resource import Resource, EncodingResourceWrapper from twisted.web.server import Site, GzipEncoderFactory from twisted.internet import reactor # standard factory example factory_s = Site(FileController(DEFAULT_ROOT_PATH)) # experimental factory root = Resource() root.putChild("/", FileController) root.putChild("/file", FileController) factory_r = Site(root) # experimental factory: enable gzip compression wrapped = EncodingResourceWrapper( FileController( root=DEFAULT_ROOT_PATH, # DANGER, WILL ROBINSON! These values allow deletion of ALL files! do_delete=True, delete_whitelist=[]), [GzipEncoderFactory()]) factory_s_gz = Site(wrapped) reactor.listenTCP(18888, factory_s_gz) reactor.run()
request.setHeader("Access-Control-Allow-Origin", '*') request.setHeader("Access-Control-Allow-Methods", 'PUT, GET') return _CorsEncoder(request) class _CorsEncoder(object): """ @ivar _request: A reference to the originating request. @since: 12.3 """ def __init__(self, request): self._request = request def encode(self, data): return data def finish(self): return "" root = JSON_RPC().customize(ExampleServer) wrapped = EncodingResourceWrapper(root, [CorsEncoderFactory()]) site = server.Site(wrapped) # 8008 is the port you want to run under. Choose something >1024 PORT = 8008 print('Listening on port %d...' % PORT) reactor.listenTCP(PORT, site) reactor.run()
#!/usr/bin/python from twisted.web.server import Site, GzipEncoderFactory from twisted.web.static import File from twisted.web.resource import EncodingResourceWrapper from twisted.internet import reactor PORT = 8000 print("serving twisted reactor at port", PORT) wrapped = EncodingResourceWrapper(Site(File(".")), [GzipEncoderFactory()]) site = Site(wrapped) reactor.listenTCP(PORT, site) reactor.run()
def putGZChild(self, path, child): child.isGZ = True self.putChild(six.ensure_binary(path), EncodingResourceWrapper(child, [GzipEncoderFactory()]))
def __init__(self): resource.Resource.__init__(self) self.putChild(b'css', LookupResource('Css', rpath('css'))) if os.path.isfile(rpath('js', 'gettext.js')): self.putChild( b'gettext.js', EncodingResourceWrapper(GetText(), [server.GzipEncoderFactory()]), ) else: log.warning( 'Cannot find "gettext.js" translation file!' ' Text will only be available in English.' ) self.putChild(b'gettext.js', MockGetText()) self.putChild(b'flag', Flag()) self.putChild(b'icons', LookupResource('Icons', rpath('icons'))) self.putChild(b'images', LookupResource('Images', rpath('images'))) self.putChild( b'ui_images', LookupResource( 'UI_Images', common.resource_filename('deluge.ui.data', 'pixmaps') ), ) js = ScriptResource() # configure the dev scripts js.add_script( 'ext-base-debug.js', rpath('js', 'extjs', 'ext-base-debug.js'), 'dev' ) js.add_script( 'ext-all-debug.js', rpath('js', 'extjs', 'ext-all-debug.js'), 'dev' ) js.add_script_folder( 'ext-extensions', rpath('js', 'extjs', 'ext-extensions'), 'dev' ) js.add_script_folder('deluge-all', rpath('js', 'deluge-all'), 'dev') # configure the debug scripts js.add_script( 'ext-base-debug.js', rpath('js', 'extjs', 'ext-base-debug.js'), 'debug' ) js.add_script( 'ext-all-debug.js', rpath('js', 'extjs', 'ext-all-debug.js'), 'debug' ) js.add_script( 'ext-extensions-debug.js', rpath('js', 'extjs', 'ext-extensions-debug.js'), 'debug', ) js.add_script( 'deluge-all-debug.js', rpath('js', 'deluge-all-debug.js'), 'debug' ) # configure the normal scripts js.add_script('ext-base.js', rpath('js', 'extjs', 'ext-base.js')) js.add_script('ext-all.js', rpath('js', 'extjs', 'ext-all.js')) js.add_script('ext-extensions.js', rpath('js', 'extjs', 'ext-extensions.js')) js.add_script('deluge-all.js', rpath('js', 'deluge-all.js')) self.js = js self.putChild(b'js', js) self.putChild( b'json', EncodingResourceWrapper(JSON(), [server.GzipEncoderFactory()]) ) self.putChild( b'upload', EncodingResourceWrapper(Upload(), [server.GzipEncoderFactory()]) ) self.putChild(b'render', Render()) self.putChild(b'themes', Themes(rpath('themes'))) self.putChild(b'tracker', Tracker()) theme = component.get('DelugeWeb').config['theme'] if not os.path.isfile(rpath('themes', 'css', 'xtheme-%s.css' % theme)): theme = CONFIG_DEFAULTS.get('theme') self.__stylesheets.insert(1, 'themes/css/xtheme-%s.css' % theme)
def getChild(self, path, request): # NOQA: N802 child = static.File.getChild(self, path, request) if request.uri.endswith(b'css'): return EncodingResourceWrapper(child, [server.GzipEncoderFactory()]) else: return child
def getChild(self, path, request): # NOQA: N802 if hasattr(request, 'lookup_path'): request.lookup_path += b'/' + path else: request.lookup_path = path return EncodingResourceWrapper(self, [server.GzipEncoderFactory()])
def makeService(config): ini = ConfigParser.RawConfigParser() ini.read(config['config']) configPath = FilePath(config['config']).parent() rproxyConf = dict(ini.items("rproxy")) hostsConf = dict(ini.items("hosts")) hosts = {} for k, v in hostsConf.items(): k = k.lower() hostname, part = k.rsplit("_", 1) if hostname not in hosts: hosts[hostname] = {} hosts[hostname][part] = v if not hosts: raise ValueError("No hosts configured.") for i in hosts: if "port" not in hosts[i]: raise ValueError("All hosts need a port.") if "host" not in hosts[i]: print("%s does not have a host, making localhost" % (i, )) hosts[i]["host"] = "localhost" if "wwwtoo" not in hosts[i]: print("%s does not have an wwwtoo setting, making True" % (i, )) hosts[i]["wwwtoo"] = "True" if "proxysecure" not in hosts[i]: print("%s does not have an proxysecure setting, making False" % (i, )) hosts[i]["proxysecure"] = False hosts[i]["wwwtoo"] = True if hosts[i]["wwwtoo"] == "True" else False hosts[i]["proxysecure"] = True if hosts[i][ "proxysecure"] == "True" else False hosts[i]["sendhsts"] = True if hosts[i].get( "sendhsts") == "True" else False from twisted.internet import reactor pool = HTTPConnectionPool(reactor) resource = EncodingResourceWrapper( RProxyResource(hosts, rproxyConf.get("clacks"), pool, reactor, {}, False), [server.GzipEncoderFactory()]) responder = HTTP01Responder() site = server.Site(EnsureHTTPS(resource, responder.resource), ) multiService = service.MultiService() certificates = rproxyConf.get("certificates", None) if certificates: try: configPath.child(certificates).makedirs() except: pass certificates = configPath.child(certificates).path for i in rproxyConf.get("https_ports").split(","): print("Starting HTTPS on port " + i) multiService.addService( strports.service('txsni:' + certificates + ':tcp:' + i, site)) for host in hosts.keys(): with open(FilePath(certificates).child(host + ".pem").path, 'w'): # Open it so that txacme can find it pass if hosts[host]["wwwtoo"]: with open( FilePath(certificates).child("www." + host + ".pem").path, 'w'): # Open it so that txacme can find it pass for i in rproxyConf.get("http_ports", "").split(","): print("Starting HTTP on port " + i) multiService.addService(strports.service('tcp:' + i, site)) issuingService = AcmeIssuingService( cert_store=DirectoryStore(FilePath(certificates)), client_creator=(lambda: Client.from_url( reactor=reactor, url=LETSENCRYPT_DIRECTORY, key=load_or_create_client_key(FilePath(certificates)), alg=RS256, )), clock=reactor, responders=[responder], ) issuingService.setServiceParent(multiService) return multiService
def makeService(config): ini = RawConfigParser() ini.read(config['config']) configPath = FilePath(config['config']).parent() rproxyConf = dict(ini.items("rproxy")) hostsConf = dict(ini.items("hosts")) hosts = {} for k, v in hostsConf.items(): k = k.lower() hostname, part = k.rsplit("_", 1) if hostname not in hosts: hosts[hostname] = {} hosts[hostname][part] = v if not hosts: raise ValueError("No hosts configured.") for i in hosts: if "port" not in hosts[i]: raise ValueError("All hosts need a port.") if "host" not in hosts[i]: print("%s does not have a host, making localhost" % (i, )) hosts[i]["host"] = "localhost" if "onlysecure" not in hosts[i]: print("%s does not have an onlysecure setting, making False" % (i, )) hosts[i]["onlysecure"] = False if "wwwtoo" not in hosts[i]: print("%s does not have an wwwtoo setting, making True" % (i, )) hosts[i]["wwwtoo"] = "True" if "proxysecure" not in hosts[i]: print("%s does not have an proxysecure setting, making False" % (i, )) hosts[i]["proxysecure"] = False if "sendhsts" not in hosts[i]: print( "%s does not have an sendhsts setting, making the value of onlysecure" % (i, )) hosts[i]["sendhsts"] = hosts[i]["onlysecure"] hosts[i][ "onlysecure"] = True if hosts[i]["onlysecure"] == "True" else False hosts[i]["proxysecure"] = True if hosts[i][ "proxysecure"] == "True" else False hosts[i][ "sendhsts"] = True if hosts[i]["sendhsts"] == "True" else False hosts[i]["wwwtoo"] = True if hosts[i]["wwwtoo"] == "True" else False if hosts[i]["onlysecure"] and not hosts[i]["proxysecure"]: if not hosts[i].get("iamokwithalocalnetworkattackerpwningmyusers", "False") == "True": raise ValueError( "%s has onlysecure==True, but proxysecure==False. This will mean TLS protected requests will not be TLS-protected between the proxy and the proxied server. If this is okay (e.g., if it's going over localhost), set %s_iamokwithalocalnetworkattackerpwningmyusers=True in your config." % (i, i)) if hosts[i]["proxysecure"] and not hosts[i]["onlysecure"]: if not hosts[i].get( "iamokwithlyingtomyproxiedserverthatheuserisoverhttps", "False") == "True": raise ValueError( "%s has onlysecure==False, but proxysecure==True. This means that the connection may not be TLS protected between the user and this proxy, only the proxy and the proxied server. This can trick your proxied server into thinking the user is being served over HTTPS. If this is okay (I can't imagine why it is), set %s_iamokwithlyingtomyproxiedserverthatheuserisoverhttps=True in your config." % (i, i)) from twisted.internet import reactor from twisted.web.client import HTTPConnectionPool pool = HTTPConnectionPool(reactor) anonymous = False resource = EncodingResourceWrapper( RProxyResource(hosts, rproxyConf.get("clacks"), pool, reactor, {}, False), [server.GzipEncoderFactory()]) site = server.Site(resource) multiService = service.MultiService() certificates = rproxyConf.get("certificates", None) if certificates: try: configPath.child(certificates).makedirs() except: pass certificates = configPath.child(certificates).path for i in rproxyConf.get("https_ports").split(","): print("Starting HTTPS on port " + i) multiService.addService( strports.service('le:' + certificates + ':tcp:' + i, site)) for host in hosts.keys(): with open(FilePath(certificates).child(host + ".pem").path, 'a'): # Open it so that txacme can find it pass if hosts[host]["wwwtoo"]: with open( FilePath(certificates).child("www." + host + ".pem").path, 'a'): # Open it so that txacme can find it pass for i in rproxyConf.get("http_ports", "").split(","): print("Starting HTTP on port " + i) multiService.addService(strports.service('tcp:' + i, site)) return multiService
from twisted.web.server import Site, GzipEncoderFactory from twisted.web.resource import Resource, EncodingResourceWrapper from twisted.internet import reactor class Daak(Resource): isLeaf = True def render_GET(self, request): return "<html>Hello, world!</html>" def render_POST(self, request): return '<html><body>You submitted: %s</body></html>' % (cgi.escape( request.args["the-field"][0]), ) resourceDaak = Daak() wrappedDaak = EncodingResourceWrapper(resourceDaak, [GzipEncoderFactory()]) site = Site(wrappedDaak) reactor.listenTCP(5000, site) reactor.run()
def getChild(self, path, request): if self.isGZ: return EncodingResourceWrapper(self.__class__(self.session, path), [GzipEncoderFactory()]) else: return self.__class__(self.session, path)
def getChild(self, path, request): if path in self.children: return self.children[path] return EncodingResourceWrapper(self, [GzipEncoderFactory()])