Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

267 Zeilen
8.2 KiB

  1. from __future__ import annotations
  2. import io
  3. import typing
  4. from base64 import b64encode
  5. from enum import Enum
  6. from ..exceptions import UnrewindableBodyError
  7. from .util import to_bytes
  8. if typing.TYPE_CHECKING:
  9. from typing import Final
  10. # Pass as a value within ``headers`` to skip
  11. # emitting some HTTP headers that are added automatically.
  12. # The only headers that are supported are ``Accept-Encoding``,
  13. # ``Host``, and ``User-Agent``.
  14. SKIP_HEADER = "@@@SKIP_HEADER@@@"
  15. SKIPPABLE_HEADERS = frozenset(["accept-encoding", "host", "user-agent"])
  16. ACCEPT_ENCODING = "gzip,deflate"
  17. try:
  18. try:
  19. import brotlicffi as _unused_module_brotli # type: ignore[import-not-found] # noqa: F401
  20. except ImportError:
  21. import brotli as _unused_module_brotli # type: ignore[import-not-found] # noqa: F401
  22. except ImportError:
  23. pass
  24. else:
  25. ACCEPT_ENCODING += ",br"
  26. try:
  27. from compression import ( # type: ignore[import-not-found] # noqa: F401
  28. zstd as _unused_module_zstd,
  29. )
  30. ACCEPT_ENCODING += ",zstd"
  31. except ImportError:
  32. try:
  33. import zstandard as _unused_module_zstd # noqa: F401
  34. ACCEPT_ENCODING += ",zstd"
  35. except ImportError:
  36. pass
  37. class _TYPE_FAILEDTELL(Enum):
  38. token = 0
  39. _FAILEDTELL: Final[_TYPE_FAILEDTELL] = _TYPE_FAILEDTELL.token
  40. _TYPE_BODY_POSITION = typing.Union[int, _TYPE_FAILEDTELL]
  41. # When sending a request with these methods we aren't expecting
  42. # a body so don't need to set an explicit 'Content-Length: 0'
  43. # The reason we do this in the negative instead of tracking methods
  44. # which 'should' have a body is because unknown methods should be
  45. # treated as if they were 'POST' which *does* expect a body.
  46. _METHODS_NOT_EXPECTING_BODY = {"GET", "HEAD", "DELETE", "TRACE", "OPTIONS", "CONNECT"}
  47. def make_headers(
  48. keep_alive: bool | None = None,
  49. accept_encoding: bool | list[str] | str | None = None,
  50. user_agent: str | None = None,
  51. basic_auth: str | None = None,
  52. proxy_basic_auth: str | None = None,
  53. disable_cache: bool | None = None,
  54. ) -> dict[str, str]:
  55. """
  56. Shortcuts for generating request headers.
  57. :param keep_alive:
  58. If ``True``, adds 'connection: keep-alive' header.
  59. :param accept_encoding:
  60. Can be a boolean, list, or string.
  61. ``True`` translates to 'gzip,deflate'. If the dependencies for
  62. Brotli (either the ``brotli`` or ``brotlicffi`` package) and/or Zstandard
  63. (the ``zstandard`` package) algorithms are installed, then their encodings are
  64. included in the string ('br' and 'zstd', respectively).
  65. List will get joined by comma.
  66. String will be used as provided.
  67. :param user_agent:
  68. String representing the user-agent you want, such as
  69. "python-urllib3/0.6"
  70. :param basic_auth:
  71. Colon-separated username:password string for 'authorization: basic ...'
  72. auth header.
  73. :param proxy_basic_auth:
  74. Colon-separated username:password string for 'proxy-authorization: basic ...'
  75. auth header.
  76. :param disable_cache:
  77. If ``True``, adds 'cache-control: no-cache' header.
  78. Example:
  79. .. code-block:: python
  80. import urllib3
  81. print(urllib3.util.make_headers(keep_alive=True, user_agent="Batman/1.0"))
  82. # {'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}
  83. print(urllib3.util.make_headers(accept_encoding=True))
  84. # {'accept-encoding': 'gzip,deflate'}
  85. """
  86. headers: dict[str, str] = {}
  87. if accept_encoding:
  88. if isinstance(accept_encoding, str):
  89. pass
  90. elif isinstance(accept_encoding, list):
  91. accept_encoding = ",".join(accept_encoding)
  92. else:
  93. accept_encoding = ACCEPT_ENCODING
  94. headers["accept-encoding"] = accept_encoding
  95. if user_agent:
  96. headers["user-agent"] = user_agent
  97. if keep_alive:
  98. headers["connection"] = "keep-alive"
  99. if basic_auth:
  100. headers["authorization"] = (
  101. f"Basic {b64encode(basic_auth.encode('latin-1')).decode()}"
  102. )
  103. if proxy_basic_auth:
  104. headers["proxy-authorization"] = (
  105. f"Basic {b64encode(proxy_basic_auth.encode('latin-1')).decode()}"
  106. )
  107. if disable_cache:
  108. headers["cache-control"] = "no-cache"
  109. return headers
  110. def set_file_position(
  111. body: typing.Any, pos: _TYPE_BODY_POSITION | None
  112. ) -> _TYPE_BODY_POSITION | None:
  113. """
  114. If a position is provided, move file to that point.
  115. Otherwise, we'll attempt to record a position for future use.
  116. """
  117. if pos is not None:
  118. rewind_body(body, pos)
  119. elif getattr(body, "tell", None) is not None:
  120. try:
  121. pos = body.tell()
  122. except OSError:
  123. # This differentiates from None, allowing us to catch
  124. # a failed `tell()` later when trying to rewind the body.
  125. pos = _FAILEDTELL
  126. return pos
  127. def rewind_body(body: typing.IO[typing.AnyStr], body_pos: _TYPE_BODY_POSITION) -> None:
  128. """
  129. Attempt to rewind body to a certain position.
  130. Primarily used for request redirects and retries.
  131. :param body:
  132. File-like object that supports seek.
  133. :param int pos:
  134. Position to seek to in file.
  135. """
  136. body_seek = getattr(body, "seek", None)
  137. if body_seek is not None and isinstance(body_pos, int):
  138. try:
  139. body_seek(body_pos)
  140. except OSError as e:
  141. raise UnrewindableBodyError(
  142. "An error occurred when rewinding request body for redirect/retry."
  143. ) from e
  144. elif body_pos is _FAILEDTELL:
  145. raise UnrewindableBodyError(
  146. "Unable to record file position for rewinding "
  147. "request body during a redirect/retry."
  148. )
  149. else:
  150. raise ValueError(
  151. f"body_pos must be of type integer, instead it was {type(body_pos)}."
  152. )
  153. class ChunksAndContentLength(typing.NamedTuple):
  154. chunks: typing.Iterable[bytes] | None
  155. content_length: int | None
  156. def body_to_chunks(
  157. body: typing.Any | None, method: str, blocksize: int
  158. ) -> ChunksAndContentLength:
  159. """Takes the HTTP request method, body, and blocksize and
  160. transforms them into an iterable of chunks to pass to
  161. socket.sendall() and an optional 'Content-Length' header.
  162. A 'Content-Length' of 'None' indicates the length of the body
  163. can't be determined so should use 'Transfer-Encoding: chunked'
  164. for framing instead.
  165. """
  166. chunks: typing.Iterable[bytes] | None
  167. content_length: int | None
  168. # No body, we need to make a recommendation on 'Content-Length'
  169. # based on whether that request method is expected to have
  170. # a body or not.
  171. if body is None:
  172. chunks = None
  173. if method.upper() not in _METHODS_NOT_EXPECTING_BODY:
  174. content_length = 0
  175. else:
  176. content_length = None
  177. # Bytes or strings become bytes
  178. elif isinstance(body, (str, bytes)):
  179. chunks = (to_bytes(body),)
  180. content_length = len(chunks[0])
  181. # File-like object, TODO: use seek() and tell() for length?
  182. elif hasattr(body, "read"):
  183. def chunk_readable() -> typing.Iterable[bytes]:
  184. nonlocal body, blocksize
  185. encode = isinstance(body, io.TextIOBase)
  186. while True:
  187. datablock = body.read(blocksize)
  188. if not datablock:
  189. break
  190. if encode:
  191. datablock = datablock.encode("utf-8")
  192. yield datablock
  193. chunks = chunk_readable()
  194. content_length = None
  195. # Otherwise we need to start checking via duck-typing.
  196. else:
  197. try:
  198. # Check if the body implements the buffer API.
  199. mv = memoryview(body)
  200. except TypeError:
  201. try:
  202. # Check if the body is an iterable
  203. chunks = iter(body)
  204. content_length = None
  205. except TypeError:
  206. raise TypeError(
  207. f"'body' must be a bytes-like object, file-like "
  208. f"object, or iterable. Instead was {body!r}"
  209. ) from None
  210. else:
  211. # Since it implements the buffer API can be passed directly to socket.sendall()
  212. chunks = (body,)
  213. content_length = mv.nbytes
  214. return ChunksAndContentLength(chunks=chunks, content_length=content_length)