From 2d386b8a91b713fe4d84ef8e026e991a609dc5fd Mon Sep 17 00:00:00 2001 From: Ben Bangert Date: Wed, 11 Jan 2017 14:57:28 -0800 Subject: [PATCH] feat: add cache-control header to 410's (#773) Our 410's should result in the sender no longer sending, but senders ignore this. To avoid excessive re-processing on repeats, 410's now include a Cache-Control header set to the max of 1 day for nginx to cache results. Closes #770 --- autopush/tests/test_integration.py | 11 ++++++++++- autopush/web/base.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/autopush/tests/test_integration.py b/autopush/tests/test_integration.py index bddeb2ad..6bd3da27 100644 --- a/autopush/tests/test_integration.py +++ b/autopush/tests/test_integration.py @@ -11,7 +11,9 @@ import uuid from contextlib import contextmanager from StringIO import StringIO +from httplib import HTTPResponse # noqa from unittest.case import SkipTest + from zope.interface import implementer import boto @@ -22,6 +24,7 @@ from autobahn.twisted.websocket import WebSocketServerFactory from jose import jws from nose.tools import eq_, ok_ +from typing import Optional # noqa from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks, returnValue, Deferred from twisted.internet.endpoints import SSL4ServerEndpoint, TCP4ServerEndpoint @@ -136,6 +139,7 @@ def __init__(self, url, use_webpush=False, sslcontext=None): self.use_webpush = use_webpush self.channels = {} self.messages = {} + self.notif_response = None # type: Optional[HTTPResponse] self._crypto_key = """\ keyid="http://example.org/bob/keys/123;salt="XZwpw6o37R-6qoZjw6KwAw"\ """ @@ -276,6 +280,7 @@ def send_notification(self, channel=None, version=None, data=None, log.debug("%s Response (%s): %s", method, resp.status, resp.read()) http.close() eq_(resp.status, status) + self.notif_response = resp location = resp.getheader("Location", None) log.debug("Response Headers: %s", resp.getheaders()) if self.use_webpush: @@ -1028,7 +1033,7 @@ def test_multiple_delivery_with_multiple_ack(self): @inlineCallbacks def test_no_delivery_to_unregistered(self): data = str(uuid.uuid4()) - client = yield self.quick_register(use_webpush=True) + client = yield self.quick_register(use_webpush=True) # type: Client ok_(client.channels) chan = client.channels.keys()[0] @@ -1039,6 +1044,10 @@ def test_no_delivery_to_unregistered(self): yield client.unregister(chan) result = yield client.send_notification(data=data, status=410) + + # Verify cache-control + eq_(client.notif_response.getheader("Cache-Control"), "max-age=86400") + eq_(result, None) yield self.shut_down(client) diff --git a/autopush/web/base.py b/autopush/web/base.py index bd1558e2..b9eb4643 100644 --- a/autopush/web/base.py +++ b/autopush/web/base.py @@ -184,6 +184,11 @@ def _write_response(self, status_code, errno, message=None, error=None, if headers: for header in headers.keys(): self.set_header(header, headers.get(header)) + + # 410's get the max-age cache control header + if status_code == 410: + self.set_header("Cache-Control", "max-age=86400") + self._track_timing() self.finish() @@ -195,6 +200,7 @@ def _validation_err(self, fail): status_code=exc.status_code, errno=exc.errno, client_info=self._client_info) + self._write_response(exc.status_code, exc.errno, message="Request did not validate %s" % (exc.message or ""),