示例#1
0
async def static_handler(request):
    # modified from aiohttp_web_urldispatcher.StaticResource_handle
    request_path = request.match_info['path'].lstrip('/')

    directory = request.app['static_dir']
    if request_path == '':
        return FileResponse(directory / 'index.html')

    try:
        filename = Path(request_path)
        if filename.anchor:
            # request_path is an absolute name like
            # /static/\\machine_name\c$ or /static/D:\path
            # where the static dir is totally different
            raise HTTPNotFound()
        filepath = directory.joinpath(filename).resolve()
        filepath.relative_to(directory)
    except HTTPForbidden:
        raise
    except Exception as exc:
        # perm error or other kind!
        logger.warning('error resolving path %r', request_path, exc_info=True)
        raise HTTPNotFound() from exc

    if request_path.startswith('iframes/') and request_path.endswith('.html'):
        new_root = request_root(request)
        content = filepath.read_text().replace('http://localhost:3000',
                                               new_root)
        return Response(text=content, content_type='text/html')
    elif filepath.is_file():
        return FileResponse(filepath)
    else:
        return FileResponse(directory / 'index.html')
    def _handle(self, request):
        filename = unquote(request.match_info['filename'])
        try:
            filepath = self._directory.joinpath(filename).resolve()
            if not self._follow_symlinks:
                filepath.relative_to(self._directory)
        except (ValueError, FileNotFoundError) as error:
            # relatively safe
            raise HTTPNotFound() from error
        except Exception as error:
            # perm error or other kind!
            request.app.logger.exception(error)
            raise HTTPNotFound() from error

        # on opening a dir, load it's contents if allowed
        if filepath.is_dir():
            if self._show_index:
                try:
                    ret = Response(text=self._directory_as_html(filepath),
                                   content_type="text/html")
                except PermissionError:
                    raise HTTPForbidden()
            else:
                raise HTTPForbidden()
        elif filepath.is_file():
            ret = FileResponse(filepath, chunk_size=self._chunk_size)
        else:
            raise HTTPNotFound

        return ret
示例#3
0
 async def index(self, request: web.Request) -> web.Response:
     path = "{}/{}".format(
         os.path.dirname(
             self.context.get("context", {}).get("_service_file_path")),
         "public/index.html")
     response: web.Response = FileResponse(path=path, chunk_size=256 * 1024)
     return response
示例#4
0
    async def _handle(self, request):
        filename = unquote(request.match_info['filename'])
        filepath = None
        ret = None
        for directory in self._directories:
            try:
                filepath = directory.joinpath(filename).resolve()
                if not self._follow_symlinks:
                    filepath.relative_to(directory)
            except (ValueError, FileNotFoundError) as error:
                # relatively safe
                pass
            except Exception as error:
                # perm error or other kind!
                request.app.logger.exception(error)

            if not filepath:
                continue

            # on opening a dir, load it's contents if allowed
            if filepath.is_file():
                ret = FileResponse(filepath, chunk_size=self._chunk_size)
                if request.app['env'] == 'development':
                    ret.headers['Cache-control'] = 'no-cache'
                break
            else:
                continue
        if not ret:
            raise HTTPNotFound()
        return ret
示例#5
0
def test_using_gzip_if_header_present_and_file_available(loop) -> None:
    request = make_mocked_request(
        'GET', 'http://python.org/logo.png', headers={
            hdrs.ACCEPT_ENCODING: 'gzip'
        }
    )

    gz_filepath = mock.Mock()
    gz_filepath.open = mock.mock_open()
    gz_filepath.is_file.return_value = True
    gz_filepath.stat.return_value = mock.MagicMock()
    gz_filepath.stat.st_size = 1024

    filepath = mock.Mock()
    filepath.name = 'logo.png'
    filepath.open = mock.mock_open()
    filepath.with_name.return_value = gz_filepath

    file_sender = FileResponse(filepath)
    file_sender._sendfile = make_mocked_coro(None)

    loop.run_until_complete(file_sender.prepare(request))

    assert not filepath.open.called
    assert gz_filepath.open.called
