diff --git a/connect/headers.go b/connect/headers.go new file mode 100644 index 0000000000000000000000000000000000000000..b3ab18a018314d00b5ffe9aabad8943d959b1b6a --- /dev/null +++ b/connect/headers.go @@ -0,0 +1,9 @@ +//go:build !js || !wasm + +package connect + +import "net/http" + +func (wc *webConn) addHeaders(header http.Header) http.Header { + return header +} diff --git a/connect/headers_js.go b/connect/headers_js.go new file mode 100644 index 0000000000000000000000000000000000000000..17620bc905aec1c2f49c50ff375626c4745f297d --- /dev/null +++ b/connect/headers_js.go @@ -0,0 +1,10 @@ +//go:build js && wasm + +package connect + +import "net/http" + +func (wc *webConn) addHeaders(header http.Header) http.Header { + req.Header.Add("js.fetch:mode", "no-cors") + return header +} diff --git a/connect/webConn.go b/connect/webConn.go index 3f899829b1db63521757b29fb66c25c595306b2e..4eb69ce246bb919c8f1471a893b4fbe071e2a31e 100644 --- a/connect/webConn.go +++ b/connect/webConn.go @@ -191,10 +191,10 @@ func (wc *webConn) IsOnline() (time.Duration, bool) { addr := wc.h.GetAddress() pingTimeout := wc.h.params.PingTimeout - return isOnlineHelper(addr, pingTimeout) + return wc.isOnlineHelper(addr, pingTimeout) } -func isOnlineHelper(addr string, pingTimeout time.Duration) (time.Duration, bool) { +func (wc *webConn) isOnlineHelper(addr string, pingTimeout time.Duration) (time.Duration, bool) { start := time.Now() tr := &http.Transport{ TLSClientConfig: &tls.Config{ @@ -225,7 +225,7 @@ func isOnlineHelper(addr string, pingTimeout time.Duration) (time.Duration, bool } // IMPORTANT - enables better HTTP(S) discovery, because many browsers block CORS by default. - //req.Header.Add("js.fetch:mode", "no-cors") + req.Header = wc.addHeaders(req.Header) jww.TRACE.Printf("(GO request): %+v", req) req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) diff --git a/connect/webConn_test.go b/connect/webConn_test.go index 8bca386662a2710b8cb36e0dd35b3361c9fe0067..a84c92c08632898b037569dff07d4cbc21d933b7 100644 --- a/connect/webConn_test.go +++ b/connect/webConn_test.go @@ -145,6 +145,7 @@ func Test_checkErrorExceptions(t *testing.T) { //func Test_isOnline_actual(t *testing.T) { // targetAddr := ".xxnode.io:22840" -// _, ok := isOnlineHelper(targetAddr, time.Second*10) +// wc := webConn{} +// _, ok := wc.isOnlineHelper(targetAddr, time.Second*10) // t.Fatal(ok) //}