2015-01-18 15:50:20 +01:00
|
|
|
package nghttp2
|
|
|
|
|
|
|
|
import (
|
2015-01-20 14:19:28 +01:00
|
|
|
"bufio"
|
2015-01-18 15:50:20 +01:00
|
|
|
"bytes"
|
2015-01-19 16:45:51 +01:00
|
|
|
"crypto/tls"
|
2015-01-18 15:50:20 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/bradfitz/http2"
|
|
|
|
"github.com/bradfitz/http2/hpack"
|
2015-01-19 16:45:51 +01:00
|
|
|
"github.com/tatsuhiro-t/go-nghttp2"
|
2015-01-20 17:03:56 +01:00
|
|
|
"golang.org/x/net/spdy"
|
2015-01-20 14:19:28 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-01-18 15:50:20 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
serverBin = buildDir + "/src/nghttpx"
|
|
|
|
serverPort = 3009
|
2015-01-20 17:03:56 +01:00
|
|
|
testDir = buildDir + "/integration-tests"
|
2015-01-18 15:50:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func pair(name, value string) hpack.HeaderField {
|
|
|
|
return hpack.HeaderField{
|
|
|
|
Name: name,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverTester struct {
|
|
|
|
args []string // command-line arguments
|
|
|
|
cmd *exec.Cmd // test frontend server process, which is test subject
|
2015-01-20 14:19:28 +01:00
|
|
|
url string // test frontend server URL
|
2015-01-18 15:50:20 +01:00
|
|
|
t *testing.T
|
|
|
|
ts *httptest.Server // backend server
|
2015-02-03 17:15:56 +01:00
|
|
|
backendHost string // backend server host
|
2015-01-18 15:50:20 +01:00
|
|
|
conn net.Conn // connection to frontend server
|
|
|
|
h2PrefaceSent bool // HTTP/2 preface was sent in conn
|
|
|
|
nextStreamID uint32 // next stream ID
|
2015-01-20 17:03:56 +01:00
|
|
|
fr *http2.Framer // HTTP/2 framer
|
|
|
|
spdyFr *spdy.Framer // SPDY/3.1 framer
|
|
|
|
headerBlkBuf bytes.Buffer // buffer to store encoded header block
|
|
|
|
enc *hpack.Encoder // HTTP/2 HPACK encoder
|
|
|
|
header http.Header // received header fields
|
|
|
|
dec *hpack.Decoder // HTTP/2 HPACK decoder
|
|
|
|
authority string // server's host:port
|
|
|
|
frCh chan http2.Frame // used for incoming HTTP/2 frame
|
|
|
|
spdyFrCh chan spdy.Frame // used for incoming SPDY frame
|
2015-01-18 15:50:20 +01:00
|
|
|
errCh chan error
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:03:56 +01:00
|
|
|
// newServerTester creates test context for plain TCP frontend
|
|
|
|
// connection.
|
2015-01-18 15:50:20 +01:00
|
|
|
func newServerTester(args []string, t *testing.T, handler http.HandlerFunc) *serverTester {
|
2015-01-25 07:36:14 +01:00
|
|
|
return newServerTesterInternal(args, t, handler, false, nil)
|
2015-01-20 17:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newServerTester creates test context for TLS frontend connection.
|
|
|
|
func newServerTesterTLS(args []string, t *testing.T, handler http.HandlerFunc) *serverTester {
|
2015-01-25 07:36:14 +01:00
|
|
|
return newServerTesterInternal(args, t, handler, true, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newServerTester creates test context for TLS frontend connection
|
|
|
|
// with given clientConfig
|
|
|
|
func newServerTesterTLSConfig(args []string, t *testing.T, handler http.HandlerFunc, clientConfig *tls.Config) *serverTester {
|
|
|
|
return newServerTesterInternal(args, t, handler, true, clientConfig)
|
2015-01-20 17:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newServerTesterInternal creates test context. If frontendTLS is
|
|
|
|
// true, set up TLS frontend connection.
|
2015-01-25 07:36:14 +01:00
|
|
|
func newServerTesterInternal(args []string, t *testing.T, handler http.HandlerFunc, frontendTLS bool, clientConfig *tls.Config) *serverTester {
|
2015-01-19 16:45:51 +01:00
|
|
|
ts := httptest.NewUnstartedServer(handler)
|
2015-01-18 15:50:20 +01:00
|
|
|
|
2015-01-19 16:45:51 +01:00
|
|
|
backendTLS := false
|
|
|
|
for _, k := range args {
|
2015-01-20 17:03:56 +01:00
|
|
|
switch k {
|
|
|
|
case "--http2-bridge":
|
2015-01-19 16:45:51 +01:00
|
|
|
backendTLS = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if backendTLS {
|
|
|
|
nghttp2.ConfigureServer(ts.Config, &nghttp2.Server{})
|
|
|
|
// According to httptest/server.go, we have to set
|
|
|
|
// NextProtos separately for ts.TLS. NextProtos set
|
|
|
|
// in nghttp2.ConfigureServer is effectively ignored.
|
|
|
|
ts.TLS = new(tls.Config)
|
|
|
|
ts.TLS.NextProtos = append(ts.TLS.NextProtos, "h2-14")
|
|
|
|
ts.StartTLS()
|
|
|
|
args = append(args, "-k")
|
|
|
|
} else {
|
|
|
|
ts.Start()
|
|
|
|
}
|
2015-01-20 17:03:56 +01:00
|
|
|
scheme := "http"
|
|
|
|
if frontendTLS {
|
|
|
|
scheme = "https"
|
|
|
|
args = append(args, testDir+"/server.key", testDir+"/server.crt")
|
|
|
|
} else {
|
|
|
|
args = append(args, "--frontend-no-tls")
|
|
|
|
}
|
|
|
|
|
|
|
|
backendURL, err := url.Parse(ts.URL)
|
2015-01-18 15:50:20 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error parsing URL from httptest.Server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL.Host looks like "127.0.0.1:8080", but we want
|
|
|
|
// "127.0.0.1,8080"
|
2015-01-20 17:03:56 +01:00
|
|
|
b := "-b" + strings.Replace(backendURL.Host, ":", ",", -1)
|
2015-01-18 15:50:20 +01:00
|
|
|
args = append(args, fmt.Sprintf("-f127.0.0.1,%v", serverPort), b,
|
2015-01-20 17:03:56 +01:00
|
|
|
"--errorlog-file="+testDir+"/log.txt", "-LINFO")
|
|
|
|
|
|
|
|
authority := fmt.Sprintf("127.0.0.1:%v", serverPort)
|
2015-01-18 15:50:20 +01:00
|
|
|
|
|
|
|
st := &serverTester{
|
|
|
|
cmd: exec.Command(serverBin, args...),
|
|
|
|
t: t,
|
|
|
|
ts: ts,
|
2015-01-20 17:03:56 +01:00
|
|
|
url: fmt.Sprintf("%v://%v", scheme, authority),
|
2015-02-03 17:15:56 +01:00
|
|
|
backendHost: backendURL.Host,
|
2015-01-18 15:50:20 +01:00
|
|
|
nextStreamID: 1,
|
2015-01-20 17:03:56 +01:00
|
|
|
authority: authority,
|
2015-01-18 15:50:20 +01:00
|
|
|
frCh: make(chan http2.Frame),
|
2015-01-20 17:03:56 +01:00
|
|
|
spdyFrCh: make(chan spdy.Frame),
|
2015-01-18 15:50:20 +01:00
|
|
|
errCh: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.cmd.Start(); err != nil {
|
|
|
|
st.t.Fatalf("Error starting %v: %v", serverBin, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
retry := 0
|
|
|
|
for {
|
2015-01-20 17:03:56 +01:00
|
|
|
var conn net.Conn
|
|
|
|
var err error
|
|
|
|
if frontendTLS {
|
2015-01-25 07:36:14 +01:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if clientConfig == nil {
|
|
|
|
tlsConfig = new(tls.Config)
|
|
|
|
} else {
|
|
|
|
tlsConfig = clientConfig
|
2015-01-20 17:03:56 +01:00
|
|
|
}
|
2015-01-25 07:36:14 +01:00
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
tlsConfig.NextProtos = []string{"h2-14", "spdy/3.1"}
|
2015-01-20 17:03:56 +01:00
|
|
|
conn, err = tls.Dial("tcp", authority, tlsConfig)
|
|
|
|
} else {
|
|
|
|
conn, err = net.Dial("tcp", authority)
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
if err != nil {
|
|
|
|
retry += 1
|
2015-01-20 13:19:52 +01:00
|
|
|
if retry >= 100 {
|
2015-01-20 17:03:56 +01:00
|
|
|
st.Close()
|
2015-01-18 15:50:20 +01:00
|
|
|
st.t.Fatalf("Error server is not responding too long; server command-line arguments may be invalid")
|
|
|
|
}
|
2015-01-19 13:25:59 +01:00
|
|
|
time.Sleep(150 * time.Millisecond)
|
2015-01-18 15:50:20 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-01-20 17:03:56 +01:00
|
|
|
if frontendTLS {
|
|
|
|
tlsConn := conn.(*tls.Conn)
|
|
|
|
cs := tlsConn.ConnectionState()
|
|
|
|
if !cs.NegotiatedProtocolIsMutual {
|
|
|
|
st.Close()
|
|
|
|
st.t.Fatalf("Error negotiated next protocol is not mutual")
|
|
|
|
}
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
st.conn = conn
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
st.fr = http2.NewFramer(st.conn, st.conn)
|
2015-01-20 17:03:56 +01:00
|
|
|
spdyFr, err := spdy.NewFramer(st.conn, st.conn)
|
|
|
|
if err != nil {
|
|
|
|
st.Close()
|
|
|
|
st.t.Fatalf("Error spdy.NewFramer: %v", err)
|
|
|
|
}
|
|
|
|
st.spdyFr = spdyFr
|
2015-01-18 15:50:20 +01:00
|
|
|
st.enc = hpack.NewEncoder(&st.headerBlkBuf)
|
|
|
|
st.dec = hpack.NewDecoder(4096, func(f hpack.HeaderField) {
|
|
|
|
st.header.Add(f.Name, f.Value)
|
|
|
|
})
|
|
|
|
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *serverTester) Close() {
|
|
|
|
if st.conn != nil {
|
|
|
|
st.conn.Close()
|
|
|
|
}
|
|
|
|
if st.cmd != nil {
|
|
|
|
st.cmd.Process.Kill()
|
|
|
|
st.cmd.Wait()
|
|
|
|
}
|
|
|
|
if st.ts != nil {
|
|
|
|
st.ts.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *serverTester) readFrame() (http2.Frame, error) {
|
|
|
|
go func() {
|
|
|
|
f, err := st.fr.ReadFrame()
|
|
|
|
if err != nil {
|
|
|
|
st.errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
st.frCh <- f
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case f := <-st.frCh:
|
|
|
|
return f, nil
|
|
|
|
case err := <-st.errCh:
|
|
|
|
return nil, err
|
2015-01-21 17:43:56 +01:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-01-18 15:50:20 +01:00
|
|
|
return nil, errors.New("timeout waiting for frame")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:03:56 +01:00
|
|
|
func (st *serverTester) readSpdyFrame() (spdy.Frame, error) {
|
|
|
|
go func() {
|
|
|
|
f, err := st.spdyFr.ReadFrame()
|
|
|
|
if err != nil {
|
|
|
|
st.errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
st.spdyFrCh <- f
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case f := <-st.spdyFrCh:
|
|
|
|
return f, nil
|
|
|
|
case err := <-st.errCh:
|
|
|
|
return nil, err
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
return nil, errors.New("timeout waiting for frame")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:50:20 +01:00
|
|
|
type requestParam struct {
|
2015-01-19 13:24:18 +01:00
|
|
|
name string // name for this request to identify the request in log easily
|
|
|
|
streamID uint32 // stream ID, automatically assigned if 0
|
|
|
|
method string // method, defaults to GET
|
|
|
|
scheme string // scheme, defaults to http
|
|
|
|
authority string // authority, defaults to backend server address
|
|
|
|
path string // path, defaults to /
|
|
|
|
header []hpack.HeaderField // additional request header fields
|
|
|
|
body []byte // request body
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-20 14:19:28 +01:00
|
|
|
func (st *serverTester) http1(rp requestParam) (*serverResponse, error) {
|
|
|
|
method := "GET"
|
|
|
|
if rp.method != "" {
|
|
|
|
method = rp.method
|
|
|
|
}
|
|
|
|
|
|
|
|
var body io.Reader
|
|
|
|
if rp.body != nil {
|
|
|
|
body = bytes.NewBuffer(rp.body)
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, st.url, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, h := range rp.header {
|
|
|
|
req.Header.Add(h.Name, h.Value)
|
|
|
|
}
|
|
|
|
req.Header.Add("Test-Case", rp.name)
|
|
|
|
|
|
|
|
if err := req.Write(st.conn); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(st.conn), req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
res := &serverResponse{
|
2015-01-22 15:46:35 +01:00
|
|
|
status: resp.StatusCode,
|
|
|
|
header: resp.Header,
|
|
|
|
body: respBody,
|
|
|
|
connClose: resp.Close,
|
2015-01-20 14:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:03:56 +01:00
|
|
|
func (st *serverTester) spdy(rp requestParam) (*serverResponse, error) {
|
|
|
|
res := &serverResponse{}
|
|
|
|
|
|
|
|
var id spdy.StreamId
|
|
|
|
if rp.streamID != 0 {
|
|
|
|
id = spdy.StreamId(rp.streamID)
|
|
|
|
if id >= spdy.StreamId(st.nextStreamID) && id%2 == 1 {
|
|
|
|
st.nextStreamID = uint32(id) + 2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
id = spdy.StreamId(st.nextStreamID)
|
|
|
|
st.nextStreamID += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
|
|
|
if rp.method != "" {
|
|
|
|
method = rp.method
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme := "http"
|
|
|
|
if rp.scheme != "" {
|
|
|
|
scheme = rp.scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
host := st.authority
|
|
|
|
if rp.authority != "" {
|
|
|
|
host = rp.authority
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "/"
|
|
|
|
if rp.path != "" {
|
|
|
|
path = rp.path
|
|
|
|
}
|
|
|
|
|
|
|
|
header := make(http.Header)
|
|
|
|
header.Add(":method", method)
|
|
|
|
header.Add(":scheme", scheme)
|
|
|
|
header.Add(":host", host)
|
|
|
|
header.Add(":path", path)
|
|
|
|
header.Add(":version", "HTTP/1.1")
|
|
|
|
header.Add("test-case", rp.name)
|
|
|
|
for _, h := range rp.header {
|
|
|
|
header.Add(h.Name, h.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var synStreamFlags spdy.ControlFlags
|
|
|
|
if len(rp.body) == 0 {
|
|
|
|
synStreamFlags = spdy.ControlFlagFin
|
|
|
|
}
|
|
|
|
if err := st.spdyFr.WriteFrame(&spdy.SynStreamFrame{
|
|
|
|
CFHeader: spdy.ControlFrameHeader{
|
|
|
|
Flags: synStreamFlags,
|
|
|
|
},
|
|
|
|
StreamId: id,
|
|
|
|
Headers: header,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rp.body) != 0 {
|
|
|
|
if err := st.spdyFr.WriteFrame(&spdy.DataFrame{
|
|
|
|
StreamId: id,
|
|
|
|
Flags: spdy.DataFlagFin,
|
|
|
|
Data: rp.body,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
fr, err := st.readSpdyFrame()
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
switch f := fr.(type) {
|
|
|
|
case *spdy.SynReplyFrame:
|
|
|
|
if f.StreamId != id {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.header = cloneHeader(f.Headers)
|
|
|
|
if _, err := fmt.Sscan(res.header.Get(":status"), &res.status); err != nil {
|
|
|
|
return res, fmt.Errorf("Error parsing status code: %v", err)
|
|
|
|
}
|
|
|
|
if f.CFHeader.Flags&spdy.ControlFlagFin != 0 {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
case *spdy.DataFrame:
|
|
|
|
if f.StreamId != id {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.body = append(res.body, f.Data...)
|
|
|
|
if f.Flags&spdy.DataFlagFin != 0 {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
case *spdy.RstStreamFrame:
|
|
|
|
if f.StreamId != id {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.spdyRstErrCode = f.Status
|
|
|
|
break loop
|
|
|
|
case *spdy.GoAwayFrame:
|
|
|
|
if f.Status == spdy.GoAwayOK {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.spdyGoAwayErrCode = f.Status
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:50:20 +01:00
|
|
|
func (st *serverTester) http2(rp requestParam) (*serverResponse, error) {
|
|
|
|
res := &serverResponse{}
|
|
|
|
st.headerBlkBuf.Reset()
|
|
|
|
st.header = make(http.Header)
|
|
|
|
|
|
|
|
var id uint32
|
|
|
|
if rp.streamID != 0 {
|
|
|
|
id = rp.streamID
|
|
|
|
if id >= st.nextStreamID && id%2 == 1 {
|
|
|
|
st.nextStreamID = id + 2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
id = st.nextStreamID
|
|
|
|
st.nextStreamID += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if !st.h2PrefaceSent {
|
|
|
|
st.h2PrefaceSent = true
|
|
|
|
fmt.Fprint(st.conn, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
|
|
|
|
if err := st.fr.WriteSettings(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
|
|
|
if rp.method != "" {
|
|
|
|
method = rp.method
|
|
|
|
}
|
|
|
|
_ = st.enc.WriteField(pair(":method", method))
|
|
|
|
|
|
|
|
scheme := "http"
|
|
|
|
if rp.scheme != "" {
|
|
|
|
scheme = rp.scheme
|
|
|
|
}
|
|
|
|
_ = st.enc.WriteField(pair(":scheme", scheme))
|
|
|
|
|
|
|
|
authority := st.authority
|
|
|
|
if rp.authority != "" {
|
|
|
|
authority = rp.authority
|
|
|
|
}
|
|
|
|
_ = st.enc.WriteField(pair(":authority", authority))
|
|
|
|
|
|
|
|
path := "/"
|
|
|
|
if rp.path != "" {
|
|
|
|
path = rp.path
|
|
|
|
}
|
|
|
|
_ = st.enc.WriteField(pair(":path", path))
|
|
|
|
|
|
|
|
_ = st.enc.WriteField(pair("test-case", rp.name))
|
|
|
|
|
2015-01-19 13:24:18 +01:00
|
|
|
for _, h := range rp.header {
|
2015-01-21 17:43:56 +01:00
|
|
|
_ = st.enc.WriteField(h)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err := st.fr.WriteHeaders(http2.HeadersFrameParam{
|
|
|
|
StreamID: id,
|
|
|
|
EndStream: len(rp.body) == 0,
|
|
|
|
EndHeaders: true,
|
|
|
|
BlockFragment: st.headerBlkBuf.Bytes(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rp.body) != 0 {
|
|
|
|
// TODO we assume rp.body fits in 1 frame
|
|
|
|
if err := st.fr.WriteData(id, true, rp.body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
fr, err := st.readFrame()
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
switch f := fr.(type) {
|
|
|
|
case *http2.HeadersFrame:
|
|
|
|
_, err := st.dec.Write(f.HeaderBlockFragment())
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if f.FrameHeader.StreamID != id {
|
|
|
|
st.header = make(http.Header)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.header = cloneHeader(st.header)
|
2015-01-20 17:03:56 +01:00
|
|
|
var status int
|
|
|
|
status, err = strconv.Atoi(res.header.Get(":status"))
|
2015-01-18 15:50:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("Error parsing status code: %v", err)
|
|
|
|
}
|
2015-01-20 17:03:56 +01:00
|
|
|
res.status = status
|
2015-01-18 15:50:20 +01:00
|
|
|
if f.StreamEnded() {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
case *http2.DataFrame:
|
|
|
|
if f.FrameHeader.StreamID != id {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.body = append(res.body, f.Data()...)
|
|
|
|
if f.StreamEnded() {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
case *http2.RSTStreamFrame:
|
|
|
|
if f.FrameHeader.StreamID != id {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.errCode = f.ErrCode
|
|
|
|
break loop
|
|
|
|
case *http2.GoAwayFrame:
|
2015-01-20 17:03:56 +01:00
|
|
|
if f.ErrCode == http2.ErrCodeNo {
|
2015-01-18 15:50:20 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
res.errCode = f.ErrCode
|
|
|
|
res.connErr = true
|
|
|
|
break loop
|
|
|
|
case *http2.SettingsFrame:
|
|
|
|
if f.IsAck() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := st.fr.WriteSettingsAck(); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2015-01-20 17:03:56 +01:00
|
|
|
// TODO handle PUSH_PROMISE as well, since it alters HPACK context
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverResponse struct {
|
2015-01-20 17:03:56 +01:00
|
|
|
status int // HTTP status code
|
|
|
|
header http.Header // response header fields
|
|
|
|
body []byte // response body
|
|
|
|
errCode http2.ErrCode // error code received in HTTP/2 RST_STREAM or GOAWAY
|
|
|
|
connErr bool // true if HTTP/2 connection error
|
|
|
|
spdyGoAwayErrCode spdy.GoAwayStatus // status code received in SPDY RST_STREAM
|
|
|
|
spdyRstErrCode spdy.RstStreamStatus // status code received in SPDY GOAWAY
|
2015-01-22 15:46:35 +01:00
|
|
|
connClose bool // Conection: close is included in response header in HTTP/1 test
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cloneHeader(h http.Header) http.Header {
|
|
|
|
h2 := make(http.Header, len(h))
|
|
|
|
for k, vv := range h {
|
|
|
|
vv2 := make([]string, len(vv))
|
|
|
|
copy(vv2, vv)
|
|
|
|
h2[k] = vv2
|
|
|
|
}
|
|
|
|
return h2
|
|
|
|
}
|
|
|
|
|
|
|
|
func noopHandler(w http.ResponseWriter, r *http.Request) {}
|