示例#6
0
文件: static.py 项目: bakasa/nosht
async def static_handler(request):
    # modified from aiohttp_web_urldispatcher.StaticResource_handle
    request_path = request.match_info['path'].lstrip('/')

    directory = request.app['static_dir']
    csp_headers = request.app['csp_headers']
    if request_path == '':
        return FileResponse(directory / 'index.html', headers=csp_headers)
    elif request_path == 'sitemap.xml':
        raise HTTPMovedPermanently(
            location=f'https://{request.host}/api/sitemap.xml')

    try:
        filename = Path(request_path)
        if filename.anchor:  # pragma: no cover
            # windows only I think, but keep it just in case
            # request_path is an absolute name like
            # /static/\\machine_name\c$ or /static/D:\path
            # where the static dir is totally different
            raise HTTPNotFound()
        filepath = directory.joinpath(filename).resolve()
        filepath.relative_to(directory)
    except Exception as exc:
        # perm error or other kind!
        logger.warning('error resolving path %r', request_path, exc_info=True)
        raise HTTPNotFound() from exc

    is_file = filepath.is_file()
    if request_path.startswith('iframes/') and request_path.endswith(
            '.html') and is_file:
        new_root = request_root(request)
        content = filepath.read_text().replace('http://localhost:3000',
                                               new_root)
        # no csp header here, it's defined in the page as a http-equiv header
        return Response(text=content, content_type='text/html')
    elif is_file:
        return FileResponse(filepath, headers=csp_headers)
    elif request_path.startswith('pvt/'):
        return FileResponse(directory / 'index.html',
                            headers={
                                **csp_headers,
                                **{
                                    'X-Robots-Tag': 'noindex'
                                }
                            })
    else:
        return FileResponse(directory / 'index.html', headers=csp_headers)
示例#7
0
    async def get_image(self, request: Request):
        image_doc: ImageDocument = self._kernel.server_db_session.query(
            ImageDocument).filter(ImageDocument.id == int(
                request.match_info.get("image_id"))).one()

        return FileResponse(
            f"{self._kernel.game.config.folder_path}/data/images/"
            f"{image_doc.id}{image_doc.extension}")
async def download_cached_reads(request):
    name = request.match_info["name"]

    tmpdir = Path(tempfile.mkdtemp())

    file = (tmpdir / name)
    file.touch()

    return FileResponse(file)
示例#9
0
 async def index(self, request: web.Request) -> web.Response:
     path = '{}/{}'.format(
         os.path.dirname(
             self.context.get('context', {}).get('_service_file_path')),
         'public/index.html')
     response = FileResponse(
         path=path,  # type: ignore
         chunk_size=256 * 1024)  # type: web.Response
     return response
示例#10
0
    async def handle_file(self,
                          request: 'web.Request',
                          filename: str,
                          from_index=None) -> 'web.Response':
        """Handle file requests."""
        try:
            filepath = self._directory.joinpath(filename).resolve()
            if not self._follow_symlinks:
                filepath.relative_to(self._directory)
        except (ValueError, FileNotFoundError) as error:
            # relatively safe
            raise HTTPNotFound() from error
        except Exception as error:
            # perm error or other kind!
            request.app.logger.exception(error)
            raise HTTPNotFound() from error

        # on opening a dir, load it's contents if allowed
        if filepath.is_dir():
            if filename.endswith('/') or not filename:
                ret = await self.handle_file(request,
                                             filename + 'index.html',
                                             from_index=filename)
            else:
                # Redirect and add trailing slash so relative links work (Issue #3140)
                new_url = request.rel_url.path + '/'
                if request.rel_url.query_string:
                    new_url += '?' + request.rel_url.query_string
                raise HTTPMovedPermanently(new_url)
        elif filepath.is_file():
            ct, encoding = mimetypes.guess_type(str(filepath))
            encoding = encoding or 'utf-8'
            if ct == 'text/html' and self.modify_html:
                if sys.version_info[0] == 3 and sys.version_info[1] <= 5:
                    # Python 3.4 and 3.5 do not accept pathlib.Path objects in calls to open()
                    filepath = str(filepath)
                with open(filepath, 'r', encoding=encoding) as fh:
                    text = fh.read()
                    text = self.transform_html(text)
                    ret = Response(text=text,
                                   content_type=ct,
                                   charset=encoding)
            else:
                ret = FileResponse(filepath, chunk_size=self._chunk_size)
        elif from_index:
            filepath = self._directory.joinpath(from_index).resolve()
            try:
                return Response(text=self._directory_as_html(filepath),
                                content_type="text/html")
            except PermissionError:
                raise HTTPForbidden
        else:
            raise HTTPNotFound

        return ret
async def download_reads_file(request):
    sample_id = request.match_info["sample_id"]
    filename = request.match_info["filename"]

    if sample_id != TEST_SAMPLE_ID or filename not in ("reads_1.fq.gz",
                                                       "reads_2.fq.gz"):
        return not_found()

    file_name = "paired_small_1.fq.gz" if filename == "reads_1.fq.gz" else "paired_small_2.fq.gz"

    return FileResponse(ANALYSIS_TEST_FILES_DIR / file_name)
