Skip to content

Commit

Permalink
Add RecordSize to options (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
SherClockHolmes authored Apr 11, 2019
1 parent c438199 commit 7ed4780
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
18 changes: 12 additions & 6 deletions webpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import (
"golang.org/x/crypto/hkdf"
)

const (
maxRecordSize uint32 = 4096
maxRecordLength int = int(maxRecordSize) - 16
)
const MaxRecordSize uint32 = 4096

// saltFunc generates a salt of 16 bytes
var saltFunc = func() ([]byte, error) {
Expand All @@ -42,6 +39,7 @@ type HTTPClient interface {
// Options are config and extra params needed to send a notification
type Options struct {
HTTPClient HTTPClient // Will replace with *http.Client by default if not included
RecordSize uint32 // Limit the record size
Subscriber string // Sub in VAPID JWT token
Topic string // Set the Topic header to collapse a pending messages (Optional)
TTL int // Set the TTL on the endpoint POST request
Expand Down Expand Up @@ -144,11 +142,19 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http.
return nil, err
}

// Get the record size
recordSize := options.RecordSize
if recordSize == 0 {
recordSize = MaxRecordSize
}

recordLength := int(recordSize) - 16

// Encryption Content-Coding Header
recordBuf := bytes.NewBuffer(salt)

rs := make([]byte, 4)
binary.BigEndian.PutUint32(rs, maxRecordSize)
binary.BigEndian.PutUint32(rs, recordSize)

recordBuf.Write(rs)
recordBuf.Write([]byte{byte(len(localPublicKey))})
Expand All @@ -160,7 +166,7 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http.
// Pad content to max record size - 16 - header
// Padding ending delimeter
dataBuf.Write([]byte("\x02"))
pad(dataBuf, maxRecordLength-recordBuf.Len())
pad(dataBuf, recordLength-recordBuf.Len())

// Compose the ciphertext
ciphertext := gcm.Seal([]byte{}, nonce, dataBuf.Bytes(), nil)
Expand Down
5 changes: 3 additions & 2 deletions webpush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func getStandardEncodedTestSubscription() *Subscription {
func TestSendNotificationToURLEncodedSubscription(t *testing.T) {
resp, err := SendNotification([]byte("Test"), getURLEncodedTestSubscription(), &Options{
HTTPClient: &testHTTPClient{},
Subscriber: "mailto:<[email protected]>",
RecordSize: 3070,
Subscriber: "<[email protected]>",
Topic: "test_topic",
TTL: 0,
Urgency: "low",
Expand All @@ -57,7 +58,7 @@ func TestSendNotificationToURLEncodedSubscription(t *testing.T) {
func TestSendNotificationToStandardEncodedSubscription(t *testing.T) {
resp, err := SendNotification([]byte("Test"), getStandardEncodedTestSubscription(), &Options{
HTTPClient: &testHTTPClient{},
Subscriber: "mailto:<[email protected]>",
Subscriber: "<[email protected]>",
Topic: "test_topic",
TTL: 0,
Urgency: "low",
Expand Down

0 comments on commit 7ed4780

Please sign in to comment.