Skip to content

Commit 12a894d

Browse files
authored
Webpush headers (#123)
* Support adding arbitrary HTTP headers when using webpush() Useful for adding headers like `Topic` an `Urgency` that don't have dedicated parameters. * Send headers provided in the `--head` CLI argument
1 parent 07662b8 commit 12a894d

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

pywebpush/__init__.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ def webpush(subscription_info,
382382
curl=False,
383383
timeout=None,
384384
ttl=0,
385-
verbose=False):
385+
verbose=False,
386+
headers=None):
386387
"""
387388
One call solution to endcode and send `data` to the endpoint
388389
contained in `subscription_info` using optional VAPID auth headers.
@@ -427,8 +428,16 @@ def webpush(subscription_info,
427428
:param verbose: Provide verbose feedback
428429
:type verbose: bool
429430
:return requests.Response or string
431+
:param headers: Dictionary of extra HTTP headers to include
432+
:type headers: dict
430433
431434
"""
435+
if headers is None:
436+
headers = dict()
437+
else:
438+
# Ensure we don't leak VAPID headers by mutating the passed in dict.
439+
headers = headers.copy()
440+
432441
vapid_headers = None
433442
if vapid_claims:
434443
if verbose:
@@ -462,10 +471,11 @@ def webpush(subscription_info,
462471
vapid_headers = vv.sign(vapid_claims)
463472
if verbose:
464473
print("\t headers: {}".format(vapid_headers))
474+
headers.update(vapid_headers)
465475

466476
response = WebPusher(subscription_info, verbose=verbose).send(
467477
data,
468-
vapid_headers,
478+
headers,
469479
ttl=ttl,
470480
content_encoding=content_encoding,
471481
curl=curl,

pywebpush/__main__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def get_config():
99
parser = argparse.ArgumentParser(description="WebPush tool")
1010
parser.add_argument("--data", '-d', help="Data file")
1111
parser.add_argument("--info", "-i", help="Subscription Info JSON file")
12-
parser.add_argument("--head", help="Header Info JSON file")
12+
parser.add_argument("--head", help="Header Info JSON file")
1313
parser.add_argument("--claims", help="Vapid claim file")
1414
parser.add_argument("--key", help="Vapid private key file path")
1515
parser.add_argument("--curl", help="Don't send, display as curl command",
@@ -56,7 +56,8 @@ def main():
5656
vapid_claims=args.claims,
5757
curl=args.curl,
5858
content_encoding=args.encoding,
59-
verbose=args.verbose)
59+
verbose=args.verbose,
60+
headers=args.head)
6061
print(result)
6162
except Exception as ex:
6263
print("ERROR: {}".format(ex))

pywebpush/tests/test_webpush.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def test_send_vapid(self, mock_post):
154154
data=data,
155155
vapid_private_key=self.vapid_key,
156156
vapid_claims={"sub": "mailto:ops@example.com"},
157-
content_encoding="aesgcm"
157+
content_encoding="aesgcm",
158+
headers={"Test-Header": "test-value"}
158159
)
159160
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
160161
pheaders = mock_post.call_args[1].get('headers')
@@ -173,6 +174,7 @@ def repad(str):
173174
ckey = pheaders.get('crypto-key')
174175
ok_('dh=' in ckey)
175176
eq_(pheaders.get('content-encoding'), 'aesgcm')
177+
eq_(pheaders.get('test-header'), 'test-value')
176178

177179
@patch.object(WebPusher, "send")
178180
@patch.object(py_vapid.Vapid, "sign")

0 commit comments

Comments
 (0)