X-Git-Url: https://code.kerkeslager.com/?p=fwx;a=blobdiff_plain;f=phial.py;fp=phial.py;h=5388c050b437338d850a84c95e8052db7a0376cb;hp=bc07b5fe77039069621ddcda9030f5945ffc7231;hb=067d5b39e8fb4e7fb9587901d55e94b7c4c1ce69;hpb=f4de177de239a52c4780ca49a03935e36e262399 diff --git a/phial.py b/phial.py index bc07b5f..5388c05 100644 --- a/phial.py +++ b/phial.py @@ -1,13 +1,67 @@ import collections 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', + 'cookies', + 'method', + 'path', + 'parameters', + 'query', + 'user_agent', ) ) +class Request(_Request): + def __new__(cls, env): + accept = env.get('HTTP_ACCEPT') + accept_encoding = env.get('HTTP_ACCEPT_ENCODING') + accept_language = env.get('HTTP_ACCEPT_LANGUAGE') + content = env.get('CONTENT', '') + content_length = env.get('CONTENT_LENGTH') + content_type = env.get('CONTENT_TYPE') + cookies = env.get('HTTP_COOKIE') + method = env.get('REQUEST_METHOD') + path = env.get('PATH_INFO') + query = env.get('QUERY_STRING') + user_agent = env.get('HTTP_USER_AGENT') + + GET = urllib.parse.parse_qs(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, + cookies=cookies, + method=method, + parameters=parameters, + path=path, + query=query, + user_agent=user_agent, + ) + + result.subpath = path + return result + _Response = collections.namedtuple( 'Response', ( @@ -137,8 +191,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)