-
Notifications
You must be signed in to change notification settings - Fork 0
/
webmention.go
156 lines (136 loc) · 3.09 KB
/
webmention.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
// webmention project webmention.go
package webmention
import (
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/andyleap/microformats"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
type WebMention struct {
mentionQueue chan *mention
timer *time.Timer
Mention func(source, target *url.URL, sourceData *microformats.Data)
}
func New() *WebMention {
wm := &WebMention{
mentionQueue: make(chan *mention, 100),
}
wm.timer = time.NewTimer(5 * time.Second)
go func() {
for _ = range wm.timer.C {
wm.process()
}
}()
return wm
}
type mention struct {
source *url.URL
target *url.URL
}
func (wm *WebMention) GetTargetEndpoint(target *url.URL) (*url.URL, error) {
resp, err := http.Get(target.String())
if err != nil {
return nil, err
}
links := GetHeaderLinks(resp.Header["Link"])
for _, link := range links {
for _, rel := range link.Params["rel"] {
if rel == "webmention" || rel == "http://webmention.org" {
return link.URL, nil
}
}
}
parser := microformats.New()
mf2data := parser.Parse(resp.Body)
resp.Body.Close()
for _, link := range mf2data.Rels["webmention"] {
wmurl, err := url.Parse(link)
if err != nil {
fmt.Println(err)
continue
}
return wmurl, nil
}
return nil, nil
}
func (wm *WebMention) SendNotification(target *url.URL, source *url.URL) {
endpoint, err := wm.GetTargetEndpoint(target)
if err != nil {
fmt.Println(err)
return
}
if endpoint == nil {
return
}
values := make(url.Values)
values.Set("source", source.String())
values.Set("target", target.String())
http.PostForm(endpoint.String(), values)
}
func (wm *WebMention) WebMentionEndpoint(rw http.ResponseWriter, req *http.Request) {
source := req.FormValue("source")
target := req.FormValue("target")
if source != "" && target != "" {
sourceurl, _ := url.Parse(source)
targeturl, _ := url.Parse(target)
wm.mentionQueue <- &mention{
sourceurl,
targeturl,
}
}
rw.WriteHeader(http.StatusAccepted)
}
func (wm *WebMention) process() {
mention := <-wm.mentionQueue
resp, err := http.Get(mention.source.String())
if err != nil {
log.Printf("Error getting source %s: %s", mention.source, err)
return
}
body, err := html.Parse(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("Error parsing source %s: %s", mention.source, err)
return
}
found := searchLinks(body, mention.target)
if found {
p := microformats.New()
data := p.ParseNode(body)
wm.Mention(mention.source, mention.target, data)
}
}
func searchLinks(node *html.Node, link *url.URL) bool {
if node.Type == html.ElementNode {
if node.DataAtom == atom.A {
if href := getAttr(node, "href"); href != "" {
target, err := url.Parse(href)
if err == nil {
if target.String() == link.String() {
return true
}
}
}
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
found := searchLinks(c, link)
if found {
return found
}
}
return false
}
func getAttr(node *html.Node, name string) string {
for _, attr := range node.Attr {
if strings.EqualFold(attr.Key, name) {
return attr.Val
}
}
return ""
}