~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Tools/Scripts/webkitpy/common/net/file_uploader.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Copyright (C) 2010 Google Inc. All rights reserved.
 
3
#
 
4
# Redistribution and use in source and binary forms, with or without
 
5
# modification, are permitted provided that the following conditions are
 
6
# met:
 
7
#
 
8
#     * Redistributions of source code must retain the above copyright
 
9
# notice, this list of conditions and the following disclaimer.
 
10
#     * Redistributions in binary form must reproduce the above
 
11
# copyright notice, this list of conditions and the following disclaimer
 
12
# in the documentation and/or other materials provided with the
 
13
# distribution.
 
14
#     * Neither the name of Google Inc. nor the names of its
 
15
# contributors may be used to endorse or promote products derived from
 
16
# this software without specific prior written permission.
 
17
#
 
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
import mimetypes
 
31
import time
 
32
import urllib2
 
33
 
 
34
from webkitpy.common.net.networktransaction import NetworkTransaction, NetworkTimeout
 
35
 
 
36
 
 
37
def get_mime_type(filename):
 
38
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
 
39
 
 
40
 
 
41
# FIXME: Rather than taking tuples, this function should take more structured data.
 
42
def _encode_multipart_form_data(fields, files):
 
43
    """Encode form fields for multipart/form-data.
 
44
 
 
45
    Args:
 
46
      fields: A sequence of (name, value) elements for regular form fields.
 
47
      files: A sequence of (name, filename, value) elements for data to be
 
48
             uploaded as files.
 
49
    Returns:
 
50
      (content_type, body) ready for httplib.HTTP instance.
 
51
 
 
52
    Source:
 
53
      http://code.google.com/p/rietveld/source/browse/trunk/upload.py
 
54
    """
 
55
    BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-'
 
56
    CRLF = '\r\n'
 
57
    lines = []
 
58
 
 
59
    for key, value in fields:
 
60
        lines.append('--' + BOUNDARY)
 
61
        lines.append('Content-Disposition: form-data; name="%s"' % key)
 
62
        lines.append('')
 
63
        if isinstance(value, unicode):
 
64
            value = value.encode('utf-8')
 
65
        lines.append(value)
 
66
 
 
67
    for key, filename, value in files:
 
68
        lines.append('--' + BOUNDARY)
 
69
        lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
 
70
        lines.append('Content-Type: %s' % get_mime_type(filename))
 
71
        lines.append('')
 
72
        if isinstance(value, unicode):
 
73
            value = value.encode('utf-8')
 
74
        lines.append(value)
 
75
 
 
76
    lines.append('--' + BOUNDARY + '--')
 
77
    lines.append('')
 
78
    body = CRLF.join(lines)
 
79
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
 
80
    return content_type, body
 
81
 
 
82
 
 
83
class FileUploader(object):
 
84
    def __init__(self, url, timeout_seconds):
 
85
        self._url = url
 
86
        self._timeout_seconds = timeout_seconds
 
87
 
 
88
    def upload_single_text_file(self, filesystem, content_type, filename):
 
89
        return self._upload_data(content_type, filesystem.read_text_file(filename))
 
90
 
 
91
    def upload_as_multipart_form_data(self, filesystem, files, attrs):
 
92
        file_objs = []
 
93
        for filename, path in files:
 
94
            file_objs.append(('file', filename, filesystem.read_binary_file(path)))
 
95
 
 
96
        # FIXME: We should use the same variable names for the formal and actual parameters.
 
97
        content_type, data = _encode_multipart_form_data(attrs, file_objs)
 
98
        return self._upload_data(content_type, data)
 
99
 
 
100
    def _upload_data(self, content_type, data):
 
101
        def callback():
 
102
            # FIXME: Setting a timeout, either globally using socket.setdefaulttimeout()
 
103
            # or in urlopen(), doesn't appear to work on Mac 10.5 with Python 2.7.
 
104
            # For now we will ignore the timeout value and hope for the best.
 
105
            request = urllib2.Request(self._url, data, {"Content-Type": content_type})
 
106
            return urllib2.urlopen(request)
 
107
 
 
108
        return NetworkTransaction(timeout_seconds=self._timeout_seconds).run(callback)