16
16
17
17
18
18
_fingerprint_cache = weakref .WeakKeyDictionary ()
19
- def request_fingerprint (request , include_headers = None ):
19
+ def request_fingerprint (request , include_headers = None , keep_fragments = False ):
20
20
"""
21
21
Return the request fingerprint.
22
22
@@ -42,24 +42,30 @@ def request_fingerprint(request, include_headers=None):
42
42
the fingeprint. If you want to include specific headers use the
43
43
include_headers argument, which is a list of Request headers to include.
44
44
45
+ Also, servers usually ignore fragments in urls when handling requests,
46
+ so they are also ignored by default when calculating the fingerprint.
47
+ If you want to include them, set the keep_fragments argument to True
48
+ (for instance when handling requests with a headless browser).
49
+
45
50
"""
46
51
if include_headers :
47
52
include_headers = tuple (to_bytes (h .lower ())
48
53
for h in sorted (include_headers ))
49
54
cache = _fingerprint_cache .setdefault (request , {})
50
- if include_headers not in cache :
55
+ cache_key = (include_headers , keep_fragments )
56
+ if cache_key not in cache :
51
57
fp = hashlib .sha1 ()
52
58
fp .update (to_bytes (request .method ))
53
- fp .update (to_bytes (canonicalize_url (request .url )))
59
+ fp .update (to_bytes (canonicalize_url (request .url , keep_fragments = keep_fragments )))
54
60
fp .update (request .body or b'' )
55
61
if include_headers :
56
62
for hdr in include_headers :
57
63
if hdr in request .headers :
58
64
fp .update (hdr )
59
65
for v in request .headers .getlist (hdr ):
60
66
fp .update (v )
61
- cache [include_headers ] = fp .hexdigest ()
62
- return cache [include_headers ]
67
+ cache [cache_key ] = fp .hexdigest ()
68
+ return cache [cache_key ]
63
69
64
70
65
71
def request_authenticate (request , username , password ):
0 commit comments