LEFT | RIGHT |
(no file at all) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This Source Code is subject to the terms of the Mozilla Public License |
| 4 # version 2.0 (the "License"). You can obtain a copy of the License at |
| 5 # http://mozilla.org/MPL/2.0/. |
| 6 |
| 7 import re, os, sys |
| 8 from urlparse import parse_qs |
| 9 from tempfile import mkstemp |
| 10 from sitescripts.utils import get_config, setupStderr |
| 11 from sitescripts.web import url_handler |
| 12 |
| 13 @url_handler('/submitCrash') |
| 14 def handleRequest(environ, start_response): |
| 15 setupStderr(environ['wsgi.errors']) |
| 16 |
| 17 if not environ.get('HTTP_X_ADBLOCK_PLUS'): |
| 18 return showError('Please use Adblock Plus to submit crashes', start_response
) |
| 19 |
| 20 if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYP
E', '').startswith('text/xml'): |
| 21 return showError('Unsupported request method', start_response) |
| 22 |
| 23 params = parse_qs(environ.get('QUERY_STRING', '')) |
| 24 |
| 25 requestVersion = params.get('version', ['0'])[0] |
| 26 if requestVersion != '1': |
| 27 return showError('Unsupported request version', start_response) |
| 28 |
| 29 try: |
| 30 request_body_size = int(environ.get('CONTENT_LENGTH', 0)) |
| 31 except (ValueError): |
| 32 return showError('No content', start_response) |
| 33 |
| 34 dir = get_config().get('crashes', 'dataPath') |
| 35 if not os.path.exists(dir): |
| 36 os.makedirs(dir) |
| 37 |
| 38 filename = None |
| 39 try: |
| 40 fd, filename = mkstemp('.xml.tmp', 'crash_', dir) |
| 41 file = os.fdopen(fd, 'wb') |
| 42 file.write(environ['wsgi.input'].read(request_body_size)) |
| 43 file.close() |
| 44 os.rename(filename, os.path.splitext(filename)[0]); |
| 45 except Exception, e: |
| 46 if filename != None and os.path.isfile(filename): |
| 47 os.remove(filename) |
| 48 raise e |
| 49 |
| 50 start_response('200 Ok', [('Content-Type', 'text/plain; charset=utf-8')]) |
| 51 return ['saved'.encode('utf-8')] |
| 52 |
| 53 def showError(message, start_response): |
| 54 start_response('400 Processing Error', [('Content-Type', 'text/plain; charset=
utf-8')]) |
| 55 return [message.encode('utf-8')] |
LEFT | RIGHT |