~rcj/vmbuilder/jenkins_kvm-test

« back to all changes in this revision

Viewing changes to pylib/requests/packages/urllib3/util/request.py

  • Committer: Ben Howard
  • Date: 2014-08-19 20:30:00 UTC
  • Revision ID: ben.howard@ubuntu.com-20140819203000-9gfgaryo1w41orxu
12.04 does not ship with a version of python3-requests, so we need
to provided it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from base64 import b64encode
 
2
 
 
3
from ..packages import six
 
4
 
 
5
 
 
6
ACCEPT_ENCODING = 'gzip,deflate'
 
7
 
 
8
 
 
9
def make_headers(keep_alive=None, accept_encoding=None, user_agent=None,
 
10
                 basic_auth=None, proxy_basic_auth=None):
 
11
    """
 
12
    Shortcuts for generating request headers.
 
13
 
 
14
    :param keep_alive:
 
15
        If ``True``, adds 'connection: keep-alive' header.
 
16
 
 
17
    :param accept_encoding:
 
18
        Can be a boolean, list, or string.
 
19
        ``True`` translates to 'gzip,deflate'.
 
20
        List will get joined by comma.
 
21
        String will be used as provided.
 
22
 
 
23
    :param user_agent:
 
24
        String representing the user-agent you want, such as
 
25
        "python-urllib3/0.6"
 
26
 
 
27
    :param basic_auth:
 
28
        Colon-separated username:password string for 'authorization: basic ...'
 
29
        auth header.
 
30
 
 
31
    :param proxy_basic_auth:
 
32
        Colon-separated username:password string for 'proxy-authorization: basic ...'
 
33
        auth header.
 
34
 
 
35
    Example: ::
 
36
 
 
37
        >>> make_headers(keep_alive=True, user_agent="Batman/1.0")
 
38
        {'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}
 
39
        >>> make_headers(accept_encoding=True)
 
40
        {'accept-encoding': 'gzip,deflate'}
 
41
    """
 
42
    headers = {}
 
43
    if accept_encoding:
 
44
        if isinstance(accept_encoding, str):
 
45
            pass
 
46
        elif isinstance(accept_encoding, list):
 
47
            accept_encoding = ','.join(accept_encoding)
 
48
        else:
 
49
            accept_encoding = ACCEPT_ENCODING
 
50
        headers['accept-encoding'] = accept_encoding
 
51
 
 
52
    if user_agent:
 
53
        headers['user-agent'] = user_agent
 
54
 
 
55
    if keep_alive:
 
56
        headers['connection'] = 'keep-alive'
 
57
 
 
58
    if basic_auth:
 
59
        headers['authorization'] = 'Basic ' + \
 
60
            b64encode(six.b(basic_auth)).decode('utf-8')
 
61
 
 
62
    if proxy_basic_auth:
 
63
        headers['proxy-authorization'] = 'Basic ' + \
 
64
            b64encode(six.b(proxy_basic_auth)).decode('utf-8')
 
65
 
 
66
    return headers
 
67
 
 
68