Cookies support added.

This commit is contained in:
Musab Gültekin
2019-06-17 13:31:19 +03:00
parent dd6687f976
commit a5ec28664d
5 changed files with 88 additions and 21 deletions

View File

@ -1,14 +1,27 @@
package internal
import (
"errors"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
var (
// ErrNoCookieJar is the error type for missing cookie jar
ErrNoCookieJar = errors.New("cookie jar is not available")
)
// Client is a small wrapper around *http.Client to provide new methods.
type Client struct {
*http.Client
}
// NewClient creates http.Client with modified values for typical web scraper
func NewClient() *http.Client {
return &http.Client{
func NewClient(cookiesDisabled bool) *Client {
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
@ -24,6 +37,35 @@ func NewClient() *http.Client {
},
Timeout: time.Second * 180, // Google's timeout
}
if !cookiesDisabled {
client.Jar, _ = cookiejar.New(nil)
}
return &Client{Client: client}
}
// SetCookies handles the receipt of the cookies in a reply for the given URL
func (c *Client) SetCookies(URL string, cookies []*http.Cookie) error {
if c.Jar == nil {
return ErrNoCookieJar
}
u, err := url.Parse(URL)
if err != nil {
return err
}
c.Jar.SetCookies(u, cookies)
return nil
}
// Cookies returns the cookies to send in a request for the given URL.
func (c *Client) Cookies(URL string) []*http.Cookie {
if c.Jar == nil {
return nil
}
parsedURL, err := url.Parse(URL)
if err != nil {
return nil
}
return c.Jar.Cookies(parsedURL)
}
// SetDefaultHeader sets header if not exists before