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