util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "heckel.io/ntfy/util"
  6. "io"
  7. "mime"
  8. "net/http"
  9. "net/netip"
  10. "strings"
  11. )
  12. var mimeDecoder mime.WordDecoder
  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 readCommaSeparatedParam(r *http.Request, names ...string) (params []string) {
  21. paramStr := readParam(r, names...)
  22. if paramStr != "" {
  23. params = make([]string, 0)
  24. for _, s := range util.SplitNoEmpty(paramStr, ",") {
  25. params = append(params, strings.TrimSpace(s))
  26. }
  27. }
  28. return params
  29. }
  30. func readParam(r *http.Request, names ...string) string {
  31. value := readHeaderParam(r, names...)
  32. if value != "" {
  33. return value
  34. }
  35. return readQueryParam(r, names...)
  36. }
  37. func readHeaderParam(r *http.Request, names ...string) string {
  38. for _, name := range names {
  39. value := r.Header.Get(name)
  40. if value != "" {
  41. return strings.TrimSpace(value)
  42. }
  43. }
  44. return ""
  45. }
  46. func readQueryParam(r *http.Request, names ...string) string {
  47. for _, name := range names {
  48. value := r.URL.Query().Get(strings.ToLower(name))
  49. if value != "" {
  50. return strings.TrimSpace(value)
  51. }
  52. }
  53. return ""
  54. }
  55. func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr {
  56. remoteAddr := r.RemoteAddr
  57. addrPort, err := netip.ParseAddrPort(remoteAddr)
  58. ip := addrPort.Addr()
  59. if err != nil {
  60. // This should not happen in real life; only in tests. So, using falling back to 0.0.0.0 if address unspecified
  61. ip, err = netip.ParseAddr(remoteAddr)
  62. if err != nil {
  63. ip = netip.IPv4Unspecified()
  64. if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used
  65. logr(r).Err(err).Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created", remoteAddr)
  66. }
  67. }
  68. }
  69. if behindProxy && strings.TrimSpace(r.Header.Get("X-Forwarded-For")) != "" {
  70. // X-Forwarded-For can contain multiple addresses (see #328). If we are behind a proxy,
  71. // only the right-most address can be trusted (as this is the one added by our proxy server).
  72. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For for details.
  73. ips := util.SplitNoEmpty(r.Header.Get("X-Forwarded-For"), ",")
  74. realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr)))
  75. if err != nil {
  76. logr(r).Err(err).Error("invalid IP address %s received in X-Forwarded-For header", ip)
  77. // Fall back to regular remote address if X-Forwarded-For is damaged
  78. } else {
  79. ip = realIP
  80. }
  81. }
  82. return ip
  83. }
  84. func readJSONWithLimit[T any](r io.ReadCloser, limit int, allowEmpty bool) (*T, error) {
  85. obj, err := util.UnmarshalJSONWithLimit[T](r, limit, allowEmpty)
  86. if err == util.ErrUnmarshalJSON {
  87. return nil, errHTTPBadRequestJSONInvalid
  88. } else if err == util.ErrTooLargeJSON {
  89. return nil, errHTTPEntityTooLargeJSONBody
  90. } else if err != nil {
  91. return nil, err
  92. }
  93. return obj, nil
  94. }
  95. func withContext(r *http.Request, ctx map[contextKey]any) *http.Request {
  96. c := r.Context()
  97. for k, v := range ctx {
  98. c = context.WithValue(c, k, v)
  99. }
  100. return r.WithContext(c)
  101. }
  102. func fromContext[T any](r *http.Request, key contextKey) (T, error) {
  103. t, ok := r.Context().Value(key).(T)
  104. if !ok {
  105. return t, fmt.Errorf("cannot find key %v in request context", key)
  106. }
  107. return t, nil
  108. }
  109. func maybeDecodeHeader(header string) string {
  110. decoded, err := mimeDecoder.DecodeHeader(header)
  111. if err != nil {
  112. return header
  113. }
  114. return decoded
  115. }