Skip to content

Commit

Permalink
Add context to SendNotification (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Richter <[email protected]>
  • Loading branch information
Richtermeister and Daniel Richter authored Feb 1, 2022
1 parent 9f15307 commit 9f057bc
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions webpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package webpush

import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
Expand Down Expand Up @@ -62,10 +63,15 @@ type Subscription struct {
Keys Keys `json:"keys"`
}

// SendNotification sends a push notification to a subscription's endpoint
// SendNotification calls SendNotificationWithContext with default context for backwards-compatibility
func SendNotification(message []byte, s *Subscription, options *Options) (*http.Response, error) {
return SendNotificationWithContext(context.Background(), message, s, options)
}

// SendNotificationWithContext sends a push notification to a subscription's endpoint
// Message Encryption for Web Push, and VAPID protocols.
// FOR MORE INFORMATION SEE RFC8291: https://datatracker.ietf.org/doc/rfc8291
func SendNotification(message []byte, s *Subscription, options *Options) (*http.Response, error) {
func SendNotificationWithContext(ctx context.Context, message []byte, s *Subscription, options *Options) (*http.Response, error) {
// Authentication secret (auth_secret)
authSecret, err := decodeSubscriptionKey(s.Keys.Auth)
if err != nil {
Expand Down Expand Up @@ -182,6 +188,10 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http.
return nil, err
}

if ctx != nil {
req = req.WithContext(ctx)
}

req.Header.Set("Content-Encoding", "aes128gcm")
req.Header.Set("Content-Length", strconv.Itoa(len(ciphertext)))
req.Header.Set("Content-Type", "application/octet-stream")
Expand Down

0 comments on commit 9f057bc

Please sign in to comment.