request.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from __future__ import absolute_import
  2. from base64 import b64encode
  3. from ..exceptions import UnrewindableBodyError
  4. from ..packages.six import b, integer_types
  5. # Pass as a value within ``headers`` to skip
  6. # emitting some HTTP headers that are added automatically.
  7. # The only headers that are supported are ``Accept-Encoding``,
  8. # ``Host``, and ``User-Agent``.
  9. SKIP_HEADER = "@@@SKIP_HEADER@@@"
  10. SKIPPABLE_HEADERS = frozenset(["accept-encoding", "host", "user-agent"])
  11. ACCEPT_ENCODING = "gzip,deflate"
  12. try:
  13. try:
  14. import brotlicffi as _unused_module_brotli # noqa: F401
  15. except ImportError:
  16. import brotli as _unused_module_brotli # noqa: F401
  17. except ImportError:
  18. pass
  19. else:
  20. ACCEPT_ENCODING += ",br"
  21. _FAILEDTELL = object()
  22. def make_headers(
  23. keep_alive=None,
  24. accept_encoding=None,
  25. user_agent=None,
  26. basic_auth=None,
  27. proxy_basic_auth=None,
  28. disable_cache=None,
  29. ):
  30. """
  31. Shortcuts for generating request headers.
  32. :param keep_alive:
  33. If ``True``, adds 'connection: keep-alive' header.
  34. :param accept_encoding:
  35. Can be a boolean, list, or string.
  36. ``True`` translates to 'gzip,deflate'.
  37. List will get joined by comma.
  38. String will be used as provided.
  39. :param user_agent:
  40. String representing the user-agent you want, such as
  41. "python-urllib3/0.6"
  42. :param basic_auth:
  43. Colon-separated username:password string for 'authorization: basic ...'
  44. auth header.
  45. :param proxy_basic_auth:
  46. Colon-separated username:password string for 'proxy-authorization: basic ...'
  47. auth header.
  48. :param disable_cache:
  49. If ``True``, adds 'cache-control: no-cache' header.
  50. Example::
  51. >>> make_headers(keep_alive=True, user_agent="Batman/1.0")
  52. {'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}
  53. >>> make_headers(accept_encoding=True)
  54. {'accept-encoding': 'gzip,deflate'}
  55. """
  56. headers = {}
  57. if accept_encoding:
  58. if isinstance(accept_encoding, str):
  59. pass
  60. elif isinstance(accept_encoding, list):
  61. accept_encoding = ",".join(accept_encoding)
  62. else:
  63. accept_encoding = ACCEPT_ENCODING
  64. headers["accept-encoding"] = accept_encoding
  65. if user_agent:
  66. headers["user-agent"] = user_agent
  67. if keep_alive:
  68. headers["connection"] = "keep-alive"
  69. if basic_auth:
  70. headers["authorization"] = "Basic " + b64encode(b(basic_auth)).decode("utf-8")
  71. if proxy_basic_auth:
  72. headers["proxy-authorization"] = "Basic " + b64encode(
  73. b(proxy_basic_auth)
  74. ).decode("utf-8")
  75. if disable_cache:
  76. headers["cache-control"] = "no-cache"
  77. return headers
  78. def set_file_position(body, pos):
  79. """
  80. If a position is provided, move file to that point.
  81. Otherwise, we'll attempt to record a position for future use.
  82. """
  83. if pos is not None:
  84. rewind_body(body, pos)
  85. elif getattr(body, "tell", None) is not None:
  86. try:
  87. pos = body.tell()
  88. except (IOError, OSError):
  89. # This differentiates from None, allowing us to catch
  90. # a failed `tell()` later when trying to rewind the body.
  91. pos = _FAILEDTELL
  92. return pos
  93. def rewind_body(body, body_pos):
  94. """
  95. Attempt to rewind body to a certain position.
  96. Primarily used for request redirects and retries.
  97. :param body:
  98. File-like object that supports seek.
  99. :param int pos:
  100. Position to seek to in file.
  101. """
  102. body_seek = getattr(body, "seek", None)
  103. if body_seek is not None and isinstance(body_pos, integer_types):
  104. try:
  105. body_seek(body_pos)
  106. except (IOError, OSError):
  107. raise UnrewindableBodyError(
  108. "An error occurred when rewinding request body for redirect/retry."
  109. )
  110. elif body_pos is _FAILEDTELL:
  111. raise UnrewindableBodyError(
  112. "Unable to record file position for rewinding "
  113. "request body during a redirect/retry."
  114. )
  115. else:
  116. raise ValueError(
  117. "body_pos must be of type integer, instead it was %s." % type(body_pos)
  118. )