1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
__all__ = ('UrlDispatcher', 'UrlMappingMatchInfo',
'Route', 'PlainRoute', 'DynamicRoute', 'StaticRoute')
import abc
import asyncio
import collections
import mimetypes
import re
import os
import inspect
from urllib.parse import urlencode, unquote
from . import hdrs
from .abc import AbstractRouter, AbstractMatchInfo
from .protocol import HttpVersion11
from .web_exceptions import HTTPMethodNotAllowed, HTTPNotFound, HTTPNotModified
from .web_reqrep import StreamResponse
from .multidict import upstr
class UrlMappingMatchInfo(dict, AbstractMatchInfo):
def __init__(self, match_dict, route):
super().__init__(match_dict)
self._route = route
@property
def handler(self):
return self._route.handler
@property
def route(self):
return self._route
def __repr__(self):
return "<MatchInfo {}: {}>".format(super().__repr__(), self._route)
@asyncio.coroutine
def _defaultExpectHandler(request):
"""Default handler for Except: 100-continue"""
if request.version == HttpVersion11:
request.transport.write(b"HTTP/1.1 100 Continue\r\n\r\n")
class Route(metaclass=abc.ABCMeta):
def __init__(self, method, handler, name, *, expect_handler=None):
if expect_handler is None:
expect_handler = _defaultExpectHandler
assert asyncio.iscoroutinefunction(expect_handler), \
'Coroutine is expected, got {!r}'.format(expect_handler)
self._method = method
self._handler = handler
self._name = name
self._expect_handler = expect_handler
@property
def method(self):
return self._method
@property
def handler(self):
return self._handler
@property
def name(self):
return self._name
@abc.abstractmethod # pragma: no branch
def match(self, path):
"""Return dict with info for given path or
None if route cannot process path."""
@abc.abstractmethod # pragma: no branch
def url(self, **kwargs):
"""Construct url for route with additional params."""
@asyncio.coroutine
def handle_expect_header(self, request):
return (yield from self._expect_handler(request))
@staticmethod
def _append_query(url, query):
if query is not None:
return url + "?" + urlencode(query)
else:
return url
class PlainRoute(Route):
def __init__(self, method, handler, name, path, *, expect_handler=None):
super().__init__(method, handler, name, expect_handler=expect_handler)
self._path = path
def match(self, path):
# string comparison is about 10 times faster than regexp matching
if self._path == path:
return {}
else:
return None
def url(self, *, query=None):
return self._append_query(self._path, query)
def __repr__(self):
name = "'" + self.name + "' " if self.name is not None else ""
return "<PlainRoute {name}[{method}] {path} -> {handler!r}".format(
name=name, method=self.method, path=self._path,
handler=self.handler)
class DynamicRoute(Route):
def __init__(self, method, handler, name, pattern, formatter, *,
expect_handler=None):
super().__init__(method, handler, name, expect_handler=expect_handler)
self._pattern = pattern
self._formatter = formatter
def match(self, path):
match = self._pattern.match(path)
if match is None:
return None
else:
return match.groupdict()
def url(self, *, parts, query=None):
url = self._formatter.format_map(parts)
return self._append_query(url, query)
def __repr__(self):
name = "'" + self.name + "' " if self.name is not None else ""
return ("<DynamicRoute {name}[{method}] {formatter} -> {handler!r}"
.format(name=name, method=self.method,
formatter=self._formatter, handler=self.handler))
class StaticRoute(Route):
def __init__(self, name, prefix, directory, *,
expect_handler=None, chunk_size=256*1024,
response_factory=None):
assert prefix.startswith('/'), prefix
assert prefix.endswith('/'), prefix
super().__init__(
'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
self._chunk_size = chunk_size
if response_factory is None:
self._response_factory = StreamResponse
else:
self._response_factory = response_factory
if not os.path.isdir(self._directory):
raise ValueError(
"No directory exists at '{}'".format(self._directory))
def match(self, path):
if not path.startswith(self._prefix):
return None
return {'filename': path[self._prefix_len:]}
def url(self, *, filename, query=None):
while filename.startswith('/'):
filename = filename[1:]
url = self._prefix + filename
return self._append_query(url, query)
@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)
modsince = request.if_modified_since
if modsince is not None and st.st_mtime <= modsince.timestamp():
raise HTTPNotModified()
ct, encoding = mimetypes.guess_type(filepath)
if not ct:
ct = 'application/octet-stream'
resp = self._response_factory()
resp.content_type = ct
if encoding:
resp.headers[hdrs.CONTENT_ENCODING] = encoding
resp.last_modified = st.st_mtime
file_size = st.st_size
single_chunk = file_size < self._chunk_size
if single_chunk:
resp.content_length = file_size
resp.start(request)
with open(filepath, 'rb') as f:
chunk = f.read(self._chunk_size)
if single_chunk:
resp.write(chunk)
else:
while chunk:
resp.write(chunk)
chunk = f.read(self._chunk_size)
return resp
def __repr__(self):
name = "'" + self.name + "' " if self.name is not None else ""
return "<StaticRoute {name}[{method}] {path} -> {directory!r}".format(
name=name, method=self.method, path=self._prefix,
directory=self._directory)
class SystemRoute(Route):
def __init__(self, status, reason):
super().__init__(hdrs.METH_ANY, None, None)
self._status = status
self._reason = reason
def url(self, **kwargs):
raise RuntimeError(".url() is not allowed for SystemRoute")
def match(self, path):
return None
@property
def status(self):
return self._status
@property
def reason(self):
return self._reason
def __repr__(self):
return "<SystemRoute {status}: {reason}>".format(status=self._status,
reason=self._reason)
class _NotFoundMatchInfo(UrlMappingMatchInfo):
route = SystemRoute(404, 'Not Found')
def __init__(self):
super().__init__({}, None)
@property
def handler(self):
return self._not_found
@asyncio.coroutine
def _not_found(self, request):
raise HTTPNotFound()
def __repr__(self):
return "<MatchInfo: not found>"
class _MethodNotAllowedMatchInfo(UrlMappingMatchInfo):
route = SystemRoute(405, 'Method Not Allowed')
def __init__(self, method, allowed_methods):
super().__init__({}, None)
self._method = method
self._allowed_methods = allowed_methods
@property
def handler(self):
return self._not_allowed
@asyncio.coroutine
def _not_allowed(self, request):
raise HTTPMethodNotAllowed(self._method, self._allowed_methods)
def __repr__(self):
return ("<MatchInfo: method {} is not allowed (allowed methods: {}>"
.format(self._method,
', '.join(sorted(self._allowed_methods))))
class UrlDispatcher(AbstractRouter, collections.abc.Mapping):
DYN = re.compile(r'^\{(?P<var>[a-zA-Z][_a-zA-Z0-9]*)\}$')
DYN_WITH_RE = re.compile(
r'^\{(?P<var>[a-zA-Z][_a-zA-Z0-9]*):(?P<re>.+)\}$')
GOOD = r'[^{}/]+'
ROUTE_RE = re.compile(r'(\{[_a-zA-Z][^{}]*(?:\{[^{}]*\}[^{}]*)*\})')
METHODS = {hdrs.METH_ANY, hdrs.METH_POST,
hdrs.METH_GET, hdrs.METH_PUT, hdrs.METH_DELETE,
hdrs.METH_PATCH, hdrs.METH_HEAD, hdrs.METH_OPTIONS}
def __init__(self):
super().__init__()
self._urls = []
self._routes = {}
@asyncio.coroutine
def resolve(self, request):
path = request.raw_path
method = request.method
allowed_methods = set()
for route in self._urls:
match_dict = route.match(path)
if match_dict is None:
continue
route_method = route.method
if route_method == method or route_method == hdrs.METH_ANY:
# Unquote separate matching parts
match_dict = {key: unquote(value) for key, value in
match_dict.items()}
return UrlMappingMatchInfo(match_dict, route)
allowed_methods.add(route_method)
else:
if allowed_methods:
return _MethodNotAllowedMatchInfo(method, allowed_methods)
else:
return _NotFoundMatchInfo()
def __iter__(self):
return iter(self._routes)
def __len__(self):
return len(self._routes)
def __contains__(self, name):
return name in self._routes
def __getitem__(self, name):
return self._routes[name]
def register_route(self, route):
assert isinstance(route, Route), 'Instance of Route class is required.'
name = route.name
if name is not None:
if name in self._routes:
raise ValueError('Duplicate {!r}, '
'already handled by {!r}'
.format(name, self._routes[name]))
else:
self._routes[name] = route
self._urls.append(route)
def add_route(self, method, path, handler,
*, name=None, expect_handler=None):
if not path.startswith('/'):
raise ValueError("path should be started with /")
assert callable(handler), handler
if (not asyncio.iscoroutinefunction(handler) and
not inspect.isgeneratorfunction(handler)):
handler = asyncio.coroutine(handler)
method = upstr(method)
if method not in self.METHODS:
raise ValueError("{} is not allowed HTTP method".format(method))
if not ('{' in path or '}' in path or self.ROUTE_RE.search(path)):
route = PlainRoute(
method, handler, name, path, expect_handler=expect_handler)
self.register_route(route)
return route
pattern = ''
formatter = ''
for part in self.ROUTE_RE.split(path):
match = self.DYN.match(part)
if match:
pattern += '(?P<{}>{})'.format(match.group('var'), self.GOOD)
formatter += '{' + match.group('var') + '}'
continue
match = self.DYN_WITH_RE.match(part)
if match:
pattern += '(?P<{var}>{re})'.format(**match.groupdict())
formatter += '{' + match.group('var') + '}'
continue
if '{' in part or '}' in part:
raise ValueError("Invalid path '{}'['{}']".format(path, part))
formatter += part
pattern += re.escape(part)
try:
compiled = re.compile('^' + pattern + '$')
except re.error as exc:
raise ValueError(
"Bad pattern '{}': {}".format(pattern, exc)) from None
route = DynamicRoute(
method, handler, name, compiled,
formatter, expect_handler=expect_handler)
self.register_route(route)
return route
def add_static(self, prefix, path, *, name=None, expect_handler=None,
chunk_size=256*1024, response_factory=None):
"""
Adds static files view
:param prefix - url prefix
:param path - folder with files
"""
assert prefix.startswith('/')
if not prefix.endswith('/'):
prefix += '/'
route = StaticRoute(name, prefix, path,
expect_handler=expect_handler,
chunk_size=chunk_size,
response_factory=response_factory)
self.register_route(route)
return route
|