mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-09-15 22:10:26 +00:00
XHTTP client: Define Request.GetBody() for packet-up so h2 can replay after GOAWAY (#6632)
https://github.com/XTLS/Xray-core/pull/6632#issuecomment-5430735467 --------- Co-authored-by: 风扇滑翔翼 <Fangliding.fshxy@outlook.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package splithttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -330,14 +331,18 @@ func (c *Config) FillStreamRequest(request *http.Request, sessionId string, seqS
|
||||
func (c *Config) FillPacketRequest(request *http.Request, sessionId string, seqStr string, payload buf.MultiBuffer) error {
|
||||
dataPlacement := c.GetNormalizedUplinkDataPlacement()
|
||||
|
||||
data := make([]byte, payload.Len())
|
||||
payload.Copy(data)
|
||||
buf.ReleaseMulti(payload)
|
||||
|
||||
if dataPlacement == PlacementBody || dataPlacement == PlacementAuto {
|
||||
request.Header = c.GetRequestHeader()
|
||||
request.Body = io.NopCloser(&buf.MultiBufferContainer{MultiBuffer: payload})
|
||||
request.ContentLength = int64(payload.Len())
|
||||
request.Body = io.NopCloser(bytes.NewReader(data))
|
||||
request.ContentLength = int64(len(data))
|
||||
request.GetBody = func() (io.ReadCloser, error) {
|
||||
return io.NopCloser(bytes.NewReader(data)), nil
|
||||
}
|
||||
} else {
|
||||
data := make([]byte, payload.Len())
|
||||
payload.Copy(data)
|
||||
buf.ReleaseMulti(payload)
|
||||
switch dataPlacement {
|
||||
case PlacementHeader:
|
||||
request.Header = c.GetRequestHeaderWithPayload(data)
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package splithttp_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
. "github.com/xtls/xray-core/transport/internet/splithttp"
|
||||
)
|
||||
|
||||
@@ -77,3 +81,35 @@ func Test_GetNormalizedPath(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_FillPacketRequest_GetBody(t *testing.T) {
|
||||
data := []byte("hello xray")
|
||||
payload := buf.MergeBytes(nil, data)
|
||||
|
||||
req, err := http.NewRequest("POST", "https://example.com/", nil)
|
||||
common.Must(err)
|
||||
|
||||
config := &Config{}
|
||||
config.FillPacketRequest(req, "sess", "0", payload)
|
||||
|
||||
if req.GetBody == nil {
|
||||
t.Fatalf("Expected GetBody to be set")
|
||||
}
|
||||
|
||||
first, err := io.ReadAll(req.Body)
|
||||
common.Must(err)
|
||||
|
||||
if string(data) != string(first) {
|
||||
t.Fatalf("Body mismatch. Format %q and %q are not equal", data, first)
|
||||
}
|
||||
|
||||
body2, err := req.GetBody()
|
||||
common.Must(err)
|
||||
|
||||
second, err := io.ReadAll(body2)
|
||||
common.Must(err)
|
||||
|
||||
if string(data) != string(second) {
|
||||
t.Fatalf("Replayed body mismatch. Format %q and %q are not equal", data, second)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user