Skip to content

Commit

Permalink
Merge pull request #65 from Qabel/m/ignoreinvalidheaders
Browse files Browse the repository at this point in the history
ignore invalid If-Modified-Since and X-Qabel-Since headers
  • Loading branch information
julianseeger authored Sep 28, 2016
2 parents 2998ac6 + 8ecae25 commit 9255c50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 9 additions & 2 deletions drop_service/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
import logging
import uuid
from email.utils import formatdate
from time import mktime
Expand All @@ -16,6 +17,8 @@
from .notify import get_notificators
from .util import CsrfExemptView, check_drop_id, set_last_modified, utc_timestamp

logger = logging.getLogger(__name__)


def error(msg, status=status.HTTP_400_BAD_REQUEST):
return HttpResponse(json.dumps({'error': msg}), status=status)
Expand All @@ -37,7 +40,8 @@ def _get_drops(self, drop_id):
try:
have_since, since = self.get_if_modified_since()
except ValueError as value_error:
return error(str(value_error)), None
logger.warning('Could not parse modified-since header pack: %s', value_error)
have_since, since = False, None

if have_since:
drops = drops.filter(created_at__gt=since)
Expand Down Expand Up @@ -88,7 +92,10 @@ def get_if_modified_since(self):
if coarse_since and finest_since:
raise ValueError('Specify only one of X-Qabel-New-Since, If-Modified-Since')
if coarse_since:
return True, dateparser.parse(coarse_since)
since = dateparser.parse(coarse_since)
if not since:
raise ValueError('Unable to parse If-Modified-Since')
return True, since
elif finest_since:
return True, datetime.datetime.fromtimestamp(float(finest_since), datetime.timezone.utc)
else:
Expand Down
22 changes: 21 additions & 1 deletion tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@ def test_get_messages_posted_since(self):
HTTP_IF_MODIFIED_SINCE=format_datetime(dt, usegmt=True))

assert response.status_code == status.HTTP_200_OK
assert 'Hello World' in response.content.decode()
body = response.content.decode()
assert 'Hello World' in body
assert 'Bar' not in body

def test_get_messages_posted_since_invalid(self):
response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo',
HTTP_IF_MODIFIED_SINCE='warghabl')

assert response.status_code == status.HTTP_200_OK
body = response.content.decode()
assert 'Hello World' in body
assert 'Bar' not in body

def test_get_messages_posted_since_qabel(self):
response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo',
Expand All @@ -68,6 +79,15 @@ def test_get_messages_posted_since_qabel(self):
assert 'Hello World' in body
assert 'Bar' not in body

def test_get_messages_posted_since_qabel_invalid(self):
response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo',
HTTP_X_QABEL_NEW_SINCE='warghabl')

assert response.status_code == status.HTTP_200_OK
body = response.content.decode()
assert 'Hello World' in body
assert 'Bar' not in body

def test_get_qabel_round_trip(self):
response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo')
assert response.status_code == status.HTTP_200_OK
Expand Down

0 comments on commit 9255c50

Please sign in to comment.