This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
discovery.go
123 lines (114 loc) · 2.72 KB
/
discovery.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
package tent
import (
"bytes"
"errors"
"io"
"mime"
"code.google.com/p/go.net/html"
"github.com/tent/http-link-go"
)
var ErrNotTentEntity = errors.New("tent: not a valid Tent entity")
var ErrInvalidLink = errors.New("tent: invalid meta Link")
func Discover(entity string) (*MetaPost, error) {
req, err := NewRequest("HEAD", entity, nil, nil)
if req.URL.Path == "" {
req.URL.Path = "/"
}
res, err := HTTP.Do(req)
if err != nil {
return nil, newRequestError(err, req)
}
res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return nil, newResponseError(ErrBadStatusCode, res)
}
if linkHeader := res.Header.Get("Link"); linkHeader != "" {
links, err := link.Parse(linkHeader)
if err != nil {
return nil, ErrInvalidLink
}
var metaLinks []string
for _, l := range links {
if l.Rel == RelMetaPost {
metaLinks = append(metaLinks, l.URI)
}
}
if len(metaLinks) > 0 {
return getMetaPost(metaLinks, res.Request.URL)
}
}
// we didn't get anything with the HEAD request, so let's try to GET HTML links
req, _ = NewRequest("GET", entity, nil, nil)
req.Header.Set("Accept", "text/html")
res, err = HTTP.Do(req)
if err != nil {
return nil, newRequestError(err, req)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, newResponseError(ErrBadStatusCode, res)
}
contentType := res.Header.Get("Content-Type")
if contentType == "" {
return nil, ErrNotTentEntity
}
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, err
}
if mediaType != "text/html" {
return nil, ErrNotTentEntity
}
var links []string
if ok := timeoutRead(res.Body, func() {
links, err = parseHTMLMetaLinks(res.Body)
}); !ok {
return nil, newResponseError(ErrReadTimeout, res)
}
if err != nil {
return nil, err
}
if len(links) > 0 {
return getMetaPost(links, res.Request.URL)
}
return nil, ErrNotTentEntity
}
func parseHTMLMetaLinks(data io.Reader) (links []string, err error) {
t := html.NewTokenizer(data)
loop:
for {
switch t.Next() {
case html.ErrorToken:
err = t.Err()
if err == io.EOF {
err = nil
}
break loop
case html.StartTagToken, html.SelfClosingTagToken:
name, attrs := t.TagName()
if !attrs || !bytes.Equal(name, []byte("link")) {
continue loop
}
var href string
var haveRel, metaRel bool
for {
key, val, more := t.TagAttr()
if bytes.Equal(key, []byte("href")) {
href = string(val)
} else if bytes.Equal(key, []byte("rel")) {
haveRel = true
if bytes.Equal(val, []byte(RelMetaPost)) {
metaRel = true
}
}
if !more || haveRel && !metaRel || metaRel && href != "" {
break
}
}
if metaRel && href != "" {
links = append(links, href)
}
}
}
return
}