-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSRequest.py
30 lines (25 loc) · 877 Bytes
/
SRequest.py
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
import SInvalid
class HttpRequest(object):
def __init__(self, request):
self._request = request
(self.method, self.path, self.protocol, self.headers) = self.parse()
def parse(self):
line = [i.strip() for i in self._request.splitlines()]
if len(line) == 0:
print self._request
raise SInvalid.InvalidRequest('Invalid Request')
if line[0].find('HTTP') == -1:
(method, path) = [i.strip() for i in line[0].split()]
print method
#raise SInvalid.InvalidRequest('Protocol wrong!')
else:
(method, path, protocol) = [i.strip() for i in line[0].split()]
print method
headers = {}
if method == 'GET' or method == 'HEAD':
print path
for j, k in [i.split(':', 1) for i in line[1:-1]]:
headers[j.strip()] = k.strip()
else:
raise SInvalid.InvalidRequest('Http Method not' + method + '!')
return (method, path, protocol, headers)