From 0f0e815ad1b775ff93699b695f290c562c57962f Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 24 May 2019 16:29:03 +0400 Subject: =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D0=B8=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8?= =?UTF-8?q?=20http-=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE=D0=B2,=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BE=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA=20url-=D0=BF=D1=83?= =?UTF-8?q?=D1=82=D0=B5=D0=B9.=20=D0=9E=D0=B1=D0=B5=D1=80=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D1=8B=20=D1=81=20shelve=20=D0=B1=D0=B0=D0=B7=D0=B0=D0=BC=D0=B8?= =?UTF-8?q?=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85,=20html-=D1=88=D0=B0?= =?UTF-8?q?=D0=B1=D0=BB=D0=BE=D0=BD=D0=B8=D0=B7=D0=B0=D1=82=D0=BE=D1=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day7/utils.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 day7/utils.py (limited to 'day7/utils.py') diff --git a/day7/utils.py b/day7/utils.py new file mode 100644 index 0000000..0aa0709 --- /dev/null +++ b/day7/utils.py @@ -0,0 +1,91 @@ +import re +from time import strftime, gmtime + +_URI_RESERVED = { + '21': '!', '23': '#', '24': '$', '26': '&', + '27': '\'', '28': '(', '29': ')', '2A': '*', + '2B': '+', '2C': ',', '2F': '/', '3A': ':', + '3B': ';', '3D': '=', '3F': '?', '40': '@', + '5B': '[', '5D': ']' +} + +BAD_REQUEST = 'HTTP/1.1 400 Bad Request' +NOT_FOUND = 'HTTP/1.1 404 Not Found' +SUCCESS = 'HTTP/1.1 200 OK' +METHOD_NOT_ALLOWED = 'HTTP/1.1 405 Method Not Allowed' + + +def add_headers(status, html: str): + return '\r\n'.join([ + status, + f'Date: {strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime())}', + 'Server: BrandNewServer', + 'Content-Type: text/html; charset=utf-8', + 'Connection: keep-alive', + '', html + ]) + + +def validate_url(url): + url_pattern = re.compile(r'/((.*?/?)+)?') + return bool(url_pattern.fullmatch(url)) + + +def parse_cookies(cookies_line: str): + cookies_line = cookies_line.strip().strip(';') + pairs = cookies_line.split(';') + d = {} + for pair in pairs: + try: + key, value = pair.split('=', maxsplit=1) + d[key] = value + except ValueError: + raise ValueError('Wrong format of cookies') + + return d + + +def parse_headers(request_line: str): + request = request_line.split('\r\n') + method, url, http_ver = request.pop(0).split() + headers = {} + for line in request: + field, value = line.split(': ', maxsplit=1) + headers[field] = value + + return method, url, http_ver, headers + + +def parse_query(query_line: str): + pairs = query_line.strip().split('&') + d = {} + for pair in pairs: + try: + key, value = pair.split('=', maxsplit=1) + d[url_decoder(key)] = url_decoder(value) + except ValueError: + raise ValueError('Wrong format of query') + + return d + + +def url_decoder(url_line: str): + url_line = url_line.replace('+', ' ') + encoded = b'' + i = 0 + while i < len(url_line): + if url_line[i] == '%': + hex_value = url_line[i + 1: i + 3] + if hex_value in _URI_RESERVED: + integer = ord(_URI_RESERVED[hex_value]) + else: + integer = int(hex_value, 16) + + encoded += bytes([integer]) + i += 3 + continue + else: + encoded += bytes([ord(url_line[i])]) + i += 1 + + return encoded.decode() -- cgit v1.2.3