def request_start(method, uri, req_hdrs): p_uri = urlsplit(uri) if p_uri.path in static_files: headers = [] file_ext = os.path.splitext(p_uri.path)[1].lower() content_encoding = static_types.get( file_ext, b'application/octet-stream') headers.append((b'Content-Encoding', content_encoding)) headers.append((b'Cache-Control', b'max-age=300')) x.response_start(b"200", b"OK", headers) x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == b"/": try: RedWebUi(Config, '/', method, p_uri.query, x.response_start, x.response_body, x.response_done) except Exception: sys.stderr.write(""" *** FATAL ERROR REDbot has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) import traceback traceback.print_exc() thor.stop() else: x.response_start(b"404", b"Not Found", []) x.response_done([])
def request_start(method, uri, req_hdrs): p_uri = urlsplit(uri) if static_files.has_key(p_uri.path): x.response_start("200", "OK", []) # TODO: headers x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == "/": query_string = cgi.parse_qs(p_uri.query) try: RedWebUi('/', method, query_string, x.response_start, x.response_body, x.response_done) except RuntimeError: raise sys.stderr.write(""" *** FATAL ERROR RED has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) except_handler_factory(sys.stderr.write)() sys.stderr.write("\n") thor.stop() sys.exit(1) else: x.response_start("404", "Not Found", []) x.response_done([])
def request_start(method: bytes, uri: bytes, req_hdrs: RawHeaderListType) -> None: p_uri = urlsplit(uri) if p_uri.path in static_files: headers = [] file_ext = os.path.splitext(p_uri.path)[1].lower() content_encoding = static_types.get(file_ext, b'application/octet-stream') headers.append((b'Content-Encoding', content_encoding)) headers.append((b'Cache-Control', b'max-age=300')) x.response_start(b"200", b"OK", headers) x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == b"/": try: RedWebUi(config, method.decode(config['charset']), p_uri.query, x.response_start, x.response_body, x.response_done) except Exception: sys.stderr.write(""" *** FATAL ERROR REDbot has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) import traceback traceback.print_exc() thor.stop() sys.exit(1) else: x.response_start(b"404", b"Not Found", []) x.response_done([])
def standalone_main(host, port, static_dir): """Run RED as a standalone Web server.""" # load static files static_files = {} def static_walker(arg, dirname, names): for name in names: try: path = os.path.join(dirname, name) if os.path.isdir(path): continue uri = os.path.relpath(path, static_dir) static_files["/static/%s" % uri] = open(path).read() except IOError: sys.stderr.write( "* Problem loading %s\n" % path ) os.path.walk(static_dir, static_walker, "") def red_handler(x): @thor.events.on(x) def request_start(method, uri, req_hdrs): p_uri = urlsplit(uri) if static_files.has_key(p_uri.path): x.response_start("200", "OK", []) # TODO: headers x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == "/": query_string = cgi.parse_qs(p_uri.query) try: RedWebUi('/', method, query_string, x.response_start, x.response_body, x.response_done ) except RuntimeError: raise sys.stderr.write(""" *** FATAL ERROR RED has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) except_handler_factory(sys.stderr.write)() sys.stderr.write("\n") thor.stop() sys.exit(1) else: x.response_start("404", "Not Found", []) x.response_done([]) server = thor.http.HttpServer(host, port) server.on('exchange', red_handler) try: thor.run() except KeyboardInterrupt: sys.stderr.write("Stopping...\n") thor.stop()
def standalone_main(config: SectionProxy) -> None: """Run REDbot as a standalone Web server.""" # load static files static_types = { b'.js': b'text/javascript', b'.css': b'text/css', b'.png': b'image/png', } static_files = {} for root, dirs, files in os.walk(config['static_dir']): for name in files: try: path = os.path.join(root, name) uri = os.path.relpath(path, config['static_dir']) static_files[b"/static/%s" % uri.encode('utf-8')] = open(path, 'rb').read() except IOError: sys.stderr.write("* Problem loading %s\n" % path) def red_handler(x: EventEmitter) -> None: @thor.events.on(x) def request_start(method: bytes, uri: bytes, req_hdrs: RawHeaderListType) -> None: p_uri = urlsplit(uri) if p_uri.path in static_files: headers = [] file_ext = os.path.splitext(p_uri.path)[1].lower() content_encoding = static_types.get(file_ext, b'application/octet-stream') headers.append((b'Content-Encoding', content_encoding)) headers.append((b'Cache-Control', b'max-age=300')) x.response_start(b"200", b"OK", headers) x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == b"/": try: RedWebUi(config, method.decode(config['charset']), p_uri.query, x.response_start, x.response_body, x.response_done) except Exception: sys.stderr.write(""" *** FATAL ERROR REDbot has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) import traceback traceback.print_exc() thor.stop() sys.exit(1) else: x.response_start(b"404", b"Not Found", []) x.response_done([]) server = thor.http.HttpServer(config.get('host', ''), int(config['port'])) server.on('exchange', red_handler) try: thor.run() except KeyboardInterrupt: sys.stderr.write("Stopping...\n") thor.stop()
def request_start(method, uri, req_hdrs): p_uri = urlsplit(uri) if static_files.has_key(p_uri.path): x.response_start("200", "OK", []) # TODO: headers x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == "/": query_string = cgi.parse_qs(p_uri.query) try: RedWebUi('/', method, query_string, x.response_start, x.response_body, x.response_done ) except RuntimeError: raise sys.stderr.write(""" *** FATAL ERROR RED has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) except_handler_factory(sys.stderr.write)() sys.stderr.write("\n") thor.stop() sys.exit(1) else: x.response_start("404", "Not Found", []) x.response_done([])
def standalone_main(port, static_dir): """Run RED as a standalone Web server.""" # load static files static_files = {} def static_walker(arg, dirname, names): for name in names: try: path = os.path.join(dirname, name) if os.path.isdir(path): continue uri = os.path.relpath(path, static_dir) static_files["/static/%s" % uri] = open(path).read() except IOError: sys.stderr.write("* Problem loading %s\n" % path) os.path.walk(static_dir, static_walker, "") sys.stderr.write("* Static files loaded.\n") def red_handler(x): @thor.events.on(x) def request_start(method, uri, req_hdrs): p_uri = urlsplit(uri) if static_files.has_key(p_uri.path): x.response_start("200", "OK", []) # TODO: headers x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == "/": query_string = cgi.parse_qs(p_uri.query) try: RedWebUi('/', method, query_string, x.response_start, x.response_body, x.response_done) except RuntimeError: raise sys.stderr.write(""" *** FATAL ERROR RED has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) except_handler_factory(sys.stderr.write)() sys.stderr.write("\n") thor.stop() sys.exit(1) else: x.response_start("404", "Not Found", []) x.response_done([]) server = thor.HttpServer("", port) server.on('exchange', red_handler) try: thor.run() except KeyboardInterrupt: sys.stderr.write("Stopping...\n") thor.stop()
def __init__(self) -> None: # Set up the watchdog if os.environ.get("SYSTEMD_WATCHDOG"): thor.schedule(self.watchdog_freq, self.watchdog_ping) signal.signal(signal.SIGABRT, self.abrt_handler) # Set up the server server = thor.http.HttpServer(config.get("host", ""), int(config["port"])) server.on("exchange", RedHandler) try: thor.run() except KeyboardInterrupt: sys.stderr.write("Stopping...\n") thor.stop()
def request_done(self, trailers: RawHeaderListType) -> None: p_uri = urlsplit(self.uri) if p_uri.path in self.static_files: file_ext = os.path.splitext(p_uri.path)[1].lower() or b".html" content_type = self.static_types.get(file_ext, b"application/octet-stream") headers = [] headers.append((b"Content-Type", content_type)) headers.append((b"Cache-Control", b"max-age=3600")) self.exchange.response_start(b"200", b"OK", headers) self.exchange.response_body(self.static_files[p_uri.path]) self.exchange.response_done([]) elif p_uri.path == b"/": try: self.req_hdrs.append( ( b"client-ip", self.exchange.http_conn.tcp_conn.socket.getpeername()[0].encode( "idna" ), ) ) RedWebUi( self.config, self.method.decode(self.config["charset"]), p_uri.query, self.req_hdrs, self.req_body, self.exchange, self.error_log, ) except Exception: self.error_log( """ *** FATAL ERROR REDbot has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """ ) dump = traceback.format_exc() thor.stop() self.error_log(dump) sys.exit(1) else: headers = [] headers.append((b"Content-Type", b"text/plain")) headers.append((b"Cache-Control", b"max-age=3600")) self.exchange.response_start(b"404", b"Not Found", headers) self.exchange.response_body(b"'%s' not found." % p_uri.path) self.exchange.response_done([])
def __init__(self, config: SectionProxy) -> None: self.config = config self.static_files = {} # type: Dict[bytes, bytes] # Load static files self.walk_files(config['asset_dir'], b"static/") if config.get('extra_base_dir'): self.walk_files(config['extra_base_dir']) # Set up the watchdog if os.environ.get("SYSTEMD_WATCHDOG"): thor.schedule(self.watchdog_freq, self.watchdog_ping) # Set up the server server = thor.http.HttpServer(config.get('host', ''), int(config['port'])) server.on('exchange', self.red_handler) try: thor.run() except KeyboardInterrupt: sys.stderr.write("Stopping...\n") thor.stop()
def request_start(method: bytes, uri: bytes, req_hdrs: RawHeaderListType) -> None: p_uri = urlsplit(uri) if p_uri.path in self.static_files: file_ext = os.path.splitext(p_uri.path)[1].lower() or b'.html' content_type = self.static_types.get( file_ext, b'application/octet-stream') headers = [] headers.append((b'Content-Type', content_type)) headers.append((b'Cache-Control', b'max-age=3600')) x.response_start(b"200", b"OK", headers) x.response_body(self.static_files[p_uri.path]) x.response_done([]) elif p_uri.path == b"/": try: req_hdrs.append((b'client-ip', x.http_conn.tcp_conn.socket.getpeername() [0].encode('idna'))) RedWebUi(self.config, method.decode(self.config['charset']), p_uri.query, req_hdrs, x.response_start, x.response_body, x.response_done, self.error_log) except Exception: self.error_log(""" *** FATAL ERROR REDbot has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) import traceback dump = traceback.format_exc() thor.stop() self.error_log(dump) sys.exit(1) else: headers = [] headers.append((b'Content-Type', b'text/plain')) headers.append((b'Cache-Control', b'max-age=3600')) x.response_start(b"404", b"Not Found", headers) x.response_body(b"'%s' not found." % p_uri.path) x.response_done([])
def request_start(method: bytes, uri: bytes, req_hdrs: RawHeaderListType) -> None: p_uri = urlsplit(uri) if p_uri.path in self.static_files: file_ext = os.path.splitext(p_uri.path)[1].lower() or b'.html' content_type = self.static_types.get(file_ext, b'application/octet-stream') headers = [] headers.append((b'Content-Type', content_type)) headers.append((b'Cache-Control', b'max-age=3600')) x.response_start(b"200", b"OK", headers) x.response_body(self.static_files[p_uri.path]) x.response_done([]) elif p_uri.path == b"/": try: req_hdrs.append( (b'client-ip', x.http_conn.tcp_conn.socket.getpeername()[0].encode('idna'))) RedWebUi(self.config, method.decode(self.config['charset']), p_uri.query, req_hdrs, x.response_start, x.response_body, x.response_done, self.error_log) except Exception: self.error_log(""" *** FATAL ERROR REDbot has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) import traceback dump = traceback.format_exc() thor.stop() self.error_log(dump) sys.exit(1) else: headers = [] headers.append((b'Content-Type', b'text/plain')) headers.append((b'Cache-Control', b'max-age=3600')) x.response_start(b"404", b"Not Found", headers) x.response_body(b"'%s' not found." % p_uri.path) x.response_done([])
def __init__(self, config: SectionProxy) -> None: self.config = config self.handler = partial(RedHandler, server=self) # Set up the watchdog if os.environ.get("SYSTEMD_WATCHDOG"): thor.schedule(self.watchdog_freq, self.watchdog_ping) signal.signal(signal.SIGABRT, self.abrt_handler) # Read static files self.static_files = self.walk_files(self.config["asset_dir"], b"static/") if self.config.get("extra_base_dir"): self.static_files.update(self.walk_files(self.config["extra_base_dir"])) # Set up the server server = thor.http.HttpServer( self.config.get("host", ""), int(self.config["port"]) ) server.on("exchange", self.handler) try: thor.run() except KeyboardInterrupt: sys.stderr.write("Stopping...\n") thor.stop()
def fetch_done() -> None: print("done") thor.stop()
def fetch_done() -> None: print('done') thor.stop()
def standalone_main(host, port, static_dir): """Run REDbot as a standalone Web server.""" # load static files static_types = { '.js': b'text/javascript', '.css': b'text/css', '.png': b'image/png', } static_files = {} for root, dirs, files in os.walk(static_dir): for name in files: try: path = os.path.join(root, name) uri = os.path.relpath(path, static_dir) static_files[b"/static/%s" % uri.encode('utf-8')] = open( path, 'rb').read() except IOError: sys.stderr.write("* Problem loading %s\n" % path) def red_handler(x): @thor.events.on(x) def request_start(method, uri, req_hdrs): p_uri = urlsplit(uri) if p_uri.path in static_files: headers = [] file_ext = os.path.splitext(p_uri.path)[1].lower() content_encoding = static_types.get( file_ext, b'application/octet-stream') headers.append((b'Content-Encoding', content_encoding)) headers.append((b'Cache-Control', b'max-age=300')) x.response_start(b"200", b"OK", headers) x.response_body(static_files[p_uri.path]) x.response_done([]) elif p_uri.path == b"/": try: RedWebUi(Config, '/', method, p_uri.query, x.response_start, x.response_body, x.response_done) except Exception: sys.stderr.write(""" *** FATAL ERROR REDbot has encountered a fatal error which it really, really can't recover from in standalone server mode. Details follow. """) import traceback traceback.print_exc() thor.stop() else: x.response_start(b"404", b"Not Found", []) x.response_done([]) server = thor.http.HttpServer(host, port) server.on('exchange', red_handler) try: thor.run() except KeyboardInterrupt: sys.stderr.write("Stopping...\n") thor.stop()
def line_done(self, *args): self.outstanding -= 1 self.handle_result(*args) if (not self.running) and self.outstanding <= 0: stop()
def result(*args): print_result(*args) stop()