aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Copeland <ben.copeland@linaro.org>2016-12-09 16:06:37 +0000
committerBen Copeland <ben.copeland@linaro.org>2016-12-15 13:45:37 +0000
commit39994d86e93a1e23172d69442ad35d07d906c1ee (patch)
tree7b9c2920169ede0eba7c7ea1ab96ed741a772ea9
parent97b80330deb9f65cf151912141b01d5f51323693 (diff)
reporting: Add HTTP_REFERER tracking
When a download happens, it is necessary to catch the location the user is downloading from. We will get outside links if people are hotlinking, the download URL. Change-Id: I4af652e103ddb22c22c3ce447ba6e77f91768a0f Reviewed-on: https://review.linaro.org/16266 Reviewed-by: Andy Doan <andy.doan@linaro.org>
-rw-r--r--license_protected_downloads/management/commands/report_process.py3
-rw-r--r--license_protected_downloads/migrations/0002_download_ref.py19
-rw-r--r--license_protected_downloads/models.py5
-rw-r--r--license_protected_downloads/tests/test_views.py2
4 files changed, 27 insertions, 2 deletions
diff --git a/license_protected_downloads/management/commands/report_process.py b/license_protected_downloads/management/commands/report_process.py
index c901d30..4382b14 100644
--- a/license_protected_downloads/management/commands/report_process.py
+++ b/license_protected_downloads/management/commands/report_process.py
@@ -46,7 +46,8 @@ class Command(BaseCommand):
# in them and we need the real file name
download = row[1].replace('\n', '')
Download.objects.create(
- ip=row[0], name=download, link=str2bool(row[2]))
+ ip=row[0], name=download, link=str2bool(row[2]),
+ ref=row[3])
os.remove(name)
except (csv.Error, DatabaseError):
logging.exception('unable to process csv %s', name)
diff --git a/license_protected_downloads/migrations/0002_download_ref.py b/license_protected_downloads/migrations/0002_download_ref.py
new file mode 100644
index 0000000..c812057
--- /dev/null
+++ b/license_protected_downloads/migrations/0002_download_ref.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('license_protected_downloads', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='download',
+ name='ref',
+ field=models.CharField(max_length=256, null=True, blank=True),
+ ),
+ ]
diff --git a/license_protected_downloads/models.py b/license_protected_downloads/models.py
index 2df5009..18f8c67 100644
--- a/license_protected_downloads/models.py
+++ b/license_protected_downloads/models.py
@@ -138,6 +138,7 @@ class Download(models.Model):
country = models.CharField(max_length=256, blank=True, null=True)
region_isp = models.CharField(max_length=256, blank=True, null=True)
+ ref = models.CharField(max_length=256, blank=True, null=True)
@staticmethod
def mark(request, artifact):
@@ -154,9 +155,11 @@ class Download(models.Model):
ip = get_ip(request)
name = artifact.get_real_name()
link = name != artifact.url()
+ http_ref = request.META.get('HTTP_REFERER', '/')
+ ref = http_ref.replace('\n', '').replace(',', '')
with open(settings.REPORT_CSV, "a") as f:
writer = csv.writer(f)
- writer.writerow([ip, name, link])
+ writer.writerow([ip, name, link, ref])
except:
logging.exception('unable to mark download')
diff --git a/license_protected_downloads/tests/test_views.py b/license_protected_downloads/tests/test_views.py
index 715cf3b..d24519a 100644
--- a/license_protected_downloads/tests/test_views.py
+++ b/license_protected_downloads/tests/test_views.py
@@ -569,6 +569,7 @@ class ViewTests(BaseServeViewTest):
self.assertEqual('/build-info/panda-open.txt', row[1])
self.assertEqual('127.0.0.1', row[0])
self.assertEqual('False', row[2])
+ self.assertEqual('/', row[3])
# Process CSV into DB and check data
call_command('report_process')
@@ -577,6 +578,7 @@ class ViewTests(BaseServeViewTest):
self.assertEqual('/build-info/panda-open.txt', downloads[0].name)
self.assertEqual('127.0.0.1', downloads[0].ip)
self.assertFalse(downloads[0].link)
+ self.assertEqual('/', downloads[0].ref)
class HowtoViewTests(BaseServeViewTest):