request.py 3.7 KB

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