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