diff options
| author | Damien Neil <dneil@google.com> | 2024-05-29 09:24:20 -0700 |
|---|---|---|
| committer | Damien Neil <dneil@google.com> | 2024-11-05 22:14:59 +0000 |
| commit | bfc8f28068c4aff44aded67aef12e56ecc843717 (patch) | |
| tree | d689a3e8af0036e9276aa45c5193df8f1349d6f3 /src/net/http/http.go | |
| parent | 635c2dce04259f2c84aeac543f0305b3e7c8ed7b (diff) | |
| download | go-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/http.go')
| -rw-r--r-- | src/net/http/http.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/net/http/http.go b/src/net/http/http.go index 9dfc36c791..55f518607d 100644 --- a/src/net/http/http.go +++ b/src/net/http/http.go @@ -16,6 +16,54 @@ import ( "golang.org/x/net/http/httpguts" ) +// Protocols is a set of HTTP protocols. +// +// The supported protocols are: +// +// - HTTP1 is the HTTP/1.0 and HTTP/1.1 protocols. +// HTTP1 is supported on both unsecured TCP and secured TLS connections. +// +// - HTTP2 is the HTTP/2 protcol over a TLS connection. +type Protocols struct { + bits uint8 +} + +const ( + protoHTTP1 = 1 << iota + protoHTTP2 +) + +// HTTP1 reports whether p includes HTTP/1. +func (p Protocols) HTTP1() bool { return p.bits&protoHTTP1 != 0 } + +// SetHTTP1 adds or removes HTTP/1 from p. +func (p *Protocols) SetHTTP1(ok bool) { p.setBit(protoHTTP1, ok) } + +// HTTP2 reports whether p includes HTTP/2. +func (p Protocols) HTTP2() bool { return p.bits&protoHTTP2 != 0 } + +// SetHTTP2 adds or removes HTTP/2 from p. +func (p *Protocols) SetHTTP2(ok bool) { p.setBit(protoHTTP2, ok) } + +func (p *Protocols) setBit(bit uint8, ok bool) { + if ok { + p.bits |= bit + } else { + p.bits &^= bit + } +} + +func (p Protocols) String() string { + var s []string + if p.HTTP1() { + s = append(s, "HTTP1") + } + if p.HTTP2() { + s = append(s, "HTTP2") + } + return "{" + strings.Join(s, ",") + "}" +} + // incomparable is a zero-width, non-comparable type. Adding it to a struct // makes that struct also non-comparable, and generally doesn't add // any size (as long as it's first). |
