2016-11-04 16:59:19 -04:00
|
|
|
package http_test
|
|
|
|
|
|
|
|
import (
|
2020-06-03 03:16:03 -04:00
|
|
|
"bufio"
|
2018-11-07 11:16:57 -05:00
|
|
|
"bytes"
|
2017-01-13 07:47:44 -05:00
|
|
|
"context"
|
2018-11-07 11:16:57 -05:00
|
|
|
"crypto/rand"
|
2020-06-03 03:16:03 -04:00
|
|
|
"strings"
|
2016-11-04 16:59:19 -04:00
|
|
|
"testing"
|
2016-12-16 10:52:11 -05:00
|
|
|
"time"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
. "github.com/v2fly/v2ray-core/v4/transport/internet/headers/http"
|
2016-11-04 16:59:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReaderWriter(t *testing.T) {
|
2016-12-09 06:08:25 -05:00
|
|
|
cache := buf.New()
|
2018-08-16 06:05:33 -04:00
|
|
|
b := buf.New()
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(b.WriteString("abcd" + ENDING))
|
2016-12-06 05:03:42 -05:00
|
|
|
writer := NewHeaderWriter(b)
|
2016-12-16 10:52:11 -05:00
|
|
|
err := writer.Write(cache)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
if v := cache.Len(); v != 8 {
|
|
|
|
t.Error("cache len: ", v)
|
|
|
|
}
|
2016-12-16 10:52:11 -05:00
|
|
|
_, err = cache.Write([]byte{'e', 'f', 'g'})
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-11-04 16:59:19 -04:00
|
|
|
|
|
|
|
reader := &HeaderReader{}
|
2020-11-19 12:02:52 -05:00
|
|
|
_, err = reader.Read(cache)
|
2020-06-03 03:16:03 -04:00
|
|
|
if err != nil && !strings.HasPrefix(err.Error(), "malformed HTTP request") {
|
|
|
|
t.Error("unknown error ", err)
|
2019-02-03 13:46:53 -05:00
|
|
|
}
|
2016-11-04 16:59:19 -04:00
|
|
|
}
|
2016-11-07 04:47:30 -05:00
|
|
|
|
|
|
|
func TestRequestHeader(t *testing.T) {
|
2020-10-11 07:22:46 -04:00
|
|
|
auth, err := NewAuthenticator(context.Background(), &Config{
|
2016-11-07 04:47:30 -05:00
|
|
|
Request: &RequestConfig{
|
|
|
|
Uri: []string{"/"},
|
|
|
|
Header: []*Header{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Value: []string{"Value"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-01-13 07:47:44 -05:00
|
|
|
})
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-11-07 04:47:30 -05:00
|
|
|
|
2016-12-09 06:08:25 -05:00
|
|
|
cache := buf.New()
|
2017-01-13 07:47:44 -05:00
|
|
|
err = auth.GetClientWriter().Write(cache)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-11-07 04:47:30 -05:00
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if cache.String() != "GET / HTTP/1.1\r\nTest: Value\r\n\r\n" {
|
|
|
|
t.Error("cache: ", cache.String())
|
|
|
|
}
|
2016-11-07 04:47:30 -05:00
|
|
|
}
|
2016-12-16 10:52:11 -05:00
|
|
|
|
2018-11-07 11:16:57 -05:00
|
|
|
func TestLongRequestHeader(t *testing.T) {
|
|
|
|
payload := make([]byte, buf.Size+2)
|
|
|
|
common.Must2(rand.Read(payload[:buf.Size-2]))
|
2020-10-11 07:22:46 -04:00
|
|
|
copy(payload[buf.Size-2:], ENDING)
|
2018-11-07 11:16:57 -05:00
|
|
|
payload = append(payload, []byte("abcd")...)
|
|
|
|
|
|
|
|
reader := HeaderReader{}
|
2020-11-19 12:02:52 -05:00
|
|
|
_, err := reader.Read(bytes.NewReader(payload))
|
2020-06-03 03:16:03 -04:00
|
|
|
|
|
|
|
if err != nil && !(strings.HasPrefix(err.Error(), "invalid") || strings.HasPrefix(err.Error(), "malformed")) {
|
|
|
|
t.Error("unknown error ", err)
|
2018-11-07 11:16:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 10:52:11 -05:00
|
|
|
func TestConnection(t *testing.T) {
|
2020-10-11 07:22:46 -04:00
|
|
|
auth, err := NewAuthenticator(context.Background(), &Config{
|
2017-12-03 17:41:01 -05:00
|
|
|
Request: &RequestConfig{
|
|
|
|
Method: &Method{Value: "Post"},
|
|
|
|
Uri: []string{"/testpath"},
|
|
|
|
Header: []*Header{
|
|
|
|
{
|
|
|
|
Name: "Host",
|
2021-02-16 15:31:50 -05:00
|
|
|
Value: []string{"www.v2fly.org", "www.google.com"},
|
2017-12-03 17:41:01 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "User-Agent",
|
|
|
|
Value: []string{"Test-Agent"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Response: &ResponseConfig{
|
|
|
|
Version: &Version{
|
|
|
|
Value: "1.1",
|
|
|
|
},
|
|
|
|
Status: &Status{
|
|
|
|
Code: "404",
|
|
|
|
Reason: "Not Found",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-12-16 10:52:11 -05:00
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-12-16 10:52:11 -05:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
conn, err := listener.Accept()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-12-16 10:52:11 -05:00
|
|
|
authConn := auth.Server(conn)
|
|
|
|
b := make([]byte, 256)
|
|
|
|
for {
|
|
|
|
n, err := authConn.Read(b)
|
2019-02-03 13:46:53 -05:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2016-12-16 10:52:11 -05:00
|
|
|
_, err = authConn.Write(b[:n])
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-12-16 10:52:11 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-12-16 10:52:11 -05:00
|
|
|
|
|
|
|
authConn := auth.Client(conn)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer authConn.Close()
|
|
|
|
|
2016-12-16 10:52:11 -05:00
|
|
|
authConn.Write([]byte("Test payload"))
|
|
|
|
authConn.Write([]byte("Test payload 2"))
|
|
|
|
|
|
|
|
expectedResponse := "Test payloadTest payload 2"
|
|
|
|
actualResponse := make([]byte, 256)
|
|
|
|
deadline := time.Now().Add(time.Second * 5)
|
|
|
|
totalBytes := 0
|
|
|
|
for {
|
|
|
|
n, err := authConn.Read(actualResponse[totalBytes:])
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-12-16 10:52:11 -05:00
|
|
|
totalBytes += n
|
|
|
|
if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if string(actualResponse[:totalBytes]) != expectedResponse {
|
|
|
|
t.Error("response: ", string(actualResponse[:totalBytes]))
|
|
|
|
}
|
2016-12-16 10:52:11 -05:00
|
|
|
}
|
2020-06-03 03:16:03 -04:00
|
|
|
|
|
|
|
func TestConnectionInvPath(t *testing.T) {
|
2020-10-11 07:22:46 -04:00
|
|
|
auth, err := NewAuthenticator(context.Background(), &Config{
|
2020-06-03 03:16:03 -04:00
|
|
|
Request: &RequestConfig{
|
|
|
|
Method: &Method{Value: "Post"},
|
|
|
|
Uri: []string{"/testpath"},
|
|
|
|
Header: []*Header{
|
|
|
|
{
|
|
|
|
Name: "Host",
|
2021-02-16 15:31:50 -05:00
|
|
|
Value: []string{"www.v2fly.org", "www.google.com"},
|
2020-06-03 03:16:03 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "User-Agent",
|
|
|
|
Value: []string{"Test-Agent"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Response: &ResponseConfig{
|
|
|
|
Version: &Version{
|
|
|
|
Value: "1.1",
|
|
|
|
},
|
|
|
|
Status: &Status{
|
|
|
|
Code: "404",
|
|
|
|
Reason: "Not Found",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
authR, err := NewAuthenticator(context.Background(), &Config{
|
2020-06-03 03:16:03 -04:00
|
|
|
Request: &RequestConfig{
|
|
|
|
Method: &Method{Value: "Post"},
|
|
|
|
Uri: []string{"/testpathErr"},
|
|
|
|
Header: []*Header{
|
|
|
|
{
|
|
|
|
Name: "Host",
|
2021-02-16 15:31:50 -05:00
|
|
|
Value: []string{"www.v2fly.org", "www.google.com"},
|
2020-06-03 03:16:03 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "User-Agent",
|
|
|
|
Value: []string{"Test-Agent"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Response: &ResponseConfig{
|
|
|
|
Version: &Version{
|
|
|
|
Value: "1.1",
|
|
|
|
},
|
|
|
|
Status: &Status{
|
|
|
|
Code: "404",
|
|
|
|
Reason: "Not Found",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
common.Must(err)
|
|
|
|
authConn := auth.Server(conn)
|
|
|
|
b := make([]byte, 256)
|
|
|
|
for {
|
|
|
|
n, err := authConn.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
authConn.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
_, err = authConn.Write(b[:n])
|
|
|
|
common.Must(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
authConn := authR.Client(conn)
|
|
|
|
defer authConn.Close()
|
|
|
|
|
|
|
|
authConn.Write([]byte("Test payload"))
|
|
|
|
authConn.Write([]byte("Test payload 2"))
|
|
|
|
|
|
|
|
expectedResponse := "Test payloadTest payload 2"
|
|
|
|
actualResponse := make([]byte, 256)
|
|
|
|
deadline := time.Now().Add(time.Second * 5)
|
|
|
|
totalBytes := 0
|
|
|
|
for {
|
|
|
|
n, err := authConn.Read(actualResponse[totalBytes:])
|
2020-06-03 04:32:58 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Error Expected", err)
|
|
|
|
} else {
|
|
|
|
return
|
2020-06-03 03:16:03 -04:00
|
|
|
}
|
|
|
|
totalBytes += n
|
|
|
|
if totalBytes >= len(expectedResponse) || time.Now().After(deadline) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectionInvReq(t *testing.T) {
|
2020-10-11 07:22:46 -04:00
|
|
|
auth, err := NewAuthenticator(context.Background(), &Config{
|
2020-06-03 03:16:03 -04:00
|
|
|
Request: &RequestConfig{
|
|
|
|
Method: &Method{Value: "Post"},
|
|
|
|
Uri: []string{"/testpath"},
|
|
|
|
Header: []*Header{
|
|
|
|
{
|
|
|
|
Name: "Host",
|
2021-02-16 15:31:50 -05:00
|
|
|
Value: []string{"www.v2fly.org", "www.google.com"},
|
2020-06-03 03:16:03 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "User-Agent",
|
|
|
|
Value: []string{"Test-Agent"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Response: &ResponseConfig{
|
|
|
|
Version: &Version{
|
|
|
|
Value: "1.1",
|
|
|
|
},
|
|
|
|
Status: &Status{
|
|
|
|
Code: "404",
|
|
|
|
Reason: "Not Found",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
common.Must(err)
|
|
|
|
authConn := auth.Server(conn)
|
|
|
|
b := make([]byte, 256)
|
|
|
|
for {
|
|
|
|
n, err := authConn.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
authConn.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
_, err = authConn.Write(b[:n])
|
|
|
|
common.Must(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
conn.Write([]byte("ABCDEFGHIJKMLN\r\n\r\n"))
|
|
|
|
l, _, err := bufio.NewReader(conn).ReadLine()
|
|
|
|
common.Must(err)
|
|
|
|
if !strings.HasPrefix(string(l), "HTTP/1.1 400 Bad Request") {
|
|
|
|
t.Error("Resp to non http conn", string(l))
|
|
|
|
}
|
|
|
|
}
|