Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions transport/internet/splithttp/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package splithttp

import (
"bytes"
"encoding/base64"
"fmt"
"io"
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions transport/internet/splithttp/config_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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)
}
}