2 # Copyright (C) 2012 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.
31 from google.appengine.ext import db
35 from datetime import datetime
37 from models import ReportLog
38 from controller import schedule_report_process
41 class ReportHandler(webapp2.RequestHandler):
43 self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
45 headers = "\n".join([key + ': ' + value for key, value in self.request.headers.items()])
47 # Do as best as we can to remove the password
48 request_body_without_password = re.sub(r',\s*"password"\s*:\s*".+?"', '', self.request.body)
49 log = ReportLog(timestamp=datetime.now(), headers=headers, payload=request_body_without_password)
52 self._encountered_error = False
55 parsedPayload = json.loads(self.request.body)
56 password = parsedPayload.get('password', '')
58 return self._output('Failed to parse the payload as a json. Report key: %d' % log.key().id())
60 builder = log.builder()
61 builder != None or self._output('No builder named "%s"' % log.get_value('builder-name'))
62 log.branch() != None or self._output('No branch named "%s"' % log.get_value('branch'))
63 log.platform() != None or self._output('No platform named "%s"' % log.get_value('platform'))
64 log.build_number() != None or self._output('Invalid build number "%s"' % log.get_value('build-number'))
65 log.timestamp() != None or self._output('Invalid timestamp "%s"' % log.get_value('timestamp'))
66 log.webkit_revision() != None or self._output('Invalid webkit revision "%s"' % log.get_value('webkit-revision'))
69 if builder and not (self.bypass_authentication() or builder.authenticate(password)):
70 self._output('Authentication failed')
72 if not self._results_are_valid(log):
73 self._output("The payload doesn't contain results or results are malformed")
75 if self._encountered_error:
80 schedule_report_process(log)
83 def _output(self, message):
84 self._encountered_error = True
85 self.response.out.write(message + '\n')
87 def bypass_authentication(self):
90 def _results_are_valid(self, log):
92 def _is_float_convertible(value):
101 if not isinstance(log.results(), dict):
104 for testResult in log.results().values():
105 if isinstance(testResult, dict):
106 for value in testResult.values():
107 if not _is_float_convertible(value):
109 if 'avg' not in testResult:
112 if not _is_float_convertible(testResult):
118 class AdminReportHandler(ReportHandler):
119 def bypass_authentication(self):