blob: 58e83dc17ffe9e2b7f85068c5ec31af944f20de4 (
plain) (
blame)
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
|
"""HTTP response and request parsers
This module contains parses for HTTP response and HTTP requests.
"""
import webhttp.message
class RequestParser:
"""Class that parses a HTTP request"""
def __init__(self):
"""Initialize the RequestParser"""
self.buff = ''
def parse_requests(self, buff):
"""Parse requests in a buffer
Args:
buff (str): the buffer contents received from socket
Returns:
list of webhttp.Request
"""
self.buff += buff
requests = self.get_requests()
http_requests = []
for request in requests:
http_request = webhttp.message.Request()
http_request.parse(request)
http_requests.append(http_request)
return http_requests
def get_requests(self):
"""Split multiple requests from buffer
Returns:
list of str
"""
requests = self.buff.split('\r\n\r\n')
requests = filter(None, requests)
requests = [r.lstrip() + '\r\n\r\n' for r in requests]
self.buff = ''
return requests
class ResponseParser:
"""Class that parses a HTTP response"""
def __init__(self):
"""Initialize the ResponseParser"""
pass
def parse_response(self, buff):
"""Parse responses in buffer
Args:
buff (str): the buffer contents received from socket
Returns:
webhttp.Response
"""
response = webhttp.message.Response()
response.parse(buff)
return response
|