Add UserAgent
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Tyler 2019-10-16 19:25:59 -04:00
parent e245573361
commit 3bd0ce72a5
3 changed files with 46 additions and 8 deletions

View File

@ -64,10 +64,21 @@ func (api *LinkInfoApi) DefaultLinkHandler(link string) (*LinkInfo, error) {
return nil, errors.New("url is a local ip address")
}
var req *http.Request
var res *http.Response
for i := 0; i < 10; i++ {
res, err = api.Client.Head(link)
req, err = http.NewRequest(http.MethodHead, link, nil)
if err != nil {
return nil, err
}
if api.UserAgent != "" {
req.Header.Set("User-Agent", api.UserAgent)
}
res, err = api.Client.Do(req)
if err != nil {
return nil, err
@ -140,6 +151,10 @@ func (api *LinkInfoApi) detectContentType(link, defaultType string) string {
return defaultType
}
if api.UserAgent != "" {
req.Header.Set("User-Agent", api.UserAgent)
}
req.Header.Set("Range", "bytes=0-512")
res, err := api.Client.Do(req)
@ -170,7 +185,17 @@ var (
)
func (api *LinkInfoApi) retrieveHtmlLinkTitle(i *LinkInfo, link string) error {
res, err := api.Client.Get(link)
req, err := http.NewRequest(http.MethodGet, link, nil)
if err != nil {
return err
}
if api.UserAgent != "" {
req.Header.Set("User-Agent", api.UserAgent)
}
res, err := api.Client.Do(req)
if err != nil {
return err

View File

@ -12,7 +12,8 @@ type LinkHandler struct {
}
type LinkInfoApi struct {
Client *http.Client
Client *http.Client
UserAgent string
Imgur *ImgurInfoApi
Youtube *YoutubeInfoApi
@ -29,22 +30,30 @@ type LinkInfo struct {
Redirects []string `json:"redirects,omitempty"`
}
type HttpOptions struct {
Option
UserAgent string
}
func New(opts ...Option) *LinkInfoApi {
api := &LinkInfoApi{
Client: &http.Client{
Timeout: 60 * time.Second,
Timeout: 15 * time.Second,
},
}
for _, opt := range opts {
switch opt.(type) {
switch o := opt.(type) {
case *ImgurOptions:
api.Imgur = &ImgurInfoApi{api, opt.(*ImgurOptions)}
api.Imgur = &ImgurInfoApi{api, o}
case *YoutubeOptions:
api.Youtube = &YoutubeInfoApi{api, opt.(*YoutubeOptions)}
api.Youtube = &YoutubeInfoApi{api, o}
case *TwitterOptions:
api.Twitter = &TwitterInfoApi{opts: opt.(*TwitterOptions)}
api.Twitter = &TwitterInfoApi{opts: o}
api.Twitter.Init()
case *HttpOptions:
api.UserAgent = o.UserAgent
}
}

View File

@ -60,6 +60,10 @@ func main() {
})
}
opts = append(opts, &linkinfo.HttpOptions{
UserAgent: "LinkInfo/1.0",
})
api = linkinfo.New(opts...)
var err error