-
Notifications
You must be signed in to change notification settings - Fork 7
/
request.py
115 lines (94 loc) · 3.05 KB
/
request.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
import htmlunit
from ignore_urls import filterIgnoreUrlParts
from lazyproperty import lazyproperty
from form_filler import FormFiller
from vectors import urlvector, formvector, post_request_vector
class Request(object):
def __init__(self, webrequest):
self.webrequest = webrequest
self.reqresp = None
self.absrequest = None
self.formparams = FormFiller.Params({})
self.state = -1
self.statehint = False
self.changingstate = False
@lazyproperty
def method(self):
return str(self.webrequest.getHttpMethod())
@lazyproperty
def isPOST(self):
return self.method == str(htmlunit.HttpMethod.POST)
@lazyproperty
def path(self):
return self.webrequest.getUrl().getPath()
@lazyproperty
def query(self):
query = self.webrequest.getUrl().getQuery()
query = filterIgnoreUrlParts(query)
return query if query else None
@lazyproperty
def ref(self):
return self.webrequest.getUrl().getRef()
@lazyproperty
def fullpath(self):
fullpath = self.path
if self.query:
fullpath += "?" + self.query
return fullpath
@lazyproperty
def fullpathref(self):
fullpathref = self.fullpath
if self.ref:
fullpathref += "#" + self.ref
return fullpathref
@lazyproperty
def params(self):
return tuple(sorted((nv.getName(), nv.getValue())
for nv in self.webrequest.getRequestParameters()))
@lazyproperty
def cookies(self):
raise NotImplemented
@lazyproperty
def headers(self):
raise NotImplemented
@lazyproperty
def _str(self):
return "Request(%s %s)" % (self.method, self.fullpathref)
@lazyproperty
def shortstr(self):
return "%s %s" % (self.method, self.fullpath)
@lazyproperty
def urlvector(self):
return urlvector(self)
@lazyproperty
def signature_vector(self):
if self.isPOST:
# handle post request
parameters = sorted([(i.name, i.value) for i in self.webrequest.getRequestParameters()])
names, values = None, None
if parameters:
names, values = zip(*parameters)
return post_request_vector("POST", self, names, values)
else:
return tuple(["GET"] + list(self.urlvector))
def __str__(self):
return self._str
def __repr__(self):
return str(self)
@lazyproperty
def dump(self):
lines = []
lines.append(self.method + " " + self.fullpathref)
for h in self.webrequest.getAdditionalHeaders().entrySet():
me = htmlunit.Map.Entry.cast_(h)
lines.append("%s: %s" % (me.key, me.value))
body = self.webrequest.getRequestBody()
if body:
lines.append("")
lines.append(body)
if self.params:
lines.append("\n\nParams:")
for p in self.params:
lines.append("%s=%s" % p)
lines.append("")
return '\n'.join(lines)