diff --git a/default.go b/default.go index 4b20d3a..616d639 100644 --- a/default.go +++ b/default.go @@ -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 diff --git a/linkinfo.go b/linkinfo.go index 04e7b72..9d350c9 100644 --- a/linkinfo.go +++ b/linkinfo.go @@ -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 } } diff --git a/service/main.go b/service/main.go index b2083e9..26dba58 100644 --- a/service/main.go +++ b/service/main.go @@ -60,6 +60,10 @@ func main() { }) } + opts = append(opts, &linkinfo.HttpOptions{ + UserAgent: "LinkInfo/1.0", + }) + api = linkinfo.New(opts...) var err error