util.go 3.0 KB

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