-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
73 lines (59 loc) · 2.08 KB
/
Copy path__init__.py
File metadata and controls
73 lines (59 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class HttpToRequestsConverter:
def __init__(self, raw_http):
self.raw_http = raw_http.strip()
self.__method = None
self.__url = None
self.__headers = {}
self.__cookies = {}
self.__body = None
self._parse_http()
def _parse_http(self):
# Split headers and body
request_parts = self.raw_http.split('\n\n', 1)
header_lines = request_parts[0].splitlines()
# Parse the request line (GET / HTTP/1.1)
request_line = header_lines[0]
self.__method, self.__path, _ = request_line.split()
self.__url = self._get_full_url()
# Parse headers
for line in header_lines[1:]:
if line.strip(): # Ignore empty lines
key, value = line.split(":", 1)
key, value = key.strip(), value.strip()
# Separate cookies if found
if key.lower() == "cookie":
self._parse_cookies(value)
else:
self.headers[key] = value
# Parse body if it exists
if len(request_parts) > 1:
self.__body = request_parts[1].strip()
def _get_full_url(self):
# Check if the URL is relative
if self.__path.startswith("http"):
return self.__path
else:
# Add the protocol (http) and host if it's missing
host = self.headers.get("Host", "")
return f"http://{host}{self.__path}" if host else self.__path
def _parse_cookies(self, cookie_header):
# Parse cookies from Cookie header
cookies = cookie_header.split(";")
for cookie in cookies:
key, value = cookie.strip().split("=", 1)
self.__cookies[key.strip()] = value.strip()
@property
def url(self):
return self._get_full_url()
@property
def cookies(self):
return self.__cookies
@property
def headers(self):
return self.__headers
@property
def method(self):
return self.__method
@property
def body(self):
return self.__body