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

Commit

Permalink
bug: pass TTL Header value to GCM
Browse files Browse the repository at this point in the history
The code was previously using the configuration option of gcm_ttl
instead of the user supplied TTL. Instead, use gcm_ttl as the minimum
TTL in case of message loss due to TTL 0.

closes #374
  • Loading branch information
jrconlin committed Feb 25, 2016
1 parent ebc27bf commit c5ae841
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
16 changes: 8 additions & 8 deletions autopush/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ def _uaid_lookup_results(self, result):
try:
self.router = self.ap_settings.routers[router_key]
except KeyError:
log.msg("Client error", status_code=400, errno=108,
log.msg("Invalid router requested", status_code=400, errno=108,
**self._client_info)
return self._write_response(400, 108,
message="Invalid router type")
message="Invalid router")

# Only simplepush uses version/data out of body/query, GCM/APNS will
# use data out of the request body 'WebPush' style.
Expand Down Expand Up @@ -598,9 +598,9 @@ def post(self, router_type="", router_token="", uaid="", chid=""):

# normalize the path vars into parameters
if router_type not in self.ap_settings.routers:
log.msg("Invalid parameters", **self._client_info)
log.msg("Invalid router requested", **self._client_info)
return self._write_response(
400, 108, message="Invalid arguments")
400, 108, message="Invalid router")
router = self.ap_settings.routers[router_type]
valid, router_token = router.check_token(router_token)
if not valid:
Expand Down Expand Up @@ -651,9 +651,9 @@ def put(self, router_type="", router_token="", uaid="", chid=""):
self.uaid = uaid
router_data = params
if router_type not in self.ap_settings.routers or not router_data:
log.msg("Invalid parameters", **self._client_info)
log.msg("Invalid router requested", **self._client_info)
return self._write_response(
400, 108, message="Invalid arguments")
400, 108, message="Invalid router")
router = self.ap_settings.routers[router_type]
valid, router_token = router.check_token(router_token)
if not valid:
Expand Down Expand Up @@ -699,9 +699,9 @@ def delete(self, router_type="", router_token="", uaid="", chid=""):
return self._write_unauthorized_response(
message="Invalid Authentication")
if router_type not in self.ap_settings.routers:
log.msg("Invalid parameters", **self._client_info)
log.msg("Invalid router requested", **self._client_info)
return self._write_response(
400, 108, message="Invalid arguments")
400, 108, message="Invalid router")
router = self.ap_settings.routers[router_type]
valid, router_token = router.check_token(router_token)
if not valid:
Expand Down
6 changes: 3 additions & 3 deletions autopush/router/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
class GCMRouter(object):
"""GCM Router Implementation"""
gcm = None
ttl = 60
dryRun = 0
collapseKey = "simplepush"

def __init__(self, ap_settings, router_conf):
"""Create a new GCM router and connect to GCM"""
self.config = router_conf
self.ttl = router_conf.get("ttl", 60)
self.min_ttl = router_conf.get("ttl", 60)
self.dryRun = router_conf.get("dryrun", False)
self.collapseKey = router_conf.get("collapseKey", "simplepush")
self.senderIDs = router_conf.get("senderIDs")
Expand Down Expand Up @@ -100,10 +99,11 @@ def _route(self, notification, router_data):

# registration_ids are the GCM instance tokens (specified during
# registration.
router_ttl = notification.ttl or 0
payload = gcmclient.JSONMessage(
registration_ids=[router_data.get("token")],
collapse_key=self.collapseKey,
time_to_live=self.ttl,
time_to_live=max(self.min_ttl, router_ttl),
dry_run=self.dryRun or ("dryrun" in router_data),
data=data,
)
Expand Down
25 changes: 25 additions & 0 deletions autopush/tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def setUp(self, fgcm):
)
self.gcm_config = {'s3_bucket': 'None',
'max_data': 32,
'ttl': 60,
'senderid_list': {'test123':
{"auth": "12345678abcdefg"}}}
self.gcm = fgcm
Expand Down Expand Up @@ -242,6 +243,30 @@ def check_results(result):
d.addCallback(check_results)
return d

def test_ttl_none(self):
self.router.gcm = self.gcm
self.notif = Notification(version=10,
data="\xab\xad\x1d\xea",
channel_id=dummy_chid,
headers=self.headers,
ttl=None)
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
ok_(isinstance(result, RouterResponse))
assert(self.router.gcm.send.called)
# Make sure the data was encoded as base64
data = self.router.gcm.send.call_args[0][0].data
options = self.router.gcm.send.call_args[0][0].options
eq_(data['body'], 'q60d6g==')
eq_(data['enc'], 'test')
eq_(data['enckey'], 'test')
eq_(data['con'], 'aesgcm')
# use the defined min TTL
eq_(options['time_to_live'], 60)
d.addCallback(check_results)
return d

def test_long_data(self):
self.router.gcm = self.gcm
badNotif = Notification(
Expand Down
4 changes: 4 additions & 0 deletions configs/autopush_endpoint.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ port = 8082
; GCM requires a API Auth key. It is STRONGLY recommended you use one specific to this app.
#gcm_apikey = <API_KEY>

; Minimum GCM Time To Live value. Set this in case of excessive loss of
; TTL 0 messages across the GCM bridge
#gcm_ttl = 0

; AuthKey are the keys to use for Bearer Auth tokens. It uses the same
; autokey generator as the crypto_key argument, and sorted [newest, oldest]
#auth_key = [HJVPy4ZwF4Yz_JdvXTL8hRcwIhv742vC60Tg5Ycrvw8=]
Expand Down

0 comments on commit c5ae841

Please sign in to comment.