コード例 #1
0
ファイル: errors.py プロジェクト: 5l1v3r1/BlackSheep
def _load_error_page_template() -> str:
    error_css = get_resource_file_content("error.css")
    error_template = get_resource_file_content("error.html")
    assert "/*STYLES*/" in error_template

    # since later it is used in format_map...
    error_css = error_css.replace("{", "{{").replace("}", "}}")
    return error_template.replace("/*STYLES*/", error_css)
コード例 #2
0
 def get_openapi_ui_html(self) -> str:
     """
     Returns the HTML response to serve the Swagger UI.
     """
     return get_resource_file_content("openapi-ui.html").replace(
         "##SPEC_URL##", self.get_spec_path()
     )
コード例 #3
0
    def __init__(self,
                 router: Optional[Router] = None,
                 middlewares: Optional[List[Callable]] = None,
                 resources: Optional[Resources] = None,
                 services: Optional[ServicesType] = None,
                 debug: bool = False,
                 show_error_details: bool = False):
        if router is None:
            router = Router()
        if services is None:
            services = Container()
        super().__init__(get_show_error_details(debug or show_error_details), router)

        if middlewares is None:
            middlewares = []
        if resources is None:
            resources = Resources(get_resource_file_content('error.html'))
        self.services = services  # type: ServicesType
        self.debug = debug
        self.middlewares = middlewares
        self.access_logger = None
        self.logger = None
        self._default_headers = None
        self._use_sync_logging = False
        self._middlewares_configured = False
        self.resources = resources
        self._serve_files = None
        self._authentication_strategy = None  # type: Optional[AuthenticationStrategy]
        self._authorization_strategy = None  # type: Optional[AuthorizationStrategy]
        self.on_start = ApplicationEvent(self)
        self.on_stop = ApplicationEvent(self)
        self.started = False
        self.controllers_router: RoutesRegistry = controllers_router
コード例 #4
0
    def __init__(
        self,
        *,
        router: Optional[Router] = None,
        resources: Optional[Resources] = None,
        services: Optional[Container] = None,
        debug: bool = False,
        show_error_details: bool = False,
    ):
        if router is None:
            router = Router()
        if services is None:
            services = Container()
        super().__init__(show_error_details, router)

        if resources is None:
            resources = Resources(get_resource_file_content("error.html"))
        self.services: Container = services
        self._service_provider: Optional[Services] = None
        self.debug = debug
        self.middlewares: List[Callable[..., Awaitable[Response]]] = []
        self.access_logger = None
        self.logger = None
        self._default_headers: Optional[Tuple[Tuple[str, str], ...]] = None
        self._middlewares_configured = False
        self.resources = resources
        self._authentication_strategy: Optional[AuthenticationStrategy] = None
        self._authorization_strategy: Optional[AuthorizationStrategy] = None
        self.on_start = ApplicationEvent(self)
        self.after_start = ApplicationEvent(self)
        self.on_stop = ApplicationEvent(self)
        self.started = False
        self.controllers_router: RoutesRegistry = controllers_router
        self.files_handler = FilesHandler()
コード例 #5
0
ファイル: application.py プロジェクト: hzlmn/BlackSheep
    def __init__(self,
                 options: Optional[ServerOptions] = None,
                 router: Optional[Router] = None,
                 middlewares: Optional[List[Callable]] = None,
                 resources: Optional[Resources] = None,
                 services: Any = None,
                 debug: bool = False):
        if not options:
            options = ServerOptions('', 8000)
        if router is None:
            router = Router()
        if services is None:
            services = {}
        super().__init__(options, router, services)

        if middlewares is None:
            middlewares = []
        if resources is None:
            resources = Resources(get_resource_file_content('error.html'))
        self.debug = debug
        self.running = False
        self.middlewares = middlewares
        self.current_timestamp = get_current_timestamp()
        self.processes = []
        self.access_logger = None
        self.logger = None
        self._default_headers = None
        self._use_sync_logging = False
        self._middlewares_configured = False
        self.resources = resources
        self._serve_files = None
        self._serve_static_files = None
        self.on_start = ApplicationEvent(self)
        self.on_stop = ApplicationEvent(self)
コード例 #6
0
ファイル: ui.py プロジェクト: gitter-badger/BlackSheep
 def get_openapi_ui_html(self, options: UIOptions) -> str:
     """
     Returns the HTML response to serve the Swagger UI.
     """
     return (get_resource_file_content("redoc-ui.html").replace(
         "##SPEC_URL##", options.spec_url).replace("##PAGE_TITLE##",
                                                   options.page_title))
コード例 #7
0
ファイル: dynamic.py プロジェクト: 5l1v3r1/BlackSheep
def get_files_route_handler(
    files_handler: FilesHandler,
    source_folder_name: str,
    discovery: bool,
    cache_time: int,
    extensions: Set[str],
    root_path: str,
    index_document: Optional[str],
    fallback_document: Optional[str],
) -> Callable[[Request], Awaitable[Response]]:
    files_list_html = get_resource_file_content("fileslist.html")
    source_folder_full_path = os.path.abspath(str(source_folder_name))

    async def static_files_handler(request: Request) -> Response:
        assert request.route_values is not None, "Expects a route pattern with star *"
        tail = unquote(request.route_values.get("tail", "")).lstrip("/")

        try:
            return get_response_for_resource_path(
                request,
                tail,
                files_list_html,
                source_folder_name,
                files_handler,
                source_folder_full_path,
                discovery,
                cache_time,
                extensions,
                root_path,
                index_document,
            )
        except NotFound:
            if fallback_document is None:
                return Response(404)

            return get_response_for_resource_path(
                request,
                fallback_document,
                files_list_html,
                source_folder_name,
                files_handler,
                source_folder_full_path,
                discovery,
                cache_time,
                extensions,
                root_path,
                None,
            )

    return static_files_handler
コード例 #8
0
def get_files_handler(source_folder_name, source_folder_full_path, discovery,
                      cache_time, extensions):
    files_list_template = get_resource_file_content(
        'fileslist.html') if discovery else None
    source_folder_full_path = source_folder_full_path.lower()

    async def file_getter(request):
        tail = unquote(request.route_values.get('tail')).lstrip('/')

        resource_path = os.path.join(source_folder_name, tail)

        if '../' in tail:
            abs_path = os.path.abspath(resource_path)
            if not str(abs_path).lower().startswith(source_folder_full_path):
                # someone is trying to read out of the static folder
                raise NotFound()

        if not os.path.exists(resource_path) or os.path.islink(resource_path):
            raise NotFound()

        if os.path.isdir(resource_path):
            if discovery:
                return get_files_list_html_response(
                    files_list_template, tail.rstrip('/'),
                    list(get_files_to_serve(resource_path.rstrip('/'))))
            else:
                raise NotFound()

        file_extension = get_file_extension_from_name(resource_path)

        if file_extension not in extensions:
            raise NotFound()

        return get_response_for_file(request, resource_path, cache_time)

    return file_getter
コード例 #9
0
ファイル: common.py プロジェクト: vinayakkorti/BlackSheep
 def get_openapi_ui_html(self) -> str:
     """
     Returns the HTML response to serve the Swagger UI.
     """
     return get_resource_file_content("openapi-ui.html")