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