summaryrefslogtreecommitdiff
path: root/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/serialize.py
diff options
context:
space:
mode:
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/serialize.py')
-rw-r--r--venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/serialize.py194
1 files changed, 0 insertions, 194 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/serialize.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/serialize.py
deleted file mode 100644
index cd21cae..0000000
--- a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/cachecontrol/serialize.py
+++ /dev/null
@@ -1,194 +0,0 @@
1import base64
2import io
3import json
4import zlib
5
6from pip._vendor import msgpack
7from pip._vendor.requests.structures import CaseInsensitiveDict
8
9from .compat import HTTPResponse, pickle, text_type
10
11
12def _b64_decode_bytes(b):
13 return base64.b64decode(b.encode("ascii"))
14
15
16def _b64_decode_str(s):
17 return _b64_decode_bytes(s).decode("utf8")
18
19
20class Serializer(object):
21
22 def dumps(self, request, response, body=None):
23 response_headers = CaseInsensitiveDict(response.headers)
24
25 if body is None:
26 body = response.read(decode_content=False)
27
28 # NOTE: 99% sure this is dead code. I'm only leaving it
29 # here b/c I don't have a test yet to prove
30 # it. Basically, before using
31 # `cachecontrol.filewrapper.CallbackFileWrapper`,
32 # this made an effort to reset the file handle. The
33 # `CallbackFileWrapper` short circuits this code by
34 # setting the body as the content is consumed, the
35 # result being a `body` argument is *always* passed
36 # into cache_response, and in turn,
37 # `Serializer.dump`.
38 response._fp = io.BytesIO(body)
39
40 # NOTE: This is all a bit weird, but it's really important that on
41 # Python 2.x these objects are unicode and not str, even when
42 # they contain only ascii. The problem here is that msgpack
43 # understands the difference between unicode and bytes and we
44 # have it set to differentiate between them, however Python 2
45 # doesn't know the difference. Forcing these to unicode will be
46 # enough to have msgpack know the difference.
47 data = {
48 u"response": {
49 u"body": body,
50 u"headers": dict(
51 (text_type(k), text_type(v))
52 for k, v in response.headers.items()
53 ),
54 u"status": response.status,
55 u"version": response.version,
56 u"reason": text_type(response.reason),
57 u"strict": response.strict,
58 u"decode_content": response.decode_content,
59 },
60 }
61
62 # Construct our vary headers
63 data[u"vary"] = {}
64 if u"vary" in response_headers:
65 varied_headers = response_headers[u'vary'].split(',')
66 for header in varied_headers:
67 header = header.strip()
68 header_value = request.headers.get(header, None)
69 if header_value is not None:
70 header_value = text_type(header_value)
71 data[u"vary"][header] = header_value
72
73 return b",".join([b"cc=4", msgpack.dumps(data, use_bin_type=True)])
74
75 def loads(self, request, data):
76 # Short circuit if we've been given an empty set of data
77 if not data:
78 return
79
80 # Determine what version of the serializer the data was serialized
81 # with
82 try:
83 ver, data = data.split(b",", 1)
84 except ValueError:
85 ver = b"cc=0"
86
87 # Make sure that our "ver" is actually a version and isn't a false
88 # positive from a , being in the data stream.
89 if ver[:3] != b"cc=":
90 data = ver + data
91 ver = b"cc=0"
92
93 # Get the version number out of the cc=N
94 ver = ver.split(b"=", 1)[-1].decode("ascii")
95
96 # Dispatch to the actual load method for the given version
97 try:
98 return getattr(self, "_loads_v{0}".format(ver))(request, data)
99 except AttributeError:
100 # This is a version we don't have a loads function for, so we'll
101 # just treat it as a miss and return None
102 return
103
104 def prepare_response(self, request, cached):
105 """Verify our vary headers match and construct a real urllib3
106 HTTPResponse object.
107 """
108 # Special case the '*' Vary value as it means we cannot actually
109 # determine if the cached response is suitable for this request.
110 if "*" in cached.get("vary", {}):
111 return
112
113 # Ensure that the Vary headers for the cached response match our
114 # request
115 for header, value in cached.get("vary", {}).items():
116 if request.headers.get(header, None) != value:
117 return
118
119 body_raw = cached["response"].pop("body")
120
121 headers = CaseInsensitiveDict(data=cached['response']['headers'])
122 if headers.get('transfer-encoding', '') == 'chunked':
123 headers.pop('transfer-encoding')
124
125 cached['response']['headers'] = headers
126
127 try:
128 body = io.BytesIO(body_raw)
129 except TypeError:
130 # This can happen if cachecontrol serialized to v1 format (pickle)
131 # using Python 2. A Python 2 str(byte string) will be unpickled as
132 # a Python 3 str (unicode string), which will cause the above to
133 # fail with:
134 #
135 # TypeError: 'str' does not support the buffer interface
136 body = io.BytesIO(body_raw.encode('utf8'))
137
138 return HTTPResponse(
139 body=body,
140 preload_content=False,
141 **cached["response"]
142 )
143
144 def _loads_v0(self, request, data):
145 # The original legacy cache data. This doesn't contain enough
146 # information to construct everything we need, so we'll treat this as
147 # a miss.
148 return
149
150 def _loads_v1(self, request, data):
151 try:
152 cached = pickle.loads(data)
153 except ValueError:
154 return
155
156 return self.prepare_response(request, cached)
157
158 def _loads_v2(self, request, data):
159 try:
160 cached = json.loads(zlib.decompress(data).decode("utf8"))
161 except (ValueError, zlib.error):
162 return
163
164 # We need to decode the items that we've base64 encoded
165 cached["response"]["body"] = _b64_decode_bytes(
166 cached["response"]["body"]
167 )
168 cached["response"]["headers"] = dict(
169 (_b64_decode_str(k), _b64_decode_str(v))
170 for k, v in cached["response"]["headers"].items()
171 )
172 cached["response"]["reason"] = _b64_decode_str(
173 cached["response"]["reason"],
174 )
175 cached["vary"] = dict(
176 (_b64_decode_str(k), _b64_decode_str(v) if v is not None else v)
177 for k, v in cached["vary"].items()
178 )
179
180 return self.prepare_response(request, cached)
181
182 def _loads_v3(self, request, data):
183 # Due to Python 2 encoding issues, it's impossible to know for sure
184 # exactly how to load v3 entries, thus we'll treat these as a miss so
185 # that they get rewritten out as v4 entries.
186 return
187
188 def _loads_v4(self, request, data):
189 try:
190 cached = msgpack.loads(data, encoding='utf-8')
191 except ValueError:
192 return
193
194 return self.prepare_response(request, cached)