This repository has been archived by the owner on Oct 11, 2021. It is now read-only.
/
rsscloud.go
324 lines (287 loc) · 8.38 KB
/
rsscloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"bytes"
"database/sql"
"fmt"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/xml"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type RssCloudRequest struct {
RequestMethodName string
MethodName string
Host string
Port uint16
Path string
IsXmlRpc bool
FeedURL string
}
func (r *RssCloudRequest) Unpack(doc *xml.XmlDocument) error {
root := doc.Root()
requestMethods, err := root.Search("methodName/text()")
if err != nil {
return err
}
if len(requestMethods) != 1 {
return fmt.Errorf("Could not find cloud request's request method")
}
r.RequestMethodName = requestMethods[0].Content()
// Would be nice to get the child nodes but xpath will give elements only, not text nodes.
params, err := root.Search("params/param/value")
if err != nil {
return err
}
if len(params) != 5 {
return fmt.Errorf("Could not unpack cloud request with %d params", len(params))
}
node := params[0].FirstChild()
if node.NodeType() != xml.XML_TEXT_NODE {
return fmt.Errorf("Could not unpack cloud request with first param not text")
}
r.MethodName = node.Content()
node = params[1].FirstChild()
if node.NodeType() != xml.XML_ELEMENT_NODE {
return fmt.Errorf("Could not unpack cloud request with second param not an element")
}
if !(node.Name() == "i4" || node.Name() == "int") {
return fmt.Errorf("Could not unpack cloud request with second param a %s element, not int or i4", node.Name())
}
node = node.FirstChild()
if node.NodeType() != xml.XML_TEXT_NODE {
return fmt.Errorf("Could not unpack cloud request with second param not containing text")
}
port, err := strconv.Atoi(node.Content())
if err != nil {
return err
}
r.Port = uint16(port)
node = params[2].FirstChild()
if node.NodeType() != xml.XML_TEXT_NODE {
return fmt.Errorf("Could not unpack cloud request with third param not text")
}
r.Path = node.Content()
node = params[3].FirstChild()
if node.NodeType() != xml.XML_TEXT_NODE {
return fmt.Errorf("Could not unpack cloud request with fourth param not text")
}
r.IsXmlRpc = strings.TrimSpace(node.Content()) == "xml-rpc"
node = params[4].FirstChild()
if node.NodeType() != xml.XML_ELEMENT_NODE {
return fmt.Errorf("Could not unpack cloud request with fifth param not an element")
}
if node.Name() != "array" {
return fmt.Errorf("Could not unpack cloud request with fifth param a %s element, not array", node.Name())
}
params, err = node.Search("data/value/text()")
if err != nil {
return err
}
if len(params) < 1 {
return fmt.Errorf("Could not unpack cloud request with fifth param containing no data values")
}
r.FeedURL = params[0].Content()
logr.Debugln("Unpacked cloud request!")
return nil
}
type RssCloud struct {
Id uint64
URL string
Method string
SubscribedUntil time.Time
Created time.Time
}
func NewRssCloud() *RssCloud {
return &RssCloud{0, "", "", time.Unix(0, 0), time.Now().UTC()}
}
func (r *RssCloud) Notify(feedurl string) {
logr.Debugln("Building RSS cloud notification for", r.URL)
body := new(bytes.Buffer)
body.WriteString(`<?xml version="1.0"?>
<methodCall>
<methodName>`)
body.WriteString(r.Method)
body.WriteString(`</methodName>
<params>
<param>
<value>`)
body.WriteString(feedurl)
body.WriteString(`</value>
</param>
</params>
</methodCall>`)
resp, err := http.Post(r.URL, "text/xml", body)
if err != nil {
logr.Errln("Error posting RSS cloud notification to", r.URL, ":", err.Error())
return
}
// TODO: parse resp and see if it was a fault.
if resp == nil {
logr.Errln("Didn't get a response back posting RSS cloud notification", r.URL, "!!!")
return
}
logr.Debugln("Sent RSS cloud notification to", r.URL)
}
func (r *RssCloud) Save() error {
if r.Id == 0 {
return db.Insert(r)
}
_, err := db.Update(r)
return err
}
func RssCloudByURL(url string) (*RssCloud, error) {
rows, err := db.Select(RssCloud{},
"SELECT id, method, subscribedUntil, created FROM rsscloud WHERE url = $1",
url)
if err != nil {
return nil, err
}
rssCloud := rows[0].(*RssCloud)
return rssCloud, nil
}
func ActiveRssClouds() ([]*RssCloud, error) {
rows, err := db.Select(RssCloud{},
"SELECT id, url, method, subscribedUntil, created FROM rsscloud WHERE subscribedUntil > $1",
time.Now().UTC())
if err != nil {
return nil, err
}
clouds := make([]*RssCloud, len(rows))
for i, row := range rows {
clouds[i] = row.(*RssCloud)
}
return clouds, nil
}
func NotifyRssCloud(feedurl string) {
logr.Debugln("Sending RSS cloud notifications")
clouds, err := ActiveRssClouds()
if err != nil {
logr.Errln("Error finding RSS clouds to notify of feed update:", err.Error())
return
}
for _, cloud := range clouds {
go cloud.Notify(feedurl)
}
}
func writeXmlRpcError(w http.ResponseWriter, err error) {
logr.Errln("Error serving rss cloud request:", err.Error())
output := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>4</int></value>
</member>
<member>
<name>faultString</name>
<value><string>%s</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>`, err.Error())
w.Header().Set("Content-Type", "text/xml")
w.Header().Set("Content-Length", strconv.Itoa(len(output)))
w.Write([]byte(output))
}
func rssCloud(w http.ResponseWriter, r *http.Request) {
logr.Debugln("Yay a cloud request!")
if r.Method != "POST" {
w.Header().Set("Allow", "POST")
http.Error(w, "POST is required", http.StatusMethodNotAllowed)
return
}
bodyBytes := make([]byte, r.ContentLength)
_, err := r.Body.Read(bodyBytes)
if err != nil {
logr.Errln("Could not read request body:", err.Error())
http.Error(w, "Could not read body: "+err.Error(), http.StatusInternalServerError)
return
}
requestDoc, err := gokogiri.ParseXml(bodyBytes)
if err != nil {
writeXmlRpcError(w, err)
return
}
request := new(RssCloudRequest)
err = request.Unpack(requestDoc)
if err != nil {
writeXmlRpcError(w, err)
return
}
hostname := r.Header.Get("X-Forwarded-For")
if hostname == "" {
hostname = r.RemoteAddr
} else {
hostnames := strings.SplitN(hostname, ",", 2)
hostname = hostnames[0]
}
host, _, err := net.SplitHostPort(hostname)
request.Host = host
if request.RequestMethodName != "cloud.notify" {
writeXmlRpcError(w, fmt.Errorf("Unknown method %s", request.RequestMethodName))
return
}
if !request.IsXmlRpc {
writeXmlRpcError(w, fmt.Errorf("Only XML-RPC is supported"))
return
}
// TODO: use https as appropriate here? er, does river2 support cloud endpoints on HTTPS?
if request.FeedURL != fmt.Sprintf("http://%s/rss", r.Host) {
writeXmlRpcError(w, fmt.Errorf("RSS URL %s is not a feed managed here", request.FeedURL))
return
}
logr.Debugln("Yay, asked to call back to http://", request.Host, ":", request.Port, request.Path,
"with method", request.MethodName, "!")
url, _ := url.Parse("/")
if request.Port == 443 {
url.Scheme = "https"
} else {
url.Scheme = "http"
}
if request.Port == 80 || request.Port == 443 {
url.Host = request.Host
} else {
url.Host = net.JoinHostPort(request.Host, strconv.Itoa(int(request.Port)))
}
url.Path = request.Path
urlString := url.String()
rssCloud, err := RssCloudByURL(urlString)
if err == sql.ErrNoRows {
// That's cool.
} else if err != nil {
logr.Errln("Error loading rsscloud for URL", urlString, ":", err.Error())
http.Error(w, "error looking for rsscloud for URL "+urlString, http.StatusInternalServerError)
return
}
if rssCloud == nil {
rssCloud = NewRssCloud()
rssCloud.URL = urlString
}
rssCloud.Method = request.MethodName
// Subscribe until 25 hours from now.
rssCloud.SubscribedUntil = time.Now().Add(time.Duration(25) * time.Hour).UTC()
err = rssCloud.Save()
if err != nil {
logr.Errln("Error saving rsscloud for URL", urlString, ":", err.Error())
http.Error(w, "error saving rsscloud for URL "+urlString, http.StatusInternalServerError)
return
}
output := `<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>`
w.Header().Set("Content-Type", "text/xml")
w.Header().Set("Content-Length", strconv.Itoa(len(output)))
w.Write([]byte(output))
}