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"
|
2020-04-18 09:31:28 +02:00
|
|
|
"encoding/binary"
|
2015-01-18 15:50:20 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-01-20 14:19:28 +01:00
|
|
|
"io"
|
2015-01-18 15:50:20 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2017-02-18 14:29:25 +01:00
|
|
|
"os"
|
2015-01-18 15:50:20 +01:00
|
|
|
"os/exec"
|
2015-02-08 07:56:34 +01:00
|
|
|
"sort"
|
2015-01-18 15:50:20 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-09-24 16:37:01 +02:00
|
|
|
"syscall"
|
2015-01-18 15:50:20 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
2022-05-24 14:56:38 +02:00
|
|
|
|
|
|
|
"github.com/tatsuhiro-t/go-nghttp2"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/hpack"
|
|
|
|
"golang.org/x/net/websocket"
|
2015-01-18 15:50:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
serverBin = buildDir + "/src/nghttpx"
|
|
|
|
serverPort = 3009
|
2016-02-13 20:03:00 +01:00
|
|
|
testDir = sourceDir + "/integration-tests"
|
2016-02-28 16:37:51 +01:00
|
|
|
logDir = 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:41:20 +01:00
|
|
|
frontendHost string // frontend server host
|
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
|
|
|
|
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
|
2015-01-18 15:50:20 +01:00
|
|
|
errCh chan error
|
|
|
|
}
|
|
|
|
|
2022-05-24 14:47:24 +02:00
|
|
|
type options struct {
|
|
|
|
// args is the additional arguments to nghttpx.
|
|
|
|
args []string
|
|
|
|
// handler is the handler to handle the request. It defaults
|
|
|
|
// to noopHandler.
|
|
|
|
handler http.HandlerFunc
|
|
|
|
// connectPort is the server side port where client connection
|
|
|
|
// is made. It defaults to serverPort.
|
|
|
|
connectPort int
|
|
|
|
// tls, if set to true, sets up TLS frontend connection.
|
|
|
|
tls bool
|
|
|
|
// tlsConfig is the client side TLS configuration that is used
|
|
|
|
// when tls is true.
|
|
|
|
tlsConfig *tls.Config
|
2016-06-05 15:51:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 14:47:24 +02:00
|
|
|
// newServerTester creates test context.
|
|
|
|
func newServerTester(t *testing.T, opts options) *serverTester {
|
|
|
|
if opts.handler == nil {
|
|
|
|
opts.handler = noopHandler
|
|
|
|
}
|
|
|
|
if opts.connectPort == 0 {
|
|
|
|
opts.connectPort = serverPort
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
|
2022-05-24 14:47:24 +02:00
|
|
|
ts := httptest.NewUnstartedServer(opts.handler)
|
2016-02-28 16:37:51 +01:00
|
|
|
|
2022-05-24 14:47:24 +02:00
|
|
|
var args []string
|
2017-10-27 16:16:11 +02:00
|
|
|
var backendTLS, dns, externalDNS, acceptProxyProtocol, redirectIfNotTLS, affinityCookie, alpnH1 bool
|
2017-02-18 13:47:01 +01:00
|
|
|
|
2022-05-24 14:47:24 +02:00
|
|
|
for _, k := range opts.args {
|
2015-01-20 17:03:56 +01:00
|
|
|
switch k {
|
|
|
|
case "--http2-bridge":
|
2015-01-19 16:45:51 +01:00
|
|
|
backendTLS = true
|
2016-12-11 03:32:38 +01:00
|
|
|
case "--dns":
|
|
|
|
dns = true
|
|
|
|
case "--external-dns":
|
|
|
|
dns = true
|
|
|
|
externalDNS = true
|
2017-01-03 06:14:37 +01:00
|
|
|
case "--accept-proxy-protocol":
|
|
|
|
acceptProxyProtocol = true
|
2017-02-18 13:47:01 +01:00
|
|
|
case "--redirect-if-not-tls":
|
|
|
|
redirectIfNotTLS = true
|
2017-10-27 16:16:11 +02:00
|
|
|
case "--affinity-cookie":
|
|
|
|
affinityCookie = true
|
|
|
|
case "--alpn-h1":
|
|
|
|
alpnH1 = true
|
2016-02-28 16:37:51 +01:00
|
|
|
default:
|
|
|
|
args = append(args, k)
|
2015-01-19 16:45:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
2016-02-28 16:37:51 +01:00
|
|
|
ts.TLS.NextProtos = append(ts.TLS.NextProtos, "h2")
|
2015-01-19 16:45:51 +01:00
|
|
|
ts.StartTLS()
|
2016-12-11 03:32:38 +01:00
|
|
|
args = append(args, "-k")
|
2015-01-19 16:45:51 +01:00
|
|
|
} else {
|
|
|
|
ts.Start()
|
|
|
|
}
|
2015-01-20 17:03:56 +01:00
|
|
|
scheme := "http"
|
2022-05-24 14:47:24 +02:00
|
|
|
if opts.tls {
|
2015-01-20 17:03:56 +01:00
|
|
|
scheme = "https"
|
|
|
|
args = append(args, testDir+"/server.key", testDir+"/server.crt")
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2016-12-11 03:32:38 +01:00
|
|
|
b := "-b"
|
|
|
|
if !externalDNS {
|
|
|
|
b += fmt.Sprintf("%v;", strings.Replace(backendURL.Host, ":", ",", -1))
|
|
|
|
} else {
|
|
|
|
sep := strings.LastIndex(backendURL.Host, ":")
|
|
|
|
if sep == -1 {
|
|
|
|
t.Fatalf("backendURL.Host %v does not contain separator ':'", backendURL.Host)
|
|
|
|
}
|
2017-04-09 09:06:37 +02:00
|
|
|
// We use awesome service nip.io.
|
|
|
|
b += fmt.Sprintf("%v.nip.io,%v;", backendURL.Host[:sep], backendURL.Host[sep+1:])
|
2016-12-11 03:32:38 +01:00
|
|
|
}
|
|
|
|
|
2016-02-28 16:37:51 +01:00
|
|
|
if backendTLS {
|
2016-12-11 03:32:38 +01:00
|
|
|
b += ";proto=h2;tls"
|
|
|
|
}
|
|
|
|
if dns {
|
|
|
|
b += ";dns"
|
2016-02-28 16:37:51 +01:00
|
|
|
}
|
2016-03-24 16:56:20 +01:00
|
|
|
|
2017-02-18 13:47:01 +01:00
|
|
|
if redirectIfNotTLS {
|
|
|
|
b += ";redirect-if-not-tls"
|
|
|
|
}
|
|
|
|
|
2017-10-27 16:16:11 +02:00
|
|
|
if affinityCookie {
|
|
|
|
b += ";affinity=cookie;affinity-cookie-name=affinity;affinity-cookie-path=/foo/bar"
|
|
|
|
}
|
|
|
|
|
2017-01-03 06:14:37 +01:00
|
|
|
noTLS := ";no-tls"
|
2022-05-24 14:47:24 +02:00
|
|
|
if opts.tls {
|
2016-03-24 16:56:20 +01:00
|
|
|
noTLS = ""
|
|
|
|
}
|
|
|
|
|
2017-01-03 06:14:37 +01:00
|
|
|
var proxyProto string
|
|
|
|
if acceptProxyProtocol {
|
|
|
|
proxyProto = ";proxyproto"
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, fmt.Sprintf("-f127.0.0.1,%v%v%v", serverPort, noTLS, proxyProto), b,
|
2016-02-13 20:03:00 +01:00
|
|
|
"--errorlog-file="+logDir+"/log.txt", "-LINFO")
|
2015-01-20 17:03:56 +01:00
|
|
|
|
2022-05-24 14:47:24 +02:00
|
|
|
authority := fmt.Sprintf("127.0.0.1:%v", opts.connectPort)
|
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:41:20 +01:00
|
|
|
frontendHost: fmt.Sprintf("127.0.0.1:%v", serverPort),
|
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),
|
|
|
|
errCh: make(chan error),
|
|
|
|
}
|
|
|
|
|
2017-02-18 14:29:25 +01:00
|
|
|
st.cmd.Stdout = os.Stdout
|
|
|
|
st.cmd.Stderr = os.Stderr
|
|
|
|
|
2015-01-18 15:50:20 +01:00
|
|
|
if err := st.cmd.Start(); err != nil {
|
|
|
|
st.t.Fatalf("Error starting %v: %v", serverBin, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
retry := 0
|
|
|
|
for {
|
2016-06-23 15:21:12 +02:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
2015-01-20 17:03:56 +01:00
|
|
|
var conn net.Conn
|
|
|
|
var err error
|
2022-05-24 14:47:24 +02:00
|
|
|
if opts.tls {
|
2015-01-25 07:36:14 +01:00
|
|
|
var tlsConfig *tls.Config
|
2022-05-24 14:47:24 +02:00
|
|
|
if opts.tlsConfig == nil {
|
2015-01-25 07:36:14 +01:00
|
|
|
tlsConfig = new(tls.Config)
|
|
|
|
} else {
|
2022-05-24 14:47:24 +02:00
|
|
|
tlsConfig = opts.tlsConfig.Clone()
|
2015-01-20 17:03:56 +01:00
|
|
|
}
|
2015-01-25 07:36:14 +01:00
|
|
|
tlsConfig.InsecureSkipVerify = true
|
2017-10-27 16:16:11 +02:00
|
|
|
if alpnH1 {
|
|
|
|
tlsConfig.NextProtos = []string{"http/1.1"}
|
|
|
|
} else {
|
2018-05-31 02:45:20 +02:00
|
|
|
tlsConfig.NextProtos = []string{"h2"}
|
2017-10-27 16:16:11 +02:00
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-05-24 14:47:24 +02:00
|
|
|
if opts.tls {
|
2015-01-20 17:03:56 +01:00
|
|
|
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)
|
|
|
|
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 {
|
2015-09-24 16:37:01 +02:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
st.cmd.Wait()
|
2017-04-05 16:57:33 +02:00
|
|
|
close(done)
|
2015-09-24 16:37:01 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
st.cmd.Process.Signal(syscall.SIGQUIT)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
st.cmd.Process.Kill()
|
|
|
|
<-done
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestParam struct {
|
2015-04-10 14:28:12 +02: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
|
|
|
|
trailer []hpack.HeaderField // trailer part
|
|
|
|
httpUpgrade bool // true if upgraded to HTTP/2 through HTTP Upgrade
|
2016-06-23 15:21:12 +02:00
|
|
|
noEndStream bool // true if END_STREAM should not be sent
|
2015-03-08 11:31:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrapper for request body to set trailer part
|
|
|
|
type chunkedBodyReader struct {
|
|
|
|
trailer []hpack.HeaderField
|
|
|
|
trailerWritten bool
|
|
|
|
body io.Reader
|
|
|
|
req *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cbr *chunkedBodyReader) Read(p []byte) (n int, err error) {
|
|
|
|
// document says that we have to set http.Request.Trailer
|
|
|
|
// after request was sent and before body returns EOF.
|
|
|
|
if !cbr.trailerWritten {
|
|
|
|
cbr.trailerWritten = true
|
|
|
|
for _, h := range cbr.trailer {
|
|
|
|
cbr.req.Trailer.Set(h.Name, h.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cbr.body.Read(p)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2015-05-26 16:28:45 +02:00
|
|
|
func (st *serverTester) websocket(rp requestParam) (*serverResponse, error) {
|
|
|
|
urlstring := st.url + "/echo"
|
|
|
|
|
|
|
|
config, err := websocket.NewConfig(urlstring, st.url)
|
|
|
|
if err != nil {
|
|
|
|
st.t.Fatalf("websocket.NewConfig(%q, %q) returned error: %v", urlstring, st.url, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Header.Add("Test-Case", rp.name)
|
|
|
|
for _, h := range rp.header {
|
|
|
|
config.Header.Add(h.Name, h.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := websocket.NewClient(config, st.conn)
|
|
|
|
if err != nil {
|
|
|
|
st.t.Fatalf("Error creating websocket client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ws.Write(rp.body); err != nil {
|
|
|
|
st.t.Fatalf("ws.Write() returned error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := make([]byte, 1024)
|
|
|
|
var n int
|
|
|
|
if n, err = ws.Read(msg); err != nil {
|
|
|
|
st.t.Fatalf("ws.Read() returned error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &serverResponse{
|
|
|
|
body: msg[:n],
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
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
|
2015-03-08 11:31:43 +01:00
|
|
|
var cbr *chunkedBodyReader
|
2015-01-20 14:19:28 +01:00
|
|
|
if rp.body != nil {
|
|
|
|
body = bytes.NewBuffer(rp.body)
|
2015-03-08 11:31:43 +01:00
|
|
|
if len(rp.trailer) != 0 {
|
|
|
|
cbr = &chunkedBodyReader{
|
|
|
|
trailer: rp.trailer,
|
|
|
|
body: body,
|
|
|
|
}
|
|
|
|
body = cbr
|
|
|
|
}
|
2015-01-20 14:19:28 +01:00
|
|
|
}
|
2015-04-29 15:54:25 +02:00
|
|
|
|
|
|
|
reqURL := st.url
|
|
|
|
|
|
|
|
if rp.path != "" {
|
|
|
|
u, err := url.Parse(st.url)
|
|
|
|
if err != nil {
|
|
|
|
st.t.Fatalf("Error parsing URL from st.url %v: %v", st.url, err)
|
|
|
|
}
|
2016-06-13 15:11:26 +02:00
|
|
|
u.Path = ""
|
|
|
|
u.RawQuery = ""
|
|
|
|
reqURL = u.String() + rp.path
|
2015-04-29 15:54:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, reqURL, body)
|
2015-01-20 14:19:28 +01:00
|
|
|
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)
|
2015-03-08 11:31:43 +01:00
|
|
|
if cbr != nil {
|
|
|
|
cbr.req = req
|
|
|
|
// this makes request use chunked encoding
|
|
|
|
req.ContentLength = -1
|
|
|
|
req.Trailer = make(http.Header)
|
|
|
|
for _, h := range cbr.trailer {
|
|
|
|
req.Trailer.Set(h.Name, "")
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 14:19:28 +01:00
|
|
|
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
|
|
|
|
}
|
2022-05-24 11:52:30 +02:00
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
2015-01-20 14:19:28 +01:00
|
|
|
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-18 15:50:20 +01:00
|
|
|
func (st *serverTester) http2(rp requestParam) (*serverResponse, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 07:56:34 +01:00
|
|
|
res := &serverResponse{
|
|
|
|
streamID: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
streams := make(map[uint32]*serverResponse)
|
|
|
|
streams[id] = res
|
|
|
|
|
2015-04-10 14:28:12 +02:00
|
|
|
if !rp.httpUpgrade {
|
|
|
|
method := "GET"
|
|
|
|
if rp.method != "" {
|
|
|
|
method = rp.method
|
|
|
|
}
|
|
|
|
_ = st.enc.WriteField(pair(":method", method))
|
2015-01-18 15:50:20 +01:00
|
|
|
|
2015-04-10 14:28:12 +02:00
|
|
|
scheme := "http"
|
|
|
|
if rp.scheme != "" {
|
|
|
|
scheme = rp.scheme
|
|
|
|
}
|
|
|
|
_ = st.enc.WriteField(pair(":scheme", scheme))
|
2015-01-18 15:50:20 +01:00
|
|
|
|
2015-04-10 14:28:12 +02:00
|
|
|
authority := st.authority
|
|
|
|
if rp.authority != "" {
|
|
|
|
authority = rp.authority
|
|
|
|
}
|
|
|
|
_ = st.enc.WriteField(pair(":authority", authority))
|
2015-01-18 15:50:20 +01:00
|
|
|
|
2015-04-10 14:28:12 +02:00
|
|
|
path := "/"
|
|
|
|
if rp.path != "" {
|
|
|
|
path = rp.path
|
2015-03-08 11:31:43 +01:00
|
|
|
}
|
2015-04-10 14:28:12 +02:00
|
|
|
_ = st.enc.WriteField(pair(":path", path))
|
2015-03-08 11:31:43 +01:00
|
|
|
|
2015-04-10 14:28:12 +02:00
|
|
|
_ = st.enc.WriteField(pair("test-case", rp.name))
|
|
|
|
|
|
|
|
for _, h := range rp.header {
|
2015-03-08 11:31:43 +01:00
|
|
|
_ = st.enc.WriteField(h)
|
|
|
|
}
|
2015-04-10 14:28:12 +02:00
|
|
|
|
2015-03-08 11:31:43 +01:00
|
|
|
err := st.fr.WriteHeaders(http2.HeadersFrameParam{
|
|
|
|
StreamID: id,
|
2016-06-23 15:21:12 +02:00
|
|
|
EndStream: len(rp.body) == 0 && len(rp.trailer) == 0 && !rp.noEndStream,
|
2015-03-08 11:31:43 +01:00
|
|
|
EndHeaders: true,
|
|
|
|
BlockFragment: st.headerBlkBuf.Bytes(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-18 15:50:20 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:28:12 +02:00
|
|
|
if len(rp.body) != 0 {
|
|
|
|
// TODO we assume rp.body fits in 1 frame
|
2016-06-23 15:21:12 +02:00
|
|
|
if err := st.fr.WriteData(id, len(rp.trailer) == 0 && !rp.noEndStream, rp.body); err != nil {
|
2015-04-10 14:28:12 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rp.trailer) != 0 {
|
|
|
|
st.headerBlkBuf.Reset()
|
|
|
|
for _, h := range rp.trailer {
|
|
|
|
_ = st.enc.WriteField(h)
|
|
|
|
}
|
|
|
|
err := st.fr.WriteHeaders(http2.HeadersFrameParam{
|
|
|
|
StreamID: id,
|
|
|
|
EndStream: true,
|
|
|
|
EndHeaders: true,
|
|
|
|
BlockFragment: st.headerBlkBuf.Bytes(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
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
|
|
|
|
}
|
2015-02-08 07:56:34 +01:00
|
|
|
sr, ok := streams[f.FrameHeader.StreamID]
|
|
|
|
if !ok {
|
2015-01-18 15:50:20 +01:00
|
|
|
st.header = make(http.Header)
|
|
|
|
break
|
|
|
|
}
|
2015-02-08 07:56:34 +01:00
|
|
|
sr.header = cloneHeader(st.header)
|
2015-01-20 17:03:56 +01:00
|
|
|
var status int
|
2015-02-08 07:56:34 +01:00
|
|
|
status, err = strconv.Atoi(sr.header.Get(":status"))
|
2015-01-18 15:50:20 +01:00
|
|
|
if err != nil {
|
2022-05-24 11:53:08 +02:00
|
|
|
return res, fmt.Errorf("Error parsing status code: %w", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
2015-02-08 07:56:34 +01:00
|
|
|
sr.status = status
|
2015-01-18 15:50:20 +01:00
|
|
|
if f.StreamEnded() {
|
2015-02-08 07:56:34 +01:00
|
|
|
if streamEnded(res, streams, sr) {
|
|
|
|
break loop
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
2015-02-08 07:56:34 +01:00
|
|
|
case *http2.PushPromiseFrame:
|
|
|
|
_, err := st.dec.Write(f.HeaderBlockFragment())
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
sr := &serverResponse{
|
|
|
|
streamID: f.PromiseID,
|
|
|
|
reqHeader: cloneHeader(st.header),
|
|
|
|
}
|
|
|
|
streams[sr.streamID] = sr
|
2015-01-18 15:50:20 +01:00
|
|
|
case *http2.DataFrame:
|
2015-02-08 07:56:34 +01:00
|
|
|
sr, ok := streams[f.FrameHeader.StreamID]
|
|
|
|
if !ok {
|
2015-01-18 15:50:20 +01:00
|
|
|
break
|
|
|
|
}
|
2015-02-08 07:56:34 +01:00
|
|
|
sr.body = append(sr.body, f.Data()...)
|
2015-01-18 15:50:20 +01:00
|
|
|
if f.StreamEnded() {
|
2015-02-08 07:56:34 +01:00
|
|
|
if streamEnded(res, streams, sr) {
|
|
|
|
break loop
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
case *http2.RSTStreamFrame:
|
2015-02-08 07:56:34 +01:00
|
|
|
sr, ok := streams[f.FrameHeader.StreamID]
|
|
|
|
if !ok {
|
2015-01-18 15:50:20 +01:00
|
|
|
break
|
|
|
|
}
|
2015-02-08 07:56:34 +01:00
|
|
|
sr.errCode = f.ErrCode
|
|
|
|
if streamEnded(res, streams, sr) {
|
|
|
|
break loop
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
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-02-08 07:56:34 +01:00
|
|
|
sort.Sort(ByStreamID(res.pushResponse))
|
2015-01-18 15:50:20 +01:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2015-02-08 07:56:34 +01:00
|
|
|
func streamEnded(mainSr *serverResponse, streams map[uint32]*serverResponse, sr *serverResponse) bool {
|
|
|
|
delete(streams, sr.streamID)
|
|
|
|
if mainSr.streamID != sr.streamID {
|
|
|
|
mainSr.pushResponse = append(mainSr.pushResponse, sr)
|
|
|
|
}
|
|
|
|
return len(streams) == 0
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:50:20 +01:00
|
|
|
type serverResponse struct {
|
2019-08-06 13:44:15 +02:00
|
|
|
status int // HTTP status code
|
|
|
|
header http.Header // response header fields
|
|
|
|
body []byte // response body
|
|
|
|
streamID uint32 // stream ID in HTTP/2
|
|
|
|
errCode http2.ErrCode // error code received in HTTP/2 RST_STREAM or GOAWAY
|
|
|
|
connErr bool // true if HTTP/2 connection error
|
|
|
|
connClose bool // Connection: close is included in response header in HTTP/1 test
|
|
|
|
reqHeader http.Header // http request header, currently only sotres pushed request header
|
|
|
|
pushResponse []*serverResponse // pushed response
|
2015-02-08 07:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ByStreamID []*serverResponse
|
|
|
|
|
|
|
|
func (b ByStreamID) Len() int {
|
|
|
|
return len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b ByStreamID) Swap(i, j int) {
|
|
|
|
b[i], b[j] = b[j], b[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b ByStreamID) Less(i, j int) bool {
|
|
|
|
return b[i].streamID < b[j].streamID
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:48:50 +02:00
|
|
|
func noopHandler(w http.ResponseWriter, r *http.Request) {
|
2022-05-24 11:52:30 +02:00
|
|
|
io.ReadAll(r.Body)
|
2019-08-06 13:48:50 +02:00
|
|
|
}
|
2016-06-05 15:51:28 +02:00
|
|
|
|
|
|
|
type APIResponse struct {
|
2017-02-19 15:19:53 +01:00
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
Code int `json:"code,omitempty"`
|
|
|
|
Data map[string]interface{} `json:"data,omitempty"`
|
2016-06-05 15:51:28 +02:00
|
|
|
}
|
2020-04-18 09:31:28 +02:00
|
|
|
|
|
|
|
type proxyProtocolV2 struct {
|
|
|
|
command proxyProtocolV2Command
|
|
|
|
sourceAddress net.Addr
|
|
|
|
destinationAddress net.Addr
|
|
|
|
additionalData []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type proxyProtocolV2Command int
|
|
|
|
|
|
|
|
const (
|
|
|
|
proxyProtocolV2CommandLocal proxyProtocolV2Command = 0x0
|
|
|
|
proxyProtocolV2CommandProxy proxyProtocolV2Command = 0x1
|
|
|
|
)
|
|
|
|
|
|
|
|
type proxyProtocolV2Family int
|
|
|
|
|
|
|
|
const (
|
|
|
|
proxyProtocolV2FamilyUnspec proxyProtocolV2Family = 0x0
|
|
|
|
proxyProtocolV2FamilyInet proxyProtocolV2Family = 0x1
|
|
|
|
proxyProtocolV2FamilyInet6 proxyProtocolV2Family = 0x2
|
|
|
|
proxyProtocolV2FamilyUnix proxyProtocolV2Family = 0x3
|
|
|
|
)
|
|
|
|
|
|
|
|
type proxyProtocolV2Protocol int
|
|
|
|
|
|
|
|
const (
|
|
|
|
proxyProtocolV2ProtocolUnspec proxyProtocolV2Protocol = 0x0
|
|
|
|
proxyProtocolV2ProtocolStream proxyProtocolV2Protocol = 0x1
|
|
|
|
proxyProtocolV2ProtocolDgram proxyProtocolV2Protocol = 0x2
|
|
|
|
)
|
|
|
|
|
|
|
|
func writeProxyProtocolV2(w io.Writer, hdr proxyProtocolV2) {
|
|
|
|
w.Write([]byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A})
|
|
|
|
w.Write([]byte{byte(0x20 | hdr.command)})
|
|
|
|
|
|
|
|
switch srcAddr := hdr.sourceAddress.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
dstAddr := hdr.destinationAddress.(*net.TCPAddr)
|
|
|
|
if len(srcAddr.IP) != len(dstAddr.IP) {
|
|
|
|
panic("len(srcAddr.IP) != len(dstAddr.IP)")
|
|
|
|
}
|
|
|
|
var fam byte
|
|
|
|
if len(srcAddr.IP) == 4 {
|
|
|
|
fam = byte(proxyProtocolV2FamilyInet << 4)
|
|
|
|
} else {
|
|
|
|
fam = byte(proxyProtocolV2FamilyInet6 << 4)
|
|
|
|
}
|
|
|
|
fam |= byte(proxyProtocolV2ProtocolStream)
|
|
|
|
w.Write([]byte{fam})
|
|
|
|
length := uint16(len(srcAddr.IP)*2 + 4 + len(hdr.additionalData))
|
|
|
|
binary.Write(w, binary.BigEndian, length)
|
|
|
|
w.Write(srcAddr.IP)
|
|
|
|
w.Write(dstAddr.IP)
|
|
|
|
binary.Write(w, binary.BigEndian, uint16(srcAddr.Port))
|
|
|
|
binary.Write(w, binary.BigEndian, uint16(dstAddr.Port))
|
|
|
|
case *net.UnixAddr:
|
|
|
|
dstAddr := hdr.destinationAddress.(*net.UnixAddr)
|
|
|
|
if len(srcAddr.Name) > 108 {
|
|
|
|
panic("too long Unix source address")
|
|
|
|
}
|
|
|
|
if len(dstAddr.Name) > 108 {
|
|
|
|
panic("too long Unix destination address")
|
|
|
|
}
|
|
|
|
fam := byte(proxyProtocolV2FamilyUnix << 4)
|
|
|
|
switch srcAddr.Net {
|
|
|
|
case "unix":
|
|
|
|
fam |= byte(proxyProtocolV2ProtocolStream)
|
|
|
|
case "unixdgram":
|
|
|
|
fam |= byte(proxyProtocolV2ProtocolDgram)
|
|
|
|
default:
|
|
|
|
fam |= byte(proxyProtocolV2ProtocolUnspec)
|
|
|
|
}
|
|
|
|
w.Write([]byte{fam})
|
|
|
|
length := uint16(216 + len(hdr.additionalData))
|
|
|
|
binary.Write(w, binary.BigEndian, length)
|
|
|
|
zeros := make([]byte, 108)
|
|
|
|
w.Write([]byte(srcAddr.Name))
|
|
|
|
w.Write(zeros[:108-len(srcAddr.Name)])
|
|
|
|
w.Write([]byte(dstAddr.Name))
|
|
|
|
w.Write(zeros[:108-len(dstAddr.Name)])
|
|
|
|
default:
|
|
|
|
fam := byte(proxyProtocolV2FamilyUnspec<<4) | byte(proxyProtocolV2ProtocolUnspec)
|
|
|
|
w.Write([]byte{fam})
|
|
|
|
length := uint16(len(hdr.additionalData))
|
|
|
|
binary.Write(w, binary.BigEndian, length)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(hdr.additionalData)
|
|
|
|
}
|