-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpUtils.py
188 lines (155 loc) · 4.51 KB
/
httpUtils.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""Library to manipulate HTTP messages.
The main purpose is to help developing burp extensions.
Since jython does not handle exceptions well ('except' does not work), we do
not raise any exception explicitly and check values to avoid raising any.
"""
import urllib
class Request(object):
"""Request represents an HTTP request."""
Method = ''
Version = ''
Path = ''
Query = ''
Headers = None
Body = ''
@classmethod
def Parse(cls, content):
parsed = parse(content)
if not parsed:
return
first, headers, body = parsed
w = first.split(' ')
if len(w) != 3:
return
r = cls()
r.Method = w[0]
r.Version = w[2]
q = w[1].split('?')
r.Path = q[0]
r.Query = q[1] if len(q) > 1 else ''
r.Headers = Headers(headers)
r.Body = body
return r
def String(self):
path = self.Path
return '%s %s %s\r\n%s\r\n%s' % (
self.Method,
'%s?%s' % (path, self.Query) if self.Query else path,
self.Version,
self.Headers.String(),
self.Body
)
class Response(object):
"""Response represents an HTTP response."""
Version = ''
Status = 0
Message = ''
Headers = None
Body = ''
Request = None
@classmethod
def Parse(cls, content, request=None):
parsed = parse(content)
if not parsed:
return
first, headers, body = parsed
w = first.split(' ', 2)
if len(w) < 3:
return
r = cls()
r.Version = w[0]
r.Status = int(w[1])
r.Message = w[2]
r.Headers = Headers(headers)
r.Body = body
r.Request = request
return r
def String(self):
return '%s %i %s\r\n%s\r\n%s' % (
self.Version,
self.Status,
self.Message,
self.Headers.String(),
self.Body
)
class Headers(object):
"""Headers represents headers of an HTTP message."""
def __init__(self, headers=''):
self.h = []
for line in headers.split('\r\n'):
w = line.split(':', 1)
if not w[0]:
continue
if len(w) == 1:
self.Add(w[0], '')
else:
self.Add(w[0], w[1].lstrip())
def Add(self, name, value):
self.h.append((name, value))
def Del(self, name):
for key, value in self.h:
if key.lower() == name.lower():
self.h.remove((key, value))
def Get(self, name):
for key, value in self.h:
if key.lower() == name.lower():
return value
def Set(self, name, value):
self.Del(name)
self.Add(name, value)
def String(self):
if not self.h:
return ''
return '\r\n'.join('%s: %s' % (k, v) for k, v in self.h) + '\r\n'
class Values(object):
"""Values represents values in a query parameter."""
def __init__(self, q=''):
self.v = []
p = q.find('#')
if p != -1:
q = q[:p]
for a in q.split('&'):
w = a.split('=', 1)
if not w[0]:
continue
if len(w) == 1:
self.Add(w[0], '')
else:
self.Add(queryUnescape(w[0]), queryUnescape(w[1]))
def Add(self, name, value):
self.v.append((name, value))
def Del(self, name):
for key, value in self.v:
if key == name:
self.v.remove((key, value))
def Get(self, name):
for key, value in self.v:
if key == name:
return value
def Set(self, name, value):
self.Del(name)
self.Add(name, value)
def Encode(self):
v = []
for key, value in self.v:
if value:
v.append('%s=%s' % (queryEscape(key), queryEscape(value)))
else:
v.append(queryEscape(key))
return '&'.join(v)
def parse(m):
"""parse parses an HTTP message into 3 parts: first line, headers, body."""
while m.startswith('HTTP/1.1 100 Continue\r\n'):
m = m[m.find('\r\n\r\n')+4:]
p = m.find('\r\n')
if p == -1:
return
first = m[:p]
q = m.find('\r\n\r\n')
headers = m[p+2:q]
body = m[q+4:]
return first, headers, body
def queryEscape(s):
return urllib.quote_plus(s)
def queryUnescape(s):
return urllib.unquote_plus(s)