|
| 1 | +# Parser module to parse environ variables |
| 2 | +from http.cookies import SimpleCookie |
| 3 | +import io |
| 4 | +import cgi |
| 5 | +import re |
| 6 | + |
| 7 | + |
| 8 | +def clean(item): |
| 9 | + if item != "": |
| 10 | + return True |
| 11 | + |
| 12 | +def set_cookie_header(cookie): |
| 13 | + """Parse cookie object to HTTP Set-Cookie header""" |
| 14 | + |
| 15 | + header = list() |
| 16 | + cookie_options = cookie.output().split("Set-Cookie:") |
| 17 | + cookie_options = list(filter(clean, cookie_options)) |
| 18 | + for index, option in enumerate(cookie_options): |
| 19 | + cookie_options[index] = option.strip("\r\n") |
| 20 | + cookie_options = ",".join(cookie_options) + '; ' + ';'.join(cookie.options) |
| 21 | + |
| 22 | + |
| 23 | + header = [("Set-Cookie", cookie_options),] |
| 24 | + return header |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +class Cookie(SimpleCookie): |
| 30 | + """Cookie class inheriting http.SimpleCookie class""" |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + super().__init__() |
| 34 | + self.options = ["HttpOnly",] |
| 35 | + |
| 36 | + def __setitem__(self, key, value): |
| 37 | + super().__setitem__(key, value) |
| 38 | + item = self.__getitem__(key) |
| 39 | + |
| 40 | + def httponly(self, choice): |
| 41 | + """Set httponly option in Set-Cookie""" |
| 42 | + if choice == True: |
| 43 | + if not "HttpOnly" in self.options: |
| 44 | + self.options.append("HttpOnly") |
| 45 | + elif choice == False: |
| 46 | + if "HttpOnly" in self.options: |
| 47 | + self.options.remove("HttpOnly") |
| 48 | + |
| 49 | + |
| 50 | + def secure(self, choice): |
| 51 | + if choice == True: |
| 52 | + if not "Secure" in self.options: |
| 53 | + self.options.append("Secure") |
| 54 | + elif choice == False: |
| 55 | + if "Secure" in self.options: |
| 56 | + self.options.remove("Secure") |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + def flush(self): |
| 62 | + """Expires a given cookie value""" |
| 63 | + |
| 64 | + self.options.append("expires=Mon 23 Jun 1967 04:34:23 GMT") |
| 65 | + |
| 66 | + def will_expire(self, date): |
| 67 | + """Sets future expiry date for cookie value""" |
| 68 | + |
| 69 | + self.options.append("expires={}".format(date)) |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +class Context(): |
| 78 | + """Make needed environ variables accessible in a single class""" |
| 79 | + |
| 80 | + def __init__(self, environ): |
| 81 | + |
| 82 | + #Get incoming cookies |
| 83 | + cookie = environ.get("HTTP_COOKIE", "") |
| 84 | + self.session = Cookie() |
| 85 | + self.session.load(cookie) |
| 86 | + |
| 87 | + #HTTP Request |
| 88 | + self.request = dict() |
| 89 | + self.request["content-length"] = environ.get("CONTENT_LENGTH", 0) |
| 90 | + self.request["content-type"] = "text/html" |
| 91 | + self.request["user-agent"] = environ.get("HTTP_USER-AGENT", "") |
| 92 | + self.request["ip-address"] = environ.get("REMOTE_ADDR", "") |
| 93 | + self.request["path"] = environ.get("PATH_INFO", "/") |
| 94 | + self.query_str = environ.get("QUERY_STRING") |
| 95 | + self.request["method"] = environ.get("REQUEST_METHOD") |
| 96 | + self.request["protocol"] = environ.get("SERVER_PROTOCOL", "") |
| 97 | + self.request["scheme"] = environ.get("wsgi.url_scheme", "http") |
| 98 | + self.request["data"] = environ.get("wsgi.input", "") |
| 99 | + |
| 100 | + |
| 101 | + #HTTP Response dict |
| 102 | + self.response = dict() |
| 103 | + |
| 104 | + #HTTP Parameters dict() |
| 105 | + self.params = dict() |
| 106 | + |
| 107 | + if self.request["content-length"] == '': |
| 108 | + self.request["content-length"]=0 |
| 109 | + |
| 110 | + #HTTP Form |
| 111 | + self.form = dict() |
| 112 | + self.formFile = dict() |
| 113 | + fp = self.request["data"].read(int(self.request["content-length"])) |
| 114 | + formValues = cgi.FieldStorage(fp=io.BytesIO(fp), environ=environ, keep_blank_values=True) |
| 115 | + keys = formValues.keys() |
| 116 | + for key in keys: |
| 117 | + value = formValues[key] |
| 118 | + if value.filename: |
| 119 | + self.formFile[key] = value |
| 120 | + else: |
| 121 | + self.form[key] = formValues.getvalue(key) |
| 122 | + |
| 123 | + |
| 124 | + #Query matching and parsing |
| 125 | + self.query = dict() |
| 126 | + |
| 127 | + query_regex = "[a-zA-Z0-9%+]+=[a-zA-Z0-9%+]+" |
| 128 | + queries = re.findall(query_regex, self.query_str) |
| 129 | + for index, query in enumerate(queries): |
| 130 | + queries[index] = query.replace("%20", " ").replace("+", " ") |
| 131 | + |
| 132 | + #Populate Query dict |
| 133 | + for query in queries: |
| 134 | + q = query.split("=") |
| 135 | + self.query[q[0]] = q[1] |
| 136 | + |
| 137 | + |
| 138 | + |
0 commit comments