Left: | ||
Right: |
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-2014 Eyeo GmbH | 4 # Copyright (C) 2006-2015 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 MySQLdb, json, os | 18 import MySQLdb, json, os |
19 from datetime import datetime | 19 from datetime import datetime |
20 from sitescripts.web import url_handler | 20 from sitescripts.web import url_handler |
21 from sitescripts.utils import get_config, setupStderr | 21 from sitescripts.utils import get_config, setupStderr |
22 | 22 |
23 import sitescripts.filterhits.common as common | 23 import sitescripts.filterhits.common as common |
24 import sitescripts.filterhits.db as db | 24 import sitescripts.filterhits.db as db |
25 import sitescripts.filterhits.geometrical_mean as geometrical_mean | 25 import sitescripts.filterhits.geometrical_mean as geometrical_mean |
26 | 26 |
27 @url_handler("/submit") | 27 @url_handler("/submit") |
28 def submit(environ, start_response): | 28 def submit(environ, start_response): |
29 setupStderr(environ["wsgi.errors"]) | 29 setupStderr(environ["wsgi.errors"]) |
30 config = get_config() | 30 config = get_config() |
31 | 31 |
32 # Check that this is a POST request | 32 # Check that this is a POST request |
33 if environ["REQUEST_METHOD"].upper() != "POST": | 33 if environ["REQUEST_METHOD"] != "POST": |
34 return common.showError("Unsupported request method", start_response) | 34 return common.showError("Unsupported request method", start_response) |
35 | 35 |
36 # Parse the submitted JSON | 36 # Parse the submitted JSON |
37 data = "{}" | 37 data = "{}" |
38 try: | 38 try: |
39 data_length = int(environ.get("CONTENT_LENGTH", "0")) | 39 data_length = int(environ["CONTENT_LENGTH"]) |
40 except ValueError: | 40 except (ValueError, KeyError): |
41 data_length = 0 | 41 data_length = 0 |
42 if data_length != 0: | 42 if data_length != 0: |
43 data = environ["wsgi.input"].read(data_length) | 43 data = environ["wsgi.input"].read(data_length) |
44 try: | 44 try: |
45 data = json.loads(data) | 45 data = json.loads(data) |
Sebastian Noack
2015/02/17 14:59:17
It seems that parsing an empty dictionary is unnec
Sebastian Noack
2015/02/17 17:09:47
data = {}
try:
data_length = int(environ["CONTEN
Sebastian Noack
2015/02/17 17:16:57
Possibly you could also just use json.load():
try
kzar
2015/02/24 18:05:11
Done.
| |
46 except json.decoder.JSONDecodeError: | 46 except json.decoder.JSONDecodeError: |
47 return common.showError("Error while parsing JSON data.", start_response) | 47 return common.showError("Error while parsing JSON data.", start_response) |
48 | 48 |
49 # Make sure it looks roughly valid | |
50 if not common.valid_log_data(data): | |
51 return common.showError("Data looks invalid.", start_response) | |
52 | |
53 # Log the data to a file | 49 # Log the data to a file |
54 log_dir = config.get("filterhitstats", "log_dir") | 50 log_dir = config.get("filterhitstats", "log_dir") |
55 try: | 51 try: |
56 log_file = common.log_filterhits(data, log_dir, | 52 log_file = common.log_filterhits(data, log_dir, |
57 environ.get("QUERY_STRING", "")) | 53 environ.get("QUERY_STRING", "")) |
58 except OSError, IOError: | 54 except (OSError, IOError): |
59 return common.showError("Failed to write data to log file!", start_response, | 55 return common.showError("Failed to write data to log file!", start_response, |
60 "500 Logging error") | 56 "500 Logging error") |
61 | 57 |
62 # Update the geometrical_mean aggregations in the database | 58 # Update the geometrical_mean aggregations in the database |
63 interval = config.get("filterhitstats", "interval") | 59 interval = config.get("filterhitstats", "interval") |
64 try: | 60 try: |
65 db.connect(config.get("filterhitstats", "dbuser"), | 61 db_connection = db.connect(config.get("filterhitstats", "dbuser"), |
66 config.get("filterhitstats", "dbpassword"), | 62 config.get("filterhitstats", "dbpassword"), |
67 config.get("filterhitstats", "database")) | 63 config.get("filterhitstats", "database")) |
68 db.write(geometrical_mean.update(interval, data)) | 64 db.write(db_connection, geometrical_mean.update(interval, data)) |
69 except MySQLdb.Error, e: | 65 except MySQLdb.Error, e: |
70 # Updating the aggregations in the database failed for whatever reason, | 66 # Updating the aggregations in the database failed for whatever reason, |
71 # log the details but continue to return 200 OK to the client to avoid | 67 # log the details but continue to return 200 OK to the client to avoid |
72 # re-transmission of the data. | 68 # re-transmission of the data. |
73 mysql_error_log = os.path.join(config.get("filterhitstats", "log_dir"), | 69 mysql_error_log = os.path.join(config.get("filterhitstats", "log_dir"), |
74 "mysql-errors.log") | 70 "mysql-errors.log") |
75 with open(mysql_error_log, "a+") as f: | 71 with open(mysql_error_log, "a+") as f: |
76 f.write("[%s] MySQL error (%d) when processing data file %s: \"%s\"\n" % ( | 72 f.write("[%s] MySQL error (%d) when processing data file %s: \"%s\"\n" % ( |
77 datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z'), | 73 datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z'), |
78 e.args[0], log_file, e.args[1] | 74 e.args[0], log_file, e.args[1] |
79 )) | 75 )) |
80 finally: | 76 finally: |
81 db.disconnect() | 77 if db_connection: |
Sebastian Noack
2015/02/17 14:59:17
This will result in a NameError, in case db_connec
kzar
2015/02/24 18:05:11
Done.
| |
78 db_connection.close() | |
82 | 79 |
83 # Send back a 200 OK response | 80 # Send back a 200 OK response |
84 response_headers = [("Content-type", "text/plain")] | 81 response_headers = [("Content-type", "text/plain")] |
85 start_response("200 OK", response_headers) | 82 start_response("200 OK", response_headers) |
86 return [] | 83 return [] |
OLD | NEW |