Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: add cache-control header to 410's (#773)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bbangert authored and pjenvey committed Jan 11, 2017
1 parent 876c382 commit 2d386b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"\
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]

Expand All @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions autopush/web/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 ""),
Expand Down

0 comments on commit 2d386b8

Please sign in to comment.