Browse Source

Make ua column wider, and truncate its values for very long UA strings

pull/7/head
Pēteris Caune 9 years ago
parent
commit
f640b9f3be
4 changed files with 48 additions and 3 deletions
  1. +19
    -0
      hc/api/migrations/0008_auto_20150801_1213.py
  2. +1
    -1
      hc/api/models.py
  3. +26
    -1
      hc/api/tests/test_ping.py
  4. +2
    -1
      hc/api/views.py

+ 19
- 0
hc/api/migrations/0008_auto_20150801_1213.py View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0007_ping'),
]
operations = [
migrations.AlterField(
model_name='ping',
name='ua',
field=models.CharField(max_length=200, blank=True),
),
]

+ 1
- 1
hc/api/models.py View File

@ -65,5 +65,5 @@ class Ping(models.Model):
created = models.DateTimeField(auto_now_add=True)
remote_addr = models.GenericIPAddressField()
method = models.CharField(max_length=10)
ua = models.CharField(max_length=100, blank=True)
ua = models.CharField(max_length=200, blank=True)
body = models.TextField(blank=True)

+ 26
- 1
hc/api/tests/test_ping.py View File

@ -1,6 +1,6 @@
from django.test import Client, TestCase
from hc.api.models import Check
from hc.api.models import Check, Ping
class PingTestCase(TestCase):
@ -26,3 +26,28 @@ class PingTestCase(TestCase):
def test_it_handles_bad_uuid(self):
r = self.client.get("/ping/not-uuid/")
assert r.status_code == 400
def test_it_handles_120_char_ua(self):
ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/44.0.2403.89 Safari/537.36")
check = Check()
check.save()
r = self.client.get("/ping/%s/" % check.code, HTTP_USER_AGENT=ua)
assert r.status_code == 200
pings = list(Ping.objects.all())
assert pings[0].ua == ua
def test_it_truncates_long_ua(self):
ua = "01234567890" * 30
check = Check()
check.save()
r = self.client.get("/ping/%s/" % check.code, HTTP_USER_AGENT=ua)
assert r.status_code == 200
pings = list(Ping.objects.all())
assert len(pings[0].ua) == 200
assert ua.startswith(pings[0].ua)

+ 2
- 1
hc/api/views.py View File

@ -27,7 +27,8 @@ def ping(request, code):
headers = request.META
ping.remote_addr = headers.get("HTTP_X_REAL_IP", headers["REMOTE_ADDR"])
ping.method = headers["REQUEST_METHOD"]
ping.ua = headers.get("HTTP_USER_AGENT", "")
# If User-Agent is longer than 200 characters, truncate it:
ping.ua = headers.get("HTTP_USER_AGENT", "")[:200]
ping.body = request.body
ping.save()


Loading…
Cancel
Save