This repository was archived by the owner on Jul 13, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcache_spy.py
More file actions
110 lines (89 loc) · 3.38 KB
/
Copy pathcache_spy.py
File metadata and controls
110 lines (89 loc) · 3.38 KB
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
import sys
import requests
from bs4 import BeautifulSoup
from prettytable import PrettyTable
# constants
NOT_AVAILABLE = "not available"
# methods
def extract_cache_properties(request_result):
"""Extracts cache properties from a request result
request_result: Result of the request for extracting the cache properties
Returns: a tuple consisting holding the cache-control header and the etag
"""
if 'cache-control' in request_result.headers:
cache_control = request_result.headers['cache-control']
else:
cache_control = NOT_AVAILABLE
if 'etag' in request_result.headers:
etag = request_result.headers['etag']
else:
etag = NOT_AVAILABLE
return (cache_control, etag)
def get_cache_properties(web_resource_url):
"""Gets the cache properties of the given URL
web_resource_url: The URL from which the resource should be fetched
Returns: a tuple consisting holding the cache-control header and the etag
"""
print('Fetching cache props for: ' + str(web_resource_url))
# perform actual request
request_result = requests.get(web_resource_url)
cache_properties = extract_cache_properties(request_result)
return cache_properties
# check arguments
if len(sys.argv) < 2:
print("Please provide the url of a webresource, e.g. http://www.github.com")
sys.exit(1)
# aggregated cache information
cache_information_items = []
# fetch root webresource
url_to_check = sys.argv[1]
request_result = requests.get(url_to_check)
# print cache properties of "main" url
cache_props = extract_cache_properties(request_result)
cache_information = (url_to_check,) + cache_props
cache_information_items.append(cache_information)
# get all included resources
html = request_result.text
soup = BeautifulSoup(html, 'html.parser')
# fetch all resources adressed via link
links = soup.find_all('link')
for link in links:
# we need the rel attribute for checking the resource
if not link.get('rel'):
continue
rel_val = link.get('rel')[0]
# at the moment we only consider stylesheets and preloaded resources
if not rel_val in ('stylesheet', 'preload'):
continue
# get cache properties for resource
url = link.get('href')
cache_props = get_cache_properties(url)
cache_information = (url,) + cache_props
cache_information_items.append(cache_information)
# fetch all resources adressed via script (but of course not inline scripts)
scripts = soup.find_all('script')
for script in scripts:
# we don't need to fetch inline scripts
if not script.get('src'):
continue
# get cache properties for resource
url = script.get('src')
cache_props = get_cache_properties(url)
cache_information = (url,) + cache_props
cache_information_items.append(cache_information)
# fetch all images resource information
images = soup.find_all('img')
for image in images:
# src attribute is needed for fetching the image
if not image.get('src'):
continue
# get cache properties for resource
url = image.get('src')
cache_props = get_cache_properties(url)
cache_information = (url,) + cache_props
cache_information_items.append(cache_information)
# print all cache properties
t = PrettyTable(['URL', 'Cache-Control', 'Etag'])
for cache_information in cache_information_items:
t.add_row([cache_information[0],cache_information[1],cache_information[2]])
print(t)