Add debug logs to webmentions

This commit is contained in:
Jan-Lukas Else 2021-08-03 21:32:02 +02:00
parent ae7961ed49
commit 636948103a
3 changed files with 19 additions and 3 deletions

View File

@ -24,6 +24,7 @@ type config struct {
Notifications *configNotifications `mapstructure:"notifications"`
PrivateMode *configPrivateMode `mapstructure:"privateMode"`
EasterEgg *configEasterEgg `mapstructure:"easterEgg"`
Debug bool `mapstructure:"debug"`
}
type configServer struct {

9
debug.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "log"
func (a *goBlog) debug(msg ...interface{}) {
if a.cfg.Debug {
log.Println(append([]interface{}{"Debug:"}, msg...)...)
}
}

View File

@ -44,25 +44,30 @@ func (a *goBlog) initWebmention() {
}
func (a *goBlog) handleWebmention(w http.ResponseWriter, r *http.Request) {
m, err := extractMention(r)
m, err := a.extractMention(r)
if err != nil {
a.debug("Error extracting webmention:", err.Error())
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
if !strings.HasPrefix(m.Target, a.cfg.Server.PublicAddress) {
a.debug("Webmention target not allowed:", m.Target)
a.serveError(w, r, "target not allowed", http.StatusBadRequest)
return
}
if err = a.queueMention(m); err != nil {
a.debug("Failed to queue webmention", err.Error())
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusAccepted)
_, _ = fmt.Fprint(w, "Webmention accepted")
a.debug("Accepted webmention:", m.Source, m.Target)
}
func extractMention(r *http.Request) (*mention, error) {
if !strings.Contains(r.Header.Get(contentType), contenttype.WWWForm) {
func (a *goBlog) extractMention(r *http.Request) (*mention, error) {
if ct := r.Header.Get(contentType); !strings.Contains(ct, contenttype.WWWForm) {
a.debug("New webmention request with wrong content type:", ct)
return nil, errors.New("unsupported Content-Type")
}
err := r.ParseForm()
@ -72,6 +77,7 @@ func extractMention(r *http.Request) (*mention, error) {
source := r.Form.Get("source")
target := unescapedPath(r.Form.Get("target"))
if source == "" || target == "" || !isAbsoluteURL(source) || !isAbsoluteURL(target) {
a.debug("Invalid webmention request, source:", source, "target:", target)
return nil, errors.New("invalid request")
}
return &mention{