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"
|
2021-04-18 15:39:29 +02:00
|
|
|
"brainbaking.com/go-jamming/db"
|
2021-04-09 18:04:04 +02:00
|
|
|
"brainbaking.com/go-jamming/rest"
|
2021-04-09 21:00:54 +02:00
|
|
|
"encoding/xml"
|
2021-04-13 18:01:43 +02:00
|
|
|
"fmt"
|
2021-04-09 12:40:37 +02:00
|
|
|
"io/ioutil"
|
2021-04-07 11:44:58 +02:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2021-04-18 15:39:29 +02:00
|
|
|
func HandlePost(conf *common.Config, db db.MentionRepo) 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 {
|
2021-04-13 18:01:43 +02:00
|
|
|
pingbackError(w, fmt.Errorf("pingback POST: Unable to read body: %v", err))
|
2021-04-12 09:51:19 +02:00
|
|
|
return
|
2021-04-09 12:40:37 +02:00
|
|
|
}
|
|
|
|
rpc := &XmlRPCMethodCall{}
|
|
|
|
err = xml.Unmarshal(body, rpc)
|
|
|
|
if err != nil {
|
2021-04-13 18:01:43 +02:00
|
|
|
pingbackError(w, fmt.Errorf("pingback POST: Unable to unmarshal XMLRPC %s: %v", body, err))
|
2021-04-12 09:51:19 +02:00
|
|
|
return
|
2021-04-09 12:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !validate(rpc, conf) {
|
2021-04-13 18:01:43 +02:00
|
|
|
pingbackError(w, fmt.Errorf("pingback POST: malformed pingback request: %s", body))
|
2021-04-09 12:40:37 +02:00
|
|
|
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-13 09:10:32 +02:00
|
|
|
receiver := &recv.Receiver{
|
2021-04-09 12:40:37 +02:00
|
|
|
RestClient: &rest.HttpClient{},
|
|
|
|
Conf: conf,
|
2021-04-18 15:39:29 +02:00
|
|
|
Repo: db,
|
2021-04-09 12:40:37 +02:00
|
|
|
}
|
|
|
|
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.
|
2021-04-13 18:01:43 +02:00
|
|
|
func pingbackError(w http.ResponseWriter, err error) {
|
2021-04-09 12:40:37 +02:00
|
|
|
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>`
|
2022-05-16 14:47:04 +02:00
|
|
|
// No longer interested in pingback errors, these are 99.9% badly formatted spam that clog up syslog
|
|
|
|
// log.Error().Err(err).Msg("Pingback receive went wrong")
|
2021-04-09 12:40:37 +02:00
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write([]byte(xml))
|
2021-04-07 11:44:58 +02:00
|
|
|
}
|