mirror of https://github.com/jlelse/GoBlog
Add debug logs to webmentions
This commit is contained in:
parent
ae7961ed49
commit
636948103a
|
@ -24,6 +24,7 @@ type config struct {
|
||||||
Notifications *configNotifications `mapstructure:"notifications"`
|
Notifications *configNotifications `mapstructure:"notifications"`
|
||||||
PrivateMode *configPrivateMode `mapstructure:"privateMode"`
|
PrivateMode *configPrivateMode `mapstructure:"privateMode"`
|
||||||
EasterEgg *configEasterEgg `mapstructure:"easterEgg"`
|
EasterEgg *configEasterEgg `mapstructure:"easterEgg"`
|
||||||
|
Debug bool `mapstructure:"debug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type configServer struct {
|
type configServer struct {
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
func (a *goBlog) debug(msg ...interface{}) {
|
||||||
|
if a.cfg.Debug {
|
||||||
|
log.Println(append([]interface{}{"Debug:"}, msg...)...)
|
||||||
|
}
|
||||||
|
}
|
|
@ -44,25 +44,30 @@ func (a *goBlog) initWebmention() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) handleWebmention(w http.ResponseWriter, r *http.Request) {
|
func (a *goBlog) handleWebmention(w http.ResponseWriter, r *http.Request) {
|
||||||
m, err := extractMention(r)
|
m, err := a.extractMention(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
a.debug("Error extracting webmention:", err.Error())
|
||||||
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(m.Target, a.cfg.Server.PublicAddress) {
|
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)
|
a.serveError(w, r, "target not allowed", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = a.queueMention(m); err != nil {
|
if err = a.queueMention(m); err != nil {
|
||||||
|
a.debug("Failed to queue webmention", err.Error())
|
||||||
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
_, _ = fmt.Fprint(w, "Webmention accepted")
|
_, _ = fmt.Fprint(w, "Webmention accepted")
|
||||||
|
a.debug("Accepted webmention:", m.Source, m.Target)
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractMention(r *http.Request) (*mention, error) {
|
func (a *goBlog) extractMention(r *http.Request) (*mention, error) {
|
||||||
if !strings.Contains(r.Header.Get(contentType), contenttype.WWWForm) {
|
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")
|
return nil, errors.New("unsupported Content-Type")
|
||||||
}
|
}
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
|
@ -72,6 +77,7 @@ func extractMention(r *http.Request) (*mention, error) {
|
||||||
source := r.Form.Get("source")
|
source := r.Form.Get("source")
|
||||||
target := unescapedPath(r.Form.Get("target"))
|
target := unescapedPath(r.Form.Get("target"))
|
||||||
if source == "" || target == "" || !isAbsoluteURL(source) || !isAbsoluteURL(target) {
|
if source == "" || target == "" || !isAbsoluteURL(source) || !isAbsoluteURL(target) {
|
||||||
|
a.debug("Invalid webmention request, source:", source, "target:", target)
|
||||||
return nil, errors.New("invalid request")
|
return nil, errors.New("invalid request")
|
||||||
}
|
}
|
||||||
return &mention{
|
return &mention{
|
||||||
|
|
Loading…
Reference in New Issue