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.
30 from __future__ import with_statement
37 from webkitpy.common.net.networktransaction import NetworkTransaction
39 def get_mime_type(filename):
40 return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
43 def _encode_multipart_form_data(fields, files):
44 """Encode form fields for multipart/form-data.
47 fields: A sequence of (name, value) elements for regular form fields.
48 files: A sequence of (name, filename, value) elements for data to be
51 (content_type, body) ready for httplib.HTTP instance.
54 http://code.google.com/p/rietveld/source/browse/trunk/upload.py
56 BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-'
60 for key, value in fields:
61 lines.append('--' + BOUNDARY)
62 lines.append('Content-Disposition: form-data; name="%s"' % key)
64 if isinstance(value, unicode):
65 value = value.encode('utf-8')
68 for key, filename, value in files:
69 lines.append('--' + BOUNDARY)
70 lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
71 lines.append('Content-Type: %s' % get_mime_type(filename))
73 if isinstance(value, unicode):
74 value = value.encode('utf-8')
77 lines.append('--' + BOUNDARY + '--')
79 body = CRLF.join(lines)
80 content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
81 return content_type, body
84 class TestResultsUploader:
85 def __init__(self, host):
88 def _upload_files(self, attrs, file_objs):
89 url = "http://%s/testfile/upload" % self._host
90 content_type, data = _encode_multipart_form_data(attrs, file_objs)
91 headers = {"Content-Type": content_type}
92 request = urllib2.Request(url, data, headers)
93 urllib2.urlopen(request)
95 def upload(self, params, files, timeout_seconds):
97 for filename, path in files:
98 with codecs.open(path, "rb") as file:
99 file_objs.append(('file', filename, file.read()))
101 orig_timeout = socket.getdefaulttimeout()
103 socket.setdefaulttimeout(timeout_seconds)
104 NetworkTransaction(timeout_seconds=timeout_seconds).run(
105 lambda: self._upload_files(params, file_objs))
107 socket.setdefaulttimeout(orig_timeout)