m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.2 KiB

6 months ago
  1. # SPDX-FileCopyrightText: 2015 Eric Larson
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. from __future__ import annotations
  5. import functools
  6. import types
  7. import zlib
  8. from typing import TYPE_CHECKING, Any, Collection, Mapping
  9. from pip._vendor.requests.adapters import HTTPAdapter
  10. from pip._vendor.cachecontrol.cache import DictCache
  11. from pip._vendor.cachecontrol.controller import PERMANENT_REDIRECT_STATUSES, CacheController
  12. from pip._vendor.cachecontrol.filewrapper import CallbackFileWrapper
  13. if TYPE_CHECKING:
  14. from pip._vendor.requests import PreparedRequest, Response
  15. from pip._vendor.urllib3 import HTTPResponse
  16. from pip._vendor.cachecontrol.cache import BaseCache
  17. from pip._vendor.cachecontrol.heuristics import BaseHeuristic
  18. from pip._vendor.cachecontrol.serialize import Serializer
  19. class CacheControlAdapter(HTTPAdapter):
  20. invalidating_methods = {"PUT", "PATCH", "DELETE"}
  21. def __init__(
  22. self,
  23. cache: BaseCache | None = None,
  24. cache_etags: bool = True,
  25. controller_class: type[CacheController] | None = None,
  26. serializer: Serializer | None = None,
  27. heuristic: BaseHeuristic | None = None,
  28. cacheable_methods: Collection[str] | None = None,
  29. *args: Any,
  30. **kw: Any,
  31. ) -> None:
  32. super().__init__(*args, **kw)
  33. self.cache = DictCache() if cache is None else cache
  34. self.heuristic = heuristic
  35. self.cacheable_methods = cacheable_methods or ("GET",)
  36. controller_factory = controller_class or CacheController
  37. self.controller = controller_factory(
  38. self.cache, cache_etags=cache_etags, serializer=serializer
  39. )
  40. def send(
  41. self,
  42. request: PreparedRequest,
  43. stream: bool = False,
  44. timeout: None | float | tuple[float, float] | tuple[float, None] = None,
  45. verify: bool | str = True,
  46. cert: (None | bytes | str | tuple[bytes | str, bytes | str]) = None,
  47. proxies: Mapping[str, str] | None = None,
  48. cacheable_methods: Collection[str] | None = None,
  49. ) -> Response:
  50. """
  51. Send a request. Use the request information to see if it
  52. exists in the cache and cache the response if we need to and can.
  53. """
  54. cacheable = cacheable_methods or self.cacheable_methods
  55. if request.method in cacheable:
  56. try:
  57. cached_response = self.controller.cached_request(request)
  58. except zlib.error:
  59. cached_response = None
  60. if cached_response:
  61. return self.build_response(request, cached_response, from_cache=True)
  62. # check for etags and add headers if appropriate
  63. request.headers.update(self.controller.conditional_headers(request))
  64. resp = super().send(request, stream, timeout, verify, cert, proxies)
  65. return resp
  66. def build_response(
  67. self,
  68. request: PreparedRequest,
  69. response: HTTPResponse,
  70. from_cache: bool = False,
  71. cacheable_methods: Collection[str] | None = None,
  72. ) -> Response:
  73. """
  74. Build a response by making a request or using the cache.
  75. This will end up calling send and returning a potentially
  76. cached response
  77. """
  78. cacheable = cacheable_methods or self.cacheable_methods
  79. if not from_cache and request.method in cacheable:
  80. # Check for any heuristics that might update headers
  81. # before trying to cache.
  82. if self.heuristic:
  83. response = self.heuristic.apply(response)
  84. # apply any expiration heuristics
  85. if response.status == 304:
  86. # We must have sent an ETag request. This could mean
  87. # that we've been expired already or that we simply
  88. # have an etag. In either case, we want to try and
  89. # update the cache if that is the case.
  90. cached_response = self.controller.update_cached_response(
  91. request, response
  92. )
  93. if cached_response is not response:
  94. from_cache = True
  95. # We are done with the server response, read a
  96. # possible response body (compliant servers will
  97. # not return one, but we cannot be 100% sure) and
  98. # release the connection back to the pool.
  99. response.read(decode_content=False)
  100. response.release_conn()
  101. response = cached_response
  102. # We always cache the 301 responses
  103. elif int(response.status) in PERMANENT_REDIRECT_STATUSES:
  104. self.controller.cache_response(request, response)
  105. else:
  106. # Wrap the response file with a wrapper that will cache the
  107. # response when the stream has been consumed.
  108. response._fp = CallbackFileWrapper( # type: ignore[attr-defined]
  109. response._fp, # type: ignore[attr-defined]
  110. functools.partial(
  111. self.controller.cache_response, request, response
  112. ),
  113. )
  114. if response.chunked:
  115. super_update_chunk_length = response._update_chunk_length # type: ignore[attr-defined]
  116. def _update_chunk_length(self: HTTPResponse) -> None:
  117. super_update_chunk_length()
  118. if self.chunk_left == 0:
  119. self._fp._close() # type: ignore[attr-defined]
  120. response._update_chunk_length = types.MethodType( # type: ignore[attr-defined]
  121. _update_chunk_length, response
  122. )
  123. resp: Response = super().build_response(request, response) # type: ignore[no-untyped-call]
  124. # See if we should invalidate the cache.
  125. if request.method in self.invalidating_methods and resp.ok:
  126. assert request.url is not None
  127. cache_url = self.controller.cache_url(request.url)
  128. self.cache.delete(cache_url)
  129. # Give the request a from_cache attr to let people use it
  130. resp.from_cache = from_cache # type: ignore[attr-defined]
  131. return resp
  132. def close(self) -> None:
  133. self.cache.close()
  134. super().close() # type: ignore[no-untyped-call]