1//go:build !nethttpomithttp2
2
3// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
4//   $ bundle -o=h2_bundle.go -prefix=http2 -tags=!nethttpomithttp2 golang.org/x/net/http2
5
6// Package http2 implements the HTTP/2 protocol.
7//
8// This package is low-level and intended to be used directly by very
9// few people. Most users will use it indirectly through the automatic
10// use by the net/http package (from Go 1.6 and later).
11// For use in earlier Go versions see ConfigureServer. (Transport support
12// requires Go 1.6 or later)
13//
14// See https://http2.github.io/ for more information on HTTP/2.
15//
16// See https://http2.golang.org/ for a test server running this code.
17//
18// Copyright 2024 The Go Authors. All rights reserved.
19// Use of this source code is governed by a BSD-style
20// license that can be found in the LICENSE file.
21//
22
23package http
24
25import (
26	"bufio"
27	"bytes"
28	"compress/gzip"
29	"context"
30	"crypto/rand"
31	"crypto/tls"
32	"encoding/binary"
33	"errors"
34	"fmt"
35	"io"
36	"io/fs"
37	"log"
38	"math"
39	"math/bits"
40	mathrand "math/rand"
41	"net"
42	"net/http/httptrace"
43	"net/textproto"
44	"net/url"
45	"os"
46	"reflect"
47	"runtime"
48	"sort"
49	"strconv"
50	"strings"
51	"sync"
52	"sync/atomic"
53	"time"
54
55	"golang.org/x/net/http/httpguts"
56	"golang.org/x/net/http2/hpack"
57	"golang.org/x/net/idna"
58)
59
60// The HTTP protocols are defined in terms of ASCII, not Unicode. This file
61// contains helper functions which may use Unicode-aware functions which would
62// otherwise be unsafe and could introduce vulnerabilities if used improperly.
63
64// asciiEqualFold is strings.EqualFold, ASCII only. It reports whether s and t
65// are equal, ASCII-case-insensitively.
66func http2asciiEqualFold(s, t string) bool {
67	if len(s) != len(t) {
68		return false
69	}
70	for i := 0; i < len(s); i++ {
71		if http2lower(s[i]) != http2lower(t[i]) {
72			return false
73		}
74	}
75	return true
76}
77
78// lower returns the ASCII lowercase version of b.
79func http2lower(b byte) byte {
80	if 'A' <= b && b <= 'Z' {
81		return b + ('a' - 'A')
82	}
83	return b
84}
85
86// isASCIIPrint returns whether s is ASCII and printable according to
87// https://tools.ietf.org/html/rfc20#section-4.2.
88func http2isASCIIPrint(s string) bool {
89	for i := 0; i < len(s); i++ {
90		if s[i] < ' ' || s[i] > '~' {
91			return false
92		}
93	}
94	return true
95}
96
97// asciiToLower returns the lowercase version of s if s is ASCII and printable,
98// and whether or not it was.
99func http2asciiToLower(s string) (lower string, ok bool) {
100	if !http2isASCIIPrint(s) {
101		return "", false
102	}
103	return strings.ToLower(s), true
104}
105
106// A list of the possible cipher suite ids. Taken from
107// https://www.iana.org/assignments/tls-parameters/tls-parameters.txt
108
109const (
110	http2cipher_TLS_NULL_WITH_NULL_NULL               uint16 = 0x0000
111	http2cipher_TLS_RSA_WITH_NULL_MD5                 uint16 = 0x0001
112	http2cipher_TLS_RSA_WITH_NULL_SHA                 uint16 = 0x0002
113	http2cipher_TLS_RSA_EXPORT_WITH_RC4_40_MD5        uint16 = 0x0003
114	http2cipher_TLS_RSA_WITH_RC4_128_MD5              uint16 = 0x0004
115	http2cipher_TLS_RSA_WITH_RC4_128_SHA              uint16 = 0x0005
116	http2cipher_TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5    uint16 = 0x0006
117	http2cipher_TLS_RSA_WITH_IDEA_CBC_SHA             uint16 = 0x0007
118	http2cipher_TLS_RSA_EXPORT_WITH_DES40_CBC_SHA     uint16 = 0x0008
119	http2cipher_TLS_RSA_WITH_DES_CBC_SHA              uint16 = 0x0009
120	http2cipher_TLS_RSA_WITH_3DES_EDE_CBC_SHA         uint16 = 0x000A
121	http2cipher_TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA  uint16 = 0x000B
122	http2cipher_TLS_DH_DSS_WITH_DES_CBC_SHA           uint16 = 0x000C
123	http2cipher_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA      uint16 = 0x000D
124	http2cipher_TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA  uint16 = 0x000E
125	http2cipher_TLS_DH_RSA_WITH_DES_CBC_SHA           uint16 = 0x000F
126	http2cipher_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA      uint16 = 0x0010
127	http2cipher_TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA uint16 = 0x0011
128	http2cipher_TLS_DHE_DSS_WITH_DES_CBC_SHA          uint16 = 0x0012
129	http2cipher_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA     uint16 = 0x0013
130	http2cipher_TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA uint16 = 0x0014
131	http2cipher_TLS_DHE_RSA_WITH_DES_CBC_SHA          uint16 = 0x0015
132	http2cipher_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA     uint16 = 0x0016
133	http2cipher_TLS_DH_anon_EXPORT_WITH_RC4_40_MD5    uint16 = 0x0017
134	http2cipher_TLS_DH_anon_WITH_RC4_128_MD5          uint16 = 0x0018
135	http2cipher_TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA uint16 = 0x0019
136	http2cipher_TLS_DH_anon_WITH_DES_CBC_SHA          uint16 = 0x001A
137	http2cipher_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA     uint16 = 0x001B
138	// Reserved uint16 =  0x001C-1D
139	http2cipher_TLS_KRB5_WITH_DES_CBC_SHA             uint16 = 0x001E
140	http2cipher_TLS_KRB5_WITH_3DES_EDE_CBC_SHA        uint16 = 0x001F
141	http2cipher_TLS_KRB5_WITH_RC4_128_SHA             uint16 = 0x0020
142	http2cipher_TLS_KRB5_WITH_IDEA_CBC_SHA            uint16 = 0x0021
143	http2cipher_TLS_KRB5_WITH_DES_CBC_MD5             uint16 = 0x0022
144	http2cipher_TLS_KRB5_WITH_3DES_EDE_CBC_MD5        uint16 = 0x0023
145	http2cipher_TLS_KRB5_WITH_RC4_128_MD5             uint16 = 0x0024
146	http2cipher_TLS_KRB5_WITH_IDEA_CBC_MD5            uint16 = 0x0025
147	http2cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA   uint16 = 0x0026
148	http2cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA   uint16 = 0x0027
149	http2cipher_TLS_KRB5_EXPORT_WITH_RC4_40_SHA       uint16 = 0x0028
150	http2cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5   uint16 = 0x0029
151	http2cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5   uint16 = 0x002A
152	http2cipher_TLS_KRB5_EXPORT_WITH_RC4_40_MD5       uint16 = 0x002B
153	http2cipher_TLS_PSK_WITH_NULL_SHA                 uint16 = 0x002C
154	http2cipher_TLS_DHE_PSK_WITH_NULL_SHA             uint16 = 0x002D
155	http2cipher_TLS_RSA_PSK_WITH_NULL_SHA             uint16 = 0x002E
156	http2cipher_TLS_RSA_WITH_AES_128_CBC_SHA          uint16 = 0x002F
157	http2cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA       uint16 = 0x0030
158	http2cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA       uint16 = 0x0031
159	http2cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA      uint16 = 0x0032
160	http2cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA      uint16 = 0x0033
161	http2cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA      uint16 = 0x0034
162	http2cipher_TLS_RSA_WITH_AES_256_CBC_SHA          uint16 = 0x0035
163	http2cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA       uint16 = 0x0036
164	http2cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA       uint16 = 0x0037
165	http2cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA      uint16 = 0x0038
166	http2cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA      uint16 = 0x0039
167	http2cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA      uint16 = 0x003A
168	http2cipher_TLS_RSA_WITH_NULL_SHA256              uint16 = 0x003B
169	http2cipher_TLS_RSA_WITH_AES_128_CBC_SHA256       uint16 = 0x003C
170	http2cipher_TLS_RSA_WITH_AES_256_CBC_SHA256       uint16 = 0x003D
171	http2cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA256    uint16 = 0x003E
172	http2cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA256    uint16 = 0x003F
173	http2cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256   uint16 = 0x0040
174	http2cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA     uint16 = 0x0041
175	http2cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA  uint16 = 0x0042
176	http2cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA  uint16 = 0x0043
177	http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA uint16 = 0x0044
178	http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA uint16 = 0x0045
179	http2cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA uint16 = 0x0046
180	// Reserved uint16 =  0x0047-4F
181	// Reserved uint16 =  0x0050-58
182	// Reserved uint16 =  0x0059-5C
183	// Unassigned uint16 =  0x005D-5F
184	// Reserved uint16 =  0x0060-66
185	http2cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x0067
186	http2cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA256  uint16 = 0x0068
187	http2cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA256  uint16 = 0x0069
188	http2cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 uint16 = 0x006A
189	http2cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x006B
190	http2cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA256 uint16 = 0x006C
191	http2cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA256 uint16 = 0x006D
192	// Unassigned uint16 =  0x006E-83
193	http2cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA        uint16 = 0x0084
194	http2cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA     uint16 = 0x0085
195	http2cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA     uint16 = 0x0086
196	http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA    uint16 = 0x0087
197	http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA    uint16 = 0x0088
198	http2cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA    uint16 = 0x0089
199	http2cipher_TLS_PSK_WITH_RC4_128_SHA                 uint16 = 0x008A
200	http2cipher_TLS_PSK_WITH_3DES_EDE_CBC_SHA            uint16 = 0x008B
201	http2cipher_TLS_PSK_WITH_AES_128_CBC_SHA             uint16 = 0x008C
202	http2cipher_TLS_PSK_WITH_AES_256_CBC_SHA             uint16 = 0x008D
203	http2cipher_TLS_DHE_PSK_WITH_RC4_128_SHA             uint16 = 0x008E
204	http2cipher_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA        uint16 = 0x008F
205	http2cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA         uint16 = 0x0090
206	http2cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA         uint16 = 0x0091
207	http2cipher_TLS_RSA_PSK_WITH_RC4_128_SHA             uint16 = 0x0092
208	http2cipher_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA        uint16 = 0x0093
209	http2cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA         uint16 = 0x0094
210	http2cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA         uint16 = 0x0095
211	http2cipher_TLS_RSA_WITH_SEED_CBC_SHA                uint16 = 0x0096
212	http2cipher_TLS_DH_DSS_WITH_SEED_CBC_SHA             uint16 = 0x0097
213	http2cipher_TLS_DH_RSA_WITH_SEED_CBC_SHA             uint16 = 0x0098
214	http2cipher_TLS_DHE_DSS_WITH_SEED_CBC_SHA            uint16 = 0x0099
215	http2cipher_TLS_DHE_RSA_WITH_SEED_CBC_SHA            uint16 = 0x009A
216	http2cipher_TLS_DH_anon_WITH_SEED_CBC_SHA            uint16 = 0x009B
217	http2cipher_TLS_RSA_WITH_AES_128_GCM_SHA256          uint16 = 0x009C
218	http2cipher_TLS_RSA_WITH_AES_256_GCM_SHA384          uint16 = 0x009D
219	http2cipher_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256      uint16 = 0x009E
220	http2cipher_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384      uint16 = 0x009F
221	http2cipher_TLS_DH_RSA_WITH_AES_128_GCM_SHA256       uint16 = 0x00A0
222	http2cipher_TLS_DH_RSA_WITH_AES_256_GCM_SHA384       uint16 = 0x00A1
223	http2cipher_TLS_DHE_DSS_WITH_AES_128_GCM_SHA256      uint16 = 0x00A2
224	http2cipher_TLS_DHE_DSS_WITH_AES_256_GCM_SHA384      uint16 = 0x00A3
225	http2cipher_TLS_DH_DSS_WITH_AES_128_GCM_SHA256       uint16 = 0x00A4
226	http2cipher_TLS_DH_DSS_WITH_AES_256_GCM_SHA384       uint16 = 0x00A5
227	http2cipher_TLS_DH_anon_WITH_AES_128_GCM_SHA256      uint16 = 0x00A6
228	http2cipher_TLS_DH_anon_WITH_AES_256_GCM_SHA384      uint16 = 0x00A7
229	http2cipher_TLS_PSK_WITH_AES_128_GCM_SHA256          uint16 = 0x00A8
230	http2cipher_TLS_PSK_WITH_AES_256_GCM_SHA384          uint16 = 0x00A9
231	http2cipher_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256      uint16 = 0x00AA
232	http2cipher_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384      uint16 = 0x00AB
233	http2cipher_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256      uint16 = 0x00AC
234	http2cipher_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384      uint16 = 0x00AD
235	http2cipher_TLS_PSK_WITH_AES_128_CBC_SHA256          uint16 = 0x00AE
236	http2cipher_TLS_PSK_WITH_AES_256_CBC_SHA384          uint16 = 0x00AF
237	http2cipher_TLS_PSK_WITH_NULL_SHA256                 uint16 = 0x00B0
238	http2cipher_TLS_PSK_WITH_NULL_SHA384                 uint16 = 0x00B1
239	http2cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256      uint16 = 0x00B2
240	http2cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384      uint16 = 0x00B3
241	http2cipher_TLS_DHE_PSK_WITH_NULL_SHA256             uint16 = 0x00B4
242	http2cipher_TLS_DHE_PSK_WITH_NULL_SHA384             uint16 = 0x00B5
243	http2cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256      uint16 = 0x00B6
244	http2cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384      uint16 = 0x00B7
245	http2cipher_TLS_RSA_PSK_WITH_NULL_SHA256             uint16 = 0x00B8
246	http2cipher_TLS_RSA_PSK_WITH_NULL_SHA384             uint16 = 0x00B9
247	http2cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256     uint16 = 0x00BA
248	http2cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256  uint16 = 0x00BB
249	http2cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256  uint16 = 0x00BC
250	http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0x00BD
251	http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0x00BE
252	http2cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0x00BF
253	http2cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256     uint16 = 0x00C0
254	http2cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256  uint16 = 0x00C1
255	http2cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256  uint16 = 0x00C2
256	http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 uint16 = 0x00C3
257	http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 uint16 = 0x00C4
258	http2cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256 uint16 = 0x00C5
259	// Unassigned uint16 =  0x00C6-FE
260	http2cipher_TLS_EMPTY_RENEGOTIATION_INFO_SCSV uint16 = 0x00FF
261	// Unassigned uint16 =  0x01-55,*
262	http2cipher_TLS_FALLBACK_SCSV uint16 = 0x5600
263	// Unassigned                                   uint16 = 0x5601 - 0xC000
264	http2cipher_TLS_ECDH_ECDSA_WITH_NULL_SHA                 uint16 = 0xC001
265	http2cipher_TLS_ECDH_ECDSA_WITH_RC4_128_SHA              uint16 = 0xC002
266	http2cipher_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA         uint16 = 0xC003
267	http2cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA          uint16 = 0xC004
268	http2cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA          uint16 = 0xC005
269	http2cipher_TLS_ECDHE_ECDSA_WITH_NULL_SHA                uint16 = 0xC006
270	http2cipher_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA             uint16 = 0xC007
271	http2cipher_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA        uint16 = 0xC008
272	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA         uint16 = 0xC009
273	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA         uint16 = 0xC00A
274	http2cipher_TLS_ECDH_RSA_WITH_NULL_SHA                   uint16 = 0xC00B
275	http2cipher_TLS_ECDH_RSA_WITH_RC4_128_SHA                uint16 = 0xC00C
276	http2cipher_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0xC00D
277	http2cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA            uint16 = 0xC00E
278	http2cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA            uint16 = 0xC00F
279	http2cipher_TLS_ECDHE_RSA_WITH_NULL_SHA                  uint16 = 0xC010
280	http2cipher_TLS_ECDHE_RSA_WITH_RC4_128_SHA               uint16 = 0xC011
281	http2cipher_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA          uint16 = 0xC012
282	http2cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA           uint16 = 0xC013
283	http2cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA           uint16 = 0xC014
284	http2cipher_TLS_ECDH_anon_WITH_NULL_SHA                  uint16 = 0xC015
285	http2cipher_TLS_ECDH_anon_WITH_RC4_128_SHA               uint16 = 0xC016
286	http2cipher_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA          uint16 = 0xC017
287	http2cipher_TLS_ECDH_anon_WITH_AES_128_CBC_SHA           uint16 = 0xC018
288	http2cipher_TLS_ECDH_anon_WITH_AES_256_CBC_SHA           uint16 = 0xC019
289	http2cipher_TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA            uint16 = 0xC01A
290	http2cipher_TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA        uint16 = 0xC01B
291	http2cipher_TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA        uint16 = 0xC01C
292	http2cipher_TLS_SRP_SHA_WITH_AES_128_CBC_SHA             uint16 = 0xC01D
293	http2cipher_TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA         uint16 = 0xC01E
294	http2cipher_TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA         uint16 = 0xC01F
295	http2cipher_TLS_SRP_SHA_WITH_AES_256_CBC_SHA             uint16 = 0xC020
296	http2cipher_TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA         uint16 = 0xC021
297	http2cipher_TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA         uint16 = 0xC022
298	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256      uint16 = 0xC023
299	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384      uint16 = 0xC024
300	http2cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256       uint16 = 0xC025
301	http2cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384       uint16 = 0xC026
302	http2cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256        uint16 = 0xC027
303	http2cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384        uint16 = 0xC028
304	http2cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256         uint16 = 0xC029
305	http2cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384         uint16 = 0xC02A
306	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256      uint16 = 0xC02B
307	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384      uint16 = 0xC02C
308	http2cipher_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256       uint16 = 0xC02D
309	http2cipher_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384       uint16 = 0xC02E
310	http2cipher_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256        uint16 = 0xC02F
311	http2cipher_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384        uint16 = 0xC030
312	http2cipher_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0xC031
313	http2cipher_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0xC032
314	http2cipher_TLS_ECDHE_PSK_WITH_RC4_128_SHA               uint16 = 0xC033
315	http2cipher_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA          uint16 = 0xC034
316	http2cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA           uint16 = 0xC035
317	http2cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA           uint16 = 0xC036
318	http2cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256        uint16 = 0xC037
319	http2cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384        uint16 = 0xC038
320	http2cipher_TLS_ECDHE_PSK_WITH_NULL_SHA                  uint16 = 0xC039
321	http2cipher_TLS_ECDHE_PSK_WITH_NULL_SHA256               uint16 = 0xC03A
322	http2cipher_TLS_ECDHE_PSK_WITH_NULL_SHA384               uint16 = 0xC03B
323	http2cipher_TLS_RSA_WITH_ARIA_128_CBC_SHA256             uint16 = 0xC03C
324	http2cipher_TLS_RSA_WITH_ARIA_256_CBC_SHA384             uint16 = 0xC03D
325	http2cipher_TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256          uint16 = 0xC03E
326	http2cipher_TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384          uint16 = 0xC03F
327	http2cipher_TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256          uint16 = 0xC040
328	http2cipher_TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384          uint16 = 0xC041
329	http2cipher_TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC042
330	http2cipher_TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC043
331	http2cipher_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC044
332	http2cipher_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC045
333	http2cipher_TLS_DH_anon_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC046
334	http2cipher_TLS_DH_anon_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC047
335	http2cipher_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256     uint16 = 0xC048
336	http2cipher_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384     uint16 = 0xC049
337	http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256      uint16 = 0xC04A
338	http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384      uint16 = 0xC04B
339	http2cipher_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256       uint16 = 0xC04C
340	http2cipher_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384       uint16 = 0xC04D
341	http2cipher_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256        uint16 = 0xC04E
342	http2cipher_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384        uint16 = 0xC04F
343	http2cipher_TLS_RSA_WITH_ARIA_128_GCM_SHA256             uint16 = 0xC050
344	http2cipher_TLS_RSA_WITH_ARIA_256_GCM_SHA384             uint16 = 0xC051
345	http2cipher_TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC052
346	http2cipher_TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC053
347	http2cipher_TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256          uint16 = 0xC054
348	http2cipher_TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384          uint16 = 0xC055
349	http2cipher_TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC056
350	http2cipher_TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC057
351	http2cipher_TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256          uint16 = 0xC058
352	http2cipher_TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384          uint16 = 0xC059
353	http2cipher_TLS_DH_anon_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC05A
354	http2cipher_TLS_DH_anon_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC05B
355	http2cipher_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256     uint16 = 0xC05C
356	http2cipher_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384     uint16 = 0xC05D
357	http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256      uint16 = 0xC05E
358	http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384      uint16 = 0xC05F
359	http2cipher_TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256       uint16 = 0xC060
360	http2cipher_TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384       uint16 = 0xC061
361	http2cipher_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256        uint16 = 0xC062
362	http2cipher_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384        uint16 = 0xC063
363	http2cipher_TLS_PSK_WITH_ARIA_128_CBC_SHA256             uint16 = 0xC064
364	http2cipher_TLS_PSK_WITH_ARIA_256_CBC_SHA384             uint16 = 0xC065
365	http2cipher_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC066
366	http2cipher_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC067
367	http2cipher_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC068
368	http2cipher_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC069
369	http2cipher_TLS_PSK_WITH_ARIA_128_GCM_SHA256             uint16 = 0xC06A
370	http2cipher_TLS_PSK_WITH_ARIA_256_GCM_SHA384             uint16 = 0xC06B
371	http2cipher_TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC06C
372	http2cipher_TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC06D
373	http2cipher_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC06E
374	http2cipher_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC06F
375	http2cipher_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256       uint16 = 0xC070
376	http2cipher_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384       uint16 = 0xC071
377	http2cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0xC072
378	http2cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 uint16 = 0xC073
379	http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256  uint16 = 0xC074
380	http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384  uint16 = 0xC075
381	http2cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256   uint16 = 0xC076
382	http2cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384   uint16 = 0xC077
383	http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256    uint16 = 0xC078
384	http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384    uint16 = 0xC079
385	http2cipher_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256         uint16 = 0xC07A
386	http2cipher_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384         uint16 = 0xC07B
387	http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC07C
388	http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC07D
389	http2cipher_TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256      uint16 = 0xC07E
390	http2cipher_TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384      uint16 = 0xC07F
391	http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC080
392	http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC081
393	http2cipher_TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256      uint16 = 0xC082
394	http2cipher_TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384      uint16 = 0xC083
395	http2cipher_TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC084
396	http2cipher_TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC085
397	http2cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 uint16 = 0xC086
398	http2cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 uint16 = 0xC087
399	http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256  uint16 = 0xC088
400	http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384  uint16 = 0xC089
401	http2cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256   uint16 = 0xC08A
402	http2cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384   uint16 = 0xC08B
403	http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256    uint16 = 0xC08C
404	http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384    uint16 = 0xC08D
405	http2cipher_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256         uint16 = 0xC08E
406	http2cipher_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384         uint16 = 0xC08F
407	http2cipher_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC090
408	http2cipher_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC091
409	http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC092
410	http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC093
411	http2cipher_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256         uint16 = 0xC094
412	http2cipher_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384         uint16 = 0xC095
413	http2cipher_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256     uint16 = 0xC096
414	http2cipher_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384     uint16 = 0xC097
415	http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256     uint16 = 0xC098
416	http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384     uint16 = 0xC099
417	http2cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256   uint16 = 0xC09A
418	http2cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384   uint16 = 0xC09B
419	http2cipher_TLS_RSA_WITH_AES_128_CCM                     uint16 = 0xC09C
420	http2cipher_TLS_RSA_WITH_AES_256_CCM                     uint16 = 0xC09D
421	http2cipher_TLS_DHE_RSA_WITH_AES_128_CCM                 uint16 = 0xC09E
422	http2cipher_TLS_DHE_RSA_WITH_AES_256_CCM                 uint16 = 0xC09F
423	http2cipher_TLS_RSA_WITH_AES_128_CCM_8                   uint16 = 0xC0A0
424	http2cipher_TLS_RSA_WITH_AES_256_CCM_8                   uint16 = 0xC0A1
425	http2cipher_TLS_DHE_RSA_WITH_AES_128_CCM_8               uint16 = 0xC0A2
426	http2cipher_TLS_DHE_RSA_WITH_AES_256_CCM_8               uint16 = 0xC0A3
427	http2cipher_TLS_PSK_WITH_AES_128_CCM                     uint16 = 0xC0A4
428	http2cipher_TLS_PSK_WITH_AES_256_CCM                     uint16 = 0xC0A5
429	http2cipher_TLS_DHE_PSK_WITH_AES_128_CCM                 uint16 = 0xC0A6
430	http2cipher_TLS_DHE_PSK_WITH_AES_256_CCM                 uint16 = 0xC0A7
431	http2cipher_TLS_PSK_WITH_AES_128_CCM_8                   uint16 = 0xC0A8
432	http2cipher_TLS_PSK_WITH_AES_256_CCM_8                   uint16 = 0xC0A9
433	http2cipher_TLS_PSK_DHE_WITH_AES_128_CCM_8               uint16 = 0xC0AA
434	http2cipher_TLS_PSK_DHE_WITH_AES_256_CCM_8               uint16 = 0xC0AB
435	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CCM             uint16 = 0xC0AC
436	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CCM             uint16 = 0xC0AD
437	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8           uint16 = 0xC0AE
438	http2cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8           uint16 = 0xC0AF
439	// Unassigned uint16 =  0xC0B0-FF
440	// Unassigned uint16 =  0xC1-CB,*
441	// Unassigned uint16 =  0xCC00-A7
442	http2cipher_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256   uint16 = 0xCCA8
443	http2cipher_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xCCA9
444	http2cipher_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256     uint16 = 0xCCAA
445	http2cipher_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256         uint16 = 0xCCAB
446	http2cipher_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256   uint16 = 0xCCAC
447	http2cipher_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256     uint16 = 0xCCAD
448	http2cipher_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256     uint16 = 0xCCAE
449)
450
451// isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec.
452// References:
453// https://tools.ietf.org/html/rfc7540#appendix-A
454// Reject cipher suites from Appendix A.
455// "This list includes those cipher suites that do not
456// offer an ephemeral key exchange and those that are
457// based on the TLS null, stream or block cipher type"
458func http2isBadCipher(cipher uint16) bool {
459	switch cipher {
460	case http2cipher_TLS_NULL_WITH_NULL_NULL,
461		http2cipher_TLS_RSA_WITH_NULL_MD5,
462		http2cipher_TLS_RSA_WITH_NULL_SHA,
463		http2cipher_TLS_RSA_EXPORT_WITH_RC4_40_MD5,
464		http2cipher_TLS_RSA_WITH_RC4_128_MD5,
465		http2cipher_TLS_RSA_WITH_RC4_128_SHA,
466		http2cipher_TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5,
467		http2cipher_TLS_RSA_WITH_IDEA_CBC_SHA,
468		http2cipher_TLS_RSA_EXPORT_WITH_DES40_CBC_SHA,
469		http2cipher_TLS_RSA_WITH_DES_CBC_SHA,
470		http2cipher_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
471		http2cipher_TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA,
472		http2cipher_TLS_DH_DSS_WITH_DES_CBC_SHA,
473		http2cipher_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA,
474		http2cipher_TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA,
475		http2cipher_TLS_DH_RSA_WITH_DES_CBC_SHA,
476		http2cipher_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA,
477		http2cipher_TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA,
478		http2cipher_TLS_DHE_DSS_WITH_DES_CBC_SHA,
479		http2cipher_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA,
480		http2cipher_TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA,
481		http2cipher_TLS_DHE_RSA_WITH_DES_CBC_SHA,
482		http2cipher_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
483		http2cipher_TLS_DH_anon_EXPORT_WITH_RC4_40_MD5,
484		http2cipher_TLS_DH_anon_WITH_RC4_128_MD5,
485		http2cipher_TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA,
486		http2cipher_TLS_DH_anon_WITH_DES_CBC_SHA,
487		http2cipher_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA,
488		http2cipher_TLS_KRB5_WITH_DES_CBC_SHA,
489		http2cipher_TLS_KRB5_WITH_3DES_EDE_CBC_SHA,
490		http2cipher_TLS_KRB5_WITH_RC4_128_SHA,
491		http2cipher_TLS_KRB5_WITH_IDEA_CBC_SHA,
492		http2cipher_TLS_KRB5_WITH_DES_CBC_MD5,
493		http2cipher_TLS_KRB5_WITH_3DES_EDE_CBC_MD5,
494		http2cipher_TLS_KRB5_WITH_RC4_128_MD5,
495		http2cipher_TLS_KRB5_WITH_IDEA_CBC_MD5,
496		http2cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA,
497		http2cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA,
498		http2cipher_TLS_KRB5_EXPORT_WITH_RC4_40_SHA,
499		http2cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5,
500		http2cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5,
501		http2cipher_TLS_KRB5_EXPORT_WITH_RC4_40_MD5,
502		http2cipher_TLS_PSK_WITH_NULL_SHA,
503		http2cipher_TLS_DHE_PSK_WITH_NULL_SHA,
504		http2cipher_TLS_RSA_PSK_WITH_NULL_SHA,
505		http2cipher_TLS_RSA_WITH_AES_128_CBC_SHA,
506		http2cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA,
507		http2cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA,
508		http2cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
509		http2cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
510		http2cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA,
511		http2cipher_TLS_RSA_WITH_AES_256_CBC_SHA,
512		http2cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA,
513		http2cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA,
514		http2cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA,
515		http2cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
516		http2cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA,
517		http2cipher_TLS_RSA_WITH_NULL_SHA256,
518		http2cipher_TLS_RSA_WITH_AES_128_CBC_SHA256,
519		http2cipher_TLS_RSA_WITH_AES_256_CBC_SHA256,
520		http2cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA256,
521		http2cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA256,
522		http2cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,
523		http2cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
524		http2cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA,
525		http2cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA,
526		http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA,
527		http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
528		http2cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA,
529		http2cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
530		http2cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA256,
531		http2cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA256,
532		http2cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256,
533		http2cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
534		http2cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA256,
535		http2cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA256,
536		http2cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
537		http2cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA,
538		http2cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA,
539		http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA,
540		http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
541		http2cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA,
542		http2cipher_TLS_PSK_WITH_RC4_128_SHA,
543		http2cipher_TLS_PSK_WITH_3DES_EDE_CBC_SHA,
544		http2cipher_TLS_PSK_WITH_AES_128_CBC_SHA,
545		http2cipher_TLS_PSK_WITH_AES_256_CBC_SHA,
546		http2cipher_TLS_DHE_PSK_WITH_RC4_128_SHA,
547		http2cipher_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA,
548		http2cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA,
549		http2cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA,
550		http2cipher_TLS_RSA_PSK_WITH_RC4_128_SHA,
551		http2cipher_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA,
552		http2cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA,
553		http2cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA,
554		http2cipher_TLS_RSA_WITH_SEED_CBC_SHA,
555		http2cipher_TLS_DH_DSS_WITH_SEED_CBC_SHA,
556		http2cipher_TLS_DH_RSA_WITH_SEED_CBC_SHA,
557		http2cipher_TLS_DHE_DSS_WITH_SEED_CBC_SHA,
558		http2cipher_TLS_DHE_RSA_WITH_SEED_CBC_SHA,
559		http2cipher_TLS_DH_anon_WITH_SEED_CBC_SHA,
560		http2cipher_TLS_RSA_WITH_AES_128_GCM_SHA256,
561		http2cipher_TLS_RSA_WITH_AES_256_GCM_SHA384,
562		http2cipher_TLS_DH_RSA_WITH_AES_128_GCM_SHA256,
563		http2cipher_TLS_DH_RSA_WITH_AES_256_GCM_SHA384,
564		http2cipher_TLS_DH_DSS_WITH_AES_128_GCM_SHA256,
565		http2cipher_TLS_DH_DSS_WITH_AES_256_GCM_SHA384,
566		http2cipher_TLS_DH_anon_WITH_AES_128_GCM_SHA256,
567		http2cipher_TLS_DH_anon_WITH_AES_256_GCM_SHA384,
568		http2cipher_TLS_PSK_WITH_AES_128_GCM_SHA256,
569		http2cipher_TLS_PSK_WITH_AES_256_GCM_SHA384,
570		http2cipher_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256,
571		http2cipher_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384,
572		http2cipher_TLS_PSK_WITH_AES_128_CBC_SHA256,
573		http2cipher_TLS_PSK_WITH_AES_256_CBC_SHA384,
574		http2cipher_TLS_PSK_WITH_NULL_SHA256,
575		http2cipher_TLS_PSK_WITH_NULL_SHA384,
576		http2cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
577		http2cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384,
578		http2cipher_TLS_DHE_PSK_WITH_NULL_SHA256,
579		http2cipher_TLS_DHE_PSK_WITH_NULL_SHA384,
580		http2cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256,
581		http2cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384,
582		http2cipher_TLS_RSA_PSK_WITH_NULL_SHA256,
583		http2cipher_TLS_RSA_PSK_WITH_NULL_SHA384,
584		http2cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
585		http2cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256,
586		http2cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256,
587		http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256,
588		http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
589		http2cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256,
590		http2cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
591		http2cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256,
592		http2cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256,
593		http2cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256,
594		http2cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
595		http2cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256,
596		http2cipher_TLS_EMPTY_RENEGOTIATION_INFO_SCSV,
597		http2cipher_TLS_ECDH_ECDSA_WITH_NULL_SHA,
598		http2cipher_TLS_ECDH_ECDSA_WITH_RC4_128_SHA,
599		http2cipher_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
600		http2cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
601		http2cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
602		http2cipher_TLS_ECDHE_ECDSA_WITH_NULL_SHA,
603		http2cipher_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
604		http2cipher_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
605		http2cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
606		http2cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
607		http2cipher_TLS_ECDH_RSA_WITH_NULL_SHA,
608		http2cipher_TLS_ECDH_RSA_WITH_RC4_128_SHA,
609		http2cipher_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
610		http2cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
611		http2cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
612		http2cipher_TLS_ECDHE_RSA_WITH_NULL_SHA,
613		http2cipher_TLS_ECDHE_RSA_WITH_RC4_128_SHA,
614		http2cipher_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
615		http2cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
616		http2cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
617		http2cipher_TLS_ECDH_anon_WITH_NULL_SHA,
618		http2cipher_TLS_ECDH_anon_WITH_RC4_128_SHA,
619		http2cipher_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA,
620		http2cipher_TLS_ECDH_anon_WITH_AES_128_CBC_SHA,
621		http2cipher_TLS_ECDH_anon_WITH_AES_256_CBC_SHA,
622		http2cipher_TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA,
623		http2cipher_TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA,
624		http2cipher_TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA,
625		http2cipher_TLS_SRP_SHA_WITH_AES_128_CBC_SHA,
626		http2cipher_TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA,
627		http2cipher_TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA,
628		http2cipher_TLS_SRP_SHA_WITH_AES_256_CBC_SHA,
629		http2cipher_TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA,
630		http2cipher_TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA,
631		http2cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
632		http2cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
633		http2cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
634		http2cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
635		http2cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
636		http2cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
637		http2cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
638		http2cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
639		http2cipher_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
640		http2cipher_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
641		http2cipher_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
642		http2cipher_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
643		http2cipher_TLS_ECDHE_PSK_WITH_RC4_128_SHA,
644		http2cipher_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA,
645		http2cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
646		http2cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA,
647		http2cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
648		http2cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384,
649		http2cipher_TLS_ECDHE_PSK_WITH_NULL_SHA,
650		http2cipher_TLS_ECDHE_PSK_WITH_NULL_SHA256,
651		http2cipher_TLS_ECDHE_PSK_WITH_NULL_SHA384,
652		http2cipher_TLS_RSA_WITH_ARIA_128_CBC_SHA256,
653		http2cipher_TLS_RSA_WITH_ARIA_256_CBC_SHA384,
654		http2cipher_TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256,
655		http2cipher_TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384,
656		http2cipher_TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256,
657		http2cipher_TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384,
658		http2cipher_TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256,
659		http2cipher_TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384,
660		http2cipher_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256,
661		http2cipher_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384,
662		http2cipher_TLS_DH_anon_WITH_ARIA_128_CBC_SHA256,
663		http2cipher_TLS_DH_anon_WITH_ARIA_256_CBC_SHA384,
664		http2cipher_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256,
665		http2cipher_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384,
666		http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256,
667		http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384,
668		http2cipher_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256,
669		http2cipher_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384,
670		http2cipher_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256,
671		http2cipher_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384,
672		http2cipher_TLS_RSA_WITH_ARIA_128_GCM_SHA256,
673		http2cipher_TLS_RSA_WITH_ARIA_256_GCM_SHA384,
674		http2cipher_TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256,
675		http2cipher_TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384,
676		http2cipher_TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256,
677		http2cipher_TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384,
678		http2cipher_TLS_DH_anon_WITH_ARIA_128_GCM_SHA256,
679		http2cipher_TLS_DH_anon_WITH_ARIA_256_GCM_SHA384,
680		http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256,
681		http2cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384,
682		http2cipher_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256,
683		http2cipher_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384,
684		http2cipher_TLS_PSK_WITH_ARIA_128_CBC_SHA256,
685		http2cipher_TLS_PSK_WITH_ARIA_256_CBC_SHA384,
686		http2cipher_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256,
687		http2cipher_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384,
688		http2cipher_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256,
689		http2cipher_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384,
690		http2cipher_TLS_PSK_WITH_ARIA_128_GCM_SHA256,
691		http2cipher_TLS_PSK_WITH_ARIA_256_GCM_SHA384,
692		http2cipher_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256,
693		http2cipher_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384,
694		http2cipher_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256,
695		http2cipher_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384,
696		http2cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
697		http2cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
698		http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
699		http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
700		http2cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
701		http2cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384,
702		http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256,
703		http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384,
704		http2cipher_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256,
705		http2cipher_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384,
706		http2cipher_TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256,
707		http2cipher_TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384,
708		http2cipher_TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256,
709		http2cipher_TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384,
710		http2cipher_TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256,
711		http2cipher_TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384,
712		http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256,
713		http2cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384,
714		http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256,
715		http2cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384,
716		http2cipher_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256,
717		http2cipher_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384,
718		http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256,
719		http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384,
720		http2cipher_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256,
721		http2cipher_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384,
722		http2cipher_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
723		http2cipher_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
724		http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256,
725		http2cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384,
726		http2cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
727		http2cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
728		http2cipher_TLS_RSA_WITH_AES_128_CCM,
729		http2cipher_TLS_RSA_WITH_AES_256_CCM,
730		http2cipher_TLS_RSA_WITH_AES_128_CCM_8,
731		http2cipher_TLS_RSA_WITH_AES_256_CCM_8,
732		http2cipher_TLS_PSK_WITH_AES_128_CCM,
733		http2cipher_TLS_PSK_WITH_AES_256_CCM,
734		http2cipher_TLS_PSK_WITH_AES_128_CCM_8,
735		http2cipher_TLS_PSK_WITH_AES_256_CCM_8:
736		return true
737	default:
738		return false
739	}
740}
741
742// ClientConnPool manages a pool of HTTP/2 client connections.
743type http2ClientConnPool interface {
744	// GetClientConn returns a specific HTTP/2 connection (usually
745	// a TLS-TCP connection) to an HTTP/2 server. On success, the
746	// returned ClientConn accounts for the upcoming RoundTrip
747	// call, so the caller should not omit it. If the caller needs
748	// to, ClientConn.RoundTrip can be called with a bogus
749	// new(http.Request) to release the stream reservation.
750	GetClientConn(req *Request, addr string) (*http2ClientConn, error)
751	MarkDead(*http2ClientConn)
752}
753
754// clientConnPoolIdleCloser is the interface implemented by ClientConnPool
755// implementations which can close their idle connections.
756type http2clientConnPoolIdleCloser interface {
757	http2ClientConnPool
758	closeIdleConnections()
759}
760
761var (
762	_ http2clientConnPoolIdleCloser = (*http2clientConnPool)(nil)
763	_ http2clientConnPoolIdleCloser = http2noDialClientConnPool{}
764)
765
766// TODO: use singleflight for dialing and addConnCalls?
767type http2clientConnPool struct {
768	t *http2Transport
769
770	mu sync.Mutex // TODO: maybe switch to RWMutex
771	// TODO: add support for sharing conns based on cert names
772	// (e.g. share conn for googleapis.com and appspot.com)
773	conns        map[string][]*http2ClientConn // key is host:port
774	dialing      map[string]*http2dialCall     // currently in-flight dials
775	keys         map[*http2ClientConn][]string
776	addConnCalls map[string]*http2addConnCall // in-flight addConnIfNeeded calls
777}
778
779func (p *http2clientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) {
780	return p.getClientConn(req, addr, http2dialOnMiss)
781}
782
783const (
784	http2dialOnMiss   = true
785	http2noDialOnMiss = false
786)
787
788func (p *http2clientConnPool) getClientConn(req *Request, addr string, dialOnMiss bool) (*http2ClientConn, error) {
789	// TODO(dneil): Dial a new connection when t.DisableKeepAlives is set?
790	if http2isConnectionCloseRequest(req) && dialOnMiss {
791		// It gets its own connection.
792		http2traceGetConn(req, addr)
793		const singleUse = true
794		cc, err := p.t.dialClientConn(req.Context(), addr, singleUse)
795		if err != nil {
796			return nil, err
797		}
798		return cc, nil
799	}
800	for {
801		p.mu.Lock()
802		for _, cc := range p.conns[addr] {
803			if cc.ReserveNewRequest() {
804				// When a connection is presented to us by the net/http package,
805				// the GetConn hook has already been called.
806				// Don't call it a second time here.
807				if !cc.getConnCalled {
808					http2traceGetConn(req, addr)
809				}
810				cc.getConnCalled = false
811				p.mu.Unlock()
812				return cc, nil
813			}
814		}
815		if !dialOnMiss {
816			p.mu.Unlock()
817			return nil, http2ErrNoCachedConn
818		}
819		http2traceGetConn(req, addr)
820		call := p.getStartDialLocked(req.Context(), addr)
821		p.mu.Unlock()
822		<-call.done
823		if http2shouldRetryDial(call, req) {
824			continue
825		}
826		cc, err := call.res, call.err
827		if err != nil {
828			return nil, err
829		}
830		if cc.ReserveNewRequest() {
831			return cc, nil
832		}
833	}
834}
835
836// dialCall is an in-flight Transport dial call to a host.
837type http2dialCall struct {
838	_ http2incomparable
839	p *http2clientConnPool
840	// the context associated with the request
841	// that created this dialCall
842	ctx  context.Context
843	done chan struct{}    // closed when done
844	res  *http2ClientConn // valid after done is closed
845	err  error            // valid after done is closed
846}
847
848// requires p.mu is held.
849func (p *http2clientConnPool) getStartDialLocked(ctx context.Context, addr string) *http2dialCall {
850	if call, ok := p.dialing[addr]; ok {
851		// A dial is already in-flight. Don't start another.
852		return call
853	}
854	call := &http2dialCall{p: p, done: make(chan struct{}), ctx: ctx}
855	if p.dialing == nil {
856		p.dialing = make(map[string]*http2dialCall)
857	}
858	p.dialing[addr] = call
859	go call.dial(call.ctx, addr)
860	return call
861}
862
863// run in its own goroutine.
864func (c *http2dialCall) dial(ctx context.Context, addr string) {
865	const singleUse = false // shared conn
866	c.res, c.err = c.p.t.dialClientConn(ctx, addr, singleUse)
867
868	c.p.mu.Lock()
869	delete(c.p.dialing, addr)
870	if c.err == nil {
871		c.p.addConnLocked(addr, c.res)
872	}
873	c.p.mu.Unlock()
874
875	close(c.done)
876}
877
878// addConnIfNeeded makes a NewClientConn out of c if a connection for key doesn't
879// already exist. It coalesces concurrent calls with the same key.
880// This is used by the http1 Transport code when it creates a new connection. Because
881// the http1 Transport doesn't de-dup TCP dials to outbound hosts (because it doesn't know
882// the protocol), it can get into a situation where it has multiple TLS connections.
883// This code decides which ones live or die.
884// The return value used is whether c was used.
885// c is never closed.
886func (p *http2clientConnPool) addConnIfNeeded(key string, t *http2Transport, c *tls.Conn) (used bool, err error) {
887	p.mu.Lock()
888	for _, cc := range p.conns[key] {
889		if cc.CanTakeNewRequest() {
890			p.mu.Unlock()
891			return false, nil
892		}
893	}
894	call, dup := p.addConnCalls[key]
895	if !dup {
896		if p.addConnCalls == nil {
897			p.addConnCalls = make(map[string]*http2addConnCall)
898		}
899		call = &http2addConnCall{
900			p:    p,
901			done: make(chan struct{}),
902		}
903		p.addConnCalls[key] = call
904		go call.run(t, key, c)
905	}
906	p.mu.Unlock()
907
908	<-call.done
909	if call.err != nil {
910		return false, call.err
911	}
912	return !dup, nil
913}
914
915type http2addConnCall struct {
916	_    http2incomparable
917	p    *http2clientConnPool
918	done chan struct{} // closed when done
919	err  error
920}
921
922func (c *http2addConnCall) run(t *http2Transport, key string, tc *tls.Conn) {
923	cc, err := t.NewClientConn(tc)
924
925	p := c.p
926	p.mu.Lock()
927	if err != nil {
928		c.err = err
929	} else {
930		cc.getConnCalled = true // already called by the net/http package
931		p.addConnLocked(key, cc)
932	}
933	delete(p.addConnCalls, key)
934	p.mu.Unlock()
935	close(c.done)
936}
937
938// p.mu must be held
939func (p *http2clientConnPool) addConnLocked(key string, cc *http2ClientConn) {
940	for _, v := range p.conns[key] {
941		if v == cc {
942			return
943		}
944	}
945	if p.conns == nil {
946		p.conns = make(map[string][]*http2ClientConn)
947	}
948	if p.keys == nil {
949		p.keys = make(map[*http2ClientConn][]string)
950	}
951	p.conns[key] = append(p.conns[key], cc)
952	p.keys[cc] = append(p.keys[cc], key)
953}
954
955func (p *http2clientConnPool) MarkDead(cc *http2ClientConn) {
956	p.mu.Lock()
957	defer p.mu.Unlock()
958	for _, key := range p.keys[cc] {
959		vv, ok := p.conns[key]
960		if !ok {
961			continue
962		}
963		newList := http2filterOutClientConn(vv, cc)
964		if len(newList) > 0 {
965			p.conns[key] = newList
966		} else {
967			delete(p.conns, key)
968		}
969	}
970	delete(p.keys, cc)
971}
972
973func (p *http2clientConnPool) closeIdleConnections() {
974	p.mu.Lock()
975	defer p.mu.Unlock()
976	// TODO: don't close a cc if it was just added to the pool
977	// milliseconds ago and has never been used. There's currently
978	// a small race window with the HTTP/1 Transport's integration
979	// where it can add an idle conn just before using it, and
980	// somebody else can concurrently call CloseIdleConns and
981	// break some caller's RoundTrip.
982	for _, vv := range p.conns {
983		for _, cc := range vv {
984			cc.closeIfIdle()
985		}
986	}
987}
988
989func http2filterOutClientConn(in []*http2ClientConn, exclude *http2ClientConn) []*http2ClientConn {
990	out := in[:0]
991	for _, v := range in {
992		if v != exclude {
993			out = append(out, v)
994		}
995	}
996	// If we filtered it out, zero out the last item to prevent
997	// the GC from seeing it.
998	if len(in) != len(out) {
999		in[len(in)-1] = nil
1000	}
1001	return out
1002}
1003
1004// noDialClientConnPool is an implementation of http2.ClientConnPool
1005// which never dials. We let the HTTP/1.1 client dial and use its TLS
1006// connection instead.
1007type http2noDialClientConnPool struct{ *http2clientConnPool }
1008
1009func (p http2noDialClientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) {
1010	return p.getClientConn(req, addr, http2noDialOnMiss)
1011}
1012
1013// shouldRetryDial reports whether the current request should
1014// retry dialing after the call finished unsuccessfully, for example
1015// if the dial was canceled because of a context cancellation or
1016// deadline expiry.
1017func http2shouldRetryDial(call *http2dialCall, req *Request) bool {
1018	if call.err == nil {
1019		// No error, no need to retry
1020		return false
1021	}
1022	if call.ctx == req.Context() {
1023		// If the call has the same context as the request, the dial
1024		// should not be retried, since any cancellation will have come
1025		// from this request.
1026		return false
1027	}
1028	if !errors.Is(call.err, context.Canceled) && !errors.Is(call.err, context.DeadlineExceeded) {
1029		// If the call error is not because of a context cancellation or a deadline expiry,
1030		// the dial should not be retried.
1031		return false
1032	}
1033	// Only retry if the error is a context cancellation error or deadline expiry
1034	// and the context associated with the call was canceled or expired.
1035	return call.ctx.Err() != nil
1036}
1037
1038// Buffer chunks are allocated from a pool to reduce pressure on GC.
1039// The maximum wasted space per dataBuffer is 2x the largest size class,
1040// which happens when the dataBuffer has multiple chunks and there is
1041// one unread byte in both the first and last chunks. We use a few size
1042// classes to minimize overheads for servers that typically receive very
1043// small request bodies.
1044//
1045// TODO: Benchmark to determine if the pools are necessary. The GC may have
1046// improved enough that we can instead allocate chunks like this:
1047// make([]byte, max(16<<10, expectedBytesRemaining))
1048var http2dataChunkPools = [...]sync.Pool{
1049	{New: func() interface{} { return new([1 << 10]byte) }},
1050	{New: func() interface{} { return new([2 << 10]byte) }},
1051	{New: func() interface{} { return new([4 << 10]byte) }},
1052	{New: func() interface{} { return new([8 << 10]byte) }},
1053	{New: func() interface{} { return new([16 << 10]byte) }},
1054}
1055
1056func http2getDataBufferChunk(size int64) []byte {
1057	switch {
1058	case size <= 1<<10:
1059		return http2dataChunkPools[0].Get().(*[1 << 10]byte)[:]
1060	case size <= 2<<10:
1061		return http2dataChunkPools[1].Get().(*[2 << 10]byte)[:]
1062	case size <= 4<<10:
1063		return http2dataChunkPools[2].Get().(*[4 << 10]byte)[:]
1064	case size <= 8<<10:
1065		return http2dataChunkPools[3].Get().(*[8 << 10]byte)[:]
1066	default:
1067		return http2dataChunkPools[4].Get().(*[16 << 10]byte)[:]
1068	}
1069}
1070
1071func http2putDataBufferChunk(p []byte) {
1072	switch len(p) {
1073	case 1 << 10:
1074		http2dataChunkPools[0].Put((*[1 << 10]byte)(p))
1075	case 2 << 10:
1076		http2dataChunkPools[1].Put((*[2 << 10]byte)(p))
1077	case 4 << 10:
1078		http2dataChunkPools[2].Put((*[4 << 10]byte)(p))
1079	case 8 << 10:
1080		http2dataChunkPools[3].Put((*[8 << 10]byte)(p))
1081	case 16 << 10:
1082		http2dataChunkPools[4].Put((*[16 << 10]byte)(p))
1083	default:
1084		panic(fmt.Sprintf("unexpected buffer len=%v", len(p)))
1085	}
1086}
1087
1088// dataBuffer is an io.ReadWriter backed by a list of data chunks.
1089// Each dataBuffer is used to read DATA frames on a single stream.
1090// The buffer is divided into chunks so the server can limit the
1091// total memory used by a single connection without limiting the
1092// request body size on any single stream.
1093type http2dataBuffer struct {
1094	chunks   [][]byte
1095	r        int   // next byte to read is chunks[0][r]
1096	w        int   // next byte to write is chunks[len(chunks)-1][w]
1097	size     int   // total buffered bytes
1098	expected int64 // we expect at least this many bytes in future Write calls (ignored if <= 0)
1099}
1100
1101var http2errReadEmpty = errors.New("read from empty dataBuffer")
1102
1103// Read copies bytes from the buffer into p.
1104// It is an error to read when no data is available.
1105func (b *http2dataBuffer) Read(p []byte) (int, error) {
1106	if b.size == 0 {
1107		return 0, http2errReadEmpty
1108	}
1109	var ntotal int
1110	for len(p) > 0 && b.size > 0 {
1111		readFrom := b.bytesFromFirstChunk()
1112		n := copy(p, readFrom)
1113		p = p[n:]
1114		ntotal += n
1115		b.r += n
1116		b.size -= n
1117		// If the first chunk has been consumed, advance to the next chunk.
1118		if b.r == len(b.chunks[0]) {
1119			http2putDataBufferChunk(b.chunks[0])
1120			end := len(b.chunks) - 1
1121			copy(b.chunks[:end], b.chunks[1:])
1122			b.chunks[end] = nil
1123			b.chunks = b.chunks[:end]
1124			b.r = 0
1125		}
1126	}
1127	return ntotal, nil
1128}
1129
1130func (b *http2dataBuffer) bytesFromFirstChunk() []byte {
1131	if len(b.chunks) == 1 {
1132		return b.chunks[0][b.r:b.w]
1133	}
1134	return b.chunks[0][b.r:]
1135}
1136
1137// Len returns the number of bytes of the unread portion of the buffer.
1138func (b *http2dataBuffer) Len() int {
1139	return b.size
1140}
1141
1142// Write appends p to the buffer.
1143func (b *http2dataBuffer) Write(p []byte) (int, error) {
1144	ntotal := len(p)
1145	for len(p) > 0 {
1146		// If the last chunk is empty, allocate a new chunk. Try to allocate
1147		// enough to fully copy p plus any additional bytes we expect to
1148		// receive. However, this may allocate less than len(p).
1149		want := int64(len(p))
1150		if b.expected > want {
1151			want = b.expected
1152		}
1153		chunk := b.lastChunkOrAlloc(want)
1154		n := copy(chunk[b.w:], p)
1155		p = p[n:]
1156		b.w += n
1157		b.size += n
1158		b.expected -= int64(n)
1159	}
1160	return ntotal, nil
1161}
1162
1163func (b *http2dataBuffer) lastChunkOrAlloc(want int64) []byte {
1164	if len(b.chunks) != 0 {
1165		last := b.chunks[len(b.chunks)-1]
1166		if b.w < len(last) {
1167			return last
1168		}
1169	}
1170	chunk := http2getDataBufferChunk(want)
1171	b.chunks = append(b.chunks, chunk)
1172	b.w = 0
1173	return chunk
1174}
1175
1176// An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
1177type http2ErrCode uint32
1178
1179const (
1180	http2ErrCodeNo                 http2ErrCode = 0x0
1181	http2ErrCodeProtocol           http2ErrCode = 0x1
1182	http2ErrCodeInternal           http2ErrCode = 0x2
1183	http2ErrCodeFlowControl        http2ErrCode = 0x3
1184	http2ErrCodeSettingsTimeout    http2ErrCode = 0x4
1185	http2ErrCodeStreamClosed       http2ErrCode = 0x5
1186	http2ErrCodeFrameSize          http2ErrCode = 0x6
1187	http2ErrCodeRefusedStream      http2ErrCode = 0x7
1188	http2ErrCodeCancel             http2ErrCode = 0x8
1189	http2ErrCodeCompression        http2ErrCode = 0x9
1190	http2ErrCodeConnect            http2ErrCode = 0xa
1191	http2ErrCodeEnhanceYourCalm    http2ErrCode = 0xb
1192	http2ErrCodeInadequateSecurity http2ErrCode = 0xc
1193	http2ErrCodeHTTP11Required     http2ErrCode = 0xd
1194)
1195
1196var http2errCodeName = map[http2ErrCode]string{
1197	http2ErrCodeNo:                 "NO_ERROR",
1198	http2ErrCodeProtocol:           "PROTOCOL_ERROR",
1199	http2ErrCodeInternal:           "INTERNAL_ERROR",
1200	http2ErrCodeFlowControl:        "FLOW_CONTROL_ERROR",
1201	http2ErrCodeSettingsTimeout:    "SETTINGS_TIMEOUT",
1202	http2ErrCodeStreamClosed:       "STREAM_CLOSED",
1203	http2ErrCodeFrameSize:          "FRAME_SIZE_ERROR",
1204	http2ErrCodeRefusedStream:      "REFUSED_STREAM",
1205	http2ErrCodeCancel:             "CANCEL",
1206	http2ErrCodeCompression:        "COMPRESSION_ERROR",
1207	http2ErrCodeConnect:            "CONNECT_ERROR",
1208	http2ErrCodeEnhanceYourCalm:    "ENHANCE_YOUR_CALM",
1209	http2ErrCodeInadequateSecurity: "INADEQUATE_SECURITY",
1210	http2ErrCodeHTTP11Required:     "HTTP_1_1_REQUIRED",
1211}
1212
1213func (e http2ErrCode) String() string {
1214	if s, ok := http2errCodeName[e]; ok {
1215		return s
1216	}
1217	return fmt.Sprintf("unknown error code 0x%x", uint32(e))
1218}
1219
1220func (e http2ErrCode) stringToken() string {
1221	if s, ok := http2errCodeName[e]; ok {
1222		return s
1223	}
1224	return fmt.Sprintf("ERR_UNKNOWN_%d", uint32(e))
1225}
1226
1227// ConnectionError is an error that results in the termination of the
1228// entire connection.
1229type http2ConnectionError http2ErrCode
1230
1231func (e http2ConnectionError) Error() string {
1232	return fmt.Sprintf("connection error: %s", http2ErrCode(e))
1233}
1234
1235// StreamError is an error that only affects one stream within an
1236// HTTP/2 connection.
1237type http2StreamError struct {
1238	StreamID uint32
1239	Code     http2ErrCode
1240	Cause    error // optional additional detail
1241}
1242
1243// errFromPeer is a sentinel error value for StreamError.Cause to
1244// indicate that the StreamError was sent from the peer over the wire
1245// and wasn't locally generated in the Transport.
1246var http2errFromPeer = errors.New("received from peer")
1247
1248func http2streamError(id uint32, code http2ErrCode) http2StreamError {
1249	return http2StreamError{StreamID: id, Code: code}
1250}
1251
1252func (e http2StreamError) Error() string {
1253	if e.Cause != nil {
1254		return fmt.Sprintf("stream error: stream ID %d; %v; %v", e.StreamID, e.Code, e.Cause)
1255	}
1256	return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
1257}
1258
1259// 6.9.1 The Flow Control Window
1260// "If a sender receives a WINDOW_UPDATE that causes a flow control
1261// window to exceed this maximum it MUST terminate either the stream
1262// or the connection, as appropriate. For streams, [...]; for the
1263// connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
1264type http2goAwayFlowError struct{}
1265
1266func (http2goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
1267
1268// connError represents an HTTP/2 ConnectionError error code, along
1269// with a string (for debugging) explaining why.
1270//
1271// Errors of this type are only returned by the frame parser functions
1272// and converted into ConnectionError(Code), after stashing away
1273// the Reason into the Framer's errDetail field, accessible via
1274// the (*Framer).ErrorDetail method.
1275type http2connError struct {
1276	Code   http2ErrCode // the ConnectionError error code
1277	Reason string       // additional reason
1278}
1279
1280func (e http2connError) Error() string {
1281	return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
1282}
1283
1284type http2pseudoHeaderError string
1285
1286func (e http2pseudoHeaderError) Error() string {
1287	return fmt.Sprintf("invalid pseudo-header %q", string(e))
1288}
1289
1290type http2duplicatePseudoHeaderError string
1291
1292func (e http2duplicatePseudoHeaderError) Error() string {
1293	return fmt.Sprintf("duplicate pseudo-header %q", string(e))
1294}
1295
1296type http2headerFieldNameError string
1297
1298func (e http2headerFieldNameError) Error() string {
1299	return fmt.Sprintf("invalid header field name %q", string(e))
1300}
1301
1302type http2headerFieldValueError string
1303
1304func (e http2headerFieldValueError) Error() string {
1305	return fmt.Sprintf("invalid header field value for %q", string(e))
1306}
1307
1308var (
1309	http2errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers")
1310	http2errPseudoAfterRegular   = errors.New("pseudo header field after regular")
1311)
1312
1313// inflowMinRefresh is the minimum number of bytes we'll send for a
1314// flow control window update.
1315const http2inflowMinRefresh = 4 << 10
1316
1317// inflow accounts for an inbound flow control window.
1318// It tracks both the latest window sent to the peer (used for enforcement)
1319// and the accumulated unsent window.
1320type http2inflow struct {
1321	avail  int32
1322	unsent int32
1323}
1324
1325// init sets the initial window.
1326func (f *http2inflow) init(n int32) {
1327	f.avail = n
1328}
1329
1330// add adds n bytes to the window, with a maximum window size of max,
1331// indicating that the peer can now send us more data.
1332// For example, the user read from a {Request,Response} body and consumed
1333// some of the buffered data, so the peer can now send more.
1334// It returns the number of bytes to send in a WINDOW_UPDATE frame to the peer.
1335// Window updates are accumulated and sent when the unsent capacity
1336// is at least inflowMinRefresh or will at least double the peer's available window.
1337func (f *http2inflow) add(n int) (connAdd int32) {
1338	if n < 0 {
1339		panic("negative update")
1340	}
1341	unsent := int64(f.unsent) + int64(n)
1342	// "A sender MUST NOT allow a flow-control window to exceed 2^31-1 octets."
1343	// RFC 7540 Section 6.9.1.
1344	const maxWindow = 1<<31 - 1
1345	if unsent+int64(f.avail) > maxWindow {
1346		panic("flow control update exceeds maximum window size")
1347	}
1348	f.unsent = int32(unsent)
1349	if f.unsent < http2inflowMinRefresh && f.unsent < f.avail {
1350		// If there aren't at least inflowMinRefresh bytes of window to send,
1351		// and this update won't at least double the window, buffer the update for later.
1352		return 0
1353	}
1354	f.avail += f.unsent
1355	f.unsent = 0
1356	return int32(unsent)
1357}
1358
1359// take attempts to take n bytes from the peer's flow control window.
1360// It reports whether the window has available capacity.
1361func (f *http2inflow) take(n uint32) bool {
1362	if n > uint32(f.avail) {
1363		return false
1364	}
1365	f.avail -= int32(n)
1366	return true
1367}
1368
1369// takeInflows attempts to take n bytes from two inflows,
1370// typically connection-level and stream-level flows.
1371// It reports whether both windows have available capacity.
1372func http2takeInflows(f1, f2 *http2inflow, n uint32) bool {
1373	if n > uint32(f1.avail) || n > uint32(f2.avail) {
1374		return false
1375	}
1376	f1.avail -= int32(n)
1377	f2.avail -= int32(n)
1378	return true
1379}
1380
1381// outflow is the outbound flow control window's size.
1382type http2outflow struct {
1383	_ http2incomparable
1384
1385	// n is the number of DATA bytes we're allowed to send.
1386	// An outflow is kept both on a conn and a per-stream.
1387	n int32
1388
1389	// conn points to the shared connection-level outflow that is
1390	// shared by all streams on that conn. It is nil for the outflow
1391	// that's on the conn directly.
1392	conn *http2outflow
1393}
1394
1395func (f *http2outflow) setConnFlow(cf *http2outflow) { f.conn = cf }
1396
1397func (f *http2outflow) available() int32 {
1398	n := f.n
1399	if f.conn != nil && f.conn.n < n {
1400		n = f.conn.n
1401	}
1402	return n
1403}
1404
1405func (f *http2outflow) take(n int32) {
1406	if n > f.available() {
1407		panic("internal error: took too much")
1408	}
1409	f.n -= n
1410	if f.conn != nil {
1411		f.conn.n -= n
1412	}
1413}
1414
1415// add adds n bytes (positive or negative) to the flow control window.
1416// It returns false if the sum would exceed 2^31-1.
1417func (f *http2outflow) add(n int32) bool {
1418	sum := f.n + n
1419	if (sum > n) == (f.n > 0) {
1420		f.n = sum
1421		return true
1422	}
1423	return false
1424}
1425
1426const http2frameHeaderLen = 9
1427
1428var http2padZeros = make([]byte, 255) // zeros for padding
1429
1430// A FrameType is a registered frame type as defined in
1431// https://httpwg.org/specs/rfc7540.html#rfc.section.11.2
1432type http2FrameType uint8
1433
1434const (
1435	http2FrameData         http2FrameType = 0x0
1436	http2FrameHeaders      http2FrameType = 0x1
1437	http2FramePriority     http2FrameType = 0x2
1438	http2FrameRSTStream    http2FrameType = 0x3
1439	http2FrameSettings     http2FrameType = 0x4
1440	http2FramePushPromise  http2FrameType = 0x5
1441	http2FramePing         http2FrameType = 0x6
1442	http2FrameGoAway       http2FrameType = 0x7
1443	http2FrameWindowUpdate http2FrameType = 0x8
1444	http2FrameContinuation http2FrameType = 0x9
1445)
1446
1447var http2frameName = map[http2FrameType]string{
1448	http2FrameData:         "DATA",
1449	http2FrameHeaders:      "HEADERS",
1450	http2FramePriority:     "PRIORITY",
1451	http2FrameRSTStream:    "RST_STREAM",
1452	http2FrameSettings:     "SETTINGS",
1453	http2FramePushPromise:  "PUSH_PROMISE",
1454	http2FramePing:         "PING",
1455	http2FrameGoAway:       "GOAWAY",
1456	http2FrameWindowUpdate: "WINDOW_UPDATE",
1457	http2FrameContinuation: "CONTINUATION",
1458}
1459
1460func (t http2FrameType) String() string {
1461	if s, ok := http2frameName[t]; ok {
1462		return s
1463	}
1464	return fmt.Sprintf("UNKNOWN_FRAME_TYPE_%d", uint8(t))
1465}
1466
1467// Flags is a bitmask of HTTP/2 flags.
1468// The meaning of flags varies depending on the frame type.
1469type http2Flags uint8
1470
1471// Has reports whether f contains all (0 or more) flags in v.
1472func (f http2Flags) Has(v http2Flags) bool {
1473	return (f & v) == v
1474}
1475
1476// Frame-specific FrameHeader flag bits.
1477const (
1478	// Data Frame
1479	http2FlagDataEndStream http2Flags = 0x1
1480	http2FlagDataPadded    http2Flags = 0x8
1481
1482	// Headers Frame
1483	http2FlagHeadersEndStream  http2Flags = 0x1
1484	http2FlagHeadersEndHeaders http2Flags = 0x4
1485	http2FlagHeadersPadded     http2Flags = 0x8
1486	http2FlagHeadersPriority   http2Flags = 0x20
1487
1488	// Settings Frame
1489	http2FlagSettingsAck http2Flags = 0x1
1490
1491	// Ping Frame
1492	http2FlagPingAck http2Flags = 0x1
1493
1494	// Continuation Frame
1495	http2FlagContinuationEndHeaders http2Flags = 0x4
1496
1497	http2FlagPushPromiseEndHeaders http2Flags = 0x4
1498	http2FlagPushPromisePadded     http2Flags = 0x8
1499)
1500
1501var http2flagName = map[http2FrameType]map[http2Flags]string{
1502	http2FrameData: {
1503		http2FlagDataEndStream: "END_STREAM",
1504		http2FlagDataPadded:    "PADDED",
1505	},
1506	http2FrameHeaders: {
1507		http2FlagHeadersEndStream:  "END_STREAM",
1508		http2FlagHeadersEndHeaders: "END_HEADERS",
1509		http2FlagHeadersPadded:     "PADDED",
1510		http2FlagHeadersPriority:   "PRIORITY",
1511	},
1512	http2FrameSettings: {
1513		http2FlagSettingsAck: "ACK",
1514	},
1515	http2FramePing: {
1516		http2FlagPingAck: "ACK",
1517	},
1518	http2FrameContinuation: {
1519		http2FlagContinuationEndHeaders: "END_HEADERS",
1520	},
1521	http2FramePushPromise: {
1522		http2FlagPushPromiseEndHeaders: "END_HEADERS",
1523		http2FlagPushPromisePadded:     "PADDED",
1524	},
1525}
1526
1527// a frameParser parses a frame given its FrameHeader and payload
1528// bytes. The length of payload will always equal fh.Length (which
1529// might be 0).
1530type http2frameParser func(fc *http2frameCache, fh http2FrameHeader, countError func(string), payload []byte) (http2Frame, error)
1531
1532var http2frameParsers = map[http2FrameType]http2frameParser{
1533	http2FrameData:         http2parseDataFrame,
1534	http2FrameHeaders:      http2parseHeadersFrame,
1535	http2FramePriority:     http2parsePriorityFrame,
1536	http2FrameRSTStream:    http2parseRSTStreamFrame,
1537	http2FrameSettings:     http2parseSettingsFrame,
1538	http2FramePushPromise:  http2parsePushPromise,
1539	http2FramePing:         http2parsePingFrame,
1540	http2FrameGoAway:       http2parseGoAwayFrame,
1541	http2FrameWindowUpdate: http2parseWindowUpdateFrame,
1542	http2FrameContinuation: http2parseContinuationFrame,
1543}
1544
1545func http2typeFrameParser(t http2FrameType) http2frameParser {
1546	if f := http2frameParsers[t]; f != nil {
1547		return f
1548	}
1549	return http2parseUnknownFrame
1550}
1551
1552// A FrameHeader is the 9 byte header of all HTTP/2 frames.
1553//
1554// See https://httpwg.org/specs/rfc7540.html#FrameHeader
1555type http2FrameHeader struct {
1556	valid bool // caller can access []byte fields in the Frame
1557
1558	// Type is the 1 byte frame type. There are ten standard frame
1559	// types, but extension frame types may be written by WriteRawFrame
1560	// and will be returned by ReadFrame (as UnknownFrame).
1561	Type http2FrameType
1562
1563	// Flags are the 1 byte of 8 potential bit flags per frame.
1564	// They are specific to the frame type.
1565	Flags http2Flags
1566
1567	// Length is the length of the frame, not including the 9 byte header.
1568	// The maximum size is one byte less than 16MB (uint24), but only
1569	// frames up to 16KB are allowed without peer agreement.
1570	Length uint32
1571
1572	// StreamID is which stream this frame is for. Certain frames
1573	// are not stream-specific, in which case this field is 0.
1574	StreamID uint32
1575}
1576
1577// Header returns h. It exists so FrameHeaders can be embedded in other
1578// specific frame types and implement the Frame interface.
1579func (h http2FrameHeader) Header() http2FrameHeader { return h }
1580
1581func (h http2FrameHeader) String() string {
1582	var buf bytes.Buffer
1583	buf.WriteString("[FrameHeader ")
1584	h.writeDebug(&buf)
1585	buf.WriteByte(']')
1586	return buf.String()
1587}
1588
1589func (h http2FrameHeader) writeDebug(buf *bytes.Buffer) {
1590	buf.WriteString(h.Type.String())
1591	if h.Flags != 0 {
1592		buf.WriteString(" flags=")
1593		set := 0
1594		for i := uint8(0); i < 8; i++ {
1595			if h.Flags&(1<<i) == 0 {
1596				continue
1597			}
1598			set++
1599			if set > 1 {
1600				buf.WriteByte('|')
1601			}
1602			name := http2flagName[h.Type][http2Flags(1<<i)]
1603			if name != "" {
1604				buf.WriteString(name)
1605			} else {
1606				fmt.Fprintf(buf, "0x%x", 1<<i)
1607			}
1608		}
1609	}
1610	if h.StreamID != 0 {
1611		fmt.Fprintf(buf, " stream=%d", h.StreamID)
1612	}
1613	fmt.Fprintf(buf, " len=%d", h.Length)
1614}
1615
1616func (h *http2FrameHeader) checkValid() {
1617	if !h.valid {
1618		panic("Frame accessor called on non-owned Frame")
1619	}
1620}
1621
1622func (h *http2FrameHeader) invalidate() { h.valid = false }
1623
1624// frame header bytes.
1625// Used only by ReadFrameHeader.
1626var http2fhBytes = sync.Pool{
1627	New: func() interface{} {
1628		buf := make([]byte, http2frameHeaderLen)
1629		return &buf
1630	},
1631}
1632
1633// ReadFrameHeader reads 9 bytes from r and returns a FrameHeader.
1634// Most users should use Framer.ReadFrame instead.
1635func http2ReadFrameHeader(r io.Reader) (http2FrameHeader, error) {
1636	bufp := http2fhBytes.Get().(*[]byte)
1637	defer http2fhBytes.Put(bufp)
1638	return http2readFrameHeader(*bufp, r)
1639}
1640
1641func http2readFrameHeader(buf []byte, r io.Reader) (http2FrameHeader, error) {
1642	_, err := io.ReadFull(r, buf[:http2frameHeaderLen])
1643	if err != nil {
1644		return http2FrameHeader{}, err
1645	}
1646	return http2FrameHeader{
1647		Length:   (uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])),
1648		Type:     http2FrameType(buf[3]),
1649		Flags:    http2Flags(buf[4]),
1650		StreamID: binary.BigEndian.Uint32(buf[5:]) & (1<<31 - 1),
1651		valid:    true,
1652	}, nil
1653}
1654
1655// A Frame is the base interface implemented by all frame types.
1656// Callers will generally type-assert the specific frame type:
1657// *HeadersFrame, *SettingsFrame, *WindowUpdateFrame, etc.
1658//
1659// Frames are only valid until the next call to Framer.ReadFrame.
1660type http2Frame interface {
1661	Header() http2FrameHeader
1662
1663	// invalidate is called by Framer.ReadFrame to make this
1664	// frame's buffers as being invalid, since the subsequent
1665	// frame will reuse them.
1666	invalidate()
1667}
1668
1669// A Framer reads and writes Frames.
1670type http2Framer struct {
1671	r         io.Reader
1672	lastFrame http2Frame
1673	errDetail error
1674
1675	// countError is a non-nil func that's called on a frame parse
1676	// error with some unique error path token. It's initialized
1677	// from Transport.CountError or Server.CountError.
1678	countError func(errToken string)
1679
1680	// lastHeaderStream is non-zero if the last frame was an
1681	// unfinished HEADERS/CONTINUATION.
1682	lastHeaderStream uint32
1683
1684	maxReadSize uint32
1685	headerBuf   [http2frameHeaderLen]byte
1686
1687	// TODO: let getReadBuf be configurable, and use a less memory-pinning
1688	// allocator in server.go to minimize memory pinned for many idle conns.
1689	// Will probably also need to make frame invalidation have a hook too.
1690	getReadBuf func(size uint32) []byte
1691	readBuf    []byte // cache for default getReadBuf
1692
1693	maxWriteSize uint32 // zero means unlimited; TODO: implement
1694
1695	w    io.Writer
1696	wbuf []byte
1697
1698	// AllowIllegalWrites permits the Framer's Write methods to
1699	// write frames that do not conform to the HTTP/2 spec. This
1700	// permits using the Framer to test other HTTP/2
1701	// implementations' conformance to the spec.
1702	// If false, the Write methods will prefer to return an error
1703	// rather than comply.
1704	AllowIllegalWrites bool
1705
1706	// AllowIllegalReads permits the Framer's ReadFrame method
1707	// to return non-compliant frames or frame orders.
1708	// This is for testing and permits using the Framer to test
1709	// other HTTP/2 implementations' conformance to the spec.
1710	// It is not compatible with ReadMetaHeaders.
1711	AllowIllegalReads bool
1712
1713	// ReadMetaHeaders if non-nil causes ReadFrame to merge
1714	// HEADERS and CONTINUATION frames together and return
1715	// MetaHeadersFrame instead.
1716	ReadMetaHeaders *hpack.Decoder
1717
1718	// MaxHeaderListSize is the http2 MAX_HEADER_LIST_SIZE.
1719	// It's used only if ReadMetaHeaders is set; 0 means a sane default
1720	// (currently 16MB)
1721	// If the limit is hit, MetaHeadersFrame.Truncated is set true.
1722	MaxHeaderListSize uint32
1723
1724	// TODO: track which type of frame & with which flags was sent
1725	// last. Then return an error (unless AllowIllegalWrites) if
1726	// we're in the middle of a header block and a
1727	// non-Continuation or Continuation on a different stream is
1728	// attempted to be written.
1729
1730	logReads, logWrites bool
1731
1732	debugFramer       *http2Framer // only use for logging written writes
1733	debugFramerBuf    *bytes.Buffer
1734	debugReadLoggerf  func(string, ...interface{})
1735	debugWriteLoggerf func(string, ...interface{})
1736
1737	frameCache *http2frameCache // nil if frames aren't reused (default)
1738}
1739
1740func (fr *http2Framer) maxHeaderListSize() uint32 {
1741	if fr.MaxHeaderListSize == 0 {
1742		return 16 << 20 // sane default, per docs
1743	}
1744	return fr.MaxHeaderListSize
1745}
1746
1747func (f *http2Framer) startWrite(ftype http2FrameType, flags http2Flags, streamID uint32) {
1748	// Write the FrameHeader.
1749	f.wbuf = append(f.wbuf[:0],
1750		0, // 3 bytes of length, filled in in endWrite
1751		0,
1752		0,
1753		byte(ftype),
1754		byte(flags),
1755		byte(streamID>>24),
1756		byte(streamID>>16),
1757		byte(streamID>>8),
1758		byte(streamID))
1759}
1760
1761func (f *http2Framer) endWrite() error {
1762	// Now that we know the final size, fill in the FrameHeader in
1763	// the space previously reserved for it. Abuse append.
1764	length := len(f.wbuf) - http2frameHeaderLen
1765	if length >= (1 << 24) {
1766		return http2ErrFrameTooLarge
1767	}
1768	_ = append(f.wbuf[:0],
1769		byte(length>>16),
1770		byte(length>>8),
1771		byte(length))
1772	if f.logWrites {
1773		f.logWrite()
1774	}
1775
1776	n, err := f.w.Write(f.wbuf)
1777	if err == nil && n != len(f.wbuf) {
1778		err = io.ErrShortWrite
1779	}
1780	return err
1781}
1782
1783func (f *http2Framer) logWrite() {
1784	if f.debugFramer == nil {
1785		f.debugFramerBuf = new(bytes.Buffer)
1786		f.debugFramer = http2NewFramer(nil, f.debugFramerBuf)
1787		f.debugFramer.logReads = false // we log it ourselves, saying "wrote" below
1788		// Let us read anything, even if we accidentally wrote it
1789		// in the wrong order:
1790		f.debugFramer.AllowIllegalReads = true
1791	}
1792	f.debugFramerBuf.Write(f.wbuf)
1793	fr, err := f.debugFramer.ReadFrame()
1794	if err != nil {
1795		f.debugWriteLoggerf("http2: Framer %p: failed to decode just-written frame", f)
1796		return
1797	}
1798	f.debugWriteLoggerf("http2: Framer %p: wrote %v", f, http2summarizeFrame(fr))
1799}
1800
1801func (f *http2Framer) writeByte(v byte) { f.wbuf = append(f.wbuf, v) }
1802
1803func (f *http2Framer) writeBytes(v []byte) { f.wbuf = append(f.wbuf, v...) }
1804
1805func (f *http2Framer) writeUint16(v uint16) { f.wbuf = append(f.wbuf, byte(v>>8), byte(v)) }
1806
1807func (f *http2Framer) writeUint32(v uint32) {
1808	f.wbuf = append(f.wbuf, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
1809}
1810
1811const (
1812	http2minMaxFrameSize = 1 << 14
1813	http2maxFrameSize    = 1<<24 - 1
1814)
1815
1816// SetReuseFrames allows the Framer to reuse Frames.
1817// If called on a Framer, Frames returned by calls to ReadFrame are only
1818// valid until the next call to ReadFrame.
1819func (fr *http2Framer) SetReuseFrames() {
1820	if fr.frameCache != nil {
1821		return
1822	}
1823	fr.frameCache = &http2frameCache{}
1824}
1825
1826type http2frameCache struct {
1827	dataFrame http2DataFrame
1828}
1829
1830func (fc *http2frameCache) getDataFrame() *http2DataFrame {
1831	if fc == nil {
1832		return &http2DataFrame{}
1833	}
1834	return &fc.dataFrame
1835}
1836
1837// NewFramer returns a Framer that writes frames to w and reads them from r.
1838func http2NewFramer(w io.Writer, r io.Reader) *http2Framer {
1839	fr := &http2Framer{
1840		w:                 w,
1841		r:                 r,
1842		countError:        func(string) {},
1843		logReads:          http2logFrameReads,
1844		logWrites:         http2logFrameWrites,
1845		debugReadLoggerf:  log.Printf,
1846		debugWriteLoggerf: log.Printf,
1847	}
1848	fr.getReadBuf = func(size uint32) []byte {
1849		if cap(fr.readBuf) >= int(size) {
1850			return fr.readBuf[:size]
1851		}
1852		fr.readBuf = make([]byte, size)
1853		return fr.readBuf
1854	}
1855	fr.SetMaxReadFrameSize(http2maxFrameSize)
1856	return fr
1857}
1858
1859// SetMaxReadFrameSize sets the maximum size of a frame
1860// that will be read by a subsequent call to ReadFrame.
1861// It is the caller's responsibility to advertise this
1862// limit with a SETTINGS frame.
1863func (fr *http2Framer) SetMaxReadFrameSize(v uint32) {
1864	if v > http2maxFrameSize {
1865		v = http2maxFrameSize
1866	}
1867	fr.maxReadSize = v
1868}
1869
1870// ErrorDetail returns a more detailed error of the last error
1871// returned by Framer.ReadFrame. For instance, if ReadFrame
1872// returns a StreamError with code PROTOCOL_ERROR, ErrorDetail
1873// will say exactly what was invalid. ErrorDetail is not guaranteed
1874// to return a non-nil value and like the rest of the http2 package,
1875// its return value is not protected by an API compatibility promise.
1876// ErrorDetail is reset after the next call to ReadFrame.
1877func (fr *http2Framer) ErrorDetail() error {
1878	return fr.errDetail
1879}
1880
1881// ErrFrameTooLarge is returned from Framer.ReadFrame when the peer
1882// sends a frame that is larger than declared with SetMaxReadFrameSize.
1883var http2ErrFrameTooLarge = errors.New("http2: frame too large")
1884
1885// terminalReadFrameError reports whether err is an unrecoverable
1886// error from ReadFrame and no other frames should be read.
1887func http2terminalReadFrameError(err error) bool {
1888	if _, ok := err.(http2StreamError); ok {
1889		return false
1890	}
1891	return err != nil
1892}
1893
1894// ReadFrame reads a single frame. The returned Frame is only valid
1895// until the next call to ReadFrame.
1896//
1897// If the frame is larger than previously set with SetMaxReadFrameSize, the
1898// returned error is ErrFrameTooLarge. Other errors may be of type
1899// ConnectionError, StreamError, or anything else from the underlying
1900// reader.
1901//
1902// If ReadFrame returns an error and a non-nil Frame, the Frame's StreamID
1903// indicates the stream responsible for the error.
1904func (fr *http2Framer) ReadFrame() (http2Frame, error) {
1905	fr.errDetail = nil
1906	if fr.lastFrame != nil {
1907		fr.lastFrame.invalidate()
1908	}
1909	fh, err := http2readFrameHeader(fr.headerBuf[:], fr.r)
1910	if err != nil {
1911		return nil, err
1912	}
1913	if fh.Length > fr.maxReadSize {
1914		return nil, http2ErrFrameTooLarge
1915	}
1916	payload := fr.getReadBuf(fh.Length)
1917	if _, err := io.ReadFull(fr.r, payload); err != nil {
1918		return nil, err
1919	}
1920	f, err := http2typeFrameParser(fh.Type)(fr.frameCache, fh, fr.countError, payload)
1921	if err != nil {
1922		if ce, ok := err.(http2connError); ok {
1923			return nil, fr.connError(ce.Code, ce.Reason)
1924		}
1925		return nil, err
1926	}
1927	if err := fr.checkFrameOrder(f); err != nil {
1928		return nil, err
1929	}
1930	if fr.logReads {
1931		fr.debugReadLoggerf("http2: Framer %p: read %v", fr, http2summarizeFrame(f))
1932	}
1933	if fh.Type == http2FrameHeaders && fr.ReadMetaHeaders != nil {
1934		return fr.readMetaFrame(f.(*http2HeadersFrame))
1935	}
1936	return f, nil
1937}
1938
1939// connError returns ConnectionError(code) but first
1940// stashes away a public reason to the caller can optionally relay it
1941// to the peer before hanging up on them. This might help others debug
1942// their implementations.
1943func (fr *http2Framer) connError(code http2ErrCode, reason string) error {
1944	fr.errDetail = errors.New(reason)
1945	return http2ConnectionError(code)
1946}
1947
1948// checkFrameOrder reports an error if f is an invalid frame to return
1949// next from ReadFrame. Mostly it checks whether HEADERS and
1950// CONTINUATION frames are contiguous.
1951func (fr *http2Framer) checkFrameOrder(f http2Frame) error {
1952	last := fr.lastFrame
1953	fr.lastFrame = f
1954	if fr.AllowIllegalReads {
1955		return nil
1956	}
1957
1958	fh := f.Header()
1959	if fr.lastHeaderStream != 0 {
1960		if fh.Type != http2FrameContinuation {
1961			return fr.connError(http2ErrCodeProtocol,
1962				fmt.Sprintf("got %s for stream %d; expected CONTINUATION following %s for stream %d",
1963					fh.Type, fh.StreamID,
1964					last.Header().Type, fr.lastHeaderStream))
1965		}
1966		if fh.StreamID != fr.lastHeaderStream {
1967			return fr.connError(http2ErrCodeProtocol,
1968				fmt.Sprintf("got CONTINUATION for stream %d; expected stream %d",
1969					fh.StreamID, fr.lastHeaderStream))
1970		}
1971	} else if fh.Type == http2FrameContinuation {
1972		return fr.connError(http2ErrCodeProtocol, fmt.Sprintf("unexpected CONTINUATION for stream %d", fh.StreamID))
1973	}
1974
1975	switch fh.Type {
1976	case http2FrameHeaders, http2FrameContinuation:
1977		if fh.Flags.Has(http2FlagHeadersEndHeaders) {
1978			fr.lastHeaderStream = 0
1979		} else {
1980			fr.lastHeaderStream = fh.StreamID
1981		}
1982	}
1983
1984	return nil
1985}
1986
1987// A DataFrame conveys arbitrary, variable-length sequences of octets
1988// associated with a stream.
1989// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.1
1990type http2DataFrame struct {
1991	http2FrameHeader
1992	data []byte
1993}
1994
1995func (f *http2DataFrame) StreamEnded() bool {
1996	return f.http2FrameHeader.Flags.Has(http2FlagDataEndStream)
1997}
1998
1999// Data returns the frame's data octets, not including any padding
2000// size byte or padding suffix bytes.
2001// The caller must not retain the returned memory past the next
2002// call to ReadFrame.
2003func (f *http2DataFrame) Data() []byte {
2004	f.checkValid()
2005	return f.data
2006}
2007
2008func http2parseDataFrame(fc *http2frameCache, fh http2FrameHeader, countError func(string), payload []byte) (http2Frame, error) {
2009	if fh.StreamID == 0 {
2010		// DATA frames MUST be associated with a stream. If a
2011		// DATA frame is received whose stream identifier
2012		// field is 0x0, the recipient MUST respond with a
2013		// connection error (Section 5.4.1) of type
2014		// PROTOCOL_ERROR.
2015		countError("frame_data_stream_0")
2016		return nil, http2connError{http2ErrCodeProtocol, "DATA frame with stream ID 0"}
2017	}
2018	f := fc.getDataFrame()
2019	f.http2FrameHeader = fh
2020
2021	var padSize byte
2022	if fh.Flags.Has(http2FlagDataPadded) {
2023		var err error
2024		payload, padSize, err = http2readByte(payload)
2025		if err != nil {
2026			countError("frame_data_pad_byte_short")
2027			return nil, err
2028		}
2029	}
2030	if int(padSize) > len(payload) {
2031		// If the length of the padding is greater than the
2032		// length of the frame payload, the recipient MUST
2033		// treat this as a connection error.
2034		// Filed: https://github.com/http2/http2-spec/issues/610
2035		countError("frame_data_pad_too_big")
2036		return nil, http2connError{http2ErrCodeProtocol, "pad size larger than data payload"}
2037	}
2038	f.data = payload[:len(payload)-int(padSize)]
2039	return f, nil
2040}
2041
2042var (
2043	http2errStreamID    = errors.New("invalid stream ID")
2044	http2errDepStreamID = errors.New("invalid dependent stream ID")
2045	http2errPadLength   = errors.New("pad length too large")
2046	http2errPadBytes    = errors.New("padding bytes must all be zeros unless AllowIllegalWrites is enabled")
2047)
2048
2049func http2validStreamIDOrZero(streamID uint32) bool {
2050	return streamID&(1<<31) == 0
2051}
2052
2053func http2validStreamID(streamID uint32) bool {
2054	return streamID != 0 && streamID&(1<<31) == 0
2055}
2056
2057// WriteData writes a DATA frame.
2058//
2059// It will perform exactly one Write to the underlying Writer.
2060// It is the caller's responsibility not to violate the maximum frame size
2061// and to not call other Write methods concurrently.
2062func (f *http2Framer) WriteData(streamID uint32, endStream bool, data []byte) error {
2063	return f.WriteDataPadded(streamID, endStream, data, nil)
2064}
2065
2066// WriteDataPadded writes a DATA frame with optional padding.
2067//
2068// If pad is nil, the padding bit is not sent.
2069// The length of pad must not exceed 255 bytes.
2070// The bytes of pad must all be zero, unless f.AllowIllegalWrites is set.
2071//
2072// It will perform exactly one Write to the underlying Writer.
2073// It is the caller's responsibility not to violate the maximum frame size
2074// and to not call other Write methods concurrently.
2075func (f *http2Framer) WriteDataPadded(streamID uint32, endStream bool, data, pad []byte) error {
2076	if err := f.startWriteDataPadded(streamID, endStream, data, pad); err != nil {
2077		return err
2078	}
2079	return f.endWrite()
2080}
2081
2082// startWriteDataPadded is WriteDataPadded, but only writes the frame to the Framer's internal buffer.
2083// The caller should call endWrite to flush the frame to the underlying writer.
2084func (f *http2Framer) startWriteDataPadded(streamID uint32, endStream bool, data, pad []byte) error {
2085	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
2086		return http2errStreamID
2087	}
2088	if len(pad) > 0 {
2089		if len(pad) > 255 {
2090			return http2errPadLength
2091		}
2092		if !f.AllowIllegalWrites {
2093			for _, b := range pad {
2094				if b != 0 {
2095					// "Padding octets MUST be set to zero when sending."
2096					return http2errPadBytes
2097				}
2098			}
2099		}
2100	}
2101	var flags http2Flags
2102	if endStream {
2103		flags |= http2FlagDataEndStream
2104	}
2105	if pad != nil {
2106		flags |= http2FlagDataPadded
2107	}
2108	f.startWrite(http2FrameData, flags, streamID)
2109	if pad != nil {
2110		f.wbuf = append(f.wbuf, byte(len(pad)))
2111	}
2112	f.wbuf = append(f.wbuf, data...)
2113	f.wbuf = append(f.wbuf, pad...)
2114	return nil
2115}
2116
2117// A SettingsFrame conveys configuration parameters that affect how
2118// endpoints communicate, such as preferences and constraints on peer
2119// behavior.
2120//
2121// See https://httpwg.org/specs/rfc7540.html#SETTINGS
2122type http2SettingsFrame struct {
2123	http2FrameHeader
2124	p []byte
2125}
2126
2127func http2parseSettingsFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (http2Frame, error) {
2128	if fh.Flags.Has(http2FlagSettingsAck) && fh.Length > 0 {
2129		// When this (ACK 0x1) bit is set, the payload of the
2130		// SETTINGS frame MUST be empty. Receipt of a
2131		// SETTINGS frame with the ACK flag set and a length
2132		// field value other than 0 MUST be treated as a
2133		// connection error (Section 5.4.1) of type
2134		// FRAME_SIZE_ERROR.
2135		countError("frame_settings_ack_with_length")
2136		return nil, http2ConnectionError(http2ErrCodeFrameSize)
2137	}
2138	if fh.StreamID != 0 {
2139		// SETTINGS frames always apply to a connection,
2140		// never a single stream. The stream identifier for a
2141		// SETTINGS frame MUST be zero (0x0).  If an endpoint
2142		// receives a SETTINGS frame whose stream identifier
2143		// field is anything other than 0x0, the endpoint MUST
2144		// respond with a connection error (Section 5.4.1) of
2145		// type PROTOCOL_ERROR.
2146		countError("frame_settings_has_stream")
2147		return nil, http2ConnectionError(http2ErrCodeProtocol)
2148	}
2149	if len(p)%6 != 0 {
2150		countError("frame_settings_mod_6")
2151		// Expecting even number of 6 byte settings.
2152		return nil, http2ConnectionError(http2ErrCodeFrameSize)
2153	}
2154	f := &http2SettingsFrame{http2FrameHeader: fh, p: p}
2155	if v, ok := f.Value(http2SettingInitialWindowSize); ok && v > (1<<31)-1 {
2156		countError("frame_settings_window_size_too_big")
2157		// Values above the maximum flow control window size of 2^31 - 1 MUST
2158		// be treated as a connection error (Section 5.4.1) of type
2159		// FLOW_CONTROL_ERROR.
2160		return nil, http2ConnectionError(http2ErrCodeFlowControl)
2161	}
2162	return f, nil
2163}
2164
2165func (f *http2SettingsFrame) IsAck() bool {
2166	return f.http2FrameHeader.Flags.Has(http2FlagSettingsAck)
2167}
2168
2169func (f *http2SettingsFrame) Value(id http2SettingID) (v uint32, ok bool) {
2170	f.checkValid()
2171	for i := 0; i < f.NumSettings(); i++ {
2172		if s := f.Setting(i); s.ID == id {
2173			return s.Val, true
2174		}
2175	}
2176	return 0, false
2177}
2178
2179// Setting returns the setting from the frame at the given 0-based index.
2180// The index must be >= 0 and less than f.NumSettings().
2181func (f *http2SettingsFrame) Setting(i int) http2Setting {
2182	buf := f.p
2183	return http2Setting{
2184		ID:  http2SettingID(binary.BigEndian.Uint16(buf[i*6 : i*6+2])),
2185		Val: binary.BigEndian.Uint32(buf[i*6+2 : i*6+6]),
2186	}
2187}
2188
2189func (f *http2SettingsFrame) NumSettings() int { return len(f.p) / 6 }
2190
2191// HasDuplicates reports whether f contains any duplicate setting IDs.
2192func (f *http2SettingsFrame) HasDuplicates() bool {
2193	num := f.NumSettings()
2194	if num == 0 {
2195		return false
2196	}
2197	// If it's small enough (the common case), just do the n^2
2198	// thing and avoid a map allocation.
2199	if num < 10 {
2200		for i := 0; i < num; i++ {
2201			idi := f.Setting(i).ID
2202			for j := i + 1; j < num; j++ {
2203				idj := f.Setting(j).ID
2204				if idi == idj {
2205					return true
2206				}
2207			}
2208		}
2209		return false
2210	}
2211	seen := map[http2SettingID]bool{}
2212	for i := 0; i < num; i++ {
2213		id := f.Setting(i).ID
2214		if seen[id] {
2215			return true
2216		}
2217		seen[id] = true
2218	}
2219	return false
2220}
2221
2222// ForeachSetting runs fn for each setting.
2223// It stops and returns the first error.
2224func (f *http2SettingsFrame) ForeachSetting(fn func(http2Setting) error) error {
2225	f.checkValid()
2226	for i := 0; i < f.NumSettings(); i++ {
2227		if err := fn(f.Setting(i)); err != nil {
2228			return err
2229		}
2230	}
2231	return nil
2232}
2233
2234// WriteSettings writes a SETTINGS frame with zero or more settings
2235// specified and the ACK bit not set.
2236//
2237// It will perform exactly one Write to the underlying Writer.
2238// It is the caller's responsibility to not call other Write methods concurrently.
2239func (f *http2Framer) WriteSettings(settings ...http2Setting) error {
2240	f.startWrite(http2FrameSettings, 0, 0)
2241	for _, s := range settings {
2242		f.writeUint16(uint16(s.ID))
2243		f.writeUint32(s.Val)
2244	}
2245	return f.endWrite()
2246}
2247
2248// WriteSettingsAck writes an empty SETTINGS frame with the ACK bit set.
2249//
2250// It will perform exactly one Write to the underlying Writer.
2251// It is the caller's responsibility to not call other Write methods concurrently.
2252func (f *http2Framer) WriteSettingsAck() error {
2253	f.startWrite(http2FrameSettings, http2FlagSettingsAck, 0)
2254	return f.endWrite()
2255}
2256
2257// A PingFrame is a mechanism for measuring a minimal round trip time
2258// from the sender, as well as determining whether an idle connection
2259// is still functional.
2260// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.7
2261type http2PingFrame struct {
2262	http2FrameHeader
2263	Data [8]byte
2264}
2265
2266func (f *http2PingFrame) IsAck() bool { return f.Flags.Has(http2FlagPingAck) }
2267
2268func http2parsePingFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), payload []byte) (http2Frame, error) {
2269	if len(payload) != 8 {
2270		countError("frame_ping_length")
2271		return nil, http2ConnectionError(http2ErrCodeFrameSize)
2272	}
2273	if fh.StreamID != 0 {
2274		countError("frame_ping_has_stream")
2275		return nil, http2ConnectionError(http2ErrCodeProtocol)
2276	}
2277	f := &http2PingFrame{http2FrameHeader: fh}
2278	copy(f.Data[:], payload)
2279	return f, nil
2280}
2281
2282func (f *http2Framer) WritePing(ack bool, data [8]byte) error {
2283	var flags http2Flags
2284	if ack {
2285		flags = http2FlagPingAck
2286	}
2287	f.startWrite(http2FramePing, flags, 0)
2288	f.writeBytes(data[:])
2289	return f.endWrite()
2290}
2291
2292// A GoAwayFrame informs the remote peer to stop creating streams on this connection.
2293// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.8
2294type http2GoAwayFrame struct {
2295	http2FrameHeader
2296	LastStreamID uint32
2297	ErrCode      http2ErrCode
2298	debugData    []byte
2299}
2300
2301// DebugData returns any debug data in the GOAWAY frame. Its contents
2302// are not defined.
2303// The caller must not retain the returned memory past the next
2304// call to ReadFrame.
2305func (f *http2GoAwayFrame) DebugData() []byte {
2306	f.checkValid()
2307	return f.debugData
2308}
2309
2310func http2parseGoAwayFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (http2Frame, error) {
2311	if fh.StreamID != 0 {
2312		countError("frame_goaway_has_stream")
2313		return nil, http2ConnectionError(http2ErrCodeProtocol)
2314	}
2315	if len(p) < 8 {
2316		countError("frame_goaway_short")
2317		return nil, http2ConnectionError(http2ErrCodeFrameSize)
2318	}
2319	return &http2GoAwayFrame{
2320		http2FrameHeader: fh,
2321		LastStreamID:     binary.BigEndian.Uint32(p[:4]) & (1<<31 - 1),
2322		ErrCode:          http2ErrCode(binary.BigEndian.Uint32(p[4:8])),
2323		debugData:        p[8:],
2324	}, nil
2325}
2326
2327func (f *http2Framer) WriteGoAway(maxStreamID uint32, code http2ErrCode, debugData []byte) error {
2328	f.startWrite(http2FrameGoAway, 0, 0)
2329	f.writeUint32(maxStreamID & (1<<31 - 1))
2330	f.writeUint32(uint32(code))
2331	f.writeBytes(debugData)
2332	return f.endWrite()
2333}
2334
2335// An UnknownFrame is the frame type returned when the frame type is unknown
2336// or no specific frame type parser exists.
2337type http2UnknownFrame struct {
2338	http2FrameHeader
2339	p []byte
2340}
2341
2342// Payload returns the frame's payload (after the header).  It is not
2343// valid to call this method after a subsequent call to
2344// Framer.ReadFrame, nor is it valid to retain the returned slice.
2345// The memory is owned by the Framer and is invalidated when the next
2346// frame is read.
2347func (f *http2UnknownFrame) Payload() []byte {
2348	f.checkValid()
2349	return f.p
2350}
2351
2352func http2parseUnknownFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (http2Frame, error) {
2353	return &http2UnknownFrame{fh, p}, nil
2354}
2355
2356// A WindowUpdateFrame is used to implement flow control.
2357// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.9
2358type http2WindowUpdateFrame struct {
2359	http2FrameHeader
2360	Increment uint32 // never read with high bit set
2361}
2362
2363func http2parseWindowUpdateFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (http2Frame, error) {
2364	if len(p) != 4 {
2365		countError("frame_windowupdate_bad_len")
2366		return nil, http2ConnectionError(http2ErrCodeFrameSize)
2367	}
2368	inc := binary.BigEndian.Uint32(p[:4]) & 0x7fffffff // mask off high reserved bit
2369	if inc == 0 {
2370		// A receiver MUST treat the receipt of a
2371		// WINDOW_UPDATE frame with an flow control window
2372		// increment of 0 as a stream error (Section 5.4.2) of
2373		// type PROTOCOL_ERROR; errors on the connection flow
2374		// control window MUST be treated as a connection
2375		// error (Section 5.4.1).
2376		if fh.StreamID == 0 {
2377			countError("frame_windowupdate_zero_inc_conn")
2378			return nil, http2ConnectionError(http2ErrCodeProtocol)
2379		}
2380		countError("frame_windowupdate_zero_inc_stream")
2381		return nil, http2streamError(fh.StreamID, http2ErrCodeProtocol)
2382	}
2383	return &http2WindowUpdateFrame{
2384		http2FrameHeader: fh,
2385		Increment:        inc,
2386	}, nil
2387}
2388
2389// WriteWindowUpdate writes a WINDOW_UPDATE frame.
2390// The increment value must be between 1 and 2,147,483,647, inclusive.
2391// If the Stream ID is zero, the window update applies to the
2392// connection as a whole.
2393func (f *http2Framer) WriteWindowUpdate(streamID, incr uint32) error {
2394	// "The legal range for the increment to the flow control window is 1 to 2^31-1 (2,147,483,647) octets."
2395	if (incr < 1 || incr > 2147483647) && !f.AllowIllegalWrites {
2396		return errors.New("illegal window increment value")
2397	}
2398	f.startWrite(http2FrameWindowUpdate, 0, streamID)
2399	f.writeUint32(incr)
2400	return f.endWrite()
2401}
2402
2403// A HeadersFrame is used to open a stream and additionally carries a
2404// header block fragment.
2405type http2HeadersFrame struct {
2406	http2FrameHeader
2407
2408	// Priority is set if FlagHeadersPriority is set in the FrameHeader.
2409	Priority http2PriorityParam
2410
2411	headerFragBuf []byte // not owned
2412}
2413
2414func (f *http2HeadersFrame) HeaderBlockFragment() []byte {
2415	f.checkValid()
2416	return f.headerFragBuf
2417}
2418
2419func (f *http2HeadersFrame) HeadersEnded() bool {
2420	return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndHeaders)
2421}
2422
2423func (f *http2HeadersFrame) StreamEnded() bool {
2424	return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndStream)
2425}
2426
2427func (f *http2HeadersFrame) HasPriority() bool {
2428	return f.http2FrameHeader.Flags.Has(http2FlagHeadersPriority)
2429}
2430
2431func http2parseHeadersFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (_ http2Frame, err error) {
2432	hf := &http2HeadersFrame{
2433		http2FrameHeader: fh,
2434	}
2435	if fh.StreamID == 0 {
2436		// HEADERS frames MUST be associated with a stream. If a HEADERS frame
2437		// is received whose stream identifier field is 0x0, the recipient MUST
2438		// respond with a connection error (Section 5.4.1) of type
2439		// PROTOCOL_ERROR.
2440		countError("frame_headers_zero_stream")
2441		return nil, http2connError{http2ErrCodeProtocol, "HEADERS frame with stream ID 0"}
2442	}
2443	var padLength uint8
2444	if fh.Flags.Has(http2FlagHeadersPadded) {
2445		if p, padLength, err = http2readByte(p); err != nil {
2446			countError("frame_headers_pad_short")
2447			return
2448		}
2449	}
2450	if fh.Flags.Has(http2FlagHeadersPriority) {
2451		var v uint32
2452		p, v, err = http2readUint32(p)
2453		if err != nil {
2454			countError("frame_headers_prio_short")
2455			return nil, err
2456		}
2457		hf.Priority.StreamDep = v & 0x7fffffff
2458		hf.Priority.Exclusive = (v != hf.Priority.StreamDep) // high bit was set
2459		p, hf.Priority.Weight, err = http2readByte(p)
2460		if err != nil {
2461			countError("frame_headers_prio_weight_short")
2462			return nil, err
2463		}
2464	}
2465	if len(p)-int(padLength) < 0 {
2466		countError("frame_headers_pad_too_big")
2467		return nil, http2streamError(fh.StreamID, http2ErrCodeProtocol)
2468	}
2469	hf.headerFragBuf = p[:len(p)-int(padLength)]
2470	return hf, nil
2471}
2472
2473// HeadersFrameParam are the parameters for writing a HEADERS frame.
2474type http2HeadersFrameParam struct {
2475	// StreamID is the required Stream ID to initiate.
2476	StreamID uint32
2477	// BlockFragment is part (or all) of a Header Block.
2478	BlockFragment []byte
2479
2480	// EndStream indicates that the header block is the last that
2481	// the endpoint will send for the identified stream. Setting
2482	// this flag causes the stream to enter one of "half closed"
2483	// states.
2484	EndStream bool
2485
2486	// EndHeaders indicates that this frame contains an entire
2487	// header block and is not followed by any
2488	// CONTINUATION frames.
2489	EndHeaders bool
2490
2491	// PadLength is the optional number of bytes of zeros to add
2492	// to this frame.
2493	PadLength uint8
2494
2495	// Priority, if non-zero, includes stream priority information
2496	// in the HEADER frame.
2497	Priority http2PriorityParam
2498}
2499
2500// WriteHeaders writes a single HEADERS frame.
2501//
2502// This is a low-level header writing method. Encoding headers and
2503// splitting them into any necessary CONTINUATION frames is handled
2504// elsewhere.
2505//
2506// It will perform exactly one Write to the underlying Writer.
2507// It is the caller's responsibility to not call other Write methods concurrently.
2508func (f *http2Framer) WriteHeaders(p http2HeadersFrameParam) error {
2509	if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites {
2510		return http2errStreamID
2511	}
2512	var flags http2Flags
2513	if p.PadLength != 0 {
2514		flags |= http2FlagHeadersPadded
2515	}
2516	if p.EndStream {
2517		flags |= http2FlagHeadersEndStream
2518	}
2519	if p.EndHeaders {
2520		flags |= http2FlagHeadersEndHeaders
2521	}
2522	if !p.Priority.IsZero() {
2523		flags |= http2FlagHeadersPriority
2524	}
2525	f.startWrite(http2FrameHeaders, flags, p.StreamID)
2526	if p.PadLength != 0 {
2527		f.writeByte(p.PadLength)
2528	}
2529	if !p.Priority.IsZero() {
2530		v := p.Priority.StreamDep
2531		if !http2validStreamIDOrZero(v) && !f.AllowIllegalWrites {
2532			return http2errDepStreamID
2533		}
2534		if p.Priority.Exclusive {
2535			v |= 1 << 31
2536		}
2537		f.writeUint32(v)
2538		f.writeByte(p.Priority.Weight)
2539	}
2540	f.wbuf = append(f.wbuf, p.BlockFragment...)
2541	f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...)
2542	return f.endWrite()
2543}
2544
2545// A PriorityFrame specifies the sender-advised priority of a stream.
2546// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.3
2547type http2PriorityFrame struct {
2548	http2FrameHeader
2549	http2PriorityParam
2550}
2551
2552// PriorityParam are the stream prioritzation parameters.
2553type http2PriorityParam struct {
2554	// StreamDep is a 31-bit stream identifier for the
2555	// stream that this stream depends on. Zero means no
2556	// dependency.
2557	StreamDep uint32
2558
2559	// Exclusive is whether the dependency is exclusive.
2560	Exclusive bool
2561
2562	// Weight is the stream's zero-indexed weight. It should be
2563	// set together with StreamDep, or neither should be set. Per
2564	// the spec, "Add one to the value to obtain a weight between
2565	// 1 and 256."
2566	Weight uint8
2567}
2568
2569func (p http2PriorityParam) IsZero() bool {
2570	return p == http2PriorityParam{}
2571}
2572
2573func http2parsePriorityFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), payload []byte) (http2Frame, error) {
2574	if fh.StreamID == 0 {
2575		countError("frame_priority_zero_stream")
2576		return nil, http2connError{http2ErrCodeProtocol, "PRIORITY frame with stream ID 0"}
2577	}
2578	if len(payload) != 5 {
2579		countError("frame_priority_bad_length")
2580		return nil, http2connError{http2ErrCodeFrameSize, fmt.Sprintf("PRIORITY frame payload size was %d; want 5", len(payload))}
2581	}
2582	v := binary.BigEndian.Uint32(payload[:4])
2583	streamID := v & 0x7fffffff // mask off high bit
2584	return &http2PriorityFrame{
2585		http2FrameHeader: fh,
2586		http2PriorityParam: http2PriorityParam{
2587			Weight:    payload[4],
2588			StreamDep: streamID,
2589			Exclusive: streamID != v, // was high bit set?
2590		},
2591	}, nil
2592}
2593
2594// WritePriority writes a PRIORITY frame.
2595//
2596// It will perform exactly one Write to the underlying Writer.
2597// It is the caller's responsibility to not call other Write methods concurrently.
2598func (f *http2Framer) WritePriority(streamID uint32, p http2PriorityParam) error {
2599	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
2600		return http2errStreamID
2601	}
2602	if !http2validStreamIDOrZero(p.StreamDep) {
2603		return http2errDepStreamID
2604	}
2605	f.startWrite(http2FramePriority, 0, streamID)
2606	v := p.StreamDep
2607	if p.Exclusive {
2608		v |= 1 << 31
2609	}
2610	f.writeUint32(v)
2611	f.writeByte(p.Weight)
2612	return f.endWrite()
2613}
2614
2615// A RSTStreamFrame allows for abnormal termination of a stream.
2616// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.4
2617type http2RSTStreamFrame struct {
2618	http2FrameHeader
2619	ErrCode http2ErrCode
2620}
2621
2622func http2parseRSTStreamFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (http2Frame, error) {
2623	if len(p) != 4 {
2624		countError("frame_rststream_bad_len")
2625		return nil, http2ConnectionError(http2ErrCodeFrameSize)
2626	}
2627	if fh.StreamID == 0 {
2628		countError("frame_rststream_zero_stream")
2629		return nil, http2ConnectionError(http2ErrCodeProtocol)
2630	}
2631	return &http2RSTStreamFrame{fh, http2ErrCode(binary.BigEndian.Uint32(p[:4]))}, nil
2632}
2633
2634// WriteRSTStream writes a RST_STREAM frame.
2635//
2636// It will perform exactly one Write to the underlying Writer.
2637// It is the caller's responsibility to not call other Write methods concurrently.
2638func (f *http2Framer) WriteRSTStream(streamID uint32, code http2ErrCode) error {
2639	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
2640		return http2errStreamID
2641	}
2642	f.startWrite(http2FrameRSTStream, 0, streamID)
2643	f.writeUint32(uint32(code))
2644	return f.endWrite()
2645}
2646
2647// A ContinuationFrame is used to continue a sequence of header block fragments.
2648// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.10
2649type http2ContinuationFrame struct {
2650	http2FrameHeader
2651	headerFragBuf []byte
2652}
2653
2654func http2parseContinuationFrame(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (http2Frame, error) {
2655	if fh.StreamID == 0 {
2656		countError("frame_continuation_zero_stream")
2657		return nil, http2connError{http2ErrCodeProtocol, "CONTINUATION frame with stream ID 0"}
2658	}
2659	return &http2ContinuationFrame{fh, p}, nil
2660}
2661
2662func (f *http2ContinuationFrame) HeaderBlockFragment() []byte {
2663	f.checkValid()
2664	return f.headerFragBuf
2665}
2666
2667func (f *http2ContinuationFrame) HeadersEnded() bool {
2668	return f.http2FrameHeader.Flags.Has(http2FlagContinuationEndHeaders)
2669}
2670
2671// WriteContinuation writes a CONTINUATION frame.
2672//
2673// It will perform exactly one Write to the underlying Writer.
2674// It is the caller's responsibility to not call other Write methods concurrently.
2675func (f *http2Framer) WriteContinuation(streamID uint32, endHeaders bool, headerBlockFragment []byte) error {
2676	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
2677		return http2errStreamID
2678	}
2679	var flags http2Flags
2680	if endHeaders {
2681		flags |= http2FlagContinuationEndHeaders
2682	}
2683	f.startWrite(http2FrameContinuation, flags, streamID)
2684	f.wbuf = append(f.wbuf, headerBlockFragment...)
2685	return f.endWrite()
2686}
2687
2688// A PushPromiseFrame is used to initiate a server stream.
2689// See https://httpwg.org/specs/rfc7540.html#rfc.section.6.6
2690type http2PushPromiseFrame struct {
2691	http2FrameHeader
2692	PromiseID     uint32
2693	headerFragBuf []byte // not owned
2694}
2695
2696func (f *http2PushPromiseFrame) HeaderBlockFragment() []byte {
2697	f.checkValid()
2698	return f.headerFragBuf
2699}
2700
2701func (f *http2PushPromiseFrame) HeadersEnded() bool {
2702	return f.http2FrameHeader.Flags.Has(http2FlagPushPromiseEndHeaders)
2703}
2704
2705func http2parsePushPromise(_ *http2frameCache, fh http2FrameHeader, countError func(string), p []byte) (_ http2Frame, err error) {
2706	pp := &http2PushPromiseFrame{
2707		http2FrameHeader: fh,
2708	}
2709	if pp.StreamID == 0 {
2710		// PUSH_PROMISE frames MUST be associated with an existing,
2711		// peer-initiated stream. The stream identifier of a
2712		// PUSH_PROMISE frame indicates the stream it is associated
2713		// with. If the stream identifier field specifies the value
2714		// 0x0, a recipient MUST respond with a connection error
2715		// (Section 5.4.1) of type PROTOCOL_ERROR.
2716		countError("frame_pushpromise_zero_stream")
2717		return nil, http2ConnectionError(http2ErrCodeProtocol)
2718	}
2719	// The PUSH_PROMISE frame includes optional padding.
2720	// Padding fields and flags are identical to those defined for DATA frames
2721	var padLength uint8
2722	if fh.Flags.Has(http2FlagPushPromisePadded) {
2723		if p, padLength, err = http2readByte(p); err != nil {
2724			countError("frame_pushpromise_pad_short")
2725			return
2726		}
2727	}
2728
2729	p, pp.PromiseID, err = http2readUint32(p)
2730	if err != nil {
2731		countError("frame_pushpromise_promiseid_short")
2732		return
2733	}
2734	pp.PromiseID = pp.PromiseID & (1<<31 - 1)
2735
2736	if int(padLength) > len(p) {
2737		// like the DATA frame, error out if padding is longer than the body.
2738		countError("frame_pushpromise_pad_too_big")
2739		return nil, http2ConnectionError(http2ErrCodeProtocol)
2740	}
2741	pp.headerFragBuf = p[:len(p)-int(padLength)]
2742	return pp, nil
2743}
2744
2745// PushPromiseParam are the parameters for writing a PUSH_PROMISE frame.
2746type http2PushPromiseParam struct {
2747	// StreamID is the required Stream ID to initiate.
2748	StreamID uint32
2749
2750	// PromiseID is the required Stream ID which this
2751	// Push Promises
2752	PromiseID uint32
2753
2754	// BlockFragment is part (or all) of a Header Block.
2755	BlockFragment []byte
2756
2757	// EndHeaders indicates that this frame contains an entire
2758	// header block and is not followed by any
2759	// CONTINUATION frames.
2760	EndHeaders bool
2761
2762	// PadLength is the optional number of bytes of zeros to add
2763	// to this frame.
2764	PadLength uint8
2765}
2766
2767// WritePushPromise writes a single PushPromise Frame.
2768//
2769// As with Header Frames, This is the low level call for writing
2770// individual frames. Continuation frames are handled elsewhere.
2771//
2772// It will perform exactly one Write to the underlying Writer.
2773// It is the caller's responsibility to not call other Write methods concurrently.
2774func (f *http2Framer) WritePushPromise(p http2PushPromiseParam) error {
2775	if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites {
2776		return http2errStreamID
2777	}
2778	var flags http2Flags
2779	if p.PadLength != 0 {
2780		flags |= http2FlagPushPromisePadded
2781	}
2782	if p.EndHeaders {
2783		flags |= http2FlagPushPromiseEndHeaders
2784	}
2785	f.startWrite(http2FramePushPromise, flags, p.StreamID)
2786	if p.PadLength != 0 {
2787		f.writeByte(p.PadLength)
2788	}
2789	if !http2validStreamID(p.PromiseID) && !f.AllowIllegalWrites {
2790		return http2errStreamID
2791	}
2792	f.writeUint32(p.PromiseID)
2793	f.wbuf = append(f.wbuf, p.BlockFragment...)
2794	f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...)
2795	return f.endWrite()
2796}
2797
2798// WriteRawFrame writes a raw frame. This can be used to write
2799// extension frames unknown to this package.
2800func (f *http2Framer) WriteRawFrame(t http2FrameType, flags http2Flags, streamID uint32, payload []byte) error {
2801	f.startWrite(t, flags, streamID)
2802	f.writeBytes(payload)
2803	return f.endWrite()
2804}
2805
2806func http2readByte(p []byte) (remain []byte, b byte, err error) {
2807	if len(p) == 0 {
2808		return nil, 0, io.ErrUnexpectedEOF
2809	}
2810	return p[1:], p[0], nil
2811}
2812
2813func http2readUint32(p []byte) (remain []byte, v uint32, err error) {
2814	if len(p) < 4 {
2815		return nil, 0, io.ErrUnexpectedEOF
2816	}
2817	return p[4:], binary.BigEndian.Uint32(p[:4]), nil
2818}
2819
2820type http2streamEnder interface {
2821	StreamEnded() bool
2822}
2823
2824type http2headersEnder interface {
2825	HeadersEnded() bool
2826}
2827
2828type http2headersOrContinuation interface {
2829	http2headersEnder
2830	HeaderBlockFragment() []byte
2831}
2832
2833// A MetaHeadersFrame is the representation of one HEADERS frame and
2834// zero or more contiguous CONTINUATION frames and the decoding of
2835// their HPACK-encoded contents.
2836//
2837// This type of frame does not appear on the wire and is only returned
2838// by the Framer when Framer.ReadMetaHeaders is set.
2839type http2MetaHeadersFrame struct {
2840	*http2HeadersFrame
2841
2842	// Fields are the fields contained in the HEADERS and
2843	// CONTINUATION frames. The underlying slice is owned by the
2844	// Framer and must not be retained after the next call to
2845	// ReadFrame.
2846	//
2847	// Fields are guaranteed to be in the correct http2 order and
2848	// not have unknown pseudo header fields or invalid header
2849	// field names or values. Required pseudo header fields may be
2850	// missing, however. Use the MetaHeadersFrame.Pseudo accessor
2851	// method access pseudo headers.
2852	Fields []hpack.HeaderField
2853
2854	// Truncated is whether the max header list size limit was hit
2855	// and Fields is incomplete. The hpack decoder state is still
2856	// valid, however.
2857	Truncated bool
2858}
2859
2860// PseudoValue returns the given pseudo header field's value.
2861// The provided pseudo field should not contain the leading colon.
2862func (mh *http2MetaHeadersFrame) PseudoValue(pseudo string) string {
2863	for _, hf := range mh.Fields {
2864		if !hf.IsPseudo() {
2865			return ""
2866		}
2867		if hf.Name[1:] == pseudo {
2868			return hf.Value
2869		}
2870	}
2871	return ""
2872}
2873
2874// RegularFields returns the regular (non-pseudo) header fields of mh.
2875// The caller does not own the returned slice.
2876func (mh *http2MetaHeadersFrame) RegularFields() []hpack.HeaderField {
2877	for i, hf := range mh.Fields {
2878		if !hf.IsPseudo() {
2879			return mh.Fields[i:]
2880		}
2881	}
2882	return nil
2883}
2884
2885// PseudoFields returns the pseudo header fields of mh.
2886// The caller does not own the returned slice.
2887func (mh *http2MetaHeadersFrame) PseudoFields() []hpack.HeaderField {
2888	for i, hf := range mh.Fields {
2889		if !hf.IsPseudo() {
2890			return mh.Fields[:i]
2891		}
2892	}
2893	return mh.Fields
2894}
2895
2896func (mh *http2MetaHeadersFrame) checkPseudos() error {
2897	var isRequest, isResponse bool
2898	pf := mh.PseudoFields()
2899	for i, hf := range pf {
2900		switch hf.Name {
2901		case ":method", ":path", ":scheme", ":authority":
2902			isRequest = true
2903		case ":status":
2904			isResponse = true
2905		default:
2906			return http2pseudoHeaderError(hf.Name)
2907		}
2908		// Check for duplicates.
2909		// This would be a bad algorithm, but N is 4.
2910		// And this doesn't allocate.
2911		for _, hf2 := range pf[:i] {
2912			if hf.Name == hf2.Name {
2913				return http2duplicatePseudoHeaderError(hf.Name)
2914			}
2915		}
2916	}
2917	if isRequest && isResponse {
2918		return http2errMixPseudoHeaderTypes
2919	}
2920	return nil
2921}
2922
2923func (fr *http2Framer) maxHeaderStringLen() int {
2924	v := int(fr.maxHeaderListSize())
2925	if v < 0 {
2926		// If maxHeaderListSize overflows an int, use no limit (0).
2927		return 0
2928	}
2929	return v
2930}
2931
2932// readMetaFrame returns 0 or more CONTINUATION frames from fr and
2933// merge them into the provided hf and returns a MetaHeadersFrame
2934// with the decoded hpack values.
2935func (fr *http2Framer) readMetaFrame(hf *http2HeadersFrame) (http2Frame, error) {
2936	if fr.AllowIllegalReads {
2937		return nil, errors.New("illegal use of AllowIllegalReads with ReadMetaHeaders")
2938	}
2939	mh := &http2MetaHeadersFrame{
2940		http2HeadersFrame: hf,
2941	}
2942	var remainSize = fr.maxHeaderListSize()
2943	var sawRegular bool
2944
2945	var invalid error // pseudo header field errors
2946	hdec := fr.ReadMetaHeaders
2947	hdec.SetEmitEnabled(true)
2948	hdec.SetMaxStringLength(fr.maxHeaderStringLen())
2949	hdec.SetEmitFunc(func(hf hpack.HeaderField) {
2950		if http2VerboseLogs && fr.logReads {
2951			fr.debugReadLoggerf("http2: decoded hpack field %+v", hf)
2952		}
2953		if !httpguts.ValidHeaderFieldValue(hf.Value) {
2954			// Don't include the value in the error, because it may be sensitive.
2955			invalid = http2headerFieldValueError(hf.Name)
2956		}
2957		isPseudo := strings.HasPrefix(hf.Name, ":")
2958		if isPseudo {
2959			if sawRegular {
2960				invalid = http2errPseudoAfterRegular
2961			}
2962		} else {
2963			sawRegular = true
2964			if !http2validWireHeaderFieldName(hf.Name) {
2965				invalid = http2headerFieldNameError(hf.Name)
2966			}
2967		}
2968
2969		if invalid != nil {
2970			hdec.SetEmitEnabled(false)
2971			return
2972		}
2973
2974		size := hf.Size()
2975		if size > remainSize {
2976			hdec.SetEmitEnabled(false)
2977			mh.Truncated = true
2978			remainSize = 0
2979			return
2980		}
2981		remainSize -= size
2982
2983		mh.Fields = append(mh.Fields, hf)
2984	})
2985	// Lose reference to MetaHeadersFrame:
2986	defer hdec.SetEmitFunc(func(hf hpack.HeaderField) {})
2987
2988	var hc http2headersOrContinuation = hf
2989	for {
2990		frag := hc.HeaderBlockFragment()
2991
2992		// Avoid parsing large amounts of headers that we will then discard.
2993		// If the sender exceeds the max header list size by too much,
2994		// skip parsing the fragment and close the connection.
2995		//
2996		// "Too much" is either any CONTINUATION frame after we've already
2997		// exceeded the max header list size (in which case remainSize is 0),
2998		// or a frame whose encoded size is more than twice the remaining
2999		// header list bytes we're willing to accept.
3000		if int64(len(frag)) > int64(2*remainSize) {
3001			if http2VerboseLogs {
3002				log.Printf("http2: header list too large")
3003			}
3004			// It would be nice to send a RST_STREAM before sending the GOAWAY,
3005			// but the structure of the server's frame writer makes this difficult.
3006			return mh, http2ConnectionError(http2ErrCodeProtocol)
3007		}
3008
3009		// Also close the connection after any CONTINUATION frame following an
3010		// invalid header, since we stop tracking the size of the headers after
3011		// an invalid one.
3012		if invalid != nil {
3013			if http2VerboseLogs {
3014				log.Printf("http2: invalid header: %v", invalid)
3015			}
3016			// It would be nice to send a RST_STREAM before sending the GOAWAY,
3017			// but the structure of the server's frame writer makes this difficult.
3018			return mh, http2ConnectionError(http2ErrCodeProtocol)
3019		}
3020
3021		if _, err := hdec.Write(frag); err != nil {
3022			return mh, http2ConnectionError(http2ErrCodeCompression)
3023		}
3024
3025		if hc.HeadersEnded() {
3026			break
3027		}
3028		if f, err := fr.ReadFrame(); err != nil {
3029			return nil, err
3030		} else {
3031			hc = f.(*http2ContinuationFrame) // guaranteed by checkFrameOrder
3032		}
3033	}
3034
3035	mh.http2HeadersFrame.headerFragBuf = nil
3036	mh.http2HeadersFrame.invalidate()
3037
3038	if err := hdec.Close(); err != nil {
3039		return mh, http2ConnectionError(http2ErrCodeCompression)
3040	}
3041	if invalid != nil {
3042		fr.errDetail = invalid
3043		if http2VerboseLogs {
3044			log.Printf("http2: invalid header: %v", invalid)
3045		}
3046		return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol, invalid}
3047	}
3048	if err := mh.checkPseudos(); err != nil {
3049		fr.errDetail = err
3050		if http2VerboseLogs {
3051			log.Printf("http2: invalid pseudo headers: %v", err)
3052		}
3053		return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol, err}
3054	}
3055	return mh, nil
3056}
3057
3058func http2summarizeFrame(f http2Frame) string {
3059	var buf bytes.Buffer
3060	f.Header().writeDebug(&buf)
3061	switch f := f.(type) {
3062	case *http2SettingsFrame:
3063		n := 0
3064		f.ForeachSetting(func(s http2Setting) error {
3065			n++
3066			if n == 1 {
3067				buf.WriteString(", settings:")
3068			}
3069			fmt.Fprintf(&buf, " %v=%v,", s.ID, s.Val)
3070			return nil
3071		})
3072		if n > 0 {
3073			buf.Truncate(buf.Len() - 1) // remove trailing comma
3074		}
3075	case *http2DataFrame:
3076		data := f.Data()
3077		const max = 256
3078		if len(data) > max {
3079			data = data[:max]
3080		}
3081		fmt.Fprintf(&buf, " data=%q", data)
3082		if len(f.Data()) > max {
3083			fmt.Fprintf(&buf, " (%d bytes omitted)", len(f.Data())-max)
3084		}
3085	case *http2WindowUpdateFrame:
3086		if f.StreamID == 0 {
3087			buf.WriteString(" (conn)")
3088		}
3089		fmt.Fprintf(&buf, " incr=%v", f.Increment)
3090	case *http2PingFrame:
3091		fmt.Fprintf(&buf, " ping=%q", f.Data[:])
3092	case *http2GoAwayFrame:
3093		fmt.Fprintf(&buf, " LastStreamID=%v ErrCode=%v Debug=%q",
3094			f.LastStreamID, f.ErrCode, f.debugData)
3095	case *http2RSTStreamFrame:
3096		fmt.Fprintf(&buf, " ErrCode=%v", f.ErrCode)
3097	}
3098	return buf.String()
3099}
3100
3101var http2DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1"
3102
3103type http2goroutineLock uint64
3104
3105func http2newGoroutineLock() http2goroutineLock {
3106	if !http2DebugGoroutines {
3107		return 0
3108	}
3109	return http2goroutineLock(http2curGoroutineID())
3110}
3111
3112func (g http2goroutineLock) check() {
3113	if !http2DebugGoroutines {
3114		return
3115	}
3116	if http2curGoroutineID() != uint64(g) {
3117		panic("running on the wrong goroutine")
3118	}
3119}
3120
3121func (g http2goroutineLock) checkNotOn() {
3122	if !http2DebugGoroutines {
3123		return
3124	}
3125	if http2curGoroutineID() == uint64(g) {
3126		panic("running on the wrong goroutine")
3127	}
3128}
3129
3130var http2goroutineSpace = []byte("goroutine ")
3131
3132func http2curGoroutineID() uint64 {
3133	bp := http2littleBuf.Get().(*[]byte)
3134	defer http2littleBuf.Put(bp)
3135	b := *bp
3136	b = b[:runtime.Stack(b, false)]
3137	// Parse the 4707 out of "goroutine 4707 ["
3138	b = bytes.TrimPrefix(b, http2goroutineSpace)
3139	i := bytes.IndexByte(b, ' ')
3140	if i < 0 {
3141		panic(fmt.Sprintf("No space found in %q", b))
3142	}
3143	b = b[:i]
3144	n, err := http2parseUintBytes(b, 10, 64)
3145	if err != nil {
3146		panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
3147	}
3148	return n
3149}
3150
3151var http2littleBuf = sync.Pool{
3152	New: func() interface{} {
3153		buf := make([]byte, 64)
3154		return &buf
3155	},
3156}
3157
3158// parseUintBytes is like strconv.ParseUint, but using a []byte.
3159func http2parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) {
3160	var cutoff, maxVal uint64
3161
3162	if bitSize == 0 {
3163		bitSize = int(strconv.IntSize)
3164	}
3165
3166	s0 := s
3167	switch {
3168	case len(s) < 1:
3169		err = strconv.ErrSyntax
3170		goto Error
3171
3172	case 2 <= base && base <= 36:
3173		// valid base; nothing to do
3174
3175	case base == 0:
3176		// Look for octal, hex prefix.
3177		switch {
3178		case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
3179			base = 16
3180			s = s[2:]
3181			if len(s) < 1 {
3182				err = strconv.ErrSyntax
3183				goto Error
3184			}
3185		case s[0] == '0':
3186			base = 8
3187		default:
3188			base = 10
3189		}
3190
3191	default:
3192		err = errors.New("invalid base " + strconv.Itoa(base))
3193		goto Error
3194	}
3195
3196	n = 0
3197	cutoff = http2cutoff64(base)
3198	maxVal = 1<<uint(bitSize) - 1
3199
3200	for i := 0; i < len(s); i++ {
3201		var v byte
3202		d := s[i]
3203		switch {
3204		case '0' <= d && d <= '9':
3205			v = d - '0'
3206		case 'a' <= d && d <= 'z':
3207			v = d - 'a' + 10
3208		case 'A' <= d && d <= 'Z':
3209			v = d - 'A' + 10
3210		default:
3211			n = 0
3212			err = strconv.ErrSyntax
3213			goto Error
3214		}
3215		if int(v) >= base {
3216			n = 0
3217			err = strconv.ErrSyntax
3218			goto Error
3219		}
3220
3221		if n >= cutoff {
3222			// n*base overflows
3223			n = 1<<64 - 1
3224			err = strconv.ErrRange
3225			goto Error
3226		}
3227		n *= uint64(base)
3228
3229		n1 := n + uint64(v)
3230		if n1 < n || n1 > maxVal {
3231			// n+v overflows
3232			n = 1<<64 - 1
3233			err = strconv.ErrRange
3234			goto Error
3235		}
3236		n = n1
3237	}
3238
3239	return n, nil
3240
3241Error:
3242	return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err}
3243}
3244
3245// Return the first number n such that n*base >= 1<<64.
3246func http2cutoff64(base int) uint64 {
3247	if base < 2 {
3248		return 0
3249	}
3250	return (1<<64-1)/uint64(base) + 1
3251}
3252
3253var (
3254	http2commonBuildOnce   sync.Once
3255	http2commonLowerHeader map[string]string // Go-Canonical-Case -> lower-case
3256	http2commonCanonHeader map[string]string // lower-case -> Go-Canonical-Case
3257)
3258
3259func http2buildCommonHeaderMapsOnce() {
3260	http2commonBuildOnce.Do(http2buildCommonHeaderMaps)
3261}
3262
3263func http2buildCommonHeaderMaps() {
3264	common := []string{
3265		"accept",
3266		"accept-charset",
3267		"accept-encoding",
3268		"accept-language",
3269		"accept-ranges",
3270		"age",
3271		"access-control-allow-credentials",
3272		"access-control-allow-headers",
3273		"access-control-allow-methods",
3274		"access-control-allow-origin",
3275		"access-control-expose-headers",
3276		"access-control-max-age",
3277		"access-control-request-headers",
3278		"access-control-request-method",
3279		"allow",
3280		"authorization",
3281		"cache-control",
3282		"content-disposition",
3283		"content-encoding",
3284		"content-language",
3285		"content-length",
3286		"content-location",
3287		"content-range",
3288		"content-type",
3289		"cookie",
3290		"date",
3291		"etag",
3292		"expect",
3293		"expires",
3294		"from",
3295		"host",
3296		"if-match",
3297		"if-modified-since",
3298		"if-none-match",
3299		"if-unmodified-since",
3300		"last-modified",
3301		"link",
3302		"location",
3303		"max-forwards",
3304		"origin",
3305		"proxy-authenticate",
3306		"proxy-authorization",
3307		"range",
3308		"referer",
3309		"refresh",
3310		"retry-after",
3311		"server",
3312		"set-cookie",
3313		"strict-transport-security",
3314		"trailer",
3315		"transfer-encoding",
3316		"user-agent",
3317		"vary",
3318		"via",
3319		"www-authenticate",
3320		"x-forwarded-for",
3321		"x-forwarded-proto",
3322	}
3323	http2commonLowerHeader = make(map[string]string, len(common))
3324	http2commonCanonHeader = make(map[string]string, len(common))
3325	for _, v := range common {
3326		chk := CanonicalHeaderKey(v)
3327		http2commonLowerHeader[chk] = v
3328		http2commonCanonHeader[v] = chk
3329	}
3330}
3331
3332func http2lowerHeader(v string) (lower string, ascii bool) {
3333	http2buildCommonHeaderMapsOnce()
3334	if s, ok := http2commonLowerHeader[v]; ok {
3335		return s, true
3336	}
3337	return http2asciiToLower(v)
3338}
3339
3340func http2canonicalHeader(v string) string {
3341	http2buildCommonHeaderMapsOnce()
3342	if s, ok := http2commonCanonHeader[v]; ok {
3343		return s
3344	}
3345	return CanonicalHeaderKey(v)
3346}
3347
3348var (
3349	http2VerboseLogs    bool
3350	http2logFrameWrites bool
3351	http2logFrameReads  bool
3352	http2inTests        bool
3353)
3354
3355func init() {
3356	e := os.Getenv("GODEBUG")
3357	if strings.Contains(e, "http2debug=1") {
3358		http2VerboseLogs = true
3359	}
3360	if strings.Contains(e, "http2debug=2") {
3361		http2VerboseLogs = true
3362		http2logFrameWrites = true
3363		http2logFrameReads = true
3364	}
3365}
3366
3367const (
3368	// ClientPreface is the string that must be sent by new
3369	// connections from clients.
3370	http2ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
3371
3372	// SETTINGS_MAX_FRAME_SIZE default
3373	// https://httpwg.org/specs/rfc7540.html#rfc.section.6.5.2
3374	http2initialMaxFrameSize = 16384
3375
3376	// NextProtoTLS is the NPN/ALPN protocol negotiated during
3377	// HTTP/2's TLS setup.
3378	http2NextProtoTLS = "h2"
3379
3380	// https://httpwg.org/specs/rfc7540.html#SettingValues
3381	http2initialHeaderTableSize = 4096
3382
3383	http2initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
3384
3385	http2defaultMaxReadFrameSize = 1 << 20
3386)
3387
3388var (
3389	http2clientPreface = []byte(http2ClientPreface)
3390)
3391
3392type http2streamState int
3393
3394// HTTP/2 stream states.
3395//
3396// See http://tools.ietf.org/html/rfc7540#section-5.1.
3397//
3398// For simplicity, the server code merges "reserved (local)" into
3399// "half-closed (remote)". This is one less state transition to track.
3400// The only downside is that we send PUSH_PROMISEs slightly less
3401// liberally than allowable. More discussion here:
3402// https://lists.w3.org/Archives/Public/ietf-http-wg/2016JulSep/0599.html
3403//
3404// "reserved (remote)" is omitted since the client code does not
3405// support server push.
3406const (
3407	http2stateIdle http2streamState = iota
3408	http2stateOpen
3409	http2stateHalfClosedLocal
3410	http2stateHalfClosedRemote
3411	http2stateClosed
3412)
3413
3414var http2stateName = [...]string{
3415	http2stateIdle:             "Idle",
3416	http2stateOpen:             "Open",
3417	http2stateHalfClosedLocal:  "HalfClosedLocal",
3418	http2stateHalfClosedRemote: "HalfClosedRemote",
3419	http2stateClosed:           "Closed",
3420}
3421
3422func (st http2streamState) String() string {
3423	return http2stateName[st]
3424}
3425
3426// Setting is a setting parameter: which setting it is, and its value.
3427type http2Setting struct {
3428	// ID is which setting is being set.
3429	// See https://httpwg.org/specs/rfc7540.html#SettingFormat
3430	ID http2SettingID
3431
3432	// Val is the value.
3433	Val uint32
3434}
3435
3436func (s http2Setting) String() string {
3437	return fmt.Sprintf("[%v = %d]", s.ID, s.Val)
3438}
3439
3440// Valid reports whether the setting is valid.
3441func (s http2Setting) Valid() error {
3442	// Limits and error codes from 6.5.2 Defined SETTINGS Parameters
3443	switch s.ID {
3444	case http2SettingEnablePush:
3445		if s.Val != 1 && s.Val != 0 {
3446			return http2ConnectionError(http2ErrCodeProtocol)
3447		}
3448	case http2SettingInitialWindowSize:
3449		if s.Val > 1<<31-1 {
3450			return http2ConnectionError(http2ErrCodeFlowControl)
3451		}
3452	case http2SettingMaxFrameSize:
3453		if s.Val < 16384 || s.Val > 1<<24-1 {
3454			return http2ConnectionError(http2ErrCodeProtocol)
3455		}
3456	}
3457	return nil
3458}
3459
3460// A SettingID is an HTTP/2 setting as defined in
3461// https://httpwg.org/specs/rfc7540.html#iana-settings
3462type http2SettingID uint16
3463
3464const (
3465	http2SettingHeaderTableSize      http2SettingID = 0x1
3466	http2SettingEnablePush           http2SettingID = 0x2
3467	http2SettingMaxConcurrentStreams http2SettingID = 0x3
3468	http2SettingInitialWindowSize    http2SettingID = 0x4
3469	http2SettingMaxFrameSize         http2SettingID = 0x5
3470	http2SettingMaxHeaderListSize    http2SettingID = 0x6
3471)
3472
3473var http2settingName = map[http2SettingID]string{
3474	http2SettingHeaderTableSize:      "HEADER_TABLE_SIZE",
3475	http2SettingEnablePush:           "ENABLE_PUSH",
3476	http2SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS",
3477	http2SettingInitialWindowSize:    "INITIAL_WINDOW_SIZE",
3478	http2SettingMaxFrameSize:         "MAX_FRAME_SIZE",
3479	http2SettingMaxHeaderListSize:    "MAX_HEADER_LIST_SIZE",
3480}
3481
3482func (s http2SettingID) String() string {
3483	if v, ok := http2settingName[s]; ok {
3484		return v
3485	}
3486	return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s))
3487}
3488
3489// validWireHeaderFieldName reports whether v is a valid header field
3490// name (key). See httpguts.ValidHeaderName for the base rules.
3491//
3492// Further, http2 says:
3493//
3494//	"Just as in HTTP/1.x, header field names are strings of ASCII
3495//	characters that are compared in a case-insensitive
3496//	fashion. However, header field names MUST be converted to
3497//	lowercase prior to their encoding in HTTP/2. "
3498func http2validWireHeaderFieldName(v string) bool {
3499	if len(v) == 0 {
3500		return false
3501	}
3502	for _, r := range v {
3503		if !httpguts.IsTokenRune(r) {
3504			return false
3505		}
3506		if 'A' <= r && r <= 'Z' {
3507			return false
3508		}
3509	}
3510	return true
3511}
3512
3513func http2httpCodeString(code int) string {
3514	switch code {
3515	case 200:
3516		return "200"
3517	case 404:
3518		return "404"
3519	}
3520	return strconv.Itoa(code)
3521}
3522
3523// from pkg io
3524type http2stringWriter interface {
3525	WriteString(s string) (n int, err error)
3526}
3527
3528// A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
3529type http2closeWaiter chan struct{}
3530
3531// Init makes a closeWaiter usable.
3532// It exists because so a closeWaiter value can be placed inside a
3533// larger struct and have the Mutex and Cond's memory in the same
3534// allocation.
3535func (cw *http2closeWaiter) Init() {
3536	*cw = make(chan struct{})
3537}
3538
3539// Close marks the closeWaiter as closed and unblocks any waiters.
3540func (cw http2closeWaiter) Close() {
3541	close(cw)
3542}
3543
3544// Wait waits for the closeWaiter to become closed.
3545func (cw http2closeWaiter) Wait() {
3546	<-cw
3547}
3548
3549// bufferedWriter is a buffered writer that writes to w.
3550// Its buffered writer is lazily allocated as needed, to minimize
3551// idle memory usage with many connections.
3552type http2bufferedWriter struct {
3553	_  http2incomparable
3554	w  io.Writer     // immutable
3555	bw *bufio.Writer // non-nil when data is buffered
3556}
3557
3558func http2newBufferedWriter(w io.Writer) *http2bufferedWriter {
3559	return &http2bufferedWriter{w: w}
3560}
3561
3562// bufWriterPoolBufferSize is the size of bufio.Writer's
3563// buffers created using bufWriterPool.
3564//
3565// TODO: pick a less arbitrary value? this is a bit under
3566// (3 x typical 1500 byte MTU) at least. Other than that,
3567// not much thought went into it.
3568const http2bufWriterPoolBufferSize = 4 << 10
3569
3570var http2bufWriterPool = sync.Pool{
3571	New: func() interface{} {
3572		return bufio.NewWriterSize(nil, http2bufWriterPoolBufferSize)
3573	},
3574}
3575
3576func (w *http2bufferedWriter) Available() int {
3577	if w.bw == nil {
3578		return http2bufWriterPoolBufferSize
3579	}
3580	return w.bw.Available()
3581}
3582
3583func (w *http2bufferedWriter) Write(p []byte) (n int, err error) {
3584	if w.bw == nil {
3585		bw := http2bufWriterPool.Get().(*bufio.Writer)
3586		bw.Reset(w.w)
3587		w.bw = bw
3588	}
3589	return w.bw.Write(p)
3590}
3591
3592func (w *http2bufferedWriter) Flush() error {
3593	bw := w.bw
3594	if bw == nil {
3595		return nil
3596	}
3597	err := bw.Flush()
3598	bw.Reset(nil)
3599	http2bufWriterPool.Put(bw)
3600	w.bw = nil
3601	return err
3602}
3603
3604func http2mustUint31(v int32) uint32 {
3605	if v < 0 || v > 2147483647 {
3606		panic("out of range")
3607	}
3608	return uint32(v)
3609}
3610
3611// bodyAllowedForStatus reports whether a given response status code
3612// permits a body. See RFC 7230, section 3.3.
3613func http2bodyAllowedForStatus(status int) bool {
3614	switch {
3615	case status >= 100 && status <= 199:
3616		return false
3617	case status == 204:
3618		return false
3619	case status == 304:
3620		return false
3621	}
3622	return true
3623}
3624
3625type http2httpError struct {
3626	_       http2incomparable
3627	msg     string
3628	timeout bool
3629}
3630
3631func (e *http2httpError) Error() string { return e.msg }
3632
3633func (e *http2httpError) Timeout() bool { return e.timeout }
3634
3635func (e *http2httpError) Temporary() bool { return true }
3636
3637var http2errTimeout error = &http2httpError{msg: "http2: timeout awaiting response headers", timeout: true}
3638
3639type http2connectionStater interface {
3640	ConnectionState() tls.ConnectionState
3641}
3642
3643var http2sorterPool = sync.Pool{New: func() interface{} { return new(http2sorter) }}
3644
3645type http2sorter struct {
3646	v []string // owned by sorter
3647}
3648
3649func (s *http2sorter) Len() int { return len(s.v) }
3650
3651func (s *http2sorter) Swap(i, j int) { s.v[i], s.v[j] = s.v[j], s.v[i] }
3652
3653func (s *http2sorter) Less(i, j int) bool { return s.v[i] < s.v[j] }
3654
3655// Keys returns the sorted keys of h.
3656//
3657// The returned slice is only valid until s used again or returned to
3658// its pool.
3659func (s *http2sorter) Keys(h Header) []string {
3660	keys := s.v[:0]
3661	for k := range h {
3662		keys = append(keys, k)
3663	}
3664	s.v = keys
3665	sort.Sort(s)
3666	return keys
3667}
3668
3669func (s *http2sorter) SortStrings(ss []string) {
3670	// Our sorter works on s.v, which sorter owns, so
3671	// stash it away while we sort the user's buffer.
3672	save := s.v
3673	s.v = ss
3674	sort.Sort(s)
3675	s.v = save
3676}
3677
3678// validPseudoPath reports whether v is a valid :path pseudo-header
3679// value. It must be either:
3680//
3681//   - a non-empty string starting with '/'
3682//   - the string '*', for OPTIONS requests.
3683//
3684// For now this is only used a quick check for deciding when to clean
3685// up Opaque URLs before sending requests from the Transport.
3686// See golang.org/issue/16847
3687//
3688// We used to enforce that the path also didn't start with "//", but
3689// Google's GFE accepts such paths and Chrome sends them, so ignore
3690// that part of the spec. See golang.org/issue/19103.
3691func http2validPseudoPath(v string) bool {
3692	return (len(v) > 0 && v[0] == '/') || v == "*"
3693}
3694
3695// incomparable is a zero-width, non-comparable type. Adding it to a struct
3696// makes that struct also non-comparable, and generally doesn't add
3697// any size (as long as it's first).
3698type http2incomparable [0]func()
3699
3700// synctestGroupInterface is the methods of synctestGroup used by Server and Transport.
3701// It's defined as an interface here to let us keep synctestGroup entirely test-only
3702// and not a part of non-test builds.
3703type http2synctestGroupInterface interface {
3704	Join()
3705	Now() time.Time
3706	NewTimer(d time.Duration) http2timer
3707	AfterFunc(d time.Duration, f func()) http2timer
3708	ContextWithTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)
3709}
3710
3711// pipe is a goroutine-safe io.Reader/io.Writer pair. It's like
3712// io.Pipe except there are no PipeReader/PipeWriter halves, and the
3713// underlying buffer is an interface. (io.Pipe is always unbuffered)
3714type http2pipe struct {
3715	mu       sync.Mutex
3716	c        sync.Cond       // c.L lazily initialized to &p.mu
3717	b        http2pipeBuffer // nil when done reading
3718	unread   int             // bytes unread when done
3719	err      error           // read error once empty. non-nil means closed.
3720	breakErr error           // immediate read error (caller doesn't see rest of b)
3721	donec    chan struct{}   // closed on error
3722	readFn   func()          // optional code to run in Read before error
3723}
3724
3725type http2pipeBuffer interface {
3726	Len() int
3727	io.Writer
3728	io.Reader
3729}
3730
3731// setBuffer initializes the pipe buffer.
3732// It has no effect if the pipe is already closed.
3733func (p *http2pipe) setBuffer(b http2pipeBuffer) {
3734	p.mu.Lock()
3735	defer p.mu.Unlock()
3736	if p.err != nil || p.breakErr != nil {
3737		return
3738	}
3739	p.b = b
3740}
3741
3742func (p *http2pipe) Len() int {
3743	p.mu.Lock()
3744	defer p.mu.Unlock()
3745	if p.b == nil {
3746		return p.unread
3747	}
3748	return p.b.Len()
3749}
3750
3751// Read waits until data is available and copies bytes
3752// from the buffer into p.
3753func (p *http2pipe) Read(d []byte) (n int, err error) {
3754	p.mu.Lock()
3755	defer p.mu.Unlock()
3756	if p.c.L == nil {
3757		p.c.L = &p.mu
3758	}
3759	for {
3760		if p.breakErr != nil {
3761			return 0, p.breakErr
3762		}
3763		if p.b != nil && p.b.Len() > 0 {
3764			return p.b.Read(d)
3765		}
3766		if p.err != nil {
3767			if p.readFn != nil {
3768				p.readFn()     // e.g. copy trailers
3769				p.readFn = nil // not sticky like p.err
3770			}
3771			p.b = nil
3772			return 0, p.err
3773		}
3774		p.c.Wait()
3775	}
3776}
3777
3778var (
3779	http2errClosedPipeWrite        = errors.New("write on closed buffer")
3780	http2errUninitializedPipeWrite = errors.New("write on uninitialized buffer")
3781)
3782
3783// Write copies bytes from p into the buffer and wakes a reader.
3784// It is an error to write more data than the buffer can hold.
3785func (p *http2pipe) Write(d []byte) (n int, err error) {
3786	p.mu.Lock()
3787	defer p.mu.Unlock()
3788	if p.c.L == nil {
3789		p.c.L = &p.mu
3790	}
3791	defer p.c.Signal()
3792	if p.err != nil || p.breakErr != nil {
3793		return 0, http2errClosedPipeWrite
3794	}
3795	// pipe.setBuffer is never invoked, leaving the buffer uninitialized.
3796	// We shouldn't try to write to an uninitialized pipe,
3797	// but returning an error is better than panicking.
3798	if p.b == nil {
3799		return 0, http2errUninitializedPipeWrite
3800	}
3801	return p.b.Write(d)
3802}
3803
3804// CloseWithError causes the next Read (waking up a current blocked
3805// Read if needed) to return the provided err after all data has been
3806// read.
3807//
3808// The error must be non-nil.
3809func (p *http2pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) }
3810
3811// BreakWithError causes the next Read (waking up a current blocked
3812// Read if needed) to return the provided err immediately, without
3813// waiting for unread data.
3814func (p *http2pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) }
3815
3816// closeWithErrorAndCode is like CloseWithError but also sets some code to run
3817// in the caller's goroutine before returning the error.
3818func (p *http2pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) }
3819
3820func (p *http2pipe) closeWithError(dst *error, err error, fn func()) {
3821	if err == nil {
3822		panic("err must be non-nil")
3823	}
3824	p.mu.Lock()
3825	defer p.mu.Unlock()
3826	if p.c.L == nil {
3827		p.c.L = &p.mu
3828	}
3829	defer p.c.Signal()
3830	if *dst != nil {
3831		// Already been done.
3832		return
3833	}
3834	p.readFn = fn
3835	if dst == &p.breakErr {
3836		if p.b != nil {
3837			p.unread += p.b.Len()
3838		}
3839		p.b = nil
3840	}
3841	*dst = err
3842	p.closeDoneLocked()
3843}
3844
3845// requires p.mu be held.
3846func (p *http2pipe) closeDoneLocked() {
3847	if p.donec == nil {
3848		return
3849	}
3850	// Close if unclosed. This isn't racy since we always
3851	// hold p.mu while closing.
3852	select {
3853	case <-p.donec:
3854	default:
3855		close(p.donec)
3856	}
3857}
3858
3859// Err returns the error (if any) first set by BreakWithError or CloseWithError.
3860func (p *http2pipe) Err() error {
3861	p.mu.Lock()
3862	defer p.mu.Unlock()
3863	if p.breakErr != nil {
3864		return p.breakErr
3865	}
3866	return p.err
3867}
3868
3869// Done returns a channel which is closed if and when this pipe is closed
3870// with CloseWithError.
3871func (p *http2pipe) Done() <-chan struct{} {
3872	p.mu.Lock()
3873	defer p.mu.Unlock()
3874	if p.donec == nil {
3875		p.donec = make(chan struct{})
3876		if p.err != nil || p.breakErr != nil {
3877			// Already hit an error.
3878			p.closeDoneLocked()
3879		}
3880	}
3881	return p.donec
3882}
3883
3884const (
3885	http2prefaceTimeout         = 10 * time.Second
3886	http2firstSettingsTimeout   = 2 * time.Second // should be in-flight with preface anyway
3887	http2handlerChunkWriteSize  = 4 << 10
3888	http2defaultMaxStreams      = 250 // TODO: make this 100 as the GFE seems to?
3889	http2maxQueuedControlFrames = 10000
3890)
3891
3892var (
3893	http2errClientDisconnected = errors.New("client disconnected")
3894	http2errClosedBody         = errors.New("body closed by handler")
3895	http2errHandlerComplete    = errors.New("http2: request body closed due to handler exiting")
3896	http2errStreamClosed       = errors.New("http2: stream closed")
3897)
3898
3899var http2responseWriterStatePool = sync.Pool{
3900	New: func() interface{} {
3901		rws := &http2responseWriterState{}
3902		rws.bw = bufio.NewWriterSize(http2chunkWriter{rws}, http2handlerChunkWriteSize)
3903		return rws
3904	},
3905}
3906
3907// Test hooks.
3908var (
3909	http2testHookOnConn        func()
3910	http2testHookGetServerConn func(*http2serverConn)
3911	http2testHookOnPanicMu     *sync.Mutex // nil except in tests
3912	http2testHookOnPanic       func(sc *http2serverConn, panicVal interface{}) (rePanic bool)
3913)
3914
3915// Server is an HTTP/2 server.
3916type http2Server struct {
3917	// MaxHandlers limits the number of http.Handler ServeHTTP goroutines
3918	// which may run at a time over all connections.
3919	// Negative or zero no limit.
3920	// TODO: implement
3921	MaxHandlers int
3922
3923	// MaxConcurrentStreams optionally specifies the number of
3924	// concurrent streams that each client may have open at a
3925	// time. This is unrelated to the number of http.Handler goroutines
3926	// which may be active globally, which is MaxHandlers.
3927	// If zero, MaxConcurrentStreams defaults to at least 100, per
3928	// the HTTP/2 spec's recommendations.
3929	MaxConcurrentStreams uint32
3930
3931	// MaxDecoderHeaderTableSize optionally specifies the http2
3932	// SETTINGS_HEADER_TABLE_SIZE to send in the initial settings frame. It
3933	// informs the remote endpoint of the maximum size of the header compression
3934	// table used to decode header blocks, in octets. If zero, the default value
3935	// of 4096 is used.
3936	MaxDecoderHeaderTableSize uint32
3937
3938	// MaxEncoderHeaderTableSize optionally specifies an upper limit for the
3939	// header compression table used for encoding request headers. Received
3940	// SETTINGS_HEADER_TABLE_SIZE settings are capped at this limit. If zero,
3941	// the default value of 4096 is used.
3942	MaxEncoderHeaderTableSize uint32
3943
3944	// MaxReadFrameSize optionally specifies the largest frame
3945	// this server is willing to read. A valid value is between
3946	// 16k and 16M, inclusive. If zero or otherwise invalid, a
3947	// default value is used.
3948	MaxReadFrameSize uint32
3949
3950	// PermitProhibitedCipherSuites, if true, permits the use of
3951	// cipher suites prohibited by the HTTP/2 spec.
3952	PermitProhibitedCipherSuites bool
3953
3954	// IdleTimeout specifies how long until idle clients should be
3955	// closed with a GOAWAY frame. PING frames are not considered
3956	// activity for the purposes of IdleTimeout.
3957	// If zero or negative, there is no timeout.
3958	IdleTimeout time.Duration
3959
3960	// MaxUploadBufferPerConnection is the size of the initial flow
3961	// control window for each connections. The HTTP/2 spec does not
3962	// allow this to be smaller than 65535 or larger than 2^32-1.
3963	// If the value is outside this range, a default value will be
3964	// used instead.
3965	MaxUploadBufferPerConnection int32
3966
3967	// MaxUploadBufferPerStream is the size of the initial flow control
3968	// window for each stream. The HTTP/2 spec does not allow this to
3969	// be larger than 2^32-1. If the value is zero or larger than the
3970	// maximum, a default value will be used instead.
3971	MaxUploadBufferPerStream int32
3972
3973	// NewWriteScheduler constructs a write scheduler for a connection.
3974	// If nil, a default scheduler is chosen.
3975	NewWriteScheduler func() http2WriteScheduler
3976
3977	// CountError, if non-nil, is called on HTTP/2 server errors.
3978	// It's intended to increment a metric for monitoring, such
3979	// as an expvar or Prometheus metric.
3980	// The errType consists of only ASCII word characters.
3981	CountError func(errType string)
3982
3983	// Internal state. This is a pointer (rather than embedded directly)
3984	// so that we don't embed a Mutex in this struct, which will make the
3985	// struct non-copyable, which might break some callers.
3986	state *http2serverInternalState
3987
3988	// Synchronization group used for testing.
3989	// Outside of tests, this is nil.
3990	group http2synctestGroupInterface
3991}
3992
3993func (s *http2Server) markNewGoroutine() {
3994	if s.group != nil {
3995		s.group.Join()
3996	}
3997}
3998
3999func (s *http2Server) now() time.Time {
4000	if s.group != nil {
4001		return s.group.Now()
4002	}
4003	return time.Now()
4004}
4005
4006// newTimer creates a new time.Timer, or a synthetic timer in tests.
4007func (s *http2Server) newTimer(d time.Duration) http2timer {
4008	if s.group != nil {
4009		return s.group.NewTimer(d)
4010	}
4011	return http2timeTimer{time.NewTimer(d)}
4012}
4013
4014// afterFunc creates a new time.AfterFunc timer, or a synthetic timer in tests.
4015func (s *http2Server) afterFunc(d time.Duration, f func()) http2timer {
4016	if s.group != nil {
4017		return s.group.AfterFunc(d, f)
4018	}
4019	return http2timeTimer{time.AfterFunc(d, f)}
4020}
4021
4022func (s *http2Server) initialConnRecvWindowSize() int32 {
4023	if s.MaxUploadBufferPerConnection >= http2initialWindowSize {
4024		return s.MaxUploadBufferPerConnection
4025	}
4026	return 1 << 20
4027}
4028
4029func (s *http2Server) initialStreamRecvWindowSize() int32 {
4030	if s.MaxUploadBufferPerStream > 0 {
4031		return s.MaxUploadBufferPerStream
4032	}
4033	return 1 << 20
4034}
4035
4036func (s *http2Server) maxReadFrameSize() uint32 {
4037	if v := s.MaxReadFrameSize; v >= http2minMaxFrameSize && v <= http2maxFrameSize {
4038		return v
4039	}
4040	return http2defaultMaxReadFrameSize
4041}
4042
4043func (s *http2Server) maxConcurrentStreams() uint32 {
4044	if v := s.MaxConcurrentStreams; v > 0 {
4045		return v
4046	}
4047	return http2defaultMaxStreams
4048}
4049
4050func (s *http2Server) maxDecoderHeaderTableSize() uint32 {
4051	if v := s.MaxDecoderHeaderTableSize; v > 0 {
4052		return v
4053	}
4054	return http2initialHeaderTableSize
4055}
4056
4057func (s *http2Server) maxEncoderHeaderTableSize() uint32 {
4058	if v := s.MaxEncoderHeaderTableSize; v > 0 {
4059		return v
4060	}
4061	return http2initialHeaderTableSize
4062}
4063
4064// maxQueuedControlFrames is the maximum number of control frames like
4065// SETTINGS, PING and RST_STREAM that will be queued for writing before
4066// the connection is closed to prevent memory exhaustion attacks.
4067func (s *http2Server) maxQueuedControlFrames() int {
4068	// TODO: if anybody asks, add a Server field, and remember to define the
4069	// behavior of negative values.
4070	return http2maxQueuedControlFrames
4071}
4072
4073type http2serverInternalState struct {
4074	mu          sync.Mutex
4075	activeConns map[*http2serverConn]struct{}
4076}
4077
4078func (s *http2serverInternalState) registerConn(sc *http2serverConn) {
4079	if s == nil {
4080		return // if the Server was used without calling ConfigureServer
4081	}
4082	s.mu.Lock()
4083	s.activeConns[sc] = struct{}{}
4084	s.mu.Unlock()
4085}
4086
4087func (s *http2serverInternalState) unregisterConn(sc *http2serverConn) {
4088	if s == nil {
4089		return // if the Server was used without calling ConfigureServer
4090	}
4091	s.mu.Lock()
4092	delete(s.activeConns, sc)
4093	s.mu.Unlock()
4094}
4095
4096func (s *http2serverInternalState) startGracefulShutdown() {
4097	if s == nil {
4098		return // if the Server was used without calling ConfigureServer
4099	}
4100	s.mu.Lock()
4101	for sc := range s.activeConns {
4102		sc.startGracefulShutdown()
4103	}
4104	s.mu.Unlock()
4105}
4106
4107// ConfigureServer adds HTTP/2 support to a net/http Server.
4108//
4109// The configuration conf may be nil.
4110//
4111// ConfigureServer must be called before s begins serving.
4112func http2ConfigureServer(s *Server, conf *http2Server) error {
4113	if s == nil {
4114		panic("nil *http.Server")
4115	}
4116	if conf == nil {
4117		conf = new(http2Server)
4118	}
4119	conf.state = &http2serverInternalState{activeConns: make(map[*http2serverConn]struct{})}
4120	if h1, h2 := s, conf; h2.IdleTimeout == 0 {
4121		if h1.IdleTimeout != 0 {
4122			h2.IdleTimeout = h1.IdleTimeout
4123		} else {
4124			h2.IdleTimeout = h1.ReadTimeout
4125		}
4126	}
4127	s.RegisterOnShutdown(conf.state.startGracefulShutdown)
4128
4129	if s.TLSConfig == nil {
4130		s.TLSConfig = new(tls.Config)
4131	} else if s.TLSConfig.CipherSuites != nil && s.TLSConfig.MinVersion < tls.VersionTLS13 {
4132		// If they already provided a TLS 1.0–1.2 CipherSuite list, return an
4133		// error if it is missing ECDHE_RSA_WITH_AES_128_GCM_SHA256 or
4134		// ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.
4135		haveRequired := false
4136		for _, cs := range s.TLSConfig.CipherSuites {
4137			switch cs {
4138			case tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
4139				// Alternative MTI cipher to not discourage ECDSA-only servers.
4140				// See http://golang.org/cl/30721 for further information.
4141				tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
4142				haveRequired = true
4143			}
4144		}
4145		if !haveRequired {
4146			return fmt.Errorf("http2: TLSConfig.CipherSuites is missing an HTTP/2-required AES_128_GCM_SHA256 cipher (need at least one of TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 or TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)")
4147		}
4148	}
4149
4150	// Note: not setting MinVersion to tls.VersionTLS12,
4151	// as we don't want to interfere with HTTP/1.1 traffic
4152	// on the user's server. We enforce TLS 1.2 later once
4153	// we accept a connection. Ideally this should be done
4154	// during next-proto selection, but using TLS <1.2 with
4155	// HTTP/2 is still the client's bug.
4156
4157	s.TLSConfig.PreferServerCipherSuites = true
4158
4159	if !http2strSliceContains(s.TLSConfig.NextProtos, http2NextProtoTLS) {
4160		s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, http2NextProtoTLS)
4161	}
4162	if !http2strSliceContains(s.TLSConfig.NextProtos, "http/1.1") {
4163		s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "http/1.1")
4164	}
4165
4166	if s.TLSNextProto == nil {
4167		s.TLSNextProto = map[string]func(*Server, *tls.Conn, Handler){}
4168	}
4169	protoHandler := func(hs *Server, c *tls.Conn, h Handler) {
4170		if http2testHookOnConn != nil {
4171			http2testHookOnConn()
4172		}
4173		// The TLSNextProto interface predates contexts, so
4174		// the net/http package passes down its per-connection
4175		// base context via an exported but unadvertised
4176		// method on the Handler. This is for internal
4177		// net/http<=>http2 use only.
4178		var ctx context.Context
4179		type baseContexter interface {
4180			BaseContext() context.Context
4181		}
4182		if bc, ok := h.(baseContexter); ok {
4183			ctx = bc.BaseContext()
4184		}
4185		conf.ServeConn(c, &http2ServeConnOpts{
4186			Context:    ctx,
4187			Handler:    h,
4188			BaseConfig: hs,
4189		})
4190	}
4191	s.TLSNextProto[http2NextProtoTLS] = protoHandler
4192	return nil
4193}
4194
4195// ServeConnOpts are options for the Server.ServeConn method.
4196type http2ServeConnOpts struct {
4197	// Context is the base context to use.
4198	// If nil, context.Background is used.
4199	Context context.Context
4200
4201	// BaseConfig optionally sets the base configuration
4202	// for values. If nil, defaults are used.
4203	BaseConfig *Server
4204
4205	// Handler specifies which handler to use for processing
4206	// requests. If nil, BaseConfig.Handler is used. If BaseConfig
4207	// or BaseConfig.Handler is nil, http.DefaultServeMux is used.
4208	Handler Handler
4209
4210	// UpgradeRequest is an initial request received on a connection
4211	// undergoing an h2c upgrade. The request body must have been
4212	// completely read from the connection before calling ServeConn,
4213	// and the 101 Switching Protocols response written.
4214	UpgradeRequest *Request
4215
4216	// Settings is the decoded contents of the HTTP2-Settings header
4217	// in an h2c upgrade request.
4218	Settings []byte
4219
4220	// SawClientPreface is set if the HTTP/2 connection preface
4221	// has already been read from the connection.
4222	SawClientPreface bool
4223}
4224
4225func (o *http2ServeConnOpts) context() context.Context {
4226	if o != nil && o.Context != nil {
4227		return o.Context
4228	}
4229	return context.Background()
4230}
4231
4232func (o *http2ServeConnOpts) baseConfig() *Server {
4233	if o != nil && o.BaseConfig != nil {
4234		return o.BaseConfig
4235	}
4236	return new(Server)
4237}
4238
4239func (o *http2ServeConnOpts) handler() Handler {
4240	if o != nil {
4241		if o.Handler != nil {
4242			return o.Handler
4243		}
4244		if o.BaseConfig != nil && o.BaseConfig.Handler != nil {
4245			return o.BaseConfig.Handler
4246		}
4247	}
4248	return DefaultServeMux
4249}
4250
4251// ServeConn serves HTTP/2 requests on the provided connection and
4252// blocks until the connection is no longer readable.
4253//
4254// ServeConn starts speaking HTTP/2 assuming that c has not had any
4255// reads or writes. It writes its initial settings frame and expects
4256// to be able to read the preface and settings frame from the
4257// client. If c has a ConnectionState method like a *tls.Conn, the
4258// ConnectionState is used to verify the TLS ciphersuite and to set
4259// the Request.TLS field in Handlers.
4260//
4261// ServeConn does not support h2c by itself. Any h2c support must be
4262// implemented in terms of providing a suitably-behaving net.Conn.
4263//
4264// The opts parameter is optional. If nil, default values are used.
4265func (s *http2Server) ServeConn(c net.Conn, opts *http2ServeConnOpts) {
4266	s.serveConn(c, opts, nil)
4267}
4268
4269func (s *http2Server) serveConn(c net.Conn, opts *http2ServeConnOpts, newf func(*http2serverConn)) {
4270	baseCtx, cancel := http2serverConnBaseContext(c, opts)
4271	defer cancel()
4272
4273	sc := &http2serverConn{
4274		srv:                         s,
4275		hs:                          opts.baseConfig(),
4276		conn:                        c,
4277		baseCtx:                     baseCtx,
4278		remoteAddrStr:               c.RemoteAddr().String(),
4279		bw:                          http2newBufferedWriter(c),
4280		handler:                     opts.handler(),
4281		streams:                     make(map[uint32]*http2stream),
4282		readFrameCh:                 make(chan http2readFrameResult),
4283		wantWriteFrameCh:            make(chan http2FrameWriteRequest, 8),
4284		serveMsgCh:                  make(chan interface{}, 8),
4285		wroteFrameCh:                make(chan http2frameWriteResult, 1), // buffered; one send in writeFrameAsync
4286		bodyReadCh:                  make(chan http2bodyReadMsg),         // buffering doesn't matter either way
4287		doneServing:                 make(chan struct{}),
4288		clientMaxStreams:            math.MaxUint32, // Section 6.5.2: "Initially, there is no limit to this value"
4289		advMaxStreams:               s.maxConcurrentStreams(),
4290		initialStreamSendWindowSize: http2initialWindowSize,
4291		maxFrameSize:                http2initialMaxFrameSize,
4292		serveG:                      http2newGoroutineLock(),
4293		pushEnabled:                 true,
4294		sawClientPreface:            opts.SawClientPreface,
4295	}
4296	if newf != nil {
4297		newf(sc)
4298	}
4299
4300	s.state.registerConn(sc)
4301	defer s.state.unregisterConn(sc)
4302
4303	// The net/http package sets the write deadline from the
4304	// http.Server.WriteTimeout during the TLS handshake, but then
4305	// passes the connection off to us with the deadline already set.
4306	// Write deadlines are set per stream in serverConn.newStream.
4307	// Disarm the net.Conn write deadline here.
4308	if sc.hs.WriteTimeout > 0 {
4309		sc.conn.SetWriteDeadline(time.Time{})
4310	}
4311
4312	if s.NewWriteScheduler != nil {
4313		sc.writeSched = s.NewWriteScheduler()
4314	} else {
4315		sc.writeSched = http2newRoundRobinWriteScheduler()
4316	}
4317
4318	// These start at the RFC-specified defaults. If there is a higher
4319	// configured value for inflow, that will be updated when we send a
4320	// WINDOW_UPDATE shortly after sending SETTINGS.
4321	sc.flow.add(http2initialWindowSize)
4322	sc.inflow.init(http2initialWindowSize)
4323	sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
4324	sc.hpackEncoder.SetMaxDynamicTableSizeLimit(s.maxEncoderHeaderTableSize())
4325
4326	fr := http2NewFramer(sc.bw, c)
4327	if s.CountError != nil {
4328		fr.countError = s.CountError
4329	}
4330	fr.ReadMetaHeaders = hpack.NewDecoder(s.maxDecoderHeaderTableSize(), nil)
4331	fr.MaxHeaderListSize = sc.maxHeaderListSize()
4332	fr.SetMaxReadFrameSize(s.maxReadFrameSize())
4333	sc.framer = fr
4334
4335	if tc, ok := c.(http2connectionStater); ok {
4336		sc.tlsState = new(tls.ConnectionState)
4337		*sc.tlsState = tc.ConnectionState()
4338		// 9.2 Use of TLS Features
4339		// An implementation of HTTP/2 over TLS MUST use TLS
4340		// 1.2 or higher with the restrictions on feature set
4341		// and cipher suite described in this section. Due to
4342		// implementation limitations, it might not be
4343		// possible to fail TLS negotiation. An endpoint MUST
4344		// immediately terminate an HTTP/2 connection that
4345		// does not meet the TLS requirements described in
4346		// this section with a connection error (Section
4347		// 5.4.1) of type INADEQUATE_SECURITY.
4348		if sc.tlsState.Version < tls.VersionTLS12 {
4349			sc.rejectConn(http2ErrCodeInadequateSecurity, "TLS version too low")
4350			return
4351		}
4352
4353		if sc.tlsState.ServerName == "" {
4354			// Client must use SNI, but we don't enforce that anymore,
4355			// since it was causing problems when connecting to bare IP
4356			// addresses during development.
4357			//
4358			// TODO: optionally enforce? Or enforce at the time we receive
4359			// a new request, and verify the ServerName matches the :authority?
4360			// But that precludes proxy situations, perhaps.
4361			//
4362			// So for now, do nothing here again.
4363		}
4364
4365		if !s.PermitProhibitedCipherSuites && http2isBadCipher(sc.tlsState.CipherSuite) {
4366			// "Endpoints MAY choose to generate a connection error
4367			// (Section 5.4.1) of type INADEQUATE_SECURITY if one of
4368			// the prohibited cipher suites are negotiated."
4369			//
4370			// We choose that. In my opinion, the spec is weak
4371			// here. It also says both parties must support at least
4372			// TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 so there's no
4373			// excuses here. If we really must, we could allow an
4374			// "AllowInsecureWeakCiphers" option on the server later.
4375			// Let's see how it plays out first.
4376			sc.rejectConn(http2ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
4377			return
4378		}
4379	}
4380
4381	if opts.Settings != nil {
4382		fr := &http2SettingsFrame{
4383			http2FrameHeader: http2FrameHeader{valid: true},
4384			p:                opts.Settings,
4385		}
4386		if err := fr.ForeachSetting(sc.processSetting); err != nil {
4387			sc.rejectConn(http2ErrCodeProtocol, "invalid settings")
4388			return
4389		}
4390		opts.Settings = nil
4391	}
4392
4393	if hook := http2testHookGetServerConn; hook != nil {
4394		hook(sc)
4395	}
4396
4397	if opts.UpgradeRequest != nil {
4398		sc.upgradeRequest(opts.UpgradeRequest)
4399		opts.UpgradeRequest = nil
4400	}
4401
4402	sc.serve()
4403}
4404
4405func http2serverConnBaseContext(c net.Conn, opts *http2ServeConnOpts) (ctx context.Context, cancel func()) {
4406	ctx, cancel = context.WithCancel(opts.context())
4407	ctx = context.WithValue(ctx, LocalAddrContextKey, c.LocalAddr())
4408	if hs := opts.baseConfig(); hs != nil {
4409		ctx = context.WithValue(ctx, ServerContextKey, hs)
4410	}
4411	return
4412}
4413
4414func (sc *http2serverConn) rejectConn(err http2ErrCode, debug string) {
4415	sc.vlogf("http2: server rejecting conn: %v, %s", err, debug)
4416	// ignoring errors. hanging up anyway.
4417	sc.framer.WriteGoAway(0, err, []byte(debug))
4418	sc.bw.Flush()
4419	sc.conn.Close()
4420}
4421
4422type http2serverConn struct {
4423	// Immutable:
4424	srv              *http2Server
4425	hs               *Server
4426	conn             net.Conn
4427	bw               *http2bufferedWriter // writing to conn
4428	handler          Handler
4429	baseCtx          context.Context
4430	framer           *http2Framer
4431	doneServing      chan struct{}               // closed when serverConn.serve ends
4432	readFrameCh      chan http2readFrameResult   // written by serverConn.readFrames
4433	wantWriteFrameCh chan http2FrameWriteRequest // from handlers -> serve
4434	wroteFrameCh     chan http2frameWriteResult  // from writeFrameAsync -> serve, tickles more frame writes
4435	bodyReadCh       chan http2bodyReadMsg       // from handlers -> serve
4436	serveMsgCh       chan interface{}            // misc messages & code to send to / run on the serve loop
4437	flow             http2outflow                // conn-wide (not stream-specific) outbound flow control
4438	inflow           http2inflow                 // conn-wide inbound flow control
4439	tlsState         *tls.ConnectionState        // shared by all handlers, like net/http
4440	remoteAddrStr    string
4441	writeSched       http2WriteScheduler
4442
4443	// Everything following is owned by the serve loop; use serveG.check():
4444	serveG                      http2goroutineLock // used to verify funcs are on serve()
4445	pushEnabled                 bool
4446	sawClientPreface            bool // preface has already been read, used in h2c upgrade
4447	sawFirstSettings            bool // got the initial SETTINGS frame after the preface
4448	needToSendSettingsAck       bool
4449	unackedSettings             int    // how many SETTINGS have we sent without ACKs?
4450	queuedControlFrames         int    // control frames in the writeSched queue
4451	clientMaxStreams            uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit)
4452	advMaxStreams               uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client
4453	curClientStreams            uint32 // number of open streams initiated by the client
4454	curPushedStreams            uint32 // number of open streams initiated by server push
4455	curHandlers                 uint32 // number of running handler goroutines
4456	maxClientStreamID           uint32 // max ever seen from client (odd), or 0 if there have been no client requests
4457	maxPushPromiseID            uint32 // ID of the last push promise (even), or 0 if there have been no pushes
4458	streams                     map[uint32]*http2stream
4459	unstartedHandlers           []http2unstartedHandler
4460	initialStreamSendWindowSize int32
4461	maxFrameSize                int32
4462	peerMaxHeaderListSize       uint32            // zero means unknown (default)
4463	canonHeader                 map[string]string // http2-lower-case -> Go-Canonical-Case
4464	canonHeaderKeysSize         int               // canonHeader keys size in bytes
4465	writingFrame                bool              // started writing a frame (on serve goroutine or separate)
4466	writingFrameAsync           bool              // started a frame on its own goroutine but haven't heard back on wroteFrameCh
4467	needsFrameFlush             bool              // last frame write wasn't a flush
4468	inGoAway                    bool              // we've started to or sent GOAWAY
4469	inFrameScheduleLoop         bool              // whether we're in the scheduleFrameWrite loop
4470	needToSendGoAway            bool              // we need to schedule a GOAWAY frame write
4471	goAwayCode                  http2ErrCode
4472	shutdownTimer               http2timer // nil until used
4473	idleTimer                   http2timer // nil if unused
4474
4475	// Owned by the writeFrameAsync goroutine:
4476	headerWriteBuf bytes.Buffer
4477	hpackEncoder   *hpack.Encoder
4478
4479	// Used by startGracefulShutdown.
4480	shutdownOnce sync.Once
4481}
4482
4483func (sc *http2serverConn) maxHeaderListSize() uint32 {
4484	n := sc.hs.MaxHeaderBytes
4485	if n <= 0 {
4486		n = DefaultMaxHeaderBytes
4487	}
4488	// http2's count is in a slightly different unit and includes 32 bytes per pair.
4489	// So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
4490	const perFieldOverhead = 32 // per http2 spec
4491	const typicalHeaders = 10   // conservative
4492	return uint32(n + typicalHeaders*perFieldOverhead)
4493}
4494
4495func (sc *http2serverConn) curOpenStreams() uint32 {
4496	sc.serveG.check()
4497	return sc.curClientStreams + sc.curPushedStreams
4498}
4499
4500// stream represents a stream. This is the minimal metadata needed by
4501// the serve goroutine. Most of the actual stream state is owned by
4502// the http.Handler's goroutine in the responseWriter. Because the
4503// responseWriter's responseWriterState is recycled at the end of a
4504// handler, this struct intentionally has no pointer to the
4505// *responseWriter{,State} itself, as the Handler ending nils out the
4506// responseWriter's state field.
4507type http2stream struct {
4508	// immutable:
4509	sc        *http2serverConn
4510	id        uint32
4511	body      *http2pipe       // non-nil if expecting DATA frames
4512	cw        http2closeWaiter // closed wait stream transitions to closed state
4513	ctx       context.Context
4514	cancelCtx func()
4515
4516	// owned by serverConn's serve loop:
4517	bodyBytes        int64        // body bytes seen so far
4518	declBodyBytes    int64        // or -1 if undeclared
4519	flow             http2outflow // limits writing from Handler to client
4520	inflow           http2inflow  // what the client is allowed to POST/etc to us
4521	state            http2streamState
4522	resetQueued      bool       // RST_STREAM queued for write; set by sc.resetStream
4523	gotTrailerHeader bool       // HEADER frame for trailers was seen
4524	wroteHeaders     bool       // whether we wrote headers (not status 100)
4525	readDeadline     http2timer // nil if unused
4526	writeDeadline    http2timer // nil if unused
4527	closeErr         error      // set before cw is closed
4528
4529	trailer    Header // accumulated trailers
4530	reqTrailer Header // handler's Request.Trailer
4531}
4532
4533func (sc *http2serverConn) Framer() *http2Framer { return sc.framer }
4534
4535func (sc *http2serverConn) CloseConn() error { return sc.conn.Close() }
4536
4537func (sc *http2serverConn) Flush() error { return sc.bw.Flush() }
4538
4539func (sc *http2serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) {
4540	return sc.hpackEncoder, &sc.headerWriteBuf
4541}
4542
4543func (sc *http2serverConn) state(streamID uint32) (http2streamState, *http2stream) {
4544	sc.serveG.check()
4545	// http://tools.ietf.org/html/rfc7540#section-5.1
4546	if st, ok := sc.streams[streamID]; ok {
4547		return st.state, st
4548	}
4549	// "The first use of a new stream identifier implicitly closes all
4550	// streams in the "idle" state that might have been initiated by
4551	// that peer with a lower-valued stream identifier. For example, if
4552	// a client sends a HEADERS frame on stream 7 without ever sending a
4553	// frame on stream 5, then stream 5 transitions to the "closed"
4554	// state when the first frame for stream 7 is sent or received."
4555	if streamID%2 == 1 {
4556		if streamID <= sc.maxClientStreamID {
4557			return http2stateClosed, nil
4558		}
4559	} else {
4560		if streamID <= sc.maxPushPromiseID {
4561			return http2stateClosed, nil
4562		}
4563	}
4564	return http2stateIdle, nil
4565}
4566
4567// setConnState calls the net/http ConnState hook for this connection, if configured.
4568// Note that the net/http package does StateNew and StateClosed for us.
4569// There is currently no plan for StateHijacked or hijacking HTTP/2 connections.
4570func (sc *http2serverConn) setConnState(state ConnState) {
4571	if sc.hs.ConnState != nil {
4572		sc.hs.ConnState(sc.conn, state)
4573	}
4574}
4575
4576func (sc *http2serverConn) vlogf(format string, args ...interface{}) {
4577	if http2VerboseLogs {
4578		sc.logf(format, args...)
4579	}
4580}
4581
4582func (sc *http2serverConn) logf(format string, args ...interface{}) {
4583	if lg := sc.hs.ErrorLog; lg != nil {
4584		lg.Printf(format, args...)
4585	} else {
4586		log.Printf(format, args...)
4587	}
4588}
4589
4590// errno returns v's underlying uintptr, else 0.
4591//
4592// TODO: remove this helper function once http2 can use build
4593// tags. See comment in isClosedConnError.
4594func http2errno(v error) uintptr {
4595	if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
4596		return uintptr(rv.Uint())
4597	}
4598	return 0
4599}
4600
4601// isClosedConnError reports whether err is an error from use of a closed
4602// network connection.
4603func http2isClosedConnError(err error) bool {
4604	if err == nil {
4605		return false
4606	}
4607
4608	if errors.Is(err, net.ErrClosed) {
4609		return true
4610	}
4611
4612	// TODO(bradfitz): x/tools/cmd/bundle doesn't really support
4613	// build tags, so I can't make an http2_windows.go file with
4614	// Windows-specific stuff. Fix that and move this, once we
4615	// have a way to bundle this into std's net/http somehow.
4616	if runtime.GOOS == "windows" {
4617		if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
4618			if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
4619				const WSAECONNABORTED = 10053
4620				const WSAECONNRESET = 10054
4621				if n := http2errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
4622					return true
4623				}
4624			}
4625		}
4626	}
4627	return false
4628}
4629
4630func (sc *http2serverConn) condlogf(err error, format string, args ...interface{}) {
4631	if err == nil {
4632		return
4633	}
4634	if err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) || err == http2errPrefaceTimeout {
4635		// Boring, expected errors.
4636		sc.vlogf(format, args...)
4637	} else {
4638		sc.logf(format, args...)
4639	}
4640}
4641
4642// maxCachedCanonicalHeadersKeysSize is an arbitrarily-chosen limit on the size
4643// of the entries in the canonHeader cache.
4644// This should be larger than the size of unique, uncommon header keys likely to
4645// be sent by the peer, while not so high as to permit unreasonable memory usage
4646// if the peer sends an unbounded number of unique header keys.
4647const http2maxCachedCanonicalHeadersKeysSize = 2048
4648
4649func (sc *http2serverConn) canonicalHeader(v string) string {
4650	sc.serveG.check()
4651	http2buildCommonHeaderMapsOnce()
4652	cv, ok := http2commonCanonHeader[v]
4653	if ok {
4654		return cv
4655	}
4656	cv, ok = sc.canonHeader[v]
4657	if ok {
4658		return cv
4659	}
4660	if sc.canonHeader == nil {
4661		sc.canonHeader = make(map[string]string)
4662	}
4663	cv = CanonicalHeaderKey(v)
4664	size := 100 + len(v)*2 // 100 bytes of map overhead + key + value
4665	if sc.canonHeaderKeysSize+size <= http2maxCachedCanonicalHeadersKeysSize {
4666		sc.canonHeader[v] = cv
4667		sc.canonHeaderKeysSize += size
4668	}
4669	return cv
4670}
4671
4672type http2readFrameResult struct {
4673	f   http2Frame // valid until readMore is called
4674	err error
4675
4676	// readMore should be called once the consumer no longer needs or
4677	// retains f. After readMore, f is invalid and more frames can be
4678	// read.
4679	readMore func()
4680}
4681
4682// readFrames is the loop that reads incoming frames.
4683// It takes care to only read one frame at a time, blocking until the
4684// consumer is done with the frame.
4685// It's run on its own goroutine.
4686func (sc *http2serverConn) readFrames() {
4687	sc.srv.markNewGoroutine()
4688	gate := make(chan struct{})
4689	gateDone := func() { gate <- struct{}{} }
4690	for {
4691		f, err := sc.framer.ReadFrame()
4692		select {
4693		case sc.readFrameCh <- http2readFrameResult{f, err, gateDone}:
4694		case <-sc.doneServing:
4695			return
4696		}
4697		select {
4698		case <-gate:
4699		case <-sc.doneServing:
4700			return
4701		}
4702		if http2terminalReadFrameError(err) {
4703			return
4704		}
4705	}
4706}
4707
4708// frameWriteResult is the message passed from writeFrameAsync to the serve goroutine.
4709type http2frameWriteResult struct {
4710	_   http2incomparable
4711	wr  http2FrameWriteRequest // what was written (or attempted)
4712	err error                  // result of the writeFrame call
4713}
4714
4715// writeFrameAsync runs in its own goroutine and writes a single frame
4716// and then reports when it's done.
4717// At most one goroutine can be running writeFrameAsync at a time per
4718// serverConn.
4719func (sc *http2serverConn) writeFrameAsync(wr http2FrameWriteRequest, wd *http2writeData) {
4720	sc.srv.markNewGoroutine()
4721	var err error
4722	if wd == nil {
4723		err = wr.write.writeFrame(sc)
4724	} else {
4725		err = sc.framer.endWrite()
4726	}
4727	sc.wroteFrameCh <- http2frameWriteResult{wr: wr, err: err}
4728}
4729
4730func (sc *http2serverConn) closeAllStreamsOnConnClose() {
4731	sc.serveG.check()
4732	for _, st := range sc.streams {
4733		sc.closeStream(st, http2errClientDisconnected)
4734	}
4735}
4736
4737func (sc *http2serverConn) stopShutdownTimer() {
4738	sc.serveG.check()
4739	if t := sc.shutdownTimer; t != nil {
4740		t.Stop()
4741	}
4742}
4743
4744func (sc *http2serverConn) notePanic() {
4745	// Note: this is for serverConn.serve panicking, not http.Handler code.
4746	if http2testHookOnPanicMu != nil {
4747		http2testHookOnPanicMu.Lock()
4748		defer http2testHookOnPanicMu.Unlock()
4749	}
4750	if http2testHookOnPanic != nil {
4751		if e := recover(); e != nil {
4752			if http2testHookOnPanic(sc, e) {
4753				panic(e)
4754			}
4755		}
4756	}
4757}
4758
4759func (sc *http2serverConn) serve() {
4760	sc.serveG.check()
4761	defer sc.notePanic()
4762	defer sc.conn.Close()
4763	defer sc.closeAllStreamsOnConnClose()
4764	defer sc.stopShutdownTimer()
4765	defer close(sc.doneServing) // unblocks handlers trying to send
4766
4767	if http2VerboseLogs {
4768		sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
4769	}
4770
4771	sc.writeFrame(http2FrameWriteRequest{
4772		write: http2writeSettings{
4773			{http2SettingMaxFrameSize, sc.srv.maxReadFrameSize()},
4774			{http2SettingMaxConcurrentStreams, sc.advMaxStreams},
4775			{http2SettingMaxHeaderListSize, sc.maxHeaderListSize()},
4776			{http2SettingHeaderTableSize, sc.srv.maxDecoderHeaderTableSize()},
4777			{http2SettingInitialWindowSize, uint32(sc.srv.initialStreamRecvWindowSize())},
4778		},
4779	})
4780	sc.unackedSettings++
4781
4782	// Each connection starts with initialWindowSize inflow tokens.
4783	// If a higher value is configured, we add more tokens.
4784	if diff := sc.srv.initialConnRecvWindowSize() - http2initialWindowSize; diff > 0 {
4785		sc.sendWindowUpdate(nil, int(diff))
4786	}
4787
4788	if err := sc.readPreface(); err != nil {
4789		sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
4790		return
4791	}
4792	// Now that we've got the preface, get us out of the
4793	// "StateNew" state. We can't go directly to idle, though.
4794	// Active means we read some data and anticipate a request. We'll
4795	// do another Active when we get a HEADERS frame.
4796	sc.setConnState(StateActive)
4797	sc.setConnState(StateIdle)
4798
4799	if sc.srv.IdleTimeout > 0 {
4800		sc.idleTimer = sc.srv.afterFunc(sc.srv.IdleTimeout, sc.onIdleTimer)
4801		defer sc.idleTimer.Stop()
4802	}
4803
4804	go sc.readFrames() // closed by defer sc.conn.Close above
4805
4806	settingsTimer := sc.srv.afterFunc(http2firstSettingsTimeout, sc.onSettingsTimer)
4807	defer settingsTimer.Stop()
4808
4809	loopNum := 0
4810	for {
4811		loopNum++
4812		select {
4813		case wr := <-sc.wantWriteFrameCh:
4814			if se, ok := wr.write.(http2StreamError); ok {
4815				sc.resetStream(se)
4816				break
4817			}
4818			sc.writeFrame(wr)
4819		case res := <-sc.wroteFrameCh:
4820			sc.wroteFrame(res)
4821		case res := <-sc.readFrameCh:
4822			// Process any written frames before reading new frames from the client since a
4823			// written frame could have triggered a new stream to be started.
4824			if sc.writingFrameAsync {
4825				select {
4826				case wroteRes := <-sc.wroteFrameCh:
4827					sc.wroteFrame(wroteRes)
4828				default:
4829				}
4830			}
4831			if !sc.processFrameFromReader(res) {
4832				return
4833			}
4834			res.readMore()
4835			if settingsTimer != nil {
4836				settingsTimer.Stop()
4837				settingsTimer = nil
4838			}
4839		case m := <-sc.bodyReadCh:
4840			sc.noteBodyRead(m.st, m.n)
4841		case msg := <-sc.serveMsgCh:
4842			switch v := msg.(type) {
4843			case func(int):
4844				v(loopNum) // for testing
4845			case *http2serverMessage:
4846				switch v {
4847				case http2settingsTimerMsg:
4848					sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
4849					return
4850				case http2idleTimerMsg:
4851					sc.vlogf("connection is idle")
4852					sc.goAway(http2ErrCodeNo)
4853				case http2shutdownTimerMsg:
4854					sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr())
4855					return
4856				case http2gracefulShutdownMsg:
4857					sc.startGracefulShutdownInternal()
4858				case http2handlerDoneMsg:
4859					sc.handlerDone()
4860				default:
4861					panic("unknown timer")
4862				}
4863			case *http2startPushRequest:
4864				sc.startPush(v)
4865			case func(*http2serverConn):
4866				v(sc)
4867			default:
4868				panic(fmt.Sprintf("unexpected type %T", v))
4869			}
4870		}
4871
4872		// If the peer is causing us to generate a lot of control frames,
4873		// but not reading them from us, assume they are trying to make us
4874		// run out of memory.
4875		if sc.queuedControlFrames > sc.srv.maxQueuedControlFrames() {
4876			sc.vlogf("http2: too many control frames in send queue, closing connection")
4877			return
4878		}
4879
4880		// Start the shutdown timer after sending a GOAWAY. When sending GOAWAY
4881		// with no error code (graceful shutdown), don't start the timer until
4882		// all open streams have been completed.
4883		sentGoAway := sc.inGoAway && !sc.needToSendGoAway && !sc.writingFrame
4884		gracefulShutdownComplete := sc.goAwayCode == http2ErrCodeNo && sc.curOpenStreams() == 0
4885		if sentGoAway && sc.shutdownTimer == nil && (sc.goAwayCode != http2ErrCodeNo || gracefulShutdownComplete) {
4886			sc.shutDownIn(http2goAwayTimeout)
4887		}
4888	}
4889}
4890
4891type http2serverMessage int
4892
4893// Message values sent to serveMsgCh.
4894var (
4895	http2settingsTimerMsg    = new(http2serverMessage)
4896	http2idleTimerMsg        = new(http2serverMessage)
4897	http2shutdownTimerMsg    = new(http2serverMessage)
4898	http2gracefulShutdownMsg = new(http2serverMessage)
4899	http2handlerDoneMsg      = new(http2serverMessage)
4900)
4901
4902func (sc *http2serverConn) onSettingsTimer() { sc.sendServeMsg(http2settingsTimerMsg) }
4903
4904func (sc *http2serverConn) onIdleTimer() { sc.sendServeMsg(http2idleTimerMsg) }
4905
4906func (sc *http2serverConn) onShutdownTimer() { sc.sendServeMsg(http2shutdownTimerMsg) }
4907
4908func (sc *http2serverConn) sendServeMsg(msg interface{}) {
4909	sc.serveG.checkNotOn() // NOT
4910	select {
4911	case sc.serveMsgCh <- msg:
4912	case <-sc.doneServing:
4913	}
4914}
4915
4916var http2errPrefaceTimeout = errors.New("timeout waiting for client preface")
4917
4918// readPreface reads the ClientPreface greeting from the peer or
4919// returns errPrefaceTimeout on timeout, or an error if the greeting
4920// is invalid.
4921func (sc *http2serverConn) readPreface() error {
4922	if sc.sawClientPreface {
4923		return nil
4924	}
4925	errc := make(chan error, 1)
4926	go func() {
4927		// Read the client preface
4928		buf := make([]byte, len(http2ClientPreface))
4929		if _, err := io.ReadFull(sc.conn, buf); err != nil {
4930			errc <- err
4931		} else if !bytes.Equal(buf, http2clientPreface) {
4932			errc <- fmt.Errorf("bogus greeting %q", buf)
4933		} else {
4934			errc <- nil
4935		}
4936	}()
4937	timer := sc.srv.newTimer(http2prefaceTimeout) // TODO: configurable on *Server?
4938	defer timer.Stop()
4939	select {
4940	case <-timer.C():
4941		return http2errPrefaceTimeout
4942	case err := <-errc:
4943		if err == nil {
4944			if http2VerboseLogs {
4945				sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr())
4946			}
4947		}
4948		return err
4949	}
4950}
4951
4952var http2errChanPool = sync.Pool{
4953	New: func() interface{} { return make(chan error, 1) },
4954}
4955
4956var http2writeDataPool = sync.Pool{
4957	New: func() interface{} { return new(http2writeData) },
4958}
4959
4960// writeDataFromHandler writes DATA response frames from a handler on
4961// the given stream.
4962func (sc *http2serverConn) writeDataFromHandler(stream *http2stream, data []byte, endStream bool) error {
4963	ch := http2errChanPool.Get().(chan error)
4964	writeArg := http2writeDataPool.Get().(*http2writeData)
4965	*writeArg = http2writeData{stream.id, data, endStream}
4966	err := sc.writeFrameFromHandler(http2FrameWriteRequest{
4967		write:  writeArg,
4968		stream: stream,
4969		done:   ch,
4970	})
4971	if err != nil {
4972		return err
4973	}
4974	var frameWriteDone bool // the frame write is done (successfully or not)
4975	select {
4976	case err = <-ch:
4977		frameWriteDone = true
4978	case <-sc.doneServing:
4979		return http2errClientDisconnected
4980	case <-stream.cw:
4981		// If both ch and stream.cw were ready (as might
4982		// happen on the final Write after an http.Handler
4983		// ends), prefer the write result. Otherwise this
4984		// might just be us successfully closing the stream.
4985		// The writeFrameAsync and serve goroutines guarantee
4986		// that the ch send will happen before the stream.cw
4987		// close.
4988		select {
4989		case err = <-ch:
4990			frameWriteDone = true
4991		default:
4992			return http2errStreamClosed
4993		}
4994	}
4995	http2errChanPool.Put(ch)
4996	if frameWriteDone {
4997		http2writeDataPool.Put(writeArg)
4998	}
4999	return err
5000}
5001
5002// writeFrameFromHandler sends wr to sc.wantWriteFrameCh, but aborts
5003// if the connection has gone away.
5004//
5005// This must not be run from the serve goroutine itself, else it might
5006// deadlock writing to sc.wantWriteFrameCh (which is only mildly
5007// buffered and is read by serve itself). If you're on the serve
5008// goroutine, call writeFrame instead.
5009func (sc *http2serverConn) writeFrameFromHandler(wr http2FrameWriteRequest) error {
5010	sc.serveG.checkNotOn() // NOT
5011	select {
5012	case sc.wantWriteFrameCh <- wr:
5013		return nil
5014	case <-sc.doneServing:
5015		// Serve loop is gone.
5016		// Client has closed their connection to the server.
5017		return http2errClientDisconnected
5018	}
5019}
5020
5021// writeFrame schedules a frame to write and sends it if there's nothing
5022// already being written.
5023//
5024// There is no pushback here (the serve goroutine never blocks). It's
5025// the http.Handlers that block, waiting for their previous frames to
5026// make it onto the wire
5027//
5028// If you're not on the serve goroutine, use writeFrameFromHandler instead.
5029func (sc *http2serverConn) writeFrame(wr http2FrameWriteRequest) {
5030	sc.serveG.check()
5031
5032	// If true, wr will not be written and wr.done will not be signaled.
5033	var ignoreWrite bool
5034
5035	// We are not allowed to write frames on closed streams. RFC 7540 Section
5036	// 5.1.1 says: "An endpoint MUST NOT send frames other than PRIORITY on
5037	// a closed stream." Our server never sends PRIORITY, so that exception
5038	// does not apply.
5039	//
5040	// The serverConn might close an open stream while the stream's handler
5041	// is still running. For example, the server might close a stream when it
5042	// receives bad data from the client. If this happens, the handler might
5043	// attempt to write a frame after the stream has been closed (since the
5044	// handler hasn't yet been notified of the close). In this case, we simply
5045	// ignore the frame. The handler will notice that the stream is closed when
5046	// it waits for the frame to be written.
5047	//
5048	// As an exception to this rule, we allow sending RST_STREAM after close.
5049	// This allows us to immediately reject new streams without tracking any
5050	// state for those streams (except for the queued RST_STREAM frame). This
5051	// may result in duplicate RST_STREAMs in some cases, but the client should
5052	// ignore those.
5053	if wr.StreamID() != 0 {
5054		_, isReset := wr.write.(http2StreamError)
5055		if state, _ := sc.state(wr.StreamID()); state == http2stateClosed && !isReset {
5056			ignoreWrite = true
5057		}
5058	}
5059
5060	// Don't send a 100-continue response if we've already sent headers.
5061	// See golang.org/issue/14030.
5062	switch wr.write.(type) {
5063	case *http2writeResHeaders:
5064		wr.stream.wroteHeaders = true
5065	case http2write100ContinueHeadersFrame:
5066		if wr.stream.wroteHeaders {
5067			// We do not need to notify wr.done because this frame is
5068			// never written with wr.done != nil.
5069			if wr.done != nil {
5070				panic("wr.done != nil for write100ContinueHeadersFrame")
5071			}
5072			ignoreWrite = true
5073		}
5074	}
5075
5076	if !ignoreWrite {
5077		if wr.isControl() {
5078			sc.queuedControlFrames++
5079			// For extra safety, detect wraparounds, which should not happen,
5080			// and pull the plug.
5081			if sc.queuedControlFrames < 0 {
5082				sc.conn.Close()
5083			}
5084		}
5085		sc.writeSched.Push(wr)
5086	}
5087	sc.scheduleFrameWrite()
5088}
5089
5090// startFrameWrite starts a goroutine to write wr (in a separate
5091// goroutine since that might block on the network), and updates the
5092// serve goroutine's state about the world, updated from info in wr.
5093func (sc *http2serverConn) startFrameWrite(wr http2FrameWriteRequest) {
5094	sc.serveG.check()
5095	if sc.writingFrame {
5096		panic("internal error: can only be writing one frame at a time")
5097	}
5098
5099	st := wr.stream
5100	if st != nil {
5101		switch st.state {
5102		case http2stateHalfClosedLocal:
5103			switch wr.write.(type) {
5104			case http2StreamError, http2handlerPanicRST, http2writeWindowUpdate:
5105				// RFC 7540 Section 5.1 allows sending RST_STREAM, PRIORITY, and WINDOW_UPDATE
5106				// in this state. (We never send PRIORITY from the server, so that is not checked.)
5107			default:
5108				panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr))
5109			}
5110		case http2stateClosed:
5111			panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr))
5112		}
5113	}
5114	if wpp, ok := wr.write.(*http2writePushPromise); ok {
5115		var err error
5116		wpp.promisedID, err = wpp.allocatePromisedID()
5117		if err != nil {
5118			sc.writingFrameAsync = false
5119			wr.replyToWriter(err)
5120			return
5121		}
5122	}
5123
5124	sc.writingFrame = true
5125	sc.needsFrameFlush = true
5126	if wr.write.staysWithinBuffer(sc.bw.Available()) {
5127		sc.writingFrameAsync = false
5128		err := wr.write.writeFrame(sc)
5129		sc.wroteFrame(http2frameWriteResult{wr: wr, err: err})
5130	} else if wd, ok := wr.write.(*http2writeData); ok {
5131		// Encode the frame in the serve goroutine, to ensure we don't have
5132		// any lingering asynchronous references to data passed to Write.
5133		// See https://go.dev/issue/58446.
5134		sc.framer.startWriteDataPadded(wd.streamID, wd.endStream, wd.p, nil)
5135		sc.writingFrameAsync = true
5136		go sc.writeFrameAsync(wr, wd)
5137	} else {
5138		sc.writingFrameAsync = true
5139		go sc.writeFrameAsync(wr, nil)
5140	}
5141}
5142
5143// errHandlerPanicked is the error given to any callers blocked in a read from
5144// Request.Body when the main goroutine panics. Since most handlers read in the
5145// main ServeHTTP goroutine, this will show up rarely.
5146var http2errHandlerPanicked = errors.New("http2: handler panicked")
5147
5148// wroteFrame is called on the serve goroutine with the result of
5149// whatever happened on writeFrameAsync.
5150func (sc *http2serverConn) wroteFrame(res http2frameWriteResult) {
5151	sc.serveG.check()
5152	if !sc.writingFrame {
5153		panic("internal error: expected to be already writing a frame")
5154	}
5155	sc.writingFrame = false
5156	sc.writingFrameAsync = false
5157
5158	wr := res.wr
5159
5160	if http2writeEndsStream(wr.write) {
5161		st := wr.stream
5162		if st == nil {
5163			panic("internal error: expecting non-nil stream")
5164		}
5165		switch st.state {
5166		case http2stateOpen:
5167			// Here we would go to stateHalfClosedLocal in
5168			// theory, but since our handler is done and
5169			// the net/http package provides no mechanism
5170			// for closing a ResponseWriter while still
5171			// reading data (see possible TODO at top of
5172			// this file), we go into closed state here
5173			// anyway, after telling the peer we're
5174			// hanging up on them. We'll transition to
5175			// stateClosed after the RST_STREAM frame is
5176			// written.
5177			st.state = http2stateHalfClosedLocal
5178			// Section 8.1: a server MAY request that the client abort
5179			// transmission of a request without error by sending a
5180			// RST_STREAM with an error code of NO_ERROR after sending
5181			// a complete response.
5182			sc.resetStream(http2streamError(st.id, http2ErrCodeNo))
5183		case http2stateHalfClosedRemote:
5184			sc.closeStream(st, http2errHandlerComplete)
5185		}
5186	} else {
5187		switch v := wr.write.(type) {
5188		case http2StreamError:
5189			// st may be unknown if the RST_STREAM was generated to reject bad input.
5190			if st, ok := sc.streams[v.StreamID]; ok {
5191				sc.closeStream(st, v)
5192			}
5193		case http2handlerPanicRST:
5194			sc.closeStream(wr.stream, http2errHandlerPanicked)
5195		}
5196	}
5197
5198	// Reply (if requested) to unblock the ServeHTTP goroutine.
5199	wr.replyToWriter(res.err)
5200
5201	sc.scheduleFrameWrite()
5202}
5203
5204// scheduleFrameWrite tickles the frame writing scheduler.
5205//
5206// If a frame is already being written, nothing happens. This will be called again
5207// when the frame is done being written.
5208//
5209// If a frame isn't being written and we need to send one, the best frame
5210// to send is selected by writeSched.
5211//
5212// If a frame isn't being written and there's nothing else to send, we
5213// flush the write buffer.
5214func (sc *http2serverConn) scheduleFrameWrite() {
5215	sc.serveG.check()
5216	if sc.writingFrame || sc.inFrameScheduleLoop {
5217		return
5218	}
5219	sc.inFrameScheduleLoop = true
5220	for !sc.writingFrameAsync {
5221		if sc.needToSendGoAway {
5222			sc.needToSendGoAway = false
5223			sc.startFrameWrite(http2FrameWriteRequest{
5224				write: &http2writeGoAway{
5225					maxStreamID: sc.maxClientStreamID,
5226					code:        sc.goAwayCode,
5227				},
5228			})
5229			continue
5230		}
5231		if sc.needToSendSettingsAck {
5232			sc.needToSendSettingsAck = false
5233			sc.startFrameWrite(http2FrameWriteRequest{write: http2writeSettingsAck{}})
5234			continue
5235		}
5236		if !sc.inGoAway || sc.goAwayCode == http2ErrCodeNo {
5237			if wr, ok := sc.writeSched.Pop(); ok {
5238				if wr.isControl() {
5239					sc.queuedControlFrames--
5240				}
5241				sc.startFrameWrite(wr)
5242				continue
5243			}
5244		}
5245		if sc.needsFrameFlush {
5246			sc.startFrameWrite(http2FrameWriteRequest{write: http2flushFrameWriter{}})
5247			sc.needsFrameFlush = false // after startFrameWrite, since it sets this true
5248			continue
5249		}
5250		break
5251	}
5252	sc.inFrameScheduleLoop = false
5253}
5254
5255// startGracefulShutdown gracefully shuts down a connection. This
5256// sends GOAWAY with ErrCodeNo to tell the client we're gracefully
5257// shutting down. The connection isn't closed until all current
5258// streams are done.
5259//
5260// startGracefulShutdown returns immediately; it does not wait until
5261// the connection has shut down.
5262func (sc *http2serverConn) startGracefulShutdown() {
5263	sc.serveG.checkNotOn() // NOT
5264	sc.shutdownOnce.Do(func() { sc.sendServeMsg(http2gracefulShutdownMsg) })
5265}
5266
5267// After sending GOAWAY with an error code (non-graceful shutdown), the
5268// connection will close after goAwayTimeout.
5269//
5270// If we close the connection immediately after sending GOAWAY, there may
5271// be unsent data in our kernel receive buffer, which will cause the kernel
5272// to send a TCP RST on close() instead of a FIN. This RST will abort the
5273// connection immediately, whether or not the client had received the GOAWAY.
5274//
5275// Ideally we should delay for at least 1 RTT + epsilon so the client has
5276// a chance to read the GOAWAY and stop sending messages. Measuring RTT
5277// is hard, so we approximate with 1 second. See golang.org/issue/18701.
5278//
5279// This is a var so it can be shorter in tests, where all requests uses the
5280// loopback interface making the expected RTT very small.
5281//
5282// TODO: configurable?
5283var http2goAwayTimeout = 1 * time.Second
5284
5285func (sc *http2serverConn) startGracefulShutdownInternal() {
5286	sc.goAway(http2ErrCodeNo)
5287}
5288
5289func (sc *http2serverConn) goAway(code http2ErrCode) {
5290	sc.serveG.check()
5291	if sc.inGoAway {
5292		if sc.goAwayCode == http2ErrCodeNo {
5293			sc.goAwayCode = code
5294		}
5295		return
5296	}
5297	sc.inGoAway = true
5298	sc.needToSendGoAway = true
5299	sc.goAwayCode = code
5300	sc.scheduleFrameWrite()
5301}
5302
5303func (sc *http2serverConn) shutDownIn(d time.Duration) {
5304	sc.serveG.check()
5305	sc.shutdownTimer = sc.srv.afterFunc(d, sc.onShutdownTimer)
5306}
5307
5308func (sc *http2serverConn) resetStream(se http2StreamError) {
5309	sc.serveG.check()
5310	sc.writeFrame(http2FrameWriteRequest{write: se})
5311	if st, ok := sc.streams[se.StreamID]; ok {
5312		st.resetQueued = true
5313	}
5314}
5315
5316// processFrameFromReader processes the serve loop's read from readFrameCh from the
5317// frame-reading goroutine.
5318// processFrameFromReader returns whether the connection should be kept open.
5319func (sc *http2serverConn) processFrameFromReader(res http2readFrameResult) bool {
5320	sc.serveG.check()
5321	err := res.err
5322	if err != nil {
5323		if err == http2ErrFrameTooLarge {
5324			sc.goAway(http2ErrCodeFrameSize)
5325			return true // goAway will close the loop
5326		}
5327		clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err)
5328		if clientGone {
5329			// TODO: could we also get into this state if
5330			// the peer does a half close
5331			// (e.g. CloseWrite) because they're done
5332			// sending frames but they're still wanting
5333			// our open replies?  Investigate.
5334			// TODO: add CloseWrite to crypto/tls.Conn first
5335			// so we have a way to test this? I suppose
5336			// just for testing we could have a non-TLS mode.
5337			return false
5338		}
5339	} else {
5340		f := res.f
5341		if http2VerboseLogs {
5342			sc.vlogf("http2: server read frame %v", http2summarizeFrame(f))
5343		}
5344		err = sc.processFrame(f)
5345		if err == nil {
5346			return true
5347		}
5348	}
5349
5350	switch ev := err.(type) {
5351	case http2StreamError:
5352		sc.resetStream(ev)
5353		return true
5354	case http2goAwayFlowError:
5355		sc.goAway(http2ErrCodeFlowControl)
5356		return true
5357	case http2ConnectionError:
5358		if res.f != nil {
5359			if id := res.f.Header().StreamID; id > sc.maxClientStreamID {
5360				sc.maxClientStreamID = id
5361			}
5362		}
5363		sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev)
5364		sc.goAway(http2ErrCode(ev))
5365		return true // goAway will handle shutdown
5366	default:
5367		if res.err != nil {
5368			sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err)
5369		} else {
5370			sc.logf("http2: server closing client connection: %v", err)
5371		}
5372		return false
5373	}
5374}
5375
5376func (sc *http2serverConn) processFrame(f http2Frame) error {
5377	sc.serveG.check()
5378
5379	// First frame received must be SETTINGS.
5380	if !sc.sawFirstSettings {
5381		if _, ok := f.(*http2SettingsFrame); !ok {
5382			return sc.countError("first_settings", http2ConnectionError(http2ErrCodeProtocol))
5383		}
5384		sc.sawFirstSettings = true
5385	}
5386
5387	// Discard frames for streams initiated after the identified last
5388	// stream sent in a GOAWAY, or all frames after sending an error.
5389	// We still need to return connection-level flow control for DATA frames.
5390	// RFC 9113 Section 6.8.
5391	if sc.inGoAway && (sc.goAwayCode != http2ErrCodeNo || f.Header().StreamID > sc.maxClientStreamID) {
5392
5393		if f, ok := f.(*http2DataFrame); ok {
5394			if !sc.inflow.take(f.Length) {
5395				return sc.countError("data_flow", http2streamError(f.Header().StreamID, http2ErrCodeFlowControl))
5396			}
5397			sc.sendWindowUpdate(nil, int(f.Length)) // conn-level
5398		}
5399		return nil
5400	}
5401
5402	switch f := f.(type) {
5403	case *http2SettingsFrame:
5404		return sc.processSettings(f)
5405	case *http2MetaHeadersFrame:
5406		return sc.processHeaders(f)
5407	case *http2WindowUpdateFrame:
5408		return sc.processWindowUpdate(f)
5409	case *http2PingFrame:
5410		return sc.processPing(f)
5411	case *http2DataFrame:
5412		return sc.processData(f)
5413	case *http2RSTStreamFrame:
5414		return sc.processResetStream(f)
5415	case *http2PriorityFrame:
5416		return sc.processPriority(f)
5417	case *http2GoAwayFrame:
5418		return sc.processGoAway(f)
5419	case *http2PushPromiseFrame:
5420		// A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE
5421		// frame as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
5422		return sc.countError("push_promise", http2ConnectionError(http2ErrCodeProtocol))
5423	default:
5424		sc.vlogf("http2: server ignoring frame: %v", f.Header())
5425		return nil
5426	}
5427}
5428
5429func (sc *http2serverConn) processPing(f *http2PingFrame) error {
5430	sc.serveG.check()
5431	if f.IsAck() {
5432		// 6.7 PING: " An endpoint MUST NOT respond to PING frames
5433		// containing this flag."
5434		return nil
5435	}
5436	if f.StreamID != 0 {
5437		// "PING frames are not associated with any individual
5438		// stream. If a PING frame is received with a stream
5439		// identifier field value other than 0x0, the recipient MUST
5440		// respond with a connection error (Section 5.4.1) of type
5441		// PROTOCOL_ERROR."
5442		return sc.countError("ping_on_stream", http2ConnectionError(http2ErrCodeProtocol))
5443	}
5444	sc.writeFrame(http2FrameWriteRequest{write: http2writePingAck{f}})
5445	return nil
5446}
5447
5448func (sc *http2serverConn) processWindowUpdate(f *http2WindowUpdateFrame) error {
5449	sc.serveG.check()
5450	switch {
5451	case f.StreamID != 0: // stream-level flow control
5452		state, st := sc.state(f.StreamID)
5453		if state == http2stateIdle {
5454			// Section 5.1: "Receiving any frame other than HEADERS
5455			// or PRIORITY on a stream in this state MUST be
5456			// treated as a connection error (Section 5.4.1) of
5457			// type PROTOCOL_ERROR."
5458			return sc.countError("stream_idle", http2ConnectionError(http2ErrCodeProtocol))
5459		}
5460		if st == nil {
5461			// "WINDOW_UPDATE can be sent by a peer that has sent a
5462			// frame bearing the END_STREAM flag. This means that a
5463			// receiver could receive a WINDOW_UPDATE frame on a "half
5464			// closed (remote)" or "closed" stream. A receiver MUST
5465			// NOT treat this as an error, see Section 5.1."
5466			return nil
5467		}
5468		if !st.flow.add(int32(f.Increment)) {
5469			return sc.countError("bad_flow", http2streamError(f.StreamID, http2ErrCodeFlowControl))
5470		}
5471	default: // connection-level flow control
5472		if !sc.flow.add(int32(f.Increment)) {
5473			return http2goAwayFlowError{}
5474		}
5475	}
5476	sc.scheduleFrameWrite()
5477	return nil
5478}
5479
5480func (sc *http2serverConn) processResetStream(f *http2RSTStreamFrame) error {
5481	sc.serveG.check()
5482
5483	state, st := sc.state(f.StreamID)
5484	if state == http2stateIdle {
5485		// 6.4 "RST_STREAM frames MUST NOT be sent for a
5486		// stream in the "idle" state. If a RST_STREAM frame
5487		// identifying an idle stream is received, the
5488		// recipient MUST treat this as a connection error
5489		// (Section 5.4.1) of type PROTOCOL_ERROR.
5490		return sc.countError("reset_idle_stream", http2ConnectionError(http2ErrCodeProtocol))
5491	}
5492	if st != nil {
5493		st.cancelCtx()
5494		sc.closeStream(st, http2streamError(f.StreamID, f.ErrCode))
5495	}
5496	return nil
5497}
5498
5499func (sc *http2serverConn) closeStream(st *http2stream, err error) {
5500	sc.serveG.check()
5501	if st.state == http2stateIdle || st.state == http2stateClosed {
5502		panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state))
5503	}
5504	st.state = http2stateClosed
5505	if st.readDeadline != nil {
5506		st.readDeadline.Stop()
5507	}
5508	if st.writeDeadline != nil {
5509		st.writeDeadline.Stop()
5510	}
5511	if st.isPushed() {
5512		sc.curPushedStreams--
5513	} else {
5514		sc.curClientStreams--
5515	}
5516	delete(sc.streams, st.id)
5517	if len(sc.streams) == 0 {
5518		sc.setConnState(StateIdle)
5519		if sc.srv.IdleTimeout > 0 && sc.idleTimer != nil {
5520			sc.idleTimer.Reset(sc.srv.IdleTimeout)
5521		}
5522		if http2h1ServerKeepAlivesDisabled(sc.hs) {
5523			sc.startGracefulShutdownInternal()
5524		}
5525	}
5526	if p := st.body; p != nil {
5527		// Return any buffered unread bytes worth of conn-level flow control.
5528		// See golang.org/issue/16481
5529		sc.sendWindowUpdate(nil, p.Len())
5530
5531		p.CloseWithError(err)
5532	}
5533	if e, ok := err.(http2StreamError); ok {
5534		if e.Cause != nil {
5535			err = e.Cause
5536		} else {
5537			err = http2errStreamClosed
5538		}
5539	}
5540	st.closeErr = err
5541	st.cancelCtx()
5542	st.cw.Close() // signals Handler's CloseNotifier, unblocks writes, etc
5543	sc.writeSched.CloseStream(st.id)
5544}
5545
5546func (sc *http2serverConn) processSettings(f *http2SettingsFrame) error {
5547	sc.serveG.check()
5548	if f.IsAck() {
5549		sc.unackedSettings--
5550		if sc.unackedSettings < 0 {
5551			// Why is the peer ACKing settings we never sent?
5552			// The spec doesn't mention this case, but
5553			// hang up on them anyway.
5554			return sc.countError("ack_mystery", http2ConnectionError(http2ErrCodeProtocol))
5555		}
5556		return nil
5557	}
5558	if f.NumSettings() > 100 || f.HasDuplicates() {
5559		// This isn't actually in the spec, but hang up on
5560		// suspiciously large settings frames or those with
5561		// duplicate entries.
5562		return sc.countError("settings_big_or_dups", http2ConnectionError(http2ErrCodeProtocol))
5563	}
5564	if err := f.ForeachSetting(sc.processSetting); err != nil {
5565		return err
5566	}
5567	// TODO: judging by RFC 7540, Section 6.5.3 each SETTINGS frame should be
5568	// acknowledged individually, even if multiple are received before the ACK.
5569	sc.needToSendSettingsAck = true
5570	sc.scheduleFrameWrite()
5571	return nil
5572}
5573
5574func (sc *http2serverConn) processSetting(s http2Setting) error {
5575	sc.serveG.check()
5576	if err := s.Valid(); err != nil {
5577		return err
5578	}
5579	if http2VerboseLogs {
5580		sc.vlogf("http2: server processing setting %v", s)
5581	}
5582	switch s.ID {
5583	case http2SettingHeaderTableSize:
5584		sc.hpackEncoder.SetMaxDynamicTableSize(s.Val)
5585	case http2SettingEnablePush:
5586		sc.pushEnabled = s.Val != 0
5587	case http2SettingMaxConcurrentStreams:
5588		sc.clientMaxStreams = s.Val
5589	case http2SettingInitialWindowSize:
5590		return sc.processSettingInitialWindowSize(s.Val)
5591	case http2SettingMaxFrameSize:
5592		sc.maxFrameSize = int32(s.Val) // the maximum valid s.Val is < 2^31
5593	case http2SettingMaxHeaderListSize:
5594		sc.peerMaxHeaderListSize = s.Val
5595	default:
5596		// Unknown setting: "An endpoint that receives a SETTINGS
5597		// frame with any unknown or unsupported identifier MUST
5598		// ignore that setting."
5599		if http2VerboseLogs {
5600			sc.vlogf("http2: server ignoring unknown setting %v", s)
5601		}
5602	}
5603	return nil
5604}
5605
5606func (sc *http2serverConn) processSettingInitialWindowSize(val uint32) error {
5607	sc.serveG.check()
5608	// Note: val already validated to be within range by
5609	// processSetting's Valid call.
5610
5611	// "A SETTINGS frame can alter the initial flow control window
5612	// size for all current streams. When the value of
5613	// SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST
5614	// adjust the size of all stream flow control windows that it
5615	// maintains by the difference between the new value and the
5616	// old value."
5617	old := sc.initialStreamSendWindowSize
5618	sc.initialStreamSendWindowSize = int32(val)
5619	growth := int32(val) - old // may be negative
5620	for _, st := range sc.streams {
5621		if !st.flow.add(growth) {
5622			// 6.9.2 Initial Flow Control Window Size
5623			// "An endpoint MUST treat a change to
5624			// SETTINGS_INITIAL_WINDOW_SIZE that causes any flow
5625			// control window to exceed the maximum size as a
5626			// connection error (Section 5.4.1) of type
5627			// FLOW_CONTROL_ERROR."
5628			return sc.countError("setting_win_size", http2ConnectionError(http2ErrCodeFlowControl))
5629		}
5630	}
5631	return nil
5632}
5633
5634func (sc *http2serverConn) processData(f *http2DataFrame) error {
5635	sc.serveG.check()
5636	id := f.Header().StreamID
5637
5638	data := f.Data()
5639	state, st := sc.state(id)
5640	if id == 0 || state == http2stateIdle {
5641		// Section 6.1: "DATA frames MUST be associated with a
5642		// stream. If a DATA frame is received whose stream
5643		// identifier field is 0x0, the recipient MUST respond
5644		// with a connection error (Section 5.4.1) of type
5645		// PROTOCOL_ERROR."
5646		//
5647		// Section 5.1: "Receiving any frame other than HEADERS
5648		// or PRIORITY on a stream in this state MUST be
5649		// treated as a connection error (Section 5.4.1) of
5650		// type PROTOCOL_ERROR."
5651		return sc.countError("data_on_idle", http2ConnectionError(http2ErrCodeProtocol))
5652	}
5653
5654	// "If a DATA frame is received whose stream is not in "open"
5655	// or "half closed (local)" state, the recipient MUST respond
5656	// with a stream error (Section 5.4.2) of type STREAM_CLOSED."
5657	if st == nil || state != http2stateOpen || st.gotTrailerHeader || st.resetQueued {
5658		// This includes sending a RST_STREAM if the stream is
5659		// in stateHalfClosedLocal (which currently means that
5660		// the http.Handler returned, so it's done reading &
5661		// done writing). Try to stop the client from sending
5662		// more DATA.
5663
5664		// But still enforce their connection-level flow control,
5665		// and return any flow control bytes since we're not going
5666		// to consume them.
5667		if !sc.inflow.take(f.Length) {
5668			return sc.countError("data_flow", http2streamError(id, http2ErrCodeFlowControl))
5669		}
5670		sc.sendWindowUpdate(nil, int(f.Length)) // conn-level
5671
5672		if st != nil && st.resetQueued {
5673			// Already have a stream error in flight. Don't send another.
5674			return nil
5675		}
5676		return sc.countError("closed", http2streamError(id, http2ErrCodeStreamClosed))
5677	}
5678	if st.body == nil {
5679		panic("internal error: should have a body in this state")
5680	}
5681
5682	// Sender sending more than they'd declared?
5683	if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes {
5684		if !sc.inflow.take(f.Length) {
5685			return sc.countError("data_flow", http2streamError(id, http2ErrCodeFlowControl))
5686		}
5687		sc.sendWindowUpdate(nil, int(f.Length)) // conn-level
5688
5689		st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes))
5690		// RFC 7540, sec 8.1.2.6: A request or response is also malformed if the
5691		// value of a content-length header field does not equal the sum of the
5692		// DATA frame payload lengths that form the body.
5693		return sc.countError("send_too_much", http2streamError(id, http2ErrCodeProtocol))
5694	}
5695	if f.Length > 0 {
5696		// Check whether the client has flow control quota.
5697		if !http2takeInflows(&sc.inflow, &st.inflow, f.Length) {
5698			return sc.countError("flow_on_data_length", http2streamError(id, http2ErrCodeFlowControl))
5699		}
5700
5701		if len(data) > 0 {
5702			st.bodyBytes += int64(len(data))
5703			wrote, err := st.body.Write(data)
5704			if err != nil {
5705				// The handler has closed the request body.
5706				// Return the connection-level flow control for the discarded data,
5707				// but not the stream-level flow control.
5708				sc.sendWindowUpdate(nil, int(f.Length)-wrote)
5709				return nil
5710			}
5711			if wrote != len(data) {
5712				panic("internal error: bad Writer")
5713			}
5714		}
5715
5716		// Return any padded flow control now, since we won't
5717		// refund it later on body reads.
5718		// Call sendWindowUpdate even if there is no padding,
5719		// to return buffered flow control credit if the sent
5720		// window has shrunk.
5721		pad := int32(f.Length) - int32(len(data))
5722		sc.sendWindowUpdate32(nil, pad)
5723		sc.sendWindowUpdate32(st, pad)
5724	}
5725	if f.StreamEnded() {
5726		st.endStream()
5727	}
5728	return nil
5729}
5730
5731func (sc *http2serverConn) processGoAway(f *http2GoAwayFrame) error {
5732	sc.serveG.check()
5733	if f.ErrCode != http2ErrCodeNo {
5734		sc.logf("http2: received GOAWAY %+v, starting graceful shutdown", f)
5735	} else {
5736		sc.vlogf("http2: received GOAWAY %+v, starting graceful shutdown", f)
5737	}
5738	sc.startGracefulShutdownInternal()
5739	// http://tools.ietf.org/html/rfc7540#section-6.8
5740	// We should not create any new streams, which means we should disable push.
5741	sc.pushEnabled = false
5742	return nil
5743}
5744
5745// isPushed reports whether the stream is server-initiated.
5746func (st *http2stream) isPushed() bool {
5747	return st.id%2 == 0
5748}
5749
5750// endStream closes a Request.Body's pipe. It is called when a DATA
5751// frame says a request body is over (or after trailers).
5752func (st *http2stream) endStream() {
5753	sc := st.sc
5754	sc.serveG.check()
5755
5756	if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes {
5757		st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes",
5758			st.declBodyBytes, st.bodyBytes))
5759	} else {
5760		st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest)
5761		st.body.CloseWithError(io.EOF)
5762	}
5763	st.state = http2stateHalfClosedRemote
5764}
5765
5766// copyTrailersToHandlerRequest is run in the Handler's goroutine in
5767// its Request.Body.Read just before it gets io.EOF.
5768func (st *http2stream) copyTrailersToHandlerRequest() {
5769	for k, vv := range st.trailer {
5770		if _, ok := st.reqTrailer[k]; ok {
5771			// Only copy it over it was pre-declared.
5772			st.reqTrailer[k] = vv
5773		}
5774	}
5775}
5776
5777// onReadTimeout is run on its own goroutine (from time.AfterFunc)
5778// when the stream's ReadTimeout has fired.
5779func (st *http2stream) onReadTimeout() {
5780	if st.body != nil {
5781		// Wrap the ErrDeadlineExceeded to avoid callers depending on us
5782		// returning the bare error.
5783		st.body.CloseWithError(fmt.Errorf("%w", os.ErrDeadlineExceeded))
5784	}
5785}
5786
5787// onWriteTimeout is run on its own goroutine (from time.AfterFunc)
5788// when the stream's WriteTimeout has fired.
5789func (st *http2stream) onWriteTimeout() {
5790	st.sc.writeFrameFromHandler(http2FrameWriteRequest{write: http2StreamError{
5791		StreamID: st.id,
5792		Code:     http2ErrCodeInternal,
5793		Cause:    os.ErrDeadlineExceeded,
5794	}})
5795}
5796
5797func (sc *http2serverConn) processHeaders(f *http2MetaHeadersFrame) error {
5798	sc.serveG.check()
5799	id := f.StreamID
5800	// http://tools.ietf.org/html/rfc7540#section-5.1.1
5801	// Streams initiated by a client MUST use odd-numbered stream
5802	// identifiers. [...] An endpoint that receives an unexpected
5803	// stream identifier MUST respond with a connection error
5804	// (Section 5.4.1) of type PROTOCOL_ERROR.
5805	if id%2 != 1 {
5806		return sc.countError("headers_even", http2ConnectionError(http2ErrCodeProtocol))
5807	}
5808	// A HEADERS frame can be used to create a new stream or
5809	// send a trailer for an open one. If we already have a stream
5810	// open, let it process its own HEADERS frame (trailers at this
5811	// point, if it's valid).
5812	if st := sc.streams[f.StreamID]; st != nil {
5813		if st.resetQueued {
5814			// We're sending RST_STREAM to close the stream, so don't bother
5815			// processing this frame.
5816			return nil
5817		}
5818		// RFC 7540, sec 5.1: If an endpoint receives additional frames, other than
5819		// WINDOW_UPDATE, PRIORITY, or RST_STREAM, for a stream that is in
5820		// this state, it MUST respond with a stream error (Section 5.4.2) of
5821		// type STREAM_CLOSED.
5822		if st.state == http2stateHalfClosedRemote {
5823			return sc.countError("headers_half_closed", http2streamError(id, http2ErrCodeStreamClosed))
5824		}
5825		return st.processTrailerHeaders(f)
5826	}
5827
5828	// [...] The identifier of a newly established stream MUST be
5829	// numerically greater than all streams that the initiating
5830	// endpoint has opened or reserved. [...]  An endpoint that
5831	// receives an unexpected stream identifier MUST respond with
5832	// a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
5833	if id <= sc.maxClientStreamID {
5834		return sc.countError("stream_went_down", http2ConnectionError(http2ErrCodeProtocol))
5835	}
5836	sc.maxClientStreamID = id
5837
5838	if sc.idleTimer != nil {
5839		sc.idleTimer.Stop()
5840	}
5841
5842	// http://tools.ietf.org/html/rfc7540#section-5.1.2
5843	// [...] Endpoints MUST NOT exceed the limit set by their peer. An
5844	// endpoint that receives a HEADERS frame that causes their
5845	// advertised concurrent stream limit to be exceeded MUST treat
5846	// this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR
5847	// or REFUSED_STREAM.
5848	if sc.curClientStreams+1 > sc.advMaxStreams {
5849		if sc.unackedSettings == 0 {
5850			// They should know better.
5851			return sc.countError("over_max_streams", http2streamError(id, http2ErrCodeProtocol))
5852		}
5853		// Assume it's a network race, where they just haven't
5854		// received our last SETTINGS update. But actually
5855		// this can't happen yet, because we don't yet provide
5856		// a way for users to adjust server parameters at
5857		// runtime.
5858		return sc.countError("over_max_streams_race", http2streamError(id, http2ErrCodeRefusedStream))
5859	}
5860
5861	initialState := http2stateOpen
5862	if f.StreamEnded() {
5863		initialState = http2stateHalfClosedRemote
5864	}
5865	st := sc.newStream(id, 0, initialState)
5866
5867	if f.HasPriority() {
5868		if err := sc.checkPriority(f.StreamID, f.Priority); err != nil {
5869			return err
5870		}
5871		sc.writeSched.AdjustStream(st.id, f.Priority)
5872	}
5873
5874	rw, req, err := sc.newWriterAndRequest(st, f)
5875	if err != nil {
5876		return err
5877	}
5878	st.reqTrailer = req.Trailer
5879	if st.reqTrailer != nil {
5880		st.trailer = make(Header)
5881	}
5882	st.body = req.Body.(*http2requestBody).pipe // may be nil
5883	st.declBodyBytes = req.ContentLength
5884
5885	handler := sc.handler.ServeHTTP
5886	if f.Truncated {
5887		// Their header list was too long. Send a 431 error.
5888		handler = http2handleHeaderListTooLong
5889	} else if err := http2checkValidHTTP2RequestHeaders(req.Header); err != nil {
5890		handler = http2new400Handler(err)
5891	}
5892
5893	// The net/http package sets the read deadline from the
5894	// http.Server.ReadTimeout during the TLS handshake, but then
5895	// passes the connection off to us with the deadline already
5896	// set. Disarm it here after the request headers are read,
5897	// similar to how the http1 server works. Here it's
5898	// technically more like the http1 Server's ReadHeaderTimeout
5899	// (in Go 1.8), though. That's a more sane option anyway.
5900	if sc.hs.ReadTimeout > 0 {
5901		sc.conn.SetReadDeadline(time.Time{})
5902		st.readDeadline = sc.srv.afterFunc(sc.hs.ReadTimeout, st.onReadTimeout)
5903	}
5904
5905	return sc.scheduleHandler(id, rw, req, handler)
5906}
5907
5908func (sc *http2serverConn) upgradeRequest(req *Request) {
5909	sc.serveG.check()
5910	id := uint32(1)
5911	sc.maxClientStreamID = id
5912	st := sc.newStream(id, 0, http2stateHalfClosedRemote)
5913	st.reqTrailer = req.Trailer
5914	if st.reqTrailer != nil {
5915		st.trailer = make(Header)
5916	}
5917	rw := sc.newResponseWriter(st, req)
5918
5919	// Disable any read deadline set by the net/http package
5920	// prior to the upgrade.
5921	if sc.hs.ReadTimeout > 0 {
5922		sc.conn.SetReadDeadline(time.Time{})
5923	}
5924
5925	// This is the first request on the connection,
5926	// so start the handler directly rather than going
5927	// through scheduleHandler.
5928	sc.curHandlers++
5929	go sc.runHandler(rw, req, sc.handler.ServeHTTP)
5930}
5931
5932func (st *http2stream) processTrailerHeaders(f *http2MetaHeadersFrame) error {
5933	sc := st.sc
5934	sc.serveG.check()
5935	if st.gotTrailerHeader {
5936		return sc.countError("dup_trailers", http2ConnectionError(http2ErrCodeProtocol))
5937	}
5938	st.gotTrailerHeader = true
5939	if !f.StreamEnded() {
5940		return sc.countError("trailers_not_ended", http2streamError(st.id, http2ErrCodeProtocol))
5941	}
5942
5943	if len(f.PseudoFields()) > 0 {
5944		return sc.countError("trailers_pseudo", http2streamError(st.id, http2ErrCodeProtocol))
5945	}
5946	if st.trailer != nil {
5947		for _, hf := range f.RegularFields() {
5948			key := sc.canonicalHeader(hf.Name)
5949			if !httpguts.ValidTrailerHeader(key) {
5950				// TODO: send more details to the peer somehow. But http2 has
5951				// no way to send debug data at a stream level. Discuss with
5952				// HTTP folk.
5953				return sc.countError("trailers_bogus", http2streamError(st.id, http2ErrCodeProtocol))
5954			}
5955			st.trailer[key] = append(st.trailer[key], hf.Value)
5956		}
5957	}
5958	st.endStream()
5959	return nil
5960}
5961
5962func (sc *http2serverConn) checkPriority(streamID uint32, p http2PriorityParam) error {
5963	if streamID == p.StreamDep {
5964		// Section 5.3.1: "A stream cannot depend on itself. An endpoint MUST treat
5965		// this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR."
5966		// Section 5.3.3 says that a stream can depend on one of its dependencies,
5967		// so it's only self-dependencies that are forbidden.
5968		return sc.countError("priority", http2streamError(streamID, http2ErrCodeProtocol))
5969	}
5970	return nil
5971}
5972
5973func (sc *http2serverConn) processPriority(f *http2PriorityFrame) error {
5974	if err := sc.checkPriority(f.StreamID, f.http2PriorityParam); err != nil {
5975		return err
5976	}
5977	sc.writeSched.AdjustStream(f.StreamID, f.http2PriorityParam)
5978	return nil
5979}
5980
5981func (sc *http2serverConn) newStream(id, pusherID uint32, state http2streamState) *http2stream {
5982	sc.serveG.check()
5983	if id == 0 {
5984		panic("internal error: cannot create stream with id 0")
5985	}
5986
5987	ctx, cancelCtx := context.WithCancel(sc.baseCtx)
5988	st := &http2stream{
5989		sc:        sc,
5990		id:        id,
5991		state:     state,
5992		ctx:       ctx,
5993		cancelCtx: cancelCtx,
5994	}
5995	st.cw.Init()
5996	st.flow.conn = &sc.flow // link to conn-level counter
5997	st.flow.add(sc.initialStreamSendWindowSize)
5998	st.inflow.init(sc.srv.initialStreamRecvWindowSize())
5999	if sc.hs.WriteTimeout > 0 {
6000		st.writeDeadline = sc.srv.afterFunc(sc.hs.WriteTimeout, st.onWriteTimeout)
6001	}
6002
6003	sc.streams[id] = st
6004	sc.writeSched.OpenStream(st.id, http2OpenStreamOptions{PusherID: pusherID})
6005	if st.isPushed() {
6006		sc.curPushedStreams++
6007	} else {
6008		sc.curClientStreams++
6009	}
6010	if sc.curOpenStreams() == 1 {
6011		sc.setConnState(StateActive)
6012	}
6013
6014	return st
6015}
6016
6017func (sc *http2serverConn) newWriterAndRequest(st *http2stream, f *http2MetaHeadersFrame) (*http2responseWriter, *Request, error) {
6018	sc.serveG.check()
6019
6020	rp := http2requestParam{
6021		method:    f.PseudoValue("method"),
6022		scheme:    f.PseudoValue("scheme"),
6023		authority: f.PseudoValue("authority"),
6024		path:      f.PseudoValue("path"),
6025	}
6026
6027	isConnect := rp.method == "CONNECT"
6028	if isConnect {
6029		if rp.path != "" || rp.scheme != "" || rp.authority == "" {
6030			return nil, nil, sc.countError("bad_connect", http2streamError(f.StreamID, http2ErrCodeProtocol))
6031		}
6032	} else if rp.method == "" || rp.path == "" || (rp.scheme != "https" && rp.scheme != "http") {
6033		// See 8.1.2.6 Malformed Requests and Responses:
6034		//
6035		// Malformed requests or responses that are detected
6036		// MUST be treated as a stream error (Section 5.4.2)
6037		// of type PROTOCOL_ERROR."
6038		//
6039		// 8.1.2.3 Request Pseudo-Header Fields
6040		// "All HTTP/2 requests MUST include exactly one valid
6041		// value for the :method, :scheme, and :path
6042		// pseudo-header fields"
6043		return nil, nil, sc.countError("bad_path_method", http2streamError(f.StreamID, http2ErrCodeProtocol))
6044	}
6045
6046	rp.header = make(Header)
6047	for _, hf := range f.RegularFields() {
6048		rp.header.Add(sc.canonicalHeader(hf.Name), hf.Value)
6049	}
6050	if rp.authority == "" {
6051		rp.authority = rp.header.Get("Host")
6052	}
6053
6054	rw, req, err := sc.newWriterAndRequestNoBody(st, rp)
6055	if err != nil {
6056		return nil, nil, err
6057	}
6058	bodyOpen := !f.StreamEnded()
6059	if bodyOpen {
6060		if vv, ok := rp.header["Content-Length"]; ok {
6061			if cl, err := strconv.ParseUint(vv[0], 10, 63); err == nil {
6062				req.ContentLength = int64(cl)
6063			} else {
6064				req.ContentLength = 0
6065			}
6066		} else {
6067			req.ContentLength = -1
6068		}
6069		req.Body.(*http2requestBody).pipe = &http2pipe{
6070			b: &http2dataBuffer{expected: req.ContentLength},
6071		}
6072	}
6073	return rw, req, nil
6074}
6075
6076type http2requestParam struct {
6077	method                  string
6078	scheme, authority, path string
6079	header                  Header
6080}
6081
6082func (sc *http2serverConn) newWriterAndRequestNoBody(st *http2stream, rp http2requestParam) (*http2responseWriter, *Request, error) {
6083	sc.serveG.check()
6084
6085	var tlsState *tls.ConnectionState // nil if not scheme https
6086	if rp.scheme == "https" {
6087		tlsState = sc.tlsState
6088	}
6089
6090	needsContinue := httpguts.HeaderValuesContainsToken(rp.header["Expect"], "100-continue")
6091	if needsContinue {
6092		rp.header.Del("Expect")
6093	}
6094	// Merge Cookie headers into one "; "-delimited value.
6095	if cookies := rp.header["Cookie"]; len(cookies) > 1 {
6096		rp.header.Set("Cookie", strings.Join(cookies, "; "))
6097	}
6098
6099	// Setup Trailers
6100	var trailer Header
6101	for _, v := range rp.header["Trailer"] {
6102		for _, key := range strings.Split(v, ",") {
6103			key = CanonicalHeaderKey(textproto.TrimString(key))
6104			switch key {
6105			case "Transfer-Encoding", "Trailer", "Content-Length":
6106				// Bogus. (copy of http1 rules)
6107				// Ignore.
6108			default:
6109				if trailer == nil {
6110					trailer = make(Header)
6111				}
6112				trailer[key] = nil
6113			}
6114		}
6115	}
6116	delete(rp.header, "Trailer")
6117
6118	var url_ *url.URL
6119	var requestURI string
6120	if rp.method == "CONNECT" {
6121		url_ = &url.URL{Host: rp.authority}
6122		requestURI = rp.authority // mimic HTTP/1 server behavior
6123	} else {
6124		var err error
6125		url_, err = url.ParseRequestURI(rp.path)
6126		if err != nil {
6127			return nil, nil, sc.countError("bad_path", http2streamError(st.id, http2ErrCodeProtocol))
6128		}
6129		requestURI = rp.path
6130	}
6131
6132	body := &http2requestBody{
6133		conn:          sc,
6134		stream:        st,
6135		needsContinue: needsContinue,
6136	}
6137	req := &Request{
6138		Method:     rp.method,
6139		URL:        url_,
6140		RemoteAddr: sc.remoteAddrStr,
6141		Header:     rp.header,
6142		RequestURI: requestURI,
6143		Proto:      "HTTP/2.0",
6144		ProtoMajor: 2,
6145		ProtoMinor: 0,
6146		TLS:        tlsState,
6147		Host:       rp.authority,
6148		Body:       body,
6149		Trailer:    trailer,
6150	}
6151	req = req.WithContext(st.ctx)
6152
6153	rw := sc.newResponseWriter(st, req)
6154	return rw, req, nil
6155}
6156
6157func (sc *http2serverConn) newResponseWriter(st *http2stream, req *Request) *http2responseWriter {
6158	rws := http2responseWriterStatePool.Get().(*http2responseWriterState)
6159	bwSave := rws.bw
6160	*rws = http2responseWriterState{} // zero all the fields
6161	rws.conn = sc
6162	rws.bw = bwSave
6163	rws.bw.Reset(http2chunkWriter{rws})
6164	rws.stream = st
6165	rws.req = req
6166	return &http2responseWriter{rws: rws}
6167}
6168
6169type http2unstartedHandler struct {
6170	streamID uint32
6171	rw       *http2responseWriter
6172	req      *Request
6173	handler  func(ResponseWriter, *Request)
6174}
6175
6176// scheduleHandler starts a handler goroutine,
6177// or schedules one to start as soon as an existing handler finishes.
6178func (sc *http2serverConn) scheduleHandler(streamID uint32, rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) error {
6179	sc.serveG.check()
6180	maxHandlers := sc.advMaxStreams
6181	if sc.curHandlers < maxHandlers {
6182		sc.curHandlers++
6183		go sc.runHandler(rw, req, handler)
6184		return nil
6185	}
6186	if len(sc.unstartedHandlers) > int(4*sc.advMaxStreams) {
6187		return sc.countError("too_many_early_resets", http2ConnectionError(http2ErrCodeEnhanceYourCalm))
6188	}
6189	sc.unstartedHandlers = append(sc.unstartedHandlers, http2unstartedHandler{
6190		streamID: streamID,
6191		rw:       rw,
6192		req:      req,
6193		handler:  handler,
6194	})
6195	return nil
6196}
6197
6198func (sc *http2serverConn) handlerDone() {
6199	sc.serveG.check()
6200	sc.curHandlers--
6201	i := 0
6202	maxHandlers := sc.advMaxStreams
6203	for ; i < len(sc.unstartedHandlers); i++ {
6204		u := sc.unstartedHandlers[i]
6205		if sc.streams[u.streamID] == nil {
6206			// This stream was reset before its goroutine had a chance to start.
6207			continue
6208		}
6209		if sc.curHandlers >= maxHandlers {
6210			break
6211		}
6212		sc.curHandlers++
6213		go sc.runHandler(u.rw, u.req, u.handler)
6214		sc.unstartedHandlers[i] = http2unstartedHandler{} // don't retain references
6215	}
6216	sc.unstartedHandlers = sc.unstartedHandlers[i:]
6217	if len(sc.unstartedHandlers) == 0 {
6218		sc.unstartedHandlers = nil
6219	}
6220}
6221
6222// Run on its own goroutine.
6223func (sc *http2serverConn) runHandler(rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) {
6224	sc.srv.markNewGoroutine()
6225	defer sc.sendServeMsg(http2handlerDoneMsg)
6226	didPanic := true
6227	defer func() {
6228		rw.rws.stream.cancelCtx()
6229		if req.MultipartForm != nil {
6230			req.MultipartForm.RemoveAll()
6231		}
6232		if didPanic {
6233			e := recover()
6234			sc.writeFrameFromHandler(http2FrameWriteRequest{
6235				write:  http2handlerPanicRST{rw.rws.stream.id},
6236				stream: rw.rws.stream,
6237			})
6238			// Same as net/http:
6239			if e != nil && e != ErrAbortHandler {
6240				const size = 64 << 10
6241				buf := make([]byte, size)
6242				buf = buf[:runtime.Stack(buf, false)]
6243				sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
6244			}
6245			return
6246		}
6247		rw.handlerDone()
6248	}()
6249	handler(rw, req)
6250	didPanic = false
6251}
6252
6253func http2handleHeaderListTooLong(w ResponseWriter, r *Request) {
6254	// 10.5.1 Limits on Header Block Size:
6255	// .. "A server that receives a larger header block than it is
6256	// willing to handle can send an HTTP 431 (Request Header Fields Too
6257	// Large) status code"
6258	const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+
6259	w.WriteHeader(statusRequestHeaderFieldsTooLarge)
6260	io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>")
6261}
6262
6263// called from handler goroutines.
6264// h may be nil.
6265func (sc *http2serverConn) writeHeaders(st *http2stream, headerData *http2writeResHeaders) error {
6266	sc.serveG.checkNotOn() // NOT on
6267	var errc chan error
6268	if headerData.h != nil {
6269		// If there's a header map (which we don't own), so we have to block on
6270		// waiting for this frame to be written, so an http.Flush mid-handler
6271		// writes out the correct value of keys, before a handler later potentially
6272		// mutates it.
6273		errc = http2errChanPool.Get().(chan error)
6274	}
6275	if err := sc.writeFrameFromHandler(http2FrameWriteRequest{
6276		write:  headerData,
6277		stream: st,
6278		done:   errc,
6279	}); err != nil {
6280		return err
6281	}
6282	if errc != nil {
6283		select {
6284		case err := <-errc:
6285			http2errChanPool.Put(errc)
6286			return err
6287		case <-sc.doneServing:
6288			return http2errClientDisconnected
6289		case <-st.cw:
6290			return http2errStreamClosed
6291		}
6292	}
6293	return nil
6294}
6295
6296// called from handler goroutines.
6297func (sc *http2serverConn) write100ContinueHeaders(st *http2stream) {
6298	sc.writeFrameFromHandler(http2FrameWriteRequest{
6299		write:  http2write100ContinueHeadersFrame{st.id},
6300		stream: st,
6301	})
6302}
6303
6304// A bodyReadMsg tells the server loop that the http.Handler read n
6305// bytes of the DATA from the client on the given stream.
6306type http2bodyReadMsg struct {
6307	st *http2stream
6308	n  int
6309}
6310
6311// called from handler goroutines.
6312// Notes that the handler for the given stream ID read n bytes of its body
6313// and schedules flow control tokens to be sent.
6314func (sc *http2serverConn) noteBodyReadFromHandler(st *http2stream, n int, err error) {
6315	sc.serveG.checkNotOn() // NOT on
6316	if n > 0 {
6317		select {
6318		case sc.bodyReadCh <- http2bodyReadMsg{st, n}:
6319		case <-sc.doneServing:
6320		}
6321	}
6322}
6323
6324func (sc *http2serverConn) noteBodyRead(st *http2stream, n int) {
6325	sc.serveG.check()
6326	sc.sendWindowUpdate(nil, n) // conn-level
6327	if st.state != http2stateHalfClosedRemote && st.state != http2stateClosed {
6328		// Don't send this WINDOW_UPDATE if the stream is closed
6329		// remotely.
6330		sc.sendWindowUpdate(st, n)
6331	}
6332}
6333
6334// st may be nil for conn-level
6335func (sc *http2serverConn) sendWindowUpdate32(st *http2stream, n int32) {
6336	sc.sendWindowUpdate(st, int(n))
6337}
6338
6339// st may be nil for conn-level
6340func (sc *http2serverConn) sendWindowUpdate(st *http2stream, n int) {
6341	sc.serveG.check()
6342	var streamID uint32
6343	var send int32
6344	if st == nil {
6345		send = sc.inflow.add(n)
6346	} else {
6347		streamID = st.id
6348		send = st.inflow.add(n)
6349	}
6350	if send == 0 {
6351		return
6352	}
6353	sc.writeFrame(http2FrameWriteRequest{
6354		write:  http2writeWindowUpdate{streamID: streamID, n: uint32(send)},
6355		stream: st,
6356	})
6357}
6358
6359// requestBody is the Handler's Request.Body type.
6360// Read and Close may be called concurrently.
6361type http2requestBody struct {
6362	_             http2incomparable
6363	stream        *http2stream
6364	conn          *http2serverConn
6365	closeOnce     sync.Once  // for use by Close only
6366	sawEOF        bool       // for use by Read only
6367	pipe          *http2pipe // non-nil if we have an HTTP entity message body
6368	needsContinue bool       // need to send a 100-continue
6369}
6370
6371func (b *http2requestBody) Close() error {
6372	b.closeOnce.Do(func() {
6373		if b.pipe != nil {
6374			b.pipe.BreakWithError(http2errClosedBody)
6375		}
6376	})
6377	return nil
6378}
6379
6380func (b *http2requestBody) Read(p []byte) (n int, err error) {
6381	if b.needsContinue {
6382		b.needsContinue = false
6383		b.conn.write100ContinueHeaders(b.stream)
6384	}
6385	if b.pipe == nil || b.sawEOF {
6386		return 0, io.EOF
6387	}
6388	n, err = b.pipe.Read(p)
6389	if err == io.EOF {
6390		b.sawEOF = true
6391	}
6392	if b.conn == nil && http2inTests {
6393		return
6394	}
6395	b.conn.noteBodyReadFromHandler(b.stream, n, err)
6396	return
6397}
6398
6399// responseWriter is the http.ResponseWriter implementation. It's
6400// intentionally small (1 pointer wide) to minimize garbage. The
6401// responseWriterState pointer inside is zeroed at the end of a
6402// request (in handlerDone) and calls on the responseWriter thereafter
6403// simply crash (caller's mistake), but the much larger responseWriterState
6404// and buffers are reused between multiple requests.
6405type http2responseWriter struct {
6406	rws *http2responseWriterState
6407}
6408
6409// Optional http.ResponseWriter interfaces implemented.
6410var (
6411	_ CloseNotifier     = (*http2responseWriter)(nil)
6412	_ Flusher           = (*http2responseWriter)(nil)
6413	_ http2stringWriter = (*http2responseWriter)(nil)
6414)
6415
6416type http2responseWriterState struct {
6417	// immutable within a request:
6418	stream *http2stream
6419	req    *Request
6420	conn   *http2serverConn
6421
6422	// TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc
6423	bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState}
6424
6425	// mutated by http.Handler goroutine:
6426	handlerHeader Header   // nil until called
6427	snapHeader    Header   // snapshot of handlerHeader at WriteHeader time
6428	trailers      []string // set in writeChunk
6429	status        int      // status code passed to WriteHeader
6430	wroteHeader   bool     // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet.
6431	sentHeader    bool     // have we sent the header frame?
6432	handlerDone   bool     // handler has finished
6433
6434	sentContentLen int64 // non-zero if handler set a Content-Length header
6435	wroteBytes     int64
6436
6437	closeNotifierMu sync.Mutex // guards closeNotifierCh
6438	closeNotifierCh chan bool  // nil until first used
6439}
6440
6441type http2chunkWriter struct{ rws *http2responseWriterState }
6442
6443func (cw http2chunkWriter) Write(p []byte) (n int, err error) {
6444	n, err = cw.rws.writeChunk(p)
6445	if err == http2errStreamClosed {
6446		// If writing failed because the stream has been closed,
6447		// return the reason it was closed.
6448		err = cw.rws.stream.closeErr
6449	}
6450	return n, err
6451}
6452
6453func (rws *http2responseWriterState) hasTrailers() bool { return len(rws.trailers) > 0 }
6454
6455func (rws *http2responseWriterState) hasNonemptyTrailers() bool {
6456	for _, trailer := range rws.trailers {
6457		if _, ok := rws.handlerHeader[trailer]; ok {
6458			return true
6459		}
6460	}
6461	return false
6462}
6463
6464// declareTrailer is called for each Trailer header when the
6465// response header is written. It notes that a header will need to be
6466// written in the trailers at the end of the response.
6467func (rws *http2responseWriterState) declareTrailer(k string) {
6468	k = CanonicalHeaderKey(k)
6469	if !httpguts.ValidTrailerHeader(k) {
6470		// Forbidden by RFC 7230, section 4.1.2.
6471		rws.conn.logf("ignoring invalid trailer %q", k)
6472		return
6473	}
6474	if !http2strSliceContains(rws.trailers, k) {
6475		rws.trailers = append(rws.trailers, k)
6476	}
6477}
6478
6479// writeChunk writes chunks from the bufio.Writer. But because
6480// bufio.Writer may bypass its chunking, sometimes p may be
6481// arbitrarily large.
6482//
6483// writeChunk is also responsible (on the first chunk) for sending the
6484// HEADER response.
6485func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) {
6486	if !rws.wroteHeader {
6487		rws.writeHeader(200)
6488	}
6489
6490	if rws.handlerDone {
6491		rws.promoteUndeclaredTrailers()
6492	}
6493
6494	isHeadResp := rws.req.Method == "HEAD"
6495	if !rws.sentHeader {
6496		rws.sentHeader = true
6497		var ctype, clen string
6498		if clen = rws.snapHeader.Get("Content-Length"); clen != "" {
6499			rws.snapHeader.Del("Content-Length")
6500			if cl, err := strconv.ParseUint(clen, 10, 63); err == nil {
6501				rws.sentContentLen = int64(cl)
6502			} else {
6503				clen = ""
6504			}
6505		}
6506		_, hasContentLength := rws.snapHeader["Content-Length"]
6507		if !hasContentLength && clen == "" && rws.handlerDone && http2bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
6508			clen = strconv.Itoa(len(p))
6509		}
6510		_, hasContentType := rws.snapHeader["Content-Type"]
6511		// If the Content-Encoding is non-blank, we shouldn't
6512		// sniff the body. See Issue golang.org/issue/31753.
6513		ce := rws.snapHeader.Get("Content-Encoding")
6514		hasCE := len(ce) > 0
6515		if !hasCE && !hasContentType && http2bodyAllowedForStatus(rws.status) && len(p) > 0 {
6516			ctype = DetectContentType(p)
6517		}
6518		var date string
6519		if _, ok := rws.snapHeader["Date"]; !ok {
6520			// TODO(bradfitz): be faster here, like net/http? measure.
6521			date = rws.conn.srv.now().UTC().Format(TimeFormat)
6522		}
6523
6524		for _, v := range rws.snapHeader["Trailer"] {
6525			http2foreachHeaderElement(v, rws.declareTrailer)
6526		}
6527
6528		// "Connection" headers aren't allowed in HTTP/2 (RFC 7540, 8.1.2.2),
6529		// but respect "Connection" == "close" to mean sending a GOAWAY and tearing
6530		// down the TCP connection when idle, like we do for HTTP/1.
6531		// TODO: remove more Connection-specific header fields here, in addition
6532		// to "Connection".
6533		if _, ok := rws.snapHeader["Connection"]; ok {
6534			v := rws.snapHeader.Get("Connection")
6535			delete(rws.snapHeader, "Connection")
6536			if v == "close" {
6537				rws.conn.startGracefulShutdown()
6538			}
6539		}
6540
6541		endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp
6542		err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{
6543			streamID:      rws.stream.id,
6544			httpResCode:   rws.status,
6545			h:             rws.snapHeader,
6546			endStream:     endStream,
6547			contentType:   ctype,
6548			contentLength: clen,
6549			date:          date,
6550		})
6551		if err != nil {
6552			return 0, err
6553		}
6554		if endStream {
6555			return 0, nil
6556		}
6557	}
6558	if isHeadResp {
6559		return len(p), nil
6560	}
6561	if len(p) == 0 && !rws.handlerDone {
6562		return 0, nil
6563	}
6564
6565	// only send trailers if they have actually been defined by the
6566	// server handler.
6567	hasNonemptyTrailers := rws.hasNonemptyTrailers()
6568	endStream := rws.handlerDone && !hasNonemptyTrailers
6569	if len(p) > 0 || endStream {
6570		// only send a 0 byte DATA frame if we're ending the stream.
6571		if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil {
6572			return 0, err
6573		}
6574	}
6575
6576	if rws.handlerDone && hasNonemptyTrailers {
6577		err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{
6578			streamID:  rws.stream.id,
6579			h:         rws.handlerHeader,
6580			trailers:  rws.trailers,
6581			endStream: true,
6582		})
6583		return len(p), err
6584	}
6585	return len(p), nil
6586}
6587
6588// TrailerPrefix is a magic prefix for ResponseWriter.Header map keys
6589// that, if present, signals that the map entry is actually for
6590// the response trailers, and not the response headers. The prefix
6591// is stripped after the ServeHTTP call finishes and the values are
6592// sent in the trailers.
6593//
6594// This mechanism is intended only for trailers that are not known
6595// prior to the headers being written. If the set of trailers is fixed
6596// or known before the header is written, the normal Go trailers mechanism
6597// is preferred:
6598//
6599//	https://golang.org/pkg/net/http/#ResponseWriter
6600//	https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
6601const http2TrailerPrefix = "Trailer:"
6602
6603// promoteUndeclaredTrailers permits http.Handlers to set trailers
6604// after the header has already been flushed. Because the Go
6605// ResponseWriter interface has no way to set Trailers (only the
6606// Header), and because we didn't want to expand the ResponseWriter
6607// interface, and because nobody used trailers, and because RFC 7230
6608// says you SHOULD (but not must) predeclare any trailers in the
6609// header, the official ResponseWriter rules said trailers in Go must
6610// be predeclared, and then we reuse the same ResponseWriter.Header()
6611// map to mean both Headers and Trailers. When it's time to write the
6612// Trailers, we pick out the fields of Headers that were declared as
6613// trailers. That worked for a while, until we found the first major
6614// user of Trailers in the wild: gRPC (using them only over http2),
6615// and gRPC libraries permit setting trailers mid-stream without
6616// predeclaring them. So: change of plans. We still permit the old
6617// way, but we also permit this hack: if a Header() key begins with
6618// "Trailer:", the suffix of that key is a Trailer. Because ':' is an
6619// invalid token byte anyway, there is no ambiguity. (And it's already
6620// filtered out) It's mildly hacky, but not terrible.
6621//
6622// This method runs after the Handler is done and promotes any Header
6623// fields to be trailers.
6624func (rws *http2responseWriterState) promoteUndeclaredTrailers() {
6625	for k, vv := range rws.handlerHeader {
6626		if !strings.HasPrefix(k, http2TrailerPrefix) {
6627			continue
6628		}
6629		trailerKey := strings.TrimPrefix(k, http2TrailerPrefix)
6630		rws.declareTrailer(trailerKey)
6631		rws.handlerHeader[CanonicalHeaderKey(trailerKey)] = vv
6632	}
6633
6634	if len(rws.trailers) > 1 {
6635		sorter := http2sorterPool.Get().(*http2sorter)
6636		sorter.SortStrings(rws.trailers)
6637		http2sorterPool.Put(sorter)
6638	}
6639}
6640
6641func (w *http2responseWriter) SetReadDeadline(deadline time.Time) error {
6642	st := w.rws.stream
6643	if !deadline.IsZero() && deadline.Before(w.rws.conn.srv.now()) {
6644		// If we're setting a deadline in the past, reset the stream immediately
6645		// so writes after SetWriteDeadline returns will fail.
6646		st.onReadTimeout()
6647		return nil
6648	}
6649	w.rws.conn.sendServeMsg(func(sc *http2serverConn) {
6650		if st.readDeadline != nil {
6651			if !st.readDeadline.Stop() {
6652				// Deadline already exceeded, or stream has been closed.
6653				return
6654			}
6655		}
6656		if deadline.IsZero() {
6657			st.readDeadline = nil
6658		} else if st.readDeadline == nil {
6659			st.readDeadline = sc.srv.afterFunc(deadline.Sub(sc.srv.now()), st.onReadTimeout)
6660		} else {
6661			st.readDeadline.Reset(deadline.Sub(sc.srv.now()))
6662		}
6663	})
6664	return nil
6665}
6666
6667func (w *http2responseWriter) SetWriteDeadline(deadline time.Time) error {
6668	st := w.rws.stream
6669	if !deadline.IsZero() && deadline.Before(w.rws.conn.srv.now()) {
6670		// If we're setting a deadline in the past, reset the stream immediately
6671		// so writes after SetWriteDeadline returns will fail.
6672		st.onWriteTimeout()
6673		return nil
6674	}
6675	w.rws.conn.sendServeMsg(func(sc *http2serverConn) {
6676		if st.writeDeadline != nil {
6677			if !st.writeDeadline.Stop() {
6678				// Deadline already exceeded, or stream has been closed.
6679				return
6680			}
6681		}
6682		if deadline.IsZero() {
6683			st.writeDeadline = nil
6684		} else if st.writeDeadline == nil {
6685			st.writeDeadline = sc.srv.afterFunc(deadline.Sub(sc.srv.now()), st.onWriteTimeout)
6686		} else {
6687			st.writeDeadline.Reset(deadline.Sub(sc.srv.now()))
6688		}
6689	})
6690	return nil
6691}
6692
6693func (w *http2responseWriter) Flush() {
6694	w.FlushError()
6695}
6696
6697func (w *http2responseWriter) FlushError() error {
6698	rws := w.rws
6699	if rws == nil {
6700		panic("Header called after Handler finished")
6701	}
6702	var err error
6703	if rws.bw.Buffered() > 0 {
6704		err = rws.bw.Flush()
6705	} else {
6706		// The bufio.Writer won't call chunkWriter.Write
6707		// (writeChunk with zero bytes), so we have to do it
6708		// ourselves to force the HTTP response header and/or
6709		// final DATA frame (with END_STREAM) to be sent.
6710		_, err = http2chunkWriter{rws}.Write(nil)
6711		if err == nil {
6712			select {
6713			case <-rws.stream.cw:
6714				err = rws.stream.closeErr
6715			default:
6716			}
6717		}
6718	}
6719	return err
6720}
6721
6722func (w *http2responseWriter) CloseNotify() <-chan bool {
6723	rws := w.rws
6724	if rws == nil {
6725		panic("CloseNotify called after Handler finished")
6726	}
6727	rws.closeNotifierMu.Lock()
6728	ch := rws.closeNotifierCh
6729	if ch == nil {
6730		ch = make(chan bool, 1)
6731		rws.closeNotifierCh = ch
6732		cw := rws.stream.cw
6733		go func() {
6734			cw.Wait() // wait for close
6735			ch <- true
6736		}()
6737	}
6738	rws.closeNotifierMu.Unlock()
6739	return ch
6740}
6741
6742func (w *http2responseWriter) Header() Header {
6743	rws := w.rws
6744	if rws == nil {
6745		panic("Header called after Handler finished")
6746	}
6747	if rws.handlerHeader == nil {
6748		rws.handlerHeader = make(Header)
6749	}
6750	return rws.handlerHeader
6751}
6752
6753// checkWriteHeaderCode is a copy of net/http's checkWriteHeaderCode.
6754func http2checkWriteHeaderCode(code int) {
6755	// Issue 22880: require valid WriteHeader status codes.
6756	// For now we only enforce that it's three digits.
6757	// In the future we might block things over 599 (600 and above aren't defined
6758	// at http://httpwg.org/specs/rfc7231.html#status.codes).
6759	// But for now any three digits.
6760	//
6761	// We used to send "HTTP/1.1 000 0" on the wire in responses but there's
6762	// no equivalent bogus thing we can realistically send in HTTP/2,
6763	// so we'll consistently panic instead and help people find their bugs
6764	// early. (We can't return an error from WriteHeader even if we wanted to.)
6765	if code < 100 || code > 999 {
6766		panic(fmt.Sprintf("invalid WriteHeader code %v", code))
6767	}
6768}
6769
6770func (w *http2responseWriter) WriteHeader(code int) {
6771	rws := w.rws
6772	if rws == nil {
6773		panic("WriteHeader called after Handler finished")
6774	}
6775	rws.writeHeader(code)
6776}
6777
6778func (rws *http2responseWriterState) writeHeader(code int) {
6779	if rws.wroteHeader {
6780		return
6781	}
6782
6783	http2checkWriteHeaderCode(code)
6784
6785	// Handle informational headers
6786	if code >= 100 && code <= 199 {
6787		// Per RFC 8297 we must not clear the current header map
6788		h := rws.handlerHeader
6789
6790		_, cl := h["Content-Length"]
6791		_, te := h["Transfer-Encoding"]
6792		if cl || te {
6793			h = h.Clone()
6794			h.Del("Content-Length")
6795			h.Del("Transfer-Encoding")
6796		}
6797
6798		rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{
6799			streamID:    rws.stream.id,
6800			httpResCode: code,
6801			h:           h,
6802			endStream:   rws.handlerDone && !rws.hasTrailers(),
6803		})
6804
6805		return
6806	}
6807
6808	rws.wroteHeader = true
6809	rws.status = code
6810	if len(rws.handlerHeader) > 0 {
6811		rws.snapHeader = http2cloneHeader(rws.handlerHeader)
6812	}
6813}
6814
6815func http2cloneHeader(h Header) Header {
6816	h2 := make(Header, len(h))
6817	for k, vv := range h {
6818		vv2 := make([]string, len(vv))
6819		copy(vv2, vv)
6820		h2[k] = vv2
6821	}
6822	return h2
6823}
6824
6825// The Life Of A Write is like this:
6826//
6827// * Handler calls w.Write or w.WriteString ->
6828// * -> rws.bw (*bufio.Writer) ->
6829// * (Handler might call Flush)
6830// * -> chunkWriter{rws}
6831// * -> responseWriterState.writeChunk(p []byte)
6832// * -> responseWriterState.writeChunk (most of the magic; see comment there)
6833func (w *http2responseWriter) Write(p []byte) (n int, err error) {
6834	return w.write(len(p), p, "")
6835}
6836
6837func (w *http2responseWriter) WriteString(s string) (n int, err error) {
6838	return w.write(len(s), nil, s)
6839}
6840
6841// either dataB or dataS is non-zero.
6842func (w *http2responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) {
6843	rws := w.rws
6844	if rws == nil {
6845		panic("Write called after Handler finished")
6846	}
6847	if !rws.wroteHeader {
6848		w.WriteHeader(200)
6849	}
6850	if !http2bodyAllowedForStatus(rws.status) {
6851		return 0, ErrBodyNotAllowed
6852	}
6853	rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) // only one can be set
6854	if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen {
6855		// TODO: send a RST_STREAM
6856		return 0, errors.New("http2: handler wrote more than declared Content-Length")
6857	}
6858
6859	if dataB != nil {
6860		return rws.bw.Write(dataB)
6861	} else {
6862		return rws.bw.WriteString(dataS)
6863	}
6864}
6865
6866func (w *http2responseWriter) handlerDone() {
6867	rws := w.rws
6868	rws.handlerDone = true
6869	w.Flush()
6870	w.rws = nil
6871	http2responseWriterStatePool.Put(rws)
6872}
6873
6874// Push errors.
6875var (
6876	http2ErrRecursivePush    = errors.New("http2: recursive push not allowed")
6877	http2ErrPushLimitReached = errors.New("http2: push would exceed peer's SETTINGS_MAX_CONCURRENT_STREAMS")
6878)
6879
6880var _ Pusher = (*http2responseWriter)(nil)
6881
6882func (w *http2responseWriter) Push(target string, opts *PushOptions) error {
6883	st := w.rws.stream
6884	sc := st.sc
6885	sc.serveG.checkNotOn()
6886
6887	// No recursive pushes: "PUSH_PROMISE frames MUST only be sent on a peer-initiated stream."
6888	// http://tools.ietf.org/html/rfc7540#section-6.6
6889	if st.isPushed() {
6890		return http2ErrRecursivePush
6891	}
6892
6893	if opts == nil {
6894		opts = new(PushOptions)
6895	}
6896
6897	// Default options.
6898	if opts.Method == "" {
6899		opts.Method = "GET"
6900	}
6901	if opts.Header == nil {
6902		opts.Header = Header{}
6903	}
6904	wantScheme := "http"
6905	if w.rws.req.TLS != nil {
6906		wantScheme = "https"
6907	}
6908
6909	// Validate the request.
6910	u, err := url.Parse(target)
6911	if err != nil {
6912		return err
6913	}
6914	if u.Scheme == "" {
6915		if !strings.HasPrefix(target, "/") {
6916			return fmt.Errorf("target must be an absolute URL or an absolute path: %q", target)
6917		}
6918		u.Scheme = wantScheme
6919		u.Host = w.rws.req.Host
6920	} else {
6921		if u.Scheme != wantScheme {
6922			return fmt.Errorf("cannot push URL with scheme %q from request with scheme %q", u.Scheme, wantScheme)
6923		}
6924		if u.Host == "" {
6925			return errors.New("URL must have a host")
6926		}
6927	}
6928	for k := range opts.Header {
6929		if strings.HasPrefix(k, ":") {
6930			return fmt.Errorf("promised request headers cannot include pseudo header %q", k)
6931		}
6932		// These headers are meaningful only if the request has a body,
6933		// but PUSH_PROMISE requests cannot have a body.
6934		// http://tools.ietf.org/html/rfc7540#section-8.2
6935		// Also disallow Host, since the promised URL must be absolute.
6936		if http2asciiEqualFold(k, "content-length") ||
6937			http2asciiEqualFold(k, "content-encoding") ||
6938			http2asciiEqualFold(k, "trailer") ||
6939			http2asciiEqualFold(k, "te") ||
6940			http2asciiEqualFold(k, "expect") ||
6941			http2asciiEqualFold(k, "host") {
6942			return fmt.Errorf("promised request headers cannot include %q", k)
6943		}
6944	}
6945	if err := http2checkValidHTTP2RequestHeaders(opts.Header); err != nil {
6946		return err
6947	}
6948
6949	// The RFC effectively limits promised requests to GET and HEAD:
6950	// "Promised requests MUST be cacheable [GET, HEAD, or POST], and MUST be safe [GET or HEAD]"
6951	// http://tools.ietf.org/html/rfc7540#section-8.2
6952	if opts.Method != "GET" && opts.Method != "HEAD" {
6953		return fmt.Errorf("method %q must be GET or HEAD", opts.Method)
6954	}
6955
6956	msg := &http2startPushRequest{
6957		parent: st,
6958		method: opts.Method,
6959		url:    u,
6960		header: http2cloneHeader(opts.Header),
6961		done:   http2errChanPool.Get().(chan error),
6962	}
6963
6964	select {
6965	case <-sc.doneServing:
6966		return http2errClientDisconnected
6967	case <-st.cw:
6968		return http2errStreamClosed
6969	case sc.serveMsgCh <- msg:
6970	}
6971
6972	select {
6973	case <-sc.doneServing:
6974		return http2errClientDisconnected
6975	case <-st.cw:
6976		return http2errStreamClosed
6977	case err := <-msg.done:
6978		http2errChanPool.Put(msg.done)
6979		return err
6980	}
6981}
6982
6983type http2startPushRequest struct {
6984	parent *http2stream
6985	method string
6986	url    *url.URL
6987	header Header
6988	done   chan error
6989}
6990
6991func (sc *http2serverConn) startPush(msg *http2startPushRequest) {
6992	sc.serveG.check()
6993
6994	// http://tools.ietf.org/html/rfc7540#section-6.6.
6995	// PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that
6996	// is in either the "open" or "half-closed (remote)" state.
6997	if msg.parent.state != http2stateOpen && msg.parent.state != http2stateHalfClosedRemote {
6998		// responseWriter.Push checks that the stream is peer-initiated.
6999		msg.done <- http2errStreamClosed
7000		return
7001	}
7002
7003	// http://tools.ietf.org/html/rfc7540#section-6.6.
7004	if !sc.pushEnabled {
7005		msg.done <- ErrNotSupported
7006		return
7007	}
7008
7009	// PUSH_PROMISE frames must be sent in increasing order by stream ID, so
7010	// we allocate an ID for the promised stream lazily, when the PUSH_PROMISE
7011	// is written. Once the ID is allocated, we start the request handler.
7012	allocatePromisedID := func() (uint32, error) {
7013		sc.serveG.check()
7014
7015		// Check this again, just in case. Technically, we might have received
7016		// an updated SETTINGS by the time we got around to writing this frame.
7017		if !sc.pushEnabled {
7018			return 0, ErrNotSupported
7019		}
7020		// http://tools.ietf.org/html/rfc7540#section-6.5.2.
7021		if sc.curPushedStreams+1 > sc.clientMaxStreams {
7022			return 0, http2ErrPushLimitReached
7023		}
7024
7025		// http://tools.ietf.org/html/rfc7540#section-5.1.1.
7026		// Streams initiated by the server MUST use even-numbered identifiers.
7027		// A server that is unable to establish a new stream identifier can send a GOAWAY
7028		// frame so that the client is forced to open a new connection for new streams.
7029		if sc.maxPushPromiseID+2 >= 1<<31 {
7030			sc.startGracefulShutdownInternal()
7031			return 0, http2ErrPushLimitReached
7032		}
7033		sc.maxPushPromiseID += 2
7034		promisedID := sc.maxPushPromiseID
7035
7036		// http://tools.ietf.org/html/rfc7540#section-8.2.
7037		// Strictly speaking, the new stream should start in "reserved (local)", then
7038		// transition to "half closed (remote)" after sending the initial HEADERS, but
7039		// we start in "half closed (remote)" for simplicity.
7040		// See further comments at the definition of stateHalfClosedRemote.
7041		promised := sc.newStream(promisedID, msg.parent.id, http2stateHalfClosedRemote)
7042		rw, req, err := sc.newWriterAndRequestNoBody(promised, http2requestParam{
7043			method:    msg.method,
7044			scheme:    msg.url.Scheme,
7045			authority: msg.url.Host,
7046			path:      msg.url.RequestURI(),
7047			header:    http2cloneHeader(msg.header), // clone since handler runs concurrently with writing the PUSH_PROMISE
7048		})
7049		if err != nil {
7050			// Should not happen, since we've already validated msg.url.
7051			panic(fmt.Sprintf("newWriterAndRequestNoBody(%+v): %v", msg.url, err))
7052		}
7053
7054		sc.curHandlers++
7055		go sc.runHandler(rw, req, sc.handler.ServeHTTP)
7056		return promisedID, nil
7057	}
7058
7059	sc.writeFrame(http2FrameWriteRequest{
7060		write: &http2writePushPromise{
7061			streamID:           msg.parent.id,
7062			method:             msg.method,
7063			url:                msg.url,
7064			h:                  msg.header,
7065			allocatePromisedID: allocatePromisedID,
7066		},
7067		stream: msg.parent,
7068		done:   msg.done,
7069	})
7070}
7071
7072// foreachHeaderElement splits v according to the "#rule" construction
7073// in RFC 7230 section 7 and calls fn for each non-empty element.
7074func http2foreachHeaderElement(v string, fn func(string)) {
7075	v = textproto.TrimString(v)
7076	if v == "" {
7077		return
7078	}
7079	if !strings.Contains(v, ",") {
7080		fn(v)
7081		return
7082	}
7083	for _, f := range strings.Split(v, ",") {
7084		if f = textproto.TrimString(f); f != "" {
7085			fn(f)
7086		}
7087	}
7088}
7089
7090// From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2
7091var http2connHeaders = []string{
7092	"Connection",
7093	"Keep-Alive",
7094	"Proxy-Connection",
7095	"Transfer-Encoding",
7096	"Upgrade",
7097}
7098
7099// checkValidHTTP2RequestHeaders checks whether h is a valid HTTP/2 request,
7100// per RFC 7540 Section 8.1.2.2.
7101// The returned error is reported to users.
7102func http2checkValidHTTP2RequestHeaders(h Header) error {
7103	for _, k := range http2connHeaders {
7104		if _, ok := h[k]; ok {
7105			return fmt.Errorf("request header %q is not valid in HTTP/2", k)
7106		}
7107	}
7108	te := h["Te"]
7109	if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) {
7110		return errors.New(`request header "TE" may only be "trailers" in HTTP/2`)
7111	}
7112	return nil
7113}
7114
7115func http2new400Handler(err error) HandlerFunc {
7116	return func(w ResponseWriter, r *Request) {
7117		Error(w, err.Error(), StatusBadRequest)
7118	}
7119}
7120
7121// h1ServerKeepAlivesDisabled reports whether hs has its keep-alives
7122// disabled. See comments on h1ServerShutdownChan above for why
7123// the code is written this way.
7124func http2h1ServerKeepAlivesDisabled(hs *Server) bool {
7125	var x interface{} = hs
7126	type I interface {
7127		doKeepAlives() bool
7128	}
7129	if hs, ok := x.(I); ok {
7130		return !hs.doKeepAlives()
7131	}
7132	return false
7133}
7134
7135func (sc *http2serverConn) countError(name string, err error) error {
7136	if sc == nil || sc.srv == nil {
7137		return err
7138	}
7139	f := sc.srv.CountError
7140	if f == nil {
7141		return err
7142	}
7143	var typ string
7144	var code http2ErrCode
7145	switch e := err.(type) {
7146	case http2ConnectionError:
7147		typ = "conn"
7148		code = http2ErrCode(e)
7149	case http2StreamError:
7150		typ = "stream"
7151		code = http2ErrCode(e.Code)
7152	default:
7153		return err
7154	}
7155	codeStr := http2errCodeName[code]
7156	if codeStr == "" {
7157		codeStr = strconv.Itoa(int(code))
7158	}
7159	f(fmt.Sprintf("%s_%s_%s", typ, codeStr, name))
7160	return err
7161}
7162
7163// A timer is a time.Timer, as an interface which can be replaced in tests.
7164type http2timer = interface {
7165	C() <-chan time.Time
7166	Reset(d time.Duration) bool
7167	Stop() bool
7168}
7169
7170// timeTimer adapts a time.Timer to the timer interface.
7171type http2timeTimer struct {
7172	*time.Timer
7173}
7174
7175func (t http2timeTimer) C() <-chan time.Time { return t.Timer.C }
7176
7177const (
7178	// transportDefaultConnFlow is how many connection-level flow control
7179	// tokens we give the server at start-up, past the default 64k.
7180	http2transportDefaultConnFlow = 1 << 30
7181
7182	// transportDefaultStreamFlow is how many stream-level flow
7183	// control tokens we announce to the peer, and how many bytes
7184	// we buffer per stream.
7185	http2transportDefaultStreamFlow = 4 << 20
7186
7187	http2defaultUserAgent = "Go-http-client/2.0"
7188
7189	// initialMaxConcurrentStreams is a connections maxConcurrentStreams until
7190	// it's received servers initial SETTINGS frame, which corresponds with the
7191	// spec's minimum recommended value.
7192	http2initialMaxConcurrentStreams = 100
7193
7194	// defaultMaxConcurrentStreams is a connections default maxConcurrentStreams
7195	// if the server doesn't include one in its initial SETTINGS frame.
7196	http2defaultMaxConcurrentStreams = 1000
7197)
7198
7199// Transport is an HTTP/2 Transport.
7200//
7201// A Transport internally caches connections to servers. It is safe
7202// for concurrent use by multiple goroutines.
7203type http2Transport struct {
7204	// DialTLSContext specifies an optional dial function with context for
7205	// creating TLS connections for requests.
7206	//
7207	// If DialTLSContext and DialTLS is nil, tls.Dial is used.
7208	//
7209	// If the returned net.Conn has a ConnectionState method like tls.Conn,
7210	// it will be used to set http.Response.TLS.
7211	DialTLSContext func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error)
7212
7213	// DialTLS specifies an optional dial function for creating
7214	// TLS connections for requests.
7215	//
7216	// If DialTLSContext and DialTLS is nil, tls.Dial is used.
7217	//
7218	// Deprecated: Use DialTLSContext instead, which allows the transport
7219	// to cancel dials as soon as they are no longer needed.
7220	// If both are set, DialTLSContext takes priority.
7221	DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
7222
7223	// TLSClientConfig specifies the TLS configuration to use with
7224	// tls.Client. If nil, the default configuration is used.
7225	TLSClientConfig *tls.Config
7226
7227	// ConnPool optionally specifies an alternate connection pool to use.
7228	// If nil, the default is used.
7229	ConnPool http2ClientConnPool
7230
7231	// DisableCompression, if true, prevents the Transport from
7232	// requesting compression with an "Accept-Encoding: gzip"
7233	// request header when the Request contains no existing
7234	// Accept-Encoding value. If the Transport requests gzip on
7235	// its own and gets a gzipped response, it's transparently
7236	// decoded in the Response.Body. However, if the user
7237	// explicitly requested gzip it is not automatically
7238	// uncompressed.
7239	DisableCompression bool
7240
7241	// AllowHTTP, if true, permits HTTP/2 requests using the insecure,
7242	// plain-text "http" scheme. Note that this does not enable h2c support.
7243	AllowHTTP bool
7244
7245	// MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to
7246	// send in the initial settings frame. It is how many bytes
7247	// of response headers are allowed. Unlike the http2 spec, zero here
7248	// means to use a default limit (currently 10MB). If you actually
7249	// want to advertise an unlimited value to the peer, Transport
7250	// interprets the highest possible value here (0xffffffff or 1<<32-1)
7251	// to mean no limit.
7252	MaxHeaderListSize uint32
7253
7254	// MaxReadFrameSize is the http2 SETTINGS_MAX_FRAME_SIZE to send in the
7255	// initial settings frame. It is the size in bytes of the largest frame
7256	// payload that the sender is willing to receive. If 0, no setting is
7257	// sent, and the value is provided by the peer, which should be 16384
7258	// according to the spec:
7259	// https://datatracker.ietf.org/doc/html/rfc7540#section-6.5.2.
7260	// Values are bounded in the range 16k to 16M.
7261	MaxReadFrameSize uint32
7262
7263	// MaxDecoderHeaderTableSize optionally specifies the http2
7264	// SETTINGS_HEADER_TABLE_SIZE to send in the initial settings frame. It
7265	// informs the remote endpoint of the maximum size of the header compression
7266	// table used to decode header blocks, in octets. If zero, the default value
7267	// of 4096 is used.
7268	MaxDecoderHeaderTableSize uint32
7269
7270	// MaxEncoderHeaderTableSize optionally specifies an upper limit for the
7271	// header compression table used for encoding request headers. Received
7272	// SETTINGS_HEADER_TABLE_SIZE settings are capped at this limit. If zero,
7273	// the default value of 4096 is used.
7274	MaxEncoderHeaderTableSize uint32
7275
7276	// StrictMaxConcurrentStreams controls whether the server's
7277	// SETTINGS_MAX_CONCURRENT_STREAMS should be respected
7278	// globally. If false, new TCP connections are created to the
7279	// server as needed to keep each under the per-connection
7280	// SETTINGS_MAX_CONCURRENT_STREAMS limit. If true, the
7281	// server's SETTINGS_MAX_CONCURRENT_STREAMS is interpreted as
7282	// a global limit and callers of RoundTrip block when needed,
7283	// waiting for their turn.
7284	StrictMaxConcurrentStreams bool
7285
7286	// IdleConnTimeout is the maximum amount of time an idle
7287	// (keep-alive) connection will remain idle before closing
7288	// itself.
7289	// Zero means no limit.
7290	IdleConnTimeout time.Duration
7291
7292	// ReadIdleTimeout is the timeout after which a health check using ping
7293	// frame will be carried out if no frame is received on the connection.
7294	// Note that a ping response will is considered a received frame, so if
7295	// there is no other traffic on the connection, the health check will
7296	// be performed every ReadIdleTimeout interval.
7297	// If zero, no health check is performed.
7298	ReadIdleTimeout time.Duration
7299
7300	// PingTimeout is the timeout after which the connection will be closed
7301	// if a response to Ping is not received.
7302	// Defaults to 15s.
7303	PingTimeout time.Duration
7304
7305	// WriteByteTimeout is the timeout after which the connection will be
7306	// closed no data can be written to it. The timeout begins when data is
7307	// available to write, and is extended whenever any bytes are written.
7308	WriteByteTimeout time.Duration
7309
7310	// CountError, if non-nil, is called on HTTP/2 transport errors.
7311	// It's intended to increment a metric for monitoring, such
7312	// as an expvar or Prometheus metric.
7313	// The errType consists of only ASCII word characters.
7314	CountError func(errType string)
7315
7316	// t1, if non-nil, is the standard library Transport using
7317	// this transport. Its settings are used (but not its
7318	// RoundTrip method, etc).
7319	t1 *Transport
7320
7321	connPoolOnce  sync.Once
7322	connPoolOrDef http2ClientConnPool // non-nil version of ConnPool
7323
7324	*http2transportTestHooks
7325}
7326
7327// Hook points used for testing.
7328// Outside of tests, t.transportTestHooks is nil and these all have minimal implementations.
7329// Inside tests, see the testSyncHooks function docs.
7330
7331type http2transportTestHooks struct {
7332	newclientconn func(*http2ClientConn)
7333	group         http2synctestGroupInterface
7334}
7335
7336func (t *http2Transport) markNewGoroutine() {
7337	if t != nil && t.http2transportTestHooks != nil {
7338		t.http2transportTestHooks.group.Join()
7339	}
7340}
7341
7342// newTimer creates a new time.Timer, or a synthetic timer in tests.
7343func (t *http2Transport) newTimer(d time.Duration) http2timer {
7344	if t.http2transportTestHooks != nil {
7345		return t.http2transportTestHooks.group.NewTimer(d)
7346	}
7347	return http2timeTimer{time.NewTimer(d)}
7348}
7349
7350// afterFunc creates a new time.AfterFunc timer, or a synthetic timer in tests.
7351func (t *http2Transport) afterFunc(d time.Duration, f func()) http2timer {
7352	if t.http2transportTestHooks != nil {
7353		return t.http2transportTestHooks.group.AfterFunc(d, f)
7354	}
7355	return http2timeTimer{time.AfterFunc(d, f)}
7356}
7357
7358func (t *http2Transport) contextWithTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc) {
7359	if t.http2transportTestHooks != nil {
7360		return t.http2transportTestHooks.group.ContextWithTimeout(ctx, d)
7361	}
7362	return context.WithTimeout(ctx, d)
7363}
7364
7365func (t *http2Transport) maxHeaderListSize() uint32 {
7366	if t.MaxHeaderListSize == 0 {
7367		return 10 << 20
7368	}
7369	if t.MaxHeaderListSize == 0xffffffff {
7370		return 0
7371	}
7372	return t.MaxHeaderListSize
7373}
7374
7375func (t *http2Transport) maxFrameReadSize() uint32 {
7376	if t.MaxReadFrameSize == 0 {
7377		return 0 // use the default provided by the peer
7378	}
7379	if t.MaxReadFrameSize < http2minMaxFrameSize {
7380		return http2minMaxFrameSize
7381	}
7382	if t.MaxReadFrameSize > http2maxFrameSize {
7383		return http2maxFrameSize
7384	}
7385	return t.MaxReadFrameSize
7386}
7387
7388func (t *http2Transport) disableCompression() bool {
7389	return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression)
7390}
7391
7392func (t *http2Transport) pingTimeout() time.Duration {
7393	if t.PingTimeout == 0 {
7394		return 15 * time.Second
7395	}
7396	return t.PingTimeout
7397
7398}
7399
7400// ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
7401// It returns an error if t1 has already been HTTP/2-enabled.
7402//
7403// Use ConfigureTransports instead to configure the HTTP/2 Transport.
7404func http2ConfigureTransport(t1 *Transport) error {
7405	_, err := http2ConfigureTransports(t1)
7406	return err
7407}
7408
7409// ConfigureTransports configures a net/http HTTP/1 Transport to use HTTP/2.
7410// It returns a new HTTP/2 Transport for further configuration.
7411// It returns an error if t1 has already been HTTP/2-enabled.
7412func http2ConfigureTransports(t1 *Transport) (*http2Transport, error) {
7413	return http2configureTransports(t1)
7414}
7415
7416func http2configureTransports(t1 *Transport) (*http2Transport, error) {
7417	connPool := new(http2clientConnPool)
7418	t2 := &http2Transport{
7419		ConnPool: http2noDialClientConnPool{connPool},
7420		t1:       t1,
7421	}
7422	connPool.t = t2
7423	if err := http2registerHTTPSProtocol(t1, http2noDialH2RoundTripper{t2}); err != nil {
7424		return nil, err
7425	}
7426	if t1.TLSClientConfig == nil {
7427		t1.TLSClientConfig = new(tls.Config)
7428	}
7429	if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
7430		t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
7431	}
7432	if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") {
7433		t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1")
7434	}
7435	upgradeFn := func(authority string, c *tls.Conn) RoundTripper {
7436		addr := http2authorityAddr("https", authority)
7437		if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil {
7438			go c.Close()
7439			return http2erringRoundTripper{err}
7440		} else if !used {
7441			// Turns out we don't need this c.
7442			// For example, two goroutines made requests to the same host
7443			// at the same time, both kicking off TCP dials. (since protocol
7444			// was unknown)
7445			go c.Close()
7446		}
7447		return t2
7448	}
7449	if m := t1.TLSNextProto; len(m) == 0 {
7450		t1.TLSNextProto = map[string]func(string, *tls.Conn) RoundTripper{
7451			"h2": upgradeFn,
7452		}
7453	} else {
7454		m["h2"] = upgradeFn
7455	}
7456	return t2, nil
7457}
7458
7459func (t *http2Transport) connPool() http2ClientConnPool {
7460	t.connPoolOnce.Do(t.initConnPool)
7461	return t.connPoolOrDef
7462}
7463
7464func (t *http2Transport) initConnPool() {
7465	if t.ConnPool != nil {
7466		t.connPoolOrDef = t.ConnPool
7467	} else {
7468		t.connPoolOrDef = &http2clientConnPool{t: t}
7469	}
7470}
7471
7472// ClientConn is the state of a single HTTP/2 client connection to an
7473// HTTP/2 server.
7474type http2ClientConn struct {
7475	t             *http2Transport
7476	tconn         net.Conn             // usually *tls.Conn, except specialized impls
7477	tlsState      *tls.ConnectionState // nil only for specialized impls
7478	reused        uint32               // whether conn is being reused; atomic
7479	singleUse     bool                 // whether being used for a single http.Request
7480	getConnCalled bool                 // used by clientConnPool
7481
7482	// readLoop goroutine fields:
7483	readerDone chan struct{} // closed on error
7484	readerErr  error         // set before readerDone is closed
7485
7486	idleTimeout time.Duration // or 0 for never
7487	idleTimer   http2timer
7488
7489	mu              sync.Mutex   // guards following
7490	cond            *sync.Cond   // hold mu; broadcast on flow/closed changes
7491	flow            http2outflow // our conn-level flow control quota (cs.outflow is per stream)
7492	inflow          http2inflow  // peer's conn-level flow control
7493	doNotReuse      bool         // whether conn is marked to not be reused for any future requests
7494	closing         bool
7495	closed          bool
7496	seenSettings    bool                          // true if we've seen a settings frame, false otherwise
7497	wantSettingsAck bool                          // we sent a SETTINGS frame and haven't heard back
7498	goAway          *http2GoAwayFrame             // if non-nil, the GoAwayFrame we received
7499	goAwayDebug     string                        // goAway frame's debug data, retained as a string
7500	streams         map[uint32]*http2clientStream // client-initiated
7501	streamsReserved int                           // incr by ReserveNewRequest; decr on RoundTrip
7502	nextStreamID    uint32
7503	pendingRequests int                       // requests blocked and waiting to be sent because len(streams) == maxConcurrentStreams
7504	pings           map[[8]byte]chan struct{} // in flight ping data to notification channel
7505	br              *bufio.Reader
7506	lastActive      time.Time
7507	lastIdle        time.Time // time last idle
7508	// Settings from peer: (also guarded by wmu)
7509	maxFrameSize           uint32
7510	maxConcurrentStreams   uint32
7511	peerMaxHeaderListSize  uint64
7512	peerMaxHeaderTableSize uint32
7513	initialWindowSize      uint32
7514
7515	// reqHeaderMu is a 1-element semaphore channel controlling access to sending new requests.
7516	// Write to reqHeaderMu to lock it, read from it to unlock.
7517	// Lock reqmu BEFORE mu or wmu.
7518	reqHeaderMu chan struct{}
7519
7520	// wmu is held while writing.
7521	// Acquire BEFORE mu when holding both, to avoid blocking mu on network writes.
7522	// Only acquire both at the same time when changing peer settings.
7523	wmu  sync.Mutex
7524	bw   *bufio.Writer
7525	fr   *http2Framer
7526	werr error        // first write error that has occurred
7527	hbuf bytes.Buffer // HPACK encoder writes into this
7528	henc *hpack.Encoder
7529}
7530
7531// clientStream is the state for a single HTTP/2 stream. One of these
7532// is created for each Transport.RoundTrip call.
7533type http2clientStream struct {
7534	cc *http2ClientConn
7535
7536	// Fields of Request that we may access even after the response body is closed.
7537	ctx       context.Context
7538	reqCancel <-chan struct{}
7539
7540	trace         *httptrace.ClientTrace // or nil
7541	ID            uint32
7542	bufPipe       http2pipe // buffered pipe with the flow-controlled response payload
7543	requestedGzip bool
7544	isHead        bool
7545
7546	abortOnce sync.Once
7547	abort     chan struct{} // closed to signal stream should end immediately
7548	abortErr  error         // set if abort is closed
7549
7550	peerClosed chan struct{} // closed when the peer sends an END_STREAM flag
7551	donec      chan struct{} // closed after the stream is in the closed state
7552	on100      chan struct{} // buffered; written to if a 100 is received
7553
7554	respHeaderRecv chan struct{} // closed when headers are received
7555	res            *Response     // set if respHeaderRecv is closed
7556
7557	flow        http2outflow // guarded by cc.mu
7558	inflow      http2inflow  // guarded by cc.mu
7559	bytesRemain int64        // -1 means unknown; owned by transportResponseBody.Read
7560	readErr     error        // sticky read error; owned by transportResponseBody.Read
7561
7562	reqBody              io.ReadCloser
7563	reqBodyContentLength int64         // -1 means unknown
7564	reqBodyClosed        chan struct{} // guarded by cc.mu; non-nil on Close, closed when done
7565
7566	// owned by writeRequest:
7567	sentEndStream bool // sent an END_STREAM flag to the peer
7568	sentHeaders   bool
7569
7570	// owned by clientConnReadLoop:
7571	firstByte    bool  // got the first response byte
7572	pastHeaders  bool  // got first MetaHeadersFrame (actual headers)
7573	pastTrailers bool  // got optional second MetaHeadersFrame (trailers)
7574	num1xx       uint8 // number of 1xx responses seen
7575	readClosed   bool  // peer sent an END_STREAM flag
7576	readAborted  bool  // read loop reset the stream
7577
7578	trailer    Header  // accumulated trailers
7579	resTrailer *Header // client's Response.Trailer
7580}
7581
7582var http2got1xxFuncForTests func(int, textproto.MIMEHeader) error
7583
7584// get1xxTraceFunc returns the value of request's httptrace.ClientTrace.Got1xxResponse func,
7585// if any. It returns nil if not set or if the Go version is too old.
7586func (cs *http2clientStream) get1xxTraceFunc() func(int, textproto.MIMEHeader) error {
7587	if fn := http2got1xxFuncForTests; fn != nil {
7588		return fn
7589	}
7590	return http2traceGot1xxResponseFunc(cs.trace)
7591}
7592
7593func (cs *http2clientStream) abortStream(err error) {
7594	cs.cc.mu.Lock()
7595	defer cs.cc.mu.Unlock()
7596	cs.abortStreamLocked(err)
7597}
7598
7599func (cs *http2clientStream) abortStreamLocked(err error) {
7600	cs.abortOnce.Do(func() {
7601		cs.abortErr = err
7602		close(cs.abort)
7603	})
7604	if cs.reqBody != nil {
7605		cs.closeReqBodyLocked()
7606	}
7607	// TODO(dneil): Clean up tests where cs.cc.cond is nil.
7608	if cs.cc.cond != nil {
7609		// Wake up writeRequestBody if it is waiting on flow control.
7610		cs.cc.cond.Broadcast()
7611	}
7612}
7613
7614func (cs *http2clientStream) abortRequestBodyWrite() {
7615	cc := cs.cc
7616	cc.mu.Lock()
7617	defer cc.mu.Unlock()
7618	if cs.reqBody != nil && cs.reqBodyClosed == nil {
7619		cs.closeReqBodyLocked()
7620		cc.cond.Broadcast()
7621	}
7622}
7623
7624func (cs *http2clientStream) closeReqBodyLocked() {
7625	if cs.reqBodyClosed != nil {
7626		return
7627	}
7628	cs.reqBodyClosed = make(chan struct{})
7629	reqBodyClosed := cs.reqBodyClosed
7630	go func() {
7631		cs.cc.t.markNewGoroutine()
7632		cs.reqBody.Close()
7633		close(reqBodyClosed)
7634	}()
7635}
7636
7637type http2stickyErrWriter struct {
7638	conn    net.Conn
7639	timeout time.Duration
7640	err     *error
7641}
7642
7643func (sew http2stickyErrWriter) Write(p []byte) (n int, err error) {
7644	if *sew.err != nil {
7645		return 0, *sew.err
7646	}
7647	for {
7648		if sew.timeout != 0 {
7649			sew.conn.SetWriteDeadline(time.Now().Add(sew.timeout))
7650		}
7651		nn, err := sew.conn.Write(p[n:])
7652		n += nn
7653		if n < len(p) && nn > 0 && errors.Is(err, os.ErrDeadlineExceeded) {
7654			// Keep extending the deadline so long as we're making progress.
7655			continue
7656		}
7657		if sew.timeout != 0 {
7658			sew.conn.SetWriteDeadline(time.Time{})
7659		}
7660		*sew.err = err
7661		return n, err
7662	}
7663}
7664
7665// noCachedConnError is the concrete type of ErrNoCachedConn, which
7666// needs to be detected by net/http regardless of whether it's its
7667// bundled version (in h2_bundle.go with a rewritten type name) or
7668// from a user's x/net/http2. As such, as it has a unique method name
7669// (IsHTTP2NoCachedConnError) that net/http sniffs for via func
7670// isNoCachedConnError.
7671type http2noCachedConnError struct{}
7672
7673func (http2noCachedConnError) IsHTTP2NoCachedConnError() {}
7674
7675func (http2noCachedConnError) Error() string { return "http2: no cached connection was available" }
7676
7677// isNoCachedConnError reports whether err is of type noCachedConnError
7678// or its equivalent renamed type in net/http2's h2_bundle.go. Both types
7679// may coexist in the same running program.
7680func http2isNoCachedConnError(err error) bool {
7681	_, ok := err.(interface{ IsHTTP2NoCachedConnError() })
7682	return ok
7683}
7684
7685var http2ErrNoCachedConn error = http2noCachedConnError{}
7686
7687// RoundTripOpt are options for the Transport.RoundTripOpt method.
7688type http2RoundTripOpt struct {
7689	// OnlyCachedConn controls whether RoundTripOpt may
7690	// create a new TCP connection. If set true and
7691	// no cached connection is available, RoundTripOpt
7692	// will return ErrNoCachedConn.
7693	OnlyCachedConn bool
7694}
7695
7696func (t *http2Transport) RoundTrip(req *Request) (*Response, error) {
7697	return t.RoundTripOpt(req, http2RoundTripOpt{})
7698}
7699
7700// authorityAddr returns a given authority (a host/IP, or host:port / ip:port)
7701// and returns a host:port. The port 443 is added if needed.
7702func http2authorityAddr(scheme string, authority string) (addr string) {
7703	host, port, err := net.SplitHostPort(authority)
7704	if err != nil { // authority didn't have a port
7705		host = authority
7706		port = ""
7707	}
7708	if port == "" { // authority's port was empty
7709		port = "443"
7710		if scheme == "http" {
7711			port = "80"
7712		}
7713	}
7714	if a, err := idna.ToASCII(host); err == nil {
7715		host = a
7716	}
7717	// IPv6 address literal, without a port:
7718	if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
7719		return host + ":" + port
7720	}
7721	return net.JoinHostPort(host, port)
7722}
7723
7724// RoundTripOpt is like RoundTrip, but takes options.
7725func (t *http2Transport) RoundTripOpt(req *Request, opt http2RoundTripOpt) (*Response, error) {
7726	if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) {
7727		return nil, errors.New("http2: unsupported scheme")
7728	}
7729
7730	addr := http2authorityAddr(req.URL.Scheme, req.URL.Host)
7731	for retry := 0; ; retry++ {
7732		cc, err := t.connPool().GetClientConn(req, addr)
7733		if err != nil {
7734			t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err)
7735			return nil, err
7736		}
7737		reused := !atomic.CompareAndSwapUint32(&cc.reused, 0, 1)
7738		http2traceGotConn(req, cc, reused)
7739		res, err := cc.RoundTrip(req)
7740		if err != nil && retry <= 6 {
7741			roundTripErr := err
7742			if req, err = http2shouldRetryRequest(req, err); err == nil {
7743				// After the first retry, do exponential backoff with 10% jitter.
7744				if retry == 0 {
7745					t.vlogf("RoundTrip retrying after failure: %v", roundTripErr)
7746					continue
7747				}
7748				backoff := float64(uint(1) << (uint(retry) - 1))
7749				backoff += backoff * (0.1 * mathrand.Float64())
7750				d := time.Second * time.Duration(backoff)
7751				tm := t.newTimer(d)
7752				select {
7753				case <-tm.C():
7754					t.vlogf("RoundTrip retrying after failure: %v", roundTripErr)
7755					continue
7756				case <-req.Context().Done():
7757					tm.Stop()
7758					err = req.Context().Err()
7759				}
7760			}
7761		}
7762		if err != nil {
7763			t.vlogf("RoundTrip failure: %v", err)
7764			return nil, err
7765		}
7766		return res, nil
7767	}
7768}
7769
7770// CloseIdleConnections closes any connections which were previously
7771// connected from previous requests but are now sitting idle.
7772// It does not interrupt any connections currently in use.
7773func (t *http2Transport) CloseIdleConnections() {
7774	if cp, ok := t.connPool().(http2clientConnPoolIdleCloser); ok {
7775		cp.closeIdleConnections()
7776	}
7777}
7778
7779var (
7780	http2errClientConnClosed    = errors.New("http2: client conn is closed")
7781	http2errClientConnUnusable  = errors.New("http2: client conn not usable")
7782	http2errClientConnGotGoAway = errors.New("http2: Transport received Server's graceful shutdown GOAWAY")
7783)
7784
7785// shouldRetryRequest is called by RoundTrip when a request fails to get
7786// response headers. It is always called with a non-nil error.
7787// It returns either a request to retry (either the same request, or a
7788// modified clone), or an error if the request can't be replayed.
7789func http2shouldRetryRequest(req *Request, err error) (*Request, error) {
7790	if !http2canRetryError(err) {
7791		return nil, err
7792	}
7793	// If the Body is nil (or http.NoBody), it's safe to reuse
7794	// this request and its Body.
7795	if req.Body == nil || req.Body == NoBody {
7796		return req, nil
7797	}
7798
7799	// If the request body can be reset back to its original
7800	// state via the optional req.GetBody, do that.
7801	if req.GetBody != nil {
7802		body, err := req.GetBody()
7803		if err != nil {
7804			return nil, err
7805		}
7806		newReq := *req
7807		newReq.Body = body
7808		return &newReq, nil
7809	}
7810
7811	// The Request.Body can't reset back to the beginning, but we
7812	// don't seem to have started to read from it yet, so reuse
7813	// the request directly.
7814	if err == http2errClientConnUnusable {
7815		return req, nil
7816	}
7817
7818	return nil, fmt.Errorf("http2: Transport: cannot retry err [%v] after Request.Body was written; define Request.GetBody to avoid this error", err)
7819}
7820
7821func http2canRetryError(err error) bool {
7822	if err == http2errClientConnUnusable || err == http2errClientConnGotGoAway {
7823		return true
7824	}
7825	if se, ok := err.(http2StreamError); ok {
7826		if se.Code == http2ErrCodeProtocol && se.Cause == http2errFromPeer {
7827			// See golang/go#47635, golang/go#42777
7828			return true
7829		}
7830		return se.Code == http2ErrCodeRefusedStream
7831	}
7832	return false
7833}
7834
7835func (t *http2Transport) dialClientConn(ctx context.Context, addr string, singleUse bool) (*http2ClientConn, error) {
7836	if t.http2transportTestHooks != nil {
7837		return t.newClientConn(nil, singleUse)
7838	}
7839	host, _, err := net.SplitHostPort(addr)
7840	if err != nil {
7841		return nil, err
7842	}
7843	tconn, err := t.dialTLS(ctx, "tcp", addr, t.newTLSConfig(host))
7844	if err != nil {
7845		return nil, err
7846	}
7847	return t.newClientConn(tconn, singleUse)
7848}
7849
7850func (t *http2Transport) newTLSConfig(host string) *tls.Config {
7851	cfg := new(tls.Config)
7852	if t.TLSClientConfig != nil {
7853		*cfg = *t.TLSClientConfig.Clone()
7854	}
7855	if !http2strSliceContains(cfg.NextProtos, http2NextProtoTLS) {
7856		cfg.NextProtos = append([]string{http2NextProtoTLS}, cfg.NextProtos...)
7857	}
7858	if cfg.ServerName == "" {
7859		cfg.ServerName = host
7860	}
7861	return cfg
7862}
7863
7864func (t *http2Transport) dialTLS(ctx context.Context, network, addr string, tlsCfg *tls.Config) (net.Conn, error) {
7865	if t.DialTLSContext != nil {
7866		return t.DialTLSContext(ctx, network, addr, tlsCfg)
7867	} else if t.DialTLS != nil {
7868		return t.DialTLS(network, addr, tlsCfg)
7869	}
7870
7871	tlsCn, err := t.dialTLSWithContext(ctx, network, addr, tlsCfg)
7872	if err != nil {
7873		return nil, err
7874	}
7875	state := tlsCn.ConnectionState()
7876	if p := state.NegotiatedProtocol; p != http2NextProtoTLS {
7877		return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, http2NextProtoTLS)
7878	}
7879	if !state.NegotiatedProtocolIsMutual {
7880		return nil, errors.New("http2: could not negotiate protocol mutually")
7881	}
7882	return tlsCn, nil
7883}
7884
7885// disableKeepAlives reports whether connections should be closed as
7886// soon as possible after handling the first request.
7887func (t *http2Transport) disableKeepAlives() bool {
7888	return t.t1 != nil && t.t1.DisableKeepAlives
7889}
7890
7891func (t *http2Transport) expectContinueTimeout() time.Duration {
7892	if t.t1 == nil {
7893		return 0
7894	}
7895	return t.t1.ExpectContinueTimeout
7896}
7897
7898func (t *http2Transport) maxDecoderHeaderTableSize() uint32 {
7899	if v := t.MaxDecoderHeaderTableSize; v > 0 {
7900		return v
7901	}
7902	return http2initialHeaderTableSize
7903}
7904
7905func (t *http2Transport) maxEncoderHeaderTableSize() uint32 {
7906	if v := t.MaxEncoderHeaderTableSize; v > 0 {
7907		return v
7908	}
7909	return http2initialHeaderTableSize
7910}
7911
7912func (t *http2Transport) NewClientConn(c net.Conn) (*http2ClientConn, error) {
7913	return t.newClientConn(c, t.disableKeepAlives())
7914}
7915
7916func (t *http2Transport) newClientConn(c net.Conn, singleUse bool) (*http2ClientConn, error) {
7917	cc := &http2ClientConn{
7918		t:                     t,
7919		tconn:                 c,
7920		readerDone:            make(chan struct{}),
7921		nextStreamID:          1,
7922		maxFrameSize:          16 << 10,                         // spec default
7923		initialWindowSize:     65535,                            // spec default
7924		maxConcurrentStreams:  http2initialMaxConcurrentStreams, // "infinite", per spec. Use a smaller value until we have received server settings.
7925		peerMaxHeaderListSize: 0xffffffffffffffff,               // "infinite", per spec. Use 2^64-1 instead.
7926		streams:               make(map[uint32]*http2clientStream),
7927		singleUse:             singleUse,
7928		wantSettingsAck:       true,
7929		pings:                 make(map[[8]byte]chan struct{}),
7930		reqHeaderMu:           make(chan struct{}, 1),
7931	}
7932	if t.http2transportTestHooks != nil {
7933		t.markNewGoroutine()
7934		t.http2transportTestHooks.newclientconn(cc)
7935		c = cc.tconn
7936	}
7937	if http2VerboseLogs {
7938		t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr())
7939	}
7940
7941	cc.cond = sync.NewCond(&cc.mu)
7942	cc.flow.add(int32(http2initialWindowSize))
7943
7944	// TODO: adjust this writer size to account for frame size +
7945	// MTU + crypto/tls record padding.
7946	cc.bw = bufio.NewWriter(http2stickyErrWriter{
7947		conn:    c,
7948		timeout: t.WriteByteTimeout,
7949		err:     &cc.werr,
7950	})
7951	cc.br = bufio.NewReader(c)
7952	cc.fr = http2NewFramer(cc.bw, cc.br)
7953	if t.maxFrameReadSize() != 0 {
7954		cc.fr.SetMaxReadFrameSize(t.maxFrameReadSize())
7955	}
7956	if t.CountError != nil {
7957		cc.fr.countError = t.CountError
7958	}
7959	maxHeaderTableSize := t.maxDecoderHeaderTableSize()
7960	cc.fr.ReadMetaHeaders = hpack.NewDecoder(maxHeaderTableSize, nil)
7961	cc.fr.MaxHeaderListSize = t.maxHeaderListSize()
7962
7963	cc.henc = hpack.NewEncoder(&cc.hbuf)
7964	cc.henc.SetMaxDynamicTableSizeLimit(t.maxEncoderHeaderTableSize())
7965	cc.peerMaxHeaderTableSize = http2initialHeaderTableSize
7966
7967	if t.AllowHTTP {
7968		cc.nextStreamID = 3
7969	}
7970
7971	if cs, ok := c.(http2connectionStater); ok {
7972		state := cs.ConnectionState()
7973		cc.tlsState = &state
7974	}
7975
7976	initialSettings := []http2Setting{
7977		{ID: http2SettingEnablePush, Val: 0},
7978		{ID: http2SettingInitialWindowSize, Val: http2transportDefaultStreamFlow},
7979	}
7980	if max := t.maxFrameReadSize(); max != 0 {
7981		initialSettings = append(initialSettings, http2Setting{ID: http2SettingMaxFrameSize, Val: max})
7982	}
7983	if max := t.maxHeaderListSize(); max != 0 {
7984		initialSettings = append(initialSettings, http2Setting{ID: http2SettingMaxHeaderListSize, Val: max})
7985	}
7986	if maxHeaderTableSize != http2initialHeaderTableSize {
7987		initialSettings = append(initialSettings, http2Setting{ID: http2SettingHeaderTableSize, Val: maxHeaderTableSize})
7988	}
7989
7990	cc.bw.Write(http2clientPreface)
7991	cc.fr.WriteSettings(initialSettings...)
7992	cc.fr.WriteWindowUpdate(0, http2transportDefaultConnFlow)
7993	cc.inflow.init(http2transportDefaultConnFlow + http2initialWindowSize)
7994	cc.bw.Flush()
7995	if cc.werr != nil {
7996		cc.Close()
7997		return nil, cc.werr
7998	}
7999
8000	// Start the idle timer after the connection is fully initialized.
8001	if d := t.idleConnTimeout(); d != 0 {
8002		cc.idleTimeout = d
8003		cc.idleTimer = t.afterFunc(d, cc.onIdleTimeout)
8004	}
8005
8006	go cc.readLoop()
8007	return cc, nil
8008}
8009
8010func (cc *http2ClientConn) healthCheck() {
8011	pingTimeout := cc.t.pingTimeout()
8012	// We don't need to periodically ping in the health check, because the readLoop of ClientConn will
8013	// trigger the healthCheck again if there is no frame received.
8014	ctx, cancel := cc.t.contextWithTimeout(context.Background(), pingTimeout)
8015	defer cancel()
8016	cc.vlogf("http2: Transport sending health check")
8017	err := cc.Ping(ctx)
8018	if err != nil {
8019		cc.vlogf("http2: Transport health check failure: %v", err)
8020		cc.closeForLostPing()
8021	} else {
8022		cc.vlogf("http2: Transport health check success")
8023	}
8024}
8025
8026// SetDoNotReuse marks cc as not reusable for future HTTP requests.
8027func (cc *http2ClientConn) SetDoNotReuse() {
8028	cc.mu.Lock()
8029	defer cc.mu.Unlock()
8030	cc.doNotReuse = true
8031}
8032
8033func (cc *http2ClientConn) setGoAway(f *http2GoAwayFrame) {
8034	cc.mu.Lock()
8035	defer cc.mu.Unlock()
8036
8037	old := cc.goAway
8038	cc.goAway = f
8039
8040	// Merge the previous and current GoAway error frames.
8041	if cc.goAwayDebug == "" {
8042		cc.goAwayDebug = string(f.DebugData())
8043	}
8044	if old != nil && old.ErrCode != http2ErrCodeNo {
8045		cc.goAway.ErrCode = old.ErrCode
8046	}
8047	last := f.LastStreamID
8048	for streamID, cs := range cc.streams {
8049		if streamID <= last {
8050			// The server's GOAWAY indicates that it received this stream.
8051			// It will either finish processing it, or close the connection
8052			// without doing so. Either way, leave the stream alone for now.
8053			continue
8054		}
8055		if streamID == 1 && cc.goAway.ErrCode != http2ErrCodeNo {
8056			// Don't retry the first stream on a connection if we get a non-NO error.
8057			// If the server is sending an error on a new connection,
8058			// retrying the request on a new one probably isn't going to work.
8059			cs.abortStreamLocked(fmt.Errorf("http2: Transport received GOAWAY from server ErrCode:%v", cc.goAway.ErrCode))
8060		} else {
8061			// Aborting the stream with errClentConnGotGoAway indicates that
8062			// the request should be retried on a new connection.
8063			cs.abortStreamLocked(http2errClientConnGotGoAway)
8064		}
8065	}
8066}
8067
8068// CanTakeNewRequest reports whether the connection can take a new request,
8069// meaning it has not been closed or received or sent a GOAWAY.
8070//
8071// If the caller is going to immediately make a new request on this
8072// connection, use ReserveNewRequest instead.
8073func (cc *http2ClientConn) CanTakeNewRequest() bool {
8074	cc.mu.Lock()
8075	defer cc.mu.Unlock()
8076	return cc.canTakeNewRequestLocked()
8077}
8078
8079// ReserveNewRequest is like CanTakeNewRequest but also reserves a
8080// concurrent stream in cc. The reservation is decremented on the
8081// next call to RoundTrip.
8082func (cc *http2ClientConn) ReserveNewRequest() bool {
8083	cc.mu.Lock()
8084	defer cc.mu.Unlock()
8085	if st := cc.idleStateLocked(); !st.canTakeNewRequest {
8086		return false
8087	}
8088	cc.streamsReserved++
8089	return true
8090}
8091
8092// ClientConnState describes the state of a ClientConn.
8093type http2ClientConnState struct {
8094	// Closed is whether the connection is closed.
8095	Closed bool
8096
8097	// Closing is whether the connection is in the process of
8098	// closing. It may be closing due to shutdown, being a
8099	// single-use connection, being marked as DoNotReuse, or
8100	// having received a GOAWAY frame.
8101	Closing bool
8102
8103	// StreamsActive is how many streams are active.
8104	StreamsActive int
8105
8106	// StreamsReserved is how many streams have been reserved via
8107	// ClientConn.ReserveNewRequest.
8108	StreamsReserved int
8109
8110	// StreamsPending is how many requests have been sent in excess
8111	// of the peer's advertised MaxConcurrentStreams setting and
8112	// are waiting for other streams to complete.
8113	StreamsPending int
8114
8115	// MaxConcurrentStreams is how many concurrent streams the
8116	// peer advertised as acceptable. Zero means no SETTINGS
8117	// frame has been received yet.
8118	MaxConcurrentStreams uint32
8119
8120	// LastIdle, if non-zero, is when the connection last
8121	// transitioned to idle state.
8122	LastIdle time.Time
8123}
8124
8125// State returns a snapshot of cc's state.
8126func (cc *http2ClientConn) State() http2ClientConnState {
8127	cc.wmu.Lock()
8128	maxConcurrent := cc.maxConcurrentStreams
8129	if !cc.seenSettings {
8130		maxConcurrent = 0
8131	}
8132	cc.wmu.Unlock()
8133
8134	cc.mu.Lock()
8135	defer cc.mu.Unlock()
8136	return http2ClientConnState{
8137		Closed:               cc.closed,
8138		Closing:              cc.closing || cc.singleUse || cc.doNotReuse || cc.goAway != nil,
8139		StreamsActive:        len(cc.streams),
8140		StreamsReserved:      cc.streamsReserved,
8141		StreamsPending:       cc.pendingRequests,
8142		LastIdle:             cc.lastIdle,
8143		MaxConcurrentStreams: maxConcurrent,
8144	}
8145}
8146
8147// clientConnIdleState describes the suitability of a client
8148// connection to initiate a new RoundTrip request.
8149type http2clientConnIdleState struct {
8150	canTakeNewRequest bool
8151}
8152
8153func (cc *http2ClientConn) idleState() http2clientConnIdleState {
8154	cc.mu.Lock()
8155	defer cc.mu.Unlock()
8156	return cc.idleStateLocked()
8157}
8158
8159func (cc *http2ClientConn) idleStateLocked() (st http2clientConnIdleState) {
8160	if cc.singleUse && cc.nextStreamID > 1 {
8161		return
8162	}
8163	var maxConcurrentOkay bool
8164	if cc.t.StrictMaxConcurrentStreams {
8165		// We'll tell the caller we can take a new request to
8166		// prevent the caller from dialing a new TCP
8167		// connection, but then we'll block later before
8168		// writing it.
8169		maxConcurrentOkay = true
8170	} else {
8171		maxConcurrentOkay = int64(len(cc.streams)+cc.streamsReserved+1) <= int64(cc.maxConcurrentStreams)
8172	}
8173
8174	st.canTakeNewRequest = cc.goAway == nil && !cc.closed && !cc.closing && maxConcurrentOkay &&
8175		!cc.doNotReuse &&
8176		int64(cc.nextStreamID)+2*int64(cc.pendingRequests) < math.MaxInt32 &&
8177		!cc.tooIdleLocked()
8178	return
8179}
8180
8181func (cc *http2ClientConn) canTakeNewRequestLocked() bool {
8182	st := cc.idleStateLocked()
8183	return st.canTakeNewRequest
8184}
8185
8186// tooIdleLocked reports whether this connection has been been sitting idle
8187// for too much wall time.
8188func (cc *http2ClientConn) tooIdleLocked() bool {
8189	// The Round(0) strips the monontonic clock reading so the
8190	// times are compared based on their wall time. We don't want
8191	// to reuse a connection that's been sitting idle during
8192	// VM/laptop suspend if monotonic time was also frozen.
8193	return cc.idleTimeout != 0 && !cc.lastIdle.IsZero() && time.Since(cc.lastIdle.Round(0)) > cc.idleTimeout
8194}
8195
8196// onIdleTimeout is called from a time.AfterFunc goroutine. It will
8197// only be called when we're idle, but because we're coming from a new
8198// goroutine, there could be a new request coming in at the same time,
8199// so this simply calls the synchronized closeIfIdle to shut down this
8200// connection. The timer could just call closeIfIdle, but this is more
8201// clear.
8202func (cc *http2ClientConn) onIdleTimeout() {
8203	cc.closeIfIdle()
8204}
8205
8206func (cc *http2ClientConn) closeConn() {
8207	t := time.AfterFunc(250*time.Millisecond, cc.forceCloseConn)
8208	defer t.Stop()
8209	cc.tconn.Close()
8210}
8211
8212// A tls.Conn.Close can hang for a long time if the peer is unresponsive.
8213// Try to shut it down more aggressively.
8214func (cc *http2ClientConn) forceCloseConn() {
8215	tc, ok := cc.tconn.(*tls.Conn)
8216	if !ok {
8217		return
8218	}
8219	if nc := tc.NetConn(); nc != nil {
8220		nc.Close()
8221	}
8222}
8223
8224func (cc *http2ClientConn) closeIfIdle() {
8225	cc.mu.Lock()
8226	if len(cc.streams) > 0 || cc.streamsReserved > 0 {
8227		cc.mu.Unlock()
8228		return
8229	}
8230	cc.closed = true
8231	nextID := cc.nextStreamID
8232	// TODO: do clients send GOAWAY too? maybe? Just Close:
8233	cc.mu.Unlock()
8234
8235	if http2VerboseLogs {
8236		cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2)
8237	}
8238	cc.closeConn()
8239}
8240
8241func (cc *http2ClientConn) isDoNotReuseAndIdle() bool {
8242	cc.mu.Lock()
8243	defer cc.mu.Unlock()
8244	return cc.doNotReuse && len(cc.streams) == 0
8245}
8246
8247var http2shutdownEnterWaitStateHook = func() {}
8248
8249// Shutdown gracefully closes the client connection, waiting for running streams to complete.
8250func (cc *http2ClientConn) Shutdown(ctx context.Context) error {
8251	if err := cc.sendGoAway(); err != nil {
8252		return err
8253	}
8254	// Wait for all in-flight streams to complete or connection to close
8255	done := make(chan struct{})
8256	cancelled := false // guarded by cc.mu
8257	go func() {
8258		cc.t.markNewGoroutine()
8259		cc.mu.Lock()
8260		defer cc.mu.Unlock()
8261		for {
8262			if len(cc.streams) == 0 || cc.closed {
8263				cc.closed = true
8264				close(done)
8265				break
8266			}
8267			if cancelled {
8268				break
8269			}
8270			cc.cond.Wait()
8271		}
8272	}()
8273	http2shutdownEnterWaitStateHook()
8274	select {
8275	case <-done:
8276		cc.closeConn()
8277		return nil
8278	case <-ctx.Done():
8279		cc.mu.Lock()
8280		// Free the goroutine above
8281		cancelled = true
8282		cc.cond.Broadcast()
8283		cc.mu.Unlock()
8284		return ctx.Err()
8285	}
8286}
8287
8288func (cc *http2ClientConn) sendGoAway() error {
8289	cc.mu.Lock()
8290	closing := cc.closing
8291	cc.closing = true
8292	maxStreamID := cc.nextStreamID
8293	cc.mu.Unlock()
8294	if closing {
8295		// GOAWAY sent already
8296		return nil
8297	}
8298
8299	cc.wmu.Lock()
8300	defer cc.wmu.Unlock()
8301	// Send a graceful shutdown frame to server
8302	if err := cc.fr.WriteGoAway(maxStreamID, http2ErrCodeNo, nil); err != nil {
8303		return err
8304	}
8305	if err := cc.bw.Flush(); err != nil {
8306		return err
8307	}
8308	// Prevent new requests
8309	return nil
8310}
8311
8312// closes the client connection immediately. In-flight requests are interrupted.
8313// err is sent to streams.
8314func (cc *http2ClientConn) closeForError(err error) {
8315	cc.mu.Lock()
8316	cc.closed = true
8317	for _, cs := range cc.streams {
8318		cs.abortStreamLocked(err)
8319	}
8320	cc.cond.Broadcast()
8321	cc.mu.Unlock()
8322	cc.closeConn()
8323}
8324
8325// Close closes the client connection immediately.
8326//
8327// In-flight requests are interrupted. For a graceful shutdown, use Shutdown instead.
8328func (cc *http2ClientConn) Close() error {
8329	err := errors.New("http2: client connection force closed via ClientConn.Close")
8330	cc.closeForError(err)
8331	return nil
8332}
8333
8334// closes the client connection immediately. In-flight requests are interrupted.
8335func (cc *http2ClientConn) closeForLostPing() {
8336	err := errors.New("http2: client connection lost")
8337	if f := cc.t.CountError; f != nil {
8338		f("conn_close_lost_ping")
8339	}
8340	cc.closeForError(err)
8341}
8342
8343// errRequestCanceled is a copy of net/http's errRequestCanceled because it's not
8344// exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests.
8345var http2errRequestCanceled = errors.New("net/http: request canceled")
8346
8347func http2commaSeparatedTrailers(req *Request) (string, error) {
8348	keys := make([]string, 0, len(req.Trailer))
8349	for k := range req.Trailer {
8350		k = http2canonicalHeader(k)
8351		switch k {
8352		case "Transfer-Encoding", "Trailer", "Content-Length":
8353			return "", fmt.Errorf("invalid Trailer key %q", k)
8354		}
8355		keys = append(keys, k)
8356	}
8357	if len(keys) > 0 {
8358		sort.Strings(keys)
8359		return strings.Join(keys, ","), nil
8360	}
8361	return "", nil
8362}
8363
8364func (cc *http2ClientConn) responseHeaderTimeout() time.Duration {
8365	if cc.t.t1 != nil {
8366		return cc.t.t1.ResponseHeaderTimeout
8367	}
8368	// No way to do this (yet?) with just an http2.Transport. Probably
8369	// no need. Request.Cancel this is the new way. We only need to support
8370	// this for compatibility with the old http.Transport fields when
8371	// we're doing transparent http2.
8372	return 0
8373}
8374
8375// checkConnHeaders checks whether req has any invalid connection-level headers.
8376// per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields.
8377// Certain headers are special-cased as okay but not transmitted later.
8378func http2checkConnHeaders(req *Request) error {
8379	if v := req.Header.Get("Upgrade"); v != "" {
8380		return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"])
8381	}
8382	if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") {
8383		return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv)
8384	}
8385	if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && !http2asciiEqualFold(vv[0], "close") && !http2asciiEqualFold(vv[0], "keep-alive")) {
8386		return fmt.Errorf("http2: invalid Connection request header: %q", vv)
8387	}
8388	return nil
8389}
8390
8391// actualContentLength returns a sanitized version of
8392// req.ContentLength, where 0 actually means zero (not unknown) and -1
8393// means unknown.
8394func http2actualContentLength(req *Request) int64 {
8395	if req.Body == nil || req.Body == NoBody {
8396		return 0
8397	}
8398	if req.ContentLength != 0 {
8399		return req.ContentLength
8400	}
8401	return -1
8402}
8403
8404func (cc *http2ClientConn) decrStreamReservations() {
8405	cc.mu.Lock()
8406	defer cc.mu.Unlock()
8407	cc.decrStreamReservationsLocked()
8408}
8409
8410func (cc *http2ClientConn) decrStreamReservationsLocked() {
8411	if cc.streamsReserved > 0 {
8412		cc.streamsReserved--
8413	}
8414}
8415
8416func (cc *http2ClientConn) RoundTrip(req *Request) (*Response, error) {
8417	return cc.roundTrip(req, nil)
8418}
8419
8420func (cc *http2ClientConn) roundTrip(req *Request, streamf func(*http2clientStream)) (*Response, error) {
8421	ctx := req.Context()
8422	cs := &http2clientStream{
8423		cc:                   cc,
8424		ctx:                  ctx,
8425		reqCancel:            req.Cancel,
8426		isHead:               req.Method == "HEAD",
8427		reqBody:              req.Body,
8428		reqBodyContentLength: http2actualContentLength(req),
8429		trace:                httptrace.ContextClientTrace(ctx),
8430		peerClosed:           make(chan struct{}),
8431		abort:                make(chan struct{}),
8432		respHeaderRecv:       make(chan struct{}),
8433		donec:                make(chan struct{}),
8434	}
8435
8436	// TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere?
8437	if !cc.t.disableCompression() &&
8438		req.Header.Get("Accept-Encoding") == "" &&
8439		req.Header.Get("Range") == "" &&
8440		!cs.isHead {
8441		// Request gzip only, not deflate. Deflate is ambiguous and
8442		// not as universally supported anyway.
8443		// See: https://zlib.net/zlib_faq.html#faq39
8444		//
8445		// Note that we don't request this for HEAD requests,
8446		// due to a bug in nginx:
8447		//   http://trac.nginx.org/nginx/ticket/358
8448		//   https://golang.org/issue/5522
8449		//
8450		// We don't request gzip if the request is for a range, since
8451		// auto-decoding a portion of a gzipped document will just fail
8452		// anyway. See https://golang.org/issue/8923
8453		cs.requestedGzip = true
8454	}
8455
8456	go cs.doRequest(req, streamf)
8457
8458	waitDone := func() error {
8459		select {
8460		case <-cs.donec:
8461			return nil
8462		case <-ctx.Done():
8463			return ctx.Err()
8464		case <-cs.reqCancel:
8465			return http2errRequestCanceled
8466		}
8467	}
8468
8469	handleResponseHeaders := func() (*Response, error) {
8470		res := cs.res
8471		if res.StatusCode > 299 {
8472			// On error or status code 3xx, 4xx, 5xx, etc abort any
8473			// ongoing write, assuming that the server doesn't care
8474			// about our request body. If the server replied with 1xx or
8475			// 2xx, however, then assume the server DOES potentially
8476			// want our body (e.g. full-duplex streaming:
8477			// golang.org/issue/13444). If it turns out the server
8478			// doesn't, they'll RST_STREAM us soon enough. This is a
8479			// heuristic to avoid adding knobs to Transport. Hopefully
8480			// we can keep it.
8481			cs.abortRequestBodyWrite()
8482		}
8483		res.Request = req
8484		res.TLS = cc.tlsState
8485		if res.Body == http2noBody && http2actualContentLength(req) == 0 {
8486			// If there isn't a request or response body still being
8487			// written, then wait for the stream to be closed before
8488			// RoundTrip returns.
8489			if err := waitDone(); err != nil {
8490				return nil, err
8491			}
8492		}
8493		return res, nil
8494	}
8495
8496	cancelRequest := func(cs *http2clientStream, err error) error {
8497		cs.cc.mu.Lock()
8498		bodyClosed := cs.reqBodyClosed
8499		cs.cc.mu.Unlock()
8500		// Wait for the request body to be closed.
8501		//
8502		// If nothing closed the body before now, abortStreamLocked
8503		// will have started a goroutine to close it.
8504		//
8505		// Closing the body before returning avoids a race condition
8506		// with net/http checking its readTrackingBody to see if the
8507		// body was read from or closed. See golang/go#60041.
8508		//
8509		// The body is closed in a separate goroutine without the
8510		// connection mutex held, but dropping the mutex before waiting
8511		// will keep us from holding it indefinitely if the body
8512		// close is slow for some reason.
8513		if bodyClosed != nil {
8514			<-bodyClosed
8515		}
8516		return err
8517	}
8518
8519	for {
8520		select {
8521		case <-cs.respHeaderRecv:
8522			return handleResponseHeaders()
8523		case <-cs.abort:
8524			select {
8525			case <-cs.respHeaderRecv:
8526				// If both cs.respHeaderRecv and cs.abort are signaling,
8527				// pick respHeaderRecv. The server probably wrote the
8528				// response and immediately reset the stream.
8529				// golang.org/issue/49645
8530				return handleResponseHeaders()
8531			default:
8532				waitDone()
8533				return nil, cs.abortErr
8534			}
8535		case <-ctx.Done():
8536			err := ctx.Err()
8537			cs.abortStream(err)
8538			return nil, cancelRequest(cs, err)
8539		case <-cs.reqCancel:
8540			cs.abortStream(http2errRequestCanceled)
8541			return nil, cancelRequest(cs, http2errRequestCanceled)
8542		}
8543	}
8544}
8545
8546// doRequest runs for the duration of the request lifetime.
8547//
8548// It sends the request and performs post-request cleanup (closing Request.Body, etc.).
8549func (cs *http2clientStream) doRequest(req *Request, streamf func(*http2clientStream)) {
8550	cs.cc.t.markNewGoroutine()
8551	err := cs.writeRequest(req, streamf)
8552	cs.cleanupWriteRequest(err)
8553}
8554
8555// writeRequest sends a request.
8556//
8557// It returns nil after the request is written, the response read,
8558// and the request stream is half-closed by the peer.
8559//
8560// It returns non-nil if the request ends otherwise.
8561// If the returned error is StreamError, the error Code may be used in resetting the stream.
8562func (cs *http2clientStream) writeRequest(req *Request, streamf func(*http2clientStream)) (err error) {
8563	cc := cs.cc
8564	ctx := cs.ctx
8565
8566	if err := http2checkConnHeaders(req); err != nil {
8567		return err
8568	}
8569
8570	// Acquire the new-request lock by writing to reqHeaderMu.
8571	// This lock guards the critical section covering allocating a new stream ID
8572	// (requires mu) and creating the stream (requires wmu).
8573	if cc.reqHeaderMu == nil {
8574		panic("RoundTrip on uninitialized ClientConn") // for tests
8575	}
8576	select {
8577	case cc.reqHeaderMu <- struct{}{}:
8578	case <-cs.reqCancel:
8579		return http2errRequestCanceled
8580	case <-ctx.Done():
8581		return ctx.Err()
8582	}
8583
8584	cc.mu.Lock()
8585	if cc.idleTimer != nil {
8586		cc.idleTimer.Stop()
8587	}
8588	cc.decrStreamReservationsLocked()
8589	if err := cc.awaitOpenSlotForStreamLocked(cs); err != nil {
8590		cc.mu.Unlock()
8591		<-cc.reqHeaderMu
8592		return err
8593	}
8594	cc.addStreamLocked(cs) // assigns stream ID
8595	if http2isConnectionCloseRequest(req) {
8596		cc.doNotReuse = true
8597	}
8598	cc.mu.Unlock()
8599
8600	if streamf != nil {
8601		streamf(cs)
8602	}
8603
8604	continueTimeout := cc.t.expectContinueTimeout()
8605	if continueTimeout != 0 {
8606		if !httpguts.HeaderValuesContainsToken(req.Header["Expect"], "100-continue") {
8607			continueTimeout = 0
8608		} else {
8609			cs.on100 = make(chan struct{}, 1)
8610		}
8611	}
8612
8613	// Past this point (where we send request headers), it is possible for
8614	// RoundTrip to return successfully. Since the RoundTrip contract permits
8615	// the caller to "mutate or reuse" the Request after closing the Response's Body,
8616	// we must take care when referencing the Request from here on.
8617	err = cs.encodeAndWriteHeaders(req)
8618	<-cc.reqHeaderMu
8619	if err != nil {
8620		return err
8621	}
8622
8623	hasBody := cs.reqBodyContentLength != 0
8624	if !hasBody {
8625		cs.sentEndStream = true
8626	} else {
8627		if continueTimeout != 0 {
8628			http2traceWait100Continue(cs.trace)
8629			timer := time.NewTimer(continueTimeout)
8630			select {
8631			case <-timer.C:
8632				err = nil
8633			case <-cs.on100:
8634				err = nil
8635			case <-cs.abort:
8636				err = cs.abortErr
8637			case <-ctx.Done():
8638				err = ctx.Err()
8639			case <-cs.reqCancel:
8640				err = http2errRequestCanceled
8641			}
8642			timer.Stop()
8643			if err != nil {
8644				http2traceWroteRequest(cs.trace, err)
8645				return err
8646			}
8647		}
8648
8649		if err = cs.writeRequestBody(req); err != nil {
8650			if err != http2errStopReqBodyWrite {
8651				http2traceWroteRequest(cs.trace, err)
8652				return err
8653			}
8654		} else {
8655			cs.sentEndStream = true
8656		}
8657	}
8658
8659	http2traceWroteRequest(cs.trace, err)
8660
8661	var respHeaderTimer <-chan time.Time
8662	var respHeaderRecv chan struct{}
8663	if d := cc.responseHeaderTimeout(); d != 0 {
8664		timer := cc.t.newTimer(d)
8665		defer timer.Stop()
8666		respHeaderTimer = timer.C()
8667		respHeaderRecv = cs.respHeaderRecv
8668	}
8669	// Wait until the peer half-closes its end of the stream,
8670	// or until the request is aborted (via context, error, or otherwise),
8671	// whichever comes first.
8672	for {
8673		select {
8674		case <-cs.peerClosed:
8675			return nil
8676		case <-respHeaderTimer:
8677			return http2errTimeout
8678		case <-respHeaderRecv:
8679			respHeaderRecv = nil
8680			respHeaderTimer = nil // keep waiting for END_STREAM
8681		case <-cs.abort:
8682			return cs.abortErr
8683		case <-ctx.Done():
8684			return ctx.Err()
8685		case <-cs.reqCancel:
8686			return http2errRequestCanceled
8687		}
8688	}
8689}
8690
8691func (cs *http2clientStream) encodeAndWriteHeaders(req *Request) error {
8692	cc := cs.cc
8693	ctx := cs.ctx
8694
8695	cc.wmu.Lock()
8696	defer cc.wmu.Unlock()
8697
8698	// If the request was canceled while waiting for cc.mu, just quit.
8699	select {
8700	case <-cs.abort:
8701		return cs.abortErr
8702	case <-ctx.Done():
8703		return ctx.Err()
8704	case <-cs.reqCancel:
8705		return http2errRequestCanceled
8706	default:
8707	}
8708
8709	// Encode headers.
8710	//
8711	// we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is
8712	// sent by writeRequestBody below, along with any Trailers,
8713	// again in form HEADERS{1}, CONTINUATION{0,})
8714	trailers, err := http2commaSeparatedTrailers(req)
8715	if err != nil {
8716		return err
8717	}
8718	hasTrailers := trailers != ""
8719	contentLen := http2actualContentLength(req)
8720	hasBody := contentLen != 0
8721	hdrs, err := cc.encodeHeaders(req, cs.requestedGzip, trailers, contentLen)
8722	if err != nil {
8723		return err
8724	}
8725
8726	// Write the request.
8727	endStream := !hasBody && !hasTrailers
8728	cs.sentHeaders = true
8729	err = cc.writeHeaders(cs.ID, endStream, int(cc.maxFrameSize), hdrs)
8730	http2traceWroteHeaders(cs.trace)
8731	return err
8732}
8733
8734// cleanupWriteRequest performs post-request tasks.
8735//
8736// If err (the result of writeRequest) is non-nil and the stream is not closed,
8737// cleanupWriteRequest will send a reset to the peer.
8738func (cs *http2clientStream) cleanupWriteRequest(err error) {
8739	cc := cs.cc
8740
8741	if cs.ID == 0 {
8742		// We were canceled before creating the stream, so return our reservation.
8743		cc.decrStreamReservations()
8744	}
8745
8746	// TODO: write h12Compare test showing whether
8747	// Request.Body is closed by the Transport,
8748	// and in multiple cases: server replies <=299 and >299
8749	// while still writing request body
8750	cc.mu.Lock()
8751	mustCloseBody := false
8752	if cs.reqBody != nil && cs.reqBodyClosed == nil {
8753		mustCloseBody = true
8754		cs.reqBodyClosed = make(chan struct{})
8755	}
8756	bodyClosed := cs.reqBodyClosed
8757	cc.mu.Unlock()
8758	if mustCloseBody {
8759		cs.reqBody.Close()
8760		close(bodyClosed)
8761	}
8762	if bodyClosed != nil {
8763		<-bodyClosed
8764	}
8765
8766	if err != nil && cs.sentEndStream {
8767		// If the connection is closed immediately after the response is read,
8768		// we may be aborted before finishing up here. If the stream was closed
8769		// cleanly on both sides, there is no error.
8770		select {
8771		case <-cs.peerClosed:
8772			err = nil
8773		default:
8774		}
8775	}
8776	if err != nil {
8777		cs.abortStream(err) // possibly redundant, but harmless
8778		if cs.sentHeaders {
8779			if se, ok := err.(http2StreamError); ok {
8780				if se.Cause != http2errFromPeer {
8781					cc.writeStreamReset(cs.ID, se.Code, err)
8782				}
8783			} else {
8784				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, err)
8785			}
8786		}
8787		cs.bufPipe.CloseWithError(err) // no-op if already closed
8788	} else {
8789		if cs.sentHeaders && !cs.sentEndStream {
8790			cc.writeStreamReset(cs.ID, http2ErrCodeNo, nil)
8791		}
8792		cs.bufPipe.CloseWithError(http2errRequestCanceled)
8793	}
8794	if cs.ID != 0 {
8795		cc.forgetStreamID(cs.ID)
8796	}
8797
8798	cc.wmu.Lock()
8799	werr := cc.werr
8800	cc.wmu.Unlock()
8801	if werr != nil {
8802		cc.Close()
8803	}
8804
8805	close(cs.donec)
8806}
8807
8808// awaitOpenSlotForStreamLocked waits until len(streams) < maxConcurrentStreams.
8809// Must hold cc.mu.
8810func (cc *http2ClientConn) awaitOpenSlotForStreamLocked(cs *http2clientStream) error {
8811	for {
8812		cc.lastActive = time.Now()
8813		if cc.closed || !cc.canTakeNewRequestLocked() {
8814			return http2errClientConnUnusable
8815		}
8816		cc.lastIdle = time.Time{}
8817		if int64(len(cc.streams)) < int64(cc.maxConcurrentStreams) {
8818			return nil
8819		}
8820		cc.pendingRequests++
8821		cc.cond.Wait()
8822		cc.pendingRequests--
8823		select {
8824		case <-cs.abort:
8825			return cs.abortErr
8826		default:
8827		}
8828	}
8829}
8830
8831// requires cc.wmu be held
8832func (cc *http2ClientConn) writeHeaders(streamID uint32, endStream bool, maxFrameSize int, hdrs []byte) error {
8833	first := true // first frame written (HEADERS is first, then CONTINUATION)
8834	for len(hdrs) > 0 && cc.werr == nil {
8835		chunk := hdrs
8836		if len(chunk) > maxFrameSize {
8837			chunk = chunk[:maxFrameSize]
8838		}
8839		hdrs = hdrs[len(chunk):]
8840		endHeaders := len(hdrs) == 0
8841		if first {
8842			cc.fr.WriteHeaders(http2HeadersFrameParam{
8843				StreamID:      streamID,
8844				BlockFragment: chunk,
8845				EndStream:     endStream,
8846				EndHeaders:    endHeaders,
8847			})
8848			first = false
8849		} else {
8850			cc.fr.WriteContinuation(streamID, endHeaders, chunk)
8851		}
8852	}
8853	cc.bw.Flush()
8854	return cc.werr
8855}
8856
8857// internal error values; they don't escape to callers
8858var (
8859	// abort request body write; don't send cancel
8860	http2errStopReqBodyWrite = errors.New("http2: aborting request body write")
8861
8862	// abort request body write, but send stream reset of cancel.
8863	http2errStopReqBodyWriteAndCancel = errors.New("http2: canceling request")
8864
8865	http2errReqBodyTooLong = errors.New("http2: request body larger than specified content length")
8866)
8867
8868// frameScratchBufferLen returns the length of a buffer to use for
8869// outgoing request bodies to read/write to/from.
8870//
8871// It returns max(1, min(peer's advertised max frame size,
8872// Request.ContentLength+1, 512KB)).
8873func (cs *http2clientStream) frameScratchBufferLen(maxFrameSize int) int {
8874	const max = 512 << 10
8875	n := int64(maxFrameSize)
8876	if n > max {
8877		n = max
8878	}
8879	if cl := cs.reqBodyContentLength; cl != -1 && cl+1 < n {
8880		// Add an extra byte past the declared content-length to
8881		// give the caller's Request.Body io.Reader a chance to
8882		// give us more bytes than they declared, so we can catch it
8883		// early.
8884		n = cl + 1
8885	}
8886	if n < 1 {
8887		return 1
8888	}
8889	return int(n) // doesn't truncate; max is 512K
8890}
8891
8892// Seven bufPools manage different frame sizes. This helps to avoid scenarios where long-running
8893// streaming requests using small frame sizes occupy large buffers initially allocated for prior
8894// requests needing big buffers. The size ranges are as follows:
8895// {0 KB, 16 KB], {16 KB, 32 KB], {32 KB, 64 KB], {64 KB, 128 KB], {128 KB, 256 KB],
8896// {256 KB, 512 KB], {512 KB, infinity}
8897// In practice, the maximum scratch buffer size should not exceed 512 KB due to
8898// frameScratchBufferLen(maxFrameSize), thus the "infinity pool" should never be used.
8899// It exists mainly as a safety measure, for potential future increases in max buffer size.
8900var http2bufPools [7]sync.Pool // of *[]byte
8901
8902func http2bufPoolIndex(size int) int {
8903	if size <= 16384 {
8904		return 0
8905	}
8906	size -= 1
8907	bits := bits.Len(uint(size))
8908	index := bits - 14
8909	if index >= len(http2bufPools) {
8910		return len(http2bufPools) - 1
8911	}
8912	return index
8913}
8914
8915func (cs *http2clientStream) writeRequestBody(req *Request) (err error) {
8916	cc := cs.cc
8917	body := cs.reqBody
8918	sentEnd := false // whether we sent the final DATA frame w/ END_STREAM
8919
8920	hasTrailers := req.Trailer != nil
8921	remainLen := cs.reqBodyContentLength
8922	hasContentLen := remainLen != -1
8923
8924	cc.mu.Lock()
8925	maxFrameSize := int(cc.maxFrameSize)
8926	cc.mu.Unlock()
8927
8928	// Scratch buffer for reading into & writing from.
8929	scratchLen := cs.frameScratchBufferLen(maxFrameSize)
8930	var buf []byte
8931	index := http2bufPoolIndex(scratchLen)
8932	if bp, ok := http2bufPools[index].Get().(*[]byte); ok && len(*bp) >= scratchLen {
8933		defer http2bufPools[index].Put(bp)
8934		buf = *bp
8935	} else {
8936		buf = make([]byte, scratchLen)
8937		defer http2bufPools[index].Put(&buf)
8938	}
8939
8940	var sawEOF bool
8941	for !sawEOF {
8942		n, err := body.Read(buf)
8943		if hasContentLen {
8944			remainLen -= int64(n)
8945			if remainLen == 0 && err == nil {
8946				// The request body's Content-Length was predeclared and
8947				// we just finished reading it all, but the underlying io.Reader
8948				// returned the final chunk with a nil error (which is one of
8949				// the two valid things a Reader can do at EOF). Because we'd prefer
8950				// to send the END_STREAM bit early, double-check that we're actually
8951				// at EOF. Subsequent reads should return (0, EOF) at this point.
8952				// If either value is different, we return an error in one of two ways below.
8953				var scratch [1]byte
8954				var n1 int
8955				n1, err = body.Read(scratch[:])
8956				remainLen -= int64(n1)
8957			}
8958			if remainLen < 0 {
8959				err = http2errReqBodyTooLong
8960				return err
8961			}
8962		}
8963		if err != nil {
8964			cc.mu.Lock()
8965			bodyClosed := cs.reqBodyClosed != nil
8966			cc.mu.Unlock()
8967			switch {
8968			case bodyClosed:
8969				return http2errStopReqBodyWrite
8970			case err == io.EOF:
8971				sawEOF = true
8972				err = nil
8973			default:
8974				return err
8975			}
8976		}
8977
8978		remain := buf[:n]
8979		for len(remain) > 0 && err == nil {
8980			var allowed int32
8981			allowed, err = cs.awaitFlowControl(len(remain))
8982			if err != nil {
8983				return err
8984			}
8985			cc.wmu.Lock()
8986			data := remain[:allowed]
8987			remain = remain[allowed:]
8988			sentEnd = sawEOF && len(remain) == 0 && !hasTrailers
8989			err = cc.fr.WriteData(cs.ID, sentEnd, data)
8990			if err == nil {
8991				// TODO(bradfitz): this flush is for latency, not bandwidth.
8992				// Most requests won't need this. Make this opt-in or
8993				// opt-out?  Use some heuristic on the body type? Nagel-like
8994				// timers?  Based on 'n'? Only last chunk of this for loop,
8995				// unless flow control tokens are low? For now, always.
8996				// If we change this, see comment below.
8997				err = cc.bw.Flush()
8998			}
8999			cc.wmu.Unlock()
9000		}
9001		if err != nil {
9002			return err
9003		}
9004	}
9005
9006	if sentEnd {
9007		// Already sent END_STREAM (which implies we have no
9008		// trailers) and flushed, because currently all
9009		// WriteData frames above get a flush. So we're done.
9010		return nil
9011	}
9012
9013	// Since the RoundTrip contract permits the caller to "mutate or reuse"
9014	// a request after the Response's Body is closed, verify that this hasn't
9015	// happened before accessing the trailers.
9016	cc.mu.Lock()
9017	trailer := req.Trailer
9018	err = cs.abortErr
9019	cc.mu.Unlock()
9020	if err != nil {
9021		return err
9022	}
9023
9024	cc.wmu.Lock()
9025	defer cc.wmu.Unlock()
9026	var trls []byte
9027	if len(trailer) > 0 {
9028		trls, err = cc.encodeTrailers(trailer)
9029		if err != nil {
9030			return err
9031		}
9032	}
9033
9034	// Two ways to send END_STREAM: either with trailers, or
9035	// with an empty DATA frame.
9036	if len(trls) > 0 {
9037		err = cc.writeHeaders(cs.ID, true, maxFrameSize, trls)
9038	} else {
9039		err = cc.fr.WriteData(cs.ID, true, nil)
9040	}
9041	if ferr := cc.bw.Flush(); ferr != nil && err == nil {
9042		err = ferr
9043	}
9044	return err
9045}
9046
9047// awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow
9048// control tokens from the server.
9049// It returns either the non-zero number of tokens taken or an error
9050// if the stream is dead.
9051func (cs *http2clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) {
9052	cc := cs.cc
9053	ctx := cs.ctx
9054	cc.mu.Lock()
9055	defer cc.mu.Unlock()
9056	for {
9057		if cc.closed {
9058			return 0, http2errClientConnClosed
9059		}
9060		if cs.reqBodyClosed != nil {
9061			return 0, http2errStopReqBodyWrite
9062		}
9063		select {
9064		case <-cs.abort:
9065			return 0, cs.abortErr
9066		case <-ctx.Done():
9067			return 0, ctx.Err()
9068		case <-cs.reqCancel:
9069			return 0, http2errRequestCanceled
9070		default:
9071		}
9072		if a := cs.flow.available(); a > 0 {
9073			take := a
9074			if int(take) > maxBytes {
9075
9076				take = int32(maxBytes) // can't truncate int; take is int32
9077			}
9078			if take > int32(cc.maxFrameSize) {
9079				take = int32(cc.maxFrameSize)
9080			}
9081			cs.flow.take(take)
9082			return take, nil
9083		}
9084		cc.cond.Wait()
9085	}
9086}
9087
9088func http2validateHeaders(hdrs Header) string {
9089	for k, vv := range hdrs {
9090		if !httpguts.ValidHeaderFieldName(k) {
9091			return fmt.Sprintf("name %q", k)
9092		}
9093		for _, v := range vv {
9094			if !httpguts.ValidHeaderFieldValue(v) {
9095				// Don't include the value in the error,
9096				// because it may be sensitive.
9097				return fmt.Sprintf("value for header %q", k)
9098			}
9099		}
9100	}
9101	return ""
9102}
9103
9104var http2errNilRequestURL = errors.New("http2: Request.URI is nil")
9105
9106// requires cc.wmu be held.
9107func (cc *http2ClientConn) encodeHeaders(req *Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) {
9108	cc.hbuf.Reset()
9109	if req.URL == nil {
9110		return nil, http2errNilRequestURL
9111	}
9112
9113	host := req.Host
9114	if host == "" {
9115		host = req.URL.Host
9116	}
9117	host, err := httpguts.PunycodeHostPort(host)
9118	if err != nil {
9119		return nil, err
9120	}
9121	if !httpguts.ValidHostHeader(host) {
9122		return nil, errors.New("http2: invalid Host header")
9123	}
9124
9125	var path string
9126	if req.Method != "CONNECT" {
9127		path = req.URL.RequestURI()
9128		if !http2validPseudoPath(path) {
9129			orig := path
9130			path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host)
9131			if !http2validPseudoPath(path) {
9132				if req.URL.Opaque != "" {
9133					return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque)
9134				} else {
9135					return nil, fmt.Errorf("invalid request :path %q", orig)
9136				}
9137			}
9138		}
9139	}
9140
9141	// Check for any invalid headers+trailers and return an error before we
9142	// potentially pollute our hpack state. (We want to be able to
9143	// continue to reuse the hpack encoder for future requests)
9144	if err := http2validateHeaders(req.Header); err != "" {
9145		return nil, fmt.Errorf("invalid HTTP header %s", err)
9146	}
9147	if err := http2validateHeaders(req.Trailer); err != "" {
9148		return nil, fmt.Errorf("invalid HTTP trailer %s", err)
9149	}
9150
9151	enumerateHeaders := func(f func(name, value string)) {
9152		// 8.1.2.3 Request Pseudo-Header Fields
9153		// The :path pseudo-header field includes the path and query parts of the
9154		// target URI (the path-absolute production and optionally a '?' character
9155		// followed by the query production, see Sections 3.3 and 3.4 of
9156		// [RFC3986]).
9157		f(":authority", host)
9158		m := req.Method
9159		if m == "" {
9160			m = MethodGet
9161		}
9162		f(":method", m)
9163		if req.Method != "CONNECT" {
9164			f(":path", path)
9165			f(":scheme", req.URL.Scheme)
9166		}
9167		if trailers != "" {
9168			f("trailer", trailers)
9169		}
9170
9171		var didUA bool
9172		for k, vv := range req.Header {
9173			if http2asciiEqualFold(k, "host") || http2asciiEqualFold(k, "content-length") {
9174				// Host is :authority, already sent.
9175				// Content-Length is automatic, set below.
9176				continue
9177			} else if http2asciiEqualFold(k, "connection") ||
9178				http2asciiEqualFold(k, "proxy-connection") ||
9179				http2asciiEqualFold(k, "transfer-encoding") ||
9180				http2asciiEqualFold(k, "upgrade") ||
9181				http2asciiEqualFold(k, "keep-alive") {
9182				// Per 8.1.2.2 Connection-Specific Header
9183				// Fields, don't send connection-specific
9184				// fields. We have already checked if any
9185				// are error-worthy so just ignore the rest.
9186				continue
9187			} else if http2asciiEqualFold(k, "user-agent") {
9188				// Match Go's http1 behavior: at most one
9189				// User-Agent. If set to nil or empty string,
9190				// then omit it. Otherwise if not mentioned,
9191				// include the default (below).
9192				didUA = true
9193				if len(vv) < 1 {
9194					continue
9195				}
9196				vv = vv[:1]
9197				if vv[0] == "" {
9198					continue
9199				}
9200			} else if http2asciiEqualFold(k, "cookie") {
9201				// Per 8.1.2.5 To allow for better compression efficiency, the
9202				// Cookie header field MAY be split into separate header fields,
9203				// each with one or more cookie-pairs.
9204				for _, v := range vv {
9205					for {
9206						p := strings.IndexByte(v, ';')
9207						if p < 0 {
9208							break
9209						}
9210						f("cookie", v[:p])
9211						p++
9212						// strip space after semicolon if any.
9213						for p+1 <= len(v) && v[p] == ' ' {
9214							p++
9215						}
9216						v = v[p:]
9217					}
9218					if len(v) > 0 {
9219						f("cookie", v)
9220					}
9221				}
9222				continue
9223			}
9224
9225			for _, v := range vv {
9226				f(k, v)
9227			}
9228		}
9229		if http2shouldSendReqContentLength(req.Method, contentLength) {
9230			f("content-length", strconv.FormatInt(contentLength, 10))
9231		}
9232		if addGzipHeader {
9233			f("accept-encoding", "gzip")
9234		}
9235		if !didUA {
9236			f("user-agent", http2defaultUserAgent)
9237		}
9238	}
9239
9240	// Do a first pass over the headers counting bytes to ensure
9241	// we don't exceed cc.peerMaxHeaderListSize. This is done as a
9242	// separate pass before encoding the headers to prevent
9243	// modifying the hpack state.
9244	hlSize := uint64(0)
9245	enumerateHeaders(func(name, value string) {
9246		hf := hpack.HeaderField{Name: name, Value: value}
9247		hlSize += uint64(hf.Size())
9248	})
9249
9250	if hlSize > cc.peerMaxHeaderListSize {
9251		return nil, http2errRequestHeaderListSize
9252	}
9253
9254	trace := httptrace.ContextClientTrace(req.Context())
9255	traceHeaders := http2traceHasWroteHeaderField(trace)
9256
9257	// Header list size is ok. Write the headers.
9258	enumerateHeaders(func(name, value string) {
9259		name, ascii := http2lowerHeader(name)
9260		if !ascii {
9261			// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
9262			// field names have to be ASCII characters (just as in HTTP/1.x).
9263			return
9264		}
9265		cc.writeHeader(name, value)
9266		if traceHeaders {
9267			http2traceWroteHeaderField(trace, name, value)
9268		}
9269	})
9270
9271	return cc.hbuf.Bytes(), nil
9272}
9273
9274// shouldSendReqContentLength reports whether the http2.Transport should send
9275// a "content-length" request header. This logic is basically a copy of the net/http
9276// transferWriter.shouldSendContentLength.
9277// The contentLength is the corrected contentLength (so 0 means actually 0, not unknown).
9278// -1 means unknown.
9279func http2shouldSendReqContentLength(method string, contentLength int64) bool {
9280	if contentLength > 0 {
9281		return true
9282	}
9283	if contentLength < 0 {
9284		return false
9285	}
9286	// For zero bodies, whether we send a content-length depends on the method.
9287	// It also kinda doesn't matter for http2 either way, with END_STREAM.
9288	switch method {
9289	case "POST", "PUT", "PATCH":
9290		return true
9291	default:
9292		return false
9293	}
9294}
9295
9296// requires cc.wmu be held.
9297func (cc *http2ClientConn) encodeTrailers(trailer Header) ([]byte, error) {
9298	cc.hbuf.Reset()
9299
9300	hlSize := uint64(0)
9301	for k, vv := range trailer {
9302		for _, v := range vv {
9303			hf := hpack.HeaderField{Name: k, Value: v}
9304			hlSize += uint64(hf.Size())
9305		}
9306	}
9307	if hlSize > cc.peerMaxHeaderListSize {
9308		return nil, http2errRequestHeaderListSize
9309	}
9310
9311	for k, vv := range trailer {
9312		lowKey, ascii := http2lowerHeader(k)
9313		if !ascii {
9314			// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
9315			// field names have to be ASCII characters (just as in HTTP/1.x).
9316			continue
9317		}
9318		// Transfer-Encoding, etc.. have already been filtered at the
9319		// start of RoundTrip
9320		for _, v := range vv {
9321			cc.writeHeader(lowKey, v)
9322		}
9323	}
9324	return cc.hbuf.Bytes(), nil
9325}
9326
9327func (cc *http2ClientConn) writeHeader(name, value string) {
9328	if http2VerboseLogs {
9329		log.Printf("http2: Transport encoding header %q = %q", name, value)
9330	}
9331	cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
9332}
9333
9334type http2resAndError struct {
9335	_   http2incomparable
9336	res *Response
9337	err error
9338}
9339
9340// requires cc.mu be held.
9341func (cc *http2ClientConn) addStreamLocked(cs *http2clientStream) {
9342	cs.flow.add(int32(cc.initialWindowSize))
9343	cs.flow.setConnFlow(&cc.flow)
9344	cs.inflow.init(http2transportDefaultStreamFlow)
9345	cs.ID = cc.nextStreamID
9346	cc.nextStreamID += 2
9347	cc.streams[cs.ID] = cs
9348	if cs.ID == 0 {
9349		panic("assigned stream ID 0")
9350	}
9351}
9352
9353func (cc *http2ClientConn) forgetStreamID(id uint32) {
9354	cc.mu.Lock()
9355	slen := len(cc.streams)
9356	delete(cc.streams, id)
9357	if len(cc.streams) != slen-1 {
9358		panic("forgetting unknown stream id")
9359	}
9360	cc.lastActive = time.Now()
9361	if len(cc.streams) == 0 && cc.idleTimer != nil {
9362		cc.idleTimer.Reset(cc.idleTimeout)
9363		cc.lastIdle = time.Now()
9364	}
9365	// Wake up writeRequestBody via clientStream.awaitFlowControl and
9366	// wake up RoundTrip if there is a pending request.
9367	cc.cond.Broadcast()
9368
9369	closeOnIdle := cc.singleUse || cc.doNotReuse || cc.t.disableKeepAlives() || cc.goAway != nil
9370	if closeOnIdle && cc.streamsReserved == 0 && len(cc.streams) == 0 {
9371		if http2VerboseLogs {
9372			cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, cc.nextStreamID-2)
9373		}
9374		cc.closed = true
9375		defer cc.closeConn()
9376	}
9377
9378	cc.mu.Unlock()
9379}
9380
9381// clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop.
9382type http2clientConnReadLoop struct {
9383	_  http2incomparable
9384	cc *http2ClientConn
9385}
9386
9387// readLoop runs in its own goroutine and reads and dispatches frames.
9388func (cc *http2ClientConn) readLoop() {
9389	cc.t.markNewGoroutine()
9390	rl := &http2clientConnReadLoop{cc: cc}
9391	defer rl.cleanup()
9392	cc.readerErr = rl.run()
9393	if ce, ok := cc.readerErr.(http2ConnectionError); ok {
9394		cc.wmu.Lock()
9395		cc.fr.WriteGoAway(0, http2ErrCode(ce), nil)
9396		cc.wmu.Unlock()
9397	}
9398}
9399
9400// GoAwayError is returned by the Transport when the server closes the
9401// TCP connection after sending a GOAWAY frame.
9402type http2GoAwayError struct {
9403	LastStreamID uint32
9404	ErrCode      http2ErrCode
9405	DebugData    string
9406}
9407
9408func (e http2GoAwayError) Error() string {
9409	return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q",
9410		e.LastStreamID, e.ErrCode, e.DebugData)
9411}
9412
9413func http2isEOFOrNetReadError(err error) bool {
9414	if err == io.EOF {
9415		return true
9416	}
9417	ne, ok := err.(*net.OpError)
9418	return ok && ne.Op == "read"
9419}
9420
9421func (rl *http2clientConnReadLoop) cleanup() {
9422	cc := rl.cc
9423	cc.t.connPool().MarkDead(cc)
9424	defer cc.closeConn()
9425	defer close(cc.readerDone)
9426
9427	if cc.idleTimer != nil {
9428		cc.idleTimer.Stop()
9429	}
9430
9431	// Close any response bodies if the server closes prematurely.
9432	// TODO: also do this if we've written the headers but not
9433	// gotten a response yet.
9434	err := cc.readerErr
9435	cc.mu.Lock()
9436	if cc.goAway != nil && http2isEOFOrNetReadError(err) {
9437		err = http2GoAwayError{
9438			LastStreamID: cc.goAway.LastStreamID,
9439			ErrCode:      cc.goAway.ErrCode,
9440			DebugData:    cc.goAwayDebug,
9441		}
9442	} else if err == io.EOF {
9443		err = io.ErrUnexpectedEOF
9444	}
9445	cc.closed = true
9446
9447	for _, cs := range cc.streams {
9448		select {
9449		case <-cs.peerClosed:
9450			// The server closed the stream before closing the conn,
9451			// so no need to interrupt it.
9452		default:
9453			cs.abortStreamLocked(err)
9454		}
9455	}
9456	cc.cond.Broadcast()
9457	cc.mu.Unlock()
9458}
9459
9460// countReadFrameError calls Transport.CountError with a string
9461// representing err.
9462func (cc *http2ClientConn) countReadFrameError(err error) {
9463	f := cc.t.CountError
9464	if f == nil || err == nil {
9465		return
9466	}
9467	if ce, ok := err.(http2ConnectionError); ok {
9468		errCode := http2ErrCode(ce)
9469		f(fmt.Sprintf("read_frame_conn_error_%s", errCode.stringToken()))
9470		return
9471	}
9472	if errors.Is(err, io.EOF) {
9473		f("read_frame_eof")
9474		return
9475	}
9476	if errors.Is(err, io.ErrUnexpectedEOF) {
9477		f("read_frame_unexpected_eof")
9478		return
9479	}
9480	if errors.Is(err, http2ErrFrameTooLarge) {
9481		f("read_frame_too_large")
9482		return
9483	}
9484	f("read_frame_other")
9485}
9486
9487func (rl *http2clientConnReadLoop) run() error {
9488	cc := rl.cc
9489	gotSettings := false
9490	readIdleTimeout := cc.t.ReadIdleTimeout
9491	var t http2timer
9492	if readIdleTimeout != 0 {
9493		t = cc.t.afterFunc(readIdleTimeout, cc.healthCheck)
9494	}
9495	for {
9496		f, err := cc.fr.ReadFrame()
9497		if t != nil {
9498			t.Reset(readIdleTimeout)
9499		}
9500		if err != nil {
9501			cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err)
9502		}
9503		if se, ok := err.(http2StreamError); ok {
9504			if cs := rl.streamByID(se.StreamID); cs != nil {
9505				if se.Cause == nil {
9506					se.Cause = cc.fr.errDetail
9507				}
9508				rl.endStreamError(cs, se)
9509			}
9510			continue
9511		} else if err != nil {
9512			cc.countReadFrameError(err)
9513			return err
9514		}
9515		if http2VerboseLogs {
9516			cc.vlogf("http2: Transport received %s", http2summarizeFrame(f))
9517		}
9518		if !gotSettings {
9519			if _, ok := f.(*http2SettingsFrame); !ok {
9520				cc.logf("protocol error: received %T before a SETTINGS frame", f)
9521				return http2ConnectionError(http2ErrCodeProtocol)
9522			}
9523			gotSettings = true
9524		}
9525
9526		switch f := f.(type) {
9527		case *http2MetaHeadersFrame:
9528			err = rl.processHeaders(f)
9529		case *http2DataFrame:
9530			err = rl.processData(f)
9531		case *http2GoAwayFrame:
9532			err = rl.processGoAway(f)
9533		case *http2RSTStreamFrame:
9534			err = rl.processResetStream(f)
9535		case *http2SettingsFrame:
9536			err = rl.processSettings(f)
9537		case *http2PushPromiseFrame:
9538			err = rl.processPushPromise(f)
9539		case *http2WindowUpdateFrame:
9540			err = rl.processWindowUpdate(f)
9541		case *http2PingFrame:
9542			err = rl.processPing(f)
9543		default:
9544			cc.logf("Transport: unhandled response frame type %T", f)
9545		}
9546		if err != nil {
9547			if http2VerboseLogs {
9548				cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, http2summarizeFrame(f), err)
9549			}
9550			return err
9551		}
9552	}
9553}
9554
9555func (rl *http2clientConnReadLoop) processHeaders(f *http2MetaHeadersFrame) error {
9556	cs := rl.streamByID(f.StreamID)
9557	if cs == nil {
9558		// We'd get here if we canceled a request while the
9559		// server had its response still in flight. So if this
9560		// was just something we canceled, ignore it.
9561		return nil
9562	}
9563	if cs.readClosed {
9564		rl.endStreamError(cs, http2StreamError{
9565			StreamID: f.StreamID,
9566			Code:     http2ErrCodeProtocol,
9567			Cause:    errors.New("protocol error: headers after END_STREAM"),
9568		})
9569		return nil
9570	}
9571	if !cs.firstByte {
9572		if cs.trace != nil {
9573			// TODO(bradfitz): move first response byte earlier,
9574			// when we first read the 9 byte header, not waiting
9575			// until all the HEADERS+CONTINUATION frames have been
9576			// merged. This works for now.
9577			http2traceFirstResponseByte(cs.trace)
9578		}
9579		cs.firstByte = true
9580	}
9581	if !cs.pastHeaders {
9582		cs.pastHeaders = true
9583	} else {
9584		return rl.processTrailers(cs, f)
9585	}
9586
9587	res, err := rl.handleResponse(cs, f)
9588	if err != nil {
9589		if _, ok := err.(http2ConnectionError); ok {
9590			return err
9591		}
9592		// Any other error type is a stream error.
9593		rl.endStreamError(cs, http2StreamError{
9594			StreamID: f.StreamID,
9595			Code:     http2ErrCodeProtocol,
9596			Cause:    err,
9597		})
9598		return nil // return nil from process* funcs to keep conn alive
9599	}
9600	if res == nil {
9601		// (nil, nil) special case. See handleResponse docs.
9602		return nil
9603	}
9604	cs.resTrailer = &res.Trailer
9605	cs.res = res
9606	close(cs.respHeaderRecv)
9607	if f.StreamEnded() {
9608		rl.endStream(cs)
9609	}
9610	return nil
9611}
9612
9613// may return error types nil, or ConnectionError. Any other error value
9614// is a StreamError of type ErrCodeProtocol. The returned error in that case
9615// is the detail.
9616//
9617// As a special case, handleResponse may return (nil, nil) to skip the
9618// frame (currently only used for 1xx responses).
9619func (rl *http2clientConnReadLoop) handleResponse(cs *http2clientStream, f *http2MetaHeadersFrame) (*Response, error) {
9620	if f.Truncated {
9621		return nil, http2errResponseHeaderListSize
9622	}
9623
9624	status := f.PseudoValue("status")
9625	if status == "" {
9626		return nil, errors.New("malformed response from server: missing status pseudo header")
9627	}
9628	statusCode, err := strconv.Atoi(status)
9629	if err != nil {
9630		return nil, errors.New("malformed response from server: malformed non-numeric status pseudo header")
9631	}
9632
9633	regularFields := f.RegularFields()
9634	strs := make([]string, len(regularFields))
9635	header := make(Header, len(regularFields))
9636	res := &Response{
9637		Proto:      "HTTP/2.0",
9638		ProtoMajor: 2,
9639		Header:     header,
9640		StatusCode: statusCode,
9641		Status:     status + " " + StatusText(statusCode),
9642	}
9643	for _, hf := range regularFields {
9644		key := http2canonicalHeader(hf.Name)
9645		if key == "Trailer" {
9646			t := res.Trailer
9647			if t == nil {
9648				t = make(Header)
9649				res.Trailer = t
9650			}
9651			http2foreachHeaderElement(hf.Value, func(v string) {
9652				t[http2canonicalHeader(v)] = nil
9653			})
9654		} else {
9655			vv := header[key]
9656			if vv == nil && len(strs) > 0 {
9657				// More than likely this will be a single-element key.
9658				// Most headers aren't multi-valued.
9659				// Set the capacity on strs[0] to 1, so any future append
9660				// won't extend the slice into the other strings.
9661				vv, strs = strs[:1:1], strs[1:]
9662				vv[0] = hf.Value
9663				header[key] = vv
9664			} else {
9665				header[key] = append(vv, hf.Value)
9666			}
9667		}
9668	}
9669
9670	if statusCode >= 100 && statusCode <= 199 {
9671		if f.StreamEnded() {
9672			return nil, errors.New("1xx informational response with END_STREAM flag")
9673		}
9674		cs.num1xx++
9675		const max1xxResponses = 5 // arbitrary bound on number of informational responses, same as net/http
9676		if cs.num1xx > max1xxResponses {
9677			return nil, errors.New("http2: too many 1xx informational responses")
9678		}
9679		if fn := cs.get1xxTraceFunc(); fn != nil {
9680			if err := fn(statusCode, textproto.MIMEHeader(header)); err != nil {
9681				return nil, err
9682			}
9683		}
9684		if statusCode == 100 {
9685			http2traceGot100Continue(cs.trace)
9686			select {
9687			case cs.on100 <- struct{}{}:
9688			default:
9689			}
9690		}
9691		cs.pastHeaders = false // do it all again
9692		return nil, nil
9693	}
9694
9695	res.ContentLength = -1
9696	if clens := res.Header["Content-Length"]; len(clens) == 1 {
9697		if cl, err := strconv.ParseUint(clens[0], 10, 63); err == nil {
9698			res.ContentLength = int64(cl)
9699		} else {
9700			// TODO: care? unlike http/1, it won't mess up our framing, so it's
9701			// more safe smuggling-wise to ignore.
9702		}
9703	} else if len(clens) > 1 {
9704		// TODO: care? unlike http/1, it won't mess up our framing, so it's
9705		// more safe smuggling-wise to ignore.
9706	} else if f.StreamEnded() && !cs.isHead {
9707		res.ContentLength = 0
9708	}
9709
9710	if cs.isHead {
9711		res.Body = http2noBody
9712		return res, nil
9713	}
9714
9715	if f.StreamEnded() {
9716		if res.ContentLength > 0 {
9717			res.Body = http2missingBody{}
9718		} else {
9719			res.Body = http2noBody
9720		}
9721		return res, nil
9722	}
9723
9724	cs.bufPipe.setBuffer(&http2dataBuffer{expected: res.ContentLength})
9725	cs.bytesRemain = res.ContentLength
9726	res.Body = http2transportResponseBody{cs}
9727
9728	if cs.requestedGzip && http2asciiEqualFold(res.Header.Get("Content-Encoding"), "gzip") {
9729		res.Header.Del("Content-Encoding")
9730		res.Header.Del("Content-Length")
9731		res.ContentLength = -1
9732		res.Body = &http2gzipReader{body: res.Body}
9733		res.Uncompressed = true
9734	}
9735	return res, nil
9736}
9737
9738func (rl *http2clientConnReadLoop) processTrailers(cs *http2clientStream, f *http2MetaHeadersFrame) error {
9739	if cs.pastTrailers {
9740		// Too many HEADERS frames for this stream.
9741		return http2ConnectionError(http2ErrCodeProtocol)
9742	}
9743	cs.pastTrailers = true
9744	if !f.StreamEnded() {
9745		// We expect that any headers for trailers also
9746		// has END_STREAM.
9747		return http2ConnectionError(http2ErrCodeProtocol)
9748	}
9749	if len(f.PseudoFields()) > 0 {
9750		// No pseudo header fields are defined for trailers.
9751		// TODO: ConnectionError might be overly harsh? Check.
9752		return http2ConnectionError(http2ErrCodeProtocol)
9753	}
9754
9755	trailer := make(Header)
9756	for _, hf := range f.RegularFields() {
9757		key := http2canonicalHeader(hf.Name)
9758		trailer[key] = append(trailer[key], hf.Value)
9759	}
9760	cs.trailer = trailer
9761
9762	rl.endStream(cs)
9763	return nil
9764}
9765
9766// transportResponseBody is the concrete type of Transport.RoundTrip's
9767// Response.Body. It is an io.ReadCloser.
9768type http2transportResponseBody struct {
9769	cs *http2clientStream
9770}
9771
9772func (b http2transportResponseBody) Read(p []byte) (n int, err error) {
9773	cs := b.cs
9774	cc := cs.cc
9775
9776	if cs.readErr != nil {
9777		return 0, cs.readErr
9778	}
9779	n, err = b.cs.bufPipe.Read(p)
9780	if cs.bytesRemain != -1 {
9781		if int64(n) > cs.bytesRemain {
9782			n = int(cs.bytesRemain)
9783			if err == nil {
9784				err = errors.New("net/http: server replied with more than declared Content-Length; truncated")
9785				cs.abortStream(err)
9786			}
9787			cs.readErr = err
9788			return int(cs.bytesRemain), err
9789		}
9790		cs.bytesRemain -= int64(n)
9791		if err == io.EOF && cs.bytesRemain > 0 {
9792			err = io.ErrUnexpectedEOF
9793			cs.readErr = err
9794			return n, err
9795		}
9796	}
9797	if n == 0 {
9798		// No flow control tokens to send back.
9799		return
9800	}
9801
9802	cc.mu.Lock()
9803	connAdd := cc.inflow.add(n)
9804	var streamAdd int32
9805	if err == nil { // No need to refresh if the stream is over or failed.
9806		streamAdd = cs.inflow.add(n)
9807	}
9808	cc.mu.Unlock()
9809
9810	if connAdd != 0 || streamAdd != 0 {
9811		cc.wmu.Lock()
9812		defer cc.wmu.Unlock()
9813		if connAdd != 0 {
9814			cc.fr.WriteWindowUpdate(0, http2mustUint31(connAdd))
9815		}
9816		if streamAdd != 0 {
9817			cc.fr.WriteWindowUpdate(cs.ID, http2mustUint31(streamAdd))
9818		}
9819		cc.bw.Flush()
9820	}
9821	return
9822}
9823
9824var http2errClosedResponseBody = errors.New("http2: response body closed")
9825
9826func (b http2transportResponseBody) Close() error {
9827	cs := b.cs
9828	cc := cs.cc
9829
9830	cs.bufPipe.BreakWithError(http2errClosedResponseBody)
9831	cs.abortStream(http2errClosedResponseBody)
9832
9833	unread := cs.bufPipe.Len()
9834	if unread > 0 {
9835		cc.mu.Lock()
9836		// Return connection-level flow control.
9837		connAdd := cc.inflow.add(unread)
9838		cc.mu.Unlock()
9839
9840		// TODO(dneil): Acquiring this mutex can block indefinitely.
9841		// Move flow control return to a goroutine?
9842		cc.wmu.Lock()
9843		// Return connection-level flow control.
9844		if connAdd > 0 {
9845			cc.fr.WriteWindowUpdate(0, uint32(connAdd))
9846		}
9847		cc.bw.Flush()
9848		cc.wmu.Unlock()
9849	}
9850
9851	select {
9852	case <-cs.donec:
9853	case <-cs.ctx.Done():
9854		// See golang/go#49366: The net/http package can cancel the
9855		// request context after the response body is fully read.
9856		// Don't treat this as an error.
9857		return nil
9858	case <-cs.reqCancel:
9859		return http2errRequestCanceled
9860	}
9861	return nil
9862}
9863
9864func (rl *http2clientConnReadLoop) processData(f *http2DataFrame) error {
9865	cc := rl.cc
9866	cs := rl.streamByID(f.StreamID)
9867	data := f.Data()
9868	if cs == nil {
9869		cc.mu.Lock()
9870		neverSent := cc.nextStreamID
9871		cc.mu.Unlock()
9872		if f.StreamID >= neverSent {
9873			// We never asked for this.
9874			cc.logf("http2: Transport received unsolicited DATA frame; closing connection")
9875			return http2ConnectionError(http2ErrCodeProtocol)
9876		}
9877		// We probably did ask for this, but canceled. Just ignore it.
9878		// TODO: be stricter here? only silently ignore things which
9879		// we canceled, but not things which were closed normally
9880		// by the peer? Tough without accumulating too much state.
9881
9882		// But at least return their flow control:
9883		if f.Length > 0 {
9884			cc.mu.Lock()
9885			ok := cc.inflow.take(f.Length)
9886			connAdd := cc.inflow.add(int(f.Length))
9887			cc.mu.Unlock()
9888			if !ok {
9889				return http2ConnectionError(http2ErrCodeFlowControl)
9890			}
9891			if connAdd > 0 {
9892				cc.wmu.Lock()
9893				cc.fr.WriteWindowUpdate(0, uint32(connAdd))
9894				cc.bw.Flush()
9895				cc.wmu.Unlock()
9896			}
9897		}
9898		return nil
9899	}
9900	if cs.readClosed {
9901		cc.logf("protocol error: received DATA after END_STREAM")
9902		rl.endStreamError(cs, http2StreamError{
9903			StreamID: f.StreamID,
9904			Code:     http2ErrCodeProtocol,
9905		})
9906		return nil
9907	}
9908	if !cs.pastHeaders {
9909		cc.logf("protocol error: received DATA before a HEADERS frame")
9910		rl.endStreamError(cs, http2StreamError{
9911			StreamID: f.StreamID,
9912			Code:     http2ErrCodeProtocol,
9913		})
9914		return nil
9915	}
9916	if f.Length > 0 {
9917		if cs.isHead && len(data) > 0 {
9918			cc.logf("protocol error: received DATA on a HEAD request")
9919			rl.endStreamError(cs, http2StreamError{
9920				StreamID: f.StreamID,
9921				Code:     http2ErrCodeProtocol,
9922			})
9923			return nil
9924		}
9925		// Check connection-level flow control.
9926		cc.mu.Lock()
9927		if !http2takeInflows(&cc.inflow, &cs.inflow, f.Length) {
9928			cc.mu.Unlock()
9929			return http2ConnectionError(http2ErrCodeFlowControl)
9930		}
9931		// Return any padded flow control now, since we won't
9932		// refund it later on body reads.
9933		var refund int
9934		if pad := int(f.Length) - len(data); pad > 0 {
9935			refund += pad
9936		}
9937
9938		didReset := false
9939		var err error
9940		if len(data) > 0 {
9941			if _, err = cs.bufPipe.Write(data); err != nil {
9942				// Return len(data) now if the stream is already closed,
9943				// since data will never be read.
9944				didReset = true
9945				refund += len(data)
9946			}
9947		}
9948
9949		sendConn := cc.inflow.add(refund)
9950		var sendStream int32
9951		if !didReset {
9952			sendStream = cs.inflow.add(refund)
9953		}
9954		cc.mu.Unlock()
9955
9956		if sendConn > 0 || sendStream > 0 {
9957			cc.wmu.Lock()
9958			if sendConn > 0 {
9959				cc.fr.WriteWindowUpdate(0, uint32(sendConn))
9960			}
9961			if sendStream > 0 {
9962				cc.fr.WriteWindowUpdate(cs.ID, uint32(sendStream))
9963			}
9964			cc.bw.Flush()
9965			cc.wmu.Unlock()
9966		}
9967
9968		if err != nil {
9969			rl.endStreamError(cs, err)
9970			return nil
9971		}
9972	}
9973
9974	if f.StreamEnded() {
9975		rl.endStream(cs)
9976	}
9977	return nil
9978}
9979
9980func (rl *http2clientConnReadLoop) endStream(cs *http2clientStream) {
9981	// TODO: check that any declared content-length matches, like
9982	// server.go's (*stream).endStream method.
9983	if !cs.readClosed {
9984		cs.readClosed = true
9985		// Close cs.bufPipe and cs.peerClosed with cc.mu held to avoid a
9986		// race condition: The caller can read io.EOF from Response.Body
9987		// and close the body before we close cs.peerClosed, causing
9988		// cleanupWriteRequest to send a RST_STREAM.
9989		rl.cc.mu.Lock()
9990		defer rl.cc.mu.Unlock()
9991		cs.bufPipe.closeWithErrorAndCode(io.EOF, cs.copyTrailers)
9992		close(cs.peerClosed)
9993	}
9994}
9995
9996func (rl *http2clientConnReadLoop) endStreamError(cs *http2clientStream, err error) {
9997	cs.readAborted = true
9998	cs.abortStream(err)
9999}
10000
10001func (rl *http2clientConnReadLoop) streamByID(id uint32) *http2clientStream {
10002	rl.cc.mu.Lock()
10003	defer rl.cc.mu.Unlock()
10004	cs := rl.cc.streams[id]
10005	if cs != nil && !cs.readAborted {
10006		return cs
10007	}
10008	return nil
10009}
10010
10011func (cs *http2clientStream) copyTrailers() {
10012	for k, vv := range cs.trailer {
10013		t := cs.resTrailer
10014		if *t == nil {
10015			*t = make(Header)
10016		}
10017		(*t)[k] = vv
10018	}
10019}
10020
10021func (rl *http2clientConnReadLoop) processGoAway(f *http2GoAwayFrame) error {
10022	cc := rl.cc
10023	cc.t.connPool().MarkDead(cc)
10024	if f.ErrCode != 0 {
10025		// TODO: deal with GOAWAY more. particularly the error code
10026		cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode)
10027		if fn := cc.t.CountError; fn != nil {
10028			fn("recv_goaway_" + f.ErrCode.stringToken())
10029		}
10030	}
10031	cc.setGoAway(f)
10032	return nil
10033}
10034
10035func (rl *http2clientConnReadLoop) processSettings(f *http2SettingsFrame) error {
10036	cc := rl.cc
10037	// Locking both mu and wmu here allows frame encoding to read settings with only wmu held.
10038	// Acquiring wmu when f.IsAck() is unnecessary, but convenient and mostly harmless.
10039	cc.wmu.Lock()
10040	defer cc.wmu.Unlock()
10041
10042	if err := rl.processSettingsNoWrite(f); err != nil {
10043		return err
10044	}
10045	if !f.IsAck() {
10046		cc.fr.WriteSettingsAck()
10047		cc.bw.Flush()
10048	}
10049	return nil
10050}
10051
10052func (rl *http2clientConnReadLoop) processSettingsNoWrite(f *http2SettingsFrame) error {
10053	cc := rl.cc
10054	cc.mu.Lock()
10055	defer cc.mu.Unlock()
10056
10057	if f.IsAck() {
10058		if cc.wantSettingsAck {
10059			cc.wantSettingsAck = false
10060			return nil
10061		}
10062		return http2ConnectionError(http2ErrCodeProtocol)
10063	}
10064
10065	var seenMaxConcurrentStreams bool
10066	err := f.ForeachSetting(func(s http2Setting) error {
10067		switch s.ID {
10068		case http2SettingMaxFrameSize:
10069			cc.maxFrameSize = s.Val
10070		case http2SettingMaxConcurrentStreams:
10071			cc.maxConcurrentStreams = s.Val
10072			seenMaxConcurrentStreams = true
10073		case http2SettingMaxHeaderListSize:
10074			cc.peerMaxHeaderListSize = uint64(s.Val)
10075		case http2SettingInitialWindowSize:
10076			// Values above the maximum flow-control
10077			// window size of 2^31-1 MUST be treated as a
10078			// connection error (Section 5.4.1) of type
10079			// FLOW_CONTROL_ERROR.
10080			if s.Val > math.MaxInt32 {
10081				return http2ConnectionError(http2ErrCodeFlowControl)
10082			}
10083
10084			// Adjust flow control of currently-open
10085			// frames by the difference of the old initial
10086			// window size and this one.
10087			delta := int32(s.Val) - int32(cc.initialWindowSize)
10088			for _, cs := range cc.streams {
10089				cs.flow.add(delta)
10090			}
10091			cc.cond.Broadcast()
10092
10093			cc.initialWindowSize = s.Val
10094		case http2SettingHeaderTableSize:
10095			cc.henc.SetMaxDynamicTableSize(s.Val)
10096			cc.peerMaxHeaderTableSize = s.Val
10097		default:
10098			cc.vlogf("Unhandled Setting: %v", s)
10099		}
10100		return nil
10101	})
10102	if err != nil {
10103		return err
10104	}
10105
10106	if !cc.seenSettings {
10107		if !seenMaxConcurrentStreams {
10108			// This was the servers initial SETTINGS frame and it
10109			// didn't contain a MAX_CONCURRENT_STREAMS field so
10110			// increase the number of concurrent streams this
10111			// connection can establish to our default.
10112			cc.maxConcurrentStreams = http2defaultMaxConcurrentStreams
10113		}
10114		cc.seenSettings = true
10115	}
10116
10117	return nil
10118}
10119
10120func (rl *http2clientConnReadLoop) processWindowUpdate(f *http2WindowUpdateFrame) error {
10121	cc := rl.cc
10122	cs := rl.streamByID(f.StreamID)
10123	if f.StreamID != 0 && cs == nil {
10124		return nil
10125	}
10126
10127	cc.mu.Lock()
10128	defer cc.mu.Unlock()
10129
10130	fl := &cc.flow
10131	if cs != nil {
10132		fl = &cs.flow
10133	}
10134	if !fl.add(int32(f.Increment)) {
10135		// For stream, the sender sends RST_STREAM with an error code of FLOW_CONTROL_ERROR
10136		if cs != nil {
10137			rl.endStreamError(cs, http2StreamError{
10138				StreamID: f.StreamID,
10139				Code:     http2ErrCodeFlowControl,
10140			})
10141			return nil
10142		}
10143
10144		return http2ConnectionError(http2ErrCodeFlowControl)
10145	}
10146	cc.cond.Broadcast()
10147	return nil
10148}
10149
10150func (rl *http2clientConnReadLoop) processResetStream(f *http2RSTStreamFrame) error {
10151	cs := rl.streamByID(f.StreamID)
10152	if cs == nil {
10153		// TODO: return error if server tries to RST_STREAM an idle stream
10154		return nil
10155	}
10156	serr := http2streamError(cs.ID, f.ErrCode)
10157	serr.Cause = http2errFromPeer
10158	if f.ErrCode == http2ErrCodeProtocol {
10159		rl.cc.SetDoNotReuse()
10160	}
10161	if fn := cs.cc.t.CountError; fn != nil {
10162		fn("recv_rststream_" + f.ErrCode.stringToken())
10163	}
10164	cs.abortStream(serr)
10165
10166	cs.bufPipe.CloseWithError(serr)
10167	return nil
10168}
10169
10170// Ping sends a PING frame to the server and waits for the ack.
10171func (cc *http2ClientConn) Ping(ctx context.Context) error {
10172	c := make(chan struct{})
10173	// Generate a random payload
10174	var p [8]byte
10175	for {
10176		if _, err := rand.Read(p[:]); err != nil {
10177			return err
10178		}
10179		cc.mu.Lock()
10180		// check for dup before insert
10181		if _, found := cc.pings[p]; !found {
10182			cc.pings[p] = c
10183			cc.mu.Unlock()
10184			break
10185		}
10186		cc.mu.Unlock()
10187	}
10188	var pingError error
10189	errc := make(chan struct{})
10190	go func() {
10191		cc.t.markNewGoroutine()
10192		cc.wmu.Lock()
10193		defer cc.wmu.Unlock()
10194		if pingError = cc.fr.WritePing(false, p); pingError != nil {
10195			close(errc)
10196			return
10197		}
10198		if pingError = cc.bw.Flush(); pingError != nil {
10199			close(errc)
10200			return
10201		}
10202	}()
10203	select {
10204	case <-c:
10205		return nil
10206	case <-errc:
10207		return pingError
10208	case <-ctx.Done():
10209		return ctx.Err()
10210	case <-cc.readerDone:
10211		// connection closed
10212		return cc.readerErr
10213	}
10214}
10215
10216func (rl *http2clientConnReadLoop) processPing(f *http2PingFrame) error {
10217	if f.IsAck() {
10218		cc := rl.cc
10219		cc.mu.Lock()
10220		defer cc.mu.Unlock()
10221		// If ack, notify listener if any
10222		if c, ok := cc.pings[f.Data]; ok {
10223			close(c)
10224			delete(cc.pings, f.Data)
10225		}
10226		return nil
10227	}
10228	cc := rl.cc
10229	cc.wmu.Lock()
10230	defer cc.wmu.Unlock()
10231	if err := cc.fr.WritePing(true, f.Data); err != nil {
10232		return err
10233	}
10234	return cc.bw.Flush()
10235}
10236
10237func (rl *http2clientConnReadLoop) processPushPromise(f *http2PushPromiseFrame) error {
10238	// We told the peer we don't want them.
10239	// Spec says:
10240	// "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH
10241	// setting of the peer endpoint is set to 0. An endpoint that
10242	// has set this setting and has received acknowledgement MUST
10243	// treat the receipt of a PUSH_PROMISE frame as a connection
10244	// error (Section 5.4.1) of type PROTOCOL_ERROR."
10245	return http2ConnectionError(http2ErrCodeProtocol)
10246}
10247
10248func (cc *http2ClientConn) writeStreamReset(streamID uint32, code http2ErrCode, err error) {
10249	// TODO: map err to more interesting error codes, once the
10250	// HTTP community comes up with some. But currently for
10251	// RST_STREAM there's no equivalent to GOAWAY frame's debug
10252	// data, and the error codes are all pretty vague ("cancel").
10253	cc.wmu.Lock()
10254	cc.fr.WriteRSTStream(streamID, code)
10255	cc.bw.Flush()
10256	cc.wmu.Unlock()
10257}
10258
10259var (
10260	http2errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit")
10261	http2errRequestHeaderListSize  = errors.New("http2: request header list larger than peer's advertised limit")
10262)
10263
10264func (cc *http2ClientConn) logf(format string, args ...interface{}) {
10265	cc.t.logf(format, args...)
10266}
10267
10268func (cc *http2ClientConn) vlogf(format string, args ...interface{}) {
10269	cc.t.vlogf(format, args...)
10270}
10271
10272func (t *http2Transport) vlogf(format string, args ...interface{}) {
10273	if http2VerboseLogs {
10274		t.logf(format, args...)
10275	}
10276}
10277
10278func (t *http2Transport) logf(format string, args ...interface{}) {
10279	log.Printf(format, args...)
10280}
10281
10282var http2noBody io.ReadCloser = http2noBodyReader{}
10283
10284type http2noBodyReader struct{}
10285
10286func (http2noBodyReader) Close() error { return nil }
10287
10288func (http2noBodyReader) Read([]byte) (int, error) { return 0, io.EOF }
10289
10290type http2missingBody struct{}
10291
10292func (http2missingBody) Close() error { return nil }
10293
10294func (http2missingBody) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF }
10295
10296func http2strSliceContains(ss []string, s string) bool {
10297	for _, v := range ss {
10298		if v == s {
10299			return true
10300		}
10301	}
10302	return false
10303}
10304
10305type http2erringRoundTripper struct{ err error }
10306
10307func (rt http2erringRoundTripper) RoundTripErr() error { return rt.err }
10308
10309func (rt http2erringRoundTripper) RoundTrip(*Request) (*Response, error) { return nil, rt.err }
10310
10311// gzipReader wraps a response body so it can lazily
10312// call gzip.NewReader on the first call to Read
10313type http2gzipReader struct {
10314	_    http2incomparable
10315	body io.ReadCloser // underlying Response.Body
10316	zr   *gzip.Reader  // lazily-initialized gzip reader
10317	zerr error         // sticky error
10318}
10319
10320func (gz *http2gzipReader) Read(p []byte) (n int, err error) {
10321	if gz.zerr != nil {
10322		return 0, gz.zerr
10323	}
10324	if gz.zr == nil {
10325		gz.zr, err = gzip.NewReader(gz.body)
10326		if err != nil {
10327			gz.zerr = err
10328			return 0, err
10329		}
10330	}
10331	return gz.zr.Read(p)
10332}
10333
10334func (gz *http2gzipReader) Close() error {
10335	if err := gz.body.Close(); err != nil {
10336		return err
10337	}
10338	gz.zerr = fs.ErrClosed
10339	return nil
10340}
10341
10342type http2errorReader struct{ err error }
10343
10344func (r http2errorReader) Read(p []byte) (int, error) { return 0, r.err }
10345
10346// isConnectionCloseRequest reports whether req should use its own
10347// connection for a single request and then close the connection.
10348func http2isConnectionCloseRequest(req *Request) bool {
10349	return req.Close || httpguts.HeaderValuesContainsToken(req.Header["Connection"], "close")
10350}
10351
10352// registerHTTPSProtocol calls Transport.RegisterProtocol but
10353// converting panics into errors.
10354func http2registerHTTPSProtocol(t *Transport, rt http2noDialH2RoundTripper) (err error) {
10355	defer func() {
10356		if e := recover(); e != nil {
10357			err = fmt.Errorf("%v", e)
10358		}
10359	}()
10360	t.RegisterProtocol("https", rt)
10361	return nil
10362}
10363
10364// noDialH2RoundTripper is a RoundTripper which only tries to complete the request
10365// if there's already has a cached connection to the host.
10366// (The field is exported so it can be accessed via reflect from net/http; tested
10367// by TestNoDialH2RoundTripperType)
10368type http2noDialH2RoundTripper struct{ *http2Transport }
10369
10370func (rt http2noDialH2RoundTripper) RoundTrip(req *Request) (*Response, error) {
10371	res, err := rt.http2Transport.RoundTrip(req)
10372	if http2isNoCachedConnError(err) {
10373		return nil, ErrSkipAltProtocol
10374	}
10375	return res, err
10376}
10377
10378func (t *http2Transport) idleConnTimeout() time.Duration {
10379	// to keep things backwards compatible, we use non-zero values of
10380	// IdleConnTimeout, followed by using the IdleConnTimeout on the underlying
10381	// http1 transport, followed by 0
10382	if t.IdleConnTimeout != 0 {
10383		return t.IdleConnTimeout
10384	}
10385
10386	if t.t1 != nil {
10387		return t.t1.IdleConnTimeout
10388	}
10389
10390	return 0
10391}
10392
10393func http2traceGetConn(req *Request, hostPort string) {
10394	trace := httptrace.ContextClientTrace(req.Context())
10395	if trace == nil || trace.GetConn == nil {
10396		return
10397	}
10398	trace.GetConn(hostPort)
10399}
10400
10401func http2traceGotConn(req *Request, cc *http2ClientConn, reused bool) {
10402	trace := httptrace.ContextClientTrace(req.Context())
10403	if trace == nil || trace.GotConn == nil {
10404		return
10405	}
10406	ci := httptrace.GotConnInfo{Conn: cc.tconn}
10407	ci.Reused = reused
10408	cc.mu.Lock()
10409	ci.WasIdle = len(cc.streams) == 0 && reused
10410	if ci.WasIdle && !cc.lastActive.IsZero() {
10411		ci.IdleTime = time.Since(cc.lastActive)
10412	}
10413	cc.mu.Unlock()
10414
10415	trace.GotConn(ci)
10416}
10417
10418func http2traceWroteHeaders(trace *httptrace.ClientTrace) {
10419	if trace != nil && trace.WroteHeaders != nil {
10420		trace.WroteHeaders()
10421	}
10422}
10423
10424func http2traceGot100Continue(trace *httptrace.ClientTrace) {
10425	if trace != nil && trace.Got100Continue != nil {
10426		trace.Got100Continue()
10427	}
10428}
10429
10430func http2traceWait100Continue(trace *httptrace.ClientTrace) {
10431	if trace != nil && trace.Wait100Continue != nil {
10432		trace.Wait100Continue()
10433	}
10434}
10435
10436func http2traceWroteRequest(trace *httptrace.ClientTrace, err error) {
10437	if trace != nil && trace.WroteRequest != nil {
10438		trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
10439	}
10440}
10441
10442func http2traceFirstResponseByte(trace *httptrace.ClientTrace) {
10443	if trace != nil && trace.GotFirstResponseByte != nil {
10444		trace.GotFirstResponseByte()
10445	}
10446}
10447
10448func http2traceHasWroteHeaderField(trace *httptrace.ClientTrace) bool {
10449	return trace != nil && trace.WroteHeaderField != nil
10450}
10451
10452func http2traceWroteHeaderField(trace *httptrace.ClientTrace, k, v string) {
10453	if trace != nil && trace.WroteHeaderField != nil {
10454		trace.WroteHeaderField(k, []string{v})
10455	}
10456}
10457
10458func http2traceGot1xxResponseFunc(trace *httptrace.ClientTrace) func(int, textproto.MIMEHeader) error {
10459	if trace != nil {
10460		return trace.Got1xxResponse
10461	}
10462	return nil
10463}
10464
10465// dialTLSWithContext uses tls.Dialer, added in Go 1.15, to open a TLS
10466// connection.
10467func (t *http2Transport) dialTLSWithContext(ctx context.Context, network, addr string, cfg *tls.Config) (*tls.Conn, error) {
10468	dialer := &tls.Dialer{
10469		Config: cfg,
10470	}
10471	cn, err := dialer.DialContext(ctx, network, addr)
10472	if err != nil {
10473		return nil, err
10474	}
10475	tlsCn := cn.(*tls.Conn) // DialContext comment promises this will always succeed
10476	return tlsCn, nil
10477}
10478
10479// writeFramer is implemented by any type that is used to write frames.
10480type http2writeFramer interface {
10481	writeFrame(http2writeContext) error
10482
10483	// staysWithinBuffer reports whether this writer promises that
10484	// it will only write less than or equal to size bytes, and it
10485	// won't Flush the write context.
10486	staysWithinBuffer(size int) bool
10487}
10488
10489// writeContext is the interface needed by the various frame writer
10490// types below. All the writeFrame methods below are scheduled via the
10491// frame writing scheduler (see writeScheduler in writesched.go).
10492//
10493// This interface is implemented by *serverConn.
10494//
10495// TODO: decide whether to a) use this in the client code (which didn't
10496// end up using this yet, because it has a simpler design, not
10497// currently implementing priorities), or b) delete this and
10498// make the server code a bit more concrete.
10499type http2writeContext interface {
10500	Framer() *http2Framer
10501	Flush() error
10502	CloseConn() error
10503	// HeaderEncoder returns an HPACK encoder that writes to the
10504	// returned buffer.
10505	HeaderEncoder() (*hpack.Encoder, *bytes.Buffer)
10506}
10507
10508// writeEndsStream reports whether w writes a frame that will transition
10509// the stream to a half-closed local state. This returns false for RST_STREAM,
10510// which closes the entire stream (not just the local half).
10511func http2writeEndsStream(w http2writeFramer) bool {
10512	switch v := w.(type) {
10513	case *http2writeData:
10514		return v.endStream
10515	case *http2writeResHeaders:
10516		return v.endStream
10517	case nil:
10518		// This can only happen if the caller reuses w after it's
10519		// been intentionally nil'ed out to prevent use. Keep this
10520		// here to catch future refactoring breaking it.
10521		panic("writeEndsStream called on nil writeFramer")
10522	}
10523	return false
10524}
10525
10526type http2flushFrameWriter struct{}
10527
10528func (http2flushFrameWriter) writeFrame(ctx http2writeContext) error {
10529	return ctx.Flush()
10530}
10531
10532func (http2flushFrameWriter) staysWithinBuffer(max int) bool { return false }
10533
10534type http2writeSettings []http2Setting
10535
10536func (s http2writeSettings) staysWithinBuffer(max int) bool {
10537	const settingSize = 6 // uint16 + uint32
10538	return http2frameHeaderLen+settingSize*len(s) <= max
10539
10540}
10541
10542func (s http2writeSettings) writeFrame(ctx http2writeContext) error {
10543	return ctx.Framer().WriteSettings([]http2Setting(s)...)
10544}
10545
10546type http2writeGoAway struct {
10547	maxStreamID uint32
10548	code        http2ErrCode
10549}
10550
10551func (p *http2writeGoAway) writeFrame(ctx http2writeContext) error {
10552	err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil)
10553	ctx.Flush() // ignore error: we're hanging up on them anyway
10554	return err
10555}
10556
10557func (*http2writeGoAway) staysWithinBuffer(max int) bool { return false } // flushes
10558
10559type http2writeData struct {
10560	streamID  uint32
10561	p         []byte
10562	endStream bool
10563}
10564
10565func (w *http2writeData) String() string {
10566	return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream)
10567}
10568
10569func (w *http2writeData) writeFrame(ctx http2writeContext) error {
10570	return ctx.Framer().WriteData(w.streamID, w.endStream, w.p)
10571}
10572
10573func (w *http2writeData) staysWithinBuffer(max int) bool {
10574	return http2frameHeaderLen+len(w.p) <= max
10575}
10576
10577// handlerPanicRST is the message sent from handler goroutines when
10578// the handler panics.
10579type http2handlerPanicRST struct {
10580	StreamID uint32
10581}
10582
10583func (hp http2handlerPanicRST) writeFrame(ctx http2writeContext) error {
10584	return ctx.Framer().WriteRSTStream(hp.StreamID, http2ErrCodeInternal)
10585}
10586
10587func (hp http2handlerPanicRST) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max }
10588
10589func (se http2StreamError) writeFrame(ctx http2writeContext) error {
10590	return ctx.Framer().WriteRSTStream(se.StreamID, se.Code)
10591}
10592
10593func (se http2StreamError) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max }
10594
10595type http2writePingAck struct{ pf *http2PingFrame }
10596
10597func (w http2writePingAck) writeFrame(ctx http2writeContext) error {
10598	return ctx.Framer().WritePing(true, w.pf.Data)
10599}
10600
10601func (w http2writePingAck) staysWithinBuffer(max int) bool {
10602	return http2frameHeaderLen+len(w.pf.Data) <= max
10603}
10604
10605type http2writeSettingsAck struct{}
10606
10607func (http2writeSettingsAck) writeFrame(ctx http2writeContext) error {
10608	return ctx.Framer().WriteSettingsAck()
10609}
10610
10611func (http2writeSettingsAck) staysWithinBuffer(max int) bool { return http2frameHeaderLen <= max }
10612
10613// splitHeaderBlock splits headerBlock into fragments so that each fragment fits
10614// in a single frame, then calls fn for each fragment. firstFrag/lastFrag are true
10615// for the first/last fragment, respectively.
10616func http2splitHeaderBlock(ctx http2writeContext, headerBlock []byte, fn func(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error) error {
10617	// For now we're lazy and just pick the minimum MAX_FRAME_SIZE
10618	// that all peers must support (16KB). Later we could care
10619	// more and send larger frames if the peer advertised it, but
10620	// there's little point. Most headers are small anyway (so we
10621	// generally won't have CONTINUATION frames), and extra frames
10622	// only waste 9 bytes anyway.
10623	const maxFrameSize = 16384
10624
10625	first := true
10626	for len(headerBlock) > 0 {
10627		frag := headerBlock
10628		if len(frag) > maxFrameSize {
10629			frag = frag[:maxFrameSize]
10630		}
10631		headerBlock = headerBlock[len(frag):]
10632		if err := fn(ctx, frag, first, len(headerBlock) == 0); err != nil {
10633			return err
10634		}
10635		first = false
10636	}
10637	return nil
10638}
10639
10640// writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames
10641// for HTTP response headers or trailers from a server handler.
10642type http2writeResHeaders struct {
10643	streamID    uint32
10644	httpResCode int      // 0 means no ":status" line
10645	h           Header   // may be nil
10646	trailers    []string // if non-nil, which keys of h to write. nil means all.
10647	endStream   bool
10648
10649	date          string
10650	contentType   string
10651	contentLength string
10652}
10653
10654func http2encKV(enc *hpack.Encoder, k, v string) {
10655	if http2VerboseLogs {
10656		log.Printf("http2: server encoding header %q = %q", k, v)
10657	}
10658	enc.WriteField(hpack.HeaderField{Name: k, Value: v})
10659}
10660
10661func (w *http2writeResHeaders) staysWithinBuffer(max int) bool {
10662	// TODO: this is a common one. It'd be nice to return true
10663	// here and get into the fast path if we could be clever and
10664	// calculate the size fast enough, or at least a conservative
10665	// upper bound that usually fires. (Maybe if w.h and
10666	// w.trailers are nil, so we don't need to enumerate it.)
10667	// Otherwise I'm afraid that just calculating the length to
10668	// answer this question would be slower than the ~2µs benefit.
10669	return false
10670}
10671
10672func (w *http2writeResHeaders) writeFrame(ctx http2writeContext) error {
10673	enc, buf := ctx.HeaderEncoder()
10674	buf.Reset()
10675
10676	if w.httpResCode != 0 {
10677		http2encKV(enc, ":status", http2httpCodeString(w.httpResCode))
10678	}
10679
10680	http2encodeHeaders(enc, w.h, w.trailers)
10681
10682	if w.contentType != "" {
10683		http2encKV(enc, "content-type", w.contentType)
10684	}
10685	if w.contentLength != "" {
10686		http2encKV(enc, "content-length", w.contentLength)
10687	}
10688	if w.date != "" {
10689		http2encKV(enc, "date", w.date)
10690	}
10691
10692	headerBlock := buf.Bytes()
10693	if len(headerBlock) == 0 && w.trailers == nil {
10694		panic("unexpected empty hpack")
10695	}
10696
10697	return http2splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
10698}
10699
10700func (w *http2writeResHeaders) writeHeaderBlock(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error {
10701	if firstFrag {
10702		return ctx.Framer().WriteHeaders(http2HeadersFrameParam{
10703			StreamID:      w.streamID,
10704			BlockFragment: frag,
10705			EndStream:     w.endStream,
10706			EndHeaders:    lastFrag,
10707		})
10708	} else {
10709		return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
10710	}
10711}
10712
10713// writePushPromise is a request to write a PUSH_PROMISE and 0+ CONTINUATION frames.
10714type http2writePushPromise struct {
10715	streamID uint32   // pusher stream
10716	method   string   // for :method
10717	url      *url.URL // for :scheme, :authority, :path
10718	h        Header
10719
10720	// Creates an ID for a pushed stream. This runs on serveG just before
10721	// the frame is written. The returned ID is copied to promisedID.
10722	allocatePromisedID func() (uint32, error)
10723	promisedID         uint32
10724}
10725
10726func (w *http2writePushPromise) staysWithinBuffer(max int) bool {
10727	// TODO: see writeResHeaders.staysWithinBuffer
10728	return false
10729}
10730
10731func (w *http2writePushPromise) writeFrame(ctx http2writeContext) error {
10732	enc, buf := ctx.HeaderEncoder()
10733	buf.Reset()
10734
10735	http2encKV(enc, ":method", w.method)
10736	http2encKV(enc, ":scheme", w.url.Scheme)
10737	http2encKV(enc, ":authority", w.url.Host)
10738	http2encKV(enc, ":path", w.url.RequestURI())
10739	http2encodeHeaders(enc, w.h, nil)
10740
10741	headerBlock := buf.Bytes()
10742	if len(headerBlock) == 0 {
10743		panic("unexpected empty hpack")
10744	}
10745
10746	return http2splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
10747}
10748
10749func (w *http2writePushPromise) writeHeaderBlock(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error {
10750	if firstFrag {
10751		return ctx.Framer().WritePushPromise(http2PushPromiseParam{
10752			StreamID:      w.streamID,
10753			PromiseID:     w.promisedID,
10754			BlockFragment: frag,
10755			EndHeaders:    lastFrag,
10756		})
10757	} else {
10758		return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
10759	}
10760}
10761
10762type http2write100ContinueHeadersFrame struct {
10763	streamID uint32
10764}
10765
10766func (w http2write100ContinueHeadersFrame) writeFrame(ctx http2writeContext) error {
10767	enc, buf := ctx.HeaderEncoder()
10768	buf.Reset()
10769	http2encKV(enc, ":status", "100")
10770	return ctx.Framer().WriteHeaders(http2HeadersFrameParam{
10771		StreamID:      w.streamID,
10772		BlockFragment: buf.Bytes(),
10773		EndStream:     false,
10774		EndHeaders:    true,
10775	})
10776}
10777
10778func (w http2write100ContinueHeadersFrame) staysWithinBuffer(max int) bool {
10779	// Sloppy but conservative:
10780	return 9+2*(len(":status")+len("100")) <= max
10781}
10782
10783type http2writeWindowUpdate struct {
10784	streamID uint32 // or 0 for conn-level
10785	n        uint32
10786}
10787
10788func (wu http2writeWindowUpdate) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max }
10789
10790func (wu http2writeWindowUpdate) writeFrame(ctx http2writeContext) error {
10791	return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n)
10792}
10793
10794// encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
10795// is encoded only if k is in keys.
10796func http2encodeHeaders(enc *hpack.Encoder, h Header, keys []string) {
10797	if keys == nil {
10798		sorter := http2sorterPool.Get().(*http2sorter)
10799		// Using defer here, since the returned keys from the
10800		// sorter.Keys method is only valid until the sorter
10801		// is returned:
10802		defer http2sorterPool.Put(sorter)
10803		keys = sorter.Keys(h)
10804	}
10805	for _, k := range keys {
10806		vv := h[k]
10807		k, ascii := http2lowerHeader(k)
10808		if !ascii {
10809			// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
10810			// field names have to be ASCII characters (just as in HTTP/1.x).
10811			continue
10812		}
10813		if !http2validWireHeaderFieldName(k) {
10814			// Skip it as backup paranoia. Per
10815			// golang.org/issue/14048, these should
10816			// already be rejected at a higher level.
10817			continue
10818		}
10819		isTE := k == "transfer-encoding"
10820		for _, v := range vv {
10821			if !httpguts.ValidHeaderFieldValue(v) {
10822				// TODO: return an error? golang.org/issue/14048
10823				// For now just omit it.
10824				continue
10825			}
10826			// TODO: more of "8.1.2.2 Connection-Specific Header Fields"
10827			if isTE && v != "trailers" {
10828				continue
10829			}
10830			http2encKV(enc, k, v)
10831		}
10832	}
10833}
10834
10835// WriteScheduler is the interface implemented by HTTP/2 write schedulers.
10836// Methods are never called concurrently.
10837type http2WriteScheduler interface {
10838	// OpenStream opens a new stream in the write scheduler.
10839	// It is illegal to call this with streamID=0 or with a streamID that is
10840	// already open -- the call may panic.
10841	OpenStream(streamID uint32, options http2OpenStreamOptions)
10842
10843	// CloseStream closes a stream in the write scheduler. Any frames queued on
10844	// this stream should be discarded. It is illegal to call this on a stream
10845	// that is not open -- the call may panic.
10846	CloseStream(streamID uint32)
10847
10848	// AdjustStream adjusts the priority of the given stream. This may be called
10849	// on a stream that has not yet been opened or has been closed. Note that
10850	// RFC 7540 allows PRIORITY frames to be sent on streams in any state. See:
10851	// https://tools.ietf.org/html/rfc7540#section-5.1
10852	AdjustStream(streamID uint32, priority http2PriorityParam)
10853
10854	// Push queues a frame in the scheduler. In most cases, this will not be
10855	// called with wr.StreamID()!=0 unless that stream is currently open. The one
10856	// exception is RST_STREAM frames, which may be sent on idle or closed streams.
10857	Push(wr http2FrameWriteRequest)
10858
10859	// Pop dequeues the next frame to write. Returns false if no frames can
10860	// be written. Frames with a given wr.StreamID() are Pop'd in the same
10861	// order they are Push'd, except RST_STREAM frames. No frames should be
10862	// discarded except by CloseStream.
10863	Pop() (wr http2FrameWriteRequest, ok bool)
10864}
10865
10866// OpenStreamOptions specifies extra options for WriteScheduler.OpenStream.
10867type http2OpenStreamOptions struct {
10868	// PusherID is zero if the stream was initiated by the client. Otherwise,
10869	// PusherID names the stream that pushed the newly opened stream.
10870	PusherID uint32
10871}
10872
10873// FrameWriteRequest is a request to write a frame.
10874type http2FrameWriteRequest struct {
10875	// write is the interface value that does the writing, once the
10876	// WriteScheduler has selected this frame to write. The write
10877	// functions are all defined in write.go.
10878	write http2writeFramer
10879
10880	// stream is the stream on which this frame will be written.
10881	// nil for non-stream frames like PING and SETTINGS.
10882	// nil for RST_STREAM streams, which use the StreamError.StreamID field instead.
10883	stream *http2stream
10884
10885	// done, if non-nil, must be a buffered channel with space for
10886	// 1 message and is sent the return value from write (or an
10887	// earlier error) when the frame has been written.
10888	done chan error
10889}
10890
10891// StreamID returns the id of the stream this frame will be written to.
10892// 0 is used for non-stream frames such as PING and SETTINGS.
10893func (wr http2FrameWriteRequest) StreamID() uint32 {
10894	if wr.stream == nil {
10895		if se, ok := wr.write.(http2StreamError); ok {
10896			// (*serverConn).resetStream doesn't set
10897			// stream because it doesn't necessarily have
10898			// one. So special case this type of write
10899			// message.
10900			return se.StreamID
10901		}
10902		return 0
10903	}
10904	return wr.stream.id
10905}
10906
10907// isControl reports whether wr is a control frame for MaxQueuedControlFrames
10908// purposes. That includes non-stream frames and RST_STREAM frames.
10909func (wr http2FrameWriteRequest) isControl() bool {
10910	return wr.stream == nil
10911}
10912
10913// DataSize returns the number of flow control bytes that must be consumed
10914// to write this entire frame. This is 0 for non-DATA frames.
10915func (wr http2FrameWriteRequest) DataSize() int {
10916	if wd, ok := wr.write.(*http2writeData); ok {
10917		return len(wd.p)
10918	}
10919	return 0
10920}
10921
10922// Consume consumes min(n, available) bytes from this frame, where available
10923// is the number of flow control bytes available on the stream. Consume returns
10924// 0, 1, or 2 frames, where the integer return value gives the number of frames
10925// returned.
10926//
10927// If flow control prevents consuming any bytes, this returns (_, _, 0). If
10928// the entire frame was consumed, this returns (wr, _, 1). Otherwise, this
10929// returns (consumed, rest, 2), where 'consumed' contains the consumed bytes and
10930// 'rest' contains the remaining bytes. The consumed bytes are deducted from the
10931// underlying stream's flow control budget.
10932func (wr http2FrameWriteRequest) Consume(n int32) (http2FrameWriteRequest, http2FrameWriteRequest, int) {
10933	var empty http2FrameWriteRequest
10934
10935	// Non-DATA frames are always consumed whole.
10936	wd, ok := wr.write.(*http2writeData)
10937	if !ok || len(wd.p) == 0 {
10938		return wr, empty, 1
10939	}
10940
10941	// Might need to split after applying limits.
10942	allowed := wr.stream.flow.available()
10943	if n < allowed {
10944		allowed = n
10945	}
10946	if wr.stream.sc.maxFrameSize < allowed {
10947		allowed = wr.stream.sc.maxFrameSize
10948	}
10949	if allowed <= 0 {
10950		return empty, empty, 0
10951	}
10952	if len(wd.p) > int(allowed) {
10953		wr.stream.flow.take(allowed)
10954		consumed := http2FrameWriteRequest{
10955			stream: wr.stream,
10956			write: &http2writeData{
10957				streamID: wd.streamID,
10958				p:        wd.p[:allowed],
10959				// Even if the original had endStream set, there
10960				// are bytes remaining because len(wd.p) > allowed,
10961				// so we know endStream is false.
10962				endStream: false,
10963			},
10964			// Our caller is blocking on the final DATA frame, not
10965			// this intermediate frame, so no need to wait.
10966			done: nil,
10967		}
10968		rest := http2FrameWriteRequest{
10969			stream: wr.stream,
10970			write: &http2writeData{
10971				streamID:  wd.streamID,
10972				p:         wd.p[allowed:],
10973				endStream: wd.endStream,
10974			},
10975			done: wr.done,
10976		}
10977		return consumed, rest, 2
10978	}
10979
10980	// The frame is consumed whole.
10981	// NB: This cast cannot overflow because allowed is <= math.MaxInt32.
10982	wr.stream.flow.take(int32(len(wd.p)))
10983	return wr, empty, 1
10984}
10985
10986// String is for debugging only.
10987func (wr http2FrameWriteRequest) String() string {
10988	var des string
10989	if s, ok := wr.write.(fmt.Stringer); ok {
10990		des = s.String()
10991	} else {
10992		des = fmt.Sprintf("%T", wr.write)
10993	}
10994	return fmt.Sprintf("[FrameWriteRequest stream=%d, ch=%v, writer=%v]", wr.StreamID(), wr.done != nil, des)
10995}
10996
10997// replyToWriter sends err to wr.done and panics if the send must block
10998// This does nothing if wr.done is nil.
10999func (wr *http2FrameWriteRequest) replyToWriter(err error) {
11000	if wr.done == nil {
11001		return
11002	}
11003	select {
11004	case wr.done <- err:
11005	default:
11006		panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wr.write))
11007	}
11008	wr.write = nil // prevent use (assume it's tainted after wr.done send)
11009}
11010
11011// writeQueue is used by implementations of WriteScheduler.
11012type http2writeQueue struct {
11013	s          []http2FrameWriteRequest
11014	prev, next *http2writeQueue
11015}
11016
11017func (q *http2writeQueue) empty() bool { return len(q.s) == 0 }
11018
11019func (q *http2writeQueue) push(wr http2FrameWriteRequest) {
11020	q.s = append(q.s, wr)
11021}
11022
11023func (q *http2writeQueue) shift() http2FrameWriteRequest {
11024	if len(q.s) == 0 {
11025		panic("invalid use of queue")
11026	}
11027	wr := q.s[0]
11028	// TODO: less copy-happy queue.
11029	copy(q.s, q.s[1:])
11030	q.s[len(q.s)-1] = http2FrameWriteRequest{}
11031	q.s = q.s[:len(q.s)-1]
11032	return wr
11033}
11034
11035// consume consumes up to n bytes from q.s[0]. If the frame is
11036// entirely consumed, it is removed from the queue. If the frame
11037// is partially consumed, the frame is kept with the consumed
11038// bytes removed. Returns true iff any bytes were consumed.
11039func (q *http2writeQueue) consume(n int32) (http2FrameWriteRequest, bool) {
11040	if len(q.s) == 0 {
11041		return http2FrameWriteRequest{}, false
11042	}
11043	consumed, rest, numresult := q.s[0].Consume(n)
11044	switch numresult {
11045	case 0:
11046		return http2FrameWriteRequest{}, false
11047	case 1:
11048		q.shift()
11049	case 2:
11050		q.s[0] = rest
11051	}
11052	return consumed, true
11053}
11054
11055type http2writeQueuePool []*http2writeQueue
11056
11057// put inserts an unused writeQueue into the pool.
11058
11059// put inserts an unused writeQueue into the pool.
11060func (p *http2writeQueuePool) put(q *http2writeQueue) {
11061	for i := range q.s {
11062		q.s[i] = http2FrameWriteRequest{}
11063	}
11064	q.s = q.s[:0]
11065	*p = append(*p, q)
11066}
11067
11068// get returns an empty writeQueue.
11069func (p *http2writeQueuePool) get() *http2writeQueue {
11070	ln := len(*p)
11071	if ln == 0 {
11072		return new(http2writeQueue)
11073	}
11074	x := ln - 1
11075	q := (*p)[x]
11076	(*p)[x] = nil
11077	*p = (*p)[:x]
11078	return q
11079}
11080
11081// RFC 7540, Section 5.3.5: the default weight is 16.
11082const http2priorityDefaultWeight = 15 // 16 = 15 + 1
11083
11084// PriorityWriteSchedulerConfig configures a priorityWriteScheduler.
11085type http2PriorityWriteSchedulerConfig struct {
11086	// MaxClosedNodesInTree controls the maximum number of closed streams to
11087	// retain in the priority tree. Setting this to zero saves a small amount
11088	// of memory at the cost of performance.
11089	//
11090	// See RFC 7540, Section 5.3.4:
11091	//   "It is possible for a stream to become closed while prioritization
11092	//   information ... is in transit. ... This potentially creates suboptimal
11093	//   prioritization, since the stream could be given a priority that is
11094	//   different from what is intended. To avoid these problems, an endpoint
11095	//   SHOULD retain stream prioritization state for a period after streams
11096	//   become closed. The longer state is retained, the lower the chance that
11097	//   streams are assigned incorrect or default priority values."
11098	MaxClosedNodesInTree int
11099
11100	// MaxIdleNodesInTree controls the maximum number of idle streams to
11101	// retain in the priority tree. Setting this to zero saves a small amount
11102	// of memory at the cost of performance.
11103	//
11104	// See RFC 7540, Section 5.3.4:
11105	//   Similarly, streams that are in the "idle" state can be assigned
11106	//   priority or become a parent of other streams. This allows for the
11107	//   creation of a grouping node in the dependency tree, which enables
11108	//   more flexible expressions of priority. Idle streams begin with a
11109	//   default priority (Section 5.3.5).
11110	MaxIdleNodesInTree int
11111
11112	// ThrottleOutOfOrderWrites enables write throttling to help ensure that
11113	// data is delivered in priority order. This works around a race where
11114	// stream B depends on stream A and both streams are about to call Write
11115	// to queue DATA frames. If B wins the race, a naive scheduler would eagerly
11116	// write as much data from B as possible, but this is suboptimal because A
11117	// is a higher-priority stream. With throttling enabled, we write a small
11118	// amount of data from B to minimize the amount of bandwidth that B can
11119	// steal from A.
11120	ThrottleOutOfOrderWrites bool
11121}
11122
11123// NewPriorityWriteScheduler constructs a WriteScheduler that schedules
11124// frames by following HTTP/2 priorities as described in RFC 7540 Section 5.3.
11125// If cfg is nil, default options are used.
11126func http2NewPriorityWriteScheduler(cfg *http2PriorityWriteSchedulerConfig) http2WriteScheduler {
11127	if cfg == nil {
11128		// For justification of these defaults, see:
11129		// https://docs.google.com/document/d/1oLhNg1skaWD4_DtaoCxdSRN5erEXrH-KnLrMwEpOtFY
11130		cfg = &http2PriorityWriteSchedulerConfig{
11131			MaxClosedNodesInTree:     10,
11132			MaxIdleNodesInTree:       10,
11133			ThrottleOutOfOrderWrites: false,
11134		}
11135	}
11136
11137	ws := &http2priorityWriteScheduler{
11138		nodes:                make(map[uint32]*http2priorityNode),
11139		maxClosedNodesInTree: cfg.MaxClosedNodesInTree,
11140		maxIdleNodesInTree:   cfg.MaxIdleNodesInTree,
11141		enableWriteThrottle:  cfg.ThrottleOutOfOrderWrites,
11142	}
11143	ws.nodes[0] = &ws.root
11144	if cfg.ThrottleOutOfOrderWrites {
11145		ws.writeThrottleLimit = 1024
11146	} else {
11147		ws.writeThrottleLimit = math.MaxInt32
11148	}
11149	return ws
11150}
11151
11152type http2priorityNodeState int
11153
11154const (
11155	http2priorityNodeOpen http2priorityNodeState = iota
11156	http2priorityNodeClosed
11157	http2priorityNodeIdle
11158)
11159
11160// priorityNode is a node in an HTTP/2 priority tree.
11161// Each node is associated with a single stream ID.
11162// See RFC 7540, Section 5.3.
11163type http2priorityNode struct {
11164	q            http2writeQueue        // queue of pending frames to write
11165	id           uint32                 // id of the stream, or 0 for the root of the tree
11166	weight       uint8                  // the actual weight is weight+1, so the value is in [1,256]
11167	state        http2priorityNodeState // open | closed | idle
11168	bytes        int64                  // number of bytes written by this node, or 0 if closed
11169	subtreeBytes int64                  // sum(node.bytes) of all nodes in this subtree
11170
11171	// These links form the priority tree.
11172	parent     *http2priorityNode
11173	kids       *http2priorityNode // start of the kids list
11174	prev, next *http2priorityNode // doubly-linked list of siblings
11175}
11176
11177func (n *http2priorityNode) setParent(parent *http2priorityNode) {
11178	if n == parent {
11179		panic("setParent to self")
11180	}
11181	if n.parent == parent {
11182		return
11183	}
11184	// Unlink from current parent.
11185	if parent := n.parent; parent != nil {
11186		if n.prev == nil {
11187			parent.kids = n.next
11188		} else {
11189			n.prev.next = n.next
11190		}
11191		if n.next != nil {
11192			n.next.prev = n.prev
11193		}
11194	}
11195	// Link to new parent.
11196	// If parent=nil, remove n from the tree.
11197	// Always insert at the head of parent.kids (this is assumed by walkReadyInOrder).
11198	n.parent = parent
11199	if parent == nil {
11200		n.next = nil
11201		n.prev = nil
11202	} else {
11203		n.next = parent.kids
11204		n.prev = nil
11205		if n.next != nil {
11206			n.next.prev = n
11207		}
11208		parent.kids = n
11209	}
11210}
11211
11212func (n *http2priorityNode) addBytes(b int64) {
11213	n.bytes += b
11214	for ; n != nil; n = n.parent {
11215		n.subtreeBytes += b
11216	}
11217}
11218
11219// walkReadyInOrder iterates over the tree in priority order, calling f for each node
11220// with a non-empty write queue. When f returns true, this function returns true and the
11221// walk halts. tmp is used as scratch space for sorting.
11222//
11223// f(n, openParent) takes two arguments: the node to visit, n, and a bool that is true
11224// if any ancestor p of n is still open (ignoring the root node).
11225func (n *http2priorityNode) walkReadyInOrder(openParent bool, tmp *[]*http2priorityNode, f func(*http2priorityNode, bool) bool) bool {
11226	if !n.q.empty() && f(n, openParent) {
11227		return true
11228	}
11229	if n.kids == nil {
11230		return false
11231	}
11232
11233	// Don't consider the root "open" when updating openParent since
11234	// we can't send data frames on the root stream (only control frames).
11235	if n.id != 0 {
11236		openParent = openParent || (n.state == http2priorityNodeOpen)
11237	}
11238
11239	// Common case: only one kid or all kids have the same weight.
11240	// Some clients don't use weights; other clients (like web browsers)
11241	// use mostly-linear priority trees.
11242	w := n.kids.weight
11243	needSort := false
11244	for k := n.kids.next; k != nil; k = k.next {
11245		if k.weight != w {
11246			needSort = true
11247			break
11248		}
11249	}
11250	if !needSort {
11251		for k := n.kids; k != nil; k = k.next {
11252			if k.walkReadyInOrder(openParent, tmp, f) {
11253				return true
11254			}
11255		}
11256		return false
11257	}
11258
11259	// Uncommon case: sort the child nodes. We remove the kids from the parent,
11260	// then re-insert after sorting so we can reuse tmp for future sort calls.
11261	*tmp = (*tmp)[:0]
11262	for n.kids != nil {
11263		*tmp = append(*tmp, n.kids)
11264		n.kids.setParent(nil)
11265	}
11266	sort.Sort(http2sortPriorityNodeSiblings(*tmp))
11267	for i := len(*tmp) - 1; i >= 0; i-- {
11268		(*tmp)[i].setParent(n) // setParent inserts at the head of n.kids
11269	}
11270	for k := n.kids; k != nil; k = k.next {
11271		if k.walkReadyInOrder(openParent, tmp, f) {
11272			return true
11273		}
11274	}
11275	return false
11276}
11277
11278type http2sortPriorityNodeSiblings []*http2priorityNode
11279
11280func (z http2sortPriorityNodeSiblings) Len() int { return len(z) }
11281
11282func (z http2sortPriorityNodeSiblings) Swap(i, k int) { z[i], z[k] = z[k], z[i] }
11283
11284func (z http2sortPriorityNodeSiblings) Less(i, k int) bool {
11285	// Prefer the subtree that has sent fewer bytes relative to its weight.
11286	// See sections 5.3.2 and 5.3.4.
11287	wi, bi := float64(z[i].weight+1), float64(z[i].subtreeBytes)
11288	wk, bk := float64(z[k].weight+1), float64(z[k].subtreeBytes)
11289	if bi == 0 && bk == 0 {
11290		return wi >= wk
11291	}
11292	if bk == 0 {
11293		return false
11294	}
11295	return bi/bk <= wi/wk
11296}
11297
11298type http2priorityWriteScheduler struct {
11299	// root is the root of the priority tree, where root.id = 0.
11300	// The root queues control frames that are not associated with any stream.
11301	root http2priorityNode
11302
11303	// nodes maps stream ids to priority tree nodes.
11304	nodes map[uint32]*http2priorityNode
11305
11306	// maxID is the maximum stream id in nodes.
11307	maxID uint32
11308
11309	// lists of nodes that have been closed or are idle, but are kept in
11310	// the tree for improved prioritization. When the lengths exceed either
11311	// maxClosedNodesInTree or maxIdleNodesInTree, old nodes are discarded.
11312	closedNodes, idleNodes []*http2priorityNode
11313
11314	// From the config.
11315	maxClosedNodesInTree int
11316	maxIdleNodesInTree   int
11317	writeThrottleLimit   int32
11318	enableWriteThrottle  bool
11319
11320	// tmp is scratch space for priorityNode.walkReadyInOrder to reduce allocations.
11321	tmp []*http2priorityNode
11322
11323	// pool of empty queues for reuse.
11324	queuePool http2writeQueuePool
11325}
11326
11327func (ws *http2priorityWriteScheduler) OpenStream(streamID uint32, options http2OpenStreamOptions) {
11328	// The stream may be currently idle but cannot be opened or closed.
11329	if curr := ws.nodes[streamID]; curr != nil {
11330		if curr.state != http2priorityNodeIdle {
11331			panic(fmt.Sprintf("stream %d already opened", streamID))
11332		}
11333		curr.state = http2priorityNodeOpen
11334		return
11335	}
11336
11337	// RFC 7540, Section 5.3.5:
11338	//  "All streams are initially assigned a non-exclusive dependency on stream 0x0.
11339	//  Pushed streams initially depend on their associated stream. In both cases,
11340	//  streams are assigned a default weight of 16."
11341	parent := ws.nodes[options.PusherID]
11342	if parent == nil {
11343		parent = &ws.root
11344	}
11345	n := &http2priorityNode{
11346		q:      *ws.queuePool.get(),
11347		id:     streamID,
11348		weight: http2priorityDefaultWeight,
11349		state:  http2priorityNodeOpen,
11350	}
11351	n.setParent(parent)
11352	ws.nodes[streamID] = n
11353	if streamID > ws.maxID {
11354		ws.maxID = streamID
11355	}
11356}
11357
11358func (ws *http2priorityWriteScheduler) CloseStream(streamID uint32) {
11359	if streamID == 0 {
11360		panic("violation of WriteScheduler interface: cannot close stream 0")
11361	}
11362	if ws.nodes[streamID] == nil {
11363		panic(fmt.Sprintf("violation of WriteScheduler interface: unknown stream %d", streamID))
11364	}
11365	if ws.nodes[streamID].state != http2priorityNodeOpen {
11366		panic(fmt.Sprintf("violation of WriteScheduler interface: stream %d already closed", streamID))
11367	}
11368
11369	n := ws.nodes[streamID]
11370	n.state = http2priorityNodeClosed
11371	n.addBytes(-n.bytes)
11372
11373	q := n.q
11374	ws.queuePool.put(&q)
11375	n.q.s = nil
11376	if ws.maxClosedNodesInTree > 0 {
11377		ws.addClosedOrIdleNode(&ws.closedNodes, ws.maxClosedNodesInTree, n)
11378	} else {
11379		ws.removeNode(n)
11380	}
11381}
11382
11383func (ws *http2priorityWriteScheduler) AdjustStream(streamID uint32, priority http2PriorityParam) {
11384	if streamID == 0 {
11385		panic("adjustPriority on root")
11386	}
11387
11388	// If streamID does not exist, there are two cases:
11389	// - A closed stream that has been removed (this will have ID <= maxID)
11390	// - An idle stream that is being used for "grouping" (this will have ID > maxID)
11391	n := ws.nodes[streamID]
11392	if n == nil {
11393		if streamID <= ws.maxID || ws.maxIdleNodesInTree == 0 {
11394			return
11395		}
11396		ws.maxID = streamID
11397		n = &http2priorityNode{
11398			q:      *ws.queuePool.get(),
11399			id:     streamID,
11400			weight: http2priorityDefaultWeight,
11401			state:  http2priorityNodeIdle,
11402		}
11403		n.setParent(&ws.root)
11404		ws.nodes[streamID] = n
11405		ws.addClosedOrIdleNode(&ws.idleNodes, ws.maxIdleNodesInTree, n)
11406	}
11407
11408	// Section 5.3.1: A dependency on a stream that is not currently in the tree
11409	// results in that stream being given a default priority (Section 5.3.5).
11410	parent := ws.nodes[priority.StreamDep]
11411	if parent == nil {
11412		n.setParent(&ws.root)
11413		n.weight = http2priorityDefaultWeight
11414		return
11415	}
11416
11417	// Ignore if the client tries to make a node its own parent.
11418	if n == parent {
11419		return
11420	}
11421
11422	// Section 5.3.3:
11423	//   "If a stream is made dependent on one of its own dependencies, the
11424	//   formerly dependent stream is first moved to be dependent on the
11425	//   reprioritized stream's previous parent. The moved dependency retains
11426	//   its weight."
11427	//
11428	// That is: if parent depends on n, move parent to depend on n.parent.
11429	for x := parent.parent; x != nil; x = x.parent {
11430		if x == n {
11431			parent.setParent(n.parent)
11432			break
11433		}
11434	}
11435
11436	// Section 5.3.3: The exclusive flag causes the stream to become the sole
11437	// dependency of its parent stream, causing other dependencies to become
11438	// dependent on the exclusive stream.
11439	if priority.Exclusive {
11440		k := parent.kids
11441		for k != nil {
11442			next := k.next
11443			if k != n {
11444				k.setParent(n)
11445			}
11446			k = next
11447		}
11448	}
11449
11450	n.setParent(parent)
11451	n.weight = priority.Weight
11452}
11453
11454func (ws *http2priorityWriteScheduler) Push(wr http2FrameWriteRequest) {
11455	var n *http2priorityNode
11456	if wr.isControl() {
11457		n = &ws.root
11458	} else {
11459		id := wr.StreamID()
11460		n = ws.nodes[id]
11461		if n == nil {
11462			// id is an idle or closed stream. wr should not be a HEADERS or
11463			// DATA frame. In other case, we push wr onto the root, rather
11464			// than creating a new priorityNode.
11465			if wr.DataSize() > 0 {
11466				panic("add DATA on non-open stream")
11467			}
11468			n = &ws.root
11469		}
11470	}
11471	n.q.push(wr)
11472}
11473
11474func (ws *http2priorityWriteScheduler) Pop() (wr http2FrameWriteRequest, ok bool) {
11475	ws.root.walkReadyInOrder(false, &ws.tmp, func(n *http2priorityNode, openParent bool) bool {
11476		limit := int32(math.MaxInt32)
11477		if openParent {
11478			limit = ws.writeThrottleLimit
11479		}
11480		wr, ok = n.q.consume(limit)
11481		if !ok {
11482			return false
11483		}
11484		n.addBytes(int64(wr.DataSize()))
11485		// If B depends on A and B continuously has data available but A
11486		// does not, gradually increase the throttling limit to allow B to
11487		// steal more and more bandwidth from A.
11488		if openParent {
11489			ws.writeThrottleLimit += 1024
11490			if ws.writeThrottleLimit < 0 {
11491				ws.writeThrottleLimit = math.MaxInt32
11492			}
11493		} else if ws.enableWriteThrottle {
11494			ws.writeThrottleLimit = 1024
11495		}
11496		return true
11497	})
11498	return wr, ok
11499}
11500
11501func (ws *http2priorityWriteScheduler) addClosedOrIdleNode(list *[]*http2priorityNode, maxSize int, n *http2priorityNode) {
11502	if maxSize == 0 {
11503		return
11504	}
11505	if len(*list) == maxSize {
11506		// Remove the oldest node, then shift left.
11507		ws.removeNode((*list)[0])
11508		x := (*list)[1:]
11509		copy(*list, x)
11510		*list = (*list)[:len(x)]
11511	}
11512	*list = append(*list, n)
11513}
11514
11515func (ws *http2priorityWriteScheduler) removeNode(n *http2priorityNode) {
11516	for n.kids != nil {
11517		n.kids.setParent(n.parent)
11518	}
11519	n.setParent(nil)
11520	delete(ws.nodes, n.id)
11521}
11522
11523// NewRandomWriteScheduler constructs a WriteScheduler that ignores HTTP/2
11524// priorities. Control frames like SETTINGS and PING are written before DATA
11525// frames, but if no control frames are queued and multiple streams have queued
11526// HEADERS or DATA frames, Pop selects a ready stream arbitrarily.
11527func http2NewRandomWriteScheduler() http2WriteScheduler {
11528	return &http2randomWriteScheduler{sq: make(map[uint32]*http2writeQueue)}
11529}
11530
11531type http2randomWriteScheduler struct {
11532	// zero are frames not associated with a specific stream.
11533	zero http2writeQueue
11534
11535	// sq contains the stream-specific queues, keyed by stream ID.
11536	// When a stream is idle, closed, or emptied, it's deleted
11537	// from the map.
11538	sq map[uint32]*http2writeQueue
11539
11540	// pool of empty queues for reuse.
11541	queuePool http2writeQueuePool
11542}
11543
11544func (ws *http2randomWriteScheduler) OpenStream(streamID uint32, options http2OpenStreamOptions) {
11545	// no-op: idle streams are not tracked
11546}
11547
11548func (ws *http2randomWriteScheduler) CloseStream(streamID uint32) {
11549	q, ok := ws.sq[streamID]
11550	if !ok {
11551		return
11552	}
11553	delete(ws.sq, streamID)
11554	ws.queuePool.put(q)
11555}
11556
11557func (ws *http2randomWriteScheduler) AdjustStream(streamID uint32, priority http2PriorityParam) {
11558	// no-op: priorities are ignored
11559}
11560
11561func (ws *http2randomWriteScheduler) Push(wr http2FrameWriteRequest) {
11562	if wr.isControl() {
11563		ws.zero.push(wr)
11564		return
11565	}
11566	id := wr.StreamID()
11567	q, ok := ws.sq[id]
11568	if !ok {
11569		q = ws.queuePool.get()
11570		ws.sq[id] = q
11571	}
11572	q.push(wr)
11573}
11574
11575func (ws *http2randomWriteScheduler) Pop() (http2FrameWriteRequest, bool) {
11576	// Control and RST_STREAM frames first.
11577	if !ws.zero.empty() {
11578		return ws.zero.shift(), true
11579	}
11580	// Iterate over all non-idle streams until finding one that can be consumed.
11581	for streamID, q := range ws.sq {
11582		if wr, ok := q.consume(math.MaxInt32); ok {
11583			if q.empty() {
11584				delete(ws.sq, streamID)
11585				ws.queuePool.put(q)
11586			}
11587			return wr, true
11588		}
11589	}
11590	return http2FrameWriteRequest{}, false
11591}
11592
11593type http2roundRobinWriteScheduler struct {
11594	// control contains control frames (SETTINGS, PING, etc.).
11595	control http2writeQueue
11596
11597	// streams maps stream ID to a queue.
11598	streams map[uint32]*http2writeQueue
11599
11600	// stream queues are stored in a circular linked list.
11601	// head is the next stream to write, or nil if there are no streams open.
11602	head *http2writeQueue
11603
11604	// pool of empty queues for reuse.
11605	queuePool http2writeQueuePool
11606}
11607
11608// newRoundRobinWriteScheduler constructs a new write scheduler.
11609// The round robin scheduler priorizes control frames
11610// like SETTINGS and PING over DATA frames.
11611// When there are no control frames to send, it performs a round-robin
11612// selection from the ready streams.
11613func http2newRoundRobinWriteScheduler() http2WriteScheduler {
11614	ws := &http2roundRobinWriteScheduler{
11615		streams: make(map[uint32]*http2writeQueue),
11616	}
11617	return ws
11618}
11619
11620func (ws *http2roundRobinWriteScheduler) OpenStream(streamID uint32, options http2OpenStreamOptions) {
11621	if ws.streams[streamID] != nil {
11622		panic(fmt.Errorf("stream %d already opened", streamID))
11623	}
11624	q := ws.queuePool.get()
11625	ws.streams[streamID] = q
11626	if ws.head == nil {
11627		ws.head = q
11628		q.next = q
11629		q.prev = q
11630	} else {
11631		// Queues are stored in a ring.
11632		// Insert the new stream before ws.head, putting it at the end of the list.
11633		q.prev = ws.head.prev
11634		q.next = ws.head
11635		q.prev.next = q
11636		q.next.prev = q
11637	}
11638}
11639
11640func (ws *http2roundRobinWriteScheduler) CloseStream(streamID uint32) {
11641	q := ws.streams[streamID]
11642	if q == nil {
11643		return
11644	}
11645	if q.next == q {
11646		// This was the only open stream.
11647		ws.head = nil
11648	} else {
11649		q.prev.next = q.next
11650		q.next.prev = q.prev
11651		if ws.head == q {
11652			ws.head = q.next
11653		}
11654	}
11655	delete(ws.streams, streamID)
11656	ws.queuePool.put(q)
11657}
11658
11659func (ws *http2roundRobinWriteScheduler) AdjustStream(streamID uint32, priority http2PriorityParam) {}
11660
11661func (ws *http2roundRobinWriteScheduler) Push(wr http2FrameWriteRequest) {
11662	if wr.isControl() {
11663		ws.control.push(wr)
11664		return
11665	}
11666	q := ws.streams[wr.StreamID()]
11667	if q == nil {
11668		// This is a closed stream.
11669		// wr should not be a HEADERS or DATA frame.
11670		// We push the request onto the control queue.
11671		if wr.DataSize() > 0 {
11672			panic("add DATA on non-open stream")
11673		}
11674		ws.control.push(wr)
11675		return
11676	}
11677	q.push(wr)
11678}
11679
11680func (ws *http2roundRobinWriteScheduler) Pop() (http2FrameWriteRequest, bool) {
11681	// Control and RST_STREAM frames first.
11682	if !ws.control.empty() {
11683		return ws.control.shift(), true
11684	}
11685	if ws.head == nil {
11686		return http2FrameWriteRequest{}, false
11687	}
11688	q := ws.head
11689	for {
11690		if wr, ok := q.consume(math.MaxInt32); ok {
11691			ws.head = q.next
11692			return wr, true
11693		}
11694		q = q.next
11695		if q == ws.head {
11696			break
11697		}
11698	}
11699	return http2FrameWriteRequest{}, false
11700}
11701