request.py 3.7 KB

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