aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/example_test.go
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2024-05-29 09:24:20 -0700
committerDamien Neil <dneil@google.com>2024-11-05 22:14:59 +0000
commitbfc8f28068c4aff44aded67aef12e56ecc843717 (patch)
treed689a3e8af0036e9276aa45c5193df8f1349d6f3 /src/net/http/example_test.go
parent635c2dce04259f2c84aeac543f0305b3e7c8ed7b (diff)
downloadgo-bfc8f28068c4aff44aded67aef12e56ecc843717.tar.xz
net/http: add Protocols field to Server and Transport
Support configuring which HTTP version(s) a server or client use via an explicit set of protocols. The Protocols field takes precedence over TLSNextProto and ForceAttemptHTTP2. Fixes #67814 Change-Id: I09ece88f78ad4d98ca1f213157b5f62ae11e063f Reviewed-on: https://go-review.googlesource.com/c/go/+/607496 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/net/http/example_test.go')
-rw-r--r--src/net/http/example_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/net/http/example_test.go b/src/net/http/example_test.go
index 2f411d1d2e..f40273f14a 100644
--- a/src/net/http/example_test.go
+++ b/src/net/http/example_test.go
@@ -193,3 +193,31 @@ func ExampleNotFoundHandler() {
log.Fatal(http.ListenAndServe(":8080", mux))
}
+
+func ExampleProtocols_http1() {
+ srv := http.Server{
+ Addr: ":8443",
+ }
+
+ // Serve only HTTP/1.
+ srv.Protocols = new(http.Protocols)
+ srv.Protocols.SetHTTP1(true)
+
+ log.Fatal(srv.ListenAndServeTLS("cert.pem", "key.pem"))
+}
+
+func ExampleProtocols_http1or2() {
+ t := http.DefaultTransport.(*http.Transport).Clone()
+
+ // Use either HTTP/1 and HTTP/2.
+ t.Protocols = new(http.Protocols)
+ t.Protocols.SetHTTP1(true)
+ t.Protocols.SetHTTP2(true)
+
+ cli := &http.Client{Transport: t}
+ res, err := cli.Get("http://www.google.com/robots.txt")
+ if err != nil {
+ log.Fatal(err)
+ }
+ res.Body.Close()
+}