async def download_annotations(request):
    annotations_path = ANALYSIS_TEST_FILES_DIR / "annotations.json"
    compressed_annotations_path = annotations_path.with_suffix(".json.gz")

    if not compressed_annotations_path.exists():
        with annotations_path.open("w") as f:
            json.dump([MOCK_HMM] * 10, f)

        virtool_core.utils.compress_file(annotations_path,
                                         compressed_annotations_path)

    return FileResponse(annotations_path)
示例#13
0
        async def handler(request: web.Request) -> web.Response:
            result = compiled_pattern.match(request.path)
            filename = result.groupdict()['filename']
            filepath = '{}{}'.format(path, filename)

            try:
                if os.path.isdir(filepath) or not os.path.exists(filepath):
                    raise web.HTTPNotFound()

                pathlib.Path(filepath).open('r')
                return FileResponse(filepath)
            except PermissionError as e:
                raise web.HTTPForbidden()
async def download_artifact(request):
    sample_id = request.match_info["sample_id"]
    filename = request.match_info["filename"]

    if sample_id != TEST_SAMPLE_ID:
        return not_found()

    tempdir = Path(tempfile.mkdtemp())

    file = tempdir / filename
    file.touch()

    return FileResponse(file)
示例#15
0
    async def _handle(self, request: Request) -> StreamResponse:
        rel_url = request.match_info['filename']
        try:
            filename = Path(rel_url)
            if filename.anchor:
                # rel_url is an absolute name like
                # /static/\\machine_name\c$ or /static/D:\path
                # where the static dir is totally different
                raise HTTPForbidden()

            # alternative directories is the first place for searching
            alt = False
            directory = self._directory
            if self.canonical in self._directory_alt:
                for v in self._directory_alt[self.canonical]:
                    filepath = v.joinpath(filename).resolve()
                    if filepath.exists():
                        directory = v
                        alt = True
                        break

            if not alt:
                filepath = directory.joinpath(filename).resolve()
            if not self._follow_symlinks:
                filepath.relative_to(directory)

        except (ValueError, FileNotFoundError) as error:
            # relatively safe
            raise HTTPNotFound() from error
        except HTTPForbidden:
            raise
        except Exception as error:
            # perm error or other kind!
            request.app.logger.exception(error)
            raise HTTPNotFound() from error

        # on opening a dir, load its contents if allowed
        if filepath.is_dir():
            if self._show_index:
                try:
                    return Response(text=self._directory_as_html(filepath),
                                    content_type="text/html")
                except PermissionError:
                    raise HTTPForbidden()
            else:
                raise HTTPForbidden()
        elif filepath.is_file():
            return FileResponse(filepath, chunk_size=self._chunk_size)
        else:
            raise HTTPNotFound
示例#16
0
async def spa_static_handler(request):
    """
    Handler suitable for use with single page apps.

    Use with web.get(r'/{path:.*}', spa_static_handler, name='static')

    modified from aiohttp_web_urldispatcher.StaticResource_handle
    """
    request_path = request.match_info['path'].lstrip('/')

    directory = request.app['static_dir']
    csp_headers = request.app.get('static_headers') or {}
    if request_path == '':
        return FileResponse(directory / 'index.html', headers=csp_headers)

    # probably other paths to return 404 for?
    if request_path.startswith('.well-known/'):
        raise HTTPNotFound()

    try:
        filename = Path(request_path)
        if filename.anchor:  # pragma: no cover
            # shouldn't happen on linux, but keep it just in case
            # request_path is an absolute name like
            # /static/\\machine_name\c$ or /static/D:\path
            # where the static dir is totally different
            raise RuntimeError('request path has anchor')
        filepath = directory.joinpath(filename).resolve()
        filepath.relative_to(directory)
    except Exception:  # pragma: no cover
        logger.warning('error resolving path %r', request_path, exc_info=True)
        filepath = directory

    if filepath.is_file():
        return FileResponse(filepath, headers=csp_headers)
    else:
        return FileResponse(directory / 'index.html', headers=csp_headers)
示例#17
0
        async def handler(request: web.Request) -> web.Response:
            result = compiled_pattern.match(request.path)
            filename = result.groupdict()['filename'] if result else ''
            filepath = '{}{}'.format(path, filename)

            try:
                if os.path.commonprefix((os.path.realpath(filepath), os.path.realpath(path))) != os.path.realpath(path) or os.path.isdir(filepath) or not os.path.exists(filepath):
                    raise web.HTTPNotFound()  # type: ignore

                pathlib.Path(filepath).open('r')

                response = FileResponse(path=filepath,  # type: ignore
                                        chunk_size=256 * 1024)  # type: web.Response
                return response
            except PermissionError as e:
                raise web.HTTPForbidden()  # type: ignore
