Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle paths with pathlib #743

Merged
merged 1 commit into from
Jan 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
from urllib.parse import quote, urlencode
from collections import namedtuple
from pathlib import Path

from . import hdrs, multidict
from .errors import InvalidURL
Expand Down Expand Up @@ -204,7 +205,7 @@ def str_to_bytes(s, encoding='utf-8'):
def guess_filename(obj, default=None):
name = getattr(obj, 'name', None)
if name and name[0] != '<' and name[-1] != '>':
return os.path.split(name)[-1]
return Path(name).name
return default


Expand Down
3 changes: 2 additions & 1 deletion aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import zlib
from urllib.parse import quote, unquote, urlencode, parse_qsl
from collections import deque, Mapping, Sequence
from pathlib import Path

from .helpers import parse_mimetype
from .multidict import CIMultiDict
Expand Down Expand Up @@ -652,7 +653,7 @@ def _guess_filename(self, obj):
if isinstance(obj, io.IOBase):
name = getattr(obj, 'name', None)
if name is not None:
return os.path.basename(name)
return Path(name).name

def serialize(self):
"""Yields byte chunks for body part."""
Expand Down
40 changes: 26 additions & 14 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import inspect

from collections.abc import Sized, Iterable, Container
from pathlib import Path
from urllib.parse import urlencode, unquote
from types import MappingProxyType

Expand Down Expand Up @@ -159,14 +160,17 @@ def __init__(self, name, prefix, directory, *,
'GET', self.handle, name, expect_handler=expect_handler)
self._prefix = prefix
self._prefix_len = len(self._prefix)
self._directory = os.path.abspath(directory) + os.sep
try:
directory = Path(directory).resolve()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would accept both Path and str here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Path class already accept both!

    >>> Path('foo') == Path(Path('foo'))
    >>> True

if not directory.is_dir():
raise ValueError('Not a directory')
except (FileNotFoundError, ValueError) as error:
raise ValueError(
"No directory exists at '{}'".format(directory)) from error
self._directory = directory
self._chunk_size = chunk_size
self._response_factory = response_factory

if not os.path.isdir(self._directory):
raise ValueError(
"No directory exists at '{}'".format(self._directory))

if bool(os.environ.get("AIOHTTP_NOSENDFILE")):
self._sendfile = self._sendfile_fallback

Expand All @@ -176,6 +180,8 @@ def match(self, path):
return {'filename': path[self._prefix_len:]}

def url(self, *, filename, query=None):
if isinstance(filename, Path):
filename = str(getattr(filename, 'path', filename))
while filename.startswith('/'):
filename = filename[1:]
url = self._prefix + filename
Expand Down Expand Up @@ -266,19 +272,25 @@ def _sendfile_fallback(self, req, resp, fobj, count):
@asyncio.coroutine
def handle(self, request):
filename = request.match_info['filename']
filepath = os.path.abspath(os.path.join(self._directory, filename))
if not filepath.startswith(self._directory):
raise HTTPNotFound()
if not os.path.exists(filepath) or not os.path.isfile(filepath):
raise HTTPNotFound()

st = os.stat(filepath)
try:
filepath = self._directory.joinpath(filename).resolve()
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.logger.exception(error)
raise HTTPNotFound() from error

st = filepath.stat()

modsince = request.if_modified_since
if modsince is not None and st.st_mtime <= modsince.timestamp():
raise HTTPNotModified()

ct, encoding = mimetypes.guess_type(filepath)
path = str(getattr(filepath, 'path', filename))
ct, encoding = mimetypes.guess_type(path)
if not ct:
ct = 'application/octet-stream'

Expand All @@ -295,7 +307,7 @@ def handle(self, request):
try:
yield from resp.prepare(request)

with open(filepath, 'rb') as f:
with filepath.open('rb') as f:
yield from self._sendfile(request, resp, f, file_size)

finally:
Expand Down