Development of an internal social media platform with personalised dashboards for students
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 3.6KB

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