Skip to content

Commit

Permalink
Fix the ancient bug where client wasn't able to use non-https endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszraczylo committed Feb 26, 2023
1 parent 9e8df7e commit 42e1afb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
42 changes: 23 additions & 19 deletions gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package gql
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 42e1afb

Please sign in to comment.