Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/http/h2_bundle.go

Documentation: net/http

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

View as plain text