Skip to content

Instantly share code, notes, and snippets.

@wolfeidau
Last active July 30, 2024 22:12

Revisions

  1. wolfeidau revised this gist Jul 30, 2024. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -17,9 +17,6 @@ func main() {

    tr := http.DefaultTransport.(*http.Transport).Clone()

    tr.Proxy = http.ProxyFromEnvironment
    tr.MaxIdleConnsPerHost = 100

    if *disableHTTP2 {
    tr.ForceAttemptHTTP2 = false
    tr.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
  2. wolfeidau revised this gist Jul 30, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,6 @@ func main() {
    tr.MaxIdleConnsPerHost = 100

    if *disableHTTP2 {
    // need to disable HTTP/2.0 and just updating the default transport won't work https://github.com/golang/go/issues/50571
    tr.ForceAttemptHTTP2 = false
    tr.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
    // The default TLSClientConfig has h2 in NextProtos, so the negotiated TLS connection will assume h2 support.
  3. wolfeidau revised this gist Jul 30, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.go
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ func main() {
    Transport: tr,
    }

    resp, err := client.Get("http://agent.buildkite.com/v3/ping")
    resp, err := client.Get("https://google.com")
    if err != nil {
    panic(err)
    }
  4. wolfeidau created this gist Jul 30, 2024.
    46 changes: 46 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    package main

    import (
    "crypto/tls"
    "flag"
    "log/slog"
    "net/http"
    "os"
    )

    var disableHTTP2 = flag.Bool("disable-http2", false, "Disable HTTP/2.0")

    func main() {
    flag.Parse()

    slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{})))

    tr := http.DefaultTransport.(*http.Transport).Clone()

    tr.Proxy = http.ProxyFromEnvironment
    tr.MaxIdleConnsPerHost = 100

    if *disableHTTP2 {
    // need to disable HTTP/2.0 and just updating the default transport won't work https://github.com/golang/go/issues/50571
    tr.ForceAttemptHTTP2 = false
    tr.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
    // The default TLSClientConfig has h2 in NextProtos, so the negotiated TLS connection will assume h2 support.
    // see https://github.com/golang/go/issues/50571
    tr.TLSClientConfig = &tls.Config{}
    }

    client := http.Client{
    Transport: tr,
    }

    resp, err := client.Get("http://agent.buildkite.com/v3/ping")
    if err != nil {
    panic(err)
    }

    defer resp.Body.Close()

    // Print the HTTP Status Code
    slog.With("resp.Status", resp.Status, "resp.StatusCode", resp.StatusCode, "resp.Proto", resp.Proto).Info("HTTP Status Code")

    }