serialize.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # SPDX-FileCopyrightText: 2015 Eric Larson
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. from __future__ import annotations
  5. import io
  6. from typing import IO, TYPE_CHECKING, Any, Mapping, cast
  7. from pip._vendor import msgpack
  8. from pip._vendor.requests.structures import CaseInsensitiveDict
  9. from pip._vendor.urllib3 import HTTPResponse
  10. if TYPE_CHECKING:
  11. from pip._vendor.requests import PreparedRequest
  12. class Serializer:
  13. serde_version = "4"
  14. def dumps(
  15. self,
  16. request: PreparedRequest,
  17. response: HTTPResponse,
  18. body: bytes | None = None,
  19. ) -> bytes:
  20. response_headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(
  21. response.headers
  22. )
  23. if body is None:
  24. # When a body isn't passed in, we'll read the response. We
  25. # also update the response with a new file handler to be
  26. # sure it acts as though it was never read.
  27. body = response.read(decode_content=False)
  28. response._fp = io.BytesIO(body) # type: ignore[assignment]
  29. response.length_remaining = len(body)
  30. data = {
  31. "response": {
  32. "body": body, # Empty bytestring if body is stored separately
  33. "headers": {str(k): str(v) for k, v in response.headers.items()},
  34. "status": response.status,
  35. "version": response.version,
  36. "reason": str(response.reason),
  37. "decode_content": response.decode_content,
  38. }
  39. }
  40. # Construct our vary headers
  41. data["vary"] = {}
  42. if "vary" in response_headers:
  43. varied_headers = response_headers["vary"].split(",")
  44. for header in varied_headers:
  45. header = str(header).strip()
  46. header_value = request.headers.get(header, None)
  47. if header_value is not None:
  48. header_value = str(header_value)
  49. data["vary"][header] = header_value
  50. return b",".join([f"cc={self.serde_version}".encode(), self.serialize(data)])
  51. def serialize(self, data: dict[str, Any]) -> bytes:
  52. return cast(bytes, msgpack.dumps(data, use_bin_type=True))
  53. def loads(
  54. self,
  55. request: PreparedRequest,
  56. data: bytes,
  57. body_file: IO[bytes] | None = None,
  58. ) -> HTTPResponse | None:
  59. # Short circuit if we've been given an empty set of data
  60. if not data:
  61. return None
  62. # Previous versions of this library supported other serialization
  63. # formats, but these have all been removed.
  64. if not data.startswith(f"cc={self.serde_version},".encode()):
  65. return None
  66. data = data[5:]
  67. return self._loads_v4(request, data, body_file)
  68. def prepare_response(
  69. self,
  70. request: PreparedRequest,
  71. cached: Mapping[str, Any],
  72. body_file: IO[bytes] | None = None,
  73. ) -> HTTPResponse | None:
  74. """Verify our vary headers match and construct a real urllib3
  75. HTTPResponse object.
  76. """
  77. # Special case the '*' Vary value as it means we cannot actually
  78. # determine if the cached response is suitable for this request.
  79. # This case is also handled in the controller code when creating
  80. # a cache entry, but is left here for backwards compatibility.
  81. if "*" in cached.get("vary", {}):
  82. return None
  83. # Ensure that the Vary headers for the cached response match our
  84. # request
  85. for header, value in cached.get("vary", {}).items():
  86. if request.headers.get(header, None) != value:
  87. return None
  88. body_raw = cached["response"].pop("body")
  89. headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(
  90. data=cached["response"]["headers"]
  91. )
  92. if headers.get("transfer-encoding", "") == "chunked":
  93. headers.pop("transfer-encoding")
  94. cached["response"]["headers"] = headers
  95. try:
  96. body: IO[bytes]
  97. if body_file is None:
  98. body = io.BytesIO(body_raw)
  99. else:
  100. body = body_file
  101. except TypeError:
  102. # This can happen if cachecontrol serialized to v1 format (pickle)
  103. # using Python 2. A Python 2 str(byte string) will be unpickled as
  104. # a Python 3 str (unicode string), which will cause the above to
  105. # fail with:
  106. #
  107. # TypeError: 'str' does not support the buffer interface
  108. body = io.BytesIO(body_raw.encode("utf8"))
  109. # Discard any `strict` parameter serialized by older version of cachecontrol.
  110. cached["response"].pop("strict", None)
  111. return HTTPResponse(body=body, preload_content=False, **cached["response"])
  112. def _loads_v4(
  113. self,
  114. request: PreparedRequest,
  115. data: bytes,
  116. body_file: IO[bytes] | None = None,
  117. ) -> HTTPResponse | None:
  118. try:
  119. cached = msgpack.loads(data, raw=False)
  120. except ValueError:
  121. return None
  122. return self.prepare_response(request, cached, body_file)