2021-04-07 11:44:58 +02:00
|
|
|
package pingback
|
|
|
|
|
|
|
|
import (
|
2021-04-09 18:04:04 +02:00
|
|
|
"brainbaking.com/go-jamming/app/mf"
|
|
|
|
"brainbaking.com/go-jamming/app/webmention/recv"
|
|
|
|
"brainbaking.com/go-jamming/common"
|
|
|
|
"brainbaking.com/go-jamming/rest"
|
2021-04-09 21:00:54 +02:00
|
|
|
"encoding/xml"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-04-09 12:40:37 +02:00
|
|
|
"io/ioutil"
|
2021-04-07 11:44:58 +02:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2021-04-09 12:40:37 +02:00
|
|
|
func HandlePost(conf *common.Config) http.HandlerFunc {
|
2021-04-09 21:00:54 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 12:40:37 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:21:25 +02:00
|
|
|
wm := mf.Mention{
|
2021-04-09 12:40:37 +02:00
|
|
|
Source: rpc.Source(),
|
|
|
|
Target: rpc.Target(),
|
|
|
|
}
|
2021-04-09 18:04:04 +02:00
|
|
|
receiver := recv.Receiver{
|
2021-04-09 12:40:37 +02:00
|
|
|
RestClient: &rest.HttpClient{},
|
|
|
|
Conf: conf,
|
|
|
|
}
|
|
|
|
go receiver.Receive(wm)
|
2021-04-09 14:23:16 +02:00
|
|
|
pingbackSuccess(w)
|
2021-04-09 12:40:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:23:16 +02:00
|
|
|
func pingbackSuccess(w http.ResponseWriter) {
|
|
|
|
xml := `<?xml version="1.0" encoding="UTF-8"?>
|
2021-04-09 12:40:37 +02:00
|
|
|
<methodResponse>
|
|
|
|
<params>
|
|
|
|
<param>
|
|
|
|
<value>
|
|
|
|
<string>
|
2021-04-09 14:23:16 +02:00
|
|
|
Thanks, bro. Will process this soon, pinky swear!
|
2021-04-09 12:40:37 +02:00
|
|
|
</string>
|
|
|
|
</value>
|
|
|
|
</param>
|
|
|
|
</params>
|
2021-04-09 14:23:16 +02:00
|
|
|
</methodResponse>`
|
2021-04-09 12:40:37 +02:00
|
|
|
w.WriteHeader(200)
|
2021-04-09 14:23:16 +02:00
|
|
|
w.Write([]byte(xml))
|
2021-04-09 12:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// according to the XML-RPC spec, always return a 200, but encode it into the XML.
|
|
|
|
func pingbackError(w http.ResponseWriter, msg string) {
|
|
|
|
xml := `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<methodResponse>
|
|
|
|
<fault>
|
|
|
|
<value>
|
|
|
|
<struct>
|
|
|
|
<member>
|
|
|
|
<name>
|
|
|
|
faultCode
|
|
|
|
</name>
|
|
|
|
<value>
|
|
|
|
<int>
|
|
|
|
0
|
|
|
|
</int>
|
|
|
|
</value>
|
|
|
|
</member>
|
|
|
|
<member>
|
|
|
|
<name>
|
|
|
|
faultString
|
|
|
|
</name>
|
|
|
|
<value>
|
|
|
|
<string>
|
|
|
|
Sorry pal. Malformed request? Or something else, who knows...
|
|
|
|
</string>
|
|
|
|
</value>
|
|
|
|
</member>
|
|
|
|
</struct>
|
|
|
|
</value>
|
|
|
|
</fault>
|
|
|
|
</methodResponse>`
|
|
|
|
log.Error().Str("msg", msg).Msg("Pingback receive went wrong")
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write([]byte(xml))
|
2021-04-07 11:44:58 +02:00
|
|
|
}
|