2018-12-22 06:14:41 +02:00
|
|
|
package cdp
|
2018-10-08 03:23:36 +02:00
|
|
|
|
2019-09-05 17:49:21 +02:00
|
|
|
import "github.com/MontFerret/ferret/pkg/drivers"
|
|
|
|
|
2018-10-08 03:23:36 +02:00
|
|
|
type (
|
|
|
|
Options struct {
|
2019-03-16 01:59:05 +02:00
|
|
|
Name string
|
|
|
|
Proxy string
|
|
|
|
UserAgent string
|
|
|
|
Address string
|
|
|
|
KeepCookies bool
|
2019-09-05 17:49:21 +02:00
|
|
|
Headers drivers.HTTPHeaders
|
|
|
|
Cookies drivers.HTTPCookies
|
2018-10-08 03:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Option func(opts *Options)
|
|
|
|
)
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
const DefaultAddress = "http://127.0.0.1:9222"
|
|
|
|
|
2018-12-01 02:30:55 +02:00
|
|
|
func newOptions(setters []Option) *Options {
|
|
|
|
opts := new(Options)
|
2019-03-16 01:59:05 +02:00
|
|
|
opts.Name = DriverName
|
|
|
|
opts.Address = DefaultAddress
|
2018-12-01 02:30:55 +02:00
|
|
|
|
|
|
|
for _, setter := range setters {
|
|
|
|
setter(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
func WithAddress(address string) Option {
|
2018-12-01 02:30:55 +02:00
|
|
|
return func(opts *Options) {
|
2019-03-07 05:54:12 +02:00
|
|
|
if address != "" {
|
2019-03-16 01:59:05 +02:00
|
|
|
opts.Address = address
|
2019-03-07 05:54:12 +02:00
|
|
|
}
|
2018-12-01 02:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 03:23:36 +02:00
|
|
|
func WithProxy(address string) Option {
|
|
|
|
return func(opts *Options) {
|
2019-03-16 01:59:05 +02:00
|
|
|
opts.Proxy = address
|
2018-10-08 03:23:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 04:18:57 +02:00
|
|
|
|
|
|
|
func WithUserAgent(value string) Option {
|
|
|
|
return func(opts *Options) {
|
2019-03-16 01:59:05 +02:00
|
|
|
opts.UserAgent = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithKeepCookies() Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.KeepCookies = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCustomName(name string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Name = name
|
2018-10-08 04:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 17:49:21 +02:00
|
|
|
|
|
|
|
func WithHeader(name string, value []string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
if opts.Headers == nil {
|
|
|
|
opts.Headers = make(drivers.HTTPHeaders)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Headers[name] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithHeaders(headers drivers.HTTPHeaders) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
if opts.Headers == nil {
|
|
|
|
opts.Headers = make(drivers.HTTPHeaders)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range headers {
|
|
|
|
opts.Headers[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCookie(cookie drivers.HTTPCookie) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
if opts.Cookies == nil {
|
|
|
|
opts.Cookies = make(drivers.HTTPCookies)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Cookies[cookie.Name] = cookie
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCookies(cookies []drivers.HTTPCookie) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
if opts.Cookies == nil {
|
|
|
|
opts.Cookies = make(drivers.HTTPCookies)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cookies {
|
|
|
|
opts.Cookies[c.Name] = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|