github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/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 if sc.inGoAway && sc.curOpenStreams() == 0 && !sc.needToSendGoAway && !sc.writingFrame { 4550 return 4551 } 4552 } 4553 } 4554 4555 func (sc *http2serverConn) awaitGracefulShutdown(sharedCh <-chan struct{}, privateCh chan struct{}) { 4556 select { 4557 case <-sc.doneServing: 4558 case <-sharedCh: 4559 close(privateCh) 4560 } 4561 } 4562 4563 type http2serverMessage int 4564 4565 // Message values sent to serveMsgCh. 4566 var ( 4567 http2settingsTimerMsg = new(http2serverMessage) 4568 http2idleTimerMsg = new(http2serverMessage) 4569 http2shutdownTimerMsg = new(http2serverMessage) 4570 http2gracefulShutdownMsg = new(http2serverMessage) 4571 ) 4572 4573 func (sc *http2serverConn) onSettingsTimer() { sc.sendServeMsg(http2settingsTimerMsg) } 4574 4575 func (sc *http2serverConn) onIdleTimer() { sc.sendServeMsg(http2idleTimerMsg) } 4576 4577 func (sc *http2serverConn) onShutdownTimer() { sc.sendServeMsg(http2shutdownTimerMsg) } 4578 4579 func (sc *http2serverConn) sendServeMsg(msg interface{}) { 4580 sc.serveG.checkNotOn() // NOT 4581 select { 4582 case sc.serveMsgCh <- msg: 4583 case <-sc.doneServing: 4584 } 4585 } 4586 4587 // readPreface reads the ClientPreface greeting from the peer 4588 // or returns an error on timeout or an invalid greeting. 4589 func (sc *http2serverConn) readPreface() error { 4590 errc := make(chan error, 1) 4591 go func() { 4592 // Read the client preface 4593 buf := make([]byte, len(http2ClientPreface)) 4594 if _, err := io.ReadFull(sc.conn, buf); err != nil { 4595 errc <- err 4596 } else if !bytes.Equal(buf, http2clientPreface) { 4597 errc <- fmt.Errorf("bogus greeting %q", buf) 4598 } else { 4599 errc <- nil 4600 } 4601 }() 4602 timer := time.NewTimer(http2prefaceTimeout) // TODO: configurable on *Server? 4603 defer timer.Stop() 4604 select { 4605 case <-timer.C: 4606 return errors.New("timeout waiting for client preface") 4607 case err := <-errc: 4608 if err == nil { 4609 if http2VerboseLogs { 4610 sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr()) 4611 } 4612 } 4613 return err 4614 } 4615 } 4616 4617 var http2errChanPool = sync.Pool{ 4618 New: func() interface{} { return make(chan error, 1) }, 4619 } 4620 4621 var http2writeDataPool = sync.Pool{ 4622 New: func() interface{} { return new(http2writeData) }, 4623 } 4624 4625 // writeDataFromHandler writes DATA response frames from a handler on 4626 // the given stream. 4627 func (sc *http2serverConn) writeDataFromHandler(stream *http2stream, data []byte, endStream bool) error { 4628 ch := http2errChanPool.Get().(chan error) 4629 writeArg := http2writeDataPool.Get().(*http2writeData) 4630 *writeArg = http2writeData{stream.id, data, endStream} 4631 err := sc.writeFrameFromHandler(http2FrameWriteRequest{ 4632 write: writeArg, 4633 stream: stream, 4634 done: ch, 4635 }) 4636 if err != nil { 4637 return err 4638 } 4639 var frameWriteDone bool // the frame write is done (successfully or not) 4640 select { 4641 case err = <-ch: 4642 frameWriteDone = true 4643 case <-sc.doneServing: 4644 return http2errClientDisconnected 4645 case <-stream.cw: 4646 // If both ch and stream.cw were ready (as might 4647 // happen on the final Write after an http.Handler 4648 // ends), prefer the write result. Otherwise this 4649 // might just be us successfully closing the stream. 4650 // The writeFrameAsync and serve goroutines guarantee 4651 // that the ch send will happen before the stream.cw 4652 // close. 4653 select { 4654 case err = <-ch: 4655 frameWriteDone = true 4656 default: 4657 return http2errStreamClosed 4658 } 4659 } 4660 http2errChanPool.Put(ch) 4661 if frameWriteDone { 4662 http2writeDataPool.Put(writeArg) 4663 } 4664 return err 4665 } 4666 4667 // writeFrameFromHandler sends wr to sc.wantWriteFrameCh, but aborts 4668 // if the connection has gone away. 4669 // 4670 // This must not be run from the serve goroutine itself, else it might 4671 // deadlock writing to sc.wantWriteFrameCh (which is only mildly 4672 // buffered and is read by serve itself). If you're on the serve 4673 // goroutine, call writeFrame instead. 4674 func (sc *http2serverConn) writeFrameFromHandler(wr http2FrameWriteRequest) error { 4675 sc.serveG.checkNotOn() // NOT 4676 select { 4677 case sc.wantWriteFrameCh <- wr: 4678 return nil 4679 case <-sc.doneServing: 4680 // Serve loop is gone. 4681 // Client has closed their connection to the server. 4682 return http2errClientDisconnected 4683 } 4684 } 4685 4686 // writeFrame schedules a frame to write and sends it if there's nothing 4687 // already being written. 4688 // 4689 // There is no pushback here (the serve goroutine never blocks). It's 4690 // the http.Handlers that block, waiting for their previous frames to 4691 // make it onto the wire 4692 // 4693 // If you're not on the serve goroutine, use writeFrameFromHandler instead. 4694 func (sc *http2serverConn) writeFrame(wr http2FrameWriteRequest) { 4695 sc.serveG.check() 4696 4697 // If true, wr will not be written and wr.done will not be signaled. 4698 var ignoreWrite bool 4699 4700 // We are not allowed to write frames on closed streams. RFC 7540 Section 4701 // 5.1.1 says: "An endpoint MUST NOT send frames other than PRIORITY on 4702 // a closed stream." Our server never sends PRIORITY, so that exception 4703 // does not apply. 4704 // 4705 // The serverConn might close an open stream while the stream's handler 4706 // is still running. For example, the server might close a stream when it 4707 // receives bad data from the client. If this happens, the handler might 4708 // attempt to write a frame after the stream has been closed (since the 4709 // handler hasn't yet been notified of the close). In this case, we simply 4710 // ignore the frame. The handler will notice that the stream is closed when 4711 // it waits for the frame to be written. 4712 // 4713 // As an exception to this rule, we allow sending RST_STREAM after close. 4714 // This allows us to immediately reject new streams without tracking any 4715 // state for those streams (except for the queued RST_STREAM frame). This 4716 // may result in duplicate RST_STREAMs in some cases, but the client should 4717 // ignore those. 4718 if wr.StreamID() != 0 { 4719 _, isReset := wr.write.(http2StreamError) 4720 if state, _ := sc.state(wr.StreamID()); state == http2stateClosed && !isReset { 4721 ignoreWrite = true 4722 } 4723 } 4724 4725 // Don't send a 100-continue response if we've already sent headers. 4726 // See golang.org/issue/14030. 4727 switch wr.write.(type) { 4728 case *http2writeResHeaders: 4729 wr.stream.wroteHeaders = true 4730 case http2write100ContinueHeadersFrame: 4731 if wr.stream.wroteHeaders { 4732 // We do not need to notify wr.done because this frame is 4733 // never written with wr.done != nil. 4734 if wr.done != nil { 4735 panic("wr.done != nil for write100ContinueHeadersFrame") 4736 } 4737 ignoreWrite = true 4738 } 4739 } 4740 4741 if !ignoreWrite { 4742 sc.writeSched.Push(wr) 4743 } 4744 sc.scheduleFrameWrite() 4745 } 4746 4747 // startFrameWrite starts a goroutine to write wr (in a separate 4748 // goroutine since that might block on the network), and updates the 4749 // serve goroutine's state about the world, updated from info in wr. 4750 func (sc *http2serverConn) startFrameWrite(wr http2FrameWriteRequest) { 4751 sc.serveG.check() 4752 if sc.writingFrame { 4753 panic("internal error: can only be writing one frame at a time") 4754 } 4755 4756 st := wr.stream 4757 if st != nil { 4758 switch st.state { 4759 case http2stateHalfClosedLocal: 4760 switch wr.write.(type) { 4761 case http2StreamError, http2handlerPanicRST, http2writeWindowUpdate: 4762 // RFC 7540 Section 5.1 allows sending RST_STREAM, PRIORITY, and WINDOW_UPDATE 4763 // in this state. (We never send PRIORITY from the server, so that is not checked.) 4764 default: 4765 panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr)) 4766 } 4767 case http2stateClosed: 4768 panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr)) 4769 } 4770 } 4771 if wpp, ok := wr.write.(*http2writePushPromise); ok { 4772 var err error 4773 wpp.promisedID, err = wpp.allocatePromisedID() 4774 if err != nil { 4775 sc.writingFrameAsync = false 4776 wr.replyToWriter(err) 4777 return 4778 } 4779 } 4780 4781 sc.writingFrame = true 4782 sc.needsFrameFlush = true 4783 if wr.write.staysWithinBuffer(sc.bw.Available()) { 4784 sc.writingFrameAsync = false 4785 err := wr.write.writeFrame(sc) 4786 sc.wroteFrame(http2frameWriteResult{wr, err}) 4787 } else { 4788 sc.writingFrameAsync = true 4789 go sc.writeFrameAsync(wr) 4790 } 4791 } 4792 4793 // errHandlerPanicked is the error given to any callers blocked in a read from 4794 // Request.Body when the main goroutine panics. Since most handlers read in the 4795 // the main ServeHTTP goroutine, this will show up rarely. 4796 var http2errHandlerPanicked = errors.New("http2: handler panicked") 4797 4798 // wroteFrame is called on the serve goroutine with the result of 4799 // whatever happened on writeFrameAsync. 4800 func (sc *http2serverConn) wroteFrame(res http2frameWriteResult) { 4801 sc.serveG.check() 4802 if !sc.writingFrame { 4803 panic("internal error: expected to be already writing a frame") 4804 } 4805 sc.writingFrame = false 4806 sc.writingFrameAsync = false 4807 4808 wr := res.wr 4809 4810 if http2writeEndsStream(wr.write) { 4811 st := wr.stream 4812 if st == nil { 4813 panic("internal error: expecting non-nil stream") 4814 } 4815 switch st.state { 4816 case http2stateOpen: 4817 // Here we would go to stateHalfClosedLocal in 4818 // theory, but since our handler is done and 4819 // the net/http package provides no mechanism 4820 // for closing a ResponseWriter while still 4821 // reading data (see possible TODO at top of 4822 // this file), we go into closed state here 4823 // anyway, after telling the peer we're 4824 // hanging up on them. We'll transition to 4825 // stateClosed after the RST_STREAM frame is 4826 // written. 4827 st.state = http2stateHalfClosedLocal 4828 // Section 8.1: a server MAY request that the client abort 4829 // transmission of a request without error by sending a 4830 // RST_STREAM with an error code of NO_ERROR after sending 4831 // a complete response. 4832 sc.resetStream(http2streamError(st.id, http2ErrCodeNo)) 4833 case http2stateHalfClosedRemote: 4834 sc.closeStream(st, http2errHandlerComplete) 4835 } 4836 } else { 4837 switch v := wr.write.(type) { 4838 case http2StreamError: 4839 // st may be unknown if the RST_STREAM was generated to reject bad input. 4840 if st, ok := sc.streams[v.StreamID]; ok { 4841 sc.closeStream(st, v) 4842 } 4843 case http2handlerPanicRST: 4844 sc.closeStream(wr.stream, http2errHandlerPanicked) 4845 } 4846 } 4847 4848 // Reply (if requested) to unblock the ServeHTTP goroutine. 4849 wr.replyToWriter(res.err) 4850 4851 sc.scheduleFrameWrite() 4852 } 4853 4854 // scheduleFrameWrite tickles the frame writing scheduler. 4855 // 4856 // If a frame is already being written, nothing happens. This will be called again 4857 // when the frame is done being written. 4858 // 4859 // If a frame isn't being written we need to send one, the best frame 4860 // to send is selected, preferring first things that aren't 4861 // stream-specific (e.g. ACKing settings), and then finding the 4862 // highest priority stream. 4863 // 4864 // If a frame isn't being written and there's nothing else to send, we 4865 // flush the write buffer. 4866 func (sc *http2serverConn) scheduleFrameWrite() { 4867 sc.serveG.check() 4868 if sc.writingFrame || sc.inFrameScheduleLoop { 4869 return 4870 } 4871 sc.inFrameScheduleLoop = true 4872 for !sc.writingFrameAsync { 4873 if sc.needToSendGoAway { 4874 sc.needToSendGoAway = false 4875 sc.startFrameWrite(http2FrameWriteRequest{ 4876 write: &http2writeGoAway{ 4877 maxStreamID: sc.maxClientStreamID, 4878 code: sc.goAwayCode, 4879 }, 4880 }) 4881 continue 4882 } 4883 if sc.needToSendSettingsAck { 4884 sc.needToSendSettingsAck = false 4885 sc.startFrameWrite(http2FrameWriteRequest{write: http2writeSettingsAck{}}) 4886 continue 4887 } 4888 if !sc.inGoAway || sc.goAwayCode == http2ErrCodeNo { 4889 if wr, ok := sc.writeSched.Pop(); ok { 4890 sc.startFrameWrite(wr) 4891 continue 4892 } 4893 } 4894 if sc.needsFrameFlush { 4895 sc.startFrameWrite(http2FrameWriteRequest{write: http2flushFrameWriter{}}) 4896 sc.needsFrameFlush = false // after startFrameWrite, since it sets this true 4897 continue 4898 } 4899 break 4900 } 4901 sc.inFrameScheduleLoop = false 4902 } 4903 4904 // startGracefulShutdown gracefully shuts down a connection. This 4905 // sends GOAWAY with ErrCodeNo to tell the client we're gracefully 4906 // shutting down. The connection isn't closed until all current 4907 // streams are done. 4908 // 4909 // startGracefulShutdown returns immediately; it does not wait until 4910 // the connection has shut down. 4911 func (sc *http2serverConn) startGracefulShutdown() { 4912 sc.serveG.checkNotOn() // NOT 4913 sc.shutdownOnce.Do(func() { sc.sendServeMsg(http2gracefulShutdownMsg) }) 4914 } 4915 4916 func (sc *http2serverConn) startGracefulShutdownInternal() { 4917 sc.goAwayIn(http2ErrCodeNo, 0) 4918 } 4919 4920 func (sc *http2serverConn) goAway(code http2ErrCode) { 4921 sc.serveG.check() 4922 var forceCloseIn time.Duration 4923 if code != http2ErrCodeNo { 4924 forceCloseIn = 250 * time.Millisecond 4925 } else { 4926 // TODO: configurable 4927 forceCloseIn = 1 * time.Second 4928 } 4929 sc.goAwayIn(code, forceCloseIn) 4930 } 4931 4932 func (sc *http2serverConn) goAwayIn(code http2ErrCode, forceCloseIn time.Duration) { 4933 sc.serveG.check() 4934 if sc.inGoAway { 4935 return 4936 } 4937 if forceCloseIn != 0 { 4938 sc.shutDownIn(forceCloseIn) 4939 } 4940 sc.inGoAway = true 4941 sc.needToSendGoAway = true 4942 sc.goAwayCode = code 4943 sc.scheduleFrameWrite() 4944 } 4945 4946 func (sc *http2serverConn) shutDownIn(d time.Duration) { 4947 sc.serveG.check() 4948 sc.shutdownTimer = time.AfterFunc(d, sc.onShutdownTimer) 4949 } 4950 4951 func (sc *http2serverConn) resetStream(se http2StreamError) { 4952 sc.serveG.check() 4953 sc.writeFrame(http2FrameWriteRequest{write: se}) 4954 if st, ok := sc.streams[se.StreamID]; ok { 4955 st.resetQueued = true 4956 } 4957 } 4958 4959 // processFrameFromReader processes the serve loop's read from readFrameCh from the 4960 // frame-reading goroutine. 4961 // processFrameFromReader returns whether the connection should be kept open. 4962 func (sc *http2serverConn) processFrameFromReader(res http2readFrameResult) bool { 4963 sc.serveG.check() 4964 err := res.err 4965 if err != nil { 4966 if err == http2ErrFrameTooLarge { 4967 sc.goAway(http2ErrCodeFrameSize) 4968 return true // goAway will close the loop 4969 } 4970 clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) 4971 if clientGone { 4972 // TODO: could we also get into this state if 4973 // the peer does a half close 4974 // (e.g. CloseWrite) because they're done 4975 // sending frames but they're still wanting 4976 // our open replies? Investigate. 4977 // TODO: add CloseWrite to crypto/tls.Conn first 4978 // so we have a way to test this? I suppose 4979 // just for testing we could have a non-TLS mode. 4980 return false 4981 } 4982 } else { 4983 f := res.f 4984 if http2VerboseLogs { 4985 sc.vlogf("http2: server read frame %v", http2summarizeFrame(f)) 4986 } 4987 err = sc.processFrame(f) 4988 if err == nil { 4989 return true 4990 } 4991 } 4992 4993 switch ev := err.(type) { 4994 case http2StreamError: 4995 sc.resetStream(ev) 4996 return true 4997 case http2goAwayFlowError: 4998 sc.goAway(http2ErrCodeFlowControl) 4999 return true 5000 case http2ConnectionError: 5001 sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev) 5002 sc.goAway(http2ErrCode(ev)) 5003 return true // goAway will handle shutdown 5004 default: 5005 if res.err != nil { 5006 sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err) 5007 } else { 5008 sc.logf("http2: server closing client connection: %v", err) 5009 } 5010 return false 5011 } 5012 } 5013 5014 func (sc *http2serverConn) processFrame(f http2Frame) error { 5015 sc.serveG.check() 5016 5017 // First frame received must be SETTINGS. 5018 if !sc.sawFirstSettings { 5019 if _, ok := f.(*http2SettingsFrame); !ok { 5020 return http2ConnectionError(http2ErrCodeProtocol) 5021 } 5022 sc.sawFirstSettings = true 5023 } 5024 5025 switch f := f.(type) { 5026 case *http2SettingsFrame: 5027 return sc.processSettings(f) 5028 case *http2MetaHeadersFrame: 5029 return sc.processHeaders(f) 5030 case *http2WindowUpdateFrame: 5031 return sc.processWindowUpdate(f) 5032 case *http2PingFrame: 5033 return sc.processPing(f) 5034 case *http2DataFrame: 5035 return sc.processData(f) 5036 case *http2RSTStreamFrame: 5037 return sc.processResetStream(f) 5038 case *http2PriorityFrame: 5039 return sc.processPriority(f) 5040 case *http2GoAwayFrame: 5041 return sc.processGoAway(f) 5042 case *http2PushPromiseFrame: 5043 // A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE 5044 // frame as a connection error (Section 5.4.1) of type PROTOCOL_ERROR. 5045 return http2ConnectionError(http2ErrCodeProtocol) 5046 default: 5047 sc.vlogf("http2: server ignoring frame: %v", f.Header()) 5048 return nil 5049 } 5050 } 5051 5052 func (sc *http2serverConn) processPing(f *http2PingFrame) error { 5053 sc.serveG.check() 5054 if f.IsAck() { 5055 // 6.7 PING: " An endpoint MUST NOT respond to PING frames 5056 // containing this flag." 5057 return nil 5058 } 5059 if f.StreamID != 0 { 5060 // "PING frames are not associated with any individual 5061 // stream. If a PING frame is received with a stream 5062 // identifier field value other than 0x0, the recipient MUST 5063 // respond with a connection error (Section 5.4.1) of type 5064 // PROTOCOL_ERROR." 5065 return http2ConnectionError(http2ErrCodeProtocol) 5066 } 5067 if sc.inGoAway && sc.goAwayCode != http2ErrCodeNo { 5068 return nil 5069 } 5070 sc.writeFrame(http2FrameWriteRequest{write: http2writePingAck{f}}) 5071 return nil 5072 } 5073 5074 func (sc *http2serverConn) processWindowUpdate(f *http2WindowUpdateFrame) error { 5075 sc.serveG.check() 5076 switch { 5077 case f.StreamID != 0: // stream-level flow control 5078 state, st := sc.state(f.StreamID) 5079 if state == http2stateIdle { 5080 // Section 5.1: "Receiving any frame other than HEADERS 5081 // or PRIORITY on a stream in this state MUST be 5082 // treated as a connection error (Section 5.4.1) of 5083 // type PROTOCOL_ERROR." 5084 return http2ConnectionError(http2ErrCodeProtocol) 5085 } 5086 if st == nil { 5087 // "WINDOW_UPDATE can be sent by a peer that has sent a 5088 // frame bearing the END_STREAM flag. This means that a 5089 // receiver could receive a WINDOW_UPDATE frame on a "half 5090 // closed (remote)" or "closed" stream. A receiver MUST 5091 // NOT treat this as an error, see Section 5.1." 5092 return nil 5093 } 5094 if !st.flow.add(int32(f.Increment)) { 5095 return http2streamError(f.StreamID, http2ErrCodeFlowControl) 5096 } 5097 default: // connection-level flow control 5098 if !sc.flow.add(int32(f.Increment)) { 5099 return http2goAwayFlowError{} 5100 } 5101 } 5102 sc.scheduleFrameWrite() 5103 return nil 5104 } 5105 5106 func (sc *http2serverConn) processResetStream(f *http2RSTStreamFrame) error { 5107 sc.serveG.check() 5108 5109 state, st := sc.state(f.StreamID) 5110 if state == http2stateIdle { 5111 // 6.4 "RST_STREAM frames MUST NOT be sent for a 5112 // stream in the "idle" state. If a RST_STREAM frame 5113 // identifying an idle stream is received, the 5114 // recipient MUST treat this as a connection error 5115 // (Section 5.4.1) of type PROTOCOL_ERROR. 5116 return http2ConnectionError(http2ErrCodeProtocol) 5117 } 5118 if st != nil { 5119 st.cancelCtx() 5120 sc.closeStream(st, http2streamError(f.StreamID, f.ErrCode)) 5121 } 5122 return nil 5123 } 5124 5125 func (sc *http2serverConn) closeStream(st *http2stream, err error) { 5126 sc.serveG.check() 5127 if st.state == http2stateIdle || st.state == http2stateClosed { 5128 panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state)) 5129 } 5130 st.state = http2stateClosed 5131 if st.writeDeadline != nil { 5132 st.writeDeadline.Stop() 5133 } 5134 if st.isPushed() { 5135 sc.curPushedStreams-- 5136 } else { 5137 sc.curClientStreams-- 5138 } 5139 delete(sc.streams, st.id) 5140 if len(sc.streams) == 0 { 5141 sc.setConnState(StateIdle) 5142 if sc.srv.IdleTimeout != 0 { 5143 sc.idleTimer.Reset(sc.srv.IdleTimeout) 5144 } 5145 if http2h1ServerKeepAlivesDisabled(sc.hs) { 5146 sc.startGracefulShutdownInternal() 5147 } 5148 } 5149 if p := st.body; p != nil { 5150 // Return any buffered unread bytes worth of conn-level flow control. 5151 // See golang.org/issue/16481 5152 sc.sendWindowUpdate(nil, p.Len()) 5153 5154 p.CloseWithError(err) 5155 } 5156 st.cw.Close() // signals Handler's CloseNotifier, unblocks writes, etc 5157 sc.writeSched.CloseStream(st.id) 5158 } 5159 5160 func (sc *http2serverConn) processSettings(f *http2SettingsFrame) error { 5161 sc.serveG.check() 5162 if f.IsAck() { 5163 sc.unackedSettings-- 5164 if sc.unackedSettings < 0 { 5165 // Why is the peer ACKing settings we never sent? 5166 // The spec doesn't mention this case, but 5167 // hang up on them anyway. 5168 return http2ConnectionError(http2ErrCodeProtocol) 5169 } 5170 return nil 5171 } 5172 if err := f.ForeachSetting(sc.processSetting); err != nil { 5173 return err 5174 } 5175 sc.needToSendSettingsAck = true 5176 sc.scheduleFrameWrite() 5177 return nil 5178 } 5179 5180 func (sc *http2serverConn) processSetting(s http2Setting) error { 5181 sc.serveG.check() 5182 if err := s.Valid(); err != nil { 5183 return err 5184 } 5185 if http2VerboseLogs { 5186 sc.vlogf("http2: server processing setting %v", s) 5187 } 5188 switch s.ID { 5189 case http2SettingHeaderTableSize: 5190 sc.headerTableSize = s.Val 5191 sc.hpackEncoder.SetMaxDynamicTableSize(s.Val) 5192 case http2SettingEnablePush: 5193 sc.pushEnabled = s.Val != 0 5194 case http2SettingMaxConcurrentStreams: 5195 sc.clientMaxStreams = s.Val 5196 case http2SettingInitialWindowSize: 5197 return sc.processSettingInitialWindowSize(s.Val) 5198 case http2SettingMaxFrameSize: 5199 sc.maxFrameSize = int32(s.Val) // the maximum valid s.Val is < 2^31 5200 case http2SettingMaxHeaderListSize: 5201 sc.peerMaxHeaderListSize = s.Val 5202 default: 5203 // Unknown setting: "An endpoint that receives a SETTINGS 5204 // frame with any unknown or unsupported identifier MUST 5205 // ignore that setting." 5206 if http2VerboseLogs { 5207 sc.vlogf("http2: server ignoring unknown setting %v", s) 5208 } 5209 } 5210 return nil 5211 } 5212 5213 func (sc *http2serverConn) processSettingInitialWindowSize(val uint32) error { 5214 sc.serveG.check() 5215 // Note: val already validated to be within range by 5216 // processSetting's Valid call. 5217 5218 // "A SETTINGS frame can alter the initial flow control window 5219 // size for all current streams. When the value of 5220 // SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST 5221 // adjust the size of all stream flow control windows that it 5222 // maintains by the difference between the new value and the 5223 // old value." 5224 old := sc.initialStreamSendWindowSize 5225 sc.initialStreamSendWindowSize = int32(val) 5226 growth := int32(val) - old // may be negative 5227 for _, st := range sc.streams { 5228 if !st.flow.add(growth) { 5229 // 6.9.2 Initial Flow Control Window Size 5230 // "An endpoint MUST treat a change to 5231 // SETTINGS_INITIAL_WINDOW_SIZE that causes any flow 5232 // control window to exceed the maximum size as a 5233 // connection error (Section 5.4.1) of type 5234 // FLOW_CONTROL_ERROR." 5235 return http2ConnectionError(http2ErrCodeFlowControl) 5236 } 5237 } 5238 return nil 5239 } 5240 5241 func (sc *http2serverConn) processData(f *http2DataFrame) error { 5242 sc.serveG.check() 5243 if sc.inGoAway && sc.goAwayCode != http2ErrCodeNo { 5244 return nil 5245 } 5246 data := f.Data() 5247 5248 // "If a DATA frame is received whose stream is not in "open" 5249 // or "half closed (local)" state, the recipient MUST respond 5250 // with a stream error (Section 5.4.2) of type STREAM_CLOSED." 5251 id := f.Header().StreamID 5252 state, st := sc.state(id) 5253 if id == 0 || state == http2stateIdle { 5254 // Section 5.1: "Receiving any frame other than HEADERS 5255 // or PRIORITY on a stream in this state MUST be 5256 // treated as a connection error (Section 5.4.1) of 5257 // type PROTOCOL_ERROR." 5258 return http2ConnectionError(http2ErrCodeProtocol) 5259 } 5260 if st == nil || state != http2stateOpen || st.gotTrailerHeader || st.resetQueued { 5261 // This includes sending a RST_STREAM if the stream is 5262 // in stateHalfClosedLocal (which currently means that 5263 // the http.Handler returned, so it's done reading & 5264 // done writing). Try to stop the client from sending 5265 // more DATA. 5266 5267 // But still enforce their connection-level flow control, 5268 // and return any flow control bytes since we're not going 5269 // to consume them. 5270 if sc.inflow.available() < int32(f.Length) { 5271 return http2streamError(id, http2ErrCodeFlowControl) 5272 } 5273 // Deduct the flow control from inflow, since we're 5274 // going to immediately add it back in 5275 // sendWindowUpdate, which also schedules sending the 5276 // frames. 5277 sc.inflow.take(int32(f.Length)) 5278 sc.sendWindowUpdate(nil, int(f.Length)) // conn-level 5279 5280 if st != nil && st.resetQueued { 5281 // Already have a stream error in flight. Don't send another. 5282 return nil 5283 } 5284 return http2streamError(id, http2ErrCodeStreamClosed) 5285 } 5286 if st.body == nil { 5287 panic("internal error: should have a body in this state") 5288 } 5289 5290 // Sender sending more than they'd declared? 5291 if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes { 5292 st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes)) 5293 return http2streamError(id, http2ErrCodeStreamClosed) 5294 } 5295 if f.Length > 0 { 5296 // Check whether the client has flow control quota. 5297 if st.inflow.available() < int32(f.Length) { 5298 return http2streamError(id, http2ErrCodeFlowControl) 5299 } 5300 st.inflow.take(int32(f.Length)) 5301 5302 if len(data) > 0 { 5303 wrote, err := st.body.Write(data) 5304 if err != nil { 5305 return http2streamError(id, http2ErrCodeStreamClosed) 5306 } 5307 if wrote != len(data) { 5308 panic("internal error: bad Writer") 5309 } 5310 st.bodyBytes += int64(len(data)) 5311 } 5312 5313 // Return any padded flow control now, since we won't 5314 // refund it later on body reads. 5315 if pad := int32(f.Length) - int32(len(data)); pad > 0 { 5316 sc.sendWindowUpdate32(nil, pad) 5317 sc.sendWindowUpdate32(st, pad) 5318 } 5319 } 5320 if f.StreamEnded() { 5321 st.endStream() 5322 } 5323 return nil 5324 } 5325 5326 func (sc *http2serverConn) processGoAway(f *http2GoAwayFrame) error { 5327 sc.serveG.check() 5328 if f.ErrCode != http2ErrCodeNo { 5329 sc.logf("http2: received GOAWAY %+v, starting graceful shutdown", f) 5330 } else { 5331 sc.vlogf("http2: received GOAWAY %+v, starting graceful shutdown", f) 5332 } 5333 sc.startGracefulShutdownInternal() 5334 // http://tools.ietf.org/html/rfc7540#section-6.8 5335 // We should not create any new streams, which means we should disable push. 5336 sc.pushEnabled = false 5337 return nil 5338 } 5339 5340 // isPushed reports whether the stream is server-initiated. 5341 func (st *http2stream) isPushed() bool { 5342 return st.id%2 == 0 5343 } 5344 5345 // endStream closes a Request.Body's pipe. It is called when a DATA 5346 // frame says a request body is over (or after trailers). 5347 func (st *http2stream) endStream() { 5348 sc := st.sc 5349 sc.serveG.check() 5350 5351 if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes { 5352 st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes", 5353 st.declBodyBytes, st.bodyBytes)) 5354 } else { 5355 st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest) 5356 st.body.CloseWithError(io.EOF) 5357 } 5358 st.state = http2stateHalfClosedRemote 5359 } 5360 5361 // copyTrailersToHandlerRequest is run in the Handler's goroutine in 5362 // its Request.Body.Read just before it gets io.EOF. 5363 func (st *http2stream) copyTrailersToHandlerRequest() { 5364 for k, vv := range st.trailer { 5365 if _, ok := st.reqTrailer[k]; ok { 5366 // Only copy it over it was pre-declared. 5367 st.reqTrailer[k] = vv 5368 } 5369 } 5370 } 5371 5372 // onWriteTimeout is run on its own goroutine (from time.AfterFunc) 5373 // when the stream's WriteTimeout has fired. 5374 func (st *http2stream) onWriteTimeout() { 5375 st.sc.writeFrameFromHandler(http2FrameWriteRequest{write: http2streamError(st.id, http2ErrCodeInternal)}) 5376 } 5377 5378 func (sc *http2serverConn) processHeaders(f *http2MetaHeadersFrame) error { 5379 sc.serveG.check() 5380 id := f.StreamID 5381 if sc.inGoAway { 5382 // Ignore. 5383 return nil 5384 } 5385 // http://tools.ietf.org/html/rfc7540#section-5.1.1 5386 // Streams initiated by a client MUST use odd-numbered stream 5387 // identifiers. [...] An endpoint that receives an unexpected 5388 // stream identifier MUST respond with a connection error 5389 // (Section 5.4.1) of type PROTOCOL_ERROR. 5390 if id%2 != 1 { 5391 return http2ConnectionError(http2ErrCodeProtocol) 5392 } 5393 // A HEADERS frame can be used to create a new stream or 5394 // send a trailer for an open one. If we already have a stream 5395 // open, let it process its own HEADERS frame (trailers at this 5396 // point, if it's valid). 5397 if st := sc.streams[f.StreamID]; st != nil { 5398 if st.resetQueued { 5399 // We're sending RST_STREAM to close the stream, so don't bother 5400 // processing this frame. 5401 return nil 5402 } 5403 return st.processTrailerHeaders(f) 5404 } 5405 5406 // [...] The identifier of a newly established stream MUST be 5407 // numerically greater than all streams that the initiating 5408 // endpoint has opened or reserved. [...] An endpoint that 5409 // receives an unexpected stream identifier MUST respond with 5410 // a connection error (Section 5.4.1) of type PROTOCOL_ERROR. 5411 if id <= sc.maxClientStreamID { 5412 return http2ConnectionError(http2ErrCodeProtocol) 5413 } 5414 sc.maxClientStreamID = id 5415 5416 if sc.idleTimer != nil { 5417 sc.idleTimer.Stop() 5418 } 5419 5420 // http://tools.ietf.org/html/rfc7540#section-5.1.2 5421 // [...] Endpoints MUST NOT exceed the limit set by their peer. An 5422 // endpoint that receives a HEADERS frame that causes their 5423 // advertised concurrent stream limit to be exceeded MUST treat 5424 // this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR 5425 // or REFUSED_STREAM. 5426 if sc.curClientStreams+1 > sc.advMaxStreams { 5427 if sc.unackedSettings == 0 { 5428 // They should know better. 5429 return http2streamError(id, http2ErrCodeProtocol) 5430 } 5431 // Assume it's a network race, where they just haven't 5432 // received our last SETTINGS update. But actually 5433 // this can't happen yet, because we don't yet provide 5434 // a way for users to adjust server parameters at 5435 // runtime. 5436 return http2streamError(id, http2ErrCodeRefusedStream) 5437 } 5438 5439 initialState := http2stateOpen 5440 if f.StreamEnded() { 5441 initialState = http2stateHalfClosedRemote 5442 } 5443 st := sc.newStream(id, 0, initialState) 5444 5445 if f.HasPriority() { 5446 if err := http2checkPriority(f.StreamID, f.Priority); err != nil { 5447 return err 5448 } 5449 sc.writeSched.AdjustStream(st.id, f.Priority) 5450 } 5451 5452 rw, req, err := sc.newWriterAndRequest(st, f) 5453 if err != nil { 5454 return err 5455 } 5456 st.reqTrailer = req.Trailer 5457 if st.reqTrailer != nil { 5458 st.trailer = make(Header) 5459 } 5460 st.body = req.Body.(*http2requestBody).pipe // may be nil 5461 st.declBodyBytes = req.ContentLength 5462 5463 handler := sc.handler.ServeHTTP 5464 if f.Truncated { 5465 // Their header list was too long. Send a 431 error. 5466 handler = http2handleHeaderListTooLong 5467 } else if err := http2checkValidHTTP2RequestHeaders(req.Header); err != nil { 5468 handler = http2new400Handler(err) 5469 } 5470 5471 // The net/http package sets the read deadline from the 5472 // http.Server.ReadTimeout during the TLS handshake, but then 5473 // passes the connection off to us with the deadline already 5474 // set. Disarm it here after the request headers are read, 5475 // similar to how the http1 server works. Here it's 5476 // technically more like the http1 Server's ReadHeaderTimeout 5477 // (in Go 1.8), though. That's a more sane option anyway. 5478 if sc.hs.ReadTimeout != 0 { 5479 sc.conn.SetReadDeadline(time.Time{}) 5480 } 5481 5482 go sc.runHandler(rw, req, handler) 5483 return nil 5484 } 5485 5486 func (st *http2stream) processTrailerHeaders(f *http2MetaHeadersFrame) error { 5487 sc := st.sc 5488 sc.serveG.check() 5489 if st.gotTrailerHeader { 5490 return http2ConnectionError(http2ErrCodeProtocol) 5491 } 5492 st.gotTrailerHeader = true 5493 if !f.StreamEnded() { 5494 return http2streamError(st.id, http2ErrCodeProtocol) 5495 } 5496 5497 if len(f.PseudoFields()) > 0 { 5498 return http2streamError(st.id, http2ErrCodeProtocol) 5499 } 5500 if st.trailer != nil { 5501 for _, hf := range f.RegularFields() { 5502 key := sc.canonicalHeader(hf.Name) 5503 if !http2ValidTrailerHeader(key) { 5504 // TODO: send more details to the peer somehow. But http2 has 5505 // no way to send debug data at a stream level. Discuss with 5506 // HTTP folk. 5507 return http2streamError(st.id, http2ErrCodeProtocol) 5508 } 5509 st.trailer[key] = append(st.trailer[key], hf.Value) 5510 } 5511 } 5512 st.endStream() 5513 return nil 5514 } 5515 5516 func http2checkPriority(streamID uint32, p http2PriorityParam) error { 5517 if streamID == p.StreamDep { 5518 // Section 5.3.1: "A stream cannot depend on itself. An endpoint MUST treat 5519 // this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR." 5520 // Section 5.3.3 says that a stream can depend on one of its dependencies, 5521 // so it's only self-dependencies that are forbidden. 5522 return http2streamError(streamID, http2ErrCodeProtocol) 5523 } 5524 return nil 5525 } 5526 5527 func (sc *http2serverConn) processPriority(f *http2PriorityFrame) error { 5528 if sc.inGoAway { 5529 return nil 5530 } 5531 if err := http2checkPriority(f.StreamID, f.http2PriorityParam); err != nil { 5532 return err 5533 } 5534 sc.writeSched.AdjustStream(f.StreamID, f.http2PriorityParam) 5535 return nil 5536 } 5537 5538 func (sc *http2serverConn) newStream(id, pusherID uint32, state http2streamState) *http2stream { 5539 sc.serveG.check() 5540 if id == 0 { 5541 panic("internal error: cannot create stream with id 0") 5542 } 5543 5544 ctx, cancelCtx := http2contextWithCancel(sc.baseCtx) 5545 st := &http2stream{ 5546 sc: sc, 5547 id: id, 5548 state: state, 5549 ctx: ctx, 5550 cancelCtx: cancelCtx, 5551 } 5552 st.cw.Init() 5553 st.flow.conn = &sc.flow // link to conn-level counter 5554 st.flow.add(sc.initialStreamSendWindowSize) 5555 st.inflow.conn = &sc.inflow // link to conn-level counter 5556 st.inflow.add(sc.srv.initialStreamRecvWindowSize()) 5557 if sc.hs.WriteTimeout != 0 { 5558 st.writeDeadline = time.AfterFunc(sc.hs.WriteTimeout, st.onWriteTimeout) 5559 } 5560 5561 sc.streams[id] = st 5562 sc.writeSched.OpenStream(st.id, http2OpenStreamOptions{PusherID: pusherID}) 5563 if st.isPushed() { 5564 sc.curPushedStreams++ 5565 } else { 5566 sc.curClientStreams++ 5567 } 5568 if sc.curOpenStreams() == 1 { 5569 sc.setConnState(StateActive) 5570 } 5571 5572 return st 5573 } 5574 5575 func (sc *http2serverConn) newWriterAndRequest(st *http2stream, f *http2MetaHeadersFrame) (*http2responseWriter, *Request, error) { 5576 sc.serveG.check() 5577 5578 rp := http2requestParam{ 5579 method: f.PseudoValue("method"), 5580 scheme: f.PseudoValue("scheme"), 5581 authority: f.PseudoValue("authority"), 5582 path: f.PseudoValue("path"), 5583 } 5584 5585 isConnect := rp.method == "CONNECT" 5586 if isConnect { 5587 if rp.path != "" || rp.scheme != "" || rp.authority == "" { 5588 return nil, nil, http2streamError(f.StreamID, http2ErrCodeProtocol) 5589 } 5590 } else if rp.method == "" || rp.path == "" || (rp.scheme != "https" && rp.scheme != "http") { 5591 // See 8.1.2.6 Malformed Requests and Responses: 5592 // 5593 // Malformed requests or responses that are detected 5594 // MUST be treated as a stream error (Section 5.4.2) 5595 // of type PROTOCOL_ERROR." 5596 // 5597 // 8.1.2.3 Request Pseudo-Header Fields 5598 // "All HTTP/2 requests MUST include exactly one valid 5599 // value for the :method, :scheme, and :path 5600 // pseudo-header fields" 5601 return nil, nil, http2streamError(f.StreamID, http2ErrCodeProtocol) 5602 } 5603 5604 bodyOpen := !f.StreamEnded() 5605 if rp.method == "HEAD" && bodyOpen { 5606 // HEAD requests can't have bodies 5607 return nil, nil, http2streamError(f.StreamID, http2ErrCodeProtocol) 5608 } 5609 5610 rp.header = make(Header) 5611 for _, hf := range f.RegularFields() { 5612 rp.header.Add(sc.canonicalHeader(hf.Name), hf.Value) 5613 } 5614 if rp.authority == "" { 5615 rp.authority = rp.header.Get("Host") 5616 } 5617 5618 rw, req, err := sc.newWriterAndRequestNoBody(st, rp) 5619 if err != nil { 5620 return nil, nil, err 5621 } 5622 if bodyOpen { 5623 if vv, ok := rp.header["Content-Length"]; ok { 5624 req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64) 5625 } else { 5626 req.ContentLength = -1 5627 } 5628 req.Body.(*http2requestBody).pipe = &http2pipe{ 5629 b: &http2dataBuffer{expected: req.ContentLength}, 5630 } 5631 } 5632 return rw, req, nil 5633 } 5634 5635 type http2requestParam struct { 5636 method string 5637 scheme, authority, path string 5638 header Header 5639 } 5640 5641 func (sc *http2serverConn) newWriterAndRequestNoBody(st *http2stream, rp http2requestParam) (*http2responseWriter, *Request, error) { 5642 sc.serveG.check() 5643 5644 var tlsState *tls.ConnectionState // nil if not scheme https 5645 if rp.scheme == "https" { 5646 tlsState = sc.tlsState 5647 } 5648 5649 needsContinue := rp.header.Get("Expect") == "100-continue" 5650 if needsContinue { 5651 rp.header.Del("Expect") 5652 } 5653 // Merge Cookie headers into one "; "-delimited value. 5654 if cookies := rp.header["Cookie"]; len(cookies) > 1 { 5655 rp.header.Set("Cookie", strings.Join(cookies, "; ")) 5656 } 5657 5658 // Setup Trailers 5659 var trailer Header 5660 for _, v := range rp.header["Trailer"] { 5661 for _, key := range strings.Split(v, ",") { 5662 key = CanonicalHeaderKey(strings.TrimSpace(key)) 5663 switch key { 5664 case "Transfer-Encoding", "Trailer", "Content-Length": 5665 // Bogus. (copy of http1 rules) 5666 // Ignore. 5667 default: 5668 if trailer == nil { 5669 trailer = make(Header) 5670 } 5671 trailer[key] = nil 5672 } 5673 } 5674 } 5675 delete(rp.header, "Trailer") 5676 5677 var url_ *url.URL 5678 var requestURI string 5679 if rp.method == "CONNECT" { 5680 url_ = &url.URL{Host: rp.authority} 5681 requestURI = rp.authority // mimic HTTP/1 server behavior 5682 } else { 5683 var err error 5684 url_, err = url.ParseRequestURI(rp.path) 5685 if err != nil { 5686 return nil, nil, http2streamError(st.id, http2ErrCodeProtocol) 5687 } 5688 requestURI = rp.path 5689 } 5690 5691 body := &http2requestBody{ 5692 conn: sc, 5693 stream: st, 5694 needsContinue: needsContinue, 5695 } 5696 req := &Request{ 5697 Method: rp.method, 5698 URL: url_, 5699 RemoteAddr: sc.remoteAddrStr, 5700 Header: rp.header, 5701 RequestURI: requestURI, 5702 Proto: "HTTP/2.0", 5703 ProtoMajor: 2, 5704 ProtoMinor: 0, 5705 TLS: tlsState, 5706 Host: rp.authority, 5707 Body: body, 5708 Trailer: trailer, 5709 } 5710 req = http2requestWithContext(req, st.ctx) 5711 5712 rws := http2responseWriterStatePool.Get().(*http2responseWriterState) 5713 bwSave := rws.bw 5714 *rws = http2responseWriterState{} // zero all the fields 5715 rws.conn = sc 5716 rws.bw = bwSave 5717 rws.bw.Reset(http2chunkWriter{rws}) 5718 rws.stream = st 5719 rws.req = req 5720 rws.body = body 5721 5722 rw := &http2responseWriter{rws: rws} 5723 return rw, req, nil 5724 } 5725 5726 // Run on its own goroutine. 5727 func (sc *http2serverConn) runHandler(rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) { 5728 didPanic := true 5729 defer func() { 5730 rw.rws.stream.cancelCtx() 5731 if didPanic { 5732 e := recover() 5733 sc.writeFrameFromHandler(http2FrameWriteRequest{ 5734 write: http2handlerPanicRST{rw.rws.stream.id}, 5735 stream: rw.rws.stream, 5736 }) 5737 // Same as net/http: 5738 if http2shouldLogPanic(e) { 5739 const size = 64 << 10 5740 buf := make([]byte, size) 5741 buf = buf[:runtime.Stack(buf, false)] 5742 sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf) 5743 } 5744 return 5745 } 5746 rw.handlerDone() 5747 }() 5748 handler(rw, req) 5749 didPanic = false 5750 } 5751 5752 func http2handleHeaderListTooLong(w ResponseWriter, r *Request) { 5753 // 10.5.1 Limits on Header Block Size: 5754 // .. "A server that receives a larger header block than it is 5755 // willing to handle can send an HTTP 431 (Request Header Fields Too 5756 // Large) status code" 5757 const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+ 5758 w.WriteHeader(statusRequestHeaderFieldsTooLarge) 5759 io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>") 5760 } 5761 5762 // called from handler goroutines. 5763 // h may be nil. 5764 func (sc *http2serverConn) writeHeaders(st *http2stream, headerData *http2writeResHeaders) error { 5765 sc.serveG.checkNotOn() // NOT on 5766 var errc chan error 5767 if headerData.h != nil { 5768 // If there's a header map (which we don't own), so we have to block on 5769 // waiting for this frame to be written, so an http.Flush mid-handler 5770 // writes out the correct value of keys, before a handler later potentially 5771 // mutates it. 5772 errc = http2errChanPool.Get().(chan error) 5773 } 5774 if err := sc.writeFrameFromHandler(http2FrameWriteRequest{ 5775 write: headerData, 5776 stream: st, 5777 done: errc, 5778 }); err != nil { 5779 return err 5780 } 5781 if errc != nil { 5782 select { 5783 case err := <-errc: 5784 http2errChanPool.Put(errc) 5785 return err 5786 case <-sc.doneServing: 5787 return http2errClientDisconnected 5788 case <-st.cw: 5789 return http2errStreamClosed 5790 } 5791 } 5792 return nil 5793 } 5794 5795 // called from handler goroutines. 5796 func (sc *http2serverConn) write100ContinueHeaders(st *http2stream) { 5797 sc.writeFrameFromHandler(http2FrameWriteRequest{ 5798 write: http2write100ContinueHeadersFrame{st.id}, 5799 stream: st, 5800 }) 5801 } 5802 5803 // A bodyReadMsg tells the server loop that the http.Handler read n 5804 // bytes of the DATA from the client on the given stream. 5805 type http2bodyReadMsg struct { 5806 st *http2stream 5807 n int 5808 } 5809 5810 // called from handler goroutines. 5811 // Notes that the handler for the given stream ID read n bytes of its body 5812 // and schedules flow control tokens to be sent. 5813 func (sc *http2serverConn) noteBodyReadFromHandler(st *http2stream, n int, err error) { 5814 sc.serveG.checkNotOn() // NOT on 5815 if n > 0 { 5816 select { 5817 case sc.bodyReadCh <- http2bodyReadMsg{st, n}: 5818 case <-sc.doneServing: 5819 } 5820 } 5821 } 5822 5823 func (sc *http2serverConn) noteBodyRead(st *http2stream, n int) { 5824 sc.serveG.check() 5825 sc.sendWindowUpdate(nil, n) // conn-level 5826 if st.state != http2stateHalfClosedRemote && st.state != http2stateClosed { 5827 // Don't send this WINDOW_UPDATE if the stream is closed 5828 // remotely. 5829 sc.sendWindowUpdate(st, n) 5830 } 5831 } 5832 5833 // st may be nil for conn-level 5834 func (sc *http2serverConn) sendWindowUpdate(st *http2stream, n int) { 5835 sc.serveG.check() 5836 // "The legal range for the increment to the flow control 5837 // window is 1 to 2^31-1 (2,147,483,647) octets." 5838 // A Go Read call on 64-bit machines could in theory read 5839 // a larger Read than this. Very unlikely, but we handle it here 5840 // rather than elsewhere for now. 5841 const maxUint31 = 1<<31 - 1 5842 for n >= maxUint31 { 5843 sc.sendWindowUpdate32(st, maxUint31) 5844 n -= maxUint31 5845 } 5846 sc.sendWindowUpdate32(st, int32(n)) 5847 } 5848 5849 // st may be nil for conn-level 5850 func (sc *http2serverConn) sendWindowUpdate32(st *http2stream, n int32) { 5851 sc.serveG.check() 5852 if n == 0 { 5853 return 5854 } 5855 if n < 0 { 5856 panic("negative update") 5857 } 5858 var streamID uint32 5859 if st != nil { 5860 streamID = st.id 5861 } 5862 sc.writeFrame(http2FrameWriteRequest{ 5863 write: http2writeWindowUpdate{streamID: streamID, n: uint32(n)}, 5864 stream: st, 5865 }) 5866 var ok bool 5867 if st == nil { 5868 ok = sc.inflow.add(n) 5869 } else { 5870 ok = st.inflow.add(n) 5871 } 5872 if !ok { 5873 panic("internal error; sent too many window updates without decrements?") 5874 } 5875 } 5876 5877 // requestBody is the Handler's Request.Body type. 5878 // Read and Close may be called concurrently. 5879 type http2requestBody struct { 5880 stream *http2stream 5881 conn *http2serverConn 5882 closed bool // for use by Close only 5883 sawEOF bool // for use by Read only 5884 pipe *http2pipe // non-nil if we have a HTTP entity message body 5885 needsContinue bool // need to send a 100-continue 5886 } 5887 5888 func (b *http2requestBody) Close() error { 5889 if b.pipe != nil && !b.closed { 5890 b.pipe.BreakWithError(http2errClosedBody) 5891 } 5892 b.closed = true 5893 return nil 5894 } 5895 5896 func (b *http2requestBody) Read(p []byte) (n int, err error) { 5897 if b.needsContinue { 5898 b.needsContinue = false 5899 b.conn.write100ContinueHeaders(b.stream) 5900 } 5901 if b.pipe == nil || b.sawEOF { 5902 return 0, io.EOF 5903 } 5904 n, err = b.pipe.Read(p) 5905 if err == io.EOF { 5906 b.sawEOF = true 5907 } 5908 if b.conn == nil && http2inTests { 5909 return 5910 } 5911 b.conn.noteBodyReadFromHandler(b.stream, n, err) 5912 return 5913 } 5914 5915 // responseWriter is the http.ResponseWriter implementation. It's 5916 // intentionally small (1 pointer wide) to minimize garbage. The 5917 // responseWriterState pointer inside is zeroed at the end of a 5918 // request (in handlerDone) and calls on the responseWriter thereafter 5919 // simply crash (caller's mistake), but the much larger responseWriterState 5920 // and buffers are reused between multiple requests. 5921 type http2responseWriter struct { 5922 rws *http2responseWriterState 5923 } 5924 5925 // Optional http.ResponseWriter interfaces implemented. 5926 var ( 5927 _ CloseNotifier = (*http2responseWriter)(nil) 5928 _ Flusher = (*http2responseWriter)(nil) 5929 _ http2stringWriter = (*http2responseWriter)(nil) 5930 ) 5931 5932 type http2responseWriterState struct { 5933 // immutable within a request: 5934 stream *http2stream 5935 req *Request 5936 body *http2requestBody // to close at end of request, if DATA frames didn't 5937 conn *http2serverConn 5938 5939 // TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc 5940 bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState} 5941 5942 // mutated by http.Handler goroutine: 5943 handlerHeader Header // nil until called 5944 snapHeader Header // snapshot of handlerHeader at WriteHeader time 5945 trailers []string // set in writeChunk 5946 status int // status code passed to WriteHeader 5947 wroteHeader bool // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet. 5948 sentHeader bool // have we sent the header frame? 5949 handlerDone bool // handler has finished 5950 dirty bool // a Write failed; don't reuse this responseWriterState 5951 5952 sentContentLen int64 // non-zero if handler set a Content-Length header 5953 wroteBytes int64 5954 5955 closeNotifierMu sync.Mutex // guards closeNotifierCh 5956 closeNotifierCh chan bool // nil until first used 5957 } 5958 5959 type http2chunkWriter struct{ rws *http2responseWriterState } 5960 5961 func (cw http2chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) } 5962 5963 func (rws *http2responseWriterState) hasTrailers() bool { return len(rws.trailers) != 0 } 5964 5965 // declareTrailer is called for each Trailer header when the 5966 // response header is written. It notes that a header will need to be 5967 // written in the trailers at the end of the response. 5968 func (rws *http2responseWriterState) declareTrailer(k string) { 5969 k = CanonicalHeaderKey(k) 5970 if !http2ValidTrailerHeader(k) { 5971 // Forbidden by RFC 2616 14.40. 5972 rws.conn.logf("ignoring invalid trailer %q", k) 5973 return 5974 } 5975 if !http2strSliceContains(rws.trailers, k) { 5976 rws.trailers = append(rws.trailers, k) 5977 } 5978 } 5979 5980 // writeChunk writes chunks from the bufio.Writer. But because 5981 // bufio.Writer may bypass its chunking, sometimes p may be 5982 // arbitrarily large. 5983 // 5984 // writeChunk is also responsible (on the first chunk) for sending the 5985 // HEADER response. 5986 func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) { 5987 if !rws.wroteHeader { 5988 rws.writeHeader(200) 5989 } 5990 5991 isHeadResp := rws.req.Method == "HEAD" 5992 if !rws.sentHeader { 5993 rws.sentHeader = true 5994 var ctype, clen string 5995 if clen = rws.snapHeader.Get("Content-Length"); clen != "" { 5996 rws.snapHeader.Del("Content-Length") 5997 clen64, err := strconv.ParseInt(clen, 10, 64) 5998 if err == nil && clen64 >= 0 { 5999 rws.sentContentLen = clen64 6000 } else { 6001 clen = "" 6002 } 6003 } 6004 if clen == "" && rws.handlerDone && http2bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) { 6005 clen = strconv.Itoa(len(p)) 6006 } 6007 _, hasContentType := rws.snapHeader["Content-Type"] 6008 if !hasContentType && http2bodyAllowedForStatus(rws.status) { 6009 ctype = DetectContentType(p) 6010 } 6011 var date string 6012 if _, ok := rws.snapHeader["Date"]; !ok { 6013 // TODO(bradfitz): be faster here, like net/http? measure. 6014 date = time.Now().UTC().Format(TimeFormat) 6015 } 6016 6017 for _, v := range rws.snapHeader["Trailer"] { 6018 http2foreachHeaderElement(v, rws.declareTrailer) 6019 } 6020 6021 endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp 6022 err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{ 6023 streamID: rws.stream.id, 6024 httpResCode: rws.status, 6025 h: rws.snapHeader, 6026 endStream: endStream, 6027 contentType: ctype, 6028 contentLength: clen, 6029 date: date, 6030 }) 6031 if err != nil { 6032 rws.dirty = true 6033 return 0, err 6034 } 6035 if endStream { 6036 return 0, nil 6037 } 6038 } 6039 if isHeadResp { 6040 return len(p), nil 6041 } 6042 if len(p) == 0 && !rws.handlerDone { 6043 return 0, nil 6044 } 6045 6046 if rws.handlerDone { 6047 rws.promoteUndeclaredTrailers() 6048 } 6049 6050 endStream := rws.handlerDone && !rws.hasTrailers() 6051 if len(p) > 0 || endStream { 6052 // only send a 0 byte DATA frame if we're ending the stream. 6053 if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil { 6054 rws.dirty = true 6055 return 0, err 6056 } 6057 } 6058 6059 if rws.handlerDone && rws.hasTrailers() { 6060 err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{ 6061 streamID: rws.stream.id, 6062 h: rws.handlerHeader, 6063 trailers: rws.trailers, 6064 endStream: true, 6065 }) 6066 if err != nil { 6067 rws.dirty = true 6068 } 6069 return len(p), err 6070 } 6071 return len(p), nil 6072 } 6073 6074 // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys 6075 // that, if present, signals that the map entry is actually for 6076 // the response trailers, and not the response headers. The prefix 6077 // is stripped after the ServeHTTP call finishes and the values are 6078 // sent in the trailers. 6079 // 6080 // This mechanism is intended only for trailers that are not known 6081 // prior to the headers being written. If the set of trailers is fixed 6082 // or known before the header is written, the normal Go trailers mechanism 6083 // is preferred: 6084 // https://golang.org/pkg/net/http/#ResponseWriter 6085 // https://golang.org/pkg/net/http/#example_ResponseWriter_trailers 6086 const http2TrailerPrefix = "Trailer:" 6087 6088 // promoteUndeclaredTrailers permits http.Handlers to set trailers 6089 // after the header has already been flushed. Because the Go 6090 // ResponseWriter interface has no way to set Trailers (only the 6091 // Header), and because we didn't want to expand the ResponseWriter 6092 // interface, and because nobody used trailers, and because RFC 2616 6093 // says you SHOULD (but not must) predeclare any trailers in the 6094 // header, the official ResponseWriter rules said trailers in Go must 6095 // be predeclared, and then we reuse the same ResponseWriter.Header() 6096 // map to mean both Headers and Trailers. When it's time to write the 6097 // Trailers, we pick out the fields of Headers that were declared as 6098 // trailers. That worked for a while, until we found the first major 6099 // user of Trailers in the wild: gRPC (using them only over http2), 6100 // and gRPC libraries permit setting trailers mid-stream without 6101 // predeclarnig them. So: change of plans. We still permit the old 6102 // way, but we also permit this hack: if a Header() key begins with 6103 // "Trailer:", the suffix of that key is a Trailer. Because ':' is an 6104 // invalid token byte anyway, there is no ambiguity. (And it's already 6105 // filtered out) It's mildly hacky, but not terrible. 6106 // 6107 // This method runs after the Handler is done and promotes any Header 6108 // fields to be trailers. 6109 func (rws *http2responseWriterState) promoteUndeclaredTrailers() { 6110 for k, vv := range rws.handlerHeader { 6111 if !strings.HasPrefix(k, http2TrailerPrefix) { 6112 continue 6113 } 6114 trailerKey := strings.TrimPrefix(k, http2TrailerPrefix) 6115 rws.declareTrailer(trailerKey) 6116 rws.handlerHeader[CanonicalHeaderKey(trailerKey)] = vv 6117 } 6118 6119 if len(rws.trailers) > 1 { 6120 sorter := http2sorterPool.Get().(*http2sorter) 6121 sorter.SortStrings(rws.trailers) 6122 http2sorterPool.Put(sorter) 6123 } 6124 } 6125 6126 func (w *http2responseWriter) Flush() { 6127 rws := w.rws 6128 if rws == nil { 6129 panic("Header called after Handler finished") 6130 } 6131 if rws.bw.Buffered() > 0 { 6132 if err := rws.bw.Flush(); err != nil { 6133 // Ignore the error. The frame writer already knows. 6134 return 6135 } 6136 } else { 6137 // The bufio.Writer won't call chunkWriter.Write 6138 // (writeChunk with zero bytes, so we have to do it 6139 // ourselves to force the HTTP response header and/or 6140 // final DATA frame (with END_STREAM) to be sent. 6141 rws.writeChunk(nil) 6142 } 6143 } 6144 6145 func (w *http2responseWriter) CloseNotify() <-chan bool { 6146 rws := w.rws 6147 if rws == nil { 6148 panic("CloseNotify called after Handler finished") 6149 } 6150 rws.closeNotifierMu.Lock() 6151 ch := rws.closeNotifierCh 6152 if ch == nil { 6153 ch = make(chan bool, 1) 6154 rws.closeNotifierCh = ch 6155 cw := rws.stream.cw 6156 go func() { 6157 cw.Wait() // wait for close 6158 ch <- true 6159 }() 6160 } 6161 rws.closeNotifierMu.Unlock() 6162 return ch 6163 } 6164 6165 func (w *http2responseWriter) Header() Header { 6166 rws := w.rws 6167 if rws == nil { 6168 panic("Header called after Handler finished") 6169 } 6170 if rws.handlerHeader == nil { 6171 rws.handlerHeader = make(Header) 6172 } 6173 return rws.handlerHeader 6174 } 6175 6176 func (w *http2responseWriter) WriteHeader(code int) { 6177 rws := w.rws 6178 if rws == nil { 6179 panic("WriteHeader called after Handler finished") 6180 } 6181 rws.writeHeader(code) 6182 } 6183 6184 func (rws *http2responseWriterState) writeHeader(code int) { 6185 if !rws.wroteHeader { 6186 rws.wroteHeader = true 6187 rws.status = code 6188 if len(rws.handlerHeader) > 0 { 6189 rws.snapHeader = http2cloneHeader(rws.handlerHeader) 6190 } 6191 } 6192 } 6193 6194 func http2cloneHeader(h Header) Header { 6195 h2 := make(Header, len(h)) 6196 for k, vv := range h { 6197 vv2 := make([]string, len(vv)) 6198 copy(vv2, vv) 6199 h2[k] = vv2 6200 } 6201 return h2 6202 } 6203 6204 // The Life Of A Write is like this: 6205 // 6206 // * Handler calls w.Write or w.WriteString -> 6207 // * -> rws.bw (*bufio.Writer) -> 6208 // * (Handler might call Flush) 6209 // * -> chunkWriter{rws} 6210 // * -> responseWriterState.writeChunk(p []byte) 6211 // * -> responseWriterState.writeChunk (most of the magic; see comment there) 6212 func (w *http2responseWriter) Write(p []byte) (n int, err error) { 6213 return w.write(len(p), p, "") 6214 } 6215 6216 func (w *http2responseWriter) WriteString(s string) (n int, err error) { 6217 return w.write(len(s), nil, s) 6218 } 6219 6220 // either dataB or dataS is non-zero. 6221 func (w *http2responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) { 6222 rws := w.rws 6223 if rws == nil { 6224 panic("Write called after Handler finished") 6225 } 6226 if !rws.wroteHeader { 6227 w.WriteHeader(200) 6228 } 6229 if !http2bodyAllowedForStatus(rws.status) { 6230 return 0, ErrBodyNotAllowed 6231 } 6232 rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) // only one can be set 6233 if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen { 6234 // TODO: send a RST_STREAM 6235 return 0, errors.New("http2: handler wrote more than declared Content-Length") 6236 } 6237 6238 if dataB != nil { 6239 return rws.bw.Write(dataB) 6240 } else { 6241 return rws.bw.WriteString(dataS) 6242 } 6243 } 6244 6245 func (w *http2responseWriter) handlerDone() { 6246 rws := w.rws 6247 dirty := rws.dirty 6248 rws.handlerDone = true 6249 w.Flush() 6250 w.rws = nil 6251 if !dirty { 6252 // Only recycle the pool if all prior Write calls to 6253 // the serverConn goroutine completed successfully. If 6254 // they returned earlier due to resets from the peer 6255 // there might still be write goroutines outstanding 6256 // from the serverConn referencing the rws memory. See 6257 // issue 20704. 6258 http2responseWriterStatePool.Put(rws) 6259 } 6260 } 6261 6262 // Push errors. 6263 var ( 6264 http2ErrRecursivePush = errors.New("http2: recursive push not allowed") 6265 http2ErrPushLimitReached = errors.New("http2: push would exceed peer's SETTINGS_MAX_CONCURRENT_STREAMS") 6266 ) 6267 6268 // pushOptions is the internal version of http.PushOptions, which we 6269 // cannot include here because it's only defined in Go 1.8 and later. 6270 type http2pushOptions struct { 6271 Method string 6272 Header Header 6273 } 6274 6275 func (w *http2responseWriter) push(target string, opts http2pushOptions) error { 6276 st := w.rws.stream 6277 sc := st.sc 6278 sc.serveG.checkNotOn() 6279 6280 // No recursive pushes: "PUSH_PROMISE frames MUST only be sent on a peer-initiated stream." 6281 // http://tools.ietf.org/html/rfc7540#section-6.6 6282 if st.isPushed() { 6283 return http2ErrRecursivePush 6284 } 6285 6286 // Default options. 6287 if opts.Method == "" { 6288 opts.Method = "GET" 6289 } 6290 if opts.Header == nil { 6291 opts.Header = Header{} 6292 } 6293 wantScheme := "http" 6294 if w.rws.req.TLS != nil { 6295 wantScheme = "https" 6296 } 6297 6298 // Validate the request. 6299 u, err := url.Parse(target) 6300 if err != nil { 6301 return err 6302 } 6303 if u.Scheme == "" { 6304 if !strings.HasPrefix(target, "/") { 6305 return fmt.Errorf("target must be an absolute URL or an absolute path: %q", target) 6306 } 6307 u.Scheme = wantScheme 6308 u.Host = w.rws.req.Host 6309 } else { 6310 if u.Scheme != wantScheme { 6311 return fmt.Errorf("cannot push URL with scheme %q from request with scheme %q", u.Scheme, wantScheme) 6312 } 6313 if u.Host == "" { 6314 return errors.New("URL must have a host") 6315 } 6316 } 6317 for k := range opts.Header { 6318 if strings.HasPrefix(k, ":") { 6319 return fmt.Errorf("promised request headers cannot include pseudo header %q", k) 6320 } 6321 // These headers are meaningful only if the request has a body, 6322 // but PUSH_PROMISE requests cannot have a body. 6323 // http://tools.ietf.org/html/rfc7540#section-8.2 6324 // Also disallow Host, since the promised URL must be absolute. 6325 switch strings.ToLower(k) { 6326 case "content-length", "content-encoding", "trailer", "te", "expect", "host": 6327 return fmt.Errorf("promised request headers cannot include %q", k) 6328 } 6329 } 6330 if err := http2checkValidHTTP2RequestHeaders(opts.Header); err != nil { 6331 return err 6332 } 6333 6334 // The RFC effectively limits promised requests to GET and HEAD: 6335 // "Promised requests MUST be cacheable [GET, HEAD, or POST], and MUST be safe [GET or HEAD]" 6336 // http://tools.ietf.org/html/rfc7540#section-8.2 6337 if opts.Method != "GET" && opts.Method != "HEAD" { 6338 return fmt.Errorf("method %q must be GET or HEAD", opts.Method) 6339 } 6340 6341 msg := &http2startPushRequest{ 6342 parent: st, 6343 method: opts.Method, 6344 url: u, 6345 header: http2cloneHeader(opts.Header), 6346 done: http2errChanPool.Get().(chan error), 6347 } 6348 6349 select { 6350 case <-sc.doneServing: 6351 return http2errClientDisconnected 6352 case <-st.cw: 6353 return http2errStreamClosed 6354 case sc.serveMsgCh <- msg: 6355 } 6356 6357 select { 6358 case <-sc.doneServing: 6359 return http2errClientDisconnected 6360 case <-st.cw: 6361 return http2errStreamClosed 6362 case err := <-msg.done: 6363 http2errChanPool.Put(msg.done) 6364 return err 6365 } 6366 } 6367 6368 type http2startPushRequest struct { 6369 parent *http2stream 6370 method string 6371 url *url.URL 6372 header Header 6373 done chan error 6374 } 6375 6376 func (sc *http2serverConn) startPush(msg *http2startPushRequest) { 6377 sc.serveG.check() 6378 6379 // http://tools.ietf.org/html/rfc7540#section-6.6. 6380 // PUSH_PROMISE frames MUST only be sent on a peer-initiated stream that 6381 // is in either the "open" or "half-closed (remote)" state. 6382 if msg.parent.state != http2stateOpen && msg.parent.state != http2stateHalfClosedRemote { 6383 // responseWriter.Push checks that the stream is peer-initiaed. 6384 msg.done <- http2errStreamClosed 6385 return 6386 } 6387 6388 // http://tools.ietf.org/html/rfc7540#section-6.6. 6389 if !sc.pushEnabled { 6390 msg.done <- ErrNotSupported 6391 return 6392 } 6393 6394 // PUSH_PROMISE frames must be sent in increasing order by stream ID, so 6395 // we allocate an ID for the promised stream lazily, when the PUSH_PROMISE 6396 // is written. Once the ID is allocated, we start the request handler. 6397 allocatePromisedID := func() (uint32, error) { 6398 sc.serveG.check() 6399 6400 // Check this again, just in case. Technically, we might have received 6401 // an updated SETTINGS by the time we got around to writing this frame. 6402 if !sc.pushEnabled { 6403 return 0, ErrNotSupported 6404 } 6405 // http://tools.ietf.org/html/rfc7540#section-6.5.2. 6406 if sc.curPushedStreams+1 > sc.clientMaxStreams { 6407 return 0, http2ErrPushLimitReached 6408 } 6409 6410 // http://tools.ietf.org/html/rfc7540#section-5.1.1. 6411 // Streams initiated by the server MUST use even-numbered identifiers. 6412 // A server that is unable to establish a new stream identifier can send a GOAWAY 6413 // frame so that the client is forced to open a new connection for new streams. 6414 if sc.maxPushPromiseID+2 >= 1<<31 { 6415 sc.startGracefulShutdownInternal() 6416 return 0, http2ErrPushLimitReached 6417 } 6418 sc.maxPushPromiseID += 2 6419 promisedID := sc.maxPushPromiseID 6420 6421 // http://tools.ietf.org/html/rfc7540#section-8.2. 6422 // Strictly speaking, the new stream should start in "reserved (local)", then 6423 // transition to "half closed (remote)" after sending the initial HEADERS, but 6424 // we start in "half closed (remote)" for simplicity. 6425 // See further comments at the definition of stateHalfClosedRemote. 6426 promised := sc.newStream(promisedID, msg.parent.id, http2stateHalfClosedRemote) 6427 rw, req, err := sc.newWriterAndRequestNoBody(promised, http2requestParam{ 6428 method: msg.method, 6429 scheme: msg.url.Scheme, 6430 authority: msg.url.Host, 6431 path: msg.url.RequestURI(), 6432 header: http2cloneHeader(msg.header), // clone since handler runs concurrently with writing the PUSH_PROMISE 6433 }) 6434 if err != nil { 6435 // Should not happen, since we've already validated msg.url. 6436 panic(fmt.Sprintf("newWriterAndRequestNoBody(%+v): %v", msg.url, err)) 6437 } 6438 6439 go sc.runHandler(rw, req, sc.handler.ServeHTTP) 6440 return promisedID, nil 6441 } 6442 6443 sc.writeFrame(http2FrameWriteRequest{ 6444 write: &http2writePushPromise{ 6445 streamID: msg.parent.id, 6446 method: msg.method, 6447 url: msg.url, 6448 h: msg.header, 6449 allocatePromisedID: allocatePromisedID, 6450 }, 6451 stream: msg.parent, 6452 done: msg.done, 6453 }) 6454 } 6455 6456 // foreachHeaderElement splits v according to the "#rule" construction 6457 // in RFC 2616 section 2.1 and calls fn for each non-empty element. 6458 func http2foreachHeaderElement(v string, fn func(string)) { 6459 v = textproto.TrimString(v) 6460 if v == "" { 6461 return 6462 } 6463 if !strings.Contains(v, ",") { 6464 fn(v) 6465 return 6466 } 6467 for _, f := range strings.Split(v, ",") { 6468 if f = textproto.TrimString(f); f != "" { 6469 fn(f) 6470 } 6471 } 6472 } 6473 6474 // From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2 6475 var http2connHeaders = []string{ 6476 "Connection", 6477 "Keep-Alive", 6478 "Proxy-Connection", 6479 "Transfer-Encoding", 6480 "Upgrade", 6481 } 6482 6483 // checkValidHTTP2RequestHeaders checks whether h is a valid HTTP/2 request, 6484 // per RFC 7540 Section 8.1.2.2. 6485 // The returned error is reported to users. 6486 func http2checkValidHTTP2RequestHeaders(h Header) error { 6487 for _, k := range http2connHeaders { 6488 if _, ok := h[k]; ok { 6489 return fmt.Errorf("request header %q is not valid in HTTP/2", k) 6490 } 6491 } 6492 te := h["Te"] 6493 if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) { 6494 return errors.New(`request header "TE" may only be "trailers" in HTTP/2`) 6495 } 6496 return nil 6497 } 6498 6499 func http2new400Handler(err error) HandlerFunc { 6500 return func(w ResponseWriter, r *Request) { 6501 Error(w, err.Error(), StatusBadRequest) 6502 } 6503 } 6504 6505 // ValidTrailerHeader reports whether name is a valid header field name to appear 6506 // in trailers. 6507 // See: http://tools.ietf.org/html/rfc7230#section-4.1.2 6508 func http2ValidTrailerHeader(name string) bool { 6509 name = CanonicalHeaderKey(name) 6510 if strings.HasPrefix(name, "If-") || http2badTrailer[name] { 6511 return false 6512 } 6513 return true 6514 } 6515 6516 var http2badTrailer = map[string]bool{ 6517 "Authorization": true, 6518 "Cache-Control": true, 6519 "Connection": true, 6520 "Content-Encoding": true, 6521 "Content-Length": true, 6522 "Content-Range": true, 6523 "Content-Type": true, 6524 "Expect": true, 6525 "Host": true, 6526 "Keep-Alive": true, 6527 "Max-Forwards": true, 6528 "Pragma": true, 6529 "Proxy-Authenticate": true, 6530 "Proxy-Authorization": true, 6531 "Proxy-Connection": true, 6532 "Range": true, 6533 "Realm": true, 6534 "Te": true, 6535 "Trailer": true, 6536 "Transfer-Encoding": true, 6537 "Www-Authenticate": true, 6538 } 6539 6540 // h1ServerKeepAlivesDisabled reports whether hs has its keep-alives 6541 // disabled. See comments on h1ServerShutdownChan above for why 6542 // the code is written this way. 6543 func http2h1ServerKeepAlivesDisabled(hs *Server) bool { 6544 var x interface{} = hs 6545 type I interface { 6546 doKeepAlives() bool 6547 } 6548 if hs, ok := x.(I); ok { 6549 return !hs.doKeepAlives() 6550 } 6551 return false 6552 } 6553 6554 const ( 6555 // transportDefaultConnFlow is how many connection-level flow control 6556 // tokens we give the server at start-up, past the default 64k. 6557 http2transportDefaultConnFlow = 1 << 30 6558 6559 // transportDefaultStreamFlow is how many stream-level flow 6560 // control tokens we announce to the peer, and how many bytes 6561 // we buffer per stream. 6562 http2transportDefaultStreamFlow = 4 << 20 6563 6564 // transportDefaultStreamMinRefresh is the minimum number of bytes we'll send 6565 // a stream-level WINDOW_UPDATE for at a time. 6566 http2transportDefaultStreamMinRefresh = 4 << 10 6567 6568 http2defaultUserAgent = "Go-http-client/2.0" 6569 ) 6570 6571 // Transport is an HTTP/2 Transport. 6572 // 6573 // A Transport internally caches connections to servers. It is safe 6574 // for concurrent use by multiple goroutines. 6575 type http2Transport struct { 6576 // DialTLS specifies an optional dial function for creating 6577 // TLS connections for requests. 6578 // 6579 // If DialTLS is nil, tls.Dial is used. 6580 // 6581 // If the returned net.Conn has a ConnectionState method like tls.Conn, 6582 // it will be used to set http.Response.TLS. 6583 DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error) 6584 6585 // TLSClientConfig specifies the TLS configuration to use with 6586 // tls.Client. If nil, the default configuration is used. 6587 TLSClientConfig *tls.Config 6588 6589 // ConnPool optionally specifies an alternate connection pool to use. 6590 // If nil, the default is used. 6591 ConnPool http2ClientConnPool 6592 6593 // DisableCompression, if true, prevents the Transport from 6594 // requesting compression with an "Accept-Encoding: gzip" 6595 // request header when the Request contains no existing 6596 // Accept-Encoding value. If the Transport requests gzip on 6597 // its own and gets a gzipped response, it's transparently 6598 // decoded in the Response.Body. However, if the user 6599 // explicitly requested gzip it is not automatically 6600 // uncompressed. 6601 DisableCompression bool 6602 6603 // AllowHTTP, if true, permits HTTP/2 requests using the insecure, 6604 // plain-text "http" scheme. Note that this does not enable h2c support. 6605 AllowHTTP bool 6606 6607 // MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to 6608 // send in the initial settings frame. It is how many bytes 6609 // of response headers are allow. Unlike the http2 spec, zero here 6610 // means to use a default limit (currently 10MB). If you actually 6611 // want to advertise an ulimited value to the peer, Transport 6612 // interprets the highest possible value here (0xffffffff or 1<<32-1) 6613 // to mean no limit. 6614 MaxHeaderListSize uint32 6615 6616 // t1, if non-nil, is the standard library Transport using 6617 // this transport. Its settings are used (but not its 6618 // RoundTrip method, etc). 6619 t1 *Transport 6620 6621 connPoolOnce sync.Once 6622 connPoolOrDef http2ClientConnPool // non-nil version of ConnPool 6623 } 6624 6625 func (t *http2Transport) maxHeaderListSize() uint32 { 6626 if t.MaxHeaderListSize == 0 { 6627 return 10 << 20 6628 } 6629 if t.MaxHeaderListSize == 0xffffffff { 6630 return 0 6631 } 6632 return t.MaxHeaderListSize 6633 } 6634 6635 func (t *http2Transport) disableCompression() bool { 6636 return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression) 6637 } 6638 6639 var http2errTransportVersion = errors.New("http2: ConfigureTransport is only supported starting at Go 1.6") 6640 6641 // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2. 6642 // It requires Go 1.6 or later and returns an error if the net/http package is too old 6643 // or if t1 has already been HTTP/2-enabled. 6644 func http2ConfigureTransport(t1 *Transport) error { 6645 _, err := http2configureTransport(t1) // in configure_transport.go (go1.6) or not_go16.go 6646 return err 6647 } 6648 6649 func (t *http2Transport) connPool() http2ClientConnPool { 6650 t.connPoolOnce.Do(t.initConnPool) 6651 return t.connPoolOrDef 6652 } 6653 6654 func (t *http2Transport) initConnPool() { 6655 if t.ConnPool != nil { 6656 t.connPoolOrDef = t.ConnPool 6657 } else { 6658 t.connPoolOrDef = &http2clientConnPool{t: t} 6659 } 6660 } 6661 6662 // ClientConn is the state of a single HTTP/2 client connection to an 6663 // HTTP/2 server. 6664 type http2ClientConn struct { 6665 t *http2Transport 6666 tconn net.Conn // usually *tls.Conn, except specialized impls 6667 tlsState *tls.ConnectionState // nil only for specialized impls 6668 singleUse bool // whether being used for a single http.Request 6669 6670 // readLoop goroutine fields: 6671 readerDone chan struct{} // closed on error 6672 readerErr error // set before readerDone is closed 6673 6674 idleTimeout time.Duration // or 0 for never 6675 idleTimer *time.Timer 6676 6677 mu sync.Mutex // guards following 6678 cond *sync.Cond // hold mu; broadcast on flow/closed changes 6679 flow http2flow // our conn-level flow control quota (cs.flow is per stream) 6680 inflow http2flow // peer's conn-level flow control 6681 closed bool 6682 wantSettingsAck bool // we sent a SETTINGS frame and haven't heard back 6683 goAway *http2GoAwayFrame // if non-nil, the GoAwayFrame we received 6684 goAwayDebug string // goAway frame's debug data, retained as a string 6685 streams map[uint32]*http2clientStream // client-initiated 6686 nextStreamID uint32 6687 pendingRequests int // requests blocked and waiting to be sent because len(streams) == maxConcurrentStreams 6688 pings map[[8]byte]chan struct{} // in flight ping data to notification channel 6689 bw *bufio.Writer 6690 br *bufio.Reader 6691 fr *http2Framer 6692 lastActive time.Time 6693 // Settings from peer: (also guarded by mu) 6694 maxFrameSize uint32 6695 maxConcurrentStreams uint32 6696 initialWindowSize uint32 6697 6698 hbuf bytes.Buffer // HPACK encoder writes into this 6699 henc *hpack.Encoder 6700 freeBuf [][]byte 6701 6702 wmu sync.Mutex // held while writing; acquire AFTER mu if holding both 6703 werr error // first write error that has occurred 6704 } 6705 6706 // clientStream is the state for a single HTTP/2 stream. One of these 6707 // is created for each Transport.RoundTrip call. 6708 type http2clientStream struct { 6709 cc *http2ClientConn 6710 req *Request 6711 trace *http2clientTrace // or nil 6712 ID uint32 6713 resc chan http2resAndError 6714 bufPipe http2pipe // buffered pipe with the flow-controlled response payload 6715 startedWrite bool // started request body write; guarded by cc.mu 6716 requestedGzip bool 6717 on100 func() // optional code to run if get a 100 continue response 6718 6719 flow http2flow // guarded by cc.mu 6720 inflow http2flow // guarded by cc.mu 6721 bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read 6722 readErr error // sticky read error; owned by transportResponseBody.Read 6723 stopReqBody error // if non-nil, stop writing req body; guarded by cc.mu 6724 didReset bool // whether we sent a RST_STREAM to the server; guarded by cc.mu 6725 6726 peerReset chan struct{} // closed on peer reset 6727 resetErr error // populated before peerReset is closed 6728 6729 done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu 6730 6731 // owned by clientConnReadLoop: 6732 firstByte bool // got the first response byte 6733 pastHeaders bool // got first MetaHeadersFrame (actual headers) 6734 pastTrailers bool // got optional second MetaHeadersFrame (trailers) 6735 6736 trailer Header // accumulated trailers 6737 resTrailer *Header // client's Response.Trailer 6738 } 6739 6740 // awaitRequestCancel waits for the user to cancel a request or for the done 6741 // channel to be signaled. A non-nil error is returned only if the request was 6742 // canceled. 6743 func http2awaitRequestCancel(req *Request, done <-chan struct{}) error { 6744 ctx := http2reqContext(req) 6745 if req.Cancel == nil && ctx.Done() == nil { 6746 return nil 6747 } 6748 select { 6749 case <-req.Cancel: 6750 return http2errRequestCanceled 6751 case <-ctx.Done(): 6752 return ctx.Err() 6753 case <-done: 6754 return nil 6755 } 6756 } 6757 6758 // awaitRequestCancel waits for the user to cancel a request, its context to 6759 // expire, or for the request to be done (any way it might be removed from the 6760 // cc.streams map: peer reset, successful completion, TCP connection breakage, 6761 // etc). If the request is canceled, then cs will be canceled and closed. 6762 func (cs *http2clientStream) awaitRequestCancel(req *Request) { 6763 if err := http2awaitRequestCancel(req, cs.done); err != nil { 6764 cs.cancelStream() 6765 cs.bufPipe.CloseWithError(err) 6766 } 6767 } 6768 6769 func (cs *http2clientStream) cancelStream() { 6770 cc := cs.cc 6771 cc.mu.Lock() 6772 didReset := cs.didReset 6773 cs.didReset = true 6774 cc.mu.Unlock() 6775 6776 if !didReset { 6777 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 6778 cc.forgetStreamID(cs.ID) 6779 } 6780 } 6781 6782 // checkResetOrDone reports any error sent in a RST_STREAM frame by the 6783 // server, or errStreamClosed if the stream is complete. 6784 func (cs *http2clientStream) checkResetOrDone() error { 6785 select { 6786 case <-cs.peerReset: 6787 return cs.resetErr 6788 case <-cs.done: 6789 return http2errStreamClosed 6790 default: 6791 return nil 6792 } 6793 } 6794 6795 func (cs *http2clientStream) abortRequestBodyWrite(err error) { 6796 if err == nil { 6797 panic("nil error") 6798 } 6799 cc := cs.cc 6800 cc.mu.Lock() 6801 cs.stopReqBody = err 6802 cc.cond.Broadcast() 6803 cc.mu.Unlock() 6804 } 6805 6806 type http2stickyErrWriter struct { 6807 w io.Writer 6808 err *error 6809 } 6810 6811 func (sew http2stickyErrWriter) Write(p []byte) (n int, err error) { 6812 if *sew.err != nil { 6813 return 0, *sew.err 6814 } 6815 n, err = sew.w.Write(p) 6816 *sew.err = err 6817 return 6818 } 6819 6820 var http2ErrNoCachedConn = errors.New("http2: no cached connection was available") 6821 6822 // RoundTripOpt are options for the Transport.RoundTripOpt method. 6823 type http2RoundTripOpt struct { 6824 // OnlyCachedConn controls whether RoundTripOpt may 6825 // create a new TCP connection. If set true and 6826 // no cached connection is available, RoundTripOpt 6827 // will return ErrNoCachedConn. 6828 OnlyCachedConn bool 6829 } 6830 6831 func (t *http2Transport) RoundTrip(req *Request) (*Response, error) { 6832 return t.RoundTripOpt(req, http2RoundTripOpt{}) 6833 } 6834 6835 // authorityAddr returns a given authority (a host/IP, or host:port / ip:port) 6836 // and returns a host:port. The port 443 is added if needed. 6837 func http2authorityAddr(scheme string, authority string) (addr string) { 6838 host, port, err := net.SplitHostPort(authority) 6839 if err != nil { // authority didn't have a port 6840 port = "443" 6841 if scheme == "http" { 6842 port = "80" 6843 } 6844 host = authority 6845 } 6846 if a, err := idna.ToASCII(host); err == nil { 6847 host = a 6848 } 6849 // IPv6 address literal, without a port: 6850 if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { 6851 return host + ":" + port 6852 } 6853 return net.JoinHostPort(host, port) 6854 } 6855 6856 // RoundTripOpt is like RoundTrip, but takes options. 6857 func (t *http2Transport) RoundTripOpt(req *Request, opt http2RoundTripOpt) (*Response, error) { 6858 if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) { 6859 return nil, errors.New("http2: unsupported scheme") 6860 } 6861 6862 addr := http2authorityAddr(req.URL.Scheme, req.URL.Host) 6863 for retry := 0; ; retry++ { 6864 cc, err := t.connPool().GetClientConn(req, addr) 6865 if err != nil { 6866 t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err) 6867 return nil, err 6868 } 6869 http2traceGotConn(req, cc) 6870 res, err := cc.RoundTrip(req) 6871 if err != nil && retry <= 6 { 6872 afterBodyWrite := false 6873 if e, ok := err.(http2afterReqBodyWriteError); ok { 6874 err = e 6875 afterBodyWrite = true 6876 } 6877 if req, err = http2shouldRetryRequest(req, err, afterBodyWrite); err == nil { 6878 // After the first retry, do exponential backoff with 10% jitter. 6879 if retry == 0 { 6880 continue 6881 } 6882 backoff := float64(uint(1) << (uint(retry) - 1)) 6883 backoff += backoff * (0.1 * mathrand.Float64()) 6884 select { 6885 case <-time.After(time.Second * time.Duration(backoff)): 6886 continue 6887 case <-http2reqContext(req).Done(): 6888 return nil, http2reqContext(req).Err() 6889 } 6890 } 6891 } 6892 if err != nil { 6893 t.vlogf("RoundTrip failure: %v", err) 6894 return nil, err 6895 } 6896 return res, nil 6897 } 6898 } 6899 6900 // CloseIdleConnections closes any connections which were previously 6901 // connected from previous requests but are now sitting idle. 6902 // It does not interrupt any connections currently in use. 6903 func (t *http2Transport) CloseIdleConnections() { 6904 if cp, ok := t.connPool().(http2clientConnPoolIdleCloser); ok { 6905 cp.closeIdleConnections() 6906 } 6907 } 6908 6909 var ( 6910 http2errClientConnClosed = errors.New("http2: client conn is closed") 6911 http2errClientConnUnusable = errors.New("http2: client conn not usable") 6912 http2errClientConnGotGoAway = errors.New("http2: Transport received Server's graceful shutdown GOAWAY") 6913 ) 6914 6915 // afterReqBodyWriteError is a wrapper around errors returned by ClientConn.RoundTrip. 6916 // It is used to signal that err happened after part of Request.Body was sent to the server. 6917 type http2afterReqBodyWriteError struct { 6918 err error 6919 } 6920 6921 func (e http2afterReqBodyWriteError) Error() string { 6922 return e.err.Error() + "; some request body already written" 6923 } 6924 6925 // shouldRetryRequest is called by RoundTrip when a request fails to get 6926 // response headers. It is always called with a non-nil error. 6927 // It returns either a request to retry (either the same request, or a 6928 // modified clone), or an error if the request can't be replayed. 6929 func http2shouldRetryRequest(req *Request, err error, afterBodyWrite bool) (*Request, error) { 6930 if !http2canRetryError(err) { 6931 return nil, err 6932 } 6933 if !afterBodyWrite { 6934 return req, nil 6935 } 6936 // If the Body is nil (or http.NoBody), it's safe to reuse 6937 // this request and its Body. 6938 if req.Body == nil || http2reqBodyIsNoBody(req.Body) { 6939 return req, nil 6940 } 6941 // Otherwise we depend on the Request having its GetBody 6942 // func defined. 6943 getBody := http2reqGetBody(req) // Go 1.8: getBody = req.GetBody 6944 if getBody == nil { 6945 return nil, fmt.Errorf("http2: Transport: cannot retry err [%v] after Request.Body was written; define Request.GetBody to avoid this error", err) 6946 } 6947 body, err := getBody() 6948 if err != nil { 6949 return nil, err 6950 } 6951 newReq := *req 6952 newReq.Body = body 6953 return &newReq, nil 6954 } 6955 6956 func http2canRetryError(err error) bool { 6957 if err == http2errClientConnUnusable || err == http2errClientConnGotGoAway { 6958 return true 6959 } 6960 if se, ok := err.(http2StreamError); ok { 6961 return se.Code == http2ErrCodeRefusedStream 6962 } 6963 return false 6964 } 6965 6966 func (t *http2Transport) dialClientConn(addr string, singleUse bool) (*http2ClientConn, error) { 6967 host, _, err := net.SplitHostPort(addr) 6968 if err != nil { 6969 return nil, err 6970 } 6971 tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host)) 6972 if err != nil { 6973 return nil, err 6974 } 6975 return t.newClientConn(tconn, singleUse) 6976 } 6977 6978 func (t *http2Transport) newTLSConfig(host string) *tls.Config { 6979 cfg := new(tls.Config) 6980 if t.TLSClientConfig != nil { 6981 *cfg = *http2cloneTLSConfig(t.TLSClientConfig) 6982 } 6983 if !http2strSliceContains(cfg.NextProtos, http2NextProtoTLS) { 6984 cfg.NextProtos = append([]string{http2NextProtoTLS}, cfg.NextProtos...) 6985 } 6986 if cfg.ServerName == "" { 6987 cfg.ServerName = host 6988 } 6989 return cfg 6990 } 6991 6992 func (t *http2Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) { 6993 if t.DialTLS != nil { 6994 return t.DialTLS 6995 } 6996 return t.dialTLSDefault 6997 } 6998 6999 func (t *http2Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) { 7000 cn, err := tls.Dial(network, addr, cfg) 7001 if err != nil { 7002 return nil, err 7003 } 7004 if err := cn.Handshake(); err != nil { 7005 return nil, err 7006 } 7007 if !cfg.InsecureSkipVerify { 7008 if err := cn.VerifyHostname(cfg.ServerName); err != nil { 7009 return nil, err 7010 } 7011 } 7012 state := cn.ConnectionState() 7013 if p := state.NegotiatedProtocol; p != http2NextProtoTLS { 7014 return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, http2NextProtoTLS) 7015 } 7016 if !state.NegotiatedProtocolIsMutual { 7017 return nil, errors.New("http2: could not negotiate protocol mutually") 7018 } 7019 return cn, nil 7020 } 7021 7022 // disableKeepAlives reports whether connections should be closed as 7023 // soon as possible after handling the first request. 7024 func (t *http2Transport) disableKeepAlives() bool { 7025 return t.t1 != nil && t.t1.DisableKeepAlives 7026 } 7027 7028 func (t *http2Transport) expectContinueTimeout() time.Duration { 7029 if t.t1 == nil { 7030 return 0 7031 } 7032 return http2transportExpectContinueTimeout(t.t1) 7033 } 7034 7035 func (t *http2Transport) NewClientConn(c net.Conn) (*http2ClientConn, error) { 7036 return t.newClientConn(c, false) 7037 } 7038 7039 func (t *http2Transport) newClientConn(c net.Conn, singleUse bool) (*http2ClientConn, error) { 7040 cc := &http2ClientConn{ 7041 t: t, 7042 tconn: c, 7043 readerDone: make(chan struct{}), 7044 nextStreamID: 1, 7045 maxFrameSize: 16 << 10, // spec default 7046 initialWindowSize: 65535, // spec default 7047 maxConcurrentStreams: 1000, // "infinite", per spec. 1000 seems good enough. 7048 streams: make(map[uint32]*http2clientStream), 7049 singleUse: singleUse, 7050 wantSettingsAck: true, 7051 pings: make(map[[8]byte]chan struct{}), 7052 } 7053 if d := t.idleConnTimeout(); d != 0 { 7054 cc.idleTimeout = d 7055 cc.idleTimer = time.AfterFunc(d, cc.onIdleTimeout) 7056 } 7057 if http2VerboseLogs { 7058 t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr()) 7059 } 7060 7061 cc.cond = sync.NewCond(&cc.mu) 7062 cc.flow.add(int32(http2initialWindowSize)) 7063 7064 // TODO: adjust this writer size to account for frame size + 7065 // MTU + crypto/tls record padding. 7066 cc.bw = bufio.NewWriter(http2stickyErrWriter{c, &cc.werr}) 7067 cc.br = bufio.NewReader(c) 7068 cc.fr = http2NewFramer(cc.bw, cc.br) 7069 cc.fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil) 7070 cc.fr.MaxHeaderListSize = t.maxHeaderListSize() 7071 7072 // TODO: SetMaxDynamicTableSize, SetMaxDynamicTableSizeLimit on 7073 // henc in response to SETTINGS frames? 7074 cc.henc = hpack.NewEncoder(&cc.hbuf) 7075 7076 if cs, ok := c.(http2connectionStater); ok { 7077 state := cs.ConnectionState() 7078 cc.tlsState = &state 7079 } 7080 7081 initialSettings := []http2Setting{ 7082 {ID: http2SettingEnablePush, Val: 0}, 7083 {ID: http2SettingInitialWindowSize, Val: http2transportDefaultStreamFlow}, 7084 } 7085 if max := t.maxHeaderListSize(); max != 0 { 7086 initialSettings = append(initialSettings, http2Setting{ID: http2SettingMaxHeaderListSize, Val: max}) 7087 } 7088 7089 cc.bw.Write(http2clientPreface) 7090 cc.fr.WriteSettings(initialSettings...) 7091 cc.fr.WriteWindowUpdate(0, http2transportDefaultConnFlow) 7092 cc.inflow.add(http2transportDefaultConnFlow + http2initialWindowSize) 7093 cc.bw.Flush() 7094 if cc.werr != nil { 7095 return nil, cc.werr 7096 } 7097 7098 go cc.readLoop() 7099 return cc, nil 7100 } 7101 7102 func (cc *http2ClientConn) setGoAway(f *http2GoAwayFrame) { 7103 cc.mu.Lock() 7104 defer cc.mu.Unlock() 7105 7106 old := cc.goAway 7107 cc.goAway = f 7108 7109 // Merge the previous and current GoAway error frames. 7110 if cc.goAwayDebug == "" { 7111 cc.goAwayDebug = string(f.DebugData()) 7112 } 7113 if old != nil && old.ErrCode != http2ErrCodeNo { 7114 cc.goAway.ErrCode = old.ErrCode 7115 } 7116 last := f.LastStreamID 7117 for streamID, cs := range cc.streams { 7118 if streamID > last { 7119 select { 7120 case cs.resc <- http2resAndError{err: http2errClientConnGotGoAway}: 7121 default: 7122 } 7123 } 7124 } 7125 } 7126 7127 // CanTakeNewRequest reports whether the connection can take a new request, 7128 // meaning it has not been closed or received or sent a GOAWAY. 7129 func (cc *http2ClientConn) CanTakeNewRequest() bool { 7130 cc.mu.Lock() 7131 defer cc.mu.Unlock() 7132 return cc.canTakeNewRequestLocked() 7133 } 7134 7135 func (cc *http2ClientConn) canTakeNewRequestLocked() bool { 7136 if cc.singleUse && cc.nextStreamID > 1 { 7137 return false 7138 } 7139 return cc.goAway == nil && !cc.closed && 7140 int64(cc.nextStreamID)+int64(cc.pendingRequests) < math.MaxInt32 7141 } 7142 7143 // onIdleTimeout is called from a time.AfterFunc goroutine. It will 7144 // only be called when we're idle, but because we're coming from a new 7145 // goroutine, there could be a new request coming in at the same time, 7146 // so this simply calls the synchronized closeIfIdle to shut down this 7147 // connection. The timer could just call closeIfIdle, but this is more 7148 // clear. 7149 func (cc *http2ClientConn) onIdleTimeout() { 7150 cc.closeIfIdle() 7151 } 7152 7153 func (cc *http2ClientConn) closeIfIdle() { 7154 cc.mu.Lock() 7155 if len(cc.streams) > 0 { 7156 cc.mu.Unlock() 7157 return 7158 } 7159 cc.closed = true 7160 nextID := cc.nextStreamID 7161 // TODO: do clients send GOAWAY too? maybe? Just Close: 7162 cc.mu.Unlock() 7163 7164 if http2VerboseLogs { 7165 cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2) 7166 } 7167 cc.tconn.Close() 7168 } 7169 7170 const http2maxAllocFrameSize = 512 << 10 7171 7172 // frameBuffer returns a scratch buffer suitable for writing DATA frames. 7173 // They're capped at the min of the peer's max frame size or 512KB 7174 // (kinda arbitrarily), but definitely capped so we don't allocate 4GB 7175 // bufers. 7176 func (cc *http2ClientConn) frameScratchBuffer() []byte { 7177 cc.mu.Lock() 7178 size := cc.maxFrameSize 7179 if size > http2maxAllocFrameSize { 7180 size = http2maxAllocFrameSize 7181 } 7182 for i, buf := range cc.freeBuf { 7183 if len(buf) >= int(size) { 7184 cc.freeBuf[i] = nil 7185 cc.mu.Unlock() 7186 return buf[:size] 7187 } 7188 } 7189 cc.mu.Unlock() 7190 return make([]byte, size) 7191 } 7192 7193 func (cc *http2ClientConn) putFrameScratchBuffer(buf []byte) { 7194 cc.mu.Lock() 7195 defer cc.mu.Unlock() 7196 const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate. 7197 if len(cc.freeBuf) < maxBufs { 7198 cc.freeBuf = append(cc.freeBuf, buf) 7199 return 7200 } 7201 for i, old := range cc.freeBuf { 7202 if old == nil { 7203 cc.freeBuf[i] = buf 7204 return 7205 } 7206 } 7207 // forget about it. 7208 } 7209 7210 // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not 7211 // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests. 7212 var http2errRequestCanceled = errors.New("net/http: request canceled") 7213 7214 func http2commaSeparatedTrailers(req *Request) (string, error) { 7215 keys := make([]string, 0, len(req.Trailer)) 7216 for k := range req.Trailer { 7217 k = CanonicalHeaderKey(k) 7218 switch k { 7219 case "Transfer-Encoding", "Trailer", "Content-Length": 7220 return "", &http2badStringError{"invalid Trailer key", k} 7221 } 7222 keys = append(keys, k) 7223 } 7224 if len(keys) > 0 { 7225 sort.Strings(keys) 7226 return strings.Join(keys, ","), nil 7227 } 7228 return "", nil 7229 } 7230 7231 func (cc *http2ClientConn) responseHeaderTimeout() time.Duration { 7232 if cc.t.t1 != nil { 7233 return cc.t.t1.ResponseHeaderTimeout 7234 } 7235 // No way to do this (yet?) with just an http2.Transport. Probably 7236 // no need. Request.Cancel this is the new way. We only need to support 7237 // this for compatibility with the old http.Transport fields when 7238 // we're doing transparent http2. 7239 return 0 7240 } 7241 7242 // checkConnHeaders checks whether req has any invalid connection-level headers. 7243 // per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields. 7244 // Certain headers are special-cased as okay but not transmitted later. 7245 func http2checkConnHeaders(req *Request) error { 7246 if v := req.Header.Get("Upgrade"); v != "" { 7247 return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"]) 7248 } 7249 if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") { 7250 return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv) 7251 } 7252 if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "close" && vv[0] != "keep-alive") { 7253 return fmt.Errorf("http2: invalid Connection request header: %q", vv) 7254 } 7255 return nil 7256 } 7257 7258 // actualContentLength returns a sanitized version of 7259 // req.ContentLength, where 0 actually means zero (not unknown) and -1 7260 // means unknown. 7261 func http2actualContentLength(req *Request) int64 { 7262 if req.Body == nil || http2reqBodyIsNoBody(req.Body) { 7263 return 0 7264 } 7265 if req.ContentLength != 0 { 7266 return req.ContentLength 7267 } 7268 return -1 7269 } 7270 7271 func (cc *http2ClientConn) RoundTrip(req *Request) (*Response, error) { 7272 if err := http2checkConnHeaders(req); err != nil { 7273 return nil, err 7274 } 7275 if cc.idleTimer != nil { 7276 cc.idleTimer.Stop() 7277 } 7278 7279 trailers, err := http2commaSeparatedTrailers(req) 7280 if err != nil { 7281 return nil, err 7282 } 7283 hasTrailers := trailers != "" 7284 7285 cc.mu.Lock() 7286 if err := cc.awaitOpenSlotForRequest(req); err != nil { 7287 cc.mu.Unlock() 7288 return nil, err 7289 } 7290 7291 body := req.Body 7292 contentLen := http2actualContentLength(req) 7293 hasBody := contentLen != 0 7294 7295 // TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere? 7296 var requestedGzip bool 7297 if !cc.t.disableCompression() && 7298 req.Header.Get("Accept-Encoding") == "" && 7299 req.Header.Get("Range") == "" && 7300 req.Method != "HEAD" { 7301 // Request gzip only, not deflate. Deflate is ambiguous and 7302 // not as universally supported anyway. 7303 // See: http://www.gzip.org/zlib/zlib_faq.html#faq38 7304 // 7305 // Note that we don't request this for HEAD requests, 7306 // due to a bug in nginx: 7307 // http://trac.nginx.org/nginx/ticket/358 7308 // https://golang.org/issue/5522 7309 // 7310 // We don't request gzip if the request is for a range, since 7311 // auto-decoding a portion of a gzipped document will just fail 7312 // anyway. See https://golang.org/issue/8923 7313 requestedGzip = true 7314 } 7315 7316 // we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is 7317 // sent by writeRequestBody below, along with any Trailers, 7318 // again in form HEADERS{1}, CONTINUATION{0,}) 7319 hdrs, err := cc.encodeHeaders(req, requestedGzip, trailers, contentLen) 7320 if err != nil { 7321 cc.mu.Unlock() 7322 return nil, err 7323 } 7324 7325 cs := cc.newStream() 7326 cs.req = req 7327 cs.trace = http2requestTrace(req) 7328 cs.requestedGzip = requestedGzip 7329 bodyWriter := cc.t.getBodyWriterState(cs, body) 7330 cs.on100 = bodyWriter.on100 7331 7332 cc.wmu.Lock() 7333 endStream := !hasBody && !hasTrailers 7334 werr := cc.writeHeaders(cs.ID, endStream, hdrs) 7335 cc.wmu.Unlock() 7336 http2traceWroteHeaders(cs.trace) 7337 cc.mu.Unlock() 7338 7339 if werr != nil { 7340 if hasBody { 7341 req.Body.Close() // per RoundTripper contract 7342 bodyWriter.cancel() 7343 } 7344 cc.forgetStreamID(cs.ID) 7345 // Don't bother sending a RST_STREAM (our write already failed; 7346 // no need to keep writing) 7347 http2traceWroteRequest(cs.trace, werr) 7348 return nil, werr 7349 } 7350 7351 var respHeaderTimer <-chan time.Time 7352 if hasBody { 7353 bodyWriter.scheduleBodyWrite() 7354 } else { 7355 http2traceWroteRequest(cs.trace, nil) 7356 if d := cc.responseHeaderTimeout(); d != 0 { 7357 timer := time.NewTimer(d) 7358 defer timer.Stop() 7359 respHeaderTimer = timer.C 7360 } 7361 } 7362 7363 readLoopResCh := cs.resc 7364 bodyWritten := false 7365 ctx := http2reqContext(req) 7366 7367 handleReadLoopResponse := func(re http2resAndError) (*Response, error) { 7368 res := re.res 7369 if re.err != nil || res.StatusCode > 299 { 7370 // On error or status code 3xx, 4xx, 5xx, etc abort any 7371 // ongoing write, assuming that the server doesn't care 7372 // about our request body. If the server replied with 1xx or 7373 // 2xx, however, then assume the server DOES potentially 7374 // want our body (e.g. full-duplex streaming: 7375 // golang.org/issue/13444). If it turns out the server 7376 // doesn't, they'll RST_STREAM us soon enough. This is a 7377 // heuristic to avoid adding knobs to Transport. Hopefully 7378 // we can keep it. 7379 bodyWriter.cancel() 7380 cs.abortRequestBodyWrite(http2errStopReqBodyWrite) 7381 } 7382 if re.err != nil { 7383 cc.mu.Lock() 7384 afterBodyWrite := cs.startedWrite 7385 cc.mu.Unlock() 7386 cc.forgetStreamID(cs.ID) 7387 if afterBodyWrite { 7388 return nil, http2afterReqBodyWriteError{re.err} 7389 } 7390 return nil, re.err 7391 } 7392 res.Request = req 7393 res.TLS = cc.tlsState 7394 return res, nil 7395 } 7396 7397 for { 7398 select { 7399 case re := <-readLoopResCh: 7400 return handleReadLoopResponse(re) 7401 case <-respHeaderTimer: 7402 if !hasBody || bodyWritten { 7403 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 7404 } else { 7405 bodyWriter.cancel() 7406 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 7407 } 7408 cc.forgetStreamID(cs.ID) 7409 return nil, http2errTimeout 7410 case <-ctx.Done(): 7411 if !hasBody || bodyWritten { 7412 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 7413 } else { 7414 bodyWriter.cancel() 7415 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 7416 } 7417 cc.forgetStreamID(cs.ID) 7418 return nil, ctx.Err() 7419 case <-req.Cancel: 7420 if !hasBody || bodyWritten { 7421 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 7422 } else { 7423 bodyWriter.cancel() 7424 cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel) 7425 } 7426 cc.forgetStreamID(cs.ID) 7427 return nil, http2errRequestCanceled 7428 case <-cs.peerReset: 7429 // processResetStream already removed the 7430 // stream from the streams map; no need for 7431 // forgetStreamID. 7432 return nil, cs.resetErr 7433 case err := <-bodyWriter.resc: 7434 // Prefer the read loop's response, if available. Issue 16102. 7435 select { 7436 case re := <-readLoopResCh: 7437 return handleReadLoopResponse(re) 7438 default: 7439 } 7440 if err != nil { 7441 return nil, err 7442 } 7443 bodyWritten = true 7444 if d := cc.responseHeaderTimeout(); d != 0 { 7445 timer := time.NewTimer(d) 7446 defer timer.Stop() 7447 respHeaderTimer = timer.C 7448 } 7449 } 7450 } 7451 } 7452 7453 // awaitOpenSlotForRequest waits until len(streams) < maxConcurrentStreams. 7454 // Must hold cc.mu. 7455 func (cc *http2ClientConn) awaitOpenSlotForRequest(req *Request) error { 7456 var waitingForConn chan struct{} 7457 var waitingForConnErr error // guarded by cc.mu 7458 for { 7459 cc.lastActive = time.Now() 7460 if cc.closed || !cc.canTakeNewRequestLocked() { 7461 return http2errClientConnUnusable 7462 } 7463 if int64(len(cc.streams))+1 <= int64(cc.maxConcurrentStreams) { 7464 if waitingForConn != nil { 7465 close(waitingForConn) 7466 } 7467 return nil 7468 } 7469 // Unfortunately, we cannot wait on a condition variable and channel at 7470 // the same time, so instead, we spin up a goroutine to check if the 7471 // request is canceled while we wait for a slot to open in the connection. 7472 if waitingForConn == nil { 7473 waitingForConn = make(chan struct{}) 7474 go func() { 7475 if err := http2awaitRequestCancel(req, waitingForConn); err != nil { 7476 cc.mu.Lock() 7477 waitingForConnErr = err 7478 cc.cond.Broadcast() 7479 cc.mu.Unlock() 7480 } 7481 }() 7482 } 7483 cc.pendingRequests++ 7484 cc.cond.Wait() 7485 cc.pendingRequests-- 7486 if waitingForConnErr != nil { 7487 return waitingForConnErr 7488 } 7489 } 7490 } 7491 7492 // requires cc.wmu be held 7493 func (cc *http2ClientConn) writeHeaders(streamID uint32, endStream bool, hdrs []byte) error { 7494 first := true // first frame written (HEADERS is first, then CONTINUATION) 7495 frameSize := int(cc.maxFrameSize) 7496 for len(hdrs) > 0 && cc.werr == nil { 7497 chunk := hdrs 7498 if len(chunk) > frameSize { 7499 chunk = chunk[:frameSize] 7500 } 7501 hdrs = hdrs[len(chunk):] 7502 endHeaders := len(hdrs) == 0 7503 if first { 7504 cc.fr.WriteHeaders(http2HeadersFrameParam{ 7505 StreamID: streamID, 7506 BlockFragment: chunk, 7507 EndStream: endStream, 7508 EndHeaders: endHeaders, 7509 }) 7510 first = false 7511 } else { 7512 cc.fr.WriteContinuation(streamID, endHeaders, chunk) 7513 } 7514 } 7515 // TODO(bradfitz): this Flush could potentially block (as 7516 // could the WriteHeaders call(s) above), which means they 7517 // wouldn't respond to Request.Cancel being readable. That's 7518 // rare, but this should probably be in a goroutine. 7519 cc.bw.Flush() 7520 return cc.werr 7521 } 7522 7523 // internal error values; they don't escape to callers 7524 var ( 7525 // abort request body write; don't send cancel 7526 http2errStopReqBodyWrite = errors.New("http2: aborting request body write") 7527 7528 // abort request body write, but send stream reset of cancel. 7529 http2errStopReqBodyWriteAndCancel = errors.New("http2: canceling request") 7530 ) 7531 7532 func (cs *http2clientStream) writeRequestBody(body io.Reader, bodyCloser io.Closer) (err error) { 7533 cc := cs.cc 7534 sentEnd := false // whether we sent the final DATA frame w/ END_STREAM 7535 buf := cc.frameScratchBuffer() 7536 defer cc.putFrameScratchBuffer(buf) 7537 7538 defer func() { 7539 http2traceWroteRequest(cs.trace, err) 7540 // TODO: write h12Compare test showing whether 7541 // Request.Body is closed by the Transport, 7542 // and in multiple cases: server replies <=299 and >299 7543 // while still writing request body 7544 cerr := bodyCloser.Close() 7545 if err == nil { 7546 err = cerr 7547 } 7548 }() 7549 7550 req := cs.req 7551 hasTrailers := req.Trailer != nil 7552 7553 var sawEOF bool 7554 for !sawEOF { 7555 n, err := body.Read(buf) 7556 if err == io.EOF { 7557 sawEOF = true 7558 err = nil 7559 } else if err != nil { 7560 return err 7561 } 7562 7563 remain := buf[:n] 7564 for len(remain) > 0 && err == nil { 7565 var allowed int32 7566 allowed, err = cs.awaitFlowControl(len(remain)) 7567 switch { 7568 case err == http2errStopReqBodyWrite: 7569 return err 7570 case err == http2errStopReqBodyWriteAndCancel: 7571 cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil) 7572 return err 7573 case err != nil: 7574 return err 7575 } 7576 cc.wmu.Lock() 7577 data := remain[:allowed] 7578 remain = remain[allowed:] 7579 sentEnd = sawEOF && len(remain) == 0 && !hasTrailers 7580 err = cc.fr.WriteData(cs.ID, sentEnd, data) 7581 if err == nil { 7582 // TODO(bradfitz): this flush is for latency, not bandwidth. 7583 // Most requests won't need this. Make this opt-in or 7584 // opt-out? Use some heuristic on the body type? Nagel-like 7585 // timers? Based on 'n'? Only last chunk of this for loop, 7586 // unless flow control tokens are low? For now, always. 7587 // If we change this, see comment below. 7588 err = cc.bw.Flush() 7589 } 7590 cc.wmu.Unlock() 7591 } 7592 if err != nil { 7593 return err 7594 } 7595 } 7596 7597 if sentEnd { 7598 // Already sent END_STREAM (which implies we have no 7599 // trailers) and flushed, because currently all 7600 // WriteData frames above get a flush. So we're done. 7601 return nil 7602 } 7603 7604 var trls []byte 7605 if hasTrailers { 7606 cc.mu.Lock() 7607 defer cc.mu.Unlock() 7608 trls = cc.encodeTrailers(req) 7609 } 7610 7611 cc.wmu.Lock() 7612 defer cc.wmu.Unlock() 7613 7614 // Two ways to send END_STREAM: either with trailers, or 7615 // with an empty DATA frame. 7616 if len(trls) > 0 { 7617 err = cc.writeHeaders(cs.ID, true, trls) 7618 } else { 7619 err = cc.fr.WriteData(cs.ID, true, nil) 7620 } 7621 if ferr := cc.bw.Flush(); ferr != nil && err == nil { 7622 err = ferr 7623 } 7624 return err 7625 } 7626 7627 // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow 7628 // control tokens from the server. 7629 // It returns either the non-zero number of tokens taken or an error 7630 // if the stream is dead. 7631 func (cs *http2clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) { 7632 cc := cs.cc 7633 cc.mu.Lock() 7634 defer cc.mu.Unlock() 7635 for { 7636 if cc.closed { 7637 return 0, http2errClientConnClosed 7638 } 7639 if cs.stopReqBody != nil { 7640 return 0, cs.stopReqBody 7641 } 7642 if err := cs.checkResetOrDone(); err != nil { 7643 return 0, err 7644 } 7645 if a := cs.flow.available(); a > 0 { 7646 take := a 7647 if int(take) > maxBytes { 7648 7649 take = int32(maxBytes) // can't truncate int; take is int32 7650 } 7651 if take > int32(cc.maxFrameSize) { 7652 take = int32(cc.maxFrameSize) 7653 } 7654 cs.flow.take(take) 7655 return take, nil 7656 } 7657 cc.cond.Wait() 7658 } 7659 } 7660 7661 type http2badStringError struct { 7662 what string 7663 str string 7664 } 7665 7666 func (e *http2badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 7667 7668 // requires cc.mu be held. 7669 func (cc *http2ClientConn) encodeHeaders(req *Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) { 7670 cc.hbuf.Reset() 7671 7672 host := req.Host 7673 if host == "" { 7674 host = req.URL.Host 7675 } 7676 host, err := httplex.PunycodeHostPort(host) 7677 if err != nil { 7678 return nil, err 7679 } 7680 7681 var path string 7682 if req.Method != "CONNECT" { 7683 path = req.URL.RequestURI() 7684 if !http2validPseudoPath(path) { 7685 orig := path 7686 path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host) 7687 if !http2validPseudoPath(path) { 7688 if req.URL.Opaque != "" { 7689 return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque) 7690 } else { 7691 return nil, fmt.Errorf("invalid request :path %q", orig) 7692 } 7693 } 7694 } 7695 } 7696 7697 // Check for any invalid headers and return an error before we 7698 // potentially pollute our hpack state. (We want to be able to 7699 // continue to reuse the hpack encoder for future requests) 7700 for k, vv := range req.Header { 7701 if !httplex.ValidHeaderFieldName(k) { 7702 return nil, fmt.Errorf("invalid HTTP header name %q", k) 7703 } 7704 for _, v := range vv { 7705 if !httplex.ValidHeaderFieldValue(v) { 7706 return nil, fmt.Errorf("invalid HTTP header value %q for header %q", v, k) 7707 } 7708 } 7709 } 7710 7711 // 8.1.2.3 Request Pseudo-Header Fields 7712 // The :path pseudo-header field includes the path and query parts of the 7713 // target URI (the path-absolute production and optionally a '?' character 7714 // followed by the query production (see Sections 3.3 and 3.4 of 7715 // [RFC3986]). 7716 cc.writeHeader(":authority", host) 7717 cc.writeHeader(":method", req.Method) 7718 if req.Method != "CONNECT" { 7719 cc.writeHeader(":path", path) 7720 cc.writeHeader(":scheme", req.URL.Scheme) 7721 } 7722 if trailers != "" { 7723 cc.writeHeader("trailer", trailers) 7724 } 7725 7726 var didUA bool 7727 for k, vv := range req.Header { 7728 lowKey := strings.ToLower(k) 7729 switch lowKey { 7730 case "host", "content-length": 7731 // Host is :authority, already sent. 7732 // Content-Length is automatic, set below. 7733 continue 7734 case "connection", "proxy-connection", "transfer-encoding", "upgrade", "keep-alive": 7735 // Per 8.1.2.2 Connection-Specific Header 7736 // Fields, don't send connection-specific 7737 // fields. We have already checked if any 7738 // are error-worthy so just ignore the rest. 7739 continue 7740 case "user-agent": 7741 // Match Go's http1 behavior: at most one 7742 // User-Agent. If set to nil or empty string, 7743 // then omit it. Otherwise if not mentioned, 7744 // include the default (below). 7745 didUA = true 7746 if len(vv) < 1 { 7747 continue 7748 } 7749 vv = vv[:1] 7750 if vv[0] == "" { 7751 continue 7752 } 7753 } 7754 for _, v := range vv { 7755 cc.writeHeader(lowKey, v) 7756 } 7757 } 7758 if http2shouldSendReqContentLength(req.Method, contentLength) { 7759 cc.writeHeader("content-length", strconv.FormatInt(contentLength, 10)) 7760 } 7761 if addGzipHeader { 7762 cc.writeHeader("accept-encoding", "gzip") 7763 } 7764 if !didUA { 7765 cc.writeHeader("user-agent", http2defaultUserAgent) 7766 } 7767 return cc.hbuf.Bytes(), nil 7768 } 7769 7770 // shouldSendReqContentLength reports whether the http2.Transport should send 7771 // a "content-length" request header. This logic is basically a copy of the net/http 7772 // transferWriter.shouldSendContentLength. 7773 // The contentLength is the corrected contentLength (so 0 means actually 0, not unknown). 7774 // -1 means unknown. 7775 func http2shouldSendReqContentLength(method string, contentLength int64) bool { 7776 if contentLength > 0 { 7777 return true 7778 } 7779 if contentLength < 0 { 7780 return false 7781 } 7782 // For zero bodies, whether we send a content-length depends on the method. 7783 // It also kinda doesn't matter for http2 either way, with END_STREAM. 7784 switch method { 7785 case "POST", "PUT", "PATCH": 7786 return true 7787 default: 7788 return false 7789 } 7790 } 7791 7792 // requires cc.mu be held. 7793 func (cc *http2ClientConn) encodeTrailers(req *Request) []byte { 7794 cc.hbuf.Reset() 7795 for k, vv := range req.Trailer { 7796 // Transfer-Encoding, etc.. have already been filter at the 7797 // start of RoundTrip 7798 lowKey := strings.ToLower(k) 7799 for _, v := range vv { 7800 cc.writeHeader(lowKey, v) 7801 } 7802 } 7803 return cc.hbuf.Bytes() 7804 } 7805 7806 func (cc *http2ClientConn) writeHeader(name, value string) { 7807 if http2VerboseLogs { 7808 log.Printf("http2: Transport encoding header %q = %q", name, value) 7809 } 7810 cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value}) 7811 } 7812 7813 type http2resAndError struct { 7814 res *Response 7815 err error 7816 } 7817 7818 // requires cc.mu be held. 7819 func (cc *http2ClientConn) newStream() *http2clientStream { 7820 cs := &http2clientStream{ 7821 cc: cc, 7822 ID: cc.nextStreamID, 7823 resc: make(chan http2resAndError, 1), 7824 peerReset: make(chan struct{}), 7825 done: make(chan struct{}), 7826 } 7827 cs.flow.add(int32(cc.initialWindowSize)) 7828 cs.flow.setConnFlow(&cc.flow) 7829 cs.inflow.add(http2transportDefaultStreamFlow) 7830 cs.inflow.setConnFlow(&cc.inflow) 7831 cc.nextStreamID += 2 7832 cc.streams[cs.ID] = cs 7833 return cs 7834 } 7835 7836 func (cc *http2ClientConn) forgetStreamID(id uint32) { 7837 cc.streamByID(id, true) 7838 } 7839 7840 func (cc *http2ClientConn) streamByID(id uint32, andRemove bool) *http2clientStream { 7841 cc.mu.Lock() 7842 defer cc.mu.Unlock() 7843 cs := cc.streams[id] 7844 if andRemove && cs != nil && !cc.closed { 7845 cc.lastActive = time.Now() 7846 delete(cc.streams, id) 7847 if len(cc.streams) == 0 && cc.idleTimer != nil { 7848 cc.idleTimer.Reset(cc.idleTimeout) 7849 } 7850 close(cs.done) 7851 // Wake up checkResetOrDone via clientStream.awaitFlowControl and 7852 // wake up RoundTrip if there is a pending request. 7853 cc.cond.Broadcast() 7854 } 7855 return cs 7856 } 7857 7858 // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop. 7859 type http2clientConnReadLoop struct { 7860 cc *http2ClientConn 7861 activeRes map[uint32]*http2clientStream // keyed by streamID 7862 closeWhenIdle bool 7863 } 7864 7865 // readLoop runs in its own goroutine and reads and dispatches frames. 7866 func (cc *http2ClientConn) readLoop() { 7867 rl := &http2clientConnReadLoop{ 7868 cc: cc, 7869 activeRes: make(map[uint32]*http2clientStream), 7870 } 7871 7872 defer rl.cleanup() 7873 cc.readerErr = rl.run() 7874 if ce, ok := cc.readerErr.(http2ConnectionError); ok { 7875 cc.wmu.Lock() 7876 cc.fr.WriteGoAway(0, http2ErrCode(ce), nil) 7877 cc.wmu.Unlock() 7878 } 7879 } 7880 7881 // GoAwayError is returned by the Transport when the server closes the 7882 // TCP connection after sending a GOAWAY frame. 7883 type http2GoAwayError struct { 7884 LastStreamID uint32 7885 ErrCode http2ErrCode 7886 DebugData string 7887 } 7888 7889 func (e http2GoAwayError) Error() string { 7890 return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q", 7891 e.LastStreamID, e.ErrCode, e.DebugData) 7892 } 7893 7894 func http2isEOFOrNetReadError(err error) bool { 7895 if err == io.EOF { 7896 return true 7897 } 7898 ne, ok := err.(*net.OpError) 7899 return ok && ne.Op == "read" 7900 } 7901 7902 func (rl *http2clientConnReadLoop) cleanup() { 7903 cc := rl.cc 7904 defer cc.tconn.Close() 7905 defer cc.t.connPool().MarkDead(cc) 7906 defer close(cc.readerDone) 7907 7908 if cc.idleTimer != nil { 7909 cc.idleTimer.Stop() 7910 } 7911 7912 // Close any response bodies if the server closes prematurely. 7913 // TODO: also do this if we've written the headers but not 7914 // gotten a response yet. 7915 err := cc.readerErr 7916 cc.mu.Lock() 7917 if cc.goAway != nil && http2isEOFOrNetReadError(err) { 7918 err = http2GoAwayError{ 7919 LastStreamID: cc.goAway.LastStreamID, 7920 ErrCode: cc.goAway.ErrCode, 7921 DebugData: cc.goAwayDebug, 7922 } 7923 } else if err == io.EOF { 7924 err = io.ErrUnexpectedEOF 7925 } 7926 for _, cs := range rl.activeRes { 7927 cs.bufPipe.CloseWithError(err) 7928 } 7929 for _, cs := range cc.streams { 7930 select { 7931 case cs.resc <- http2resAndError{err: err}: 7932 default: 7933 } 7934 close(cs.done) 7935 } 7936 cc.closed = true 7937 cc.cond.Broadcast() 7938 cc.mu.Unlock() 7939 } 7940 7941 func (rl *http2clientConnReadLoop) run() error { 7942 cc := rl.cc 7943 rl.closeWhenIdle = cc.t.disableKeepAlives() || cc.singleUse 7944 gotReply := false // ever saw a HEADERS reply 7945 gotSettings := false 7946 for { 7947 f, err := cc.fr.ReadFrame() 7948 if err != nil { 7949 cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err) 7950 } 7951 if se, ok := err.(http2StreamError); ok { 7952 if cs := cc.streamByID(se.StreamID, false); cs != nil { 7953 cs.cc.writeStreamReset(cs.ID, se.Code, err) 7954 cs.cc.forgetStreamID(cs.ID) 7955 if se.Cause == nil { 7956 se.Cause = cc.fr.errDetail 7957 } 7958 rl.endStreamError(cs, se) 7959 } 7960 continue 7961 } else if err != nil { 7962 return err 7963 } 7964 if http2VerboseLogs { 7965 cc.vlogf("http2: Transport received %s", http2summarizeFrame(f)) 7966 } 7967 if !gotSettings { 7968 if _, ok := f.(*http2SettingsFrame); !ok { 7969 cc.logf("protocol error: received %T before a SETTINGS frame", f) 7970 return http2ConnectionError(http2ErrCodeProtocol) 7971 } 7972 gotSettings = true 7973 } 7974 maybeIdle := false // whether frame might transition us to idle 7975 7976 switch f := f.(type) { 7977 case *http2MetaHeadersFrame: 7978 err = rl.processHeaders(f) 7979 maybeIdle = true 7980 gotReply = true 7981 case *http2DataFrame: 7982 err = rl.processData(f) 7983 maybeIdle = true 7984 case *http2GoAwayFrame: 7985 err = rl.processGoAway(f) 7986 maybeIdle = true 7987 case *http2RSTStreamFrame: 7988 err = rl.processResetStream(f) 7989 maybeIdle = true 7990 case *http2SettingsFrame: 7991 err = rl.processSettings(f) 7992 case *http2PushPromiseFrame: 7993 err = rl.processPushPromise(f) 7994 case *http2WindowUpdateFrame: 7995 err = rl.processWindowUpdate(f) 7996 case *http2PingFrame: 7997 err = rl.processPing(f) 7998 default: 7999 cc.logf("Transport: unhandled response frame type %T", f) 8000 } 8001 if err != nil { 8002 if http2VerboseLogs { 8003 cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, http2summarizeFrame(f), err) 8004 } 8005 return err 8006 } 8007 if rl.closeWhenIdle && gotReply && maybeIdle && len(rl.activeRes) == 0 { 8008 cc.closeIfIdle() 8009 } 8010 } 8011 } 8012 8013 func (rl *http2clientConnReadLoop) processHeaders(f *http2MetaHeadersFrame) error { 8014 cc := rl.cc 8015 cs := cc.streamByID(f.StreamID, f.StreamEnded()) 8016 if cs == nil { 8017 // We'd get here if we canceled a request while the 8018 // server had its response still in flight. So if this 8019 // was just something we canceled, ignore it. 8020 return nil 8021 } 8022 if !cs.firstByte { 8023 if cs.trace != nil { 8024 // TODO(bradfitz): move first response byte earlier, 8025 // when we first read the 9 byte header, not waiting 8026 // until all the HEADERS+CONTINUATION frames have been 8027 // merged. This works for now. 8028 http2traceFirstResponseByte(cs.trace) 8029 } 8030 cs.firstByte = true 8031 } 8032 if !cs.pastHeaders { 8033 cs.pastHeaders = true 8034 } else { 8035 return rl.processTrailers(cs, f) 8036 } 8037 8038 res, err := rl.handleResponse(cs, f) 8039 if err != nil { 8040 if _, ok := err.(http2ConnectionError); ok { 8041 return err 8042 } 8043 // Any other error type is a stream error. 8044 cs.cc.writeStreamReset(f.StreamID, http2ErrCodeProtocol, err) 8045 cs.resc <- http2resAndError{err: err} 8046 return nil // return nil from process* funcs to keep conn alive 8047 } 8048 if res == nil { 8049 // (nil, nil) special case. See handleResponse docs. 8050 return nil 8051 } 8052 if res.Body != http2noBody { 8053 rl.activeRes[cs.ID] = cs 8054 } 8055 cs.resTrailer = &res.Trailer 8056 cs.resc <- http2resAndError{res: res} 8057 return nil 8058 } 8059 8060 // may return error types nil, or ConnectionError. Any other error value 8061 // is a StreamError of type ErrCodeProtocol. The returned error in that case 8062 // is the detail. 8063 // 8064 // As a special case, handleResponse may return (nil, nil) to skip the 8065 // frame (currently only used for 100 expect continue). This special 8066 // case is going away after Issue 13851 is fixed. 8067 func (rl *http2clientConnReadLoop) handleResponse(cs *http2clientStream, f *http2MetaHeadersFrame) (*Response, error) { 8068 if f.Truncated { 8069 return nil, http2errResponseHeaderListSize 8070 } 8071 8072 status := f.PseudoValue("status") 8073 if status == "" { 8074 return nil, errors.New("missing status pseudo header") 8075 } 8076 statusCode, err := strconv.Atoi(status) 8077 if err != nil { 8078 return nil, errors.New("malformed non-numeric status pseudo header") 8079 } 8080 8081 if statusCode == 100 { 8082 http2traceGot100Continue(cs.trace) 8083 if cs.on100 != nil { 8084 cs.on100() // forces any write delay timer to fire 8085 } 8086 cs.pastHeaders = false // do it all again 8087 return nil, nil 8088 } 8089 8090 header := make(Header) 8091 res := &Response{ 8092 Proto: "HTTP/2.0", 8093 ProtoMajor: 2, 8094 Header: header, 8095 StatusCode: statusCode, 8096 Status: status + " " + StatusText(statusCode), 8097 } 8098 for _, hf := range f.RegularFields() { 8099 key := CanonicalHeaderKey(hf.Name) 8100 if key == "Trailer" { 8101 t := res.Trailer 8102 if t == nil { 8103 t = make(Header) 8104 res.Trailer = t 8105 } 8106 http2foreachHeaderElement(hf.Value, func(v string) { 8107 t[CanonicalHeaderKey(v)] = nil 8108 }) 8109 } else { 8110 header[key] = append(header[key], hf.Value) 8111 } 8112 } 8113 8114 streamEnded := f.StreamEnded() 8115 isHead := cs.req.Method == "HEAD" 8116 if !streamEnded || isHead { 8117 res.ContentLength = -1 8118 if clens := res.Header["Content-Length"]; len(clens) == 1 { 8119 if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil { 8120 res.ContentLength = clen64 8121 } else { 8122 // TODO: care? unlike http/1, it won't mess up our framing, so it's 8123 // more safe smuggling-wise to ignore. 8124 } 8125 } else if len(clens) > 1 { 8126 // TODO: care? unlike http/1, it won't mess up our framing, so it's 8127 // more safe smuggling-wise to ignore. 8128 } 8129 } 8130 8131 if streamEnded || isHead { 8132 res.Body = http2noBody 8133 return res, nil 8134 } 8135 8136 cs.bufPipe = http2pipe{b: &http2dataBuffer{expected: res.ContentLength}} 8137 cs.bytesRemain = res.ContentLength 8138 res.Body = http2transportResponseBody{cs} 8139 go cs.awaitRequestCancel(cs.req) 8140 8141 if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" { 8142 res.Header.Del("Content-Encoding") 8143 res.Header.Del("Content-Length") 8144 res.ContentLength = -1 8145 res.Body = &http2gzipReader{body: res.Body} 8146 http2setResponseUncompressed(res) 8147 } 8148 return res, nil 8149 } 8150 8151 func (rl *http2clientConnReadLoop) processTrailers(cs *http2clientStream, f *http2MetaHeadersFrame) error { 8152 if cs.pastTrailers { 8153 // Too many HEADERS frames for this stream. 8154 return http2ConnectionError(http2ErrCodeProtocol) 8155 } 8156 cs.pastTrailers = true 8157 if !f.StreamEnded() { 8158 // We expect that any headers for trailers also 8159 // has END_STREAM. 8160 return http2ConnectionError(http2ErrCodeProtocol) 8161 } 8162 if len(f.PseudoFields()) > 0 { 8163 // No pseudo header fields are defined for trailers. 8164 // TODO: ConnectionError might be overly harsh? Check. 8165 return http2ConnectionError(http2ErrCodeProtocol) 8166 } 8167 8168 trailer := make(Header) 8169 for _, hf := range f.RegularFields() { 8170 key := CanonicalHeaderKey(hf.Name) 8171 trailer[key] = append(trailer[key], hf.Value) 8172 } 8173 cs.trailer = trailer 8174 8175 rl.endStream(cs) 8176 return nil 8177 } 8178 8179 // transportResponseBody is the concrete type of Transport.RoundTrip's 8180 // Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body. 8181 // On Close it sends RST_STREAM if EOF wasn't already seen. 8182 type http2transportResponseBody struct { 8183 cs *http2clientStream 8184 } 8185 8186 func (b http2transportResponseBody) Read(p []byte) (n int, err error) { 8187 cs := b.cs 8188 cc := cs.cc 8189 8190 if cs.readErr != nil { 8191 return 0, cs.readErr 8192 } 8193 n, err = b.cs.bufPipe.Read(p) 8194 if cs.bytesRemain != -1 { 8195 if int64(n) > cs.bytesRemain { 8196 n = int(cs.bytesRemain) 8197 if err == nil { 8198 err = errors.New("net/http: server replied with more than declared Content-Length; truncated") 8199 cc.writeStreamReset(cs.ID, http2ErrCodeProtocol, err) 8200 } 8201 cs.readErr = err 8202 return int(cs.bytesRemain), err 8203 } 8204 cs.bytesRemain -= int64(n) 8205 if err == io.EOF && cs.bytesRemain > 0 { 8206 err = io.ErrUnexpectedEOF 8207 cs.readErr = err 8208 return n, err 8209 } 8210 } 8211 if n == 0 { 8212 // No flow control tokens to send back. 8213 return 8214 } 8215 8216 cc.mu.Lock() 8217 defer cc.mu.Unlock() 8218 8219 var connAdd, streamAdd int32 8220 // Check the conn-level first, before the stream-level. 8221 if v := cc.inflow.available(); v < http2transportDefaultConnFlow/2 { 8222 connAdd = http2transportDefaultConnFlow - v 8223 cc.inflow.add(connAdd) 8224 } 8225 if err == nil { // No need to refresh if the stream is over or failed. 8226 // Consider any buffered body data (read from the conn but not 8227 // consumed by the client) when computing flow control for this 8228 // stream. 8229 v := int(cs.inflow.available()) + cs.bufPipe.Len() 8230 if v < http2transportDefaultStreamFlow-http2transportDefaultStreamMinRefresh { 8231 streamAdd = int32(http2transportDefaultStreamFlow - v) 8232 cs.inflow.add(streamAdd) 8233 } 8234 } 8235 if connAdd != 0 || streamAdd != 0 { 8236 cc.wmu.Lock() 8237 defer cc.wmu.Unlock() 8238 if connAdd != 0 { 8239 cc.fr.WriteWindowUpdate(0, http2mustUint31(connAdd)) 8240 } 8241 if streamAdd != 0 { 8242 cc.fr.WriteWindowUpdate(cs.ID, http2mustUint31(streamAdd)) 8243 } 8244 cc.bw.Flush() 8245 } 8246 return 8247 } 8248 8249 var http2errClosedResponseBody = errors.New("http2: response body closed") 8250 8251 func (b http2transportResponseBody) Close() error { 8252 cs := b.cs 8253 cc := cs.cc 8254 8255 serverSentStreamEnd := cs.bufPipe.Err() == io.EOF 8256 unread := cs.bufPipe.Len() 8257 8258 if unread > 0 || !serverSentStreamEnd { 8259 cc.mu.Lock() 8260 cc.wmu.Lock() 8261 if !serverSentStreamEnd { 8262 cc.fr.WriteRSTStream(cs.ID, http2ErrCodeCancel) 8263 cs.didReset = true 8264 } 8265 // Return connection-level flow control. 8266 if unread > 0 { 8267 cc.inflow.add(int32(unread)) 8268 cc.fr.WriteWindowUpdate(0, uint32(unread)) 8269 } 8270 cc.bw.Flush() 8271 cc.wmu.Unlock() 8272 cc.mu.Unlock() 8273 } 8274 8275 cs.bufPipe.BreakWithError(http2errClosedResponseBody) 8276 cc.forgetStreamID(cs.ID) 8277 return nil 8278 } 8279 8280 func (rl *http2clientConnReadLoop) processData(f *http2DataFrame) error { 8281 cc := rl.cc 8282 cs := cc.streamByID(f.StreamID, f.StreamEnded()) 8283 data := f.Data() 8284 if cs == nil { 8285 cc.mu.Lock() 8286 neverSent := cc.nextStreamID 8287 cc.mu.Unlock() 8288 if f.StreamID >= neverSent { 8289 // We never asked for this. 8290 cc.logf("http2: Transport received unsolicited DATA frame; closing connection") 8291 return http2ConnectionError(http2ErrCodeProtocol) 8292 } 8293 // We probably did ask for this, but canceled. Just ignore it. 8294 // TODO: be stricter here? only silently ignore things which 8295 // we canceled, but not things which were closed normally 8296 // by the peer? Tough without accumulating too much state. 8297 8298 // But at least return their flow control: 8299 if f.Length > 0 { 8300 cc.mu.Lock() 8301 cc.inflow.add(int32(f.Length)) 8302 cc.mu.Unlock() 8303 8304 cc.wmu.Lock() 8305 cc.fr.WriteWindowUpdate(0, uint32(f.Length)) 8306 cc.bw.Flush() 8307 cc.wmu.Unlock() 8308 } 8309 return nil 8310 } 8311 if f.Length > 0 { 8312 // Check connection-level flow control. 8313 cc.mu.Lock() 8314 if cs.inflow.available() >= int32(f.Length) { 8315 cs.inflow.take(int32(f.Length)) 8316 } else { 8317 cc.mu.Unlock() 8318 return http2ConnectionError(http2ErrCodeFlowControl) 8319 } 8320 // Return any padded flow control now, since we won't 8321 // refund it later on body reads. 8322 var refund int 8323 if pad := int(f.Length) - len(data); pad > 0 { 8324 refund += pad 8325 } 8326 // Return len(data) now if the stream is already closed, 8327 // since data will never be read. 8328 didReset := cs.didReset 8329 if didReset { 8330 refund += len(data) 8331 } 8332 if refund > 0 { 8333 cc.inflow.add(int32(refund)) 8334 cc.wmu.Lock() 8335 cc.fr.WriteWindowUpdate(0, uint32(refund)) 8336 if !didReset { 8337 cs.inflow.add(int32(refund)) 8338 cc.fr.WriteWindowUpdate(cs.ID, uint32(refund)) 8339 } 8340 cc.bw.Flush() 8341 cc.wmu.Unlock() 8342 } 8343 cc.mu.Unlock() 8344 8345 if len(data) > 0 && !didReset { 8346 if _, err := cs.bufPipe.Write(data); err != nil { 8347 rl.endStreamError(cs, err) 8348 return err 8349 } 8350 } 8351 } 8352 8353 if f.StreamEnded() { 8354 rl.endStream(cs) 8355 } 8356 return nil 8357 } 8358 8359 var http2errInvalidTrailers = errors.New("http2: invalid trailers") 8360 8361 func (rl *http2clientConnReadLoop) endStream(cs *http2clientStream) { 8362 // TODO: check that any declared content-length matches, like 8363 // server.go's (*stream).endStream method. 8364 rl.endStreamError(cs, nil) 8365 } 8366 8367 func (rl *http2clientConnReadLoop) endStreamError(cs *http2clientStream, err error) { 8368 var code func() 8369 if err == nil { 8370 err = io.EOF 8371 code = cs.copyTrailers 8372 } 8373 cs.bufPipe.closeWithErrorAndCode(err, code) 8374 delete(rl.activeRes, cs.ID) 8375 if http2isConnectionCloseRequest(cs.req) { 8376 rl.closeWhenIdle = true 8377 } 8378 8379 select { 8380 case cs.resc <- http2resAndError{err: err}: 8381 default: 8382 } 8383 } 8384 8385 func (cs *http2clientStream) copyTrailers() { 8386 for k, vv := range cs.trailer { 8387 t := cs.resTrailer 8388 if *t == nil { 8389 *t = make(Header) 8390 } 8391 (*t)[k] = vv 8392 } 8393 } 8394 8395 func (rl *http2clientConnReadLoop) processGoAway(f *http2GoAwayFrame) error { 8396 cc := rl.cc 8397 cc.t.connPool().MarkDead(cc) 8398 if f.ErrCode != 0 { 8399 // TODO: deal with GOAWAY more. particularly the error code 8400 cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode) 8401 } 8402 cc.setGoAway(f) 8403 return nil 8404 } 8405 8406 func (rl *http2clientConnReadLoop) processSettings(f *http2SettingsFrame) error { 8407 cc := rl.cc 8408 cc.mu.Lock() 8409 defer cc.mu.Unlock() 8410 8411 if f.IsAck() { 8412 if cc.wantSettingsAck { 8413 cc.wantSettingsAck = false 8414 return nil 8415 } 8416 return http2ConnectionError(http2ErrCodeProtocol) 8417 } 8418 8419 err := f.ForeachSetting(func(s http2Setting) error { 8420 switch s.ID { 8421 case http2SettingMaxFrameSize: 8422 cc.maxFrameSize = s.Val 8423 case http2SettingMaxConcurrentStreams: 8424 cc.maxConcurrentStreams = s.Val 8425 case http2SettingInitialWindowSize: 8426 // Values above the maximum flow-control 8427 // window size of 2^31-1 MUST be treated as a 8428 // connection error (Section 5.4.1) of type 8429 // FLOW_CONTROL_ERROR. 8430 if s.Val > math.MaxInt32 { 8431 return http2ConnectionError(http2ErrCodeFlowControl) 8432 } 8433 8434 // Adjust flow control of currently-open 8435 // frames by the difference of the old initial 8436 // window size and this one. 8437 delta := int32(s.Val) - int32(cc.initialWindowSize) 8438 for _, cs := range cc.streams { 8439 cs.flow.add(delta) 8440 } 8441 cc.cond.Broadcast() 8442 8443 cc.initialWindowSize = s.Val 8444 default: 8445 // TODO(bradfitz): handle more settings? SETTINGS_HEADER_TABLE_SIZE probably. 8446 cc.vlogf("Unhandled Setting: %v", s) 8447 } 8448 return nil 8449 }) 8450 if err != nil { 8451 return err 8452 } 8453 8454 cc.wmu.Lock() 8455 defer cc.wmu.Unlock() 8456 8457 cc.fr.WriteSettingsAck() 8458 cc.bw.Flush() 8459 return cc.werr 8460 } 8461 8462 func (rl *http2clientConnReadLoop) processWindowUpdate(f *http2WindowUpdateFrame) error { 8463 cc := rl.cc 8464 cs := cc.streamByID(f.StreamID, false) 8465 if f.StreamID != 0 && cs == nil { 8466 return nil 8467 } 8468 8469 cc.mu.Lock() 8470 defer cc.mu.Unlock() 8471 8472 fl := &cc.flow 8473 if cs != nil { 8474 fl = &cs.flow 8475 } 8476 if !fl.add(int32(f.Increment)) { 8477 return http2ConnectionError(http2ErrCodeFlowControl) 8478 } 8479 cc.cond.Broadcast() 8480 return nil 8481 } 8482 8483 func (rl *http2clientConnReadLoop) processResetStream(f *http2RSTStreamFrame) error { 8484 cs := rl.cc.streamByID(f.StreamID, true) 8485 if cs == nil { 8486 // TODO: return error if server tries to RST_STEAM an idle stream 8487 return nil 8488 } 8489 select { 8490 case <-cs.peerReset: 8491 // Already reset. 8492 // This is the only goroutine 8493 // which closes this, so there 8494 // isn't a race. 8495 default: 8496 err := http2streamError(cs.ID, f.ErrCode) 8497 cs.resetErr = err 8498 close(cs.peerReset) 8499 cs.bufPipe.CloseWithError(err) 8500 cs.cc.cond.Broadcast() // wake up checkResetOrDone via clientStream.awaitFlowControl 8501 } 8502 delete(rl.activeRes, cs.ID) 8503 return nil 8504 } 8505 8506 // Ping sends a PING frame to the server and waits for the ack. 8507 // Public implementation is in go17.go and not_go17.go 8508 func (cc *http2ClientConn) ping(ctx http2contextContext) error { 8509 c := make(chan struct{}) 8510 // Generate a random payload 8511 var p [8]byte 8512 for { 8513 if _, err := rand.Read(p[:]); err != nil { 8514 return err 8515 } 8516 cc.mu.Lock() 8517 // check for dup before insert 8518 if _, found := cc.pings[p]; !found { 8519 cc.pings[p] = c 8520 cc.mu.Unlock() 8521 break 8522 } 8523 cc.mu.Unlock() 8524 } 8525 cc.wmu.Lock() 8526 if err := cc.fr.WritePing(false, p); err != nil { 8527 cc.wmu.Unlock() 8528 return err 8529 } 8530 if err := cc.bw.Flush(); err != nil { 8531 cc.wmu.Unlock() 8532 return err 8533 } 8534 cc.wmu.Unlock() 8535 select { 8536 case <-c: 8537 return nil 8538 case <-ctx.Done(): 8539 return ctx.Err() 8540 case <-cc.readerDone: 8541 // connection closed 8542 return cc.readerErr 8543 } 8544 } 8545 8546 func (rl *http2clientConnReadLoop) processPing(f *http2PingFrame) error { 8547 if f.IsAck() { 8548 cc := rl.cc 8549 cc.mu.Lock() 8550 defer cc.mu.Unlock() 8551 // If ack, notify listener if any 8552 if c, ok := cc.pings[f.Data]; ok { 8553 close(c) 8554 delete(cc.pings, f.Data) 8555 } 8556 return nil 8557 } 8558 cc := rl.cc 8559 cc.wmu.Lock() 8560 defer cc.wmu.Unlock() 8561 if err := cc.fr.WritePing(true, f.Data); err != nil { 8562 return err 8563 } 8564 return cc.bw.Flush() 8565 } 8566 8567 func (rl *http2clientConnReadLoop) processPushPromise(f *http2PushPromiseFrame) error { 8568 // We told the peer we don't want them. 8569 // Spec says: 8570 // "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH 8571 // setting of the peer endpoint is set to 0. An endpoint that 8572 // has set this setting and has received acknowledgement MUST 8573 // treat the receipt of a PUSH_PROMISE frame as a connection 8574 // error (Section 5.4.1) of type PROTOCOL_ERROR." 8575 return http2ConnectionError(http2ErrCodeProtocol) 8576 } 8577 8578 func (cc *http2ClientConn) writeStreamReset(streamID uint32, code http2ErrCode, err error) { 8579 // TODO: map err to more interesting error codes, once the 8580 // HTTP community comes up with some. But currently for 8581 // RST_STREAM there's no equivalent to GOAWAY frame's debug 8582 // data, and the error codes are all pretty vague ("cancel"). 8583 cc.wmu.Lock() 8584 cc.fr.WriteRSTStream(streamID, code) 8585 cc.bw.Flush() 8586 cc.wmu.Unlock() 8587 } 8588 8589 var ( 8590 http2errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit") 8591 http2errPseudoTrailers = errors.New("http2: invalid pseudo header in trailers") 8592 ) 8593 8594 func (cc *http2ClientConn) logf(format string, args ...interface{}) { 8595 cc.t.logf(format, args...) 8596 } 8597 8598 func (cc *http2ClientConn) vlogf(format string, args ...interface{}) { 8599 cc.t.vlogf(format, args...) 8600 } 8601 8602 func (t *http2Transport) vlogf(format string, args ...interface{}) { 8603 if http2VerboseLogs { 8604 t.logf(format, args...) 8605 } 8606 } 8607 8608 func (t *http2Transport) logf(format string, args ...interface{}) { 8609 log.Printf(format, args...) 8610 } 8611 8612 var http2noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil)) 8613 8614 func http2strSliceContains(ss []string, s string) bool { 8615 for _, v := range ss { 8616 if v == s { 8617 return true 8618 } 8619 } 8620 return false 8621 } 8622 8623 type http2erringRoundTripper struct{ err error } 8624 8625 func (rt http2erringRoundTripper) RoundTrip(*Request) (*Response, error) { return nil, rt.err } 8626 8627 // gzipReader wraps a response body so it can lazily 8628 // call gzip.NewReader on the first call to Read 8629 type http2gzipReader struct { 8630 body io.ReadCloser // underlying Response.Body 8631 zr *gzip.Reader // lazily-initialized gzip reader 8632 zerr error // sticky error 8633 } 8634 8635 func (gz *http2gzipReader) Read(p []byte) (n int, err error) { 8636 if gz.zerr != nil { 8637 return 0, gz.zerr 8638 } 8639 if gz.zr == nil { 8640 gz.zr, err = gzip.NewReader(gz.body) 8641 if err != nil { 8642 gz.zerr = err 8643 return 0, err 8644 } 8645 } 8646 return gz.zr.Read(p) 8647 } 8648 8649 func (gz *http2gzipReader) Close() error { 8650 return gz.body.Close() 8651 } 8652 8653 type http2errorReader struct{ err error } 8654 8655 func (r http2errorReader) Read(p []byte) (int, error) { return 0, r.err } 8656 8657 // bodyWriterState encapsulates various state around the Transport's writing 8658 // of the request body, particularly regarding doing delayed writes of the body 8659 // when the request contains "Expect: 100-continue". 8660 type http2bodyWriterState struct { 8661 cs *http2clientStream 8662 timer *time.Timer // if non-nil, we're doing a delayed write 8663 fnonce *sync.Once // to call fn with 8664 fn func() // the code to run in the goroutine, writing the body 8665 resc chan error // result of fn's execution 8666 delay time.Duration // how long we should delay a delayed write for 8667 } 8668 8669 func (t *http2Transport) getBodyWriterState(cs *http2clientStream, body io.Reader) (s http2bodyWriterState) { 8670 s.cs = cs 8671 if body == nil { 8672 return 8673 } 8674 resc := make(chan error, 1) 8675 s.resc = resc 8676 s.fn = func() { 8677 cs.cc.mu.Lock() 8678 cs.startedWrite = true 8679 cs.cc.mu.Unlock() 8680 resc <- cs.writeRequestBody(body, cs.req.Body) 8681 } 8682 s.delay = t.expectContinueTimeout() 8683 if s.delay == 0 || 8684 !httplex.HeaderValuesContainsToken( 8685 cs.req.Header["Expect"], 8686 "100-continue") { 8687 return 8688 } 8689 s.fnonce = new(sync.Once) 8690 8691 // Arm the timer with a very large duration, which we'll 8692 // intentionally lower later. It has to be large now because 8693 // we need a handle to it before writing the headers, but the 8694 // s.delay value is defined to not start until after the 8695 // request headers were written. 8696 const hugeDuration = 365 * 24 * time.Hour 8697 s.timer = time.AfterFunc(hugeDuration, func() { 8698 s.fnonce.Do(s.fn) 8699 }) 8700 return 8701 } 8702 8703 func (s http2bodyWriterState) cancel() { 8704 if s.timer != nil { 8705 s.timer.Stop() 8706 } 8707 } 8708 8709 func (s http2bodyWriterState) on100() { 8710 if s.timer == nil { 8711 // If we didn't do a delayed write, ignore the server's 8712 // bogus 100 continue response. 8713 return 8714 } 8715 s.timer.Stop() 8716 go func() { s.fnonce.Do(s.fn) }() 8717 } 8718 8719 // scheduleBodyWrite starts writing the body, either immediately (in 8720 // the common case) or after the delay timeout. It should not be 8721 // called until after the headers have been written. 8722 func (s http2bodyWriterState) scheduleBodyWrite() { 8723 if s.timer == nil { 8724 // We're not doing a delayed write (see 8725 // getBodyWriterState), so just start the writing 8726 // goroutine immediately. 8727 go s.fn() 8728 return 8729 } 8730 http2traceWait100Continue(s.cs.trace) 8731 if s.timer.Stop() { 8732 s.timer.Reset(s.delay) 8733 } 8734 } 8735 8736 // isConnectionCloseRequest reports whether req should use its own 8737 // connection for a single request and then close the connection. 8738 func http2isConnectionCloseRequest(req *Request) bool { 8739 return req.Close || httplex.HeaderValuesContainsToken(req.Header["Connection"], "close") 8740 } 8741 8742 // writeFramer is implemented by any type that is used to write frames. 8743 type http2writeFramer interface { 8744 writeFrame(http2writeContext) error 8745 8746 // staysWithinBuffer reports whether this writer promises that 8747 // it will only write less than or equal to size bytes, and it 8748 // won't Flush the write context. 8749 staysWithinBuffer(size int) bool 8750 } 8751 8752 // writeContext is the interface needed by the various frame writer 8753 // types below. All the writeFrame methods below are scheduled via the 8754 // frame writing scheduler (see writeScheduler in writesched.go). 8755 // 8756 // This interface is implemented by *serverConn. 8757 // 8758 // TODO: decide whether to a) use this in the client code (which didn't 8759 // end up using this yet, because it has a simpler design, not 8760 // currently implementing priorities), or b) delete this and 8761 // make the server code a bit more concrete. 8762 type http2writeContext interface { 8763 Framer() *http2Framer 8764 Flush() error 8765 CloseConn() error 8766 // HeaderEncoder returns an HPACK encoder that writes to the 8767 // returned buffer. 8768 HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) 8769 } 8770 8771 // writeEndsStream reports whether w writes a frame that will transition 8772 // the stream to a half-closed local state. This returns false for RST_STREAM, 8773 // which closes the entire stream (not just the local half). 8774 func http2writeEndsStream(w http2writeFramer) bool { 8775 switch v := w.(type) { 8776 case *http2writeData: 8777 return v.endStream 8778 case *http2writeResHeaders: 8779 return v.endStream 8780 case nil: 8781 // This can only happen if the caller reuses w after it's 8782 // been intentionally nil'ed out to prevent use. Keep this 8783 // here to catch future refactoring breaking it. 8784 panic("writeEndsStream called on nil writeFramer") 8785 } 8786 return false 8787 } 8788 8789 type http2flushFrameWriter struct{} 8790 8791 func (http2flushFrameWriter) writeFrame(ctx http2writeContext) error { 8792 return ctx.Flush() 8793 } 8794 8795 func (http2flushFrameWriter) staysWithinBuffer(max int) bool { return false } 8796 8797 type http2writeSettings []http2Setting 8798 8799 func (s http2writeSettings) staysWithinBuffer(max int) bool { 8800 const settingSize = 6 // uint16 + uint32 8801 return http2frameHeaderLen+settingSize*len(s) <= max 8802 8803 } 8804 8805 func (s http2writeSettings) writeFrame(ctx http2writeContext) error { 8806 return ctx.Framer().WriteSettings([]http2Setting(s)...) 8807 } 8808 8809 type http2writeGoAway struct { 8810 maxStreamID uint32 8811 code http2ErrCode 8812 } 8813 8814 func (p *http2writeGoAway) writeFrame(ctx http2writeContext) error { 8815 err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil) 8816 if p.code != 0 { 8817 ctx.Flush() // ignore error: we're hanging up on them anyway 8818 time.Sleep(50 * time.Millisecond) 8819 ctx.CloseConn() 8820 } 8821 return err 8822 } 8823 8824 func (*http2writeGoAway) staysWithinBuffer(max int) bool { return false } // flushes 8825 8826 type http2writeData struct { 8827 streamID uint32 8828 p []byte 8829 endStream bool 8830 } 8831 8832 func (w *http2writeData) String() string { 8833 return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream) 8834 } 8835 8836 func (w *http2writeData) writeFrame(ctx http2writeContext) error { 8837 return ctx.Framer().WriteData(w.streamID, w.endStream, w.p) 8838 } 8839 8840 func (w *http2writeData) staysWithinBuffer(max int) bool { 8841 return http2frameHeaderLen+len(w.p) <= max 8842 } 8843 8844 // handlerPanicRST is the message sent from handler goroutines when 8845 // the handler panics. 8846 type http2handlerPanicRST struct { 8847 StreamID uint32 8848 } 8849 8850 func (hp http2handlerPanicRST) writeFrame(ctx http2writeContext) error { 8851 return ctx.Framer().WriteRSTStream(hp.StreamID, http2ErrCodeInternal) 8852 } 8853 8854 func (hp http2handlerPanicRST) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max } 8855 8856 func (se http2StreamError) writeFrame(ctx http2writeContext) error { 8857 return ctx.Framer().WriteRSTStream(se.StreamID, se.Code) 8858 } 8859 8860 func (se http2StreamError) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max } 8861 8862 type http2writePingAck struct{ pf *http2PingFrame } 8863 8864 func (w http2writePingAck) writeFrame(ctx http2writeContext) error { 8865 return ctx.Framer().WritePing(true, w.pf.Data) 8866 } 8867 8868 func (w http2writePingAck) staysWithinBuffer(max int) bool { 8869 return http2frameHeaderLen+len(w.pf.Data) <= max 8870 } 8871 8872 type http2writeSettingsAck struct{} 8873 8874 func (http2writeSettingsAck) writeFrame(ctx http2writeContext) error { 8875 return ctx.Framer().WriteSettingsAck() 8876 } 8877 8878 func (http2writeSettingsAck) staysWithinBuffer(max int) bool { return http2frameHeaderLen <= max } 8879 8880 // splitHeaderBlock splits headerBlock into fragments so that each fragment fits 8881 // in a single frame, then calls fn for each fragment. firstFrag/lastFrag are true 8882 // for the first/last fragment, respectively. 8883 func http2splitHeaderBlock(ctx http2writeContext, headerBlock []byte, fn func(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error) error { 8884 // For now we're lazy and just pick the minimum MAX_FRAME_SIZE 8885 // that all peers must support (16KB). Later we could care 8886 // more and send larger frames if the peer advertised it, but 8887 // there's little point. Most headers are small anyway (so we 8888 // generally won't have CONTINUATION frames), and extra frames 8889 // only waste 9 bytes anyway. 8890 const maxFrameSize = 16384 8891 8892 first := true 8893 for len(headerBlock) > 0 { 8894 frag := headerBlock 8895 if len(frag) > maxFrameSize { 8896 frag = frag[:maxFrameSize] 8897 } 8898 headerBlock = headerBlock[len(frag):] 8899 if err := fn(ctx, frag, first, len(headerBlock) == 0); err != nil { 8900 return err 8901 } 8902 first = false 8903 } 8904 return nil 8905 } 8906 8907 // writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames 8908 // for HTTP response headers or trailers from a server handler. 8909 type http2writeResHeaders struct { 8910 streamID uint32 8911 httpResCode int // 0 means no ":status" line 8912 h Header // may be nil 8913 trailers []string // if non-nil, which keys of h to write. nil means all. 8914 endStream bool 8915 8916 date string 8917 contentType string 8918 contentLength string 8919 } 8920 8921 func http2encKV(enc *hpack.Encoder, k, v string) { 8922 if http2VerboseLogs { 8923 log.Printf("http2: server encoding header %q = %q", k, v) 8924 } 8925 enc.WriteField(hpack.HeaderField{Name: k, Value: v}) 8926 } 8927 8928 func (w *http2writeResHeaders) staysWithinBuffer(max int) bool { 8929 // TODO: this is a common one. It'd be nice to return true 8930 // here and get into the fast path if we could be clever and 8931 // calculate the size fast enough, or at least a conservative 8932 // uppper bound that usually fires. (Maybe if w.h and 8933 // w.trailers are nil, so we don't need to enumerate it.) 8934 // Otherwise I'm afraid that just calculating the length to 8935 // answer this question would be slower than the ~2µs benefit. 8936 return false 8937 } 8938 8939 func (w *http2writeResHeaders) writeFrame(ctx http2writeContext) error { 8940 enc, buf := ctx.HeaderEncoder() 8941 buf.Reset() 8942 8943 if w.httpResCode != 0 { 8944 http2encKV(enc, ":status", http2httpCodeString(w.httpResCode)) 8945 } 8946 8947 http2encodeHeaders(enc, w.h, w.trailers) 8948 8949 if w.contentType != "" { 8950 http2encKV(enc, "content-type", w.contentType) 8951 } 8952 if w.contentLength != "" { 8953 http2encKV(enc, "content-length", w.contentLength) 8954 } 8955 if w.date != "" { 8956 http2encKV(enc, "date", w.date) 8957 } 8958 8959 headerBlock := buf.Bytes() 8960 if len(headerBlock) == 0 && w.trailers == nil { 8961 panic("unexpected empty hpack") 8962 } 8963 8964 return http2splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock) 8965 } 8966 8967 func (w *http2writeResHeaders) writeHeaderBlock(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error { 8968 if firstFrag { 8969 return ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 8970 StreamID: w.streamID, 8971 BlockFragment: frag, 8972 EndStream: w.endStream, 8973 EndHeaders: lastFrag, 8974 }) 8975 } else { 8976 return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag) 8977 } 8978 } 8979 8980 // writePushPromise is a request to write a PUSH_PROMISE and 0+ CONTINUATION frames. 8981 type http2writePushPromise struct { 8982 streamID uint32 // pusher stream 8983 method string // for :method 8984 url *url.URL // for :scheme, :authority, :path 8985 h Header 8986 8987 // Creates an ID for a pushed stream. This runs on serveG just before 8988 // the frame is written. The returned ID is copied to promisedID. 8989 allocatePromisedID func() (uint32, error) 8990 promisedID uint32 8991 } 8992 8993 func (w *http2writePushPromise) staysWithinBuffer(max int) bool { 8994 // TODO: see writeResHeaders.staysWithinBuffer 8995 return false 8996 } 8997 8998 func (w *http2writePushPromise) writeFrame(ctx http2writeContext) error { 8999 enc, buf := ctx.HeaderEncoder() 9000 buf.Reset() 9001 9002 http2encKV(enc, ":method", w.method) 9003 http2encKV(enc, ":scheme", w.url.Scheme) 9004 http2encKV(enc, ":authority", w.url.Host) 9005 http2encKV(enc, ":path", w.url.RequestURI()) 9006 http2encodeHeaders(enc, w.h, nil) 9007 9008 headerBlock := buf.Bytes() 9009 if len(headerBlock) == 0 { 9010 panic("unexpected empty hpack") 9011 } 9012 9013 return http2splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock) 9014 } 9015 9016 func (w *http2writePushPromise) writeHeaderBlock(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error { 9017 if firstFrag { 9018 return ctx.Framer().WritePushPromise(http2PushPromiseParam{ 9019 StreamID: w.streamID, 9020 PromiseID: w.promisedID, 9021 BlockFragment: frag, 9022 EndHeaders: lastFrag, 9023 }) 9024 } else { 9025 return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag) 9026 } 9027 } 9028 9029 type http2write100ContinueHeadersFrame struct { 9030 streamID uint32 9031 } 9032 9033 func (w http2write100ContinueHeadersFrame) writeFrame(ctx http2writeContext) error { 9034 enc, buf := ctx.HeaderEncoder() 9035 buf.Reset() 9036 http2encKV(enc, ":status", "100") 9037 return ctx.Framer().WriteHeaders(http2HeadersFrameParam{ 9038 StreamID: w.streamID, 9039 BlockFragment: buf.Bytes(), 9040 EndStream: false, 9041 EndHeaders: true, 9042 }) 9043 } 9044 9045 func (w http2write100ContinueHeadersFrame) staysWithinBuffer(max int) bool { 9046 // Sloppy but conservative: 9047 return 9+2*(len(":status")+len("100")) <= max 9048 } 9049 9050 type http2writeWindowUpdate struct { 9051 streamID uint32 // or 0 for conn-level 9052 n uint32 9053 } 9054 9055 func (wu http2writeWindowUpdate) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max } 9056 9057 func (wu http2writeWindowUpdate) writeFrame(ctx http2writeContext) error { 9058 return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n) 9059 } 9060 9061 // encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k]) 9062 // is encoded only only if k is in keys. 9063 func http2encodeHeaders(enc *hpack.Encoder, h Header, keys []string) { 9064 if keys == nil { 9065 sorter := http2sorterPool.Get().(*http2sorter) 9066 // Using defer here, since the returned keys from the 9067 // sorter.Keys method is only valid until the sorter 9068 // is returned: 9069 defer http2sorterPool.Put(sorter) 9070 keys = sorter.Keys(h) 9071 } 9072 for _, k := range keys { 9073 vv := h[k] 9074 k = http2lowerHeader(k) 9075 if !http2validWireHeaderFieldName(k) { 9076 // Skip it as backup paranoia. Per 9077 // golang.org/issue/14048, these should 9078 // already be rejected at a higher level. 9079 continue 9080 } 9081 isTE := k == "transfer-encoding" 9082 for _, v := range vv { 9083 if !httplex.ValidHeaderFieldValue(v) { 9084 // TODO: return an error? golang.org/issue/14048 9085 // For now just omit it. 9086 continue 9087 } 9088 // TODO: more of "8.1.2.2 Connection-Specific Header Fields" 9089 if isTE && v != "trailers" { 9090 continue 9091 } 9092 http2encKV(enc, k, v) 9093 } 9094 } 9095 } 9096 9097 // WriteScheduler is the interface implemented by HTTP/2 write schedulers. 9098 // Methods are never called concurrently. 9099 type http2WriteScheduler interface { 9100 // OpenStream opens a new stream in the write scheduler. 9101 // It is illegal to call this with streamID=0 or with a streamID that is 9102 // already open -- the call may panic. 9103 OpenStream(streamID uint32, options http2OpenStreamOptions) 9104 9105 // CloseStream closes a stream in the write scheduler. Any frames queued on 9106 // this stream should be discarded. It is illegal to call this on a stream 9107 // that is not open -- the call may panic. 9108 CloseStream(streamID uint32) 9109 9110 // AdjustStream adjusts the priority of the given stream. This may be called 9111 // on a stream that has not yet been opened or has been closed. Note that 9112 // RFC 7540 allows PRIORITY frames to be sent on streams in any state. See: 9113 // https://tools.ietf.org/html/rfc7540#section-5.1 9114 AdjustStream(streamID uint32, priority http2PriorityParam) 9115 9116 // Push queues a frame in the scheduler. In most cases, this will not be 9117 // called with wr.StreamID()!=0 unless that stream is currently open. The one 9118 // exception is RST_STREAM frames, which may be sent on idle or closed streams. 9119 Push(wr http2FrameWriteRequest) 9120 9121 // Pop dequeues the next frame to write. Returns false if no frames can 9122 // be written. Frames with a given wr.StreamID() are Pop'd in the same 9123 // order they are Push'd. 9124 Pop() (wr http2FrameWriteRequest, ok bool) 9125 } 9126 9127 // OpenStreamOptions specifies extra options for WriteScheduler.OpenStream. 9128 type http2OpenStreamOptions struct { 9129 // PusherID is zero if the stream was initiated by the client. Otherwise, 9130 // PusherID names the stream that pushed the newly opened stream. 9131 PusherID uint32 9132 } 9133 9134 // FrameWriteRequest is a request to write a frame. 9135 type http2FrameWriteRequest struct { 9136 // write is the interface value that does the writing, once the 9137 // WriteScheduler has selected this frame to write. The write 9138 // functions are all defined in write.go. 9139 write http2writeFramer 9140 9141 // stream is the stream on which this frame will be written. 9142 // nil for non-stream frames like PING and SETTINGS. 9143 stream *http2stream 9144 9145 // done, if non-nil, must be a buffered channel with space for 9146 // 1 message and is sent the return value from write (or an 9147 // earlier error) when the frame has been written. 9148 done chan error 9149 } 9150 9151 // StreamID returns the id of the stream this frame will be written to. 9152 // 0 is used for non-stream frames such as PING and SETTINGS. 9153 func (wr http2FrameWriteRequest) StreamID() uint32 { 9154 if wr.stream == nil { 9155 if se, ok := wr.write.(http2StreamError); ok { 9156 // (*serverConn).resetStream doesn't set 9157 // stream because it doesn't necessarily have 9158 // one. So special case this type of write 9159 // message. 9160 return se.StreamID 9161 } 9162 return 0 9163 } 9164 return wr.stream.id 9165 } 9166 9167 // DataSize returns the number of flow control bytes that must be consumed 9168 // to write this entire frame. This is 0 for non-DATA frames. 9169 func (wr http2FrameWriteRequest) DataSize() int { 9170 if wd, ok := wr.write.(*http2writeData); ok { 9171 return len(wd.p) 9172 } 9173 return 0 9174 } 9175 9176 // Consume consumes min(n, available) bytes from this frame, where available 9177 // is the number of flow control bytes available on the stream. Consume returns 9178 // 0, 1, or 2 frames, where the integer return value gives the number of frames 9179 // returned. 9180 // 9181 // If flow control prevents consuming any bytes, this returns (_, _, 0). If 9182 // the entire frame was consumed, this returns (wr, _, 1). Otherwise, this 9183 // returns (consumed, rest, 2), where 'consumed' contains the consumed bytes and 9184 // 'rest' contains the remaining bytes. The consumed bytes are deducted from the 9185 // underlying stream's flow control budget. 9186 func (wr http2FrameWriteRequest) Consume(n int32) (http2FrameWriteRequest, http2FrameWriteRequest, int) { 9187 var empty http2FrameWriteRequest 9188 9189 // Non-DATA frames are always consumed whole. 9190 wd, ok := wr.write.(*http2writeData) 9191 if !ok || len(wd.p) == 0 { 9192 return wr, empty, 1 9193 } 9194 9195 // Might need to split after applying limits. 9196 allowed := wr.stream.flow.available() 9197 if n < allowed { 9198 allowed = n 9199 } 9200 if wr.stream.sc.maxFrameSize < allowed { 9201 allowed = wr.stream.sc.maxFrameSize 9202 } 9203 if allowed <= 0 { 9204 return empty, empty, 0 9205 } 9206 if len(wd.p) > int(allowed) { 9207 wr.stream.flow.take(allowed) 9208 consumed := http2FrameWriteRequest{ 9209 stream: wr.stream, 9210 write: &http2writeData{ 9211 streamID: wd.streamID, 9212 p: wd.p[:allowed], 9213 // Even if the original had endStream set, there 9214 // are bytes remaining because len(wd.p) > allowed, 9215 // so we know endStream is false. 9216 endStream: false, 9217 }, 9218 // Our caller is blocking on the final DATA frame, not 9219 // this intermediate frame, so no need to wait. 9220 done: nil, 9221 } 9222 rest := http2FrameWriteRequest{ 9223 stream: wr.stream, 9224 write: &http2writeData{ 9225 streamID: wd.streamID, 9226 p: wd.p[allowed:], 9227 endStream: wd.endStream, 9228 }, 9229 done: wr.done, 9230 } 9231 return consumed, rest, 2 9232 } 9233 9234 // The frame is consumed whole. 9235 // NB: This cast cannot overflow because allowed is <= math.MaxInt32. 9236 wr.stream.flow.take(int32(len(wd.p))) 9237 return wr, empty, 1 9238 } 9239 9240 // String is for debugging only. 9241 func (wr http2FrameWriteRequest) String() string { 9242 var des string 9243 if s, ok := wr.write.(fmt.Stringer); ok { 9244 des = s.String() 9245 } else { 9246 des = fmt.Sprintf("%T", wr.write) 9247 } 9248 return fmt.Sprintf("[FrameWriteRequest stream=%d, ch=%v, writer=%v]", wr.StreamID(), wr.done != nil, des) 9249 } 9250 9251 // replyToWriter sends err to wr.done and panics if the send must block 9252 // This does nothing if wr.done is nil. 9253 func (wr *http2FrameWriteRequest) replyToWriter(err error) { 9254 if wr.done == nil { 9255 return 9256 } 9257 select { 9258 case wr.done <- err: 9259 default: 9260 panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wr.write)) 9261 } 9262 wr.write = nil // prevent use (assume it's tainted after wr.done send) 9263 } 9264 9265 // writeQueue is used by implementations of WriteScheduler. 9266 type http2writeQueue struct { 9267 s []http2FrameWriteRequest 9268 } 9269 9270 func (q *http2writeQueue) empty() bool { return len(q.s) == 0 } 9271 9272 func (q *http2writeQueue) push(wr http2FrameWriteRequest) { 9273 q.s = append(q.s, wr) 9274 } 9275 9276 func (q *http2writeQueue) shift() http2FrameWriteRequest { 9277 if len(q.s) == 0 { 9278 panic("invalid use of queue") 9279 } 9280 wr := q.s[0] 9281 // TODO: less copy-happy queue. 9282 copy(q.s, q.s[1:]) 9283 q.s[len(q.s)-1] = http2FrameWriteRequest{} 9284 q.s = q.s[:len(q.s)-1] 9285 return wr 9286 } 9287 9288 // consume consumes up to n bytes from q.s[0]. If the frame is 9289 // entirely consumed, it is removed from the queue. If the frame 9290 // is partially consumed, the frame is kept with the consumed 9291 // bytes removed. Returns true iff any bytes were consumed. 9292 func (q *http2writeQueue) consume(n int32) (http2FrameWriteRequest, bool) { 9293 if len(q.s) == 0 { 9294 return http2FrameWriteRequest{}, false 9295 } 9296 consumed, rest, numresult := q.s[0].Consume(n) 9297 switch numresult { 9298 case 0: 9299 return http2FrameWriteRequest{}, false 9300 case 1: 9301 q.shift() 9302 case 2: 9303 q.s[0] = rest 9304 } 9305 return consumed, true 9306 } 9307 9308 type http2writeQueuePool []*http2writeQueue 9309 9310 // put inserts an unused writeQueue into the pool. 9311 9312 // put inserts an unused writeQueue into the pool. 9313 func (p *http2writeQueuePool) put(q *http2writeQueue) { 9314 for i := range q.s { 9315 q.s[i] = http2FrameWriteRequest{} 9316 } 9317 q.s = q.s[:0] 9318 *p = append(*p, q) 9319 } 9320 9321 // get returns an empty writeQueue. 9322 func (p *http2writeQueuePool) get() *http2writeQueue { 9323 ln := len(*p) 9324 if ln == 0 { 9325 return new(http2writeQueue) 9326 } 9327 x := ln - 1 9328 q := (*p)[x] 9329 (*p)[x] = nil 9330 *p = (*p)[:x] 9331 return q 9332 } 9333 9334 // RFC 7540, Section 5.3.5: the default weight is 16. 9335 const http2priorityDefaultWeight = 15 // 16 = 15 + 1 9336 9337 // PriorityWriteSchedulerConfig configures a priorityWriteScheduler. 9338 type http2PriorityWriteSchedulerConfig struct { 9339 // MaxClosedNodesInTree controls the maximum number of closed streams to 9340 // retain in the priority tree. Setting this to zero saves a small amount 9341 // of memory at the cost of performance. 9342 // 9343 // See RFC 7540, Section 5.3.4: 9344 // "It is possible for a stream to become closed while prioritization 9345 // information ... is in transit. ... This potentially creates suboptimal 9346 // prioritization, since the stream could be given a priority that is 9347 // different from what is intended. To avoid these problems, an endpoint 9348 // SHOULD retain stream prioritization state for a period after streams 9349 // become closed. The longer state is retained, the lower the chance that 9350 // streams are assigned incorrect or default priority values." 9351 MaxClosedNodesInTree int 9352 9353 // MaxIdleNodesInTree controls the maximum number of idle streams to 9354 // retain in the priority tree. Setting this to zero saves a small amount 9355 // of memory at the cost of performance. 9356 // 9357 // See RFC 7540, Section 5.3.4: 9358 // Similarly, streams that are in the "idle" state can be assigned 9359 // priority or become a parent of other streams. This allows for the 9360 // creation of a grouping node in the dependency tree, which enables 9361 // more flexible expressions of priority. Idle streams begin with a 9362 // default priority (Section 5.3.5). 9363 MaxIdleNodesInTree int 9364 9365 // ThrottleOutOfOrderWrites enables write throttling to help ensure that 9366 // data is delivered in priority order. This works around a race where 9367 // stream B depends on stream A and both streams are about to call Write 9368 // to queue DATA frames. If B wins the race, a naive scheduler would eagerly 9369 // write as much data from B as possible, but this is suboptimal because A 9370 // is a higher-priority stream. With throttling enabled, we write a small 9371 // amount of data from B to minimize the amount of bandwidth that B can 9372 // steal from A. 9373 ThrottleOutOfOrderWrites bool 9374 } 9375 9376 // NewPriorityWriteScheduler constructs a WriteScheduler that schedules 9377 // frames by following HTTP/2 priorities as described in RFC 7540 Section 5.3. 9378 // If cfg is nil, default options are used. 9379 func http2NewPriorityWriteScheduler(cfg *http2PriorityWriteSchedulerConfig) http2WriteScheduler { 9380 if cfg == nil { 9381 // For justification of these defaults, see: 9382 // https://docs.google.com/document/d/1oLhNg1skaWD4_DtaoCxdSRN5erEXrH-KnLrMwEpOtFY 9383 cfg = &http2PriorityWriteSchedulerConfig{ 9384 MaxClosedNodesInTree: 10, 9385 MaxIdleNodesInTree: 10, 9386 ThrottleOutOfOrderWrites: false, 9387 } 9388 } 9389 9390 ws := &http2priorityWriteScheduler{ 9391 nodes: make(map[uint32]*http2priorityNode), 9392 maxClosedNodesInTree: cfg.MaxClosedNodesInTree, 9393 maxIdleNodesInTree: cfg.MaxIdleNodesInTree, 9394 enableWriteThrottle: cfg.ThrottleOutOfOrderWrites, 9395 } 9396 ws.nodes[0] = &ws.root 9397 if cfg.ThrottleOutOfOrderWrites { 9398 ws.writeThrottleLimit = 1024 9399 } else { 9400 ws.writeThrottleLimit = math.MaxInt32 9401 } 9402 return ws 9403 } 9404 9405 type http2priorityNodeState int 9406 9407 const ( 9408 http2priorityNodeOpen http2priorityNodeState = iota 9409 http2priorityNodeClosed 9410 http2priorityNodeIdle 9411 ) 9412 9413 // priorityNode is a node in an HTTP/2 priority tree. 9414 // Each node is associated with a single stream ID. 9415 // See RFC 7540, Section 5.3. 9416 type http2priorityNode struct { 9417 q http2writeQueue // queue of pending frames to write 9418 id uint32 // id of the stream, or 0 for the root of the tree 9419 weight uint8 // the actual weight is weight+1, so the value is in [1,256] 9420 state http2priorityNodeState // open | closed | idle 9421 bytes int64 // number of bytes written by this node, or 0 if closed 9422 subtreeBytes int64 // sum(node.bytes) of all nodes in this subtree 9423 9424 // These links form the priority tree. 9425 parent *http2priorityNode 9426 kids *http2priorityNode // start of the kids list 9427 prev, next *http2priorityNode // doubly-linked list of siblings 9428 } 9429 9430 func (n *http2priorityNode) setParent(parent *http2priorityNode) { 9431 if n == parent { 9432 panic("setParent to self") 9433 } 9434 if n.parent == parent { 9435 return 9436 } 9437 // Unlink from current parent. 9438 if parent := n.parent; parent != nil { 9439 if n.prev == nil { 9440 parent.kids = n.next 9441 } else { 9442 n.prev.next = n.next 9443 } 9444 if n.next != nil { 9445 n.next.prev = n.prev 9446 } 9447 } 9448 // Link to new parent. 9449 // If parent=nil, remove n from the tree. 9450 // Always insert at the head of parent.kids (this is assumed by walkReadyInOrder). 9451 n.parent = parent 9452 if parent == nil { 9453 n.next = nil 9454 n.prev = nil 9455 } else { 9456 n.next = parent.kids 9457 n.prev = nil 9458 if n.next != nil { 9459 n.next.prev = n 9460 } 9461 parent.kids = n 9462 } 9463 } 9464 9465 func (n *http2priorityNode) addBytes(b int64) { 9466 n.bytes += b 9467 for ; n != nil; n = n.parent { 9468 n.subtreeBytes += b 9469 } 9470 } 9471 9472 // walkReadyInOrder iterates over the tree in priority order, calling f for each node 9473 // with a non-empty write queue. When f returns true, this funcion returns true and the 9474 // walk halts. tmp is used as scratch space for sorting. 9475 // 9476 // f(n, openParent) takes two arguments: the node to visit, n, and a bool that is true 9477 // if any ancestor p of n is still open (ignoring the root node). 9478 func (n *http2priorityNode) walkReadyInOrder(openParent bool, tmp *[]*http2priorityNode, f func(*http2priorityNode, bool) bool) bool { 9479 if !n.q.empty() && f(n, openParent) { 9480 return true 9481 } 9482 if n.kids == nil { 9483 return false 9484 } 9485 9486 // Don't consider the root "open" when updating openParent since 9487 // we can't send data frames on the root stream (only control frames). 9488 if n.id != 0 { 9489 openParent = openParent || (n.state == http2priorityNodeOpen) 9490 } 9491 9492 // Common case: only one kid or all kids have the same weight. 9493 // Some clients don't use weights; other clients (like web browsers) 9494 // use mostly-linear priority trees. 9495 w := n.kids.weight 9496 needSort := false 9497 for k := n.kids.next; k != nil; k = k.next { 9498 if k.weight != w { 9499 needSort = true 9500 break 9501 } 9502 } 9503 if !needSort { 9504 for k := n.kids; k != nil; k = k.next { 9505 if k.walkReadyInOrder(openParent, tmp, f) { 9506 return true 9507 } 9508 } 9509 return false 9510 } 9511 9512 // Uncommon case: sort the child nodes. We remove the kids from the parent, 9513 // then re-insert after sorting so we can reuse tmp for future sort calls. 9514 *tmp = (*tmp)[:0] 9515 for n.kids != nil { 9516 *tmp = append(*tmp, n.kids) 9517 n.kids.setParent(nil) 9518 } 9519 sort.Sort(http2sortPriorityNodeSiblings(*tmp)) 9520 for i := len(*tmp) - 1; i >= 0; i-- { 9521 (*tmp)[i].setParent(n) // setParent inserts at the head of n.kids 9522 } 9523 for k := n.kids; k != nil; k = k.next { 9524 if k.walkReadyInOrder(openParent, tmp, f) { 9525 return true 9526 } 9527 } 9528 return false 9529 } 9530 9531 type http2sortPriorityNodeSiblings []*http2priorityNode 9532 9533 func (z http2sortPriorityNodeSiblings) Len() int { return len(z) } 9534 9535 func (z http2sortPriorityNodeSiblings) Swap(i, k int) { z[i], z[k] = z[k], z[i] } 9536 9537 func (z http2sortPriorityNodeSiblings) Less(i, k int) bool { 9538 // Prefer the subtree that has sent fewer bytes relative to its weight. 9539 // See sections 5.3.2 and 5.3.4. 9540 wi, bi := float64(z[i].weight+1), float64(z[i].subtreeBytes) 9541 wk, bk := float64(z[k].weight+1), float64(z[k].subtreeBytes) 9542 if bi == 0 && bk == 0 { 9543 return wi >= wk 9544 } 9545 if bk == 0 { 9546 return false 9547 } 9548 return bi/bk <= wi/wk 9549 } 9550 9551 type http2priorityWriteScheduler struct { 9552 // root is the root of the priority tree, where root.id = 0. 9553 // The root queues control frames that are not associated with any stream. 9554 root http2priorityNode 9555 9556 // nodes maps stream ids to priority tree nodes. 9557 nodes map[uint32]*http2priorityNode 9558 9559 // maxID is the maximum stream id in nodes. 9560 maxID uint32 9561 9562 // lists of nodes that have been closed or are idle, but are kept in 9563 // the tree for improved prioritization. When the lengths exceed either 9564 // maxClosedNodesInTree or maxIdleNodesInTree, old nodes are discarded. 9565 closedNodes, idleNodes []*http2priorityNode 9566 9567 // From the config. 9568 maxClosedNodesInTree int 9569 maxIdleNodesInTree int 9570 writeThrottleLimit int32 9571 enableWriteThrottle bool 9572 9573 // tmp is scratch space for priorityNode.walkReadyInOrder to reduce allocations. 9574 tmp []*http2priorityNode 9575 9576 // pool of empty queues for reuse. 9577 queuePool http2writeQueuePool 9578 } 9579 9580 func (ws *http2priorityWriteScheduler) OpenStream(streamID uint32, options http2OpenStreamOptions) { 9581 // The stream may be currently idle but cannot be opened or closed. 9582 if curr := ws.nodes[streamID]; curr != nil { 9583 if curr.state != http2priorityNodeIdle { 9584 panic(fmt.Sprintf("stream %d already opened", streamID)) 9585 } 9586 curr.state = http2priorityNodeOpen 9587 return 9588 } 9589 9590 // RFC 7540, Section 5.3.5: 9591 // "All streams are initially assigned a non-exclusive dependency on stream 0x0. 9592 // Pushed streams initially depend on their associated stream. In both cases, 9593 // streams are assigned a default weight of 16." 9594 parent := ws.nodes[options.PusherID] 9595 if parent == nil { 9596 parent = &ws.root 9597 } 9598 n := &http2priorityNode{ 9599 q: *ws.queuePool.get(), 9600 id: streamID, 9601 weight: http2priorityDefaultWeight, 9602 state: http2priorityNodeOpen, 9603 } 9604 n.setParent(parent) 9605 ws.nodes[streamID] = n 9606 if streamID > ws.maxID { 9607 ws.maxID = streamID 9608 } 9609 } 9610 9611 func (ws *http2priorityWriteScheduler) CloseStream(streamID uint32) { 9612 if streamID == 0 { 9613 panic("violation of WriteScheduler interface: cannot close stream 0") 9614 } 9615 if ws.nodes[streamID] == nil { 9616 panic(fmt.Sprintf("violation of WriteScheduler interface: unknown stream %d", streamID)) 9617 } 9618 if ws.nodes[streamID].state != http2priorityNodeOpen { 9619 panic(fmt.Sprintf("violation of WriteScheduler interface: stream %d already closed", streamID)) 9620 } 9621 9622 n := ws.nodes[streamID] 9623 n.state = http2priorityNodeClosed 9624 n.addBytes(-n.bytes) 9625 9626 q := n.q 9627 ws.queuePool.put(&q) 9628 n.q.s = nil 9629 if ws.maxClosedNodesInTree > 0 { 9630 ws.addClosedOrIdleNode(&ws.closedNodes, ws.maxClosedNodesInTree, n) 9631 } else { 9632 ws.removeNode(n) 9633 } 9634 } 9635 9636 func (ws *http2priorityWriteScheduler) AdjustStream(streamID uint32, priority http2PriorityParam) { 9637 if streamID == 0 { 9638 panic("adjustPriority on root") 9639 } 9640 9641 // If streamID does not exist, there are two cases: 9642 // - A closed stream that has been removed (this will have ID <= maxID) 9643 // - An idle stream that is being used for "grouping" (this will have ID > maxID) 9644 n := ws.nodes[streamID] 9645 if n == nil { 9646 if streamID <= ws.maxID || ws.maxIdleNodesInTree == 0 { 9647 return 9648 } 9649 ws.maxID = streamID 9650 n = &http2priorityNode{ 9651 q: *ws.queuePool.get(), 9652 id: streamID, 9653 weight: http2priorityDefaultWeight, 9654 state: http2priorityNodeIdle, 9655 } 9656 n.setParent(&ws.root) 9657 ws.nodes[streamID] = n 9658 ws.addClosedOrIdleNode(&ws.idleNodes, ws.maxIdleNodesInTree, n) 9659 } 9660 9661 // Section 5.3.1: A dependency on a stream that is not currently in the tree 9662 // results in that stream being given a default priority (Section 5.3.5). 9663 parent := ws.nodes[priority.StreamDep] 9664 if parent == nil { 9665 n.setParent(&ws.root) 9666 n.weight = http2priorityDefaultWeight 9667 return 9668 } 9669 9670 // Ignore if the client tries to make a node its own parent. 9671 if n == parent { 9672 return 9673 } 9674 9675 // Section 5.3.3: 9676 // "If a stream is made dependent on one of its own dependencies, the 9677 // formerly dependent stream is first moved to be dependent on the 9678 // reprioritized stream's previous parent. The moved dependency retains 9679 // its weight." 9680 // 9681 // That is: if parent depends on n, move parent to depend on n.parent. 9682 for x := parent.parent; x != nil; x = x.parent { 9683 if x == n { 9684 parent.setParent(n.parent) 9685 break 9686 } 9687 } 9688 9689 // Section 5.3.3: The exclusive flag causes the stream to become the sole 9690 // dependency of its parent stream, causing other dependencies to become 9691 // dependent on the exclusive stream. 9692 if priority.Exclusive { 9693 k := parent.kids 9694 for k != nil { 9695 next := k.next 9696 if k != n { 9697 k.setParent(n) 9698 } 9699 k = next 9700 } 9701 } 9702 9703 n.setParent(parent) 9704 n.weight = priority.Weight 9705 } 9706 9707 func (ws *http2priorityWriteScheduler) Push(wr http2FrameWriteRequest) { 9708 var n *http2priorityNode 9709 if id := wr.StreamID(); id == 0 { 9710 n = &ws.root 9711 } else { 9712 n = ws.nodes[id] 9713 if n == nil { 9714 // id is an idle or closed stream. wr should not be a HEADERS or 9715 // DATA frame. However, wr can be a RST_STREAM. In this case, we 9716 // push wr onto the root, rather than creating a new priorityNode, 9717 // since RST_STREAM is tiny and the stream's priority is unknown 9718 // anyway. See issue #17919. 9719 if wr.DataSize() > 0 { 9720 panic("add DATA on non-open stream") 9721 } 9722 n = &ws.root 9723 } 9724 } 9725 n.q.push(wr) 9726 } 9727 9728 func (ws *http2priorityWriteScheduler) Pop() (wr http2FrameWriteRequest, ok bool) { 9729 ws.root.walkReadyInOrder(false, &ws.tmp, func(n *http2priorityNode, openParent bool) bool { 9730 limit := int32(math.MaxInt32) 9731 if openParent { 9732 limit = ws.writeThrottleLimit 9733 } 9734 wr, ok = n.q.consume(limit) 9735 if !ok { 9736 return false 9737 } 9738 n.addBytes(int64(wr.DataSize())) 9739 // If B depends on A and B continuously has data available but A 9740 // does not, gradually increase the throttling limit to allow B to 9741 // steal more and more bandwidth from A. 9742 if openParent { 9743 ws.writeThrottleLimit += 1024 9744 if ws.writeThrottleLimit < 0 { 9745 ws.writeThrottleLimit = math.MaxInt32 9746 } 9747 } else if ws.enableWriteThrottle { 9748 ws.writeThrottleLimit = 1024 9749 } 9750 return true 9751 }) 9752 return wr, ok 9753 } 9754 9755 func (ws *http2priorityWriteScheduler) addClosedOrIdleNode(list *[]*http2priorityNode, maxSize int, n *http2priorityNode) { 9756 if maxSize == 0 { 9757 return 9758 } 9759 if len(*list) == maxSize { 9760 // Remove the oldest node, then shift left. 9761 ws.removeNode((*list)[0]) 9762 x := (*list)[1:] 9763 copy(*list, x) 9764 *list = (*list)[:len(x)] 9765 } 9766 *list = append(*list, n) 9767 } 9768 9769 func (ws *http2priorityWriteScheduler) removeNode(n *http2priorityNode) { 9770 for k := n.kids; k != nil; k = k.next { 9771 k.setParent(n.parent) 9772 } 9773 n.setParent(nil) 9774 delete(ws.nodes, n.id) 9775 } 9776 9777 // NewRandomWriteScheduler constructs a WriteScheduler that ignores HTTP/2 9778 // priorities. Control frames like SETTINGS and PING are written before DATA 9779 // frames, but if no control frames are queued and multiple streams have queued 9780 // HEADERS or DATA frames, Pop selects a ready stream arbitrarily. 9781 func http2NewRandomWriteScheduler() http2WriteScheduler { 9782 return &http2randomWriteScheduler{sq: make(map[uint32]*http2writeQueue)} 9783 } 9784 9785 type http2randomWriteScheduler struct { 9786 // zero are frames not associated with a specific stream. 9787 zero http2writeQueue 9788 9789 // sq contains the stream-specific queues, keyed by stream ID. 9790 // When a stream is idle or closed, it's deleted from the map. 9791 sq map[uint32]*http2writeQueue 9792 9793 // pool of empty queues for reuse. 9794 queuePool http2writeQueuePool 9795 } 9796 9797 func (ws *http2randomWriteScheduler) OpenStream(streamID uint32, options http2OpenStreamOptions) { 9798 // no-op: idle streams are not tracked 9799 } 9800 9801 func (ws *http2randomWriteScheduler) CloseStream(streamID uint32) { 9802 q, ok := ws.sq[streamID] 9803 if !ok { 9804 return 9805 } 9806 delete(ws.sq, streamID) 9807 ws.queuePool.put(q) 9808 } 9809 9810 func (ws *http2randomWriteScheduler) AdjustStream(streamID uint32, priority http2PriorityParam) { 9811 // no-op: priorities are ignored 9812 } 9813 9814 func (ws *http2randomWriteScheduler) Push(wr http2FrameWriteRequest) { 9815 id := wr.StreamID() 9816 if id == 0 { 9817 ws.zero.push(wr) 9818 return 9819 } 9820 q, ok := ws.sq[id] 9821 if !ok { 9822 q = ws.queuePool.get() 9823 ws.sq[id] = q 9824 } 9825 q.push(wr) 9826 } 9827 9828 func (ws *http2randomWriteScheduler) Pop() (http2FrameWriteRequest, bool) { 9829 // Control frames first. 9830 if !ws.zero.empty() { 9831 return ws.zero.shift(), true 9832 } 9833 // Iterate over all non-idle streams until finding one that can be consumed. 9834 for _, q := range ws.sq { 9835 if wr, ok := q.consume(math.MaxInt32); ok { 9836 return wr, true 9837 } 9838 } 9839 return http2FrameWriteRequest{}, false 9840 }