util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "heckel.io/ntfy/util"
  6. "net/http"
  7. "strings"
  8. )
  9. const (
  10. actionIDLength = 10
  11. actionsMax = 3
  12. )
  13. func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
  14. value := strings.ToLower(readParam(r, names...))
  15. if value == "" {
  16. return defaultValue
  17. }
  18. return value == "1" || value == "yes" || value == "true"
  19. }
  20. func readParam(r *http.Request, names ...string) string {
  21. value := readHeaderParam(r, names...)
  22. if value != "" {
  23. return value
  24. }
  25. return readQueryParam(r, names...)
  26. }
  27. func readHeaderParam(r *http.Request, names ...string) string {
  28. for _, name := range names {
  29. value := r.Header.Get(name)
  30. if value != "" {
  31. return strings.TrimSpace(value)
  32. }
  33. }
  34. return ""
  35. }
  36. func readQueryParam(r *http.Request, names ...string) string {
  37. for _, name := range names {
  38. value := r.URL.Query().Get(strings.ToLower(name))
  39. if value != "" {
  40. return strings.TrimSpace(value)
  41. }
  42. }
  43. return ""
  44. }
  45. func parseActions(s string) (actions []*action, err error) {
  46. // Parse JSON or simple format
  47. s = strings.TrimSpace(s)
  48. if strings.HasPrefix(s, "[") {
  49. actions, err = parseActionsFromJSON(s)
  50. } else {
  51. actions, err = parseActionsFromSimple(s)
  52. }
  53. if err != nil {
  54. return nil, err
  55. }
  56. // Add ID field
  57. for i := range actions {
  58. actions[i].ID = util.RandomString(actionIDLength)
  59. }
  60. // Validate
  61. if len(actions) > actionsMax {
  62. return nil, fmt.Errorf("too many actions, only %d allowed", actionsMax)
  63. }
  64. for _, action := range actions {
  65. if !util.InStringList([]string{"view", "broadcast", "http"}, action.Action) {
  66. return nil, fmt.Errorf("cannot parse actions: action '%s' unknown", action.Action)
  67. } else if action.Label == "" {
  68. return nil, fmt.Errorf("cannot parse actions: label must be set")
  69. } else if util.InStringList([]string{"view", "http"}, action.Action) && action.URL == "" {
  70. return nil, fmt.Errorf("parameter 'url' is required for action '%s'", action.Action)
  71. }
  72. }
  73. return actions, nil
  74. }
  75. func parseActionsFromJSON(s string) ([]*action, error) {
  76. actions := make([]*action, 0)
  77. if err := json.Unmarshal([]byte(s), &actions); err != nil {
  78. return nil, err
  79. }
  80. return actions, nil
  81. }
  82. func parseActionsFromSimple(s string) ([]*action, error) {
  83. actions := make([]*action, 0)
  84. rawActions := util.SplitNoEmpty(s, ";")
  85. for _, rawAction := range rawActions {
  86. newAction := &action{
  87. Headers: make(map[string]string),
  88. Extras: make(map[string]string),
  89. }
  90. parts := util.SplitNoEmpty(rawAction, ",")
  91. if len(parts) < 3 {
  92. return nil, fmt.Errorf("cannot parse action: action requires at least keys 'action', 'label' and one parameter: %s", rawAction)
  93. }
  94. for i, part := range parts {
  95. key, value := util.SplitKV(part, "=")
  96. if key == "" && i == 0 {
  97. newAction.Action = value
  98. } else if key == "" && i == 1 {
  99. newAction.Label = value
  100. } else if key == "" && util.InStringList([]string{"view", "http"}, newAction.Action) && i == 2 {
  101. newAction.URL = value
  102. } else if strings.HasPrefix(key, "headers.") {
  103. newAction.Headers[strings.TrimPrefix(key, "headers.")] = value
  104. } else if strings.HasPrefix(key, "extras.") {
  105. newAction.Extras[strings.TrimPrefix(key, "extras.")] = value
  106. } else if key != "" {
  107. switch strings.ToLower(key) {
  108. case "action":
  109. newAction.Action = value
  110. case "label":
  111. newAction.Label = value
  112. case "url":
  113. newAction.URL = value
  114. case "method":
  115. newAction.Method = value
  116. case "body":
  117. newAction.Body = value
  118. default:
  119. return nil, fmt.Errorf("cannot parse action: key '%s' not supported, please use JSON format instead", part)
  120. }
  121. } else {
  122. return nil, fmt.Errorf("cannot parse action: unknown phrase '%s'", part)
  123. }
  124. }
  125. actions = append(actions, newAction)
  126. }
  127. return actions, nil
  128. }