Skip to content

Commit

Permalink
use original url instead of panic, to align with forceIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ZekeLu committed May 14, 2021
1 parent 1a19982 commit 824741b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
6 changes: 1 addition & 5 deletions allocate.go
Expand Up @@ -489,13 +489,9 @@ func CombinedOutput(w io.Writer) ExecAllocatorOption {
// Because it contains "/devtools/browser/" and will be considered
// as a valid websocket debugger URL.
func NewRemoteAllocator(parent context.Context, url string) (context.Context, context.CancelFunc) {
url, err := detectURL(url)
if err != nil {
panic(fmt.Sprintf("failed to detect the websocket debugger url: %v", err))
}
ctx, cancel := context.WithCancel(parent)
c := &Context{Allocator: &RemoteAllocator{
wsURL: url,
wsURL: detectURL(url),
}}
ctx = context.WithValue(ctx, contextKey{}, c)
return ctx, cancel
Expand Down
12 changes: 6 additions & 6 deletions util.go
Expand Up @@ -40,37 +40,37 @@ func forceIP(urlstr string) string {
// The original URL with the following formats are accepted:
// * ws://127.0.0.1:9222/
// * http://127.0.0.1:9222/
func detectURL(urlstr string) (string, error) {
func detectURL(urlstr string) string {
if strings.Contains(urlstr, "/devtools/browser/") {
return urlstr, nil
return urlstr
}

// replace the scheme and path to construct the URL like:
// http://127.0.0.1:9222/json/version
u, err := url.Parse(urlstr)
if err != nil {
return "", err
return urlstr
}
u.Scheme = "http"
u.Path = "/json/version"

// to get "webSocketDebuggerUrl" in the response
resp, err := http.Get(u.String())
if err != nil {
return "", err
return urlstr
}
defer resp.Body.Close()

var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", err
return urlstr
}
// the browser will construct the debugger URL using the "host" header of the /json/version request.
// for example, run headless-shell in a container: docker run -d -p 9000:9222 chromedp/headless-shell:latest
// then: curl http://127.0.0.1:9000/json/version
// and the debugger URL will be something like: ws://127.0.0.1:9000/devtools/browser/...
wsURL := result["webSocketDebuggerUrl"].(string)
return wsURL, nil
return wsURL
}

func runListeners(list []cancelableListener, ev interface{}) []cancelableListener {
Expand Down

0 comments on commit 824741b

Please sign in to comment.