-
Notifications
You must be signed in to change notification settings - Fork 11
/
html.go
125 lines (98 loc) · 2.5 KB
/
html.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
package latest
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"github.com/hashicorp/go-version"
)
// HTML is used to fetch version information from a single HTML page.
type HTML struct {
// URL is HTML page URL which include version information.
URL string
// Scrap is used to scrap a single HTML page and extract version information.
// See more about HTMLScrap interface.
// By default, it does nothing, just return HTML contents.
Scrap HTMLScrap
}
// HTMLScrap is used to scrap a single HTML page and extract version information.
type HTMLScrap interface {
// Exec is called from Fetch after fetching a HTMl page from source.
// It must return version information as string list format.
Exec(r io.Reader) ([]string, *Meta, error)
}
type defaultHTMLScrap struct{}
func (s *defaultHTMLScrap) Exec(r io.Reader) ([]string, *Meta, error) {
meta := &Meta{}
b, err := ioutil.ReadAll(r)
if err != nil {
return []string{}, meta, err
}
b = bytes.Replace(b, []byte("\n"), []byte(""), -1)
return []string{string(b[:])}, meta, nil
}
func (h *HTML) scrap() HTMLScrap {
if h.Scrap == nil {
return &defaultHTMLScrap{}
}
return h.Scrap
}
func (h *HTML) Validate() error {
if len(h.URL) == 0 {
return fmt.Errorf("URL must be set")
}
// Check URL can be parsed
if _, err := url.Parse(h.URL); err != nil {
return fmt.Errorf("%s is invalid URL: %s", h.URL, err.Error())
}
return nil
}
func (h *HTML) Fetch() (*FetchResponse, error) {
fr := newFetchResponse()
// URL is validated before call
u, _ := url.Parse(h.URL)
// Create a new request
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return fr, err
}
req.Header.Add("Accept", "application/json")
// Create client
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: func(n, a string) (net.Conn, error) {
return net.DialTimeout(n, a, defaultDialTimeout)
},
}
client := &http.Client{
Transport: t,
}
resp, err := client.Do(req)
if err != nil {
return fr, err
}
if resp.StatusCode != 200 {
return fr, fmt.Errorf("unknown status: %d", resp.StatusCode)
}
scrap := h.scrap()
verStrs, meta, err := scrap.Exec(resp.Body)
if err != nil {
return fr, err
}
if len(verStrs) == 0 {
return fr, fmt.Errorf("version info is not found on %s", h.URL)
}
for _, verStr := range verStrs {
v, err := version.NewVersion(verStr)
if err != nil {
fr.Malformeds = append(fr.Malformeds, verStr)
continue
}
fr.Versions = append(fr.Versions, v)
}
fr.Meta = meta
return fr, nil
}