package pingback
import (
"brainbaking.com/go-jamming/app/mf"
"brainbaking.com/go-jamming/app/webmention/recv"
"brainbaking.com/go-jamming/common"
"brainbaking.com/go-jamming/rest"
"encoding/xml"
"github.com/rs/zerolog/log"
"io/ioutil"
"net/http"
)
func HandlePost(conf *common.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
pingbackError(w, "Unable to read request body")
}
rpc := &XmlRPCMethodCall{}
err = xml.Unmarshal(body, rpc)
if err != nil {
pingbackError(w, "Unable to unmarshal XMLRPC request body")
}
if !validate(rpc, conf) {
pingbackError(w, "malformed pingback request")
return
}
wm := mf.Mention{
Source: rpc.Source(),
Target: rpc.Target(),
}
receiver := recv.Receiver{
RestClient: &rest.HttpClient{},
Conf: conf,
}
go receiver.Receive(wm)
pingbackSuccess(w)
}
}
func pingbackSuccess(w http.ResponseWriter) {
xml := `
Thanks, bro. Will process this soon, pinky swear!
`
w.WriteHeader(200)
w.Write([]byte(xml))
}
// according to the XML-RPC spec, always return a 200, but encode it into the XML.
func pingbackError(w http.ResponseWriter, msg string) {
xml := `
faultCode
0
faultString
Sorry pal. Malformed request? Or something else, who knows...
`
log.Error().Str("msg", msg).Msg("Pingback receive went wrong")
w.WriteHeader(200)
w.Write([]byte(xml))
}