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

Commit

Permalink
fix: correct headers to use transcoded values
Browse files Browse the repository at this point in the history
Marshmallow converts "-" to "_", and some header values are stored with
slightly different names.

closes #702
  • Loading branch information
jrconlin committed Oct 13, 2016
1 parent 0aab2a9 commit 0e5e1e3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 26 deletions.
10 changes: 5 additions & 5 deletions autopush/router/apnsrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ def _route(self, notification, router_data):
}
if notification.data:
payload["body"] = notification.data
payload["con"] = notification.headers["content-encoding"]
payload["con"] = notification.headers["encoding"]
payload["enc"] = notification.headers["encryption"]

if "crypto-key" in notification.headers:
payload["cryptokey"] = notification.headers["crypto-key"]
elif "encryption-key" in notification.headers:
payload["enckey"] = notification.headers["encryption-key"]
if "crypto_key" in notification.headers:
payload["cryptokey"] = notification.headers["crypto_key"]
elif "encryption_key" in notification.headers:
payload["enckey"] = notification.headers["encryption_key"]
payload['aps'] = dict(
alert=router_data.get("title", config.get('default_title',
'Mozilla Push')),
Expand Down
10 changes: 5 additions & 5 deletions autopush/router/fcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ def _route(self, notification, router_data):
413, errno=104, log_exception=False)

data['body'] = notification.data
data['con'] = notification.headers['content-encoding']
data['con'] = notification.headers['encoding']
data['enc'] = notification.headers['encryption']

if 'crypto-key' in notification.headers:
data['cryptokey'] = notification.headers['crypto-key']
elif 'encryption-key' in notification.headers:
data['enckey'] = notification.headers['encryption-key']
if 'crypto_key' in notification.headers:
data['cryptokey'] = notification.headers['crypto_key']
elif 'encryption_key' in notification.headers:
data['enckey'] = notification.headers['encryption_key']

# registration_ids are the FCM instance tokens (specified during
# registration.
Expand Down
10 changes: 5 additions & 5 deletions autopush/router/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ def _route(self, notification, uaid_data):
413, errno=104, log_exception=False)

data['body'] = notification.data
data['con'] = notification.headers['content-encoding']
data['con'] = notification.headers['encoding']
data['enc'] = notification.headers['encryption']

if 'crypto-key' in notification.headers:
data['cryptokey'] = notification.headers['crypto-key']
elif 'encryption-key' in notification.headers:
data['enckey'] = notification.headers['encryption-key']
if 'crypto_key' in notification.headers:
data['cryptokey'] = notification.headers['crypto_key']
elif 'encryption_key' in notification.headers:
data['enckey'] = notification.headers['encryption_key']

# registration_ids are the GCM instance tokens (specified during
# registration.
Expand Down
19 changes: 15 additions & 4 deletions autopush/tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def setUp(self, mt, mc):
ttl=200,
message_id=10,
)
self.notif.cleanup_headers()
self.router_data = dict(router_data=dict(token="connect_data",
rel_channel="firefox"))

Expand Down Expand Up @@ -240,6 +241,7 @@ def test_route_crypto_key(self):
ttl=200,
message_id=10,
)
self.notif.cleanup_headers()
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
Expand Down Expand Up @@ -280,6 +282,7 @@ def setUp(self, fgcm):
ttl=200,
message_id=10,
)
self.notif.cleanup_headers()
self.router_data = dict(
router_data=dict(
token="connect_data",
Expand Down Expand Up @@ -364,6 +367,7 @@ def test_ttl_none(self):
headers=self.headers,
ttl=None
)
self.notif.cleanup_headers()
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
Expand Down Expand Up @@ -394,6 +398,7 @@ def test_ttl_high(self):
headers=self.headers,
ttl=5184000
)
self.notif.cleanup_headers()
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
Expand Down Expand Up @@ -421,6 +426,7 @@ def test_long_data(self):
headers=self.headers,
ttl=200
)

