2022-01-16 07:17:46 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-06-02 06:24:44 +03:00
|
|
|
"fmt"
|
|
|
|
"github.com/emersion/go-smtp"
|
2022-06-20 04:25:35 +03:00
|
|
|
"heckel.io/ntfy/util"
|
2022-01-16 07:17:46 +03:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-06-20 04:25:35 +03:00
|
|
|
"unicode/utf8"
|
2022-01-16 07:17:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
|
|
|
|
value := strings.ToLower(readParam(r, names...))
|
|
|
|
if value == "" {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return value == "1" || value == "yes" || value == "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
func readParam(r *http.Request, names ...string) string {
|
2022-02-26 07:25:04 +03:00
|
|
|
value := readHeaderParam(r, names...)
|
|
|
|
if value != "" {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return readQueryParam(r, names...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readHeaderParam(r *http.Request, names ...string) string {
|
2022-01-16 07:17:46 +03:00
|
|
|
for _, name := range names {
|
|
|
|
value := r.Header.Get(name)
|
|
|
|
if value != "" {
|
|
|
|
return strings.TrimSpace(value)
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 07:25:04 +03:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func readQueryParam(r *http.Request, names ...string) string {
|
2022-01-16 07:17:46 +03:00
|
|
|
for _, name := range names {
|
|
|
|
value := r.URL.Query().Get(strings.ToLower(name))
|
|
|
|
if value != "" {
|
|
|
|
return strings.TrimSpace(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2022-06-02 06:24:44 +03:00
|
|
|
|
|
|
|
func logMessagePrefix(v *visitor, m *message) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%s", v.ip, m.Topic, m.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logHTTPPrefix(v *visitor, r *http.Request) string {
|
|
|
|
requestURI := r.RequestURI
|
|
|
|
if requestURI == "" {
|
|
|
|
requestURI = r.URL.Path
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s HTTP %s %s", v.ip, r.Method, requestURI)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logSMTPPrefix(state *smtp.ConnectionState) string {
|
|
|
|
return fmt.Sprintf("%s/%s SMTP", state.Hostname, state.RemoteAddr.String())
|
|
|
|
}
|
2022-06-20 04:25:35 +03:00
|
|
|
|
|
|
|
func renderHTTPRequest(r *http.Request) string {
|
|
|
|
peekLimit := 4096
|
|
|
|
lines := fmt.Sprintf("%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
|
|
|
|
for key, values := range r.Header {
|
|
|
|
for _, value := range values {
|
|
|
|
lines += fmt.Sprintf("%s: %s\n", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lines += "\n"
|
|
|
|
body, err := util.Peek(r.Body, peekLimit)
|
|
|
|
if err != nil {
|
|
|
|
lines = fmt.Sprintf("(could not read body: %s)\n", err.Error())
|
|
|
|
} else if utf8.Valid(body.PeekedBytes) {
|
|
|
|
lines += string(body.PeekedBytes)
|
|
|
|
if body.LimitReached {
|
|
|
|
lines += fmt.Sprintf(" ... (peeked %d bytes)", peekLimit)
|
|
|
|
}
|
|
|
|
lines += "\n"
|
|
|
|
} else {
|
|
|
|
if body.LimitReached {
|
|
|
|
lines += fmt.Sprintf("(peeked bytes not UTF-8, peek limit of %d bytes reached, hex: %x ...)\n", peekLimit, body.PeekedBytes)
|
|
|
|
} else {
|
|
|
|
lines += fmt.Sprintf("(peeked bytes not UTF-8, %d bytes, hex: %x)\n", len(body.PeekedBytes), body.PeekedBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Body = body // Important: Reset body, so it can be re-read
|
|
|
|
return strings.TrimSpace(lines)
|
|
|
|
}
|