~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/apiclient/multipart.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Encoding of MIME multipart data."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    'encode_multipart_data',
 
15
    ]
 
16
 
 
17
import mimetypes
 
18
import random
 
19
import string
 
20
 
 
21
 
 
22
def make_random_boundary(length=30):
 
23
    """Create a random string for use in MIME boundary lines."""
 
24
    return b''.join(random.choice(string.letters) for ii in range(length))
 
25
 
 
26
 
 
27
def get_content_type(filename):
 
28
    """Return the MIME content type for the file with the given name."""
 
29
    return mimetypes.guess_type(filename)[0] or b'application/octet-stream'
 
30
 
 
31
 
 
32
def encode_field(field_name, data, boundary):
 
33
    """MIME-encode a form field."""
 
34
    field_name = field_name.encode('ascii')
 
35
    return (
 
36
        b'--' + boundary,
 
37
        b'Content-Disposition: form-data; name="%s"' % field_name,
 
38
        b'',
 
39
        bytes(data),
 
40
        )
 
41
 
 
42
 
 
43
def encode_file(name, fileObj, boundary):
 
44
    """MIME-encode a file upload."""
 
45
    content_type = get_content_type(name)
 
46
    name = name.encode('ascii')
 
47
    return (
 
48
        b'--' + boundary,
 
49
        b'Content-Disposition: form-data; name="%s"; filename="%s"' %
 
50
            (name, name),
 
51
        b'Content-Type: %s' % content_type,
 
52
        b'',
 
53
        fileObj.read(),
 
54
        )
 
55
 
 
56
 
 
57
def encode_multipart_data(data, files):
 
58
    """Create a MIME multipart payload from L{data} and L{files}.
 
59
 
 
60
    @param data: A mapping of names (ASCII strings) to data (byte string).
 
61
    @param files: A mapping of names (ASCII strings) to file objects ready to
 
62
        be read.
 
63
    @return: A 2-tuple of C{(body, headers)}, where C{body} is a a byte string
 
64
        and C{headers} is a dict of headers to add to the enclosing request in
 
65
        which this payload will travel.
 
66
    """
 
67
    boundary = make_random_boundary()
 
68
 
 
69
    lines = []
 
70
    for name, content in data.items():
 
71
        lines.extend(encode_field(name, content, boundary))
 
72
    for name, file_obj in files.items():
 
73
        lines.extend(encode_file(name, file_obj, boundary))
 
74
    lines.extend((b'--%s--' % boundary, b''))
 
75
    body = b'\r\n'.join(lines)
 
76
 
 
77
    headers = {
 
78
        b'content-type': b'multipart/form-data; boundary=' + boundary,
 
79
        b'content-length': b'%s' % (len(body)),
 
80
        }
 
81
 
 
82
    return body, headers