This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.rb
122 lines (105 loc) · 2.99 KB
/
server.rb
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
require 'sinatra'
require 'sinatra'
require 'json'
require 'nokogiri'
require 'securerandom'
require 'haml'
require './database.rb'
require './page.rb'
set :bind, "0.0.0.0"
set :server, "thin"
set :protection, except: :ip_spoofing
use Rack::Session::Pool
helpers do
def challenge!(url)
begin
challenge = SecureRandom.hex(16)
response = RestClient.get url, {:params => {:challenge => challenge}}
if response != challenge
throw(:halt, [401, "Echoed value didn't match challenge"])
end
rescue => error
puts "error confirming subscription: #{error}"
throw(:halt, [401, "An Error occured while confirming subscription (echo challenge)\n"])
end
end
end
configure do
pollThread = Thread.new() {
while true
puts "poll for updates"
pages = Database.new.getPages(false)
pages.each do |page|
Thread.new() {
if page.hasUpdate?
page.notifySubscribers
end
}
sleep 1
end
sleep 3600
end
}
leaseThread = Thread.new() {
while true
pages = Database.new.getPagesToRenew(3900)
pages.each do |page|
Thread.new() {
page.subscribe
}
sleep 1
end
sleep 3600
end
}
end
get '/' do
haml :index
end
post '/watches' do
begin
data = JSON.parse(request.body.string)
rescue JSON::ParserError => error
puts "could not decode data: #{error}"
return
end
challenge! data["callback"]
data["urls"].each do |url|
page = Page.new(url, data["callback"])
subscribed = page.subscribe
page.save(subscribed)
end
end
delete '/watches' do
begin
data = JSON.parse(request.body.string)
rescue JSON::ParserError => error
puts "could not decode data: #{error}"
return
end
challenge! data["callback"]
data["urls"].each do |url|
Page.new(url, data["callback"]).delete
end
end
post '/pubsubhubbub' do
Page.new(Nokogiri.XML(request.body).xpath('/rss/channel/link').text).notifySubscribers
end
get '/pubsubhubbub' do
if Database.new.register?(params["hub.topic"]) || Database.new.isWatched?(params["hub.topic"]) # if already saved, this is a renewal
Database.new.finishRegisterRequest(params["hub.topic"], params["hub.lease_seconds"].to_i)
return params["hub.challenge"]
end
end
post '/rssCloud' do
Page.new(params["origin"]).notifySubscribers
end
get '/rssCloud' do
if Database.new.register?(params["url"]) || Database.new.isWatched?(params["url"])
Database.new.finishRegisterRequest(params["url"], 86400) # rsscloud-subscriptions are always only valid for 24h
return params["challenge"]
end
end
get '/test' do
return params["challenge"]
end