|
|
|
@ -19,12 +19,12 @@ package http |
|
|
|
|
import ( |
|
|
|
|
"bufio" |
|
|
|
|
"bytes" |
|
|
|
|
"context" |
|
|
|
|
"crypto/tls" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"io" |
|
|
|
|
"net" |
|
|
|
|
"net/http" |
|
|
|
|
"runtime" |
|
|
|
|
"strconv" |
|
|
|
|
"strings" |
|
|
|
@ -212,7 +212,7 @@ func TestNewHTTPListener(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
remoteUnknownErr := "lookup unknown-host" + errMsg |
|
|
|
|
if runtime.GOOS == "wpindows" { |
|
|
|
|
if runtime.GOOS == "windows" { |
|
|
|
|
remoteUnknownErr = "listen tcp: lookup unknown-host" + errMsg |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -224,19 +224,18 @@ func TestNewHTTPListener(t *testing.T) { |
|
|
|
|
tcpKeepAliveTimeout time.Duration |
|
|
|
|
readTimeout time.Duration |
|
|
|
|
writeTimeout time.Duration |
|
|
|
|
updateBytesReadFunc func(int) |
|
|
|
|
updateBytesWrittenFunc func(int) |
|
|
|
|
errorLogFunc func(context.Context, error) |
|
|
|
|
updateBytesReadFunc func(*http.Request, int) |
|
|
|
|
updateBytesWrittenFunc func(*http.Request, int) |
|
|
|
|
expectedErr error |
|
|
|
|
}{ |
|
|
|
|
{[]string{"93.184.216.34:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, errors.New(remoteAddrErrMsgIP)}, |
|
|
|
|
{[]string{"example.org:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, errors.New(remoteAddrErrMsgHost)}, |
|
|
|
|
{[]string{"unknown-host"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, errors.New(remoteMissingErr)}, |
|
|
|
|
{[]string{"unknown-host:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, errors.New(remoteUnknownErr)}, |
|
|
|
|
{[]string{"localhost:65432", "93.184.216.34:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, errors.New(remoteAddrErrMsgIP)}, |
|
|
|
|
{[]string{"localhost:65432", "unknown-host:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, errors.New(remoteUnknownErr)}, |
|
|
|
|
{[]string{"localhost:0"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, nil}, |
|
|
|
|
{[]string{"localhost:0"}, tlsConfig, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil, nil}, |
|
|
|
|
{[]string{"93.184.216.34:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, errors.New(remoteAddrErrMsgIP)}, |
|
|
|
|
{[]string{"example.org:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, errors.New(remoteAddrErrMsgHost)}, |
|
|
|
|
{[]string{"unknown-host"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, errors.New(remoteMissingErr)}, |
|
|
|
|
{[]string{"unknown-host:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, errors.New(remoteUnknownErr)}, |
|
|
|
|
{[]string{"localhost:65432", "93.184.216.34:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, errors.New(remoteAddrErrMsgIP)}, |
|
|
|
|
{[]string{"localhost:65432", "unknown-host:65432"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, errors.New(remoteUnknownErr)}, |
|
|
|
|
{[]string{"localhost:0"}, nil, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil}, |
|
|
|
|
{[]string{"localhost:0"}, tlsConfig, time.Duration(0), time.Duration(0), time.Duration(0), nil, nil, nil}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, testCase := range testCases { |
|
|
|
@ -246,6 +245,7 @@ func TestNewHTTPListener(t *testing.T) { |
|
|
|
|
testCase.tcpKeepAliveTimeout, |
|
|
|
|
testCase.readTimeout, |
|
|
|
|
testCase.writeTimeout, |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
testCase.updateBytesReadFunc, |
|
|
|
|
testCase.updateBytesWrittenFunc, |
|
|
|
|
) |
|
|
|
@ -297,6 +297,7 @@ func TestHTTPListenerStartClose(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -344,6 +345,7 @@ func TestHTTPListenerAddr(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -388,6 +390,7 @@ func TestHTTPListenerAddrs(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -418,13 +421,14 @@ func TestHTTPListenerAccept(t *testing.T) { |
|
|
|
|
tlsConfig *tls.Config |
|
|
|
|
request string |
|
|
|
|
reply string |
|
|
|
|
expectedRequestLine string |
|
|
|
|
}{ |
|
|
|
|
{[]string{"localhost:0"}, nil, "GET / HTTP/1.0\n", "200 OK\n"}, |
|
|
|
|
{[]string{nonLoopBackIP + ":0"}, nil, "POST / HTTP/1.0\n", "200 OK\n"}, |
|
|
|
|
{[]string{"127.0.0.1:0", nonLoopBackIP + ":0"}, nil, "CONNECT \n", "200 OK\n"}, |
|
|
|
|
{[]string{"localhost:0"}, tlsConfig, "GET / HTTP/1.0\n", "200 OK\n"}, |
|
|
|
|
{[]string{nonLoopBackIP + ":0"}, tlsConfig, "POST / HTTP/1.0\n", "200 OK\n"}, |
|
|
|
|
{[]string{"127.0.0.1:0", nonLoopBackIP + ":0"}, tlsConfig, "CONNECT \n", "200 OK\n"}, |
|
|
|
|
{[]string{"localhost:0"}, nil, "GET / HTTP/1.0\r\nHost: example.org\r\n\r\n", "200 OK\r\n", "GET / HTTP/1.0\r\n"}, |
|
|
|
|
{[]string{nonLoopBackIP + ":0"}, nil, "POST / HTTP/1.0\r\nHost: example.org\r\n\r\n", "200 OK\r\n", "POST / HTTP/1.0\r\n"}, |
|
|
|
|
{[]string{"127.0.0.1:0", nonLoopBackIP + ":0"}, nil, "CONNECT \r\nHost: www.example.org\r\n\r\n", "200 OK\r\n", "CONNECT \r\n"}, |
|
|
|
|
{[]string{"localhost:0"}, tlsConfig, "GET / HTTP/1.0\r\nHost: example.org\r\n\r\n", "200 OK\r\n", "GET / HTTP/1.0\r\n"}, |
|
|
|
|
{[]string{nonLoopBackIP + ":0"}, tlsConfig, "POST / HTTP/1.0\r\nHost: example.org\r\n\r\n", "200 OK\r\n", "POST / HTTP/1.0\r\n"}, |
|
|
|
|
{[]string{"127.0.0.1:0", nonLoopBackIP + ":0"}, tlsConfig, "CONNECT \r\nHost: www.example.org\r\n\r\n", "200 OK\r\n", "CONNECT \r\n"}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, testCase := range testCases { |
|
|
|
@ -434,6 +438,7 @@ func TestHTTPListenerAccept(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -463,13 +468,13 @@ func TestHTTPListenerAccept(t *testing.T) { |
|
|
|
|
t.Fatalf("Test %d: accept: expected = <nil>, got = %v", i+1, err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
request, err := bufio.NewReader(serverConn).ReadString('\n') |
|
|
|
|
requestLine, err := bufio.NewReader(serverConn).ReadString('\n') |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatalf("Test %d: request read: expected = <nil>, got = %v", i+1, err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if testCase.request != request { |
|
|
|
|
t.Fatalf("Test %d: request: expected = %v, got = %v", i+1, testCase.request, request) |
|
|
|
|
if requestLine != testCase.expectedRequestLine { |
|
|
|
|
t.Fatalf("Test %d: request: expected = %v, got = %v", i+1, testCase.expectedRequestLine, requestLine) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, err = io.WriteString(serverConn, testCase.reply); err != nil { |
|
|
|
@ -513,6 +518,7 @@ func TestHTTPListenerAcceptPeekError(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -566,6 +572,7 @@ func TestHTTPListenerAcceptTLSError(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -632,6 +639,7 @@ func TestHTTPListenerAcceptError(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -757,6 +765,7 @@ func TestHTTPListenerAcceptParallel(t *testing.T) { |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
time.Duration(0), |
|
|
|
|
DefaultMaxHeaderBytes, |
|
|
|
|
nil, |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
@ -765,8 +774,8 @@ func TestHTTPListenerAcceptParallel(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, serverAddr := range listener.Addrs() { |
|
|
|
|
go connect(i, serverAddr.String(), testCase.tlsConfig != nil, true, "GET /1 HTTP/1.0\n", testCase.reply) |
|
|
|
|
go connect(i, serverAddr.String(), testCase.tlsConfig != nil, false, "GET /2 HTTP/1.0\n", testCase.reply) |
|
|
|
|
go connect(i, serverAddr.String(), testCase.tlsConfig != nil, true, "GET /1 HTTP/1.0\r\nHost: example.org\r\nr\n", testCase.reply) |
|
|
|
|
go connect(i, serverAddr.String(), testCase.tlsConfig != nil, false, "GET /2 HTTP/1.0\r\nHost: example.org\r\n\r\n", testCase.reply) |
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup |
|
|
|
|
|
|
|
|
@ -775,14 +784,14 @@ func TestHTTPListenerAcceptParallel(t *testing.T) { |
|
|
|
|
t.Fatalf("Test %d: accept: expected = <nil>, got = %v", i+1, err) |
|
|
|
|
} |
|
|
|
|
wg.Add(1) |
|
|
|
|
go handleConnection(i, &wg, serverConn, "GET /2 HTTP/1.0\n", testCase.reply) |
|
|
|
|
go handleConnection(i, &wg, serverConn, "GET /2 HTTP/1.0\r\n", testCase.reply) |
|
|
|
|
|
|
|
|
|
serverConn, err = listener.Accept() |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatalf("Test %d: accept: expected = <nil>, got = %v", i+1, err) |
|
|
|
|
} |
|
|
|
|
wg.Add(1) |
|
|
|
|
go handleConnection(i, &wg, serverConn, "GET /1 HTTP/1.0\n", testCase.reply) |
|
|
|
|
go handleConnection(i, &wg, serverConn, "GET /1 HTTP/1.0\r\n", testCase.reply) |
|
|
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
|
} |
|
|
|
|