Add UserAgent
continuous-integration/drone/push Build is passing Details

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

View File

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