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