2
# Copyright (C) 2010 Google Inc. All rights reserved.
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are
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
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.
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.
34
from webkitpy.common.net.networktransaction import NetworkTransaction, NetworkTimeout
37
def get_mime_type(filename):
38
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
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.
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
50
(content_type, body) ready for httplib.HTTP instance.
53
http://code.google.com/p/rietveld/source/browse/trunk/upload.py
55
BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-'
59
for key, value in fields:
60
lines.append('--' + BOUNDARY)
61
lines.append('Content-Disposition: form-data; name="%s"' % key)
63
if isinstance(value, unicode):
64
value = value.encode('utf-8')
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))
72
if isinstance(value, unicode):
73
value = value.encode('utf-8')
76
lines.append('--' + BOUNDARY + '--')
78
body = CRLF.join(lines)
79
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
80
return content_type, body
83
class FileUploader(object):
84
def __init__(self, url, timeout_seconds):
86
self._timeout_seconds = timeout_seconds
88
def upload_single_text_file(self, filesystem, content_type, filename):
89
return self._upload_data(content_type, filesystem.read_text_file(filename))
91
def upload_as_multipart_form_data(self, filesystem, files, attrs):
93
for filename, path in files:
94
file_objs.append(('file', filename, filesystem.read_binary_file(path)))
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)
100
def _upload_data(self, content_type, data):
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)
108
return NetworkTransaction(timeout_seconds=self._timeout_seconds).run(callback)