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