forked from SvenskaSpel/locust-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.py
166 lines (144 loc) · 5.92 KB
/
resource.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
import logging
import re
from locust import HttpUser
from locust.contrib.fasthttp import FastHttpUser
from lxml import html, etree
class EmbeddedResourceManager:
"""
provides features for finding and managing resources embedded in html
"""
def __init__(
self,
user,
include_resources_by_default,
default_resource_filter,
bundle_resource_stats,
cache_resource_links,
):
# store resource links for requests
self.cache_resource_links = cache_resource_links
self.resource_link_cache = {}
# bundle all stats into single line for each request (_resources)
self.bundle_resource_stats = bundle_resource_stats
self.resource_filter_pattern = re.compile(default_resource_filter)
self.include_resources = include_resources_by_default
# for finding url links in style tags
self.url_link_pattern = re.compile(
r".*URL\(\s*('|\")(.*)('|\")\s*\).*",
re.IGNORECASE | re.MULTILINE | re.DOTALL,
)
# for finding if a link is partial or full
self.full_url_pattern = re.compile("^https?://", re.IGNORECASE)
self.resource_paths = [
'//link[@rel="stylesheet"]/@href',
'//link[@rel="Stylesheet"]/@href',
'//link[@rel="STYLESHEET"]/@href',
"//script/@src",
"//img/@src",
"//source/@src",
"//embed/@src",
"//body/@background",
'//input[@type="image"]/@src',
'//input[@type="IMAGE"]/@src',
'//input[@type="Image"]/@src',
"//object/@data",
"//frame/@src",
"//iframe/@src",
]
self.client = user.client
self.client.request = self._request(self.client.request)
self.host = user.host
def get_embedded_resources(self, response_content, resource_filter):
"""
returns a list of embedded resources in response_content
provide a regex filter to limit what resources are returned
"""
resources = []
# check if defaults have been overridden for this request
if self.cache_resource_links and response_content in self.resource_link_cache:
resources = self.resource_link_cache[response_content]
else:
try:
tree = html.fromstring(response_content)
# check for base tag - otherwise use host for partial urls
base_path_links = tree.xpath("//base/@href")
base_path = base_path_links[0] if len(base_path_links) > 0 else self.host
# build resource list
for resource_path in self.resource_paths:
for resource in tree.xpath(resource_path):
if re.search(self.full_url_pattern, resource) is None:
resource = base_path + "/" + resource
if re.search(resource_filter, resource):
resources.append(resource)
# add style urls
style_tag_texts = tree.xpath("//style/text()")
for text in style_tag_texts:
# check for url
url_matches = re.match(self.url_link_pattern, text)
if url_matches is not None:
resource = url_matches[2]
if re.search(self.full_url_pattern, resource) is None:
resource = base_path + "/" + resource
if re.search(resource_filter, resource):
resources.append(resource)
if self.cache_resource_links:
self.resource_link_cache[response_content] = resources
except etree.ParserError as e:
logging.warning(str(e) + " " + str(response_content))
return resources
def _request(self, func):
def wrapper(
*args, include_resources=self.include_resources, resource_filter=self.resource_filter_pattern, **kwargs
):
response = func(*args, **kwargs)
if include_resources:
content = response.content
if isinstance(content, bytearray):
content = content.decode("utf-8")
resources = self.get_embedded_resources(content, resource_filter)
name = kwargs.get("name", args[1])
for resource in resources:
# determine name for the resource
if self.bundle_resource_stats:
resource_name = name + "_resources"
else:
resource_name = resource
self.client.request("GET", resource, name=resource_name, include_resources=False)
return response
return wrapper
class HttpUserWithResources(HttpUser):
"""
provides embedded resource management for HttpUser
"""
abstract = True
include_resources_by_default = True
default_resource_filter = ".*"
bundle_resource_stats = True
cache_resource_links = True
def __init__(self, *args):
super().__init__(*args)
EmbeddedResourceManager(
self,
self.include_resources_by_default,
self.default_resource_filter,
self.bundle_resource_stats,
self.cache_resource_links,
)
class FastHttpUserWithResources(FastHttpUser):
"""
provides embedded resource management for FastHttpUser
"""
abstract = True
include_resources_by_default = True
default_resource_filter = ".*"
bundle_resource_stats = True
cache_resource_links = True
def __init__(self, *args):
super().__init__(*args)
EmbeddedResourceManager(
self,
self.include_resources_by_default,
self.default_resource_filter,
self.bundle_resource_stats,
self.cache_resource_links,
)