From: David Kerkeslager Date: Wed, 23 Oct 2019 03:04:04 +0000 (-0400) Subject: Start providing some niceties on Request X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;h=faa593f592bd8792ccfa0e5a583e2290013456c8;p=fwx Start providing some niceties on Request --- diff --git a/phial.py b/phial.py index 5388c05..081f262 100644 --- a/phial.py +++ b/phial.py @@ -1,4 +1,5 @@ import collections +import http.cookie import json import urllib.parse @@ -13,7 +14,7 @@ _Request = collections.namedtuple( 'content', 'content_length', 'content_type', - 'cookies', + 'cookie', 'method', 'path', 'parameters', @@ -24,19 +25,40 @@ _Request = collections.namedtuple( 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_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) + 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.cookie.SimpleCookie(env.get('HTTP_COOKIE')) + except: + cookie = http.cookie.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 @@ -51,7 +73,7 @@ class Request(_Request): content = content, content_length = content_length, content_type = content_type, - cookies=cookies, + cookie=cookie, method=method, parameters=parameters, path=path,