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_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 cgi_main(config: SectionProxy) -> None: """Run REDbot as a CGI Script.""" def out(inbytes: bytes) -> None: try: sys.stdout.buffer.write(inbytes) sys.stdout.flush() except IOError: pass config['ui_uri'] = "%s://%s%s%s" % ('HTTPS' in os.environ and "https" or "http", os.environ.get('HTTP_HOST'), os.environ.get('SCRIPT_NAME'), os.environ.get('PATH_INFO', '')) method = os.environ.get('REQUEST_METHOD').encode(config['charset']) query_string = os.environ.get('QUERY_STRING', "").encode(config['charset']) req_hdrs = [] # type: RawHeaderListType for (k, v) in os.environ: if k[:5] == 'HTTP_': req_hdrs.append((k[:5].lower().encode('ascii'), v.encode('ascii'))) def response_start(code: bytes, phrase: bytes, res_hdrs: RawHeaderListType) -> None: out_v = [b"Status: %s %s" % (code, phrase)] for name, value in res_hdrs: out_v.append(b"%s: %s" % (name, value)) out_v.append(b"") out_v.append(b"") out(b"\n".join(out_v)) freak_ceiling = 20000 def response_body(chunk: bytes) -> None: rest = None if len(chunk) > freak_ceiling: rest = chunk[freak_ceiling:] chunk = chunk[:freak_ceiling] out(chunk) if rest: response_body(rest) def response_done(trailers: RawHeaderListType) -> None: thor.schedule(0, thor.stop) try: RedWebUi(config, method.decode(config['charset']), query_string, req_hdrs, response_start, response_body, response_done) thor.run() except Exception: except_handler_factory(config, qs=query_string.decode(config['charset']))()
def cgi_main(): """Run REDbot as a CGI Script.""" def out(inbytes): try: sys.stdout.buffer.write(inbytes) sys.stdout.flush() except IOError: pass ui_uri = "%s://%s%s%s" % ('HTTPS' in os.environ and "https" or "http", os.environ.get('HTTP_HOST'), os.environ.get('SCRIPT_NAME'), os.environ.get('PATH_INFO', '')) method = os.environ.get('REQUEST_METHOD').encode(Config.charset) query_string = os.environ.get('QUERY_STRING', "").encode(Config.charset) def response_start(code, phrase, res_hdrs): out_v = [b"Status: %s %s" % (code, phrase)] for k, v in res_hdrs: out_v.append(b"%s: %s" % (k, v)) out_v.append(b"") out_v.append(b"") out(b"\n".join(out_v)) freak_ceiling = 20000 def response_body(chunk): rest = None if len(chunk) > freak_ceiling: rest = chunk[freak_ceiling:] chunk = chunk[:freak_ceiling] out(chunk) if rest: response_body(rest) def response_done(trailers): thor.schedule(0, thor.stop) try: RedWebUi(Config, ui_uri, method, query_string, response_start, response_body, response_done) thor.run() except: except_handler_factory(Config, qs=query_string)()
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 cgi_main(config: SectionProxy) -> None: """Run REDbot as a CGI Script.""" def out(inbytes: bytes) -> None: try: sys.stdout.buffer.write(inbytes) sys.stdout.flush() except IOError: pass config["ui_uri"] = "%s://%s%s%s" % ( "HTTPS" in os.environ and "https" or "http", os.environ.get("HTTP_HOST"), os.environ.get("SCRIPT_NAME"), os.environ.get("PATH_INFO", ""), ) method = os.environ.get("REQUEST_METHOD").encode(config["charset"]) query_string = os.environ.get("QUERY_STRING", "").encode(config["charset"]) req_hdrs = [] # type: RawHeaderListType for (k, v) in os.environ: if k[:5] == "HTTP_": req_hdrs.append((k[:5].lower().encode("ascii"), v.encode("ascii"))) req_body = sys.stdin.read().encode("utf-8") class Exchange(HttpResponseExchange): @staticmethod def response_start(code: bytes, phrase: bytes, res_hdrs: RawHeaderListType) -> None: out_v = [b"Status: %s %s" % (code, phrase)] for name, value in res_hdrs: out_v.append(b"%s: %s" % (name, value)) out_v.append(b"") out_v.append(b"") out(b"\n".join(out_v)) @staticmethod def response_body(chunk: bytes) -> None: freak_ceiling = 20000 rest = None if len(chunk) > freak_ceiling: rest = chunk[freak_ceiling:] chunk = chunk[:freak_ceiling] out(chunk) if rest: Exchange.response_body(rest) @staticmethod def response_done(trailers: RawHeaderListType) -> None: thor.schedule(0, thor.stop) try: RedWebUi( config, method.decode(config["charset"]), query_string, req_hdrs, req_body, Exchange(), ) thor.run() except Exception: except_handler_factory(config, qs=query_string.decode(config["charset"]))()
def mod_python_handler(r): """Run REDbot as a mod_python handler.""" from mod_python import apache status_lookup = { 100: apache.HTTP_CONTINUE, 101: apache.HTTP_SWITCHING_PROTOCOLS, 102: apache.HTTP_PROCESSING, 200: apache.HTTP_OK, 201: apache.HTTP_CREATED, 202: apache.HTTP_ACCEPTED, 203: apache.HTTP_NON_AUTHORITATIVE, 204: apache.HTTP_NO_CONTENT, 205: apache.HTTP_RESET_CONTENT, 206: apache.HTTP_PARTIAL_CONTENT, 207: apache.HTTP_MULTI_STATUS, 300: apache.HTTP_MULTIPLE_CHOICES, 301: apache.HTTP_MOVED_PERMANENTLY, 302: apache.HTTP_MOVED_TEMPORARILY, 303: apache.HTTP_SEE_OTHER, 304: apache.HTTP_NOT_MODIFIED, 305: apache.HTTP_USE_PROXY, 307: apache.HTTP_TEMPORARY_REDIRECT, 308: apache.HTTP_PERMANENT_REDIRECT, 400: apache.HTTP_BAD_REQUEST, 401: apache.HTTP_UNAUTHORIZED, 402: apache.HTTP_PAYMENT_REQUIRED, 403: apache.HTTP_FORBIDDEN, 404: apache.HTTP_NOT_FOUND, 405: apache.HTTP_METHOD_NOT_ALLOWED, 406: apache.HTTP_NOT_ACCEPTABLE, 407: apache.HTTP_PROXY_AUTHENTICATION_REQUIRED, 408: apache.HTTP_REQUEST_TIME_OUT, 409: apache.HTTP_CONFLICT, 410: apache.HTTP_GONE, 411: apache.HTTP_LENGTH_REQUIRED, 412: apache.HTTP_PRECONDITION_FAILED, 413: apache.HTTP_REQUEST_ENTITY_TOO_LARGE, 414: apache.HTTP_REQUEST_URI_TOO_LARGE, 415: apache.HTTP_UNSUPPORTED_MEDIA_TYPE, 416: apache.HTTP_RANGE_NOT_SATISFIABLE, 417: apache.HTTP_EXPECTATION_FAILED, 422: apache.HTTP_UNPROCESSABLE_ENTITY, 423: apache.HTTP_LOCKED, 424: apache.HTTP_FAILED_DEPENDENCY, 426: apache.HTTP_UPGRADE_REQUIRED, 500: apache.HTTP_INTERNAL_SERVER_ERROR, 501: apache.HTTP_NOT_IMPLEMENTED, 502: apache.HTTP_BAD_GATEWAY, 503: apache.HTTP_SERVICE_UNAVAILABLE, 504: apache.HTTP_GATEWAY_TIME_OUT, 505: apache.HTTP_VERSION_NOT_SUPPORTED, 506: apache.HTTP_VARIANT_ALSO_VARIES, 507: apache.HTTP_INSUFFICIENT_STORAGE, 510: apache.HTTP_NOT_EXTENDED } r.content_type = "text/html" def response_start(code, phrase, hdrs): r.status = status_lookup.get(int(code), apache.HTTP_INTERNAL_SERVER_ERROR) for hdr in hdrs: r.headers_out[hdr[0]] = hdr[1] def response_done(trailers): thor.schedule(0, thor.stop) try: RedWebUi(Config, r.unparsed_uri, r.method, r.args or "", response_start, r.write, response_done) thor.run() except: except_handler_factory(Config, r.write, qs=r.args)() return apache.OK