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