From 42e1afbf77d3eaadf7ba995500f2d79cbb7dbf14 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sun, 26 Feb 2023 11:38:20 +0000 Subject: [PATCH] Fix the ancient bug where client wasn't able to use non-https endpoints. --- gql.go | 42 +++++++++++++++++++++++------------------- query.go | 1 + 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/gql.go b/gql.go index 0d02c77..d6eacfa 100644 --- a/gql.go +++ b/gql.go @@ -15,7 +15,6 @@ package gql import ( "crypto/tls" "fmt" - "net" "net/http" "os" "strconv" @@ -83,25 +82,30 @@ func setCacheEnabled() bool { } func NewConnection() *GraphQL { + + endpoint := pickGraphqlEndpoint() + var httpClient *http.Client + if strings.HasPrefix(endpoint, "http://") { + // HTTP/1.1 client + httpClient = &http.Client{} + } else { + // HTTP/2 or HTTPS client + http2Transport := &http2.Transport{ + AllowHTTP: true, + } + if strings.HasPrefix(endpoint, "https://") { + http2Transport.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: true, + } + } + httpClient = &http.Client{ + Transport: http2Transport, + } + } + g := GraphQL{ - Endpoint: pickGraphqlEndpoint(), - HttpClient: &http.Client{ - Transport: &http2.Transport{ - DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { - if strings.HasPrefix(network, "http") { - return net.Dial(network, addr) - } - return tls.Dial(network, addr, cfg) - }, - ReadIdleTimeout: 30 * time.Second, - DisableCompression: false, - AllowHTTP: true, - PingTimeout: 5 * time.Second, - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, - }, - }, - }, + Endpoint: endpoint, + HttpClient: httpClient, Log: logging.NewLogger(), Cache: setCacheEnabled(), CacheStore: setupCache(), diff --git a/query.go b/query.go index 60c0750..0168fae 100644 --- a/query.go +++ b/query.go @@ -83,6 +83,7 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe err = retry.Do( func() error { + g.Log.Debug("Sending the query") httpResponse, err = g.HttpClient.Do(httpRequest) if err != nil {