forked from akatrevorjay/http-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·138 lines (103 loc) · 3.17 KB
/
test.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# import pyximport
# pyximport.install(
# inplace=True,
# # reload_support=True,
# )
from vkproxy.log import get_logger
_LOG = get_logger()
import pyrox.http.parser as parser
class ParserDelegate(parser.ParserDelegate):
def on_message_begin(self):
print(self, 'on_message_begin')
pass
def on_req_method(self, method):
print(self, 'on_req_method', method)
pass
def on_req_url(self, url):
print(self, 'on_req_url', url)
pass
def on_resp_status(self, code, desc):
print(self, 'on_resp_status', code, desc)
pass
def on_header_field(self, field):
print(self, 'on_header_field', field)
pass
def on_header_value(self, value):
print(self, 'on_header_value', value)
pass
def on_http_version(self, major, minor):
print(self, 'on_http_version', major, minor)
pass
def on_headers_complete(self, keep_alive):
print(self, 'on_headers_complete', keep_alive)
pass
def on_body(self, data, length, is_chunked):
print(self, 'on_body', data, length, is_chunked)
pass
def on_message_complete(self, is_chunked, keep_alive):
print(self, 'on_message_complete', is_chunked, keep_alive)
pass
def on_chunk_header(self):
print(self, 'on_chunk_header')
pass
def on_chunk_complete(self):
print(self, 'on_chunk_complete')
pass
def on_upgrade(self):
print(self, 'on_upgrade')
pass
UNEXPECTED_HEADER_REQUEST = (
'GET /test/12345?field=f1&field2=f2#fragment HTTP/1.1\r\n'
'Test: test\r\n'
'Connection: keep-alive\r\n'
'Content-Length: 12\r\n\r\n'
'This is test'
)
NORMAL_REQUEST = (
'GET /test/12345?field=f1&field2=f2#fragment HTTP/1.1\r\n'
'Connection: keep-alive\r\n'
'Content-Length: 12\r\n\r\n'
'This is test'
)
CHUNKED_REQUEST = (
'GET /test/12345?field=f1&field2=f2#fragment HTTP/1.1\r\n'
'Connection: keep-alive\r\n'
'Transfer-Encoding: chunked\r\n\r\n'
'1e\r\nall your base are belong to us\r\n'
'0\r\n'
'\r\n'
)
requests = [UNEXPECTED_HEADER_REQUEST, NORMAL_REQUEST, CHUNKED_REQUEST]
_LOG.info('+++++')
_LOG.info('Request prep')
req_delegate = ParserDelegate()
req_parser = parser.RequestParser(req_delegate)
for request in requests:
_LOG.info('-----')
_LOG.info('request: %s', request)
print(req_parser.execute(request))
NORMAL_RESPONSE = """HTTP/1.1 200 OK\r
Content-Length: 12\r\n\r
This is test"""
CHUNKED_RESPONSE = """HTTP/1.1 200 OK\r
Transfer-Encoding: chunked\r\n\r
1e\r\nall your base are belong to us\r
0\r
"""
responses = [NORMAL_RESPONSE, CHUNKED_RESPONSE]
_LOG.info('Response prep')
resp_delegate = ParserDelegate()
resp_parser = parser.ResponseParser(resp_delegate)
for response in responses:
_LOG.info('-----')
_LOG.info('response: %s', response)
print(resp_parser.execute(response))
urlp = parser.HttpUrlParser()
p = urlp.parse('http://192.168.99.100', False)
print(p)
p = urlp.parse('192.168.99.100:443', True)
print(p)
url = 'http://trevorj:[email protected]:8080/omg?yes=true&nope=whoa#anchorbaby'
p = urlp.parse(url, False)
print(p)