This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkParser.go
67 lines (55 loc) · 1.44 KB
/
LinkParser.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
package main
import (
"errors"
"net/http"
"net/url"
"strings"
)
func ValidLink(link string) bool {
_, e := url.Parse(link)
if e != nil {
return false
}
if !strings.Contains(strings.ToLower(link), "zippyshare.com") {
return false // Invalid Link Domain
}
//return strings.HasSuffix(link, "file.html")
return true
}
func LimitRedirects(resp *http.Response, maxRedirectCount int) (*http.Response, error) {
for i := 0; i < maxRedirectCount; i++ {
if resp.StatusCode == http.StatusTemporaryRedirect || resp.StatusCode == http.StatusPermanentRedirect {
redirectLocation := resp.Header.Get("Location")
//Redirects to nowhere
if redirectLocation == stringEmpty {
return nil, errors.New("no redirect location")
}
r, e := http.Get(redirectLocation)
if LogErrorIfNecessary(stringEmpty, &e) {
return nil, e
}
_ = resp.Body.Close()
resp = r
} else {
if resp.Header.Get("Location") != stringEmpty {
return nil, errors.New("redirect loop")
} else {
//Return properly
return resp, nil
}
}
}
return nil, errors.New("too many redirects")
}
func GetLinkContent(link string) (*http.Response, error) {
l, e1 := http.Get(link)
if LogErrorIfNecessary(stringEmpty, &e1) {
return nil, e1
}
l, e2 := LimitRedirects(l, 5)
//Only allow OK results to be returned
if e1 != nil || e2 != nil || l.StatusCode != http.StatusOK {
return nil, errors.New(http.StatusText(l.StatusCode))
}
return l, nil
}