OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2016 Eyeo GmbH | 4 # Copyright (C) 2006-2016 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import re, os, sys | 18 import re, os, sys |
19 from urlparse import parse_qs | 19 from urlparse import parse_qs |
20 from sitescripts.utils import get_config, get_template, setupStderr | 20 from sitescripts.utils import get_config, get_template |
21 from sitescripts.web import url_handler | 21 from sitescripts.web import url_handler |
22 import sitescripts.subscriptions.knownIssuesParser as knownIssuesParser | 22 import sitescripts.subscriptions.knownIssuesParser as knownIssuesParser |
23 | 23 |
24 def dataIterator(source, file): | |
25 for line in source: | |
26 file.write(line) | |
27 yield line | |
28 | |
29 @url_handler('/submitReport') | 24 @url_handler('/submitReport') |
30 def handleRequest(environ, start_response): | 25 def handleRequest(environ, start_response): |
31 setupStderr(environ['wsgi.errors']) | |
32 | |
33 if not environ.get('HTTP_X_ADBLOCK_PLUS'): | 26 if not environ.get('HTTP_X_ADBLOCK_PLUS'): |
34 return showError('Please use Adblock Plus to submit reports', start_response
) | 27 return showError('Please use Adblock Plus to submit reports', start_response
) |
35 | 28 |
36 if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYP
E', '').startswith('text/xml'): | 29 if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYP
E', '').startswith('text/xml'): |
37 return showError('Unsupported request method', start_response) | 30 return showError('Unsupported request method', start_response) |
38 | 31 |
39 params = parse_qs(environ.get('QUERY_STRING', '')) | 32 params = parse_qs(environ.get('QUERY_STRING', '')) |
40 | 33 |
41 requestVersion = params.get('version', ['0'])[0] | 34 requestVersion = params.get('version', ['0'])[0] |
42 if requestVersion != '1': | 35 if requestVersion != '1': |
43 return showError('Unsupported request version', start_response) | 36 return showError('Unsupported request version', start_response) |
44 | 37 |
45 guid = params.get('guid', [''])[0].lower() | 38 guid = params.get('guid', [''])[0].lower() |
46 if not re.match(r'^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$',
guid): | 39 if not re.match(r'^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$',
guid): |
47 return showError('Invalid or missing GUID', start_response) | 40 return showError('Invalid or missing GUID', start_response) |
48 | 41 |
49 path = os.path.join(get_config().get('reports', 'dataPath'), guid + '.xml') | 42 path = os.path.join(get_config().get('reports', 'dataPath'), guid + '.xml') |
50 if os.path.exists(path) or os.path.exists(path + '.tmp'): | 43 if os.path.exists(path) or os.path.exists(path + '.tmp'): |
51 return showError('Duplicate GUID', start_response) | 44 return showError('Duplicate GUID', start_response) |
52 | 45 |
| 46 try: |
| 47 request_size= int(environ['CONTENT_LENGTH']) |
| 48 except (KeyError, ValueError): |
| 49 return showError('Invalid or missing Content-Length header', start_response, |
| 50 '411 Length Required') |
| 51 |
53 dir = os.path.dirname(path) | 52 dir = os.path.dirname(path) |
54 if not os.path.exists(dir): | 53 if not os.path.exists(dir): |
55 os.makedirs(dir) | 54 os.makedirs(dir) |
56 try: | 55 try: |
57 file = open(path + '.tmp', 'wb') | 56 file = open(path + '.tmp', 'wb') |
58 iter = dataIterator(environ['wsgi.input'], file) | 57 data = environ['wsgi.input'].read(request_size) |
59 knownIssues = knownIssuesParser.findMatches(iter, params.get('lang', ['en-US
'])[0]) | 58 file.write(data) |
60 file.close() | 59 file.close() |
61 | 60 |
| 61 knownIssues = knownIssuesParser.findMatches(data.splitlines(), params.get('l
ang', ['en-US'])[0]) |
| 62 |
62 os.rename(path + '.tmp', path); | 63 os.rename(path + '.tmp', path); |
63 except Exception, e: | 64 except Exception, e: |
64 if os.path.isfile(path + '.tmp'): | 65 if os.path.isfile(path + '.tmp'): |
65 os.remove(path + '.tmp') | 66 os.remove(path + '.tmp') |
66 raise e | 67 raise e |
67 | 68 |
68 template = get_template(get_config().get('reports', 'submitResponseTemplate')) | 69 template = get_template(get_config().get('reports', 'submitResponseTemplate')) |
69 start_response('200 OK', [('Content-Type', 'application/xhtml+xml; charset=utf
-8')]) | 70 start_response('200 OK', [('Content-Type', 'application/xhtml+xml; charset=utf
-8')]) |
70 return [template.render({'url': get_config().get('reports', 'urlRoot') + guid,
'knownIssues': knownIssues}).encode('utf-8')] | 71 return [template.render({'url': get_config().get('reports', 'urlRoot') + guid,
'knownIssues': knownIssues}).encode('utf-8')] |
71 | 72 |
72 def showError(message, start_response): | 73 def showError(message, start_response, response_code='400 Processing Error'): |
73 template = get_template(get_config().get('reports', 'errorTemplate')) | 74 template = get_template(get_config().get('reports', 'errorTemplate')) |
74 start_response('400 Processing Error', [('Content-Type', 'application/xhtml+xm
l; charset=utf-8')]) | 75 start_response(response_code, [('Content-Type', 'application/xhtml+xml; charse
t=utf-8')]) |
75 return [template.render({'message': message}).encode('utf-8')] | 76 return [template.render({'message': message}).encode('utf-8')] |
OLD | NEW |