68 lines
1.1 KiB
Go
68 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// ExpandURL ExpandURL
|
|
func ExpandURL(url string) (string, error) {
|
|
expandedURL := url
|
|
|
|
client := &http.Client{
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
expandedURL = req.URL.String()
|
|
return nil
|
|
},
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return expandedURL, nil
|
|
}
|
|
|
|
// ExpandURL2 ExpandURL2
|
|
func ExpandURL2(myURL, searchURL string) (string, error) {
|
|
nextURL := myURL
|
|
var i int
|
|
for i < 100 {
|
|
client := &http.Client{
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
}}
|
|
|
|
resp, err := client.Get(nextURL)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
//fmt.Println("StatusCode:", resp.StatusCode)
|
|
//fmt.Println(resp.Request.URL)
|
|
|
|
if resp.StatusCode == 200 {
|
|
//fmt.Println("Done!")
|
|
break
|
|
} else {
|
|
nextURLTest := resp.Header.Get("Location")
|
|
if strings.Contains(nextURL, searchURL) {
|
|
break
|
|
}
|
|
nextURL = nextURLTest
|
|
i++
|
|
}
|
|
}
|
|
|
|
return nextURL, nil
|
|
}
|