d = self.router.route_notification(bad_notif, self.router_data)

def check_results(result):
Expand All @@ -433,8 +439,8 @@ def check_results(result):

def test_route_crypto_notification(self):
self.router.gcm['test123'] = self.gcm
del(self.notif.headers['encryption-key'])
self.notif.headers['crypto-key'] = 'crypto'
del(self.notif.headers['encryption_key'])
self.notif.headers['crypto_key'] = 'crypto'
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
Expand Down Expand Up @@ -564,6 +570,7 @@ def setUp(self, ffcm):
headers=self.headers,
ttl=200
)
self.notif.cleanup_headers()
self.router_data = dict(
router_data=dict(
token="connect_data",
Expand Down Expand Up @@ -641,6 +648,7 @@ def test_ttl_none(self):
headers=self.headers,
ttl=None
)
self.notif.cleanup_headers()
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
Expand Down Expand Up @@ -668,6 +676,7 @@ def test_ttl_high(self):
headers=self.headers,
ttl=5184000
)
self.notif.cleanup_headers()
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
Expand Down Expand Up @@ -696,6 +705,7 @@ def test_long_data(self):
ttl=200,
message_id=10,
)
self.notif.cleanup_headers()
d = self.router.route_notification(bad_notif, self.router_data)

def check_results(result):
Expand All @@ -708,8 +718,8 @@ def check_results(result):

def test_route_crypto_notification(self):
self.router.fcm = self.fcm
del(self.notif.headers['encryption-key'])
self.notif.headers['crypto-key'] = 'crypto'
del(self.notif.headers['encryption_key'])
self.notif.headers['crypto_key'] = 'crypto'
d = self.router.route_notification(self.notif, self.router_data)

def check_results(result):
Expand Down Expand Up @@ -1119,6 +1129,7 @@ def test_route_to_busy_node_with_ttl_zero(self):
ttl=0,
message_id=uuid.uuid4().hex,
)
self.notif.cleanup_headers()
self.agent_mock.request.return_value = response_mock = Mock()
response_mock.addCallback.return_value = response_mock
type(response_mock).code = PropertyMock(
Expand Down
6 changes: 5 additions & 1 deletion autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,13 +1517,17 @@ def test_notification_with_webpush(self):
"timestamp": 0}
self.proto.send_notifications(payload)

fixed_headers = dict()
for header in dummy_headers:
fixed_headers[header.replace("-", "_")] = dummy_headers[header]

# Check the call result
args = json.loads(self.send_mock.call_args[0][0])
eq_(args, {"messageType": "notification",
"channelID": chid,
"data": dummy_data,
"version": "10",
"headers": dummy_headers})
"headers": fixed_headers})

def test_notification_avoid_newer_delivery(self):
self._connect()
Expand Down
15 changes: 9 additions & 6 deletions autopush/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,14 @@ def cleanup_headers(self):
head = headers[hdr].replace('"', '')
headers[hdr] = STRIP_PADDING.sub("", head)

data = dict(
encoding=headers["content-encoding"],
encryption=headers["encryption"],
)
# content-encoding header may already be stored as "encoding",
# this is a failover to ensure that the proper value is pulled in.
# Webpush expects the value of "encoding".
data = dict(encoding=headers.get("encoding",
headers.get("content-encoding")))
# AWS cannot store empty strings, so we only add these keys if
# they're present to avoid empty strings.
for name in ["encryption-key", "crypto-key"]:
for name in ["encryption", "encryption-key", "crypto-key"]:
if name in headers:
# NOTE: The client code expects all header keys to be lower
# case and s/-/_/.
Expand Down Expand Up @@ -519,7 +520,9 @@ def websocket_format(self):
)
if self.data:
payload["data"] = self.data
payload["headers"] = self.headers
payload["headers"] = {
k.replace("-", "_"): v for k, v in self.headers.items()
}
return payload


Expand Down

0 comments on commit 0e5e1e3

Please sign in to comment.