Skip to content

Commit

Permalink
resolve the issue with batch push failing when the URL contains a key
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Dec 31, 2024
1 parent f742219 commit 709052f
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions route_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ func init() {
func SetMaxBatchPushCount(count int) {
maxBatchPushCount = count
}

func routeDoPush(c *fiber.Ctx) error {
// Get content-type
contentType := utils.ToLower(utils.UnsafeString(c.Request().Header.ContentType()))
contentType = utils.ParseVendorSpecificContentType(contentType)
// Json request uses the API V2
if strings.HasPrefix(contentType, "application/json") {
return routeDoPushV2(c)
} else {
return routeDoPushV1(c)
}
}

func routeDoPushV1(c *fiber.Ctx) error {

params := make(map[string]interface{})
visitor := func(key, value []byte) {
Expand All @@ -60,7 +64,6 @@ func routeDoPush(c *fiber.Ctx) error {
c.Request().URI().QueryArgs().VisitAll(visitor)
// parse post args
c.Request().PostArgs().VisitAll(visitor)

// parse multipartForm values
form, err := c.Request().MultipartForm()
if err == nil {
Expand All @@ -70,15 +73,22 @@ func routeDoPush(c *fiber.Ctx) error {
}
}
}
// parse url path (highest priority)
pathParams, err := extractUrlPathParams(c)
if err != nil {
return c.Status(500).JSON(failed(400, "url path parse failed: %v", err))
}
for key, val := range pathParams {
params[key] = val
}

code, err := push(c, params)
code, err := push(params)
if err != nil {
return c.Status(code).JSON(failed(code, err.Error()))
} else {
return c.JSON(success())
}
}

func routeDoPushV2(c *fiber.Ctx) error {
params := make(map[string]interface{})
// parse body
Expand All @@ -89,6 +99,14 @@ func routeDoPushV2(c *fiber.Ctx) error {
c.Request().URI().QueryArgs().VisitAll(func(key, value []byte) {
params[strings.ToLower(string(key))] = string(value)
})
// parse url path (highest priority)
pathParams, err := extractUrlPathParams(c)
if err != nil {
return c.Status(500).JSON(failed(400, "url path parse failed: %v", err))
}
for key, val := range pathParams {
params[key] = val
}

var deviceKeys []string
// Get the device_keys array from params
Expand All @@ -110,7 +128,7 @@ func routeDoPushV2(c *fiber.Ctx) error {

if count == 0 {
// Single push
code, err := push(c, params)
code, err := push(params)
if err != nil {
return c.Status(code).JSON(failed(code, err.Error()))
} else {
Expand Down Expand Up @@ -139,7 +157,7 @@ func routeDoPushV2(c *fiber.Ctx) error {
defer wg.Done()

// Push
code, err := push(c, newParams)
code, err := push(newParams)

// Save result
mu.Lock()
Expand All @@ -157,7 +175,37 @@ func routeDoPushV2(c *fiber.Ctx) error {
}
}

func push(c *fiber.Ctx, params map[string]interface{}) (int, error) {
func extractUrlPathParams(c *fiber.Ctx) (map[string]interface{}, error) {
// parse url path (highest priority)
params := make(map[string]interface{})
if pathDeviceKey := c.Params("device_key"); pathDeviceKey != "" {
params["device_key"] = pathDeviceKey
}
if subtitle := c.Params("subtitle"); subtitle != "" {
str, err := url.QueryUnescape(subtitle)
if err != nil {
return nil, err
}
params["subtitle"] = str
}
if title := c.Params("title"); title != "" {
str, err := url.QueryUnescape(title)
if err != nil {
return nil, err
}
params["title"] = str
}
if body := c.Params("body"); body != "" {
str, err := url.QueryUnescape(body)
if err != nil {
return nil, err
}
params["body"] = str
}
return params, nil
}

func push(params map[string]interface{}) (int, error) {
// default value
msg := apns.PushMessage{
Body: "",
Expand Down Expand Up @@ -196,32 +244,6 @@ func push(c *fiber.Ctx, params map[string]interface{}) (int, error) {
}
}

// parse url path (highest priority)
if pathDeviceKey := c.Params("device_key"); pathDeviceKey != "" {
msg.DeviceKey = pathDeviceKey
}
if subtitle := c.Params("subtitle"); subtitle != "" {
str, err := url.QueryUnescape(subtitle)
if err != nil {
return 500, err
}
msg.Subtitle = str
}
if title := c.Params("title"); title != "" {
str, err := url.QueryUnescape(title)
if err != nil {
return 500, err
}
msg.Title = str
}
if body := c.Params("body"); body != "" {
str, err := url.QueryUnescape(body)
if err != nil {
return 500, err
}
msg.Body = str
}

if msg.DeviceKey == "" {
return 400, fmt.Errorf("device key is empty")
}
Expand Down

0 comments on commit 709052f

Please sign in to comment.