From 484fbb54a2ed2a9af4ec47c205fc3e0b286bc042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Fri, 13 Sep 2024 08:22:22 +0000 Subject: [PATCH] Fix test --- common/protocol/http/sniff.go | 6 ++++-- common/protocol/http/sniff_test.go | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/common/protocol/http/sniff.go b/common/protocol/http/sniff.go index eb561b789ed5..9476f18a283a 100644 --- a/common/protocol/http/sniff.go +++ b/common/protocol/http/sniff.go @@ -80,7 +80,9 @@ func SniffHTTP(b []byte, c context.Context) (*SniffHeader, error) { } key := strings.ToLower(string(parts[0])) value := string(bytes.TrimSpace(parts[1])) - content.SetAttribute(key, value) // Put header in attribute + if content != nil { + content.SetAttribute(key, value) // Put header in attribute + } if key == "host" { rawHost := strings.ToLower(value) dest, err := ParseHost(rawHost, net.Port(80)) @@ -93,7 +95,7 @@ func SniffHTTP(b []byte, c context.Context) (*SniffHeader, error) { // Parse request line // Request line is like this // "GET /homo/114514 HTTP/1.1" - if len(headers) > 0 { + if len(headers) > 0 && content != nil { RequestLineParts := bytes.Split(headers[0], []byte{' '}) if len(RequestLineParts) == 3 { content.SetAttribute(":method", string(RequestLineParts[0])) diff --git a/common/protocol/http/sniff_test.go b/common/protocol/http/sniff_test.go index fff66415d1b2..09ce7d6c12ef 100644 --- a/common/protocol/http/sniff_test.go +++ b/common/protocol/http/sniff_test.go @@ -1,6 +1,7 @@ package http_test import ( + "context" "testing" . "github.com/xtls/xray-core/common/protocol/http" @@ -88,7 +89,7 @@ first_name=John&last_name=Doe&action=Submit`, } for _, test := range cases { - header, err := SniffHTTP([]byte(test.input)) + header, err := SniffHTTP([]byte(test.input), context.TODO()) if test.err { if err == nil { t.Errorf("Expect error but nil, in test: %v", test)