示例#18
0
async def get_file(request: web.Request) -> FileResponse:
    """For worker to pull file for calculating file hash task

    request headers:

    * X-DISTRIBUTED-WORKER-NAME: worker's name, worker should been registered
                                 already. required

    responses:

    * 200 OK: file response
    * 202 Accepted: wait for processing files timeout and make request again
    * 204 No Content: no more files
    * 400 Bad Request: missing worker name header
    * 400 Bad Request: worker not registered yet
    """
    master = request.app["master"]

    worker_name = _get_worker_name(request)
    _ensure_worker_registered(master, worker_name)

    try:
        file_path = master.files_queue.get_nowait()
    except asyncio.QueueEmpty:
        # try to get timeout task
        timeout_file = master.get_earlist_timeout_file()
        if timeout_file:
            _worker_name, file_path, _ = timeout_file
            # remove old processing file
            master.remove_current_processing_file(_worker_name, file_path)
        else:
            # try to find out any processing files
            if not master.has_processing_files():
                # no more files
                raise web.HTTPNoContent()
            else:
                # tell workers to wait until processing files timeout
                raise web.HTTPAccepted(
                    text=("wait for processing files timeout and make request"
                          " again"))

    master.new_processing_file(worker_name, file_path)
    master.files_queue.task_done()

    full_path = master.home.get_full_path(file_path)
    headers = {constants.HTTP_HEADER_X_FILE_PATH: file_path}
    return FileResponse(full_path, headers=headers)
示例#19
0
def test_status_controlled_by_user(loop: Any) -> None:
    request = make_mocked_request("GET",
                                  "http://python.org/logo.png",
                                  headers={})

    filepath = mock.Mock()
    filepath.name = "logo.png"
    filepath.open = mock.mock_open()
    filepath.stat.return_value = mock.MagicMock()
    filepath.stat.st_size = 1024

    file_sender = FileResponse(filepath, status=203)
    file_sender._sendfile = make_mocked_coro(None)  # type: ignore[assignment]

    loop.run_until_complete(file_sender.prepare(request))

    assert file_sender._status == 203
示例#20
0
def test_status_controlled_by_user(loop) -> None:
    request = make_mocked_request('GET',
                                  'http://python.org/logo.png',
                                  headers={})

    filepath = mock.Mock()
    filepath.name = 'logo.png'
    filepath.open = mock.mock_open()
    filepath.stat.return_value = mock.MagicMock()
    filepath.stat.st_size = 1024

    file_sender = FileResponse(filepath, status=203)
    file_sender._sendfile = make_mocked_coro(None)

    loop.run_until_complete(file_sender.prepare(request))

    assert file_sender._status == 203
def function1075(arg449):
    var911 = make_mocked_request('GET',
                                 'http://python.org/logo.png',
                                 headers={})
    var2505 = mock.Mock()
    var2505.open = mock.mock_open()
    var2505.is_file.return_value = True
    var2750 = mock.Mock()
    var2750.name = 'logo.png'
    var2750.open = mock.mock_open()
    var2750.with_name.return_value = var2505
    var2750.stat.return_value = mock.MagicMock()
    var2750.stat.st_size = 1024
    var2254 = FileResponse(var2750)
    var2254._sendfile = make_mocked_coro(None)
    arg449.run_until_complete(var2254.prepare(var911))
    assert var2750.open.called
    assert (not var2505.open.called)
def function2154(arg2122):
    var3452 = make_mocked_request('GET',
                                  'http://python.org/logo.png',
                                  headers={})
    var4044 = mock.Mock()
    var4044.open = mock.mock_open()
    var4044.is_file.return_value = False
    var1683 = mock.Mock()
    var1683.name = 'logo.png'
    var1683.open = mock.mock_open()
    var1683.with_name.return_value = var4044
    var1683.stat.return_value = mock.MagicMock()
    var1683.stat.st_size = 1024
    var4675 = FileResponse(var1683)
    var4675._sendfile = make_mocked_coro(None)
    arg2122.run_until_complete(var4675.prepare(var3452))
    assert var1683.open.called
    assert (not var4044.open.called)
