Skip to content

add chromedp.NewRemoteAllocatorWithHeader #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -526,10 +527,15 @@ func WSURLReadTimeout(t time.Duration) ExecAllocatorOption {
//
// Use chromedp.NoModifyURL to prevent it from modifying the url.
func NewRemoteAllocator(parent context.Context, url string, opts ...RemoteAllocatorOption) (context.Context, context.CancelFunc) {
return NewRemoteAllocatorWithHeader(parent, url, nil, opts...)
}

func NewRemoteAllocatorWithHeader(parent context.Context, url string, header http.Header, opts ...RemoteAllocatorOption) (context.Context, context.CancelFunc) {
a := &RemoteAllocator{
wsURL: url,
modifyURLFunc: func(ctx context.Context, wsURL string) (string, error) {
return modifyURL(ctx, wsURL)
wsURL: url,
wsHeader: header,
modifyURLFunc: func(ctx context.Context, wsURL string, wsHeader http.Header) (string, error) {
return modifyURL(ctx, wsURL, wsHeader)
},
}
for _, o := range opts {
Expand All @@ -549,7 +555,8 @@ type RemoteAllocatorOption = func(*RemoteAllocator)
// process via a websocket URL.
type RemoteAllocator struct {
wsURL string
modifyURLFunc func(ctx context.Context, wsURL string) (string, error)
wsHeader http.Header
modifyURLFunc func(ctx context.Context, wsURL string, wsHeader http.Header) (string, error)

wg sync.WaitGroup
}
Expand All @@ -561,10 +568,10 @@ func (a *RemoteAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (
return nil, ErrInvalidContext
}

wsURL := a.wsURL
wsURL, wsHeader := a.wsURL, a.wsHeader
var err error
if a.modifyURLFunc != nil {
wsURL, err = a.modifyURLFunc(ctx, wsURL)
wsURL, err = a.modifyURLFunc(ctx, wsURL, wsHeader)
if err != nil {
return nil, fmt.Errorf("failed to modify wsURL: %w", err)
}
Expand All @@ -583,7 +590,7 @@ func (a *RemoteAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (
a.wg.Done()
}()

browser, err := NewBrowser(wctx, wsURL, opts...)
browser, err := NewBrowserWithHeader(wctx, wsURL, wsHeader, opts...)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"os"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -79,6 +80,10 @@ type Browser struct {
// NewBrowser creates a new browser. Typically, this function wouldn't be called
// directly, as the Allocator interface takes care of it.
func NewBrowser(ctx context.Context, urlstr string, opts ...BrowserOption) (*Browser, error) {
return NewBrowserWithHeader(ctx, urlstr, nil, opts...)
}

func NewBrowserWithHeader(ctx context.Context, urlstr string, header http.Header, opts ...BrowserOption) (*Browser, error) {
b := &Browser{
LostConnection: make(chan struct{}),
closingGracefully: make(chan struct{}),
Expand Down Expand Up @@ -109,7 +114,7 @@ func NewBrowser(ctx context.Context, urlstr string, opts ...BrowserOption) (*Bro
}

var err error
b.conn, err = DialContext(dialCtx, urlstr, WithConnDebugf(b.dbgf))
b.conn, err = DialContextWithHeader(dialCtx, urlstr, header, WithConnDebugf(b.dbgf))
if err != nil {
return nil, fmt.Errorf("could not dial %q: %w", urlstr, err)
}
Expand Down
12 changes: 10 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"io"
"net"
"net/http"

"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
Expand Down Expand Up @@ -41,9 +42,16 @@ type Conn struct {
}

// DialContext dials the specified websocket URL using gobwas/ws.
func DialContext(ctx context.Context, urlstr string, opts ...DialOption) (*Conn, error) {
func DialContext(ctx context.Context, urlstr string, header http.Header, opts ...DialOption) (*Conn, error) {
return DialContextWithHeader(ctx, urlstr, nil, opts...)
}

func DialContextWithHeader(ctx context.Context, urlstr string, header http.Header, opts ...DialOption) (*Conn, error) {
// connect
conn, br, _, err := ws.Dial(ctx, urlstr)
dialer := ws.Dialer{
Header: ws.HandshakeHeaderHTTP(header),
}
conn, br, _, err := dialer.Dial(ctx, urlstr)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func resolveHost(ctx context.Context, host string) (string, error) {
// - ws://127.0.0.1:9222/
// - http://127.0.0.1:9222/
// - http://container-name:9222/
func modifyURL(ctx context.Context, urlstr string) (string, error) {
func modifyURL(ctx context.Context, urlstr string, header http.Header) (string, error) {
lctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

Expand Down Expand Up @@ -98,6 +98,7 @@ func modifyURL(ctx context.Context, urlstr string) (string, error) {
if err != nil {
return "", err
}
req.Header = header
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
Expand Down