X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=phial.py;h=411c07761b02dd177e1919a665b35ccd6ead9cc1;hb=70c1ae8864769025ffec24822985a310377c69df;hp=bc07b5fe77039069621ddcda9030f5945ffc7231;hpb=f4de177de239a52c4780ca49a03935e36e262399;p=fwx diff --git a/phial.py b/phial.py index bc07b5f..411c077 100644 --- a/phial.py +++ b/phial.py @@ -1,13 +1,89 @@ import collections +import http.cookies import json +import urllib.parse -Request = collections.namedtuple( +_Request = collections.namedtuple( 'Request', ( - 'environ', + 'env', + 'GET', + 'accept', + 'accept_encoding', + 'accept_language', + 'content', + 'content_length', + 'content_type', + 'cookie', + 'method', + 'path', + 'parameters', + 'query', + 'user_agent', ) ) +class Request(_Request): + def __new__(cls, env): + errors = [] + + accept = env.get('HTTP_ACCEPT') + accept_encoding = env.get('HTTP_ACCEPT_ENCODING') + accept_language = env.get('HTTP_ACCEPT_LANGUAGE') + content = env.get('CONTENT', '') + content_type = env.get('CONTENT_TYPE') + method = env.get('REQUEST_METHOD') + path = env.get('PATH_INFO') + query = env.get('QUERY_STRING') + user_agent = env.get('HTTP_USER_AGENT') + + content_length = env.get('CONTENT_LENGTH') + + if content_length == '' or content_length is None: + content_length = 0 + else: + try: + content_length = int(content_length) + except ValueError: + errors.append('Unable to parse Content-Length "{}"'.format(content_length)) + content_length = 0 + + try: + cookie = http.cookies.SimpleCookie(env.get('HTTP_COOKIE')) + except: + cookie = http.cookies.SimpleCookie() + + + try: + GET = urllib.parse.parse_qs(query) + except: + GET = {} + errors.append('Unable to parse GET parameters from query string "{}"'.format(query)) + + if method == 'GET': + parameters = GET + + result = super().__new__( + cls, + env=env, + GET=GET, + accept=accept, + accept_encoding=accept_encoding, + accept_language=accept_language, + content = content, + content_length = content_length, + content_type = content_type, + cookie=cookie, + method=method, + parameters=parameters, + path=path, + query=query, + user_agent=user_agent, + ) + + result.subpath = path + return result + _Response = collections.namedtuple( 'Response', ( @@ -115,6 +191,50 @@ class RedirectResponse(_RedirectResponse): def content(self): return (b'',) +REQUEST_METHODS = ( + 'GET', + 'HEAD', + 'POST', + 'PUT', + 'PATCH', + 'DELETE', + 'CONNECT', + 'OPTIONS', + 'TRACE', +) + +def default_method_not_allowed_handler(request): + return Response('') + +def default_options_handler(handlers): + def handler(request): + return Response(','.join(handlers.keys())) + return handler + +def route_on_method(**kwargs): + handlers = {} + for method in REQUEST_METHODS: + if method in kwargs: + handlers[method] = kwargs.pop(method) + + method_not_allowed_handler = kwargs.pop( + 'method_not_allowed', + default_method_not_allowed_handler, + ) + + assert len(kwargs) == 0 + + if 'OPTIONS' not in handlers: + handlers['OPTIONS'] = default_options_handler(handlers) + + def handler(request): + return handlers.get( + request.method.upper(), + method_not_allowed_handler, + )(request) + + return handler + def _get_status(response): return { 200: '200 OK', @@ -137,8 +257,8 @@ def _get_content(response): return content def App(handler): - def app(environ, start_fn): - response = handler(Request(environ)) + def app(env, start_fn): + response = handler(Request(env)) start_fn(_get_status(response), _get_headers(response)) return _get_content(response)