def function2349(arg1111):
    var2428 = make_mocked_request('GET',
                                  'http://python.org/logo.png',
                                  headers={
                                      hdrs.ACCEPT_ENCODING: 'gzip',
                                  })
    var789 = mock.Mock()
    var789.open = mock.mock_open()
    var789.is_file.return_value = True
    var789.stat.return_value = mock.MagicMock()
    var789.stat.st_size = 1024
    var2300 = mock.Mock()
    var2300.name = 'logo.png'
    var2300.open = mock.mock_open()
    var2300.with_name.return_value = var789
    var3618 = FileResponse(var2300)
    var3618._sendfile = make_mocked_coro(None)
    arg1111.run_until_complete(var3618.prepare(var2428))
    assert (not var2300.open.called)
    assert var789.open.called
def function182(arg2286):
    var2392 = make_mocked_request('GET',
                                  'http://python.org/logo.png',
                                  headers={
                                      hdrs.ACCEPT_ENCODING: 'gzip',
                                  })
    var4676 = mock.Mock()
    var4676.open = mock.mock_open()
    var4676.is_file.return_value = False
    var3011 = mock.Mock()
    var3011.name = 'logo.png'
    var3011.open = mock.mock_open()
    var3011.with_name.return_value = var4676
    var3011.stat.return_value = mock.MagicMock()
    var3011.stat.st_size = 1024
    var410 = FileResponse(var3011)
    var410._sendfile = make_mocked_coro(None)
    arg2286.run_until_complete(var410.prepare(var2392))
    assert var3011.open.called
    assert (not var4676.open.called)
示例#25
0
async def static(request):
    request_path = request.match_info['path'].lstrip('/')
    directory: Path = request.app['output_dir'].resolve()
    if request_path == '':
        filepath = directory / 'index.html'
    else:
        try:
            filepath = (directory / request_path).resolve()
            filepath.relative_to(directory)
        except Exception as exc:
            # perm error or other kind!
            raise HTTPNotFound() from exc
    for _ in range(20):
        if filepath.exists():
            break
        await asyncio.sleep(0.1)

    if filepath.is_file():
        return FileResponse(filepath)
    else:
        raise HTTPNotFound()
示例#26
0
    async def get_static(self, request: Request) -> FileResponse:
        """
        Load static content for OpenAPI UI.
        """
        file_name = request.match_info['filename']

        # Check file exists and is a known content type.
        content_type = {
            'ss': 'text/css',
            'js': 'application/javascript',
            'ng': 'image/png',
        }.get(file_name[-2:])
        file_path = self.static_path / file_name
        if not (content_type and file_path.exists()):
            raise HttpError(HTTPStatus.NOT_FOUND, 42)

        return FileResponse(file_path, headers={
            'Content-Type': content_type,
            'Content-Encoding': 'gzip',  # Content is pre-gzipped
            'Cache-Control': 'public, max-age=300',
        })
示例#27
0
def test_gzip_if_header_not_present_and_file_available(loop) -> None:
    request = make_mocked_request("GET",
                                  "http://python.org/logo.png",
                                  headers={})

    gz_filepath = mock.Mock()
    gz_filepath.open = mock.mock_open()
    gz_filepath.is_file.return_value = True

    filepath = mock.Mock()
    filepath.name = "logo.png"
    filepath.open = mock.mock_open()
    filepath.with_name.return_value = gz_filepath
    filepath.stat.return_value = mock.MagicMock()
    filepath.stat.st_size = 1024

    file_sender = FileResponse(filepath)
    file_sender._sendfile = make_mocked_coro(None)

    loop.run_until_complete(file_sender.prepare(request))

    assert filepath.open.called
    assert not gz_filepath.open.called
示例#28
0
def test_gzip_if_header_present_and_file_not_available(loop: Any) -> None:
    request = make_mocked_request("GET",
                                  "http://python.org/logo.png",
                                  headers={hdrs.ACCEPT_ENCODING: "gzip"})

    gz_filepath = mock.Mock()
    gz_filepath.open = mock.mock_open()
    gz_filepath.is_file.return_value = False

    filepath = mock.Mock()
    filepath.name = "logo.png"
    filepath.open = mock.mock_open()
    filepath.with_name.return_value = gz_filepath
    filepath.stat.return_value = mock.MagicMock()
    filepath.stat.st_size = 1024

    file_sender = FileResponse(filepath)
    file_sender._sendfile = make_mocked_coro(None)  # type: ignore[assignment]

    loop.run_until_complete(file_sender.prepare(request))

    assert filepath.open.called
    assert not gz_filepath.open.called
示例#29
0
 async def serve_livereload_js(self, request):
     """Handle requests to /livereload.js and serve the JS file."""
     return FileResponse(LRJS_PATH)
async def download_hmm_profiles(request):
    return FileResponse(HMM_PROFILES)