github.com/ActiveState/go@v0.0.0-20170614201249-0b81c023a722/src/net/http/h2_bundle.go (about)

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