github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/net/http/h2_bundle.go (about)

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