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