示例#1
0
def test_using_gzip_if_header_present_and_file_available(loop):
    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
示例#2
0
def test_gzip_if_header_not_present_and_file_not_available(loop: Any) -> 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 = 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
示例#3
0
文件: __init__.py 项目: thuvh/nikola
    def handle_file(self, request, filename, from_index=None):
        """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 = yield from self.handle_file(request,
                                                  filename + 'index.html',
                                                  from_index=filename)
            else:
                ret = yield from self.handle_file(request,
                                                  filename + '/index.html',
                                                  from_index=filename)
        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
示例#4
0
def test_gzip_if_header_not_present_and_file_available(loop):
    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
示例#5
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
示例#6
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
示例#7
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)
示例#8
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
示例#9
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)
示例#10
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()
示例#11
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',
        })
示例#12
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)