-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.go
136 lines (111 loc) · 2.78 KB
/
push.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
package main;
import (
"net/http"
"fmt"
"log"
"io/ioutil"
"strconv"
"time"
"github.com/kr/pretty"
)
// This is the minimal amount of information allowed for a PuSH subscription.
type Pushstruct struct {
Callback string
Action string
Topic string
}
func HubCB(w http.ResponseWriter, r *http.Request) {
pledge();
log.Println("Got hit up on the hub")
r.ParseForm();
callback := r.Form["hub.callback"][0]
topic := r.Form["hub.topic"][0]
mode := r.Form["hub.mode"][0]
secret := r.Form.Get("hub.secret")
w.WriteHeader(202)
w.Write([]byte(""))
log.Println("callback: " + callback)
log.Println("mode: " + mode)
log.Println("topic: " + topic)
go HubConfirm(callback, topic, secret);
}
func HubConfirm(c string, t string, s string) {
dbh := ctx.dbh
client := &http.Client{}
req, _ := http.NewRequest("GET", c, nil);
params := req.URL.Query()
params.Add("hub.topic", t)
params.Add("hub.mode", "subscribe")
params.Add("hub.lease_seconds", strconv.Itoa((1 << 31) - 1))
// q.Set("hub.topic", t)
// q.Set("hub.mode", "subscribe")
// q.Set("hub.lease_seconds", strconv.Itoa((1 << 31) - 1))
/* req.Header.Add("hub_topic", t)
req.Header.Add("hub_mode", "subscribe")
req.Header.Add("hub_lease_seconds", strconv.Itoa((1 << 31) - 1))*/
challenge := genKey()
params.Add("hub.challenge",challenge);
req.URL.RawQuery = params.Encode()
pretty.Println(req.URL.Query())
if (req.URL.Query().Get("hub.mode") == "") {
log.Fatal("Query setting is broken. Somehow.")
}
time.Sleep(time.Second * 5);
resp, err := client.Do(req)
if (err != nil) {
log.Fatal(err)
}
str,err := ioutil.ReadAll(resp.Body)
if (err != nil) {
log.Fatal(err);
}
if ((string(str)) != challenge) {
fmt.Println("Things didn't match up!")
fmt.Println(str)
fmt.Println(challenge)
log.Fatal("Mismatched challenge");
}
log.Println("Putting " + c + " into db");
tx, err := dbh.Begin()
if (err != nil) {
log.Fatal(err);
}
insert, err := tx.Prepare("insert into subscribers(callback,challenge) values(?,?);");
defer insert.Close()
if (err != nil) {
log.Fatal(err)
}
_, err = insert.Exec(c, s)
if (err != nil) {
log.Fatal(err)
}
err = tx.Commit();
if (err != nil) {
log.Fatal(err)
}
}
/*
type subunsub int
func PushStruct(action subunsub, topic string) {
ret := Pushstruct{
Action: action,
Topic: topic,
Callback: config.Server + "/push/callback",
}
}
func PushSubscribe(PushStruct s, hub string) {
client := http.Client{}
request, _ := http.NewRequest("GET", string, nil)
var action string
switch s.Action {
case SUBSCRIBE:
action = "subscribe"
case UNSUBSCRIBE:
action = "unsubscribe"
}
request.Header.Add("hub.mode", s.action)
request.Header.Add("hub.callback", s.callback)
request.Header.Add("hub.topic", s.topic)
request.Header.Add("From", config.Owner + "@" + config.Server)
}
*/