def __call__(self, header, file_size): prefix = "bytes=" if not header.startswith(prefix): raise HTTPException(416, message="Unrecognised range type %s" % (header,)) parts = header[len(prefix):].split(",") ranges = [] for item in parts: components = item.split("-") if len(components) != 2: raise HTTPException(416, "Bad range specifier %s" % (item)) data = [] for component in components: if component == "": data.append(None) else: try: data.append(int(component)) except ValueError: raise HTTPException(416, "Bad range specifier %s" % (item)) try: ranges.append(Range(data[0], data[1], file_size)) except ValueError: raise HTTPException(416, "Bad range specifier %s" % (item)) return self.coalesce_ranges(ranges, file_size)
def python_script_handler(request, response): path = request.filesystem_path try: environ = {"__file__": path} execfile(path, environ, environ) if "main" in environ: handler = FunctionHandler(environ["main"]) handler(request, response) else: raise HTTPException(500) except IOError: raise HTTPException(404)
def __call__(self, request, response): path = filesystem_path(self.base_path, request, self.url_base) try: environ = {"__file__": path} execfile(path, environ, environ) if "main" in environ: handler = FunctionHandler(environ["main"]) handler(request, response) else: raise HTTPException(500, "No main function in script %s" % path) except IOError: raise HTTPException(404)
def as_is_handler(request, response): path = request.filesystem_path try: with open(path) as f: response.writer.write_content(f.read()) except IOError: raise HTTPException(404)
def __call__(self, request, response): path = request.filesystem_path if os.path.isdir(path): return directory_handler(request, response) try: #This is probably racy with some other process trying to change the file file_size = os.stat(path).st_size response.headers.update(self.get_headers(request, path)) if "Range" in request.headers: try: byte_ranges = RangeParser()(request.headers['Range'], file_size) except HTTPException as e: if e.code == 416: response.headers.set("Content-Range", "bytes */%i" % file_size) raise else: byte_ranges = None data = self.get_data(response, path, byte_ranges) response.content = data query = urlparse.parse_qs(request.url_parts.query) pipeline = None if "pipe" in query: pipeline = Pipeline(query["pipe"][-1]) elif os.path.splitext(path)[0].endswith(".sub"): pipeline = Pipeline("sub") if pipeline is not None: response = pipeline(request, response) return response except (OSError, IOError): raise HTTPException(404)
def __call__(self, request, response): path = filesystem_path(self.base_path, request, self.url_base) try: with open(path) as f: response.writer.write_content(f.read()) response.close_connection = True except IOError: raise HTTPException(404)
def filesystem_path(base_path, request, url_base="/"): if base_path is None: base_path = request.doc_root path = request.url_parts.path if path.startswith(url_base): path = path[len(url_base):] if ".." in path: raise HTTPException(404) new_path = os.path.join(base_path, path) # Otherwise setting path to / allows access outside the root directory if not new_path.startswith(base_path): raise HTTPException(404) return new_path
def inner(request, response): try: rv = func(request, response) except Exception: msg = traceback.format_exc() raise HTTPException(500, message=msg) if rv is not None: if isinstance(rv, tuple): if len(rv) == 3: status, headers, content = rv response.status = status elif len(rv) == 2: headers, content = rv else: raise HTTPException(500) response.headers.update(headers) else: content = rv response.content = content
def __call__(self, request, response): if not request.url_parts.path.endswith("/"): raise HTTPException(404) path = filesystem_path(self.base_path, request, self.url_base) if not os.path.isdir(path): raise HTTPException(404, "%s is not a directory" % path) response.headers = [("Content-Type", "text/html")] response.content = """<!doctype html> <meta name="viewport" content="width=device-width"> <title>Directory listing for %(path)s</title> <h1>Directory listing for %(path)s</h1> <ul> %(items)s </ul> """ % {"path": cgi.escape(request.url_parts.path), "items": "\n".join(self.list_items(request, path))}
def filesystem_path(self): if self._filesystem_path is None: path = self.url_parts.path if path.startswith("/"): path = path[1:] if ".." in path: raise HTTPException(500) self._filesystem_path = os.path.join(self.doc_root, path) logger.debug(self._filesystem_path) return self._filesystem_path
def __init__(self, headers): self.username = None self.password = None auth_schemes = {"Basic": self.decode_basic} if "authorization" in headers: header = headers.get("authorization") auth_type, data = header.split(" ", 1) if auth_type in auth_schemes: self.username, self.password = auth_schemes[auth_type](data) else: raise HTTPException(400, "Unsupported authentication scheme %s" % auth_type)
def __call__(self, request, response): path = filesystem_path(self.base_path, request, self.url_base) if not os.path.isdir(path): raise HTTPException(404, "%s is not a directory" % path) response.headers = [("Content-Type", "text/html")] jump_index = """<!doctype html> <head> <meta http-equiv="Refresh" content="0; url=%s"> </head>""" if request.url_parts.path.endswith("/"): response.content = jump_index % "/runner/index.html" elif request.url_parts.path.endswith("/runner") or request.url_parts.path.endswith("/runner/"): response.content = jump_index % "/runner/index.html"