Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

request.py 7.9KB

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