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