Path: blob/master/venv/Lib/site-packages/urllib3/util/request.py
811 views
from __future__ import absolute_import1from base64 import b64encode23from ..packages.six import b, integer_types4from ..exceptions import UnrewindableBodyError56ACCEPT_ENCODING = "gzip,deflate"7try:8import brotli as _unused_module_brotli # noqa: F4019except ImportError:10pass11else:12ACCEPT_ENCODING += ",br"1314_FAILEDTELL = object()151617def make_headers(18keep_alive=None,19accept_encoding=None,20user_agent=None,21basic_auth=None,22proxy_basic_auth=None,23disable_cache=None,24):25"""26Shortcuts for generating request headers.2728:param keep_alive:29If ``True``, adds 'connection: keep-alive' header.3031:param accept_encoding:32Can be a boolean, list, or string.33``True`` translates to 'gzip,deflate'.34List will get joined by comma.35String will be used as provided.3637:param user_agent:38String representing the user-agent you want, such as39"python-urllib3/0.6"4041:param basic_auth:42Colon-separated username:password string for 'authorization: basic ...'43auth header.4445:param proxy_basic_auth:46Colon-separated username:password string for 'proxy-authorization: basic ...'47auth header.4849:param disable_cache:50If ``True``, adds 'cache-control: no-cache' header.5152Example::5354>>> make_headers(keep_alive=True, user_agent="Batman/1.0")55{'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}56>>> make_headers(accept_encoding=True)57{'accept-encoding': 'gzip,deflate'}58"""59headers = {}60if accept_encoding:61if isinstance(accept_encoding, str):62pass63elif isinstance(accept_encoding, list):64accept_encoding = ",".join(accept_encoding)65else:66accept_encoding = ACCEPT_ENCODING67headers["accept-encoding"] = accept_encoding6869if user_agent:70headers["user-agent"] = user_agent7172if keep_alive:73headers["connection"] = "keep-alive"7475if basic_auth:76headers["authorization"] = "Basic " + b64encode(b(basic_auth)).decode("utf-8")7778if proxy_basic_auth:79headers["proxy-authorization"] = "Basic " + b64encode(80b(proxy_basic_auth)81).decode("utf-8")8283if disable_cache:84headers["cache-control"] = "no-cache"8586return headers878889def set_file_position(body, pos):90"""91If a position is provided, move file to that point.92Otherwise, we'll attempt to record a position for future use.93"""94if pos is not None:95rewind_body(body, pos)96elif getattr(body, "tell", None) is not None:97try:98pos = body.tell()99except (IOError, OSError):100# This differentiates from None, allowing us to catch101# a failed `tell()` later when trying to rewind the body.102pos = _FAILEDTELL103104return pos105106107def rewind_body(body, body_pos):108"""109Attempt to rewind body to a certain position.110Primarily used for request redirects and retries.111112:param body:113File-like object that supports seek.114115:param int pos:116Position to seek to in file.117"""118body_seek = getattr(body, "seek", None)119if body_seek is not None and isinstance(body_pos, integer_types):120try:121body_seek(body_pos)122except (IOError, OSError):123raise UnrewindableBodyError(124"An error occurred when rewinding request body for redirect/retry."125)126elif body_pos is _FAILEDTELL:127raise UnrewindableBodyError(128"Unable to record file position for rewinding "129"request body during a redirect/retry."130)131else:132raise ValueError(133"body_pos must be of type integer, instead it was %s." % type(body_pos)